StockCountService.java 19 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
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.springboot.dao.mapper.*;
import cn.csbr.springboot.dao.model.Cabinet;
import cn.csbr.springboot.dao.model.Mfcubestockcout;
import cn.csbr.springboot.dao.model.Mfcubestockdetail;
import cn.csbr.springboot.dao.model.SpdWvdetail;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.csbr.mybatis.CommonMapper;
import com.csbr.util.ArrayUtil;
import com.csbr.util.IdUtils;

/**
 * 盘点业务类
 */
@Service
public class StockCountService {
    private static Logger logger = LoggerFactory.getLogger(StockCountService.class);
    @Autowired
    private CommonMapper commonMapper;
    @Autowired
    private CabinetService cabinetService;
    @Autowired
    private GUIContext guiContext;
    @Autowired
    private SpdWvdetailMapper spdWvdetailMapper;
    @Autowired
    private FxConfigure fxConfigure;
    @Autowired
    private MfcubestockdetailMapper mfcubestockdetailMapper;
    @Autowired
    private MfcubestockstatementMapper mfcubestockstatementMapper;
    @Autowired
    private MfcubestockMapper mfcubestockMapper;
    @Autowired
    private MfcubestockcoutMapper mfcubestockcoutMapper;

    @Autowired
    private BaseService baseService;

    public synchronized List<InboundItem> queryInboundItem(String inboundNo, Map<Cabinet, List<String>> scanData, String goodsInfo, boolean ifNeedMacthRfid) {
        System.out.println("============================ scanData: " + scanData);
        String goodsInfol = null;
        Example condition = new Example(Mfcubestockdetail.class);
        Example.Criteria cr = condition.createCriteria().andEqualTo("rkbillno", inboundNo);
        cr.andCondition(" LabGuid not in (select LabGuid from  mfcubestockcout where processflag is null) and billstate='Y' ");
        if (!StringUtils.isEmpty(goodsInfo)) {
            goodsInfol = "%" + goodsInfo + "%";
            String sql = "  GoodsName like '" + goodsInfol + "' or LabGuid like'" + goodsInfol + "'";
            cr.andCondition("(" + sql + ")");
        }
        cr.andCondition(" CubeGuid in (select guid from  cabinet where groupcode='" + fxConfigure.getCabinetGroupCode() + "')");
        List<Mfcubestockdetail> mfcubestockdetails = mfcubestockdetailMapper.selectByExample(condition);
        List<InboundItem> inboundItems = new ArrayList<>();
        System.out.println("=================================== itbounditems:" + mfcubestockdetails.size());
        Set<Cabinet> cabinets = scanData.keySet();

        int rowIndex = 0;

        //转换库存对象到vo
        for (Mfcubestockdetail data : mfcubestockdetails) {
            InboundItem inboundItem = new InboundItem();
            inboundItems.add(inboundItem);
            inboundItem.setRowNo(++rowIndex);

            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.setRfiduid(data.getRfidUid());
            inboundItem.setGuid(data.getGuid());
            inboundItem.setAllocation(data.getCubeName());
            inboundItem.setAllocationGuid(data.getCubeGuid());
            inboundItem.setIfInCabinet((byte) 1);
            inboundItem.setGuid(data.getGuid());
            inboundItem.setIfScand((byte) 1);
            inboundItem.setProductiondate(data.getProductiondate());
            inboundItem.setExpiredate(data.getExpireDate());
        }

        //处理缺损情况
        List<String> scanRfids = scanData.entrySet().stream().flatMap(entry -> entry.getValue().stream()).collect(Collectors.toList());
        int i = 0;
        while (i < inboundItems.size()) {
            InboundItem item = inboundItems.get(i);
            boolean match = scanRfids.contains(item.getRfiduid());
            System.out.println("=====" + match);
            if (!match) {
                item.setCheckType("2");
                item.setConfirmFlag("0");
            }
            i++;
        }

        //处理报溢情况
        List<InboundItem> ofdatas = processOverFlow(inboundItems, scanRfids);
        inboundItems.addAll(ofdatas);

        return inboundItems;
    }

