CrmController.java 13.2 KB
package com.huaheng.api.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.api.crm.domain.BatteryCodeList;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.aspectj.lang.annotation.ApiLogger;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.barcode.barcodeDetail.domain.BarCodeDetail;
import com.huaheng.pc.barcode.barcodeDetail.service.BarCodeDetailService;
import com.huaheng.pc.barcode.barcodeHeader.domain.BarCodeHeader;
import com.huaheng.pc.barcode.barcodeHeader.service.BarCodeHeaderService;
import com.huaheng.pc.config.customer.domain.Customer;
import com.huaheng.pc.config.customer.service.CustomerServiceImpl;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.inventory.InventoryMaterialSummary.domain.InventoryMaterialSummary;
import com.huaheng.pc.inventory.InventoryMaterialSummary.service.InventoryMaterialSummaryService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransaction.service.InventoryTransactionService;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author yiwenpeng
 * @date 2023/5/8 18:39
 */
@RestController
@RequestMapping("/api/CRM")
public class CrmController {

    @Resource
    private ReceiptHeaderService receiptHeaderService;

    @Resource
    private ReceiptContainerDetailService receiptContainerDetailService;

    @Resource
    private InventoryDetailService inventoryDetailService;

    @Resource
    private TaskDetailService taskDetailService;

    @Resource
    private TaskHeaderService taskHeaderService;

    @Resource
    private ZoneService zoneService;

    @Resource
    private BarCodeDetailService barCodeDetailService;

    @Resource
    private BarCodeHeaderService barCodeHeaderService;

    @Resource
    private ShipmentHeaderService shipmentHeaderService;

    @Resource
    private CustomerServiceImpl customerServiceImpl;

    @Resource
    private InventoryTransactionService inventoryTransactionService;

    /**
     * 通过入库单号查询电池包码列表
     */
    @PostMapping("/batteryCodeList")
    @ApiLogger(apiName = "通过入库单号查询电池包码列表", from = "CRM")
    @ResponseBody
    public AjaxResult batteryCodeList(@RequestBody Map<String, String> map) {
        String code = map.get("code");
        if (StringUtils.isEmpty(code)) {
            return AjaxResult.error("编码为空");
        }
        ReceiptHeader receiptHeader = receiptHeaderService.getOne(new LambdaQueryWrapper<ReceiptHeader>().eq(ReceiptHeader::getReferCode, code));
        if (receiptHeader==null) {
            return AjaxResult.error("未查询到入库单");
        }
        List<BatteryCodeList> list = new ArrayList<>();
        List<ReceiptContainerDetail> receiptContainerDetailList = receiptContainerDetailService.list(new LambdaQueryWrapper<ReceiptContainerDetail>().eq(ReceiptContainerDetail::getReceiptCode, receiptHeader.getCode()));
        if (receiptContainerDetailList.size()==0) {
            BatteryCodeList batteryCodeList = new BatteryCodeList();
            batteryCodeList.setStatus("未入库");
            list.add(batteryCodeList);
        }
        for (ReceiptContainerDetail detail : receiptContainerDetailList) {
            BatteryCodeList batteryCodeList = new BatteryCodeList();
            batteryCodeList.setCode(detail.getBatteryPackTwoCode());
            if (detail.getStatus()==0 || detail.getStatus()==10) {
                batteryCodeList.setStatus("已组盘,未入库");
            }else if (detail.getStatus() == 20) {
                InventoryDetail inventoryDetail = inventoryDetailService.getOne(new LambdaQueryWrapper<InventoryDetail>().eq(InventoryDetail::getBatteryPackTwoCode, detail.getBatteryPackTwoCode()));
                if (inventoryDetail==null) {
                    batteryCodeList.setStatus("已出库");
                }else {
                    batteryCodeList.setStatus("已入库");
                }
            }else {
                batteryCodeList.setStatus("组盘状态未知,联系WMS处理");
            }
            list.add(batteryCodeList);
        }
        return AjaxResult.success(list);
    }




