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

import cn.iocoder.yudao.module.mes.controller.admin.planmaintenance.vo.PlanMaintenanceRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.zjtask.vo.ZjTaskRespVO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
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 java.util.stream.Collectors;

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.moldtaskmanagement.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldtaskmanagement.MoldTaskManagementDO;
import cn.iocoder.yudao.module.mes.service.moldtaskmanagement.MoldTaskManagementService;

@Tag(name = "管理后台 - 任务管理")
@RestController
@RequestMapping("/mes/mold-task-management")
@Validated
public class MoldTaskManagementController {

    @Resource
    private MoldTaskManagementService moldtaskManagementService;
    @Resource
    private AdminUserApi adminUserApi;


    @PostMapping("/create")
    @Operation(summary = "创建任务管理")
    @PreAuthorize("@ss.hasPermission('mes:mold-task-management:create')")
    public CommonResult<Long> createMoldTaskManagement(@Valid @RequestBody MoldTaskManagementSaveReqVO createReqVO) {
        return success(moldtaskManagementService.createMoldTaskManagement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新任务管理")
    @PreAuthorize("@ss.hasPermission('mes:mold-task-management:update')")
    public CommonResult<Boolean> updateMoldTaskManagement(@Valid @RequestBody MoldTaskManagementSaveReqVO updateReqVO) {
        moldtaskManagementService.updateMoldTaskManagement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除任务管理")
    @Parameter(name = "ids", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:mold-task-management:delete')")
    public CommonResult<Boolean> deleteMoldTaskManagement(@RequestParam("ids") String ids) {
        // 将逗号分隔的字符串转换为Long类型的List
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(String::trim) // 去除可能存在的空格
                .map(Long::valueOf)
                .collect(Collectors.toList());
        moldtaskManagementService.deleteMoldTaskManagement(idList);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得任务管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:mold-task-management:query')")
    public CommonResult<MoldTaskManagementRespVO> getMoldTaskManagement(@RequestParam("id") Long id) {
        MoldTaskManagementDO moldtaskManagement = moldtaskManagementService.getMoldTaskManagement(id);
        return success(BeanUtils.toBean(moldtaskManagement, MoldTaskManagementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得任务管理分页")
    @PreAuthorize("@ss.hasPermission('mes:mold-task-management:query')")
    public CommonResult<PageResult<MoldTaskManagementRespVO>> getMoldTaskManagementPage(@Valid MoldTaskManagementPageReqVO pageReqVO) {
        PageResult<MoldTaskManagementDO> pageResult = moldtaskManagementService.getMoldTaskManagementPage(pageReqVO);

        PageResult<MoldTaskManagementRespVO> moldtaskManagementRespVOPageResult = BeanUtils.toBean(pageResult, MoldTaskManagementRespVO.class);
        buildPageCreatorName(moldtaskManagementRespVOPageResult);
        return success(moldtaskManagementRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出任务管理 Excel")
    @PreAuthorize("@ss.hasPermission('mes:mold-task-management:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoldTaskManagementExcel(@Valid MoldTaskManagementPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoldTaskManagementDO> list = moldtaskManagementService.getMoldTaskManagementPage(pageReqVO).getList();
        List<MoldTaskManagementRespVO> moldtaskManagementRespVOList = BeanUtils.toBean(list, MoldTaskManagementRespVO.class);

        for (MoldTaskManagementRespVO moldTaskManagementRespVO : moldtaskManagementRespVOList) {
            AdminUserRespDTO user = adminUserApi.getUser(Long.valueOf(moldTaskManagementRespVO.getCreator()));
            moldTaskManagementRespVO.setCreatorName("(" + user.getUsername() + ")" + user.getNickname());

            // 修复：使用实例方法而不是静态方法
            if (moldTaskManagementRespVO.getTaskType() != null) {
                switch (moldTaskManagementRespVO.getTaskType()) {
                    case 1:
                        moldTaskManagementRespVO.setTaskTypeName("点检");
                        break;
                    case 2:
                        moldTaskManagementRespVO.setTaskTypeName("保养");
                        break;
                    default:
                        moldTaskManagementRespVO.setTaskTypeName(null);
                        break;
                }
            }
        }

        // 修复：使用正确的Excel文件名
        ExcelUtils.write(response, "任务管理.xls", "数据", MoldTaskManagementRespVO.class, moldtaskManagementRespVOList);
    }


    @PostMapping("/createMoldTicket")
    @Operation(summary = "创建工单管理")
    @PreAuthorize("@ss.hasPermission('mes:mold-task-management:create')")
    public CommonResult<Boolean> createTicket(@RequestParam("id") Long id) {
        moldtaskManagementService.createMoldTicket(id);
        return success(true);
    }


    private PageResult<MoldTaskManagementRespVO> buildPageCreatorName(PageResult<MoldTaskManagementRespVO> planMaintenanceRespVOPageResult) {
        for (MoldTaskManagementRespVO planMaintenanceRespVO : planMaintenanceRespVOPageResult.getList()) {
            AdminUserRespDTO user = adminUserApi.getUser(Long.valueOf(planMaintenanceRespVO.getCreator()));
            planMaintenanceRespVO.setCreatorName( "(" + user.getUsername()+ ")" + user.getNickname());

        }


        return planMaintenanceRespVOPageResult;
    }

}