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

import cn.iocoder.yudao.module.mes.controller.admin.ticketmanagement.vo.TicketManagementRespVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldsubject.MoldSubjectDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.subjectmoldplan.SubjectMoldPlanDO;
import cn.iocoder.yudao.module.mes.service.subjectmoldplan.SubjectMoldPlanService;
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.moldplanmaintenance.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldplanmaintenance.MoldPlanMaintenanceDO;
import cn.iocoder.yudao.module.mes.service.moldplanmaintenance.MoldPlanMaintenanceService;

@Tag(name = "管理后台 - 方案维护")
@RestController
@RequestMapping("/mes/mold-plan-maintenance")
@Validated
public class MoldPlanMaintenanceController {

    @Resource
    private MoldPlanMaintenanceService moldplanMaintenanceService;

    @Resource
    private SubjectMoldPlanService subjectMoldPlanService;

    @Resource
    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建方案维护")
    @PreAuthorize("@ss.hasPermission('mes:mold-plan-maintenance:create')")
    public CommonResult<Long> createMoldPlanMaintenance(@Valid @RequestBody MoldPlanMaintenanceSaveReqVO createReqVO) {
        return success(moldplanMaintenanceService.createMoldPlanMaintenance(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新方案维护")
    @PreAuthorize("@ss.hasPermission('mes:mold-plan-maintenance:update')")
    public CommonResult<Boolean> updateMoldPlanMaintenance(@Valid @RequestBody MoldPlanMaintenanceSaveReqVO updateReqVO) {
        moldplanMaintenanceService.updateMoldPlanMaintenance(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得方案维护")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:mold-plan-maintenance:query')")
    public CommonResult<MoldPlanMaintenanceRespVO> getMoldPlanMaintenance(@RequestParam("id") Long id) {
        MoldPlanMaintenanceDO moldplanMaintenance = moldplanMaintenanceService.getMoldPlanMaintenance(id);
        return success(BeanUtils.toBean(moldplanMaintenance, MoldPlanMaintenanceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得方案维护分页")
    @PreAuthorize("@ss.hasPermission('mes:mold-plan-maintenance:query')")
    public CommonResult<PageResult<MoldPlanMaintenanceRespVO>> getMoldPlanMaintenancePage(@Valid MoldPlanMaintenancePageReqVO pageReqVO) {
        PageResult<MoldPlanMaintenanceDO> pageResult = moldplanMaintenanceService.getMoldPlanMaintenancePage(pageReqVO);
        PageResult<MoldPlanMaintenanceRespVO> moldplanMaintenanceRespVOPageResult = BeanUtils.toBean(pageResult, MoldPlanMaintenanceRespVO.class);

        return success(buildPageCreatorName(moldplanMaintenanceRespVOPageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出方案维护 Excel")
    @PreAuthorize("@ss.hasPermission('mes:mold-plan-maintenance:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoldPlanMaintenanceExcel(@Valid MoldPlanMaintenancePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoldPlanMaintenanceDO> list = moldplanMaintenanceService.getMoldPlanMaintenancePage(pageReqVO).getList();

        List<MoldPlanMaintenanceRespVO> moldplanMaintenanceRespVOS = BeanUtils.toBean(list, MoldPlanMaintenanceRespVO.class);
        for (MoldPlanMaintenanceRespVO moldplanMaintenanceRespVO : moldplanMaintenanceRespVOS) {
            AdminUserRespDTO user = adminUserApi.getUser(Long.valueOf(moldplanMaintenanceRespVO.getCreator()));
            moldplanMaintenanceRespVO.setCreatorName( "(" + user.getUsername()+ ")" + user.getNickname());
        }

        // 导出 Excel
        ExcelUtils.write(response, "方案维护.xls", "数据", MoldPlanMaintenanceRespVO.class,moldplanMaintenanceRespVOS);
    }


    @GetMapping("/getSubjectList")
    @Operation(summary = "查询项目集合列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:mold-plan-maintenance:query')")
    public CommonResult<List<MoldSubjectDO>> getSubjectList(@RequestParam("id") Long id) {
        List<MoldSubjectDO> subjectMoldPlanDOList = moldplanMaintenanceService.getSubjectList(id);
        return success(subjectMoldPlanDOList);
    }


    private PageResult<MoldPlanMaintenanceRespVO> buildPageCreatorName(PageResult<MoldPlanMaintenanceRespVO> moldplanMaintenanceRespVOPageResult) {
        for (MoldPlanMaintenanceRespVO moldplanMaintenanceRespVO : moldplanMaintenanceRespVOPageResult.getList()) {
            AdminUserRespDTO user = adminUserApi.getUser(Long.valueOf(moldplanMaintenanceRespVO.getCreator()));
            moldplanMaintenanceRespVO.setCreatorName( "(" + user.getUsername()+ ")" + user.getNickname());
        }
        return moldplanMaintenanceRespVOPageResult;
    }

}