    public synchronized List<InboundItem> ActionqueryInboundItem(String cubeName, String rfiddata) {
        List<InboundItem> inboundItems = new ArrayList<>();

      //  String sql = "select * from mfcubestockdetail  where   CubeGuid in (select guid from  cabinet where groupcode='" + fxConfigure.getCabinetGroupCode() + "') " +
       //         " AND DepotGuid='" + guiContext.getLoginUser().getOrgGuid() + "'AND CubeName=" + "'" + cubeName + "'";
        String sql = "select dt.*,Model,GoodsSpec,Producer from mfcubestockdetail dt,his_goods hg  where   CubeGuid in (select guid from  cabinet where groupcode='" + fxConfigure.getCabinetGroupCode() + "') " +
                " and  hg.GUID= dt.GoodsGUID AND depotGuid='" + guiContext.getLoginUserVali().getOrgGuid() + "' AND CubeName=" + "'" + cubeName + "'";
    //    String sql = "select dt.*,Model,GoodsSpec,Producer from mfcubestockdetail dt,his_goods hg  where   CubeGuid in (select guid from  cabinet where groupcode='" + fxConfigure.getCabinetGroupCode() + "') " +
       //         " and  hg.GUID= dt.GoodsGUID ";
        System.out.println("盘点SQL:"+sql);
        List<Map> maps = commonMapper.queryList(sql);
        int rowIndex = 0;
        String tmprfid = "";
        for (Map map : maps) {

            Mfcubestockdetail data = JSON.parseObject(JSON.toJSONString(map), Mfcubestockdetail.class);
            InboundItem inboundItem = new 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.getgo);
            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.setRfiduid(data.getRfidUid());
            inboundItem.setGuid(data.getGuid());
            inboundItem.setAllocation(data.getCubeName());
            inboundItem.setAllocationGuid(data.getCubeGuid());
            inboundItem.setIfInCabinet((byte) 1);
            inboundItem.setGuid(data.getGuid());
            inboundItem.setIfScand((byte) 1);
            inboundItem.setProductiondate(data.getProductiondate());
            inboundItem.setExpiredate(data.getExpireDate());
            if(map.get("Producer")==null)
            {
                inboundItem.setProducer("");
            }
            else {
                inboundItem.setProducer(map.get("Producer").toString());
            }
            String gs=map.get("GoodsSpec")==null?"":map.get("GoodsSpec").toString();
            String model=map.get("Model")==null?"":map.get("Model").toString();
            inboundItem.setModel(gs+"("+model+")");
            if (!rfiddata.contains(data.getRfidUid())) {
                inboundItem.setCheckType("2");
                inboundItem.setConfirmFlag("0");

            }

            if (!inboundItem.getAllocation().equals(cubeName)) {
                for (Cabinet cabinet : guiContext.getCabinets()) {
                    if (cabinet.getCabinetcode().equals(cubeName)) {
                        data.setLocGuid(cabinet.getGuid());
                        data.setLocName(cabinet.getCabinetcode());
                        data.setCubeName(cabinet.getCabinetcode());
                        data.setCubeGuid(cabinet.getGuid());
                        inboundItem.setAllocation(cabinet.getCabinetcode());
                        inboundItem.setAllocationGuid(cabinet.getGuid());
                        mfcubestockdetailMapper.updateByPrimaryKeySelective(data);
                        break;

                    }

                }
            }

            if(!data.getBillState().contains("O"))
            {   inboundItems.add(inboundItem);
                inboundItem.setRowNo(++rowIndex);
            }
        }


