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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.common.SystemEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.constant.InspectionConstants;
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.permission.PermissionApi;
import cn.iocoder.yudao.module.system.service.permission.PermissionServiceImpl;
import cn.iocoder.yudao.util.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.List;

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

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


    /**
     * 获取检测资料夹
     *
     * @return inspectionFile
     */
    @GetMapping("/getFirstFolder")
    public CommonResult<?> getFirstFolder() {
        // 查询检测 并且 文件夹 是检测客户资料夹的文件夹
        InspectionFile folder = inspectionFileService.getOne(Wrappers.<InspectionFile>lambdaQuery()
                .eq(InspectionFile::getType, InspectionConstants.INSPECTION_FOLDER)
                .eq(InspectionFile::getServicePackageId, SystemEnum.INSPECTION.getCode())
                .eq(InspectionFile::getDefaultKey, InspectionConstants.INSPECTION_CUSTOMER_FILE_FOLDER));
        if (folder == null) {
            // 新增一个这个文件夹
            folder = new InspectionFile();
            folder.setFileName(InspectionConstants.INSPECTION_CUSTOMER_FILE_FOLDER_NAME);
            folder.setServicePackageId(SystemEnum.INSPECTION.getCode());
            folder.setDefaultKey(InspectionConstants.INSPECTION_CUSTOMER_FILE_FOLDER);
            folder.setType(InspectionConstants.INSPECTION_FOLDER);
            folder.setTenantId(180L);

            inspectionFileService.insertInspectionFile(folder);
        }
        return success(folder);
    }

    /**
     * 查询inspectionFile列表
     */
    @GetMapping("/list")
    @TenantIgnore
    public CommonResult list( InspectionFile inspectionFile) throws Exception {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        inspectionFile.setCreator(String.valueOf(loginUserId));
        if (ObjectUtil.isEmpty(inspectionFile.getFatherId())) {

            // 查询检测 并且 文件夹 是检测客户资料夹的文件夹
            InspectionFile folder = inspectionFileService.getOne(Wrappers.<InspectionFile>lambdaQuery()
                    .eq(InspectionFile::getType, InspectionConstants.INSPECTION_FOLDER)
                    .eq(InspectionFile::getServicePackageId, SystemEnum.INSPECTION.getCode())
                    .eq(InspectionFile::getDefaultKey, InspectionConstants.INSPECTION_CUSTOMER_FILE_FOLDER));
            if (folder == null) {
                // 新增一个这个文件夹
                folder = new InspectionFile();
                folder.setFileName(InspectionConstants.INSPECTION_CUSTOMER_FILE_FOLDER_NAME);
                folder.setServicePackageId(SystemEnum.INSPECTION.getCode());
                folder.setDefaultKey(InspectionConstants.INSPECTION_CUSTOMER_FILE_FOLDER);
                folder.setType(InspectionConstants.INSPECTION_FOLDER);
                folder.setTenantId(180L);

                inspectionFileService.insertInspectionFile(folder);
            }
            inspectionFile.setFatherId(folder.getId());
        }

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

        return success(list);
    }

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

    /**
     * 新增inspectionFile
     */
    @PostMapping("/add")
    @TenantIgnore
    public CommonResult add(@RequestBody InspectionFile inspectionFile) throws Exception {
        inspectionFile.setTenantId(180L);
        return success(inspectionFileService.insertInspectionFile(inspectionFile));
    }

    /**
     * 修改inspectionFile
     */
    @PostMapping("/edit")
    @TenantIgnore
    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}")
    @TenantIgnore
    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());
    }
}
