YYC
2023-07-13 43a14ad6fbb68a9c0ce787acce28ce446aac13b0
igds-inout/src/main/java/com/ld/igds/m/service/HLossOverService.java
@@ -2,15 +2,21 @@
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import com.ld.igds.constant.RedisConst;
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.service.InoutService;
import com.ld.igds.m.InoutManageUtil;
import com.ld.igds.models.InoutLossOver;
import com.ld.igds.models.InoutVarietyChange;
import com.ld.igds.util.DateUtil;
import com.ld.igds.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
@@ -25,6 +31,8 @@
@Component
public class HLossOverService extends HibernateDao {
    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private InoutService inoutService;
@@ -78,18 +86,6 @@
        data.setUpdateUser(ContextUtil.getLoginUserCName());
        InoutParam param = new InoutParam();
        param.setCompanyId(ContextUtil.getCompanyId());
        param.setDepotId(data.getDepotId());
        param.setProgress(InoutConstant.PROGRESS_RECORD);
        InoutData inoutData = inoutService.getLastRecord(param);
        if (null != inoutData && null != inoutData.getCompleteTime() &&
                data.getSubmitTime().before(inoutData.getCompleteTime())) {
            throw new Exception("登记时间不能早于最后一辆车完成时间:" + DateFormatUtils.format(inoutData.getCompleteTime(), "yyyy-MM-dd HH:mm"));
        }
        try {
            if (null == data.getId()) {
                IUser user = ContextUtil.getLoginUser();
@@ -106,8 +102,8 @@
                if (data.getSubmitTime().before(curDate)) {
                    data.setSubmitTime(curDate);
                }
                String id = addInoutDataByLossOver(data);
                String id = createId(data.getSubmitTime(), data.getCompanyId(), data.getHwdm());
//                String id = addInoutDataByLossOver(data);
                data.setId(id);
@@ -188,4 +184,56 @@
        inoutService.delInoutDataByLossOver(param);
    }
    public List<InoutLossOver> getMaxId(Map<String, Object> param) {
        String hql = "from " + InoutLossOver.class.getName() + " where 1 = 1 ";
        Map<String, Object> args = new HashMap<String, Object>();
        if (null != param) {
            String str = (String) param.get("timeKey");
            if (StringUtils.isNotEmpty(str)) {
                hql += " and id like :timeKey";
                args.put("timeKey", str);
            }
        }
        hql += " order by  id desc ";
        return this.query(hql, args);
    }
    public String createId(Date date, String companyId, String freightId) {
        // 时间戳标签
        String timeKey = DateFormatUtils.format(date, "yyyyMMdd");
        // 从缓存中获取已有的组织编码
        String cacheKey = RedisConst.buildKey(companyId,
                InoutManageUtil.CACHE_LOSS_OVER_ID);
        String cacheId = (String) redisUtil.get(cacheKey);
        if (null != cacheId && cacheId.indexOf(freightId + timeKey) >= 0) {
            String temp = cacheId.substring(cacheId.length() - 3);
            Integer i = Integer.valueOf(temp);
            cacheId = freightId + timeKey + String.format("%03d", ++i);
        } else {
            Map<String, Object> args = new HashMap<>();
            args.put("timeKey", freightId + timeKey + "%");
            List<InoutLossOver> result = getMaxId(args);
            if (null == result || result.size() == 0) {
                cacheId = freightId + timeKey + "001";
            } else {
                int max = 0;
                int num;
                for (InoutLossOver inoutLossOver : result) {
                    num = Integer.parseInt(inoutLossOver.getId().substring(inoutLossOver.getId().length() - 3));
                    max = Math.max(max, num);
                }
                cacheId = freightId + timeKey + String.format("%03d", ++max);
            }
        }
        // 更新缓存
        redisUtil.set(cacheKey, cacheId);
        return cacheId;
    }
}