        return inboundItems;
    }



    public synchronized List<InboundItem> processOverFlow(List<InboundItem> items, List<String> scanRfids) {
        List<InboundItem> oFDatas = new ArrayList<>();
//        Map<String, List<InboundItem>> itemMap = items.stream().collect(Collectors.groupingBy(InboundItem::getRfiduid));
        List<String> itemRfids = items.stream().filter(it -> !"2".equals(it.getCheckType())).map(it -> it.getRfiduid()).collect(Collectors.toList());
//        List<String> scanRfids = scanData.entrySet().stream().flatMap(entry -> entry.getValue().stream()).collect(Collectors.toList());
        scanRfids.removeAll(itemRfids);//排除所有扫描到到,剩下到就是数据库里有但柜子没有的
        if (ArrayUtil.isNotEmpty(scanRfids)) {
            for (String itemRfid : scanRfids) {
                InboundItem litem = new InboundItem();
                litem.setCheckType("3");
                litem.setBarcode(itemRfid);
                litem.setTraceCode(itemRfid);
                oFDatas.add(litem);

            }
        }

        return oFDatas;
    }

    private void createOverFlowItem(List<InboundItem> items, List<InboundItem> oFDatas, Cabinet cabinet, String barcode) {
        boolean match = false;
        for (InboundItem item : items) {
            String ibarcode = item.getBarcode();
            if (barcode.equals(ibarcode)) {
                match = true;
                continue;
            }
        }
        //不匹配的的就是overflow的了
        if (!match) {
            //查入库单明细表 入库单没有的情况暂时去死
            Example condition = new Example(SpdWvdetail.class);
            condition.createCriteria().andEqualTo("barcode", barcode);
            List<SpdWvdetail> wvdetails = spdWvdetailMapper.selectByExample(condition);
            if (wvdetails.size() > 0) {
                SpdWvdetail data = wvdetails.get(0);
                InboundItem litem = new InboundItem();
                litem.setCheckType("3");
                litem.setRowNo(data.getRowno());
                litem.setScanQty(data.getAmount().intValue());
                litem.setSrcOrderCode(data.getBillno());
                litem.setQty(data.getAmount().intValue());
                litem.setGoodsguid(data.getGoodsguid());
                litem.setGoodscode(data.getGoodscode());
                litem.setGoodsname(data.getGoodsname());
                litem.setGoodsspec(data.getGoodsspec());
                litem.setUnit(data.getUnit());
                litem.setIfScand((byte) 0);
                litem.setSupplierName(data.getSuppliername());
                litem.setSupplierGuid(data.getSupplierguid());
                litem.setBacthCode(data.getLot());
                litem.setSrcInboundDate(data.getMvdate());
                litem.setTraceCode(data.getBarcode());
                litem.setBarcode(data.getBarcode());
                litem.setGuid(data.getGuid());
                litem.setRegistkey(data.getRegistkey());
                litem.setManufacturer(data.getManufacturer());
                litem.setAllocation(cabinet.getCabinetcode());
                litem.setAllocationGuid(cabinet.getGuid());
                litem.setIfInCabinet((byte) 1);
                litem.setIfScand((byte) 1);
                litem.setConfirmFlag("0");
                litem.setGuid(data.getGuid());
                oFDatas.add(litem);
                //生成inbounditem记录
            }
        }
    }

    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;
//                }
//            }
            if (results.contains(item.getRfiduid())) {
                return true;
            }

        }
        return false;
    }

    public void saveStockCountCabinetInfo(List<InboundItem> inboundItems) {
        if (inboundItems == null) {
            return;
        }
        for (InboundItem item : inboundItems) {
            System.out.println("********************************--------------------item:" + item);
            if (!"1".equals(item.getConfirmFlag())) {//没确认的就不保存
                continue;
            }
            Mfcubestockcout mfcubestockcout = new Mfcubestockcout();
            mfcubestockcout.setGuid(IdUtils.getUUID());
            switch (item.getCheckType()) {
                case "1":
                    putValueFromCubeDetail(mfcubestockcout, item);
                    mfcubestockcout.setDatatype("1");
                    break;
                case "2":
                    putValueFromCubeDetail(mfcubestockcout, item);
                    mfcubestockcout.setDatatype("2");
                    break;
                case "3":
                    putValueFromSpd(mfcubestockcout, item);
                    mfcubestockcout.setDatatype("3");
                    break;
                case "4":
                    putValueFromCubeDetail(mfcubestockcout, item);
                    mfcubestockcout.setDatatype("4");
                    break;
                case "5":
                    putValueFromSpd(mfcubestockcout, item);
                    mfcubestockcout.setDatatype("5");
                    break;
                default:
                    break;
            }
            mfcubestockcout.setProcessflag("0");
            int i = mfcubestockcoutMapper.insert(mfcubestockcout);
            System.out.println("插入的数据数量为" + i);
            System.out.println("数据状态为" + mfcubestockcout.getDatatype());
        }
    }

    private void putValueFromCubeDetail(Mfcubestockcout cubedetailout, InboundItem item) {
        Mfcubestockdetail cubedetail = mfcubestockdetailMapper.selectByPrimaryKey(item.getGuid());
        cubedetailout.setDepotguid(cubedetail.getDepotGuid());
        cubedetailout.setDepotname(cubedetail.getDepotName());
        cubedetailout.setCubeguid(cubedetail.getCubeGuid());
        cubedetailout.setCubename(cubedetail.getCubeName());
        cubedetailout.setLocguid(cubedetail.getLocGuid());
        cubedetailout.setLocname(cubedetail.getLocName());
        cubedetailout.setLabguid(cubedetail.getLabGuid());
        cubedetailout.setGoodsguid(cubedetail.getGoodsGUID());
        cubedetailout.setGoodsname(cubedetail.getGoodsName());
        cubedetailout.setUnit(cubedetail.getUnit());
        cubedetailout.setLot(cubedetail.getLot());
        cubedetailout.setProductiondate(cubedetail.getProductiondate());
        cubedetailout.setExpiredate(cubedetail.getExpireDate());
        cubedetailout.setSterilantlot(cubedetail.getSterilantLot());
        cubedetailout.setSterilantdate(cubedetail.getSterilantDate());
        cubedetailout.setSterilantexpdate(cubedetail.getSterilantEXPDate());
        cubedetailout.setNumber(new BigDecimal(cubedetail.getNumber()));
        cubedetailout.setSupplierguid(cubedetail.getSupplierGUID());
        cubedetailout.setSuppliername(cubedetail.getSupplierName());
        cubedetailout.setRkbillno(cubedetail.getRkBillNo());
        cubedetailout.setPricebuy(new BigDecimal(cubedetail.getPriceBuy()));
        cubedetailout.setAmountbuy(new BigDecimal(cubedetail.getAmountBuy()));
        cubedetailout.setCreateTime(new Date());
        cubedetailout.setUpdateTime(new Date());
        cubedetailout.setConfirmoperaterguid(guiContext.getLoginUser().getGuid());
        cubedetailout.setConfirmoperatername(guiContext.getLoginUser().getRealName());
        cubedetailout.setConfirmTime(new Date());
        cubedetailout.setCofirmflag("1");

    }

    private void putValueFromSpd(Mfcubestockcout cubedetailout, InboundItem item) {
        Example condition = new Example(SpdWvdetail.class);
        condition.createCriteria().andEqualTo("barcode", item.getBarcode());
        List<SpdWvdetail> wvdetails = spdWvdetailMapper.selectByExample(condition);
        if (wvdetails.size() > 0) {
            SpdWvdetail data = wvdetails.get(0);
            cubedetailout.setDepotguid(guiContext.getLoginUser().getOrgGuid());
            cubedetailout.setDepotname(guiContext.getLoginUser().getOrgName());
            cubedetailout.setCubeguid(item.getAllocationGuid());
            cubedetailout.setCubename(item.getAllocation());
            cubedetailout.setLocguid(item.getAllocationGuid());
            cubedetailout.setLocname(item.getAllocation());
            cubedetailout.setLabguid(data.getBarcode());
            cubedetailout.setGoodsguid(data.getGoodsguid());
            cubedetailout.setGoodsname(data.getGoodsname());
            cubedetailout.setUnit(data.getUnit());
            cubedetailout.setLot(data.getLot());
            cubedetailout.setProductiondate(data.getProductiondate());
            cubedetailout.setExpiredate(data.getExpiredate());
            cubedetailout.setSterilantlot(data.getSterilantlot());
            cubedetailout.setSterilantdate(data.getSterilantdate());
            cubedetailout.setSterilantexpdate(data.getSterilantexpdate());
            cubedetailout.setNumber(data.getMvqty());
            cubedetailout.setSupplierguid(data.getSupplierguid());
            cubedetailout.setSuppliername(data.getSuppliername());
            cubedetailout.setRkbillno(data.getBillno());
            cubedetailout.setPricebuy(data.getPrice());
            cubedetailout.setAmountbuy(data.getPrice());
            cubedetailout.setCreateTime(new Date());
            cubedetailout.setUpdateTime(new Date());
            cubedetailout.setConfirmoperaterguid(guiContext.getLoginUser().getGuid());
            cubedetailout.setConfirmoperatername(guiContext.getLoginUser().getRealName());
            cubedetailout.setConfirmTime(new Date());
            cubedetailout.setCofirmflag("1");

        }

    }
}