package org.jeecg.modules.eam.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.jeecg.common.api.vo.Result; import org.jeecg.common.util.DateUtils; import org.jeecg.common.util.StrUtils; import org.jeecg.modules.eam.entity.Identity; import org.jeecg.modules.eam.entity.SerialNumberMap; import org.jeecg.modules.eam.mapper.SerialNumberMapMapper; import org.jeecg.modules.eam.service.IdentityService; import org.jeecg.modules.eam.service.SerialNumberMapService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; @Service public class SerialNumberMapServiceImpl extends ServiceImpl implements SerialNumberMapService { @Autowired private IdentityService mesSysIdentityService; // @Autowired // private IEnterpriseService enterpriseService; /** * 生成完整单号【起始固定字符+企业代码(5)+年(2)+月(2)+日(2)+流水号(4)】 * @param type 业务类型 * @return */ @Override public Result getSerialNumber(String type) { String serialNumber = ""; if(StrUtils.isNotBlankOrNull(type)){ // 获取映射表对象 SerialNumberMap byType = getByType(type); if(byType!=null){ // 判断mes_sys_identity表是否有当前type boolean b = checkIdentityByType(type); if(b){ // 单号生产规则 起始固定字符+企业代码(5)+年(2)+月(2)+日(2)+流水号(4) String num = super.baseMapper.getNum(type); String substring = num.substring(2, num.length()); serialNumber = byType.getBeginSymbol() + byType.getEnterpriseCode()+ DateUtils.date2Str(new SimpleDateFormat(byType.getYearFormat()+byType.getMonthFormat()+byType.getDayFormat())) +substring; }else{ return Result.error("生成单号失败!"); } }else{ return Result.error("类型不存在,请添加后重试!"); } }else{ return Result.error("参数错误,类型不能为空!"); } return Result.ok(serialNumber); } /** * 生成单号前缀 【起始固定字符+企业代码(5)+年(2)+月(2)+日(2)】 * @param type 业务类型 * @return */ @Override public Result getSerial(String type) { String serialNumber = ""; // Enterprise enterprise = enterpriseService.gerCurrentEnterprise(); // String enterpriseCode = enterprise.getCode().substring(0,5); if(StrUtils.isNotBlankOrNull(type)){ // 获取映射表对象 SerialNumberMap byType = getByType(type); if(byType!=null){ // 判断mes_sys_identity表是否有当前type // boolean b = checkIdentityByType(type); // if(b){ // 单号生产规则 起始固定字符+企业代码(5)+年(2)+月(2)+日(2)+流水号(4) serialNumber = byType.getBeginSymbol() + DateUtils.date2Str(new SimpleDateFormat(byType.getYearFormat()+byType.getMonthFormat())); // serialNumber = byType.getBeginSymbol() + enterpriseCode + // DateUtils.date2Str(new SimpleDateFormat(byType.getYearFormat()+byType.getMonthFormat()+byType.getDayFormat())); // }else { // return Result.error("生成单号前缀失败!"); // } }else{ return Result.error("类型不存在,请添加后重试!"); } }else{ return Result.error("参数错误,类型不能为空!"); } return Result.ok(serialNumber); } @Override public SerialNumberMap getByType(String type) { SerialNumberMap byType = super.getBaseMapper().getByType(type); return byType; } @Override public boolean updateBatchByEnterpriseCode(String enterpriseCode) { return super.baseMapper.updateBatchByEnterpriseCode(enterpriseCode); } /** * 判断mes_sys_identity表是否有当前type,如果没有新增一条 * @param type 业务类型 * @return */ public boolean checkIdentityByType(String type){ boolean b = false; // 通过type获取mes_sys_identity表实例 Identity identityByType = mesSysIdentityService.findIdentityByType(type); if(identityByType!=null){ b = true; }else{ Identity mesSysIdentity = new Identity(); mesSysIdentity.setType(type); mesSysIdentity.setMonthValue(DateUtils.date2Str(new SimpleDateFormat("MM"))); mesSysIdentity.setYearValue(DateUtils.date2Str(new SimpleDateFormat("yyyy"))); mesSysIdentity.setNum(0); mesSysIdentity.setRemark("自动添加"); boolean save = mesSysIdentityService.save(mesSysIdentity); b = save; } return b; } }