RejectService.java 9.51 KB
package cn.csbr.app.service;

import cn.csbr.app.config.FxConfigure;
import cn.csbr.app.gui.GUIContext;
import cn.csbr.app.model.InboundItem;
import cn.csbr.app.model.RfidDataModel;
import cn.csbr.springboot.dao.mapper.*;
import cn.csbr.springboot.dao.model.*;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.csbr.mybatis.CommonMapper;

/**
 * 退货业务类
 */
@Service
public class RejectService {
    private static Logger logger = LoggerFactory.getLogger(RejectService.class);
    private static Logger log = LoggerFactory.getLogger(InboundService.class);
    @Autowired
    private CommonMapper commonMapper;
    @Autowired
    private CabinetService cabinetService;
    @Autowired
    private GUIContext guiContext;
    @Autowired
    private SpdWvdetailMapper spdWvdetailMapper;
    @Autowired
    private SpdWvdetailScendMapper spdWvdetailScendMapper;
    @Autowired
    private SpdWvdetailLogMapper spdWvdetailLogMapper;

    @Autowired
    private SpdWvdetailScendLogMapper spdWvdetailScendLogMapper;

    @Autowired
    private FxConfigure fxConfigure;
    @Autowired
    private MfcubestockdetailMapper mfcubestockdetailMapper;
    @Autowired
    private MfcubestockstatementMapper mfcubestockstatementMapper;
    @Autowired
    private MfcubestockMapper mfcubestockMapper;
    @Autowired
    private BaseService baseService;

    public synchronized List<InboundItem> queryInboundItem(String inboundNo, Map<Cabinet, List<String>> scanData, String goodsInfo, boolean ifNeedMacthRfid) {
        String goodsInfol = null;
        Example condition = new Example(Mfcubestockdetail.class);
        Example.Criteria cr = condition.createCriteria().andEqualTo("rkbillno", inboundNo);
        if (!StringUtils.isEmpty(goodsInfo)) {
            goodsInfol = "%" + goodsInfo + "%";
            String sql = "  GoodsName like '" + goodsInfol + "' or LabGuid like'" + goodsInfol + "'";
            cr.andCondition("(" + sql + ")");
        }
        cr.andEqualTo("depotGuid",guiContext.getLoginUserVali().getOrgGuid());
        cr.andCondition(" CubeGuid in (select guid from  cabinet where groupcode='" + fxConfigure.getCabinetGroupCode() + "')");
        List<Mfcubestockdetail> mfcubestockdetails = mfcubestockdetailMapper.selectByExample(condition);
        List<InboundItem> inboundItems = new ArrayList();
        Set<Cabinet> cabinets = scanData.keySet();

        for (Mfcubestockdetail data : mfcubestockdetails) {
            InboundItem inboundItem = new InboundItem();
            inboundItems.add(inboundItem);
            inboundItem.setScanQty(Double.valueOf(data.getNumber()).intValue());
            inboundItem.setSrcOrderCode(data.getRkBillNo());
            inboundItem.setQty(Double.valueOf(data.getNumber()).intValue());
            inboundItem.setGoodsguid(data.getGoodsGUID());
//                        inboundItem.setGoodscode(data.getG);
            inboundItem.setGoodsname(data.getGoodsName());
//                        inboundItem.setGoodsspec(data.get);
            inboundItem.setUnit(data.getUnit());
            inboundItem.setIfScand((byte) 0);
            inboundItem.setSupplierName(data.getSupplierName());
            inboundItem.setSupplierGuid(data.getSupplierGUID());
            inboundItem.setBacthCode(data.getLot());
//                        inboundItem.setSrcInboundDate(data.getMvdate());
            inboundItem.setTraceCode(data.getLabGuid());
            inboundItem.setBarcode(data.getLabGuid());
            inboundItem.setGuid(data.getGuid());
//                        inboundItem.setRegistkey(data.get);
//                        inboundItem.setManufacturer(data.getManufacturer());
            inboundItem.setAllocation(data.getCubeName());
            inboundItem.setAllocationGuid(data.getCubeGuid());
            inboundItem.setIfInCabinet((byte) 1);
            inboundItem.setIfScand((byte) 1);
            inboundItem.setProductiondate(data.getProductiondate());
            inboundItem.setExpiredate(data.getExpireDate());
        }
        int i = 0;
        while (i < inboundItems.size()) {
            InboundItem item = inboundItems.get(i);
            boolean match = matchScanBarcode(item, scanData);
            System.out.println("==========================match:" + match);
            if (ifNeedMacthRfid) {
                if (!match) {
                    inboundItems.remove(i);
                    i--;
                }
            } else {
                if (match) {
                    inboundItems.remove(i);
                    i--;
                }

            }

            i++;
        }
        return inboundItems;
    }

