package cn.iocoder.yudao.module.pms.controller.admin.iotmainworkorderbommaterial;

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.pms.controller.admin.iotmainworkorderbommaterial.vo.IotMainWorkOrderBomMaterialPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainworkorderbommaterial.vo.IotMainWorkOrderBomMaterialRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainworkorderbommaterial.vo.IotMainWorkOrderBomMaterialSaveReqVO;
import cn.iocoder.yudao.module.pms.convert.iotmainworkorderbommaterial.IotWorkOrderBomMaterialConvert;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmainworkorderbommaterial.IotMainWorkOrderBomMaterialDO;
import cn.iocoder.yudao.module.pms.service.iotmainworkorderbommaterial.IotMainWorkOrderBomMaterialService;
import cn.iocoder.yudao.module.system.api.saporg.SapOrgApi;
import cn.iocoder.yudao.module.system.api.saporg.dto.SapOrgRespDTO;
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.security.access.prepost.PreAuthorize;
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 java.util.Map;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - PMS 保养工单明细设备BOM挂载物料关联")
@RestController
@RequestMapping("/pms/iot-main-work-order-bom-material")
@Validated
public class IotMainWorkOrderBomMaterialController {

    @Resource
    private IotMainWorkOrderBomMaterialService iotMainWorkOrderBomMaterialService;
    @Resource
    private SapOrgApi sapOrgApi;

    @PostMapping("/create")
    @Operation(summary = "创建PMS 保养工单明细设备BOM挂载物料关联")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-bom-material:create')")
    public CommonResult<Long> createIotMainWorkOrderBomMaterial(@Valid @RequestBody IotMainWorkOrderBomMaterialSaveReqVO createReqVO) {
        return success(iotMainWorkOrderBomMaterialService.createIotMainWorkOrderBomMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS 保养工单明细设备BOM挂载物料关联")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-bom-material:update')")
    public CommonResult<Boolean> updateIotMainWorkOrderBomMaterial(@Valid @RequestBody IotMainWorkOrderBomMaterialSaveReqVO updateReqVO) {
        iotMainWorkOrderBomMaterialService.updateIotMainWorkOrderBomMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除PMS 保养工单明细设备BOM挂载物料关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-bom-material:delete')")
    public CommonResult<Boolean> deleteIotMainWorkOrderBomMaterial(@RequestParam("id") Long id) {
        iotMainWorkOrderBomMaterialService.deleteIotMainWorkOrderBomMaterial(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得PMS 保养工单明细设备BOM挂载物料关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-bom-material:query')")
    public CommonResult<IotMainWorkOrderBomMaterialRespVO> getIotMainWorkOrderBomMaterial(@RequestParam("id") Long id) {
        IotMainWorkOrderBomMaterialDO iotMainWorkOrderBomMaterial = iotMainWorkOrderBomMaterialService.getIotMainWorkOrderBomMaterial(id);
        return success(BeanUtils.toBean(iotMainWorkOrderBomMaterial, IotMainWorkOrderBomMaterialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS 保养工单明细设备BOM挂载物料关联分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-bom-material:query')")
    public CommonResult<PageResult<IotMainWorkOrderBomMaterialRespVO>> getIotMainWorkOrderBomMaterialPage(@Valid IotMainWorkOrderBomMaterialPageReqVO pageReqVO) {
        PageResult<IotMainWorkOrderBomMaterialDO> pageResult = iotMainWorkOrderBomMaterialService.getIotMainWorkOrderBomMaterialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotMainWorkOrderBomMaterialRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得PMS 保养工单明细BOM挂载物料关联列表")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-bom-material:query')")
    public CommonResult<List<IotMainWorkOrderBomMaterialRespVO>> getIotMainWorkOrderBomMaterialList(@Valid IotMainWorkOrderBomMaterialPageReqVO pageReqVO) {
        List<IotMainWorkOrderBomMaterialDO> list = iotMainWorkOrderBomMaterialService.getIotMainWorkOrderBomMaterialList(pageReqVO);
        // 查询已经关联的物料的 工厂 成本中心 库存地点 信息
        Map<Long, SapOrgRespDTO> factoryMap = sapOrgApi.getSapOrgMap(
                convertList(list, IotMainWorkOrderBomMaterialDO::getFactoryId));
        Map<Long, SapOrgRespDTO> costCenterMap = sapOrgApi.getSapOrgMap(
                convertList(list, IotMainWorkOrderBomMaterialDO::getCostCenterId));
        Map<Long, SapOrgRespDTO> storageLocationMap = sapOrgApi.getSapOrgMap(
                convertList(list, IotMainWorkOrderBomMaterialDO::getStorageLocationId));
        return success(IotWorkOrderBomMaterialConvert.INSTANCE.convertList1(list, factoryMap, costCenterMap, storageLocationMap));
    }

    @GetMapping("/workOrderMaterials")
    @Operation(summary = "获得保养工单物料")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-bom-material:query')")
    public CommonResult<PageResult<IotMainWorkOrderBomMaterialRespVO>> getMainWorkOrderMaterials(@Valid IotMainWorkOrderBomMaterialPageReqVO pageReqVO) {
        PageResult<IotMainWorkOrderBomMaterialRespVO> iotMaintainMaterials = iotMainWorkOrderBomMaterialService.selectMaterialsByDept(pageReqVO);
        // 设置每个库存物料关联的BOM工厂 成本中心 库存地点
        // 拼接 工厂 成本中心 库存地点
        Map<Long, SapOrgRespDTO> factoryMap = sapOrgApi.getSapOrgMap(
                convertList(iotMaintainMaterials.getList(), IotMainWorkOrderBomMaterialRespVO::getFactoryId));
        Map<Long, SapOrgRespDTO> costCenterMap = sapOrgApi.getSapOrgMap(
                convertList(iotMaintainMaterials.getList(), IotMainWorkOrderBomMaterialRespVO::getCostCenterId));
        Map<Long, SapOrgRespDTO> storageLocationMap = sapOrgApi.getSapOrgMap(
                convertList(iotMaintainMaterials.getList(), IotMainWorkOrderBomMaterialRespVO::getStorageLocationId));
        return success(new PageResult<>(IotWorkOrderBomMaterialConvert.INSTANCE.convertList(iotMaintainMaterials.getList(), factoryMap, costCenterMap, storageLocationMap),
                iotMaintainMaterials.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS 保养工单明细设备BOM挂载物料关联 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-bom-material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotMainWorkOrderBomMaterialExcel(@Valid IotMainWorkOrderBomMaterialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotMainWorkOrderBomMaterialDO> list = iotMainWorkOrderBomMaterialService.getIotMainWorkOrderBomMaterialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS 保养工单明细设备BOM挂载物料关联.xls", "数据", IotMainWorkOrderBomMaterialRespVO.class,
                        BeanUtils.toBean(list, IotMainWorkOrderBomMaterialRespVO.class));
    }

}