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

import cn.iocoder.yudao.module.common.controller.admin.moldrepair.vo.MoldRepairRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.ticketmanagement.vo.TicketManagementBatchUpdateReqVO;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mes.controller.admin.zjtask.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.zjtask.ZjTaskDO;
import cn.iocoder.yudao.module.mes.service.zjtask.ZjTaskService;

@Tag(name = "管理后台 - 质量管理-检验任务")
@RestController
@RequestMapping("/mes/zj-task")
@Validated
public class ZjTaskController {

    @Resource
    private ZjTaskService zjTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建质量管理-检验任务")
    @PreAuthorize("@ss.hasPermission('mes:zj-task:create')")
    public CommonResult<Long> createZjTask(@Valid @RequestBody ZjTaskSaveReqVO createReqVO) {
        return success(zjTaskService.createZjTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新质量管理-检验任务")
    @PreAuthorize("@ss.hasPermission('mes:zj-task:update')")
    public CommonResult<Boolean> updateZjTask(@Valid @RequestBody ZjTaskSaveReqVO updateReqVO) {
        zjTaskService.updateZjTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除质量管理-检验任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:zj-task:delete')")
    public CommonResult<Boolean> deleteZjTask(@RequestParam("id") Long id) {
        zjTaskService.deleteZjTask(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得质量管理-检验任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:zj-task:query')")
    public CommonResult<ZjTaskRespVO> getZjTask(@RequestParam("id") Long id) {
        ZjTaskDO zjTask = zjTaskService.getZjTask(id);
        return success(BeanUtils.toBean(zjTask, ZjTaskRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得质量管理-检验任务分页")
    @PreAuthorize("@ss.hasPermission('mes:zj-task:query')")
    public CommonResult<PageResult<ZjTaskRespVO>> getZjTaskPage(@Valid ZjTaskPageReqVO pageReqVO) {
        PageResult<ZjTaskDO> pageResult = zjTaskService.getZjTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ZjTaskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出质量管理-检验任务 Excel")
    @PreAuthorize("@ss.hasPermission('mes:zj-task:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportZjTaskExcel(@Valid ZjTaskPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ZjTaskDO> list = zjTaskService.getZjTaskPage(pageReqVO).getList();
        List<ZjTaskRespVO> zjTaskRespVOList = BeanUtils.toBean(list, ZjTaskRespVO.class);
        for (ZjTaskRespVO zjTaskRespVO : zjTaskRespVOList) {
            if (zjTaskRespVO.getResult() != null) {
                switch (zjTaskRespVO.getResult()) {
                    case "0":
                        zjTaskRespVO.setResultName(null);
                        break;
                    case "1":
                        zjTaskRespVO.setResultName("通过");
                        break;
                    case "2":
                        zjTaskRespVO.setResultName("不通过");
                        break;
                    default:
                        zjTaskRespVO.setResultName(null);
                        break;
                }
            }
        }
        // 使用处理后的zjTaskRespVOList进行导出
        ExcelUtils.write(response, "质量管理-检验任务.xls", "数据", ZjTaskRespVO.class, zjTaskRespVOList);
    }

    @PutMapping("/batchUpdateStatus")
    @Operation(summary = "批量取消工单")
    @PreAuthorize("@ss.hasPermission('mes:zj-task:update')")
    public CommonResult<Boolean> batchUpdateZjTaskStatus(@Valid @RequestBody ZjTaskBatchUpdateReqVO reqVO) {
        zjTaskService.batchUpdateJobStatus(reqVO);
        return success(true);
    }



}