ShipmentICSmomOrder.java 10.3 KB
package com.huaheng.pc.shipment.shipmentHeader.controller;

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.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.aspectj.lang.annotation.Log;
import com.huaheng.framework.aspectj.lang.constant.BusinessType;
import com.huaheng.framework.web.controller.BaseController;
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.company.domain.Company;
import com.huaheng.pc.config.company.domain.CompanyU8;
import com.huaheng.pc.config.company.service.CompanyU8Service;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.u8.domain.*;
import com.huaheng.pc.u8.service.ICSReceiptDetailService;
import com.huaheng.pc.u8.service.ICSReceiptHeaderService;
import com.huaheng.pc.u8.service.ICSShipmentDetailService;
import com.huaheng.pc.u8.service.ICSShipmentHeaderService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping("/shipment/shipmentICSmomOrder")
public class ShipmentICSmomOrder extends BaseController {

    @Autowired
    private ShipmentHeaderService shipmentHeaderService;
    @Autowired
    private ShipmentDetailService shipmentDetailService;
    @Autowired
    private ICSReceiptHeaderService icsReceiptHeaderService;
    @Autowired
    private ICSReceiptDetailService icsReceiptDetailService;
    @Resource
    private ICSShipmentHeaderService icsShipmentHeaderService;
    @Resource
    private ICSShipmentDetailService icsShipmentDetailService;
    @Resource
    private CompanyU8Service companyU8Service;
    @Autowired
    private AddressService addressService;
    private String prefix = "shipment/shipmentICSmomOrder";
    @RequiresPermissions("shipment:bill:view")
    @GetMapping()
    public String shipmentHeader()
    {
        return prefix + "/shipmentICSmomOrder";
    }

    /**
     * 查询生产订单表
     */
    @RequiresPermissions("shipment:bill:list")
    @Log(title = "生产订单表", operating="查看生产订单表", action = BusinessType.GRANT)
    @PostMapping("/getICSmomOrder")
    @ResponseBody
    public AjaxResult list(ICSShipmentHeader shipmentHeader)
    {
        AjaxResult ajaxResult=AjaxResult.success();
        CompanyU8 one = companyU8Service.getOne(new LambdaQueryWrapper<CompanyU8>()
                .eq(CompanyU8::getCompanyCode, ShiroUtils.getCompanyCode())
                .eq(CompanyU8::getWarehouseCode, ShiroUtils.getWarehouseCode()));
        shipmentHeader.setType("OO");
        List<ICSShipmentHeader> icsShipmentHeaders = icsShipmentHeaderService.list(new LambdaQueryWrapper<ICSShipmentHeader>()
                .eq(ICSShipmentHeader::getType, shipmentHeader.getType())
                .eq(StringUtils.isNotNull(shipmentHeader.getMoId()),ICSShipmentHeader::getMoId, shipmentHeader.getMoId())
                .eq(!StringUtils.isEmpty(shipmentHeader.getMoCode()),ICSShipmentHeader::getMoCode, shipmentHeader.getMoCode())
                .eq(!StringUtils.isEmpty(shipmentHeader.getCMaker()),ICSShipmentHeader::getCMaker, shipmentHeader.getCMaker())
                .eq(ICSShipmentHeader::getCompanyCode,one.getUCompanyCode()));
        ajaxResult.setData(icsShipmentHeaders);
        return ajaxResult;
    }