    /**
     * 电池编码溯源
     */
    @PostMapping("/batteryCodeTraceability")
    @ApiLogger(apiName = "电池编码溯源", from = "CRM")
    @ResponseBody
    public AjaxResult batteryCodeTraceability(@RequestBody Map<String, String> map) {
        String code = map.get("code");
        String type = map.get("type");
        if (StringUtils.isEmpty(code)) {
            return AjaxResult.error("编码为空");
        }
        if (StringUtils.isEmpty(type)) {
            return AjaxResult.error("类型为空");
        }
        if ("P20".equals(code.substring(0,3))) {
            //电池包
            if (type.equals("receipt")) {
                return batteryCodeReceipt(code);
            }else if (type.equals("shipment")){
                return batteryCodeShipment(code);
            }else{
                return AjaxResult.error("类型有误");
            }
        }else{
            if (type.equals("receipt")) {
                return productReceipt(code);
            }else if (type.equals("shipment")){
                return productShipment(code);
            }else{
                return AjaxResult.error("类型有误");
            }
        }
    }


    //电池包入库
    private AjaxResult batteryCodeReceipt(String code){
        BatteryCodeList batteryCodeList = new BatteryCodeList();
        TaskDetail taskDetail = taskDetailService.getOne(new LambdaQueryWrapper<TaskDetail>().eq(TaskDetail::getInternalTaskType,100).eq(TaskDetail::getBatteryPackTwoCode, code));
        if (taskDetail!=null) {
            TaskHeader taskheader = taskHeaderService.getById(taskDetail.getTaskId());
            Zone zone = zoneService.getOne(new LambdaQueryWrapper<Zone>().eq(Zone::getCode, taskheader.getZoneCode()));
            batteryCodeList.setReceiptDate(taskheader.getCompleteTime());
            batteryCodeList.setReceiptWarehouse(zone.getName());
            batteryCodeList.setReceiptBy(taskheader.getLastUpdatedBy());
            batteryCodeList.setMaterialGrossWeight(taskDetail.getMaterialGrossWeight());
            batteryCodeList.setMaterialNetWeight(taskDetail.getMaterialNetWeight());
            batteryCodeList.setMaterialTareWeight(taskDetail.getMaterialTareWeight());

            if (StringUtils.isNotEmpty(taskDetail.getBillCode())) {
                ReceiptHeader receiptHeader = receiptHeaderService.getOne(new LambdaQueryWrapper<ReceiptHeader>().eq(ReceiptHeader::getCode, taskDetail.getBillCode()));
                if (receiptHeader!=null) {
                    batteryCodeList.setReceiptCode(receiptHeader.getReferCode());
                }
            }
        }
        return AjaxResult.success(batteryCodeList);
    }
    //电池包出库
    private AjaxResult batteryCodeShipment(String code){
        BatteryCodeList batteryCodeList = new BatteryCodeList();
        TaskDetail taskDetail = taskDetailService.getOne(new LambdaQueryWrapper<TaskDetail>().eq(TaskDetail::getInternalTaskType,200).eq(TaskDetail::getBatteryPackTwoCode, code));
        if (taskDetail!=null) {
            TaskHeader taskheader = taskHeaderService.getById(taskDetail.getTaskId());
            Zone zone = zoneService.getOne(new LambdaQueryWrapper<Zone>().eq(Zone::getCode, taskheader.getZoneCode()));
            batteryCodeList.setShipmentDate(taskheader.getCompleteTime());
            batteryCodeList.setShipmentWarehouse(zone.getName());
            batteryCodeList.setShipmentBy(taskheader.getLastUpdatedBy());
            if (StringUtils.isNotEmpty(taskDetail.getBillCode())) {
                ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>().eq(ShipmentHeader::getCode, taskDetail.getBillCode()));
                if (shipmentHeader!=null) {
                    batteryCodeList.setShipmentCode(shipmentHeader.getReferCode());
                }
            }
        }
        return AjaxResult.success(batteryCodeList);
    }

    //成品入库
    private AjaxResult productReceipt(String code) {
        BatteryCodeList batteryCodeList = new BatteryCodeList();
        List<InventoryTransaction> inventoryTransactionList = inventoryTransactionService.list(new LambdaQueryWrapper<InventoryTransaction>().eq(InventoryTransaction::getBarMaterialCode, code).eq(InventoryTransaction::getTransactionType, 10));
        if (inventoryTransactionList.size() > 0) {
            InventoryTransaction inventoryTransaction = inventoryTransactionList.get(0);
            TaskDetail taskDetail = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>().eq(TaskDetail::getBillCode,inventoryTransaction.getBillCode())).get(0);
            TaskHeader taskheader = taskHeaderService.getById(taskDetail.getTaskId());
            Zone zone = zoneService.getOne(new LambdaQueryWrapper<Zone>().eq(Zone::getCode, taskheader.getZoneCode()));
            BarCodeDetail barCodeDetail = barCodeDetailService.getOne(new LambdaQueryWrapper<BarCodeDetail>().eq(BarCodeDetail::getCode, code));
            BarCodeHeader codeHeader = barCodeHeaderService.getById(barCodeDetail.getReceiptId());

            batteryCodeList.setMaterialCode(taskDetail.getMaterialCode());
            batteryCodeList.setMaterialName(taskDetail.getMaterialName());
            batteryCodeList.setMaterialSpec(taskDetail.getMaterialSpec());
            batteryCodeList.setMaterialUnit(taskDetail.getMaterialUnit());
            batteryCodeList.setReceiptWarehouse(zone.getName());
            batteryCodeList.setReceiptBy(taskheader.getLastUpdatedBy());
            batteryCodeList.setReceiptDate(taskheader.getCompleteTime());
            batteryCodeList.setWeight(codeHeader.getTotalWeight().toString());
        }
        return AjaxResult.success(batteryCodeList);
    }

    //成品出库
    private AjaxResult productShipment(String code){
        BatteryCodeList batteryCodeList = new BatteryCodeList();
        List<InventoryTransaction> inventoryTransactions = inventoryTransactionService.list(new LambdaQueryWrapper<InventoryTransaction>().eq(InventoryTransaction::getBarMaterialCode, code).eq(InventoryTransaction::getTransactionType, 20));
        if (inventoryTransactions.size() > 0) {
            InventoryTransaction inventoryTransaction = inventoryTransactions.get(0);
            ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(new LambdaQueryWrapper<ShipmentHeader>().eq(ShipmentHeader::getCode, inventoryTransaction.getBillCode()));
            if (shipmentHeader == null) {
                return AjaxResult.error("对应出库单不存在," + inventoryTransaction.getBillCode());
            }
            TaskDetail taskDetail = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>().eq(TaskDetail::getBillCode, inventoryTransaction.getBillCode())).get(0);
            TaskHeader taskheader = taskHeaderService.getById(taskDetail.getTaskId());
            Zone zone = zoneService.getOne(new LambdaQueryWrapper<Zone>().eq(Zone::getCode, taskheader.getZoneCode()));
            BarCodeDetail barCodeDetail = barCodeDetailService.getOne(new LambdaQueryWrapper<BarCodeDetail>().eq(BarCodeDetail::getCode, code));
            BarCodeHeader codeHeader = barCodeHeaderService.getById(barCodeDetail.getReceiptId());
            if (StringUtils.isNotEmpty(shipmentHeader.getCustomerCode())) {
                Customer customer = customerServiceImpl.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getCode, shipmentHeader.getCustomerCode()));
                batteryCodeList.setCustomerName(customer.getName());
            }
            batteryCodeList.setMaterialCode(taskDetail.getMaterialCode());
            batteryCodeList.setMaterialName(taskDetail.getMaterialName());
            batteryCodeList.setMaterialSpec(taskDetail.getMaterialSpec());
            batteryCodeList.setMaterialUnit(taskDetail.getMaterialUnit());
            batteryCodeList.setShipmentWarehouse(zone.getName());
            batteryCodeList.setShipmentBy(taskheader.getLastUpdatedBy());
            batteryCodeList.setShipmentDate(taskheader.getCompleteTime());
            batteryCodeList.setWeight(codeHeader.getTotalWeight().toString());
        }
        return AjaxResult.success(batteryCodeList);
    }


}