package cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskplan;

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.iotprojecttaskplan.vo.IotProjectTaskPlanPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskplan.vo.IotProjectTaskPlanRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskplan.vo.IotProjectTaskPlanSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskplan.vo.IotProjectTaskPlanStatisticsRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojecttaskattrs.IotTaskAttrModelProperty;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojecttaskplan.IotProjectTaskPlanDO;
import cn.iocoder.yudao.module.pms.service.iotprojecttaskplan.IotProjectTaskPlanService;
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.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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("/pms/iot-project-task-plan")
@Validated
public class IotProjectTaskPlanController {

    @Resource
    private IotProjectTaskPlanService iotProjectTaskPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建日报任务计划(工作量)")
    @PreAuthorize("@ss.hasPermission('pms:iot-project-task-plan:create')")
    public CommonResult<Long> createIotProjectTaskPlan(@Valid @RequestBody IotProjectTaskPlanSaveReqVO createReqVO) {
        return success(iotProjectTaskPlanService.createIotProjectTaskPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新日报任务计划(工作量)")
    @PreAuthorize("@ss.hasPermission('pms:iot-project-task-plan:update')")
    public CommonResult<Boolean> updateIotProjectTaskPlan(@Valid @RequestBody IotProjectTaskPlanSaveReqVO updateReqVO) {
        iotProjectTaskPlanService.updateIotProjectTaskPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除日报任务计划(工作量)")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-project-task-plan:delete')")
    public CommonResult<Boolean> deleteIotProjectTaskPlan(@RequestParam("id") Long id) {
        iotProjectTaskPlanService.deleteIotProjectTaskPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得日报任务计划(工作量)")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-project-task-plan:query')")
    public CommonResult<IotProjectTaskPlanRespVO> getIotProjectTaskPlan(@RequestParam("id") Long id) {
        IotProjectTaskPlanDO iotProjectTaskPlan = iotProjectTaskPlanService.getIotProjectTaskPlan(id);
        return success(BeanUtils.toBean(iotProjectTaskPlan, IotProjectTaskPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得日报任务计划(工作量)分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-project-task-plan:query')")
    public CommonResult<PageResult<IotProjectTaskPlanRespVO>> getIotProjectTaskPlanPage(@Valid IotProjectTaskPlanPageReqVO pageReqVO) {
        PageResult<IotProjectTaskPlanDO> pageResult = iotProjectTaskPlanService.getIotProjectTaskPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotProjectTaskPlanRespVO.class));
    }

    @GetMapping("/planExtProperties")
    @Operation(summary = "获得项目任务计划对应的工作量属性列表")
    @PreAuthorize("@ss.hasPermission('pms:iot-project-task-plan:query')")
    public CommonResult<List<IotTaskAttrModelProperty>> planExtProperties(@Valid IotProjectTaskPlanPageReqVO pageReqVO) {
        List<IotTaskAttrModelProperty> taskPlanExtProperties = iotProjectTaskPlanService.planExtProperties(pageReqVO);
        return success(taskPlanExtProperties);
    }

    @GetMapping("/summaryStatistics")
    @Operation(summary = "专业公司工作量计划 汇总 统计")
    @PreAuthorize("@ss.hasPermission('pms:iot-rd-daily-report:query')")
    public CommonResult<Map<String, Object>> summaryStatistics(@Valid IotProjectTaskPlanPageReqVO pageReqVO) {
        List<IotProjectTaskPlanStatisticsRespVO> result = new ArrayList<>();
        Map<String, Object> resultMap = new HashMap<>();
        List<IotProjectTaskPlanStatisticsRespVO> statistics = iotProjectTaskPlanService.summaryStatistics(pageReqVO);
        result.addAll(statistics);
        resultMap.put("list", result);
        resultMap.put("total", result.size());
        return success(resultMap);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出日报任务计划(工作量) Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-project-task-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotProjectTaskPlanExcel(@Valid IotProjectTaskPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotProjectTaskPlanDO> list = iotProjectTaskPlanService.getIotProjectTaskPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "日报任务计划(工作量).xls", "数据", IotProjectTaskPlanRespVO.class,
                        BeanUtils.toBean(list, IotProjectTaskPlanRespVO.class));
    }

}