package cn.com.lzt.exchangeaccount.service.impl; import cn.com.lzt.common.util.UserUtil; import cn.com.lzt.exchangeaccount.dao.ExchangeAccountDao; import cn.com.lzt.exchangeaccount.dto.ExchangeAccountDto; import cn.com.lzt.exchangeaccount.entity.TBExchangeAccountLogEntity; import cn.com.lzt.exchangeaccount.service.TBExchangeAccountServiceI; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.jeecgframework.core.common.exception.BusinessException; import org.jeecgframework.core.common.service.impl.CommonServiceImpl; import cn.com.lzt.exchangeaccount.entity.TBExchangeAccountEntity; import org.jeecgframework.core.util.ResourceUtil; import org.jeecgframework.minidao.pojo.MiniDaoPage; import org.jeecgframework.web.system.pojo.base.TSUser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.io.Serializable; import org.jeecgframework.core.util.ApplicationContextUtil; import org.jeecgframework.core.util.MyClassLoader; import org.jeecgframework.core.util.StringUtil; import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter; @Service("tBExchangeAccountService") @Transactional public class TBExchangeAccountServiceImpl extends CommonServiceImpl implements TBExchangeAccountServiceI { @Autowired private ExchangeAccountDao exchangeAccountDao; public void delete(TBExchangeAccountEntity entity) throws Exception{ super.delete(entity); //执行删除操作增强业务 this.doDelBus(entity); } public Serializable save(TBExchangeAccountEntity entity) throws Exception{ Serializable t = super.save(entity); //执行新增操作增强业务 this.doAddBus(entity); return t; } public void saveOrUpdate(TBExchangeAccountEntity entity) throws Exception{ super.saveOrUpdate(entity); //执行更新操作增强业务 this.doUpdateBus(entity); } @Override public double changeBalance(String userId,String year,Date useTime, double hour,String remark,TBExchangeAccountEntity.sourceType sourceType,String sourceId,String yearMonth) { if(year==null) year = getYear(null); TBExchangeAccountEntity accountEntity = getAccount(userId,year); if(accountEntity==null){//没有账户 创建一个 accountEntity = new TBExchangeAccountEntity(); accountEntity.setHours(0d); accountEntity.setCreateDate(new Date()); accountEntity.setUserid(userId); accountEntity.setYear(year); try { save(accountEntity); } catch (Exception e) { e.printStackTrace(); } } if(sourceType==TBExchangeAccountEntity.sourceType.exchange){//调休时判断能否使用去年的余额 if(canUserLastYearAccount(useTime)){ Integer lastYear = Integer.valueOf(year)-1; TBExchangeAccountEntity lastYearAccountEntity = getAccount(userId,lastYear.toString()); if(lastYearAccountEntity!=null){ double lastYearLeftHours = lastYearAccountEntity.getHours(); double lastSubHour = 0; //调休时 hour是负数 if(-hour<=lastYearLeftHours){ lastSubHour = -hour; hour = 0; }else { lastSubHour = lastYearLeftHours; hour += lastYearLeftHours; } lastYearLeftHours-=lastSubHour; lastYearAccountEntity.setHours(lastYearLeftHours); updateEntitie(lastYearAccountEntity); //写日志 TBExchangeAccountLogEntity logEntity = new TBExchangeAccountLogEntity(); logEntity.setExchangeAccountId(lastYearAccountEntity.getId()); logEntity.setBalance(lastYearLeftHours); logEntity.setChange(-lastSubHour); logEntity.setUserid(userId); logEntity.setYear(lastYear.toString()); logEntity.setCreateDate(new Date()); logEntity.setRemark(remark); logEntity.setSourceType(sourceType.toString()); logEntity.setSourceId(sourceId); logEntity.setYearMonth(yearMonth); save(logEntity); } } } if(hour==0) return accountEntity.getHours(); double leftHour = accountEntity.getHours(); double newHour=leftHour+hour; if(hour<0 && newHour<0){ throw new BusinessException("调休账户余额不足"); } accountEntity.setHours(newHour); updateEntitie(accountEntity); //写日志 TBExchangeAccountLogEntity logEntity = new TBExchangeAccountLogEntity(); logEntity.setExchangeAccountId(accountEntity.getId()); logEntity.setBalance(newHour); logEntity.setChange(hour); logEntity.setUserid(userId); logEntity.setYear(year); logEntity.setCreateDate(new Date()); logEntity.setRemark(remark); logEntity.setSourceType(sourceType.toString()); logEntity.setSourceId(sourceId); logEntity.setYearMonth(yearMonth); save(logEntity); return 0; } @Override public double getBalance(String userId, String year,Date useTime) { if(year==null) { year = getYear(null); } TBExchangeAccountEntity accountEntity= getAccount(userId,year); double hours = 0; if(accountEntity!=null){ hours= accountEntity.getHours(); } if(canUserLastYearAccount(useTime)) { double lastYearHours = 0; Integer lastYear = Integer.parseInt(year) - 1; TBExchangeAccountEntity lastYearAccountEntity = getAccount(userId, lastYear.toString()); if (lastYearAccountEntity != null) { lastYearHours = lastYearAccountEntity.getHours(); } hours+=lastYearHours; } return hours; } private TBExchangeAccountEntity getAccount(String userId,String year){ TBExchangeAccountEntity accountEntity = singleResult(String.format("from TBExchangeAccountEntity where userid='%s' and year='%s'",userId,year)); return accountEntity; } /** * 新增操作增强业务 * @param t * @return */ private void doAddBus(TBExchangeAccountEntity t) throws Exception{ //-----------------sql增强 start---------------------------- //-----------------sql增强 end------------------------------ //-----------------java增强 start--------------------------- //-----------------java增强 end----------------------------- } /** * 更新操作增强业务 * @param t * @return */ private void doUpdateBus(TBExchangeAccountEntity t) throws Exception{ //-----------------sql增强 start---------------------------- //-----------------sql增强 end------------------------------ //-----------------java增强 start--------------------------- //-----------------java增强 end----------------------------- } /** * 删除操作增强业务 * @return */ private void doDelBus(TBExchangeAccountEntity t) throws Exception{ //-----------------sql增强 start---------------------------- //-----------------sql增强 end------------------------------ //-----------------java增强 start--------------------------- //-----------------java增强 end----------------------------- } private Map populationMap(TBExchangeAccountEntity t){ Map map = new HashMap(); map.put("id", t.getId()); map.put("create_name", t.getCreateName()); map.put("create_by", t.getCreateBy()); map.put("create_date", t.getCreateDate()); map.put("update_name", t.getUpdateName()); map.put("update_by", t.getUpdateBy()); map.put("update_date", t.getUpdateDate()); map.put("sys_org_code", t.getSysOrgCode()); map.put("sys_company_code", t.getSysCompanyCode()); map.put("bpm_status", t.getBpmStatus()); map.put("userid", t.getUserid()); map.put("year", t.getYear()); map.put("hours", t.getHours()); return map; } /** * 替换sql中的变量 * @param sql * @param t * @return */ public String replaceVal(String sql,TBExchangeAccountEntity t){ sql = sql.replace("#{id}",String.valueOf(t.getId())); sql = sql.replace("#{create_name}",String.valueOf(t.getCreateName())); sql = sql.replace("#{create_by}",String.valueOf(t.getCreateBy())); sql = sql.replace("#{create_date}",String.valueOf(t.getCreateDate())); sql = sql.replace("#{update_name}",String.valueOf(t.getUpdateName())); sql = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy())); sql = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate())); sql = sql.replace("#{sys_org_code}",String.valueOf(t.getSysOrgCode())); sql = sql.replace("#{sys_company_code}",String.valueOf(t.getSysCompanyCode())); sql = sql.replace("#{bpm_status}",String.valueOf(t.getBpmStatus())); sql = sql.replace("#{userid}",String.valueOf(t.getUserid())); sql = sql.replace("#{year}",String.valueOf(t.getYear())); sql = sql.replace("#{hours}",String.valueOf(t.getHours())); sql = sql.replace("#{UUID}",UUID.randomUUID().toString()); return sql; } /** * 执行JAVA增强 */ private void executeJavaExtend(String cgJavaType,String cgJavaValue,Map data) throws Exception { if(StringUtil.isNotEmpty(cgJavaValue)){ Object obj = null; try { if("class".equals(cgJavaType)){ //因新增时已经校验了实例化是否可以成功,所以这块就不需要再做一次判断 obj = MyClassLoader.getClassByScn(cgJavaValue).newInstance(); }else if("spring".equals(cgJavaType)){ obj = ApplicationContextUtil.getContext().getBean(cgJavaValue); } if(obj instanceof CgformEnhanceJavaInter){ CgformEnhanceJavaInter javaInter = (CgformEnhanceJavaInter) obj; javaInter.execute("t_b_exchange_account",data); } } catch (Exception e) { e.printStackTrace(); throw new Exception("执行JAVA增强出现异常!"); } } } @Override public boolean clearLastYearAccount(String yearMonth) { Calendar c = Calendar.getInstance(); c.add(Calendar.YEAR,-1); int year =c.get(Calendar.YEAR); ExchangeAccountDto dto = new ExchangeAccountDto(); dto.setYear(String.valueOf(year)); TSUser user = ResourceUtil.getSessionUser(); boolean projectUser =UserUtil.isProjectUser(user.getId()); if(projectUser){ dto.setLoginUserId(user.getId()); } MiniDaoPage list = exchangeAccountDao.getAccountList(dto, 1, Integer.MAX_VALUE); List accountList = new ArrayList<>(list.getResults().size()); accountList.addAll(list.getResults()); return clearLastYearAccountByList(accountList,yearMonth); } @Override public boolean clearAccountByIds(List ids, String yearMonth) { boolean flag = true; for(String id:ids){ TBExchangeAccountEntity entity = getEntity(TBExchangeAccountEntity.class,id); flag |= clearAccount(entity,yearMonth,TBExchangeAccountEntity.sourceType.batchRemove); } return flag; } public boolean clearLastYearAccountByList(List accountList, String yearMonth) { String patternDay = "yyyy-MM-dd"; String curDay = DateFormatUtils.format(System.currentTimeMillis(), patternDay); Calendar c = Calendar.getInstance(); c.add(Calendar.YEAR,-1); int year =c.get(Calendar.YEAR); TSUser user = ResourceUtil.getSessionUser(); for(TBExchangeAccountEntity entity:accountList){ if(!entity.getYear().equals(String.valueOf(year))) { continue; } String remark = "结算去年余额:操作日期:%s,年份:%s,结算月份:%s,共%.1f小时"; remark = String.format(remark,curDay,year,yearMonth,entity.getHours()); changeBalance(entity.getUserid(), String.valueOf(year), new Date(),-entity.getHours() , remark, TBExchangeAccountEntity.sourceType.buttonRemoveLastYear ,user.getId(),yearMonth); } return true; } @Override public boolean clearAccount(String userId, String year,String yearMonth) { TBExchangeAccountEntity entity = getAccount(userId, year); return clearAccount(entity,yearMonth,TBExchangeAccountEntity.sourceType.buttonRemove); } @Override public String getYear(Date date) { String exchange_account_year_split_day = ResourceUtil.getConfigByName("exchange_account_year_split_day"); if(date==null){ date = new Date(); } Calendar c = Calendar.getInstance(); c.setTime(date); Integer defaultCurYear = c.get(Calendar.YEAR); if(StringUtils.isBlank(exchange_account_year_split_day)) return String.valueOf(defaultCurYear); SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM-dd"); Date splitDate; try { splitDate =monthFormat.parse(defaultCurYear+"-"+exchange_account_year_split_day); } catch (ParseException e) { e.printStackTrace(); return null; } if(date.getTime()>=splitDate.getTime()){//当年 return String.valueOf(defaultCurYear); }else{ return String.valueOf((defaultCurYear)-1); } } private boolean clearAccount(TBExchangeAccountEntity entity,String yearMonth,TBExchangeAccountEntity.sourceType sourceType) { if(entity!=null) { String userId = entity.getUserid(); String year = entity.getYear(); String patternDay = "yyyy-MM-dd"; String curDay = DateFormatUtils.format(System.currentTimeMillis(), patternDay); double leftHours = entity.getHours(); TSUser user = ResourceUtil.getSessionUser(); String remark = "结算余额:操作日期:%s,结算月份:%s,共%.1f小时"; remark = String.format(remark,curDay,yearMonth,leftHours); changeBalance(userId, year,new Date(),-leftHours, remark,sourceType ,user.getId(),yearMonth); return true; } return false; } private boolean canUserLastYearAccount(Date useTime){ String beforeDay = ResourceUtil.getConfigByName("exchange_lastyear_account_use_before"); Calendar c = Calendar.getInstance(); if(useTime!=null){ c.setTime(useTime); } Integer defaultCurYear = c.get(Calendar.YEAR); SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM-dd"); Date beforeDate=null; try { beforeDate =monthFormat.parse(defaultCurYear+"-"+beforeDay); } catch (ParseException e) { e.printStackTrace(); } c.set(Calendar.HOUR,0 ); c.set(Calendar.MINUTE,0 ); c.set(Calendar.SECOND,0 ); c.set(Calendar.MILLISECOND,0); Date curDate = c.getTime(); if(curDate.compareTo(beforeDate)<=0){ return true; } return false; } }