ReceiptHeaderServiceImpl.java 13.2 KB
package com.huaheng.pc.receipt.receiptHeader.service;


import com.huaheng.api.SSP.service.OrderAssignApiService;
import com.huaheng.api.U8.domain.ICSReceiptHeader;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.mobile.receipt.ReceiptResult;
import com.huaheng.pc.general.material.domain.Material;
import com.huaheng.pc.general.material.service.IMaterialService;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
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.mapper.ReceiptHeaderMapperAuto;
import com.huaheng.pc.receipt.receiptHeader.mapper.ReceiptHeaderMapper;
import com.huaheng.pc.system.dict.service.IDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;


/**
 * 入库单主 服务层实现
 * 
 * @author huaheng
 * @date 2018-08-19
 */
@Service
public class ReceiptHeaderServiceImpl  implements IReceiptHeaderService {

    @Autowired
    IReceiptDetailService receiptDetailService;
    @Autowired
    IDictDataService dictDataService;
    @Resource
    private ReceiptHeaderMapperAuto aotuMapper;
    @Resource
    private ReceiptHeaderMapper receiptHeaderMapper;
    @Autowired
    private IMaterialService materialService;
    @Autowired
    IReceiptHeaderService receiptHeaderService;
    @Autowired
    private OrderAssignApiService orderAssignApiService;

    public List<ReceiptHeader> selectListEntityByLike(ReceiptHeader condition) {
        return aotuMapper.selectListEntityByLike(condition);
    }

    public List<ReceiptHeader> selectListEntityByEqual(ReceiptHeader condition) {
        return aotuMapper.selectListEntityByEqual(condition);
    }

    public ReceiptHeader selectFirstEntity(ReceiptHeader condition) {
        ReceiptHeader item = aotuMapper.selectFirstEntity(condition);
        return item;
    }

    public ReceiptHeader selectEntityById(Integer id) {
        return aotuMapper.selectEntityById(id);
    }

    public List<Map<String, Object>> selectListMapByEqual(String columns, ReceiptHeader condition) {
        return aotuMapper.selectListMapByEqual(columns, condition);
    }

    public Map<String, Object> selectFirstMap(String columns, ReceiptHeader condition) {
        Map<String, Object> item = aotuMapper.selectFirstMap(columns, condition);
        return item;
    }

    public int insert(ReceiptHeader record) {
        return aotuMapper.insert(record);
    }

    public int updateByModel(ReceiptHeader record) {
        return aotuMapper.updateByModel(record);
    }

    public int updateByCondition(ReceiptHeader record, ReceiptHeader condition) {
        return aotuMapper.updateByCondition(record, condition);
    }

    public int deleteById(Integer id) {
        return aotuMapper.deleteById(id);
    }

    public int deleteByCondition(ReceiptHeader condition) {
        return aotuMapper.deleteByCondition(condition);
    }


