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

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.report.vo.QhseMonthReportPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.report.vo.QhseMonthReportRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.report.vo.QhseMonthReportSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.report.QhseMonthReportDO;
import cn.iocoder.yudao.module.pms.service.qhse.report.QhseMonthReportService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
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 = "管理后台 - 资料")
@RestController
@RequestMapping("/rq/qhse-month-report")
@Validated
public class QhseMonthReportController {

    @Resource
    private QhseMonthReportService qhseMonthReportService;
    @Autowired
    private AdminUserApi adminUserApi;
    @Autowired
    private DeptApi deptApi;

    @PostMapping("/create")
    @Operation(summary = "创建资料")
    @PreAuthorize("@ss.hasPermission('rq:qhse-month-report:create')")
    public CommonResult<Long> createQhseMonthReport(@Valid @RequestBody QhseMonthReportSaveReqVO createReqVO) {
        return success(qhseMonthReportService.createQhseMonthReport(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资料")
    @PreAuthorize("@ss.hasPermission('rq:qhse-month-report:update')")
    public CommonResult<Boolean> updateQhseMonthReport(@Valid @RequestBody QhseMonthReportSaveReqVO updateReqVO) {
        qhseMonthReportService.updateQhseMonthReport(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得资料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:qhse-month-report:query')")
    public CommonResult<QhseMonthReportRespVO> getQhseMonthReport(@RequestParam("id") Long id) {
        QhseMonthReportDO qhseMonthReport = qhseMonthReportService.getQhseMonthReport(id);
        return success(BeanUtils.toBean(qhseMonthReport, QhseMonthReportRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资料分页")
    @PreAuthorize("@ss.hasPermission('rq:qhse-month-report:query')")
    public CommonResult<PageResult<QhseMonthReportRespVO>> getQhseMonthReportPage(@Valid QhseMonthReportPageReqVO pageReqVO) {
        PageResult<QhseMonthReportDO> pageResult = qhseMonthReportService.getQhseMonthReportPage(pageReqVO);
        List<QhseMonthReportRespVO> collect = pageResult.getList().stream().map(e -> {
            QhseMonthReportRespVO qhseMonthReportRespVO = new QhseMonthReportRespVO();
            BeanUtils.copyProperties(e, qhseMonthReportRespVO);
            if (Objects.nonNull(qhseMonthReportRespVO.getDutyPerson())){
                AdminUserRespDTO user = adminUserApi.getUser(qhseMonthReportRespVO.getDutyPerson());
                if (Objects.nonNull(user)){
                    qhseMonthReportRespVO.setPersonName(user.getNickname());
                }
            }
            if (Objects.nonNull(qhseMonthReportRespVO.getDeptId())){
                DeptRespDTO dept = deptApi.getDept(qhseMonthReportRespVO.getDeptId());
                if (Objects.nonNull(dept)){
                    qhseMonthReportRespVO.setDeptName(dept.getName());
                }
            }
             return qhseMonthReportRespVO;
        }).collect(Collectors.toList());
        return success(new PageResult<>(collect, pageResult.getTotal()));
    }

    @GetMapping("/page/year-month")
    @Operation(summary = "获得月报汇总信息")
    @PreAuthorize("@ss.hasPermission('rq:qhse-month-report:query')")
    public CommonResult<PageResult<QhseMonthReportRespVO>> getQhseMonthReportDistinctPage(@Valid QhseMonthReportPageReqVO pageReqVO) {
        PageResult<QhseMonthReportRespVO> pageResult = qhseMonthReportService.getDistinctPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资料 Excel")
    @PreAuthorize("@ss.hasPermission('rq:qhse-month-report:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQhseMonthReportExcel(@Valid QhseMonthReportPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QhseMonthReportDO> list = qhseMonthReportService.getQhseMonthReportPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资料.xls", "数据", QhseMonthReportRespVO.class,
                        BeanUtils.toBean(list, QhseMonthReportRespVO.class));
    }

}