    @Transactional
    public synchronized List<InboundItem> ActionqueryInboundItem(RfidDataModel rfidDataModel, String rfidData) {
        String goodsInfol = "";
        //设置操作人人是从柜子柜子对象中取得
        String cubeGuid = rfidDataModel.getCabinetCd();
        String operaterId = rfidDataModel.getUserCd();//这个UserCd获取的是哪个
        String operaterName = rfidDataModel.getUserName();//这个UserName是哪个username?是做入库的username还是开柜的username
       log.info("获取用户1"+operaterId+","+operaterName);
        if(operaterId == null || operaterId .isEmpty())
        {   operaterId =guiContext.getLoginUser().getGuid();
            operaterName=guiContext.getLoginUser().getUserName();
        }

        log.info("获取用户2"+operaterId+","+operaterName);
        String sql = " select * from mfcubestockdetail where billstate!='O' AND CubeGuid in (select guid from  cabinet where groupcode='" + fxConfigure.getCabinetGroupCode() + "') " +
                " AND depotGuid='" + guiContext.getLoginUserVali().getOrgGuid() + "' AND CubeName=" + "'" + cubeGuid + "'  AND  rfiduid  NOT IN (" + rfidData + ")";
        log.info("退货sql"+sql);
        List<Map> maps = commonMapper.queryList(sql);
        List<InboundItem> inboundItems = new ArrayList<>();

        for (Map map : maps) {
            InboundItem inboundItem = new InboundItem();
            Mfcubestockdetail data = JSON.parseObject(JSON.toJSONString(map), Mfcubestockdetail.class);
            inboundItem.setScanQty(Double.valueOf(data.getNumber()).intValue());
            inboundItem.setSrcOrderCode(data.getRkBillNo());
            inboundItem.setQty(Double.valueOf(data.getNumber()).intValue());
            inboundItem.setGoodsguid(data.getGoodsGUID());
            inboundItem.setGoodsname(data.getGoodsName());
            inboundItem.setUnit(data.getUnit());
            inboundItem.setIfScand((byte) 0);
            inboundItem.setSupplierName(data.getSupplierName());
            inboundItem.setSupplierGuid(data.getSupplierGUID());
            inboundItem.setBacthCode(data.getLot());
            inboundItem.setTraceCode(data.getLabGuid());
            inboundItem.setBarcode(data.getLabGuid());
            inboundItem.setGuid(data.getGuid());
            inboundItem.setAllocation(data.getCubeName());
            inboundItem.setAllocationGuid(data.getCubeGuid());
            inboundItem.setIfInCabinet((byte) 1);
            inboundItem.setIfScand((byte) 1);
            inboundItem.setProductiondate(data.getProductiondate());
            //inboundItem.setGoodsspec(map.get("").toString())
           // inboundItem.setProducer(map.get("").toString());
            inboundItem.setExpiredate(data.getExpireDate());
            inboundItem.setRfiduid(data.getRfidUid());
            inboundItem.setOperaterId(operaterId);
            inboundItem.setOperaterName(operaterName);
            inboundItems.add(inboundItem);
        }

        return inboundItems;
    }

    private boolean matchScanBarcode(InboundItem item, Map<Cabinet, List<String>> scanData) {
        Set<Cabinet> cabinets = scanData.keySet();
        for (Cabinet cabinet : cabinets) {
            List<String> results = scanData.get(cabinet);
            for (String barcode : results) {
                boolean match = barcode.equals(item.getBarcode());
                if (match) {
                    return true;
                }
            }

        }
        return false;
    }

    public void saveUseCabinetInfo(List<InboundItem> inboundItems) {
        if (inboundItems == null) {
            return;
        }
        for (InboundItem item : inboundItems) {
            if (item.getIfInCabinet() == null || item.getIfInCabinet() != 1) {
                continue;
            }

            Mfcubestockdetail cubedetail = mfcubestockdetailMapper.selectByPrimaryKey(item.getGuid());

            MfcubestockdetailOld mfcubestockdetailOld=new MfcubestockdetailOld();
            BeanUtils.copyProperties(cubedetail, mfcubestockdetailOld);



            mfcubestockdetailMapper.deleteByPrimaryKey(cubedetail);
            //删除库存表,然后将spd_wvdetail_scend表中的该数据isDelete为Y


            baseService.writeLog(item, cubedetail, "5");
            //处理信息
//            mfcubestockdetailMapper;

            //处理台账信息
//            mfcubestockstatementMapper;
            //处理汇总信息
//        mfcubestockMapper;
//        逻辑联合主键 是    科室id+耗材柜id+商品id+批号+有效期至
        //    baseService.sumNumber(item, cubedetail);
        }
    }
}