ShipmentHeaderServiceImpl.java 4.88 KB
package com.huaheng.pc.shipment.shipmentHeader.service;

import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.system.dict.service.IDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.mapper.ShipmentHeaderMapper;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
@Service
public class ShipmentHeaderServiceImpl extends ServiceImpl<ShipmentHeaderMapper, ShipmentHeader> implements ShipmentHeaderService{

    @Autowired
    private IDictDataService dictDataService;
    @Resource
    private ShipmentHeaderMapper shipmentHeaderMapper;
    @Autowired
    private ShipmentDetailService shipmentDetailService;
    @Autowired
    private ShipmentContainerHeaderService shipmentContainerHeaderService;


    //新增出库主单
    @Override
    public AjaxResult<Boolean> saveHeader(ShipmentHeader shipmentHeader) {
        if(dictDataService.checkConfig("shipmentType", shipmentHeader.getShipmentType()) == false)
        {
            return  AjaxResult.error("没有对应的出库单类型");
        }
        String code = createCode(shipmentHeader.getShipmentType());
        shipmentHeader.setId(null);
        shipmentHeader.setFirstStatus(0);
        shipmentHeader.setLastStatus(0);
        shipmentHeader.setLastUpdated(null);
        shipmentHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
        shipmentHeader.setCreated(null);
        shipmentHeader.setCreatedBy(ShiroUtils.getLoginName());
        shipmentHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        shipmentHeader.setCode(code);
        boolean result = this.save(shipmentHeader);
        return  AjaxResult.toAjax(result);
    }


    //根据单据类型建单据号
    public String createCode(String shipmentType)
    {
        String code = null;
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String maxCode = shipmentHeaderMapper.createCode(shipmentType);
        if (maxCode != null && maxCode.length() > 13 && maxCode.substring(maxCode.length() - 13, maxCode.length() - 5).equals(df.format(now)))
        {
            Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 5, maxCode.length()));
            code = shipmentType + df.format(now) + String.format("%05d", Count + 1);
        }
        else
        {
            code = shipmentType + df.format(now) + "00001";
        }
        return code;
    }

    /**
     * 根据Id更新这个单据的首尾状态
     * @param shipmentId
     * @return
     */
    @Override
    public AjaxResult updateShipmentStatus(int shipmentId)  {
        //获取这个单
        ShipmentHeader shipmentHeader = this.getById(shipmentId);
        if(shipmentHeader==null){
            return AjaxResult.error("单据未找到,Id:"+shipmentId);
        }
        //查询是否有生成出库货箱,如果有,则返回出库货箱的最高与最低状态
        Map<String,Integer> map = shipmentContainerHeaderService.getShipmentContainerMaxAndMinStatusByShipmentID(shipmentId);
        if(map==null){
            //说明没有货箱,则直接首位均为新建
            shipmentHeader.setFirstStatus(0);
            shipmentHeader.setLastStatus(0);
            this.saveOrUpdate(shipmentHeader);
        }else {
            int firstStatus = map.get("maxStatus");
            int lastStatus = map.get("minStatus");
            if(firstStatus<20){
                shipmentHeader.setFirstStatus(200);
            }
            if(firstStatus==20){
                shipmentHeader.setFirstStatus(300);
            }
            if(firstStatus==30){
                shipmentHeader.setFirstStatus(500);
            }
            if(lastStatus<20){
                shipmentHeader.setLastStatus(200);
            }
            if(lastStatus==20){
                shipmentHeader.setLastStatus(300);
            }
            if(lastStatus==30){
                shipmentHeader.setLastStatus(500);
            }
            //是否存在未配盘的数量,如果是,则尾状态为新建
            Integer UnCompleted = shipmentDetailService.countUnCompleted(shipmentId);
            if(UnCompleted != null && UnCompleted.intValue() > 0){
                shipmentHeader.setLastStatus(0);
            }
            this.saveOrUpdate(shipmentHeader);
        }

        return AjaxResult.success("");

    }
}