ReceiptApi.java 5.65 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.material.domain.Material;
import com.huaheng.pc.general.material.service.IMaterialService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.IReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptModel;
import com.huaheng.pc.receipt.receiptHeader.service.IReceiptHeaderService;
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;

/**
 *  入库下发和入传接口
 *    @author huaheng
 *    @date 2018-12-20
 *
 *
 *
 */
import java.util.List;

@RestController
@RequestMapping("/api/receipt")
@Api(tags = {"receipt"}, description = "入库接口")
public class ReceiptApi extends BaseController {

    @Autowired
    private IDictDataService dictDataService;
    @Autowired
    private IReceiptHeaderService receiptHeaderService;
    @Autowired
    private IReceiptDetailService receiptDetailService;
    @Autowired
    private IMaterialService materialService;



    @Log(title = "入库单下发", action = BusinessType.INSERT)
    @PostMapping("/insertReceipt")
    @ResponseBody
    public AjaxResult insertReceipt(ReceiptModel receiptModel){
        if(receiptModel==null){
            return AjaxResult.error("参数不对");
        }
        ReceiptHeader receiptHeader=receiptModel.getReceiptHeader();
        List<ReceiptDetail> receiptDetails=receiptModel.getReceiptDetails();
        if(receiptHeader==null || receiptDetails==null){
            return AjaxResult.error("入库主单或入库子单有问题");
        }

        if(dictDataService.checkConfig("receiptType", receiptHeader.getType()) == false)
        {
            return  AjaxResult.error("没有对应的入库单类型");
        }
        Integer result = receiptHeaderService.insert(receiptHeader);
        if(result<1){
            return AjaxResult.error("新增主单失败");
        }


        for(ReceiptDetail item :receiptDetails){
            ReceiptHeader receiptHeaderB=receiptHeaderService.selectEntityById(item.getReceiptId());
            if(!receiptHeader.equals(receiptHeaderB)){
                return AjaxResult.error("主子单据不匹配");
            }
            if (receiptHeader.getFirstStatus() > 100) {
                //表示已经加入了波次
                return AjaxResult.error("主单据状态不允许新增明细");
            }

            Material material = new Material();
            material.setCode(item.getMaterialCode());
            material.setWarehouseId(item.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(receiptDetailService.insert(item) > 0){
                //更新表头的总行数和总数量统计
                receiptHeader.setTotalQty(receiptHeader.getTotalQty().add(item.getQty()));
                receiptHeader.setTotalLines(receiptHeader.getTotalLines() + 1);
                receiptHeaderService.updateByModel(receiptHeader);
            }
            else {
                return AjaxResult.error("新增单据明细失败");
            }
        }
        return AjaxResult.success("入库单据下发成功");
    }




    //出库单回传
    @Log(title = "入库单回传", action = BusinessType.INSERT)
    @PostMapping("/shipmentAssign")
    @ResponseBody
    public AjaxResult shipmentAssign(String url)
    {
        ReceiptHeader receiptHeader = new ReceiptHeader();
        receiptHeader.setLastStatus((short)300);
        receiptHeader = receiptHeaderService.selectFirstEntity(receiptHeader);
        if (receiptHeader == null) {
            return AjaxResult.error("入库回传主单不存在");
        }
        ReceiptDetail receiptDetail = new ReceiptDetail();
        receiptDetail.setReceiptCode(receiptHeader.getCode());
        List<ReceiptDetail> receiptDetails = receiptDetailService.selectListEntityByEqual(receiptDetail);
        if (receiptDetails == null) {
            return AjaxResult.error("入库回传子单不存在");
        }
        ReceiptModel model=new ReceiptModel();
        model.setReceiptHeader(receiptHeader);
        model.setReceiptDetails(receiptDetails);
        String JsonParam = JSON.toJSONString(model);
        String result = HttpUtils.sendPost(url, JsonParam);
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        receiptHeader.setFirstStatus((short)900);
        receiptHeader.setLastStatus((short)900);
        receiptHeaderService.updateByModel(receiptHeader);
        return ajaxResult;
    }
}