jiazx0107@163.com
2023-06-23 7b5c179e17c9a106dcce3b2b0574af833cf97132
igds-inout/src/main/java/com/ld/igds/inout/service/impl/InoutServiceImpl.java
@@ -1,7 +1,6 @@
package com.ld.igds.inout.service.impl;
import com.bstek.dorado.data.entity.EntityUtils;
import com.ld.igds.check.dto.CheckItemData;
import com.ld.igds.common.CoreCommonService;
import com.ld.igds.constant.BizType;
import com.ld.igds.constant.Constant;
@@ -9,19 +8,18 @@
import com.ld.igds.data.CommonData;
import com.ld.igds.data.Page;
import com.ld.igds.inout.InoutConstant;
import com.ld.igds.inout.dto.InoutData;
import com.ld.igds.inout.dto.InoutParam;
import com.ld.igds.inout.dto.InoutCheckData;
import com.ld.igds.inout.dto.InoutCheckParam;
import com.ld.igds.inout.dto.InoutData;
import com.ld.igds.inout.dto.InoutParam;
import com.ld.igds.inout.mapper.InoutCheckMapper;
import com.ld.igds.inout.mapper.InoutRecordMapper;
import com.ld.igds.inout.service.InoutService;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.models.Depot;
import com.ld.igds.models.InoutPrice;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.DateUtil;
import com.ld.igds.util.RedisUtil;
import com.ld.igds.websocket.WebSocketPacket;
import com.ld.igds.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@@ -53,11 +51,14 @@
        if (StringUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
        // 首先从缓存中获取,如果没有则从数据库获取
        InoutData result = this.getFromInoutCache(param);
        if (null != result) {
            return result;
        }
        List<InoutData> list = inoutMapper.inoutProgressQuery(param);
        if (null == list || list.size() == 0)
            return null;
@@ -97,10 +98,9 @@
            return "没有获取到被删除数据的ID,无法删除!";
        }
        // 缓存中删除
        this.delInoutFromCache(param.getDeptId(), param.getType(), param.getId(), false);
        inoutMapper.deleteData(param);
        delFromCache(param.getDeptId(), param.getType(), param.getId());
        return null;
    }
@@ -110,8 +110,9 @@
        if (StringUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
        // 缓存中删除
        this.delInoutFromCache(param.getDeptId(), param.getType(), param.getId(), false);
        delFromCache(param.getDeptId(), param.getType(), param.getId());
        param.setProgress(InoutConstant.PROGRESS_RECORD);
        param.setRecordStatus(InoutConstant.RECORD_STATUS_ERROR);
@@ -154,7 +155,7 @@
            inoutMapper.insertData(data);
            this.addInoutCache(data);
            updateInoutCache(data);
        } catch (Exception e) {
            log.error("------------出入库执行保存出错---{}", e);
@@ -167,6 +168,8 @@
    @Override
    public String updateData(InoutData data) throws Exception {
        data.setUpdateTime(new Date());
        if (StringUtils.isEmpty(data.getCompanyId())) {
            data.setCompanyId(ContextUtil.getCompanyId());
        }
@@ -180,17 +183,31 @@
        inoutMapper.updateData(newData);
        //如果是完成状态不在进行后期处理,单独走complete方法
        if (InoutConstant.PROGRESS_RECORD.equals(newData.getProgress())) {
            return null;
        //更新缓存
        updateInoutCache(newData);
        return null;
    }
    @Override
    public String updateDataByHandle(InoutData data) {
        data.setUpdateTime(new Date());
        if (StringUtils.isEmpty(data.getCompanyId())) {
            data.setCompanyId(ContextUtil.getCompanyId());
        }
        InoutData newData;
        if (EntityUtils.isEntity(data)) {
            newData = new InoutData();
            BeanUtils.copyProperties(data, newData, new String[]{"checkItems", "files"});
        } else {
            newData = data;
        }
        // 更新緩存
        if (InoutConstant.PROGRESS_RECORD.equals(newData.getProgress())) {
            this.delInoutFromCache(newData.getDeptId(), newData.getType(), newData.getId(), true);
        } else {
            this.updateInoutCache(newData);
        }
        inoutMapper.updateDataByHandle(newData);
        //更新缓存
        updateInoutCache(newData);
        return null;
    }
@@ -246,16 +263,6 @@
        return records;
    }
//    @Override
//    public InoutData getLastRecord(InoutParam param) {
//        return inoutMapper.getLastRecord(param);
//    }
//    @Override
//    public int updateCuStorage(InoutParam param) {
//        return inoutMapper.updateCuStorage(param);
//    }
    @Override
    public String validate(String intelCard, String plateNum) {
        InoutParam param = new InoutParam();
@@ -274,20 +281,18 @@
    @Override
    public String toComplete(InoutParam param) throws Exception {
        if (null == param.getCompanyId()) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
        param.setUpdateTime(new Date());
        if (null == param.getFoodType()) {
            // 根据仓库信息获取缓存
            Depot depot = commonService.getCacheDepot(param.getCompanyId(), param.getDepotId());
            param.setFoodType(depot.getFoodType());
        if (null == param.getCompanyId()) {
            param.setCompanyId(ContextUtil.getDefaultCompanyId());
        }
        // 清除缓存
        this.delInoutFromCache(param.getDeptId(), param.getType(), param.getId(), true);
        this.delFromCache(param.getDeptId(), param.getType(), param.getId());
        inoutMapper.toComplete(param);
        return null;
    }
@@ -341,43 +346,34 @@
            data.setFoodType(depot.getFoodType());
        }
        // 清除缓存
        this.delInoutFromCache(data.getDeptId(), data.getType(), data.getId(), false);
        inoutMapper.updateData(data);
        updateInoutCache(data);
        return null;
    }
    @Override
    public void addInoutCache(InoutData data) {
        data.setFiles(null);
        data.setCheckItems(null);
        this.setInoutCache(data.getDeptId(), data);
        // 推送到大屏
        List<InoutData> list = this.getListInoutCache(data.getDeptId());
        this.notifyToScreen(data.getDeptId(), list, null);
    }
    @Override
    public void updateInoutCache(InoutData data) {
        data.setFiles(null);
        data.setCheckItems(null);
        this.setInoutCache(data.getDeptId(), data);
        if (InoutConstant.PROGRESS_RECORD.equals(data.getProgress())) {
        // 推送到大屏
        List<InoutData> list = this.getListInoutCache(data.getDeptId());
        List<InoutData> completeList = this.getCompleteListInoutCache(data.getDeptId());
            delFromCache(data.getDeptId(), data.getType(), data.getId());
        this.notifyToScreen(data.getCompanyId(), list, completeList);
            setCompleteInoutCache(data.getDeptId(), data);
        } else {
            setInoutCache(data.getDeptId(), data);
        }
    }
    @Override
    public void delInoutFromCache(String deptId, String type, String id, boolean addToComplete) {
    public void delFromCache(String deptId, String type, String id) {
        if (StringUtils.isEmpty(deptId)) {
            return;
        }
@@ -387,44 +383,14 @@
        if (StringUtils.isEmpty(id)) {
            return;
        }
        //删除未完成流程的缓存
        //从未完成列表中删除
        String key = this.buildInoutKey(deptId, InoutConstant.KEY_INOUT_LIST, type, id);
        InoutData inoutData = (InoutData) redisUtil.get(key);
        redisUtil.del(key);
        List<InoutData> list = this.getListInoutCache(deptId);
        //删除已完成流程的缓存
        key = this.buildInoutKey(deptId, InoutConstant.KEY_INOUT_COMPLETE_LIST, type, id);
        redisUtil.del(key);
        List<InoutData> completeList = this.getCompleteListInoutCache(deptId);
        if (addToComplete) {
            addInoutCompleteCache(inoutData);
            completeList = this.getCompleteListInoutCache(deptId);
        }
        // 通知到大屏
        notifyToScreen(deptId, list, completeList);
    }
    /**
     * 添加到已经完成的队列中
     *
     * @param data
     */
    public void addInoutCompleteCache(InoutData data) {
        if (null == data.getCompleteTime()) {
            data.setCompleteTime(new Date());
        }
        if (!InoutConstant.PROGRESS_RECORD.equals(data.getProgress())) {
            data.setProgress(InoutConstant.PROGRESS_RECORD);
        }
        this.setCompleteInoutCache(data.getDeptId(), data);
    }
    @Override
    public List<InoutData> getListInoutCache(String deptId) {
        String pattern = RedisConst.buildKey(deptId, InoutConstant.KEY_INOUT_LIST);
        Set<String> keys = redisUtil.keys(pattern);
        if (null == keys || keys.isEmpty()) {
@@ -457,6 +423,7 @@
        if (StringUtils.isEmpty(param.getDeptId())) {
            param.setDeptId(ContextUtil.subDeptId(null));
        }
        List<InoutData> list = getListInoutCache(param.getDeptId());
        if (null == list || list.isEmpty())
            return null;
@@ -480,35 +447,6 @@
            }
        }
        return null;
    }
    @Override
    public void initInoutScreen(String deptId) {
        if (null == deptId) {
            deptId = ContextUtil.subDeptId(null);
        }
        List<InoutData> curList = this.getListInoutCache(deptId);
        List<InoutData> complateList = this.getCompleteListInoutCache(deptId);
        notifyToScreen(deptId, curList, complateList);
    }
    @Override
    public void notifyWeb(InoutData result) {
        // 如果当前为满车称重执行推送
        if (result.getType().equals(InoutConstant.TYPE_IN) && result.getProgress().equals(InoutConstant.PROGRESS_WEIGHT_FULL)) {
            WebSocketPacket packet = new WebSocketPacket();
            packet.setBizType(BizType.SCREEN_CHECK.getCode());
            packet.setCompanyId(result.getCompanyId());
            packet.setOrderResp(OrderRespEnum.MSG_SUCCESS.getCode());
            packet.setData(result);
            packet.setBizTag(InoutConstant.PROGRESS_WEIGHT_FULL);
            log.debug("----向化验大屏推送满车称重信息 ----{}", result.getPlateNum());
            WebSocketServer.sendByBizTag(packet);
        }
    }
    @Override
@@ -544,10 +482,7 @@
    }
    @Override
    public InoutData getLastRecord(String companyId, String depotId) {
        InoutParam param = new InoutParam();
        param.setCompanyId(companyId);
        param.setDepotId(depotId);
    public InoutData getLastRecord(InoutParam param) {
        return inoutMapper.getLastRecord(param);
    }
@@ -555,7 +490,7 @@
    public String addInoutDataByLossOver(InoutData data) {
        // 获取上一车的流水数据
        InoutData inoutData = this.getLastRecord(data.getCompanyId(), data.getDepotId());
        //    InoutData inoutData = this.getLastRecord(data.getCompanyId(), data.getDepotId());
//        // 设置流水的库存
//        if (InoutConstant.TYPE_IN.equals(inoutData.getType())) {
@@ -586,12 +521,6 @@
        redisUtil.set(cacheKey, data.getCheckItems(), 60 * 60 * 24 * 3);
    }
    @SuppressWarnings("unchecked")
    @Override
    public List<CheckItemData> getCheckCache(String companyId, String checkId) {
        String cacheKey = RedisConst.buildKey(companyId, checkId);
        return (List<CheckItemData>) redisUtil.get(cacheKey);
    }
    /**
     * 出入库流程ID创建 202001030001 202001030001
@@ -679,9 +608,7 @@
     * @param data
     */
    private void setInoutCache(String deptId, InoutData data) {
        String key = this.buildInoutKey(deptId,
                InoutConstant.KEY_INOUT_LIST, data.getType(), data.getId());
        String key = this.buildInoutKey(deptId, InoutConstant.KEY_INOUT_LIST, data.getType(), data.getId());
        redisUtil.set(key, data, InoutConstant.KEY_INOUT_LIST_TIME);
    }
@@ -693,7 +620,6 @@
     */
    private void setCompleteInoutCache(String deptId, InoutData data) {
        String key = this.buildInoutKey(deptId, InoutConstant.KEY_INOUT_COMPLETE_LIST, data.getType(), data.getId());
        //设置缓存到第二天凌晨(计算当前时间到第二天凌晨的时间差秒数)
        redisUtil.set(key, data, DateUtil.getNowToNextDaySeconds());
    }
@@ -701,12 +627,17 @@
    /**
     * 推送到大屏,判断只有当大屏在线时候才处理当前逻辑,避免页面卡顿,调用子任务完成推送
     *
     * @param deptId
     * @param curList      未完成流程个数
     * @param completeList 已经完成流程个数
     * @param companyId 组织编号
     * @param deptId    分库编号
     * @param progress  流转节点
     */
    private void notifyToScreen(String deptId, List<InoutData> curList,
                                List<InoutData> completeList) {
    public void notifyToScreen(String companyId, String deptId, String progress) {
        // 推送到大屏
        List<InoutData> curList = this.getListInoutCache(deptId);
        List<InoutData> completeList = this.getCompleteListInoutCache(deptId);
        if (null == WebSocketServer.contextOnLineMap.get(BizType.SCREEN
                .getCode())) {
@@ -730,13 +661,10 @@
                || WebSocketServer.contextOnLineMap.get(BizType.SCREEN_CHECK
                .getCode())) {
            if (null == completeList) {
                completeList = this.getCompleteListInoutCache(deptId);
            }
            // 创建一个子任务进行推送信息
            FutureTask<String> futureTask = new FutureTask<>(
                    new NotifyScreenTask(curList, completeList, deptId));
            FutureTask<String> futureTask = new FutureTask<>(new NotifyScreenTask(curList, completeList, deptId));
            ExecutorService executorService = Executors.newCachedThreadPool();
            executorService.submit(futureTask);
            executorService.shutdown();
@@ -785,7 +713,7 @@
    }
    @Override
    public String updateSampleData(InoutData data) {
    public String updateSampleData(InoutCheckData data) {
        if (StringUtils.isEmpty(data.getCompanyId())) {
            data.setCompanyId(ContextUtil.getCompanyId());
        }
@@ -794,7 +722,54 @@
        }
        int num = inoutCheckMapper.updateSampleData(data);
        return null;
    }
    @Override
    public Page<InoutCheckData> pageCheckData(InoutCheckParam param) {
        //设置起始和截止时间
        if (null != param.getStart()) {
            param.setStart(DateUtil.getCurZero(param.getStart()));
        }
        if (null != param.getEnd()) {
            param.setEnd(DateUtil.getNextZero(param.getEnd()));
        }
        // 设置检验单据的模糊查询
        if (StringUtils.isNotEmpty(param.getCheckId())) {
            param.setCheckId("%" + param.getCheckId() + "%");
        }
        Page<InoutCheckData> page = new Page<>(param.getPage(), param.getLimit());
        page.setSearchCount(true);
        List<InoutCheckData> records = inoutCheckMapper.pageCheckData(page, param);
        page.setRecords(records);
        return page;
    }
    @Override
    public String updateCheckData(InoutData data) {
        data.setUpdateTime(new Date());
        int num = inoutCheckMapper.updateCheckData(data);
        //更新缓存
        updateInoutCache(data);
        return null;
    }
    @Override
    public List<InoutPrice> getPrice(InoutCheckParam param) {
        if (null == param.getStart()) {
            param.setStart(new Date());
        }
        if (null == param.getEnd()) {
            param.setEnd(new Date());
        }
        return inoutCheckMapper.getPrice(param);
    }
}