package cn.iocoder.yudao.module.mes.controller.admin.producereportplan;

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.mes.controller.admin.producereportplan.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.workreportplan.WorkReportPlanDO;
import cn.iocoder.yudao.module.mes.service.workreportplan.WorkReportPlanService;
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.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 static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 报工分配计划")
@RestController
@RequestMapping("/mes/work-report-plan")
@Validated
public class WorkReportPlanController {

    @Resource
    private WorkReportPlanService workReportPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建报工分配计划")
    //@PreAuthorize("@ss.hasPermission('mes:work-report-plan:create')")
    public CommonResult<Long> createWorkReportPlan(@Valid @RequestBody WorkReportPlanSaveReqVO createReqVO) {
        return success(workReportPlanService.createWorkReportPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新报工分配计划")
    //@PreAuthorize("@ss.hasPermission('mes:work-report-plan:update')")
    public CommonResult<Boolean> updateWorkReportPlan(@Valid @RequestBody WorkReportPlanSaveReqVO updateReqVO) {
        workReportPlanService.updateWorkReportPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报工分配计划")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('mes:work-report-plan:delete')")
    public CommonResult<Boolean> deleteWorkReportPlan(@RequestParam("id") Long id) {
        workReportPlanService.deleteWorkReportPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得报工分配计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('mes:work-report-plan:query')")
    public CommonResult<WorkReportPlanRespVO> getWorkReportPlan(@RequestParam("id") Long id) {
        WorkReportPlanDO workReportPlan = workReportPlanService.getWorkReportPlan(id);
        return success(BeanUtils.toBean(workReportPlan, WorkReportPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得报工分配计划分页")
    //@PreAuthorize("@ss.hasPermission('mes:work-report-plan:query')")
    public CommonResult<PageResult<WorkReportPlanRespVO>> getWorkReportPlanPage(@Valid WorkReportPlanPageReqVO pageReqVO) {
        PageResult<WorkReportPlanDO> pageResult = workReportPlanService.getWorkReportPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkReportPlanRespVO.class));
    } 
    @GetMapping("/pageView")
    @Operation(summary = "获得报工分配计划分页")
    //@PreAuthorize("@ss.hasPermission('mes:work-report-plan:query')")
    public CommonResult<PageResult<ReportPlanViewRespVO>> pageView(@Valid ReportPlanViewReqVO pageReqVO) {
        PageResult<ReportPlanViewRespVO> pageResult = workReportPlanService.getReportPlanViewPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报工分配计划 Excel")
    //@PreAuthorize("@ss.hasPermission('mes:work-report-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkReportPlanExcel(@Valid WorkReportPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkReportPlanDO> list = workReportPlanService.getWorkReportPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "报工分配计划.xls", "数据", WorkReportPlanRespVO.class,
                        BeanUtils.toBean(list, WorkReportPlanRespVO.class));
    }

}