package cn.iocoder.yudao.module.bpm.controller.admin.stamp.stampseal;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.stamp.stampseal.vo.OaStampSealPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.stamp.stampseal.vo.OaStampSealRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.stamp.stampseal.vo.OaStampSealSaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.stamp.stampseal.OaStampSealDO;
import cn.iocoder.yudao.module.bpm.service.stamp.stampseal.OaStampSealService;
import cn.iocoder.yudao.module.employee.api.EmployeeApi;
import cn.iocoder.yudao.module.employee.api.dto.EmployeeRespDTO;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.infra.api.file.dto.FileDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 用印印章信息")
@RestController
@RequestMapping("/bpm/oa-stamp-seal")
@Validated
public class OaStampSealController {

    @Resource
    private OaStampSealService oaStampSealService;

    // @Resource
    // private AdminUserApi adminUserApi;
    @Resource
    private EmployeeApi employeeApi;

    @Resource
    private FileApi fileApi;

    @PostMapping("/create")
    @Operation(summary = "创建用印印章信息")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-stamp-seal:create')")
    public CommonResult<Long> createOaStampSeal(@Valid @RequestBody OaStampSealSaveReqVO createReqVO) {
        return success(oaStampSealService.createOaStampSeal(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用印印章信息")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-stamp-seal:update')")
    public CommonResult<Boolean> updateOaStampSeal(@Valid @RequestBody OaStampSealSaveReqVO updateReqVO) {
        oaStampSealService.updateOaStampSeal(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用印印章信息")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-stamp-seal:delete')")
    public CommonResult<Boolean> deleteOaStampSeal(@RequestParam("id") Long id) {
        oaStampSealService.deleteOaStampSeal(id);
        return success(true);
    }

    @PutMapping("/open")
    @Operation(summary = "打开用印印章信息")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-stamp-seal:open')")
    public CommonResult<Boolean> openOaStampSeal(@RequestParam("id") Long id) {
        oaStampSealService.openOaStampSeal(id);
        return success(true);
    }

    @PutMapping("/close")
    @Operation(summary = "关闭用印印章信息")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-stamp-seal:close')")
    public CommonResult<Boolean> closeOaStampSeal(@RequestParam("id") Long id) {
        oaStampSealService.closeOaStampSeal(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用印印章信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-stamp-seal:query')")
    public CommonResult<OaStampSealRespVO> getOaStampSeal(@RequestParam("id") Long id) {
        OaStampSealDO oaStampSeal = oaStampSealService.getOaStampSeal(id);
        OaStampSealRespVO oaStampSealRespVO = BeanUtils.toBean(oaStampSeal, OaStampSealRespVO.class);
        // 状态描述
        String status = oaStampSealRespVO.getStatus();
        switch (status) {
            case "1":
                oaStampSealRespVO.setStatusDesc("打开");
                break;
            case "2":
                oaStampSealRespVO.setStatusDesc("关闭");
                break;
            default:
                oaStampSealRespVO.setStatusDesc("未知");
                break;
        }
        // 创建者姓名
        Long creator = oaStampSealRespVO.getCreator();
        EmployeeRespDTO employee = employeeApi.getEmployeeByUserId(creator);
        if (employee != null) {
            oaStampSealRespVO.setCreateEmployeeName(employee.getName());
        }
        // AdminUserRespDTO user = adminUserApi.getUser(creator);
        // if (user != null) {
        //     oaStampSealRespVO.setCreateEmployeeName(user.getNickname());
        // }
        // 附件列表
        List<FileDTO> fileList = fileApi.getFileDTOListByBiz(oaStampSeal.getSealUuid());
        oaStampSealRespVO.setFileList(fileList);
        List<Long> fileIdList = fileList.stream().map(FileDTO::getId).collect(Collectors.toList());
        oaStampSealRespVO.setFileIdList(fileIdList);

        return success(oaStampSealRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得用印印章信息分页")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-stamp-seal:query')")
    public CommonResult<PageResult<OaStampSealRespVO>> getOaStampSealPage(@Valid @ParameterObject OaStampSealPageReqVO pageReqVO) {
        PageResult<OaStampSealDO> pageResult = oaStampSealService.getOaStampSealPage(pageReqVO);
        PageResult<OaStampSealRespVO> voPageResult = BeanUtils.toBean(pageResult, OaStampSealRespVO.class);

        List<OaStampSealRespVO> voPageResultList = voPageResult.getList();
        List<Long> creatorList = voPageResultList.stream().map(OaStampSealRespVO::getCreator).collect(Collectors.toList());
        Map<Long, EmployeeRespDTO> employeeMap = employeeApi.getEmployeeListByUserIds(creatorList)
                .stream()
                .collect(Collectors.toMap(EmployeeRespDTO::getUserId, Function.identity()));
        // List<AdminUserRespDTO> adminUserList = adminUserApi.getUserList(creatorList);
        // Map<Long, AdminUserRespDTO> adminUserMap = adminUserList.stream().collect(Collectors.toMap(AdminUserRespDTO::getId, Function.identity()));

        for (OaStampSealRespVO oaStampSealRespVO : voPageResultList) {
            String status = oaStampSealRespVO.getStatus();
            switch (status) {
                case "1":
                    oaStampSealRespVO.setStatusDesc("打开");
                    break;
                case "2":
                    oaStampSealRespVO.setStatusDesc("关闭");
                    break;
                default:
                    oaStampSealRespVO.setStatusDesc("未知");
                    break;
            }
            EmployeeRespDTO employeeRespDTO = employeeMap.get(oaStampSealRespVO.getCreator());
            if (employeeRespDTO != null) {
                oaStampSealRespVO.setCreateEmployeeName(employeeRespDTO.getName());
            }
            // AdminUserRespDTO adminUserRespDTO = adminUserMap.get(oaStampSealRespVO.getCreator());
            // if (adminUserRespDTO != null) {
            //     oaStampSealRespVO.setCreateEmployeeName(adminUserRespDTO.getNickname());
            // }
        }
        return success(voPageResult);
    }
    @GetMapping("/list")
    @Operation(summary = "获得用印印章信息列表")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-stamp-seal:list')")
    public CommonResult<List<OaStampSealRespVO>> getOaStampSealList() {
        List<OaStampSealDO> listResult = oaStampSealService.getOaStampSealList();
        return success(BeanUtils.toBean(listResult, OaStampSealRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用印印章信息 Excel")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-stamp-seal:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOaStampSealExcel(@Valid @ParameterObject OaStampSealPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OaStampSealDO> list = oaStampSealService.getOaStampSealPage(pageReqVO).getList();
        List<OaStampSealRespVO> stampSealRespVOList = BeanUtils.toBean(list, OaStampSealRespVO.class);

        List<Long> creatorList = stampSealRespVOList.stream().map(OaStampSealRespVO::getCreator).collect(Collectors.toList());
        Map<Long, EmployeeRespDTO> employeeMap = employeeApi.getEmployeeListByUserIds(creatorList)
                .stream()
                .collect(Collectors.toMap(EmployeeRespDTO::getUserId, Function.identity()));
        // List<AdminUserRespDTO> adminUserList = adminUserApi.getUserList(creatorList);
        // Map<Long, AdminUserRespDTO> adminUserMap = adminUserList.stream().collect(Collectors.toMap(AdminUserRespDTO::getId, Function.identity()));

        for (OaStampSealRespVO oaStampSealRespVO : stampSealRespVOList) {
            String status = oaStampSealRespVO.getStatus();
            switch (status) {
                case "1":
                    oaStampSealRespVO.setStatusDesc("打开");
                    break;
                case "2":
                    oaStampSealRespVO.setStatusDesc("关闭");
                    break;
                default:
                    oaStampSealRespVO.setStatusDesc("未知");
            }
            EmployeeRespDTO employeeRespDTO = employeeMap.get(oaStampSealRespVO.getCreator());
            if (employeeRespDTO != null) {
                oaStampSealRespVO.setCreateEmployeeName(employeeRespDTO.getName());
            }
            // oaStampSealRespVO.setCreateEmployeeName(adminUserMap.get(oaStampSealRespVO.getCreator()).getNickname());
        }
        // 导出 Excel
        ExcelUtils.write(response, "用印印章信息.xls", "数据", OaStampSealRespVO.class,
                stampSealRespVOList);
    }

}