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

import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.module.mes.controller.admin.feedingrecord.vo.FeedingRecordRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.itemrequisition.vo.ItemRequisitionDetailRespVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.itemrequisition.ItemRequisitionDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.itemrequisition.ItemRequisitionDetailDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.organization.OrganizationDO;
import cn.iocoder.yudao.module.mes.service.organization.OrganizationService;
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 static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

import cn.iocoder.yudao.module.mes.controller.admin.paigongrecord.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.paigongrecord.PaigongRecordDO;
import cn.iocoder.yudao.module.mes.service.paigongrecord.PaigongRecordService;

@Tag(name = "管理后台 - 计划派工记录")
@RestController
@RequestMapping("/mes/paigong-record")
@Validated
public class PaigongRecordController {

    @Resource
    private PaigongRecordService paigongRecordService;

    @Resource
    private OrganizationService organizationService;


    @PostMapping("/create")
    @Operation(summary = "创建计划派工记录")
    @PreAuthorize("@ss.hasPermission('mes:paigong-record:create')")
    public CommonResult<Long> createPaigongRecord(@Valid @RequestBody PaigongRecordSaveReqVO createReqVO) {
        return success(paigongRecordService.createPaigongRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新计划派工记录")
    @PreAuthorize("@ss.hasPermission('mes:paigong-record:update')")
    public CommonResult<Boolean> updatePaigongRecord(@Valid @RequestBody PaigongRecordSaveReqVO updateReqVO) {
        paigongRecordService.updatePaigongRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除计划派工记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:paigong-record:delete')")
    public CommonResult<Boolean> deletePaigongRecord(@RequestParam("id") Long id) {
        paigongRecordService.deletePaigongRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得计划派工记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:paigong-record:query')")
    public CommonResult<PaigongRecordRespVO> getPaigongRecord(@RequestParam("id") Long id) {
        PaigongRecordDO paigongRecord = paigongRecordService.getPaigongRecord(id);
        return success(BeanUtils.toBean(paigongRecord, PaigongRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得计划派工记录分页")
    @PreAuthorize("@ss.hasPermission('mes:paigong-record:query')")
    public CommonResult<PageResult<PaigongRecordRespVO>> getPaigongRecordPage(@Valid PaigongRecordPageReqVO pageReqVO) {
        PageResult<PaigongRecordDO> pageResult = paigongRecordService.getPaigongRecordPage(pageReqVO);
        return  success(BeanUtils.toBean(pageResult, PaigongRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出计划派工记录 Excel")
    @PreAuthorize("@ss.hasPermission('mes:paigong-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPaigongRecordExcel(@Valid PaigongRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PaigongRecordDO> list = paigongRecordService.getPaigongRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "计划派工记录.xls", "数据", PaigongRecordRespVO.class,
                        BeanUtils.toBean(list, PaigongRecordRespVO.class));
    }

    // ==================== 子表（派工记录） ====================

    @GetMapping("/list-by-item-plan-id")
    @Operation(summary = "获得派工记录明细列表")
    @Parameter(name = "planId", description = "计划ID")
    @PreAuthorize("@ss.hasPermission('mes:paigong-record:query')")
    public CommonResult<List<PaigongRecordRespVO>> getPaigongRecordListByPlanId(@RequestParam("planId") Long planId) {
        List<PaigongRecordDO> pageResult = paigongRecordService.getPaigongRecordListByPlanId(planId);
        //return success(BeanUtils.toBean(pageResult, PaigongRecordRespVO.class));

        Map<Long, OrganizationDO> organizationMap = organizationService.getOrganizationVOMap(
                convertSet(pageResult, PaigongRecordDO::getPipeline));
        return success(BeanUtils.toBean(pageResult, PaigongRecordRespVO.class, paigongRecord -> {
            MapUtils.findAndThen(organizationMap, paigongRecord.getPipeline(), organization -> paigongRecord.setPipelineName(organization.getName()));
        }));
    }

}