| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- 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<String,Object> populationMap(TBExchangeAccountEntity t){
- Map<String,Object> map = new HashMap<String,Object>();
- 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<String,Object> 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<ExchangeAccountDto> list = exchangeAccountDao.getAccountList(dto, 1, Integer.MAX_VALUE);
- List<TBExchangeAccountEntity> accountList = new ArrayList<>(list.getResults().size());
- accountList.addAll(list.getResults());
- return clearLastYearAccountByList(accountList,yearMonth);
- }
- @Override
- public boolean clearAccountByIds(List<String> 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<TBExchangeAccountEntity> 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;
- }
- }
|