TBExchangeAccountServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. package cn.com.lzt.exchangeaccount.service.impl;
  2. import cn.com.lzt.common.util.UserUtil;
  3. import cn.com.lzt.exchangeaccount.dao.ExchangeAccountDao;
  4. import cn.com.lzt.exchangeaccount.dto.ExchangeAccountDto;
  5. import cn.com.lzt.exchangeaccount.entity.TBExchangeAccountLogEntity;
  6. import cn.com.lzt.exchangeaccount.service.TBExchangeAccountServiceI;
  7. import org.apache.commons.lang.StringUtils;
  8. import org.apache.commons.lang3.time.DateFormatUtils;
  9. import org.jeecgframework.core.common.exception.BusinessException;
  10. import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
  11. import cn.com.lzt.exchangeaccount.entity.TBExchangeAccountEntity;
  12. import org.jeecgframework.core.util.ResourceUtil;
  13. import org.jeecgframework.minidao.pojo.MiniDaoPage;
  14. import org.jeecgframework.web.system.pojo.base.TSUser;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import java.text.ParseException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.*;
  21. import java.io.Serializable;
  22. import org.jeecgframework.core.util.ApplicationContextUtil;
  23. import org.jeecgframework.core.util.MyClassLoader;
  24. import org.jeecgframework.core.util.StringUtil;
  25. import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;
  26. @Service("tBExchangeAccountService")
  27. @Transactional
  28. public class TBExchangeAccountServiceImpl extends CommonServiceImpl implements TBExchangeAccountServiceI {
  29. @Autowired
  30. private ExchangeAccountDao exchangeAccountDao;
  31. public void delete(TBExchangeAccountEntity entity) throws Exception{
  32. super.delete(entity);
  33. //执行删除操作增强业务
  34. this.doDelBus(entity);
  35. }
  36. public Serializable save(TBExchangeAccountEntity entity) throws Exception{
  37. Serializable t = super.save(entity);
  38. //执行新增操作增强业务
  39. this.doAddBus(entity);
  40. return t;
  41. }
  42. public void saveOrUpdate(TBExchangeAccountEntity entity) throws Exception{
  43. super.saveOrUpdate(entity);
  44. //执行更新操作增强业务
  45. this.doUpdateBus(entity);
  46. }
  47. @Override
  48. public double changeBalance(String userId,String year,Date useTime, double hour,String remark,TBExchangeAccountEntity.sourceType sourceType,String sourceId,String yearMonth) {
  49. if(year==null) year = getYear(null);
  50. TBExchangeAccountEntity accountEntity = getAccount(userId,year);
  51. if(accountEntity==null){//没有账户 创建一个
  52. accountEntity = new TBExchangeAccountEntity();
  53. accountEntity.setHours(0d);
  54. accountEntity.setCreateDate(new Date());
  55. accountEntity.setUserid(userId);
  56. accountEntity.setYear(year);
  57. try {
  58. save(accountEntity);
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. if(sourceType==TBExchangeAccountEntity.sourceType.exchange){//调休时判断能否使用去年的余额
  64. if(canUserLastYearAccount(useTime)){
  65. Integer lastYear = Integer.valueOf(year)-1;
  66. TBExchangeAccountEntity lastYearAccountEntity = getAccount(userId,lastYear.toString());
  67. if(lastYearAccountEntity!=null){
  68. double lastYearLeftHours = lastYearAccountEntity.getHours();
  69. double lastSubHour = 0;
  70. //调休时 hour是负数
  71. if(-hour<=lastYearLeftHours){
  72. lastSubHour = -hour;
  73. hour = 0;
  74. }else {
  75. lastSubHour = lastYearLeftHours;
  76. hour += lastYearLeftHours;
  77. }
  78. lastYearLeftHours-=lastSubHour;
  79. lastYearAccountEntity.setHours(lastYearLeftHours);
  80. updateEntitie(lastYearAccountEntity);
  81. //写日志
  82. TBExchangeAccountLogEntity logEntity = new TBExchangeAccountLogEntity();
  83. logEntity.setExchangeAccountId(lastYearAccountEntity.getId());
  84. logEntity.setBalance(lastYearLeftHours);
  85. logEntity.setChange(-lastSubHour);
  86. logEntity.setUserid(userId);
  87. logEntity.setYear(lastYear.toString());
  88. logEntity.setCreateDate(new Date());
  89. logEntity.setRemark(remark);
  90. logEntity.setSourceType(sourceType.toString());
  91. logEntity.setSourceId(sourceId);
  92. logEntity.setYearMonth(yearMonth);
  93. save(logEntity);
  94. }
  95. }
  96. }
  97. if(hour==0) return accountEntity.getHours();
  98. double leftHour = accountEntity.getHours();
  99. double newHour=leftHour+hour;
  100. if(hour<0 && newHour<0){
  101. throw new BusinessException("调休账户余额不足");
  102. }
  103. accountEntity.setHours(newHour);
  104. updateEntitie(accountEntity);
  105. //写日志
  106. TBExchangeAccountLogEntity logEntity = new TBExchangeAccountLogEntity();
  107. logEntity.setExchangeAccountId(accountEntity.getId());
  108. logEntity.setBalance(newHour);
  109. logEntity.setChange(hour);
  110. logEntity.setUserid(userId);
  111. logEntity.setYear(year);
  112. logEntity.setCreateDate(new Date());
  113. logEntity.setRemark(remark);
  114. logEntity.setSourceType(sourceType.toString());
  115. logEntity.setSourceId(sourceId);
  116. logEntity.setYearMonth(yearMonth);
  117. save(logEntity);
  118. return 0;
  119. }
  120. @Override
  121. public double getBalance(String userId, String year,Date useTime) {
  122. if(year==null) {
  123. year = getYear(null);
  124. }
  125. TBExchangeAccountEntity accountEntity= getAccount(userId,year);
  126. double hours = 0;
  127. if(accountEntity!=null){
  128. hours= accountEntity.getHours();
  129. }
  130. if(canUserLastYearAccount(useTime)) {
  131. double lastYearHours = 0;
  132. Integer lastYear = Integer.parseInt(year) - 1;
  133. TBExchangeAccountEntity lastYearAccountEntity = getAccount(userId, lastYear.toString());
  134. if (lastYearAccountEntity != null) {
  135. lastYearHours = lastYearAccountEntity.getHours();
  136. }
  137. hours+=lastYearHours;
  138. }
  139. return hours;
  140. }
  141. private TBExchangeAccountEntity getAccount(String userId,String year){
  142. TBExchangeAccountEntity accountEntity = singleResult(String.format("from TBExchangeAccountEntity where userid='%s' and year='%s'",userId,year));
  143. return accountEntity;
  144. }
  145. /**
  146. * 新增操作增强业务
  147. * @param t
  148. * @return
  149. */
  150. private void doAddBus(TBExchangeAccountEntity t) throws Exception{
  151. //-----------------sql增强 start----------------------------
  152. //-----------------sql增强 end------------------------------
  153. //-----------------java增强 start---------------------------
  154. //-----------------java增强 end-----------------------------
  155. }
  156. /**
  157. * 更新操作增强业务
  158. * @param t
  159. * @return
  160. */
  161. private void doUpdateBus(TBExchangeAccountEntity t) throws Exception{
  162. //-----------------sql增强 start----------------------------
  163. //-----------------sql增强 end------------------------------
  164. //-----------------java增强 start---------------------------
  165. //-----------------java增强 end-----------------------------
  166. }
  167. /**
  168. * 删除操作增强业务
  169. * @return
  170. */
  171. private void doDelBus(TBExchangeAccountEntity t) throws Exception{
  172. //-----------------sql增强 start----------------------------
  173. //-----------------sql增强 end------------------------------
  174. //-----------------java增强 start---------------------------
  175. //-----------------java增强 end-----------------------------
  176. }
  177. private Map<String,Object> populationMap(TBExchangeAccountEntity t){
  178. Map<String,Object> map = new HashMap<String,Object>();
  179. map.put("id", t.getId());
  180. map.put("create_name", t.getCreateName());
  181. map.put("create_by", t.getCreateBy());
  182. map.put("create_date", t.getCreateDate());
  183. map.put("update_name", t.getUpdateName());
  184. map.put("update_by", t.getUpdateBy());
  185. map.put("update_date", t.getUpdateDate());
  186. map.put("sys_org_code", t.getSysOrgCode());
  187. map.put("sys_company_code", t.getSysCompanyCode());
  188. map.put("bpm_status", t.getBpmStatus());
  189. map.put("userid", t.getUserid());
  190. map.put("year", t.getYear());
  191. map.put("hours", t.getHours());
  192. return map;
  193. }
  194. /**
  195. * 替换sql中的变量
  196. * @param sql
  197. * @param t
  198. * @return
  199. */
  200. public String replaceVal(String sql,TBExchangeAccountEntity t){
  201. sql = sql.replace("#{id}",String.valueOf(t.getId()));
  202. sql = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
  203. sql = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
  204. sql = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
  205. sql = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
  206. sql = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
  207. sql = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
  208. sql = sql.replace("#{sys_org_code}",String.valueOf(t.getSysOrgCode()));
  209. sql = sql.replace("#{sys_company_code}",String.valueOf(t.getSysCompanyCode()));
  210. sql = sql.replace("#{bpm_status}",String.valueOf(t.getBpmStatus()));
  211. sql = sql.replace("#{userid}",String.valueOf(t.getUserid()));
  212. sql = sql.replace("#{year}",String.valueOf(t.getYear()));
  213. sql = sql.replace("#{hours}",String.valueOf(t.getHours()));
  214. sql = sql.replace("#{UUID}",UUID.randomUUID().toString());
  215. return sql;
  216. }
  217. /**
  218. * 执行JAVA增强
  219. */
  220. private void executeJavaExtend(String cgJavaType,String cgJavaValue,Map<String,Object> data) throws Exception {
  221. if(StringUtil.isNotEmpty(cgJavaValue)){
  222. Object obj = null;
  223. try {
  224. if("class".equals(cgJavaType)){
  225. //因新增时已经校验了实例化是否可以成功,所以这块就不需要再做一次判断
  226. obj = MyClassLoader.getClassByScn(cgJavaValue).newInstance();
  227. }else if("spring".equals(cgJavaType)){
  228. obj = ApplicationContextUtil.getContext().getBean(cgJavaValue);
  229. }
  230. if(obj instanceof CgformEnhanceJavaInter){
  231. CgformEnhanceJavaInter javaInter = (CgformEnhanceJavaInter) obj;
  232. javaInter.execute("t_b_exchange_account",data);
  233. }
  234. } catch (Exception e) {
  235. e.printStackTrace();
  236. throw new Exception("执行JAVA增强出现异常!");
  237. }
  238. }
  239. }
  240. @Override
  241. public boolean clearLastYearAccount(String yearMonth) {
  242. Calendar c = Calendar.getInstance();
  243. c.add(Calendar.YEAR,-1);
  244. int year =c.get(Calendar.YEAR);
  245. ExchangeAccountDto dto = new ExchangeAccountDto();
  246. dto.setYear(String.valueOf(year));
  247. TSUser user = ResourceUtil.getSessionUser();
  248. boolean projectUser =UserUtil.isProjectUser(user.getId());
  249. if(projectUser){
  250. dto.setLoginUserId(user.getId());
  251. }
  252. MiniDaoPage<ExchangeAccountDto> list = exchangeAccountDao.getAccountList(dto, 1, Integer.MAX_VALUE);
  253. List<TBExchangeAccountEntity> accountList = new ArrayList<>(list.getResults().size());
  254. accountList.addAll(list.getResults());
  255. return clearLastYearAccountByList(accountList,yearMonth);
  256. }
  257. @Override
  258. public boolean clearAccountByIds(List<String> ids, String yearMonth) {
  259. boolean flag = true;
  260. for(String id:ids){
  261. TBExchangeAccountEntity entity = getEntity(TBExchangeAccountEntity.class,id);
  262. flag |= clearAccount(entity,yearMonth,TBExchangeAccountEntity.sourceType.batchRemove);
  263. }
  264. return flag;
  265. }
  266. public boolean clearLastYearAccountByList(List<TBExchangeAccountEntity> accountList, String yearMonth) {
  267. String patternDay = "yyyy-MM-dd";
  268. String curDay = DateFormatUtils.format(System.currentTimeMillis(), patternDay);
  269. Calendar c = Calendar.getInstance();
  270. c.add(Calendar.YEAR,-1);
  271. int year =c.get(Calendar.YEAR);
  272. TSUser user = ResourceUtil.getSessionUser();
  273. for(TBExchangeAccountEntity entity:accountList){
  274. if(!entity.getYear().equals(String.valueOf(year))) {
  275. continue;
  276. }
  277. String remark = "结算去年余额:操作日期:%s,年份:%s,结算月份:%s,共%.1f小时";
  278. remark = String.format(remark,curDay,year,yearMonth,entity.getHours());
  279. changeBalance(entity.getUserid(), String.valueOf(year), new Date(),-entity.getHours()
  280. , remark, TBExchangeAccountEntity.sourceType.buttonRemoveLastYear
  281. ,user.getId(),yearMonth);
  282. }
  283. return true;
  284. }
  285. @Override
  286. public boolean clearAccount(String userId, String year,String yearMonth) {
  287. TBExchangeAccountEntity entity = getAccount(userId, year);
  288. return clearAccount(entity,yearMonth,TBExchangeAccountEntity.sourceType.buttonRemove);
  289. }
  290. @Override
  291. public String getYear(Date date) {
  292. String exchange_account_year_split_day = ResourceUtil.getConfigByName("exchange_account_year_split_day");
  293. if(date==null){
  294. date = new Date();
  295. }
  296. Calendar c = Calendar.getInstance();
  297. c.setTime(date);
  298. Integer defaultCurYear = c.get(Calendar.YEAR);
  299. if(StringUtils.isBlank(exchange_account_year_split_day)) return String.valueOf(defaultCurYear);
  300. SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM-dd");
  301. Date splitDate;
  302. try {
  303. splitDate =monthFormat.parse(defaultCurYear+"-"+exchange_account_year_split_day);
  304. } catch (ParseException e) {
  305. e.printStackTrace();
  306. return null;
  307. }
  308. if(date.getTime()>=splitDate.getTime()){//当年
  309. return String.valueOf(defaultCurYear);
  310. }else{
  311. return String.valueOf((defaultCurYear)-1);
  312. }
  313. }
  314. private boolean clearAccount(TBExchangeAccountEntity entity,String yearMonth,TBExchangeAccountEntity.sourceType sourceType) {
  315. if(entity!=null) {
  316. String userId = entity.getUserid();
  317. String year = entity.getYear();
  318. String patternDay = "yyyy-MM-dd";
  319. String curDay = DateFormatUtils.format(System.currentTimeMillis(), patternDay);
  320. double leftHours = entity.getHours();
  321. TSUser user = ResourceUtil.getSessionUser();
  322. String remark = "结算余额:操作日期:%s,结算月份:%s,共%.1f小时";
  323. remark = String.format(remark,curDay,yearMonth,leftHours);
  324. changeBalance(userId, year,new Date(),-leftHours, remark,sourceType ,user.getId(),yearMonth);
  325. return true;
  326. }
  327. return false;
  328. }
  329. private boolean canUserLastYearAccount(Date useTime){
  330. String beforeDay = ResourceUtil.getConfigByName("exchange_lastyear_account_use_before");
  331. Calendar c = Calendar.getInstance();
  332. if(useTime!=null){
  333. c.setTime(useTime);
  334. }
  335. Integer defaultCurYear = c.get(Calendar.YEAR);
  336. SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM-dd");
  337. Date beforeDate=null;
  338. try {
  339. beforeDate =monthFormat.parse(defaultCurYear+"-"+beforeDay);
  340. } catch (ParseException e) {
  341. e.printStackTrace();
  342. }
  343. c.set(Calendar.HOUR,0 );
  344. c.set(Calendar.MINUTE,0 );
  345. c.set(Calendar.SECOND,0 );
  346. c.set(Calendar.MILLISECOND,0);
  347. Date curDate = c.getTime();
  348. if(curDate.compareTo(beforeDate)<=0){
  349. return true;
  350. }
  351. return false;
  352. }
  353. }