ShipmentU8ContainerHeaderServiceImpl.java 13.7 KB
package com.huaheng.pc.receipt.U8.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.api.U8.controller.ICSShipmentApi;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.redis.service.IRedisService;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.warehouse.domain.WorkOrderList;
import com.huaheng.pc.config.warehouse.mapper.WorkOrderListMapper;
import com.huaheng.pc.config.warehouse.service.WarehouseU8Service;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.receipt.U8.domain.U8Material;
import com.huaheng.pc.receipt.U8.domain.U8OutParameters;
import com.huaheng.pc.receipt.U8.domain.U8Stock;
import com.huaheng.pc.receipt.U8.domain.U8StockQuery;
import com.huaheng.pc.system.user.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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


/**
 * u8出库单据 服务层实现
 *
 * @author huaheng
 * @date 2022-03-23
 */
@Service
public class ShipmentU8ContainerHeaderServiceImpl implements IShipmentU8ContainerHeaderService {
    private static final Logger logger = LoggerFactory.getLogger(ShipmentU8ContainerHeaderServiceImpl.class);
    @Resource
    private AddressService addressService;
    @Resource
    private WarehouseU8Service warehouseWuService;
    @Resource
    private IRedisService iRedisService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private ICSShipmentApi icsShipmentApi;
    @Resource
    private WorkOrderListMapper workOrderListMapper;

    @Override
    public List getStockcData(U8StockQuery u8StockQuery) throws Exception {
        Address address = warehouseWuService.getU8Address();
        String url = this.getStockcUrl(address);
        StringBuilder param = new StringBuilder();
//        param.append("cCompanyCode=").append(u8StockQuery.getCCompanyCode())
        param.append("cCompanyCode=").append("UFDATA_807_2018")
                .append("&cWHCode=").append(u8StockQuery.getCWHCode())
                .append("&cInvCode=").append(u8StockQuery.getCInvCode());
        String result = HttpUtils.sendPost(url, param.toString());
        if (StringUtils.isEmpty(result)){
            throw new Exception("未查询到U8库存数据!");
        }
        JSONObject json = JSONObject.parseObject(result);
        List<U8Stock> u8StockList = JSONArray.parseArray(json.getString("data"), U8Stock.class);
        if (u8StockList == null || u8StockList.size() == 0){
            throw new Exception("未查询到U8库存数据!");
        }
//        List<U8Stock> resultList = new ArrayList<>();
//        u8StockList.forEach(item->{
//            // 工单行ID
//            item.setModID(u8StockQuery.getModID());
//            // 工单子件行ID
//            item.setAllocateId(u8StockQuery.getAllocateId());
//            // 应领数量
//            item.setNeedQuantity(u8StockQuery.getNeedQuantity());
//            // 已领数量
//            item.setReceiveQuantity(u8StockQuery.getReceiveQuantity());
//            String cInvCode = item.getCInvCode();
//            // 查询wms库存
//            List<Inventory> inventories = inventoryService.selectListEntityByMaterialCode(cInvCode);
//            if (inventories == null || inventories.size() == 0){
//                return;
//            }
//            int index = 0;
//
//            for (Inventory inventory :inventories){
//                U8Stock u8Stock = new U8Stock();
//                BeanUtils.copyProperties(item,  u8Stock);
//                // 库存id
//                u8Stock.setInventoryId(inventory.getId());
//                // 库存数量
//                u8Stock.setQty(inventory.getQty());
//                // 执行预定数量
//                u8Stock.setTaskQty(inventory.getTaskQty());
//                // 库区编码
//                u8Stock.setZoneCode(inventory.getZoneCode());
//                // 行号
//                u8Stock.setSerialNumber(index++);
//                resultList.add(u8Stock);
//            }
//        });
        return this.getConvertU8Stock(u8StockList, u8StockQuery);
    }

