package cn.iocoder.yudao.module.inspection.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.core.controller.BaseController;
import cn.iocoder.yudao.module.inspection.entity.InspectionFile;
import cn.iocoder.yudao.module.inspection.service.AppInspectionPartnerService;
import cn.iocoder.yudao.module.inspection.service.IInspectionFileService;
import cn.iocoder.yudao.module.inspection.vo.AssignAuthorityVo;
import cn.iocoder.yudao.module.shop.entity.ShopMallPartners;
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO;
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
import cn.iocoder.yudao.module.system.api.user.dto.UserRoleDTO;
import cn.iocoder.yudao.module.system.service.permission.PermissionServiceImpl;
import cn.iocoder.yudao.module.system.service.permission.RoleService;
import cn.iocoder.yudao.util.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * inspectionFileController
 *
 * @author zcy
 * @date 2023-10-13
 */
@RestController
@RequestMapping("/admin-api/inspectionFile/inspectionFile")
public class InspectionFileController extends BaseController {
    @Autowired
    private IInspectionFileService inspectionFileService;
    @Autowired
    private AppInspectionPartnerService partnerService;

    @Autowired
    private PermissionServiceImpl roleService;
    @Resource
    private DictDataApi dictDataApi;
    @Resource
    private PermissionApi permissionApi;

    /**
     * 查询inspectionFile列表
     */
    @GetMapping("/list")
    public CommonResult list(InspectionFile inspectionFile) throws Exception {
        ShopMallPartners partners = partnerService.shopInfoByUserId();
        inspectionFile.setPartnerId(partners.getPartnerId());

        List<InspectionFile> list = inspectionFileService.selectInspectionFileList(inspectionFile);

        return success(list);
    }

    /**
     * 查询inspectionFile列表权限
     */
    @GetMapping("/listByPermission")
    public CommonResult listByPermission(InspectionFile inspectionFile) throws Exception {
        if (ObjectUtil.isEmpty(inspectionFile.getServicePackageId())){
            inspectionFile.setServicePackageId("jiance");
        }
        //判断当前登陆人的角色是否是可以查看文件
        boolean ifFile = permissionApi.hasDictTypeRole(inspectionFile.getDictType());
        if (ifFile) {
            return success(inspectionFileService.selectInspectionFileList(inspectionFile));
        }else {
            return success(inspectionFileService.selectInspectionFileListByPermissio(inspectionFile, getLoginUser().getId()));
        }
    }

    /**
     * 导出inspectionFile列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionFile inspectionFile) {
        List<InspectionFile> list = inspectionFileService.selectInspectionFileList(inspectionFile);
        ExcelUtil<InspectionFile> util = new ExcelUtil<InspectionFile>(InspectionFile.class);
        util.exportExcel(response, list, "inspectionFile数据");
    }

    /**
     * 获取inspectionFile详细信息
     */
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable("id") Long id) {
        return success(inspectionFileService.selectInspectionFileById(id));
    }

    /**
     * 新增inspectionFile
     */
    @PostMapping("/add")
    public CommonResult add(@RequestBody InspectionFile inspectionFile) throws Exception {
        ShopMallPartners partners = partnerService.shopInfo();
        inspectionFile.setPartnerId(partners.getPartnerId());
        return success(inspectionFileService.insertInspectionFile(inspectionFile));
    }

    /**
     * 批量新增inspectionFile
     */
    @PostMapping("/addBatch")
    public CommonResult addBatch(@RequestBody List<InspectionFile> inspectionFiles) throws Exception {
        if (CollUtil.isEmpty(inspectionFiles)) {
            throw new RuntimeException("文件不能为空");
        }
        ShopMallPartners partners = partnerService.shopInfo();
        inspectionFiles.stream().forEach(inspectionFile -> inspectionFile.setPartnerId(partners.getPartnerId()));
        return success(inspectionFileService.insertBatchInspectionFile(inspectionFiles));
    }

    /**
     * 修改inspectionFile
     */
    @PostMapping("/edit")
    public CommonResult edit(@RequestBody InspectionFile inspectionFile) throws Exception {
//        if (!partners.getPartnerId().equals(file.getPartnerId())) {
//            return null;
//        }
        return toAjax(inspectionFileService.updateInspectionFile(inspectionFile));
    }

    /**
     * 删除inspectionFile
     */
    @PostMapping("/del/{id}")
    public CommonResult remove(@PathVariable Long id) throws Exception {
//        ShopMallPartners partners = partnerService.shopInfo();
//        InspectionFile file = inspectionFileService.selectInspectionFileById(id);
//        if (!partners.getPartnerId().equals(file.getPartnerId())) {
//            return null;
//        }
        return toAjax(inspectionFileService.deleteInspectionFileById(id));
    }

    /**
     * 根据id统计数量
     *
     * @param ids ids
     * @author 小李
     * @date 14:30 2024/12/14
     **/
    @GetMapping("/getCountByIds")
    public CommonResult<?> getCountByIds(@RequestParam(value = "ids", required = false) List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return null;
        }
        return success(inspectionFileService.getCountByIds(ids));
    }

    /**
     * 得到使用说明
     *
     * @author 小李
     * @date 12:31 2024/12/17
     **/
    @GetMapping("/getPresent")
    public CommonResult<?> getPresent() {
        List<InspectionFile> files = inspectionFileService.list(new LambdaQueryWrapper<InspectionFile>().eq(InspectionFile::getFileName, "使用说明"));
        if (CollUtil.isEmpty(files)) {
            return null;
        }
        InspectionFile inspectionFile = files.get(0);
        return success(inspectionFile);

    }

    /**
     * 分配权限
     *
     * @param assignAuthorityVo
     * @return
     */
    @PostMapping("/assignAuthority")
    public CommonResult assignAuthority(@RequestBody AssignAuthorityVo assignAuthorityVo) {
        return success(inspectionFileService.assignAuthority(assignAuthorityVo.getUserIds(), assignAuthorityVo.getFileId()));
    }

    /**
     * 根据文件id获取有权限的用户id
     *
     * @param fileId
     * @return
     */
    @GetMapping("/getUserIdsByFileId")
    public CommonResult getUserIdsByFileId(Integer fileId) {
        List<Long> userIds = inspectionFileService.getUserIdsByFileId(fileId);
        return success(userIds);
    }

    /**
     * 查询文件夹树
     * @return
     */
    @GetMapping("/queryTreeFolder")
    public CommonResult<?> queryTreeFolder(){
        return success(inspectionFileService.queryTreeFolder());
    }

    /**
     * 查询文件夹树
     * @return
     */
    @GetMapping("/queryTreeFolderForJx")
    public CommonResult<?> queryTreeFolderForJx(){
        return success(inspectionFileService.queryTreeFolderForJx());
    }

    /**
     * 查询文件夹树
     * @return
     */
    @GetMapping("/queryTreeFolderByServicePackageId")
    public CommonResult<?> queryTreeFolderByServicePackageId(@RequestParam String servicePackageId){
        return success(inspectionFileService.queryTreeFolderByServicePackageId(servicePackageId));
    }

    /**
     * 提醒
     */
    @RequestMapping("/warnReminder")
    public void warnReminder(){
        inspectionFileService.warnReminder();
    }
}
