ShipmentDetailServiceImpl.java 5.99 KB
package com.huaheng.pc.shipment.shipmentDetail.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.utils.DataUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.general.material.domain.Material;
import com.huaheng.pc.general.material.service.MaterialService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.shipment.shipmentDetail.mapper.ShipmentDetailMapper;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ShipmentDetailServiceImpl extends ServiceImpl<ShipmentDetailMapper, ShipmentDetail> implements ShipmentDetailService{

    @Autowired
    private ShipmentHeaderService shipmentHeaderService;

    @Autowired
    private MaterialService materialService;
    @Resource
    private ShipmentDetailMapper shipmentDetailMapper;

    /**
     * 新增出库明细
     *
     * @param shipmentDetail
     * @return
     */
    @Override
    public AjaxResult insertDetail(ShipmentDetail shipmentDetail) {
        //查看主单是否存在
        LambdaQueryWrapper<ShipmentHeader> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ShipmentHeader::getId,shipmentDetail.getShipmentId())
                .eq(ShipmentHeader::getWarehouseCode,ShiroUtils.getWarehouseCode());
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(lambdaQueryWrapper);
        if (shipmentHeader == null) {
            return AjaxResult.error("找不到主单据");
        }
        if (shipmentHeader.getFirstStatus() > 100) {
            //表示已经加入了波次
            return AjaxResult.error("主单据状态不允许新增明细");
        }

        //查找物料
        LambdaQueryWrapper<Material> lam = Wrappers.lambdaQuery();
        lam.eq(Material::getCode,shipmentDetail.getMaterialCode())
                .eq(Material::getWarehouseCode,ShiroUtils.getWarehouseCode());
        Material material = materialService.getOne(lam);
        if (material == null) {
            return AjaxResult.error("物料未找到");
        }
        if (StringUtils.isNotEmpty(material.getCompanyCode()) &&
                ShiroUtils.getCompanyCodeList().contains(material.getCompanyCode()) == false)
        {
            return AjaxResult.error("物料不属于当前货主!");
        }
        shipmentDetail.setId(null);
        shipmentDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
        shipmentDetail.setMaterialCode(material.getCode());
        shipmentDetail.setMaterialName(material.getName());
        shipmentDetail.setReferCode(shipmentHeader.getReferCode());
        shipmentDetail.setReferId(shipmentHeader.getReferId());
        shipmentDetail.setMaterialSpec(material.getSpec());
        shipmentDetail.setMaterialUnit(material.getUnit());
        shipmentDetail.setCreated(null);
        shipmentDetail.setCreatedBy(ShiroUtils.getLoginName());
        shipmentDetail.setLastUpdated(null);
        shipmentDetail.setLastUpdatedBy(ShiroUtils.getLoginName());
        if (this.save(shipmentDetail)==true){
            //更新单据总行数与总数量
            shipmentHeader.setTotalLines(shipmentHeader.getTotalLines() + 1);
            shipmentHeader.setTotalQty(shipmentHeader.getTotalQty().add(shipmentDetail.getShipQty()));
            shipmentHeaderService.saveOrUpdate(shipmentHeader);
            return AjaxResult.success("新增单据明细成功");
        }
        else
            return AjaxResult.error("新增单据明细失败");
    }


    /**
     * 删除出库单据明细
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public AjaxResult deleteDetail(String id) {
        if (StringUtils.isEmpty(id))
            return AjaxResult.error("id不能为空");
        String[] ids = id.split(",");
        List<Map<String,Integer>> list = shipmentDetailMapper.SelectFirstStatus(id);
        if (list.size() < 1) {
            return AjaxResult.error("找不到主单据!");
        }
        if (list.size() > 1) {
            return AjaxResult.error("有多个主单据,不能一起删除!");
        }
        if (list.get(0).get("firstStatus") > 100) {
            return AjaxResult.error("单据状进入订单池,不允许删除明细");
        }
        Integer result = shipmentDetailMapper.batchDelete(ids);
        if (result > 0) {
            Integer headerId=list.get(0).get("id");
            Map<String,String> map= shipmentDetailMapper.StatisticalByReceiptId(headerId);
            if(DataUtils.getInteger(map.get("totalLines")) <= 0)    {
                shipmentHeaderService.removeById(headerId);
            }
            else    {
                //更新表头的总行数和总数量统计
                ShipmentHeader shipmentHeader = new ShipmentHeader();
                shipmentHeader.setId(headerId);
                shipmentHeader.setTotalLines(DataUtils.getInteger(map.get("totalLines")));
                shipmentHeader.setTotalQty(DataUtils.getBigDecimal(map.get("totalQty")));
                shipmentHeaderService.saveOrUpdate(shipmentHeader);
            }
            return AjaxResult.success("删除单据明细成功");
        }
        else
            return AjaxResult.error("删除单据明细失败");
    }

    @Override
    public Integer countUnCompleted(Integer shipmentId) {
        return shipmentDetailMapper.countUnCompleted(shipmentId);
    }
}