    /**
     * 获取库存详情
     * @param
     * @return
     */
    @ApiOperation("获取生产订单明细")
    @Log(title = "获取生产订单明细", action = BusinessType.OTHER)
    @PostMapping("/getICSmomOrderDetail")
    @ResponseBody
    public AjaxResult getICSmomOrderDetail(ICSShipmentDetail icsShipmentDetail){
        AjaxResult ajaxResult = AjaxResult.success();
        List<ICSShipmentDetail> list;
        try {
            list = icsShipmentDetailService.list(new LambdaQueryWrapper<ICSShipmentDetail>()
                    .eq(ICSShipmentDetail::getMoId,icsShipmentDetail.getMoId())
                    .eq(ICSShipmentDetail::getMoCode,icsShipmentDetail.getMoCode()));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        ajaxResult.setData(list);
        return ajaxResult;
    }

    @ApiOperation("成品入库")
    @Log(title = "成品入库", action = BusinessType.OTHER)
    @GetMapping("/inStorage")
    @ResponseBody
    public AjaxResult inStorage(String parameters){
        try {
            if (StringUtils.isEmpty(parameters)){
                throw new Exception("请勿提交空数据!");
            }
            final JSONObject jsonObject1 = JSONObject.parseObject(parameters);
            ICSReceiptModel icsReceiptModel=new ICSReceiptModel();
            final JSONArray list = jsonObject1.getJSONArray("list");
            if(StringUtils.isNotNull(list)){
                for(int i=0;i<list.size();i++){
                    final JSONObject jsonObject = list.getJSONObject(i);
                    entitySetParameter(jsonObject, icsReceiptModel);
                }
                return setToU8(icsReceiptModel);
            }else {
                entitySetParameter(jsonObject1,icsReceiptModel);
                return setToU8(icsReceiptModel);
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    //获取U8产成品入库的接口路径
    public String getInStorageURL() throws Exception {
        String url = addressService.selectAddress("U8",ShiroUtils.getWarehouseCode());
        if (StringUtils.isEmpty(url)){
            throw new Exception("未获取到出库URL,请检查url地址配置是否完整!");
        }
        return url;
    }
    //实体参数
    public void entitySetParameter(JSONObject jsonObject,ICSReceiptModel icsReceiptModel) throws Exception{
        final Integer moId = jsonObject.getInteger("MoId");
        final String moCode = jsonObject.getString("MoCode");
        final Integer modID = jsonObject.getInteger("modID");
        final BigDecimal qty = jsonObject.getBigDecimal("qty");
        CompanyU8 companyU8 = companyU8Service.getCompanyU8ByCode(ShiroUtils.getCompanyCode(), ShiroUtils.getWarehouseCode());
        List<ICSReceiptDetail> icsReceiptDetails=new ArrayList<>();
        if(moId!=null && moCode!=null &&modID!=null &&qty!=null) {
            if(StringUtils.isNull(icsReceiptModel.getIcsReceiptHeader())){
                ICSShipmentHeader icsShipmentHeader = icsShipmentHeaderService.getOne(new LambdaQueryWrapper<ICSShipmentHeader>()
                        .eq(ICSShipmentHeader::getMoCode,moCode)
                        .eq(ICSShipmentHeader::getMoId,moId));
                ICSReceiptHeader icsReceiptHeader=new ICSReceiptHeader();
                icsReceiptHeader.setCBusType("产品入库");
                icsReceiptHeader.setVoucherType("CreateICSRdRecord_10");
                icsReceiptHeader.setCompanyCode(companyU8.getUCompanyCode());
                icsReceiptHeader.setCMaker(icsShipmentHeader.getCMaker());
                icsReceiptHeader.setCMemo(icsShipmentHeader.getCMemo());
                icsReceiptHeader.setCDepCode(icsShipmentHeader.getCDepCode());
                icsReceiptHeader.setDPODate(icsShipmentHeader.getDDate());
                if (StringUtils.isNotEmpty(icsShipmentHeader.getCWHCode())) {
                    icsReceiptHeader.setCWHCode(icsShipmentHeader.getCWHCode());
                }else {
                    icsReceiptHeader.setCWHCode("20");
                }
                icsReceiptModel.setIcsReceiptHeader(icsReceiptHeader);
            }
            if(StringUtils.isNotNull(icsReceiptModel.getIcsReceiptDetails())){
                for(ICSReceiptDetail i:icsReceiptModel.getIcsReceiptDetails()){
                    icsReceiptDetails.add(i);
                }
            }
            List<ICSShipmentDetail> icsShipmentDetails = icsShipmentDetailService.list(new LambdaQueryWrapper<ICSShipmentDetail>()
                    .eq(ICSShipmentDetail::getMoId,moId)
                    .eq(ICSShipmentDetail::getMoCode,moCode)
                    .eq(ICSShipmentDetail::getMoDId,modID));
            for (ICSShipmentDetail icsShipmentDetail1 : icsShipmentDetails) {
                ICSReceiptDetail icsReceiptDetail=new ICSReceiptDetail();
                icsReceiptDetail.setIMPoIds(modID);
                icsReceiptDetail.setIQuantity(qty);
                icsReceiptDetail.setCItemName(icsShipmentDetail1.getCInvName());
                icsReceiptDetail.setCInvCode(icsShipmentDetail1.getCInvCode());
                icsReceiptDetails.add(icsReceiptDetail);
            }
            icsReceiptModel.setIcsReceiptDetails(icsReceiptDetails);
        }
    }
    //发送请求
    public AjaxResult setToU8(ICSReceiptModel icsShipmentModel) throws Exception {
        String jsonString = JSONObject.toJSONString(icsShipmentModel);
        final String inStorageURL = getInStorageURL();
        String r = HttpUtils.bodypost(inStorageURL, jsonString, ShiroUtils.getWarehouseCode());
        System.out.println(jsonString);
        if (StringUtils.isEmpty(r)) {
            throw new Exception("入库失败!");
        }
        AjaxResult ajaxResult = JSON.parseObject(r,AjaxResult.class);
//        JSONObject json = JSONObject.parseObject(r);
//        String code = json.getString("code");
//        String msg = json.getString("msg");
        if (ajaxResult.hasErr()) {
            throw new Exception(ajaxResult.getMsg());
        }
        return AjaxResult.success("成品入库成功!");
    }
}