ShipmentApi.java 6.02 KB
package com.huaheng.api.Common;

import com.alibaba.fastjson.JSON;
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.general.company.service.ICompanyService;
import com.huaheng.pc.general.material.domain.Material;
import com.huaheng.pc.general.material.service.IMaterialService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.IShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentModel;
import com.huaheng.pc.shipment.shipmentHeader.service.IShipmentHeaderService;
import com.huaheng.pc.system.dict.service.IDictDataService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 *  出库下发和回传接口
 *    @author huaheng
 *    @date 2018-12-18
 *
 *
 *
 */
@RestController
@RequestMapping("/api/shipment")
@Api(tags = {"shipment"}, description = "出库接口")
public class ShipmentApi extends BaseController {


    @Autowired
    private IDictDataService dictDataService;
    @Autowired
    IShipmentDetailService shipmentDetailService;
    @Autowired
    ICompanyService companyService;
    @Autowired
    IShipmentHeaderService shipmentHeaderService;
    @Autowired
    private IMaterialService materialService;

    //出库单下发
    @Log(title = "出库单下发", action = BusinessType.INSERT)
    @PostMapping("/insertShipment")
    @ResponseBody
    public AjaxResult insertShipment(ShipmentModel shipmentModel){
        if(shipmentModel==null){
            return AjaxResult.error("参数不对");
        }
        ShipmentHeader shipmentHeader=new ShipmentHeader();
        List<ShipmentDetail> shipmentDetails=new ArrayList<>();
        try {
            shipmentHeader=shipmentModel.getShipmentHeader();
            shipmentDetails=shipmentModel.getShipmentDetails();
        }catch (Exception e){
            return AjaxResult.error("出库主单或出库子单有问题");
        }
        if(dictDataService.checkConfig("shipmentType", shipmentHeader.getType()) == false)
        {
            return  AjaxResult.error("没有对应的出库单类型");
        }
        int result = shipmentHeaderService.insert(shipmentHeader);
        if(result<1){
            return AjaxResult.error("新增主单失败");
        }

        //添加细表到shipmentDetail
        for(ShipmentDetail shipmentDetail : shipmentDetails) {
            ShipmentHeader shipmentHeaderB = shipmentHeaderService.selectEntityById(shipmentDetail.getShipmentId());
            if (!shipmentHeaderB.equals(shipmentHeader)) {
                return AjaxResult.error("找不到主单据");
            }
            if (shipmentHeaderB.getFirstStatus() > 100) {
                //表示已经加入了波次
                return AjaxResult.error("主单据状态不允许新增明细");
            }
            Material material = new Material();
            material.setCode(shipmentDetail.getMaterialCode());
            material.setWarehouseId(shipmentDetail.getWarehouseId());
            material = materialService.selectFirstEntity(material);
            if (material == null) {
                return AjaxResult.error("物料未找到");
            }
            if (StringUtils.isNotEmpty(material.getCompanyCode()) &&
                    ShiroUtils.getCompanyCodeList().contains(material.getCompanyCode()) == false)
            {
                return AjaxResult.error("物料不属于当前货主!");
            }

            if (shipmentDetailService.insert(shipmentDetail) > 0){
                //更新单据总行数与总数量
                shipmentHeader.setTotalLines(shipmentHeader.getTotalLines() + 1);
                shipmentHeader.setTotalQty(shipmentHeader.getTotalQty().add(shipmentDetail.getQty()));
                shipmentHeaderService.updateByModel(shipmentHeader);
            }
            else {
                return AjaxResult.error("新增单据明细失败");
            }
        }
        return AjaxResult.success("出库单下发成功");
    }



    //出库单回传
    @Log(title = "出库单回传", action = BusinessType.INSERT)
    @PostMapping("/shipmentAssign")
    @ResponseBody
    public AjaxResult shipmentAssign(String url)
    {
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        shipmentHeader.setLastStatus(300);
        shipmentHeader = shipmentHeaderService.selectFirstEntity(shipmentHeader);
        if (shipmentHeader == null) {
            return AjaxResult.error("出库回传主单不存在");
        }
        ShipmentDetail shipmentDetail = new ShipmentDetail();
        shipmentDetail.setShipmentCode(shipmentHeader.getCode());
        List<ShipmentDetail> shipmentDetails = shipmentDetailService.selectListEntityByEqual(shipmentDetail);
        if (shipmentDetails == null) {
            return AjaxResult.error("出库回传子单不存在");
        }
        ShipmentModel model=new ShipmentModel();
        model.setShipmentHeader(shipmentHeader);
        model.setShipmentDetails(shipmentDetails);
        String JsonParam = JSON.toJSONString(model);
        String result = HttpUtils.sendPost(url, JsonParam);
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        shipmentHeader.setFirstStatus(900);
        shipmentHeader.setLastStatus(900);
        shipmentHeaderService.updateByModel(shipmentHeader);
        return ajaxResult;
    }


}