    public String createCode(String receiptType)
    {
        String code = null;
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String maxCode = receiptHeaderMapper.createCode(receiptType);
        //如果指定类型的最后的code存在,并且日期一致。那么 code = 入库单类型 + 年月日 + (排序号 + 1)
        if (maxCode != null && maxCode.substring(maxCode.length() - 13, maxCode.length() - 5).equals(df.format(now)))
        {
            Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 5, maxCode.length()));
            code = receiptType + df.format(now) + String.format("%05d", Count + 1);
        }
        else
        {
            code = receiptType + df.format(now) + "00001";
        }
        return code;
    }

    @Override
    public AjaxResult<Boolean> insertHeader(ReceiptHeader receiptHeader)  {
        if(dictDataService.checkConfig("receiptType", receiptHeader.getType()) == false)
        {
            return  AjaxResult.error("没有对应的入库单类型");
        }
        String code = createCode(receiptHeader.getType());
        receiptHeader.setId(null);
        receiptHeader.setCode(code);
        receiptHeader.setWarehouseId(ShiroUtils.getWarehouseId());
        receiptHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        receiptHeader.setCreated(null);
        receiptHeader.setCreatedBy(ShiroUtils.getLoginName());
        receiptHeader.setLastUpdated(null);
        receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
        receiptHeader.setUploadStatus(0);
        Integer result = this.insert(receiptHeader);
        return  AjaxResult.toAjax(result > 0);
    }

    @Override
    public AjaxResult updateHeader(ReceiptHeader receiptHeader) {
        ReceiptHeader entity = this.selectEntityById(receiptHeader.getId());
        if (entity.getFirstStatus() > 100)
            return  AjaxResult.error("状态不允许修改!");
        receiptHeader.setCode(null);
        receiptHeader.setType(null);
        receiptHeader.setWarehouseId(null);
        receiptHeader.setWarehouseCode(null);
        receiptHeader.setFirstStatus(null);
        receiptHeader.setLastStatus(null);
        receiptHeader.setEnable(receiptHeader.getEnable() == null ? false:receiptHeader.getEnable());
        receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
        this.updateByModel(receiptHeader);
        return  AjaxResult.success("修改成功!");
    }

    @Override
    public Integer createTodayHeader(String companyId, String companyCode) {
        String receiptType = "PPI";
        String code = null;
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String maxCode = receiptHeaderMapper.createCode(receiptType);
        //如果指定类型的最后的code存在,并且日期一致。那么 code = 入库单类型 + 年月日 + (排序号 + 1)
//        if (maxCode != null && maxCode.substring(maxCode.length() - 13, maxCode.length() - 5).equals(df.format(now)))
//        {
//            Integer Count = Integer.valueOf(maxCode.substring(maxCode.length() - 5, maxCode.length()));
//            code = receiptType + df.format(now) + String.format("%05d", Count + 1);
//        }
//        else
//        {
            code = receiptType + df.format(now) + "00001";
//        }
        ReceiptHeader receiptHeader = new ReceiptHeader();
        receiptHeader.setCode(code);
        ReceiptHeader receiptHeader1 = this.selectFirstEntity(receiptHeader);
        if(receiptHeader1 != null) {
            return receiptHeader1.getId();
        }
        receiptHeader.setId(null);
        receiptHeader.setType(receiptType);
        receiptHeader.setWarehouseId(ShiroUtils.getWarehouseId());
        receiptHeader.setWarehouseCode(ShiroUtils.getWarehouseCode());
        receiptHeader.setCreated(null);
        receiptHeader.setCreatedBy(ShiroUtils.getLoginName());
        receiptHeader.setLastUpdated(null);
        receiptHeader.setLastUpdatedBy(ShiroUtils.getLoginName());
        receiptHeader.setUploadStatus(0);
        receiptHeader.setFirstStatus((short) 200);
        receiptHeader.setLastStatus((short) 200);
        receiptHeader.setCompanyId(Integer.parseInt(companyId));  //货主Id
        receiptHeader.setCompanyCode(companyCode);
        receiptHeader.setSupplierCode("CS0002");
        this.insert(receiptHeader);
        return receiptHeader.getId();
    }

    @Override
    public ReceiptHeader selectLastOne() {
        ReceiptHeader receiptHeader = receiptHeaderMapper.selectLastOne();
        return receiptHeader;
    }

    @Override
    public ReceiptHeader getReceipt(ReceiptHeader receiptHeader) {
        ReceiptHeader receipt = receiptHeaderMapper.getReceipt(receiptHeader);
        return receipt;
    }

    @Override
    public ReceiptHeader selectNosScanSSP(String code) {
        return receiptHeaderMapper.selectNosScanSSP(code);
    }

    @Override
    @Transactional
    public AjaxResult<Integer> deleteHeaderAndDetail(Integer id)  {
        ReceiptHeader receiptHeader = this.selectEntityById(id);
        if (receiptHeader == null)  return AjaxResult.success("成功!");
        ReceiptDetail condition = new ReceiptDetail();
        condition.setReceiptCode(receiptHeader.getCode());
        if (receiptHeader.getFirstStatus() < 100)
        {
            this.deleteById(id);
            receiptDetailService.deleteByCondition(condition);
            return  AjaxResult.success("成功删除!");
        }
        else if (receiptHeader.getFirstStatus() < 120)
        {
            ReceiptHeader headerRecord = new ReceiptHeader();
            headerRecord.setId(id);
            headerRecord.setDeleted(true);
            headerRecord.setLastUpdatedBy(ShiroUtils.getLoginName());
            this.updateByModel(headerRecord);

            ReceiptDetail detailRecord = new ReceiptDetail();
            detailRecord.setDeleted(true);
            detailRecord.setLastUpdatedBy(ShiroUtils.getLoginName());
            receiptDetailService.updateByCondition(detailRecord, condition);
            return  AjaxResult.success("成功删除!");
        }
        else
        {
            return AjaxResult.error("单据("+ receiptHeader.getCode() +")状态超过120已经不能删除");
        }
    }

    @Override
    public AjaxResult<Integer> checkStatus(Integer headerId, Short status){
        ReceiptHeader receiptHeader = this.selectEntityById(headerId);
        if (receiptHeader == null)
        {
            throw new ServiceException("数据错误,没有表头!");
        }
        else if (receiptHeader.getFirstStatus() > status)
        {
            throw new ServiceException("单据状态超过" + status + "已经不可修改!");
        }
        else
        {
            return   AjaxResult.success("");
        }
    }

    @Override
    public AjaxResult<Integer> checkStatus(String receiptCode, Short status)  {
        ReceiptHeader condition = new ReceiptHeader();
        condition.setCode(receiptCode);
        List<ReceiptHeader> receiptHeaders = this.selectListEntityByEqual(condition);
        if (receiptHeaders.size() < 1)
        {
            throw   new ServiceException("数据错误,没有表头!");
        }
        else if (receiptHeaders.get(0).getFirstStatus() > status)
        {
            throw new ServiceException("单据状态超过" + status + "已经不可修改!");
        }
        else
        {
            return   AjaxResult.success("");
        }
    }

    /**
     *  修改单据状态
     * @param receiptId
     * @param firstStatus
     */
    @Override
    public void receiptStatusUpdate(Integer receiptId, Short firstStatus) {
        Short lastStatus = receiptHeaderMapper.getLastStatus(receiptId);
        receiptHeaderMapper.updateHeaderStatus(receiptId, firstStatus, lastStatus);
    }


    @Override
    public void updateDetailStatus(Integer receiptDetailId,  Short status) {
        receiptHeaderMapper.updateDetailStatus(receiptDetailId, status);
    }

    @Override
    public AjaxResult receive(String code)
    {
        AjaxResult ajaxResult=new AjaxResult();
        String result=null;
        ReceiptDetail receiptDetail=new ReceiptDetail();
        receiptDetail.setReceiptCode(code);
        HashSet<String> set=new HashSet();
        List<ReceiptDetail> list=receiptDetailService.selectListEntityByEqual(receiptDetail);
        if(list.size()==0){
            ajaxResult.setCode(200);
        }else {
            for (ReceiptDetail item : list) {
                String zc = item.getZoneCode();
                if (zc == null) {
                    zc = "";
                }
                set.add(zc);
            }
            if (set.size() == 1 && set.stream().anyMatch(X->X.equals("AGV"))) {
                ajaxResult.setCode(300);
            }
            if (!set.stream().anyMatch(X->X.equals("AGV"))) {
                ajaxResult.setCode(200);
            }
            if (set.size() > 1 && set.stream().anyMatch(X->X.equals("AGV"))) {
                ajaxResult.setCode(400);
            }
        }
        if (set.stream().anyMatch(X->X.equals("QG"))) {
            ReceiptHeader item =  this.selectNosScanSSP(code);
            if (item != null ){
                AjaxResult ajaxResults=orderAssignApiService.receiptAssign(code);
            if(!ajaxResults.hasErr()){
                ajaxResult.setMsg("单据下发成功");
            }
        }else {
            ajaxResult.setMsg("单据已下发");
        }
        }
        if (set.stream().anyMatch(X->X.equals(""))) {
            ajaxResult.setCode(500);
            ajaxResult.setMsg("请先分配库区");
        }
        return ajaxResult;
    }

    @Override
    public List<ReceiptHeader> selectListByCreated() {
        return receiptHeaderMapper.selectListByCreated();
    }

}