    @Override
    public List getStockcDataAll(String json) throws Exception {
        if (StringUtils.isEmpty(json)){
            throw new Exception("请勿提交空数据!");
        }
        List<U8StockQuery> u8StockQueryList = JSON.parseArray(json, U8StockQuery.class);
        if (u8StockQueryList != null && u8StockQueryList.size() == 0){
            throw new Exception("请勿提交空数据!");
        }
        Address address = warehouseWuService.getU8Address();
        String url = this.getStockcUrl(address);
        List<U8Stock> resultList = new ArrayList<>();
        for (U8StockQuery u8StockQuery : u8StockQueryList){
            StringBuilder param = new StringBuilder();
//            param.append("cCompanyCode=").append(u8StockQuery.getCCompanyCode())
            param.append("cCompanyCode=").append("UFDATA_807_2018")
                .append("&cWHCode=").append(u8StockQuery.getCWHCode())
                .append("&cInvCode=").append(u8StockQuery.getCInvCode());
            String result = HttpUtils.sendPost(url, param.toString());
            if (StringUtils.isEmpty(result)){
                continue;
            }
            JSONObject jsonData = JSONObject.parseObject(result);
            List<U8Stock> u8StockList = JSONArray.parseArray(jsonData.getString("data"),U8Stock.class);
            if (u8StockList == null || u8StockList.size() == 0){
                continue;
            }
            List<U8Stock> temp = this.getConvertU8Stock(u8StockList, u8StockQuery);
            if (temp!= null && temp.size() > 0){
                resultList.addAll(temp);
            }
        }
        return resultList;
    }

    @Override
    public List getConvertU8Stock(List<U8Stock> list, U8StockQuery u8StockQuery) {
        List<U8Stock> resultList = new ArrayList<>();
        list.forEach(item->{
            // 工单行ID
            item.setModID(u8StockQuery.getModID());
            // 工单子件行ID
            item.setAllocateId(u8StockQuery.getAllocateId());
            // 应领数量
            item.setNeedQuantity(u8StockQuery.getNeedQuantity());
            // 已领数量
            item.setReceiveQuantity(u8StockQuery.getReceiveQuantity());
            String cInvCode = item.getCInvCode();
            // 查询wms库存
//                List<InventoryHeader> inventories = inventoryDetailService.selectListEntityByMaterialCode(cInvCode);
            List<InventoryDetail> inventories = inventoryDetailService.list(new LambdaQueryWrapper<InventoryDetail>()
                    .eq(InventoryDetail::getMaterialCode, cInvCode)
                    .eq(InventoryDetail::getWarehouseCode, ShiroUtils.getWarehouseCode()));
            if (inventories == null || inventories.size() == 0){
                return;
            }
            int index = 0;
            for (InventoryDetail inventory :inventories){
                U8Stock u8Stock = new U8Stock();
                BeanUtils.copyProperties(item,  u8Stock);
                // 库存id
                u8Stock.setInventoryId(inventory.getId());
                // 库存数量
                u8Stock.setQty(inventory.getQty());
                // 执行预定数量
                u8Stock.setTaskQty(inventory.getTaskQty());
                // 库区编码
                u8Stock.setZoneCode(inventory.getZoneCode());
                // 行号
                u8Stock.setSerialNumber(index++);
                resultList.add(u8Stock);
            }
        });
        return resultList;
    }

    @Override
    public String getStockcUrl(Address address) {
        return address.getUrl() + "GetCurrentStock";
    }

    @Override
    public String getMaterialOutStock() throws Exception {
        Address address = new Address();
        address.setParam("U8");
        address.setWarehouseCode("KS0001");
        address = addressService.selectEntity(address);
        if (address == null){
            throw new Exception("未获取到address数据,请联系管理员!");
        }
        if (StringUtils.isEmpty(address.getUrl())){
            throw new Exception("未获取到出库URL,请检查url地址配置是否完整!");
        }
        return address.getUrl() + "CreateICSRdRecord_11";
    }

    @Override
    public void queryStockcDataAll(String cCompanyCode, String cWHCode) throws Exception {
        String key = "CS_work_order_list";
        if (!iRedisService.isKeyExists(key)){
           throw new Exception("请先查询工单在进行出库!");
        }
        List<WorkOrderList> workOrderLists = JSON.parseArray(JSON.toJSON(iRedisService.get(key)).toString(),WorkOrderList.class);
        workOrderLists.forEach(item->{
            U8StockQuery query = new U8StockQuery();
            query.setCWHCode(cWHCode);
            query.setCCompanyCode(cCompanyCode);
            query.setCInvCode(item.getMaterialCode());
            try {
                this.getStockcData(query);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }

    @Override
    public AjaxResult outStock(U8OutParameters u8OutParameters) throws Exception {
        String jsonString = JSONObject.toJSONString(u8OutParameters);
        logger.info("出库信息:"+jsonString);
        String url = this.getMaterialOutStock();
        String result = "";
//        String result = HttpUtils.bodypost(url, jsonString,ShiroUtils.getWarehouseCode());
        if (StringUtils.isEmpty(result)){
            throw new Exception("出库失败!");
        }
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
//        JSONObject json = JSONObject.parseObject(result);
//        String code = json.getString("code");
//        String msg = json.getString("msg");
//        if (!"200".equals(code)){
//            throw new Exception(msg);
//        }
        if(ajaxResult.hasErr()){
            return ajaxResult;
        }
        if (u8OutParameters.getHandler() == null){
            return AjaxResult.success("材料出库成功,已生成开立状态U8材料出库单据!");
        }
        return AjaxResult.success("材料出库成功,已生成U8单据!");
    }

    @Override
    public U8OutParameters dataConversion(String jsonData) throws Exception {
        if (StringUtils.isEmpty(jsonData)){
            throw new Exception("请勿提交空数据!");
        }
        U8OutParameters outParameters = JSONObject.parseObject(jsonData, U8OutParameters.class);
        // 用户名称
        User user = ShiroUtils.getUser();
        outParameters.setUser(user.getUserName());
        for (U8Material u8Material : outParameters.getList()){
//            Inventory inventory = inventoryDetailService.selectEntityById(u8Material.getInventoryId());
            InventoryDetail inventory = inventoryDetailService.getById(u8Material.getInventoryId());
            // 判断wms库存是否存在
            if (inventory == null){
                throw new Exception("查询不到WMS库存数据,请联系管理员!");
            }
            // 判断单据是否需要审核
            if (("XN".equals(inventory.getZoneCode()) || "PK".equals(inventory.getZoneCode()))){
                outParameters.setHandler(user.getUserName());
            }
            if (u8Material.getQty().compareTo(BigDecimal.valueOf(0)) == 0){
                throw new Exception("出库数量不能为0!");
            }
            if (inventory.getQty().subtract(inventory.getTaskQty()).compareTo(u8Material.getQty()) < 0){
                throw new Exception("出库数量大于库存数量!");
            }
        }
        return outParameters;
    }

    @Override
    public Boolean importData(List<Map<String, Object>> maps, Boolean num) {
        //实体转换
        int number = 0;
        for (Map<String, Object> map : maps) {
            WorkOrderList workOrderList = new WorkOrderList();
            if(StringUtils.isNull(map.get("存货编码"))) {
                throw new ServiceException("存货编码为空");
            }
            if(StringUtils.isNull(map.get("领料数量"))) {
                throw new ServiceException("领料数量为空");
            }
            if(StringUtils.isNull(map.get("工作令号"))){
                throw new ServiceException("工作令号空");
            }
            if(StringUtils.isNull(map.get("备注"))){
                throw new ServiceException("备注空");
            }
            workOrderList.setMaterialCode(map.get("存货编码").toString());
            workOrderList.setNeedQuantity(BigDecimal.valueOf(((Long)map.get("领料数量")).intValue()));
            workOrderList.setWorkOrderNumber(map.get("工作令号").toString());
            workOrderList.setUserDef1(map.get("备注").toString());
            List<WorkOrderList> workOrderLists = workOrderListMapper.selectByOption(workOrderList);
            if(workOrderLists!=null && workOrderLists.size()>0){
                return true;
            }
            workOrderListMapper.insert(workOrderList);
        }
        return true;
    }
}