package cn.iocoder.yudao.module.pms.controller.admin.qhse.file;

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.pms.controller.admin.qhse.file.vo.QhseInfoPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.file.vo.QhseInfoRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.file.vo.QhseInfoSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotInfoPageReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.file.QhseInfoDO;
import cn.iocoder.yudao.module.pms.service.qhse.file.QhseInfoService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import com.xingyuv.captcha.util.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
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 = "管理后台 - QHSE文件资料")
@RestController
@RequestMapping("/rq/qhse-info")
@Validated
public class QhseInfoController {

    @Resource
    private QhseInfoService qhseInfoService;
    @Autowired
    private DeptApi deptApi;

    @PostMapping("/create")
    @Operation(summary = "创建QHSE文件资料")
    @PreAuthorize("@ss.hasPermission('rq:qhse-info:create')")
    public CommonResult<Boolean> createQhseInfo(@Valid @RequestBody QhseInfoSaveReqVO createReqVO) {
        qhseInfoService.createQhseInfo(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新QHSE文件资料")
    @PreAuthorize("@ss.hasPermission('rq:qhse-info:update')")
    public CommonResult<Boolean> updateQhseInfo(@Valid @RequestBody QhseInfoSaveReqVO updateReqVO) {
        qhseInfoService.updateQhseInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除QHSE文件资料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:qhse-info:delete')")
    public CommonResult<Boolean> deleteQhseInfo(@RequestParam("id") Long id) {
        qhseInfoService.deleteQhseInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得QHSE文件资料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:qhse-info:query')")
    public CommonResult<QhseInfoRespVO> getQhseInfo(@RequestParam("id") Long id) {
        QhseInfoDO qhseInfo = qhseInfoService.getQhseInfo(id);
        return success(BeanUtils.toBean(qhseInfo, QhseInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得QHSE文件资料分页")
    @PreAuthorize("@ss.hasPermission('rq:qhse-info:query')")
    public CommonResult<PageResult<QhseInfoRespVO>> getQhseInfoPage(@Valid QhseInfoPageReqVO pageReqVO) {
        PageResult<QhseInfoDO> pageResult = qhseInfoService.getQhseInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QhseInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出QHSE文件资料 Excel")
    @PreAuthorize("@ss.hasPermission('rq:qhse-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQhseInfoExcel(@Valid QhseInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QhseInfoDO> list = qhseInfoService.getQhseInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "QHSE文件资料.xls", "数据", QhseInfoRespVO.class,
                        BeanUtils.toBean(list, QhseInfoRespVO.class));
    }

    @PostMapping("/file/page")
    @Operation(summary = "获得资料分页")
    @PreAuthorize("@ss.hasPermission('rq:qhse-info:query')")
    public CommonResult<PageResult<QhseInfoRespVO>> getIotInfoFilePage(@Valid QhseInfoPageReqVO pageReqVO) {
        PageResult<QhseInfoDO> pageResult = qhseInfoService.getQhseInfoPage(pageReqVO);
        List<QhseInfoDO> list = pageResult.getList();
        List<QhseInfoRespVO> collect = list.stream().map(e -> {
            QhseInfoRespVO vo = BeanUtils.toBean(e, QhseInfoRespVO.class);
            if (Objects.nonNull(e.getDeptId())) {
                DeptRespDTO dept = deptApi.getDept(e.getDeptId());
                if (Objects.nonNull(dept)) {
                    vo.setDeptName(dept.getName());
                }
            }
            return vo;
        }).collect(Collectors.toList());
        return success(new PageResult<>(collect, pageResult.getTotal()));
    }


    @PostMapping("/child/content-file")
    @Operation(summary = "获得资料库的分类目录及文件")
    @PreAuthorize("@ss.hasPermission('rq:iot-info:query')")
    public CommonResult<List<QhseInfoRespVO>> getChildContentFile(@Valid IotInfoPageReqVO pageReqVO) {
        Long classId = pageReqVO.getClassId();
        List<QhseInfoRespVO> contentFiles = qhseInfoService.getChildContentFile(classId).stream().filter(e ->{
            if (StringUtils.isNotBlank(pageReqVO.getFilename())) {
                return e.getFilename().contains(pageReqVO.getFilename());
            } else {
                return true;
            }
        }).collect(Collectors.toList());
        return success(contentFiles);
    }

    @PostMapping("/all/content-file")
    @Operation(summary = "获得QHSE资料库下的所有分类目录及文件")
    @PreAuthorize("@ss.hasPermission('rq:iot-info:query')")
    public CommonResult<List<QhseInfoRespVO>> getAllContentFile(@Valid IotInfoPageReqVO pageReqVO) {
        Long classId = pageReqVO.getClassId();
        List<QhseInfoRespVO> contentFiles = qhseInfoService.getAllChildContentFile(classId).stream().filter(e ->{
            if (StringUtils.isNotBlank(pageReqVO.getAllName())) {
                return e.getFilename().contains(pageReqVO.getAllName());
            } else {
                return true;
            }
        }).collect(Collectors.toList());
        return success(contentFiles);
    }
}