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

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.safe.vo.QhseSafeDayPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.safe.vo.QhseSafeDayRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.safe.vo.QhseSafeDaySaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.safe.QhseSafeDayDO;
import cn.iocoder.yudao.module.pms.service.qhse.safe.QhseSafeDayService;
import com.google.common.collect.ImmutableMap;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

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-safe-day")
@Validated
public class QhseSafeDayController {

    @Resource
    private QhseSafeDayService qhseSafeDayService;

    @PostMapping("/create")
    @Operation(summary = "创建QHSE_安全生产天数")
    @PreAuthorize("@ss.hasPermission('rq:qhse-safe-day:create')")
    public CommonResult<Long> createQhseSafeDay(@Valid @RequestBody QhseSafeDaySaveReqVO createReqVO) {
        return success(qhseSafeDayService.createQhseSafeDay(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新QHSE_安全生产天数")
    @PreAuthorize("@ss.hasPermission('rq:qhse-safe-day:update')")
    public CommonResult<Boolean> updateQhseSafeDay(@Valid @RequestBody QhseSafeDaySaveReqVO updateReqVO) {
        qhseSafeDayService.updateQhseSafeDay(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除QHSE_安全生产天数")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:qhse-safe-day:delete')")
    public CommonResult<Boolean> deleteQhseSafeDay(@RequestParam("id") Long id) {
        qhseSafeDayService.deleteQhseSafeDay(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得QHSE_安全生产天数")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:qhse-safe-day:query')")
    public CommonResult<QhseSafeDayRespVO> getQhseSafeDay(@RequestParam("id") Long id) {
        QhseSafeDayDO qhseSafeDay = qhseSafeDayService.getQhseSafeDay(id);
        return success(BeanUtils.toBean(qhseSafeDay, QhseSafeDayRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得QHSE_安全生产天数分页")
    @PreAuthorize("@ss.hasPermission('rq:qhse-safe-day:query')")
    public CommonResult<PageResult<QhseSafeDayRespVO>> getQhseSafeDayPage(@Valid QhseSafeDayPageReqVO pageReqVO) {
        PageResult<QhseSafeDayDO> pageResult = qhseSafeDayService.getQhseSafeDayPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QhseSafeDayRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出QHSE_安全生产天数 Excel")
    @PreAuthorize("@ss.hasPermission('rq:qhse-safe-day:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQhseSafeDayExcel(@Valid QhseSafeDayPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QhseSafeDayDO> list = qhseSafeDayService.getQhseSafeDayPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "QHSE_安全生产天数.xls", "数据", QhseSafeDayRespVO.class,
                        BeanUtils.toBean(list, QhseSafeDayRespVO.class));
    }

    @GetMapping("/get/dept/{deptId}")
    @Operation(summary = "获得QHSE_安全生产天数")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
//    @PreAuthorize("@ss.hasPermission('rq:qhse-safe-day:query')")
    public CommonResult<Integer> getQhseSafeDayByDept(@RequestParam("deptId") Long id) {
        List<QhseSafeDayDO> dayByDept = qhseSafeDayService.getDayByDept(id);
        int minDayNumber = dayByDept.stream()
                .mapToInt(QhseSafeDayDO::getDayNumber) // 映射为 dayNumber
                .min() // 取最小值
                .orElse(0); // 集合为空时默认 0，可自己改
        return success(minDayNumber);
    }

    @GetMapping("/get/dept/company")
    @Operation(summary = "获得QHSE_安全生产天数")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PermitAll
//    @PreAuthorize("@ss.hasPermission('rq:qhse-safe-day:query')")
    public CommonResult<ImmutableMap> getQhseSafeDayCompany(@RequestParam("deptId") Long id) {
        //瑞鹰
        List<QhseSafeDayDO> dayByDept = qhseSafeDayService.getDayByDept(158L);
        int ryMinDayNumber = dayByDept.stream()
                .mapToInt(QhseSafeDayDO::getDayNumber) // 映射为 dayNumber
                .min() // 取最小值
                .orElse(0); // 集合为空时默认 0，可自己改
        //瑞恒
        List<QhseSafeDayDO> rhByDept = qhseSafeDayService.getDayByDept(157L);
        int rhMinDayNumber = rhByDept.stream()
                .mapToInt(QhseSafeDayDO::getDayNumber) // 映射为 dayNumber
                .min() // 取最小值
                .orElse(0); // 集合为空时默认 0，可自己改
        //瑞都
        List<QhseSafeDayDO> rdByDept = qhseSafeDayService.getDayByDept(163L);
        int rdMinDayNumber = rdByDept.stream()
                .mapToInt(QhseSafeDayDO::getDayNumber) // 映射为 dayNumber
                .min() // 取最小值
                .orElse(0); // 集合为空时默认 0，可自己改
        List<QhseSafeDayDO> rq = qhseSafeDayService.getByName("瑞气能源");
        List<QhseSafeDayDO> rl = qhseSafeDayService.getByName("瑞霖技术");
        List<QhseSafeDayDO> five = qhseSafeDayService.getByName("5#项目");

        return success(ImmutableMap.of("气体增产", rhMinDayNumber,"钻修井",ryMinDayNumber,"酸化压裂",rdMinDayNumber,"瑞气能源",rq.get(0).getDayNumber(),
                "瑞霖技术",rl.get(0).getDayNumber(),"5#项目",five.get(0).getDayNumber()));
    }

}