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

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.iotlockstock.vo.IotLockStockPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterialrequisitiondetail.vo.IotMaterialRequisitionDetailPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterialrequisitiondetail.vo.IotMaterialRequisitionDetailRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterialrequisitiondetail.vo.IotMaterialRequisitionDetailSaveReqVO;
import cn.iocoder.yudao.module.pms.convert.iotmaterialreq.IotMaterialReqConvert;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmaterialrequisitiondetail.IotMaterialRequisitionDetailDO;
import cn.iocoder.yudao.module.pms.service.iotlockstock.IotLockStockService;
import cn.iocoder.yudao.module.pms.service.iotmaterialrequisitiondetail.IotMaterialRequisitionDetailService;
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.math.BigDecimal;
import java.util.HashSet;
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 物料领用明细")
@RestController
@RequestMapping("/pms/iot-material-requisition-detail")
@Validated
public class IotMaterialRequisitionDetailController {

    @Resource
    private IotMaterialRequisitionDetailService iotMaterialRequisitionDetailService;
    @Resource
    private SapOrgApi sapOrgApi;
    @Resource
    private IotLockStockService iotLockStockService;

    @PostMapping("/create")
    @Operation(summary = "创建PMS 物料领用明细")
    @PreAuthorize("@ss.hasPermission('pms:iot-material-requisition-detail:create')")
    public CommonResult<Long> createIotMaterialRequisitionDetail(@Valid @RequestBody IotMaterialRequisitionDetailSaveReqVO createReqVO) {
        return success(iotMaterialRequisitionDetailService.createIotMaterialRequisitionDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS 物料领用明细")
    @PreAuthorize("@ss.hasPermission('pms:iot-material-requisition-detail:update')")
    public CommonResult<Boolean> updateIotMaterialRequisitionDetail(@Valid @RequestBody IotMaterialRequisitionDetailSaveReqVO updateReqVO) {
        iotMaterialRequisitionDetailService.updateIotMaterialRequisitionDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除PMS 物料领用明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-material-requisition-detail:delete')")
    public CommonResult<Boolean> deleteIotMaterialRequisitionDetail(@RequestParam("id") Long id) {
        iotMaterialRequisitionDetailService.deleteIotMaterialRequisitionDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得PMS 物料领用明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-material-requisition-detail:query')")
    public CommonResult<IotMaterialRequisitionDetailRespVO> getIotMaterialRequisitionDetail(@RequestParam("id") Long id) {
        IotMaterialRequisitionDetailDO iotMaterialRequisitionDetail = iotMaterialRequisitionDetailService.getIotMaterialRequisitionDetail(id);
        return success(BeanUtils.toBean(iotMaterialRequisitionDetail, IotMaterialRequisitionDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS 物料领用明细分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-material-requisition-detail:query')")
    public CommonResult<PageResult<IotMaterialRequisitionDetailRespVO>> getIotMaterialRequisitionDetailPage(@Valid IotMaterialRequisitionDetailPageReqVO pageReqVO) {
        PageResult<IotMaterialRequisitionDetailDO> pageResult = iotMaterialRequisitionDetailService.getIotMaterialRequisitionDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotMaterialRequisitionDetailRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得PMS 物料领用明细列表")
    @PreAuthorize("@ss.hasPermission('pms:iot-material-requisition-detail:query')")
    public CommonResult<List<IotMaterialRequisitionDetailRespVO>> getIotMaterialRequisitionDetailList(@Valid IotMaterialRequisitionDetailPageReqVO pageReqVO) {
        List<IotMaterialRequisitionDetailDO> list = iotMaterialRequisitionDetailService.getIotMaterialRequisitionDetailList(pageReqVO);
        // 查询每个行物料的 工厂名称 成本中心名称 当前库存
        Map<Long, SapOrgRespDTO> factoryMap = sapOrgApi.getSapOrgMap(
                convertList(list, IotMaterialRequisitionDetailDO::getFactoryId));
        Map<Long, SapOrgRespDTO> costCenterMap = sapOrgApi.getSapOrgMap(
                convertList(list, IotMaterialRequisitionDetailDO::getCostCenterId));
        IotLockStockPageReqVO reqVO = new IotLockStockPageReqVO();
        reqVO.setCostCenterIds(new HashSet<>(convertList(list, IotMaterialRequisitionDetailDO::getCostCenterId)));
        reqVO.setMaterialCodes(new HashSet<>(convertList(list, IotMaterialRequisitionDetailDO::getMaterialCode)));
        Map<String, BigDecimal> stockQuantityPair = iotLockStockService.getIotLockStockList(reqVO);
        // return success(BeanUtils.toBean(list, IotMaterialRequisitionDetailRespVO.class));
        return success(IotMaterialReqConvert.INSTANCE.convertList(list, factoryMap, costCenterMap, stockQuantityPair));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS 物料领用明细 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-material-requisition-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotMaterialRequisitionDetailExcel(@Valid IotMaterialRequisitionDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotMaterialRequisitionDetailDO> list = iotMaterialRequisitionDetailService.getIotMaterialRequisitionDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS 物料领用明细.xls", "数据", IotMaterialRequisitionDetailRespVO.class,
                        BeanUtils.toBean(list, IotMaterialRequisitionDetailRespVO.class));
    }

}