“djh”
2 days ago aad364bb323a1eaa0389ee5c6389bdc0ea7ed526
hazmat-system/src/main/java/com/gkhy/hazmat/system/service/impl/HzEntryRecordServiceImpl.java
@@ -23,6 +23,7 @@
import com.gkhy.hazmat.system.mapper.*;
import com.gkhy.hazmat.system.service.HzEntryRecordService;
import com.gkhy.hazmat.system.service.HzHazmatService;
import com.gkhy.hazmat.system.service.SubscribeService;
import com.gkhy.hazmat.system.service.TabooWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -63,12 +64,19 @@
    @Autowired
    private HzTabooWarningMapper tabooWarningMapper;
    @Autowired
    private SubscribeMapper subscribeMapper;
    @Autowired
    private SubscribeHazmatMapper subscribeHazmatMapper;
    @Override
    public CommonPage selectEntryRecordList(HzEntryRecord entryRecord) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        if (!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode()) && !currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())) {
            entryRecord.setCompanyId(currentUser.getCompanyId());
        }
        entryRecord.setType(0);
        PageUtils.startPage();
        List<HzEntryRecord> entryRecordList = baseMapper.selectEntryRecordList(entryRecord);
        return CommonPage.restPage(entryRecordList);
@@ -98,6 +106,7 @@
        }
        entryRecord.setCompanyId(currentUser.getCompanyId());
        entryRecord.setCreateBy(currentUser.getUsername());
        entryRecord.setType(0);
        checkUserAllowed(null,currentUser);
        int row=0;
        synchronized (this) {
@@ -350,6 +359,248 @@
        return CommonPage.restPage(hazmatList);
    }
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void orderEntry(Long entryRecordId) {
        HzEntryRecord entryRecord=getById(entryRecordId);
        if(entryRecord.getState().equals(EntryStateEnum.ENTER.getCode())){
            throw new ApiException("已完成入库,不能再操作");
        }
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(entryRecord,currentUser);
        Subscribe subscribe = subscribeMapper.selectbyNum(entryRecord.getSubscribeNum());
        if(subscribe==null){
            throw new ApiException("申购单不存在");
        }
        int currentStartCode = entryRecord.getStartCode();
        List<SubscribeHazmat> subscribeHazmats = subscribeHazmatMapper.selectbySubscribeId(subscribe.getId());
        for (SubscribeHazmat subscribeHazmat : subscribeHazmats) {
            HzHazmatBasic hazmatBasic=hazmatBasicMapper.selectById(subscribeHazmat.getBasicId());
            if(hazmatBasic==null){
                throw new ApiException("危化品基础数据不存在");
            }
            Integer hazmatCount = subscribeHazmat.getHazmatCount() != null ? subscribeHazmat.getHazmatCount() : 0;
            if (hazmatCount <= 0) {
                continue;
            }
            // 计算该危化品的条码范围
            int hazmatStartCode = currentStartCode;
            int hazmatEndCode = currentStartCode + hazmatCount - 1;
            // 更新下一个危化品的起始码
            currentStartCode = hazmatEndCode + 1;
            int count = hazmatMapper.selectHazmatCountOfWarehouse(entryRecord.getWarehouseId(), hazmatBasic.getId(), currentUser.getCompanyId(), entryRecord.getCupboardId());
            //新增危化品变动记录
            HzWarehouseRecord warehouseRecord = new HzWarehouseRecord()
                    .setWarehouseId(entryRecord.getWarehouseId())
                    .setCupboardId(entryRecord.getCupboardId())
                    .setBasicId(hazmatBasic.getId())
                    .setNum(entryRecord.getNum())
                    .setCompanyId(currentUser.getCompanyId())
                    .setCreateId(currentUser.getId())
                    .setRemaining(entryRecord.getNum() + count);
            warehouseRecordMapper.insert(warehouseRecord);
            // 校验生成相忌数据
            Map<String, Long>  redata = (Map<String, Long>)redisService.get(Constant.TABOO_UNIQUE_KEY);
            if (!redata.isEmpty()){
                // 查询对应仓库和柜子的物品信息获取品类 获取相冲相弱相吸相异
                List<TabooDisVo> collectData =
                        hazmatMapper.selectHazmatWarehouseCheck(entryRecord.getWarehouseId(), currentUser.getCompanyId(), entryRecord.getCupboardId());
                if (!collectData.isEmpty() && collectData.size() > 0) {
                    for (TabooDisVo collectDatum : collectData) {
                        String key1 = collectDatum.getSpNum() + "_" + hazmatBasic.getPeculiarityNumber();
                        String key2 = hazmatBasic.getPeculiarityNumber() + "_" + collectDatum.getSpNum();
                        if (redata.containsKey(key1) || redata.containsKey(key2)) {
                            // 记录数据
                            Long l = redata.get(key1);
                            Long l1 = l == null ? redata.get(key2) : l;
                            HzTabooWarning tabooWarning = new HzTabooWarning()
                                    .setWarningType(l1)
                                    .setWarehouseId(entryRecord.getWarehouseId())
                                    .setCupboardId(entryRecord.getCupboardId())
                                    .setEntryId(entryRecord.getId())
                                    .setCompanyId(currentUser.getCompanyId())
                                    .setTabooBasicId(collectDatum.getId()).setCreateBy(currentUser.getUsername());
                            tabooWarningMapper.insert(tabooWarning);
                            break;
                        }
                    }
                }
            }else {
                throw new ApiException("危化品相忌信息不存在");
            }
            List<HzHazmat> hazmatList = new ArrayList<>();
            for (int i = hazmatStartCode; i <=hazmatEndCode; i++) {
                String lastCode= StringUtils.addZeroForNum(String.valueOf(i),4);
                String code=String.format("%s%s",entryRecord.getCodePrex(),lastCode);
                hazmatList.add(new HzHazmat().setWarehouseId(entryRecord.getWarehouseId())
                        .setCupboardId(entryRecord.getCupboardId())
                        .setBasicId(subscribeHazmat.getBasicId())
                        .setCupboardId(entryRecord.getCupboardId())
                        .setEntryId(entryRecord.getId())
                        .setRemaining(hazmatBasic.getMetering())
                        .setCompanyId(currentUser.getCompanyId())
                        .setState(HazmatStatusEnum.WAREHOUSEIN.getCode())
                        .setCode(code));
            }
            //批量创建危化品
            if (hazmatList.size() > 100) {
                while (!hazmatList.isEmpty()) {
                    int endIndex = Math.min(hazmatList.size(), 100);
                    List<HzHazmat> subList = hazmatList.subList(0, endIndex);
                    hazmatService.saveBatch(subList);
                    hazmatList = hazmatList.subList(endIndex, hazmatList.size());
                }
            } else {
                hazmatService.saveBatch(hazmatList);
            }
            // }
            //更新入库记录状态
            entryRecord.setState(EntryStateEnum.ENTER.getCode());
            updateById(entryRecord);
//        IdTableNameHandler.removeCurrentId();
        }
        //异步执行
        // 注册一个事务完成后执行的回调
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                customEventPublisher.publishEntry(entryRecord.getId(), CodePrexEnum.MATERIAL.getCode(), currentUser.getCompanyId(), currentUser.getId());
            }
        });
    }
    @Override
    public int insertOrderEntryRecord(HzEntryRecord entryRecord) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        Map<String, Object> parms = new HashMap<>();
        parms.put("subscribe_num",entryRecord.getSubscribeNum());
        List<HzEntryRecord> hzEntryRecords = baseMapper.selectByMap(parms);
        if (hzEntryRecords.size()>0){
            throw new ApiException("申购单已入库,不能重复添加");
        }
        Subscribe subscribe = subscribeMapper.selectbyNum(entryRecord.getSubscribeNum());
        if(subscribe==null){
            throw new ApiException("申购单不存在");
        }
        List<SubscribeHazmat> subscribeHazmats = subscribeHazmatMapper.selectbySubscribeId(subscribe.getId());
        for (SubscribeHazmat subscribeHazmat : subscribeHazmats) {
            HzHazmatBasic hazmatBasic=hazmatBasicMapper.selectById(subscribeHazmat.getBasicId());
            if(hazmatBasic==null){
                throw new ApiException("危化品基础数据不存在"+hazmatBasic.getName());
            }
            if(subscribeHazmat.getHazmatCount()>hazmatBasic.getMaxEntry()){
                throw new ApiException(hazmatBasic.getName()+"数量超过单次入库最大数量<"+hazmatBasic.getMaxEntry()+">");
            }
        }
        entryRecord.setCompanyId(currentUser.getCompanyId());
        entryRecord.setCreateBy(currentUser.getUsername());
        entryRecord.setType(1);
        checkUserAllowed(null,currentUser);
        int row=0;
        synchronized (this) {
            //生成条码范围
            generateCode(entryRecord);
            row = baseMapper.insert(entryRecord);
            if (row < 1) {
                throw new ApiException("新增入库记录失败");
            }
        }
        return row;
    }
    @Override
    public int updateOrderEntryRecord(HzEntryRecord entryRecord) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(entryRecord,currentUser);
        Subscribe subscribe = subscribeMapper.selectbyNum(entryRecord.getSubscribeNum());
        if(subscribe==null){
            throw new ApiException("申购单不存在");
        }
        List<SubscribeHazmat> subscribeHazmats = subscribeHazmatMapper.selectbySubscribeId(subscribe.getId());
        for (SubscribeHazmat subscribeHazmat : subscribeHazmats) {
            HzHazmatBasic hazmatBasic=hazmatBasicMapper.selectById(subscribeHazmat.getBasicId());
            if(hazmatBasic==null){
                throw new ApiException("危化品基础数据不存在"+hazmatBasic.getName());
            }
            if(entryRecord.getNum()>hazmatBasic.getMaxEntry()){
                throw new ApiException(hazmatBasic.getName()+"数量超过单次入库最大数量<"+hazmatBasic.getMaxEntry()+">");
            }
        }
        HzEntryRecord existEr=baseMapper.selectById(entryRecord.getId());
        if(existEr.getState().equals(EntryStateEnum.ENTER.getCode())){
            throw new ApiException("已经入库,不能再修改");
        }
        if (!Objects.equals(existEr.getNum(),entryRecord.getNum())) {
            generateCode2(entryRecord);
        }
        entryRecord.setUpdateBy(currentUser.getUsername());
        int row=baseMapper.updateById(entryRecord);
        if(row<1){
            throw new ApiException("更新入库信息失败");
        }
        return row;
    }
    @Override
    public int deleteOrderEntryRecordById(Long entryRecordId) {
        HzEntryRecord entryRecord=baseMapper.selectById(entryRecordId);
        if(entryRecord==null){
            throw new ApiException("入库记录不存在");
        }
        if(entryRecord.getState().equals(EntryStateEnum.ENTER.getCode())){
            throw new ApiException("已入库状态的记录不能删除");
        }
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(entryRecord,currentUser);
        int update = baseMapper.deleteById(entryRecordId);
        return update;
    }
    @Override
    public CommonPage selectOrderEntryRecordList(HzEntryRecord entryRecord) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        if (!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode()) && !currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())) {
            entryRecord.setCompanyId(currentUser.getCompanyId());
        }
        entryRecord.setType(1);
        PageUtils.startPage();
        List<HzEntryRecord> entryRecordList = baseMapper.selectEntryRecordList(entryRecord);
        for (HzEntryRecord hzEntryRecord : entryRecordList) {
            if (hzEntryRecord.getSubscribeNum() != null) {
                Subscribe subscribe = subscribeMapper.selectbyNum(hzEntryRecord.getSubscribeNum());
                subscribe.setSubscribeHazmats(subscribeHazmatMapper.selectbySubscribeId(subscribe.getId()));
                hzEntryRecord.setSubscribe(subscribe);
            }
        }
        return CommonPage.restPage(entryRecordList);
    }
    @Override
    public CommonPage selectOrderHazmatListByEntryId(HzHazmat hzHazmat) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        HzEntryRecord entryRecord=getById(hzHazmat.getEntryId());
        if(entryRecord==null){
            throw new ApiException("入库信息不存在");
        }
        checkUserAllowed(entryRecord,currentUser);
//        if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
//            IdTableNameHandler.setCurrentId(entryRecord.getCompanyId());
//        }else {
//            //设置分表id
//            IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
//        }
        PageUtils.startPage();
        List<HzHazmat> hazmatList = hazmatMapper.selectHazmatList(hzHazmat);
//        IdTableNameHandler.removeCurrentId();
        return CommonPage.restPage(hazmatList);
    }
    public void checkUserAllowed(HzEntryRecord entryRecord,SysUser user) {
        if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            throw new ApiException("管理员不能操作");