package org.jeecg.modules.database.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.database.entity.DocumentFavorites;
import org.jeecg.modules.database.entity.DocumentLibrary;
import org.jeecg.modules.database.service.IDocumentFavoritesService;
import org.jeecg.modules.database.service.IDocumentLibraryService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Objects;

/**
 * @Description: 我的收藏文档
 * @Author: jeecg-boot
 * @Date: 2024-08-30
 * @Version: V1.0
 */
@Tag(name = "我的收藏文档")
@RestController
@RequestMapping("/database/documentFavorites")
@Slf4j
public class DocumentFavoritesController extends JeecgController<DocumentFavorites, IDocumentFavoritesService> {
    @Autowired
    private IDocumentFavoritesService documentFavoritesService;
    @Autowired
    private IDocumentLibraryService   documentLibraryService;

    @Autowired
    private ISysUserService userService;

    /**
     * 分页列表查询
     *
     * @param documentFavorites
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "我的收藏文档-分页列表查询")
    @Operation(summary = "我的收藏文档-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<DocumentFavorites>> queryPageList(DocumentFavorites documentFavorites,
                                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                          HttpServletRequest req) {

        String username = JwtUtil.getUserNameByToken(req);
        SysUser userByName = userService.getUserByName(username);
        documentFavorites.setUserId(userByName.getId());
        QueryWrapper<DocumentFavorites> queryWrapper = QueryGenerator.initQueryWrapper(documentFavorites, req.getParameterMap());
        Page<DocumentFavorites> page = new Page<>(pageNo, pageSize);
        IPage<DocumentFavorites> pageList = documentFavoritesService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param documentFavorites
     * @return
     */
    @AutoLog(value = "我的收藏文档-添加")
    @Operation(summary = "我的收藏文档-添加")
    @RequiresPermissions("database:document_favorites:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody DocumentFavorites documentFavorites, HttpServletRequest request) {
        String documentId = documentFavorites.getDocumentId();
        DocumentLibrary documentLibrary = documentLibraryService.getById(documentId);

        String username = JwtUtil.getUserNameByToken(request);
        SysUser userByName = userService.getUserByName(username);

        DocumentFavorites old = documentFavoritesService.getByDocIdAndUserId(documentId, userByName.getId());
        if (Objects.isNull(old)) {
            documentFavorites.setId(null);
        } else {
            documentFavorites.setId(old.getId());
        }
        documentFavorites.setUserId(userByName.getId());
        documentFavorites.setDocumentTitle(documentLibrary.getTitle());

        documentFavoritesService.saveOrUpdate(documentFavorites);
        return Result.OK("收藏成功！");
    }

    /**
     * 编辑
     *
     * @param documentFavorites
     * @return
     */
    @AutoLog(value = "我的收藏文档-编辑")
    @Operation(summary = "我的收藏文档-编辑")
    @RequiresPermissions("database:document_favorites:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody DocumentFavorites documentFavorites) {
        documentFavoritesService.updateById(documentFavorites);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "我的收藏文档-通过id删除")
    @Operation(summary = "我的收藏文档-通过id删除")
    @RequiresPermissions("database:document_favorites:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        documentFavoritesService.removeById(id);
        return Result.OK("取消收藏成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "我的收藏文档-批量删除")
    @Operation(summary = "我的收藏文档-批量删除")
    @RequiresPermissions("database:document_favorites:delete")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.documentFavoritesService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("取消收藏成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "我的收藏文档-通过id查询")
    @Operation(summary = "我的收藏文档-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<DocumentFavorites> queryById(@RequestParam(name = "id", required = true) String id) {
        DocumentFavorites documentFavorites = documentFavoritesService.getById(id);
        if (documentFavorites == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(documentFavorites);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param documentFavorites
     */

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DocumentFavorites documentFavorites) {
        return super.exportXls(request, documentFavorites, DocumentFavorites.class, "我的收藏文档");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("database:document_favorites:add")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DocumentFavorites.class);
    }

}
