ExpenseAccountServiceImpl.java 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. package com.goafanti.expenseAccount.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.goafanti.admin.bo.AdminListBo;
  4. import com.goafanti.admin.service.DepartmentService;
  5. import com.goafanti.common.bo.AdminNoticeBo;
  6. import com.goafanti.common.bo.EmailBo;
  7. import com.goafanti.common.constant.AFTConstants;
  8. import com.goafanti.common.dao.*;
  9. import com.goafanti.common.enums.NoticeStatus;
  10. import com.goafanti.common.enums.NoticeTypes;
  11. import com.goafanti.common.error.BusinessException;
  12. import com.goafanti.common.model.*;
  13. import com.goafanti.common.utils.AsyncUtils;
  14. import com.goafanti.common.utils.DateUtils;
  15. import com.goafanti.common.utils.LoggerUtils;
  16. import com.goafanti.common.utils.WeChatUtils;
  17. import com.goafanti.core.mybatis.BaseMybatisDao;
  18. import com.goafanti.core.mybatis.JDBCIdGenerator;
  19. import com.goafanti.core.mybatis.page.Pagination;
  20. import com.goafanti.core.shiro.token.TokenManager;
  21. import com.goafanti.expenseAccount.Enums.EAProcessStatus;
  22. import com.goafanti.expenseAccount.Enums.EATypes;
  23. import com.goafanti.expenseAccount.Enums.EAsecondaryTypes;
  24. import com.goafanti.expenseAccount.bo.*;
  25. import com.goafanti.expenseAccount.service.ExpenseAccountService;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.cache.annotation.CacheEvict;
  28. import org.springframework.cache.annotation.Cacheable;
  29. import org.springframework.stereotype.Service;
  30. import org.springframework.transaction.annotation.Transactional;
  31. import java.math.BigDecimal;
  32. import java.util.*;
  33. @Service
  34. public class ExpenseAccountServiceImpl extends BaseMybatisDao<ExpenseAccountMapper> implements ExpenseAccountService {
  35. @Autowired
  36. private AdminMapper adminMapper;
  37. @Autowired
  38. private ExpenseAccountMapper expenseAccountMapper;
  39. @Autowired
  40. private PublicReleaseMapper publicReleaseMapper;
  41. @Autowired
  42. private ExpenseAccountLogMapper expenseAccountLogMapper;
  43. @Autowired
  44. private AsyncUtils asyncUtils;
  45. @Autowired
  46. private WeChatUtils weChatUtils;
  47. @Autowired
  48. private ExpenseAccountDetailsMapper expenseAccountDetailsMapper;
  49. @Autowired
  50. private ExpenseAccountPrivateMapper expenseAccountPrivateMapper;
  51. @Autowired
  52. private ExpenseAccountExamineMapper expenseAccountExamineMapper;
  53. @Autowired
  54. private JDBCIdGenerator idGenerator;
  55. @Autowired
  56. private FinanceCountMapper financeCountMapper;
  57. @Autowired
  58. private ExpenseRelationshipMapper expenseRelationshipMapper;
  59. @Autowired
  60. private DepartmentService departmentService;
  61. @Override
  62. public Integer add(InputExpenseAccount in) {
  63. //如果关联公出,获取公出信息
  64. if (in.getPrid()!=null){
  65. PublicRelease pr = publicReleaseMapper.selectByPrimaryKey(in.getPrid());
  66. in.setReleaseStart(pr.getReleaseStart());
  67. in.setReleaseEnd(pr.getReleaseEnd());
  68. in.setDistrictName(pr.getDistrictName());
  69. in.setUserNames(pr.getUserNames());
  70. in.setDuration(pr.getDuration());
  71. }
  72. //判断报销类型,0=固定费用,1=报销到订单
  73. if (in.getOrderNo()==null){
  74. in.setTargetType(0);
  75. }else {
  76. in.setTargetType(1);
  77. }
  78. if (in.getAid()==null)in.setAid(TokenManager.getAdminId());
  79. Admin admin = adminMapper.selectByPrimaryKey(in.getAid());
  80. in.setAname(admin.getName());
  81. in.setApplyDep(admin.getDepartmentId());
  82. in.setPayDep(admin.getDepartmentId());
  83. expenseAccountMapper.insertSelective(in);
  84. return in.getId();
  85. }
  86. /**
  87. * 新增报销审核流程
  88. */
  89. private void addExpenseAccountExamine(Integer id,ExpenseAccount ea) {
  90. List<ExpenseAccountExamine> list=new ArrayList<>();
  91. String aid = TokenManager.getAdminId();
  92. Admin admin = adminMapper.selectByPrimaryKey(aid);
  93. for (int i=1;i<5;i++){
  94. if (i==1){
  95. //新增一级
  96. ExpenseAccountExamine eae=new ExpenseAccountExamine(id,i);
  97. AdminNoticeBo a = adminMapper.selectMyAndSuperBySuperId(aid);
  98. String examineId=null;
  99. String examineName=null;
  100. //正常用上级,没有的情况下就是自己
  101. if (a.getSuperId()!=null){
  102. examineId=a.getSuperId();
  103. examineName=a.getSuperName();
  104. }else {
  105. examineId=a.getId();
  106. examineName=a.getName();
  107. }
  108. eae.setAuditor(examineId);
  109. eae.setAuditorname(examineName);
  110. ea.setExamineName(examineName);
  111. if (eae.getAuditor().equals(aid))eae.setStatus(1);
  112. else eae.setStatus(0);
  113. list.add(eae);
  114. }else if (i==2){
  115. ExpenseAccountExamine eae=new ExpenseAccountExamine(id,i);
  116. Admin admin2 = adminMapper.selectFinanceAdminByAid(aid);
  117. if (admin2==null){
  118. throw new BusinessException("发起人部门财务专员不存在。");
  119. }
  120. eae.setAuditor(admin2.getId());
  121. eae.setAuditorname(admin2.getName());
  122. if (eae.getAuditor().equals(aid))eae.setStatus(1);
  123. else eae.setStatus(0);
  124. list.add(eae);
  125. }else if (i==3){
  126. List<Admin> admins = adminMapper.listAdminBydepIdAndRoleType(admin.getDepartmentId(), AFTConstants.SALESMAN_ADMIN);
  127. if (admins.isEmpty())throw new BusinessException("发起人部门营销管理员不存在。");
  128. for (Admin e : admins) {
  129. ExpenseAccountExamine eae=new ExpenseAccountExamine(id,i);
  130. eae.setAuditor(e.getId());
  131. eae.setAuditorname(e.getName());
  132. if (eae.getAuditor().equals(aid))eae.setStatus(1);
  133. else eae.setStatus(0);
  134. list.add(eae);
  135. }
  136. }else if (i==4){
  137. List<Admin> admins = adminMapper.selectAdminByRoleType(AFTConstants.APPROVAL_DECISION);
  138. for (Admin e : admins) {
  139. ExpenseAccountExamine eae=new ExpenseAccountExamine(id,i);
  140. eae.setAuditor(e.getId());
  141. eae.setAuditorname(e.getName());
  142. if (eae.getAuditor().equals(aid))eae.setStatus(1);
  143. else eae.setStatus(0);
  144. list.add(eae);
  145. }
  146. }
  147. }
  148. expenseAccountExamineMapper.insertBatch(list);
  149. }
  150. /**
  151. * 新增报销日志
  152. * @param eaid 报销编号
  153. * @param status 状态 0发起 1通过 2完成 3驳回 4修改
  154. * @param str 备注
  155. */
  156. private void addExpenseAccountLog(Integer eaid, Integer status,Integer processStatus,String aid, String str) {
  157. addExpenseAccountLog(eaid,status,processStatus,aid,str,null);
  158. }
  159. /**
  160. * 新增报销日志
  161. * @param eaid 报销编号
  162. * @param status 状态 0发起 1通过 2完成 3驳回 4修改
  163. * @param str 备注
  164. */
  165. private void addExpenseAccountLog(Integer eaid, Integer status,Integer processStatus,String aid, String str,Date date) {
  166. if (aid==null)aid=TokenManager.getAdminId();
  167. ExpenseAccountLog eaLog=new ExpenseAccountLog();
  168. eaLog.setEaid(eaid);
  169. eaLog.setStatus(status);
  170. eaLog.setRemarks(str);
  171. eaLog.setProcessStatus(processStatus);
  172. eaLog.setAuditor(aid);
  173. if (date !=null)eaLog.setCreateTime(date);
  174. expenseAccountLogMapper.insertSelective(eaLog);
  175. }
  176. /**
  177. * 发起、驳回发起流程切换和日志站内信发送
  178. */
  179. private void addExpenseAccountLogAndNoticEmail(ExpenseAccount in, Integer status, String str) {
  180. String aid=TokenManager.getAdminId();
  181. addExpenseAccountLog(in.getId(),status,in.getProcessStatus(),aid,str);
  182. if (status==0)in.setProcessStatus(1);
  183. Admin admin = adminMapper.selectByPrimaryKey(aid);
  184. if (admin.getSuperiorId()==null||admin.getId().equals(admin.getSuperiorId())){
  185. in.setProcessStatus(in.getProcessStatus()+1);
  186. String str2=String.format("重复审核人跳过[%s]。",EAProcessStatus.getDesc(in.getProcessStatus()));
  187. Date date=new Date();
  188. date.setTime(date.getTime()+1000L);
  189. addExpenseAccountLog(in.getId(),1,in.getProcessStatus(),aid,str2,date);
  190. }
  191. createExamineByProcess(in);
  192. }
  193. /**
  194. * 判断审核流程与发送日志与邮件
  195. */
  196. private void createExamineByProcess(ExpenseAccount in) {
  197. //审核流程推送到最新
  198. AdminListBo admin = adminMapper.getDeptNameByAid(in.getAid());
  199. examineByprocess(in,admin);
  200. List<Admin> aids=new ArrayList<>();
  201. if(in.getProcessStatus()== EAProcessStatus.SJSH.getCode()){
  202. //获取上级
  203. Admin superAdmin = adminMapper.selectByPrimaryKey(admin.getSuperiorId());
  204. aids.add(superAdmin);
  205. in.setExamineName(superAdmin.getName());
  206. }else if (in.getProcessStatus()== EAProcessStatus.CWSH.getCode()){
  207. //获取财务负责人
  208. Admin finaceAdmin = adminMapper.selectByPrimaryKey(admin.getDepFinance());
  209. aids.add(finaceAdmin);
  210. in.setExamineName(finaceAdmin.getName());
  211. addFinanceCount( admin.getDepFinance(), new Date());
  212. }else if (in.getProcessStatus()== EAProcessStatus.ZJLSH.getCode()){
  213. //获取部门负责人
  214. List<Admin> list = adminMapper.listAdminBydepIdAndRoleType(admin.getDepartmentId(), AFTConstants.SALESMAN_ADMIN);
  215. if (!list.isEmpty())aids.addAll(list);
  216. in.setExamineName(getAdmins(list));
  217. }else if (in.getProcessStatus()== EAProcessStatus.DSZSH.getCode()){
  218. //获取总裁、董事长等审核
  219. List<Admin> list = adminMapper.selectAdminByRoleType(AFTConstants.All_CED_AND_CHAIRMAN);
  220. if (!list.isEmpty())aids.addAll(list);
  221. in.setExamineName(getAdmins(list));
  222. }
  223. StringBuffer str=new StringBuffer().append("您有报销需要审核,").append("报销编号[").append(in.getId()).append("]").append(",请查看并审核。");
  224. addNoticeAndEmail(in, EAProcessStatus.SJSH.getCode(),str.toString(), admin.getName(),aids);
  225. }
  226. @Override
  227. @Transactional
  228. public int update(InputExpenseAccount in) {
  229. OutExpenseAccount useEA = expenseAccountMapper.selectByid(in.getId());
  230. if (useEA.getStatus()!=0&&useEA.getStatus()!=3){
  231. throw new BusinessException("不能操作审核中与完成的订单");
  232. }
  233. if (in.getStartTimeStr()!=null&&in.getEndTimeStr()!=null){
  234. in.setReleaseStart(DateUtils.StringToDate(in.getStartTimeStr(), AFTConstants.YYYYMMDDHHMMSS));
  235. in.setReleaseEnd(DateUtils.StringToDate(in.getEndTimeStr(), AFTConstants.YYYYMMDDHHMMSS));
  236. }
  237. //计算总金额
  238. if (in.getTotalAmount()==null){
  239. in.setTotalAmount(expenseAccountDetailsMapper.selectAmountCountByEAid(in.getId()));
  240. in.setRealAmount(in.getTotalAmount());
  241. }
  242. in.setProcessStatus(0);
  243. if (in.getOrderNo()==null){
  244. in.setTargetType(0);
  245. }else {
  246. in.setTargetType(1);
  247. }
  248. //计算本单已扣金额 如果有选择借支单,则需要修改借支状态
  249. if (in.getDebitId()!=null){
  250. //如果以前没有选择借支,修改报销和借支数据
  251. if (useEA.getDebitId()==null){
  252. pushExpenseAccountDebit(in);
  253. }else if (useEA.getDebitId()!=in.getDebitId()) {
  254. //如果是修改编号
  255. pushExpenseAccountDebit(in);
  256. //并且要处理原编号的问题
  257. pushresettingDebit(in,useEA.getDebitId());
  258. }
  259. }
  260. in.setStatus(0);
  261. return expenseAccountMapper.updateByPrimaryKeySelective(in);
  262. }
  263. private void pushresettingDebit(InputExpenseAccount in, Integer debitId) {
  264. ExpenseAccount debit = expenseAccountMapper.selectByPrimaryKey(debitId);
  265. List<ExpenseAccount> list = expenseAccountMapper.selectByDebitId(debitId);
  266. ExpenseAccount newdebit=new ExpenseAccount();
  267. newdebit.setId(debitId);
  268. //计算除开当前单,已选择抵扣金额
  269. BigDecimal count=new BigDecimal(0);
  270. for (ExpenseAccount e : list) {
  271. if (!in.getId().equals(e.getId())){
  272. count=count.add(e.getSettlementAmount());
  273. }
  274. }
  275. newdebit.setSettlementAmount(count);
  276. if (count.compareTo(new BigDecimal(0))==0){
  277. newdebit.setLiquidationStatus(0);
  278. }else if (count.compareTo(debit.getTotalAmount())<0){
  279. newdebit.setLiquidationStatus(1);
  280. }else if (count.compareTo(debit.getTotalAmount())==0){
  281. newdebit.setLiquidationStatus(2);
  282. }
  283. expenseAccountMapper.updateByPrimaryKeySelective(newdebit);
  284. }
  285. private void pushExpenseAccountDebit(InputExpenseAccount in) {
  286. ExpenseAccount debit = expenseAccountMapper.selectByPrimaryKey(in.getDebitId());
  287. ExpenseAccount newDebit=new ExpenseAccount();
  288. newDebit.setId(debit.getId());
  289. //计算剩余需要支付金额
  290. BigDecimal decbitAmount=debit.getTotalAmount().subtract(debit.getSettlementAmount());
  291. //本次金额大于等于需要抵扣金额,则变成完全抵扣,抵扣金额变成本次已付金额
  292. if (in.getTotalAmount().compareTo(decbitAmount)>=0){
  293. in.setSettlementAmount(decbitAmount);
  294. newDebit.setSettlementAmount(debit.getTotalAmount());
  295. newDebit.setLiquidationStatus(2);
  296. }else {
  297. // 本次金额小于需要抵扣,则本次变更全抵扣,借支已付加上本次金额
  298. in.setSettlementAmount(in.getTotalAmount());
  299. BigDecimal setAmount=new BigDecimal(0).add(debit.getSettlementAmount()).add(in.getTotalAmount());
  300. newDebit.setSettlementAmount(setAmount);
  301. newDebit.setLiquidationStatus(1);
  302. }
  303. expenseAccountMapper.updateByPrimaryKeySelective(newDebit);
  304. }
  305. @Override
  306. public int addDetails(InputExpenseAccountDetails in) {
  307. if (in.getAgreeTimes()!=null){
  308. in.setAgreeTime(DateUtils.StringToDate(in.getAgreeTimes(),AFTConstants.YYYYMMDD));
  309. }
  310. ExpenseAccount expenseAccount = expenseAccountMapper.selectByPrimaryKey(in.getEaid());
  311. if (expenseAccount.getType()==2){
  312. in.setType(expenseAccount.getSecondaryType());
  313. if (in.getType()==0){
  314. in.setTypeOther(expenseAccount.getSecondaryTypeOther());
  315. }
  316. }
  317. in.setRealAmount(in.getAmount());
  318. return expenseAccountDetailsMapper.insertSelective(in);
  319. }
  320. @Override
  321. public int deleteDetails(Integer id) {
  322. return expenseAccountDetailsMapper.deleteByPrimaryKey(id);
  323. }
  324. @Override
  325. public List<OutExpenseAccountDetails> detailsList(Integer eaid) {
  326. return expenseAccountDetailsMapper.selectByEAid(eaid);
  327. }
  328. @Override
  329. public Object logList(Integer eaid) {
  330. List<OutExpenseAccountLog> list = expenseAccountLogMapper.selectByEaid(eaid);
  331. if (!list.isEmpty()){
  332. OutExpenseAccountLog lastout=list.get(list.size()-1);
  333. if (lastout.getStatus()!=3){
  334. List<OutExpenseAccountLog> eaelist = expenseAccountExamineMapper.selectNotExamineByEaidAndProcessStatus(eaid, lastout.getProcessStatus());
  335. list.addAll(eaelist);
  336. }
  337. }
  338. return list;
  339. }
  340. @Override
  341. public int updateExamine(InputExpenseAccount in) {
  342. ExpenseAccount useEa = expenseAccountMapper.selectByPrimaryKey(in.getId());
  343. ExpenseAccount newEa=new ExpenseAccount();
  344. newEa.setId(useEa.getId());
  345. //审核通知 获取发送人
  346. AdminListBo admin = adminMapper.getDeptNameByAid(useEa.getAid());
  347. addExamineExpenseAccountLog(admin,in, useEa, newEa);
  348. //完成申请
  349. if (newEa.getStatus()==2){
  350. //如果是借支则订单新增
  351. List<ExpenseAccount> expenseAccounts1 = expenseAccountMapper.selectListByMainId(in.getId());
  352. for (ExpenseAccount e : expenseAccounts1) {
  353. if (e.getType()!=4){
  354. //不然就看是否挂载借支订单进行抵扣
  355. if (e.getDebitId()!=null){
  356. ExpenseAccount useDebit=expenseAccountMapper.selectByPrimaryKey(e.getDebitId());
  357. if (useDebit.getLiquidationStatus()==2){
  358. List<ExpenseAccount> expenseAccounts = expenseAccountMapper.selectByDebitId(e.getDebitId());
  359. boolean flag=true;
  360. for (ExpenseAccount expenseAccount : expenseAccounts) {
  361. if(!expenseAccount.getId().equals(in.getId())&&expenseAccount.getStatus()!=2){
  362. flag=false;
  363. break;
  364. }
  365. }
  366. if (flag){
  367. ExpenseAccount debit=new ExpenseAccount();
  368. debit.setId(e.getDebitId());
  369. debit.setLiquidationStatus(3);
  370. expenseAccountMapper.updateByPrimaryKeySelective(debit);
  371. }
  372. }
  373. }
  374. }
  375. ExpenseAccount updateSon=new ExpenseAccount();
  376. updateSon.setId(e.getId());
  377. updateSon.setStatus(2);
  378. expenseAccountMapper.updateByPrimaryKeySelective(updateSon);
  379. }
  380. }else if (newEa.getStatus()==1){
  381. if (newEa.getProcessStatus()==EAProcessStatus.CWSH.getCode()){
  382. pushFinanceCount(admin.getDepFinance(),useEa.getCreateTime(),0);
  383. //处理金额修改
  384. if(in.getRealList()!=null){
  385. List<Map> maps = JSONObject.parseArray(in.getRealList(), Map.class);
  386. for (Map map: maps) {
  387. Integer type= (Integer) map.get("type");
  388. Integer id= (Integer) map.get("id");
  389. BigDecimal amount= new BigDecimal(map.get("amount").toString());
  390. //type=1总计,type=2报销金额
  391. if(type==1){
  392. ExpenseAccount ea =new ExpenseAccount();
  393. ea.setId(id);
  394. ea.setRealAmount(amount);
  395. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  396. }else if (type==2){
  397. ExpenseAccountDetails ead=new ExpenseAccountDetails();
  398. ead.setId(id);
  399. ead.setRealAmount(amount);
  400. expenseAccountDetailsMapper.updateByPrimaryKeySelective(ead);
  401. }
  402. }
  403. }
  404. }else if (newEa.getProcessStatus()==EAProcessStatus.ZJLSH.getCode()){
  405. pushFinanceCount(admin.getDepFinance(),useEa.getCreateTime(),1);
  406. }
  407. }else if (newEa.getStatus()==3){
  408. if (useEa.getProcessStatus()==EAProcessStatus.CWSH.getCode()){
  409. pushFinanceCount(admin.getDepFinance(),useEa.getCreateTime(),2);
  410. }else if (newEa.getProcessStatus()>EAProcessStatus.ZJLSH.getCode()){
  411. pushFinanceCount(admin.getDepFinance(),useEa.getCreateTime(),3);
  412. }
  413. }
  414. //审核处理审核表
  415. pushExpenseAccountExamine(in);
  416. expenseAccountMapper.updateByPrimaryKeySelective(newEa);
  417. return 1;
  418. }
  419. private void addFinanceCount(String aid,Date startTime ) {
  420. String forDate=DateUtils.formatDate(startTime,AFTConstants.YYYYMMDD);
  421. addFinanceCount(aid,forDate);
  422. }
  423. private void addFinanceCount(String aid,String startTime ) {
  424. FinanceCount financeCount = financeCountMapper.selectByAidAndDates(aid, startTime);
  425. if (financeCount ==null){
  426. financeCount =FinanceCount.initialization(aid, startTime);
  427. financeCount.setExpenseCount(1);
  428. financeCount.setExpenseUnauditedCount(1);
  429. financeCountMapper.insertSelective(financeCount);
  430. }else {
  431. FinanceCount newFinanceCount =new FinanceCount();
  432. newFinanceCount.setId(financeCount.getId());
  433. newFinanceCount.setExpenseCount(financeCount.getExpenseCount()+1);
  434. newFinanceCount.setExpenseUnauditedCount(financeCount.getExpenseUnauditedCount()+1);
  435. financeCountMapper.updateByPrimaryKeySelective(newFinanceCount);
  436. }
  437. }
  438. private void pushExpenseAccountExamine(InputExpenseAccount in) {
  439. if (in.getStatus()==1){
  440. expenseAccountExamineMapper.updateStatusByEaidAndAid(in.getId(),TokenManager.getAdminId(),1);
  441. }else if (in.getStatus()==3){
  442. expenseAccountExamineMapper.updateStatusByEaidAndAid(in.getId(),null,0);
  443. }
  444. }
  445. private void addExamineExpenseAccountLog(AdminListBo admin,InputExpenseAccount in, ExpenseAccount useEa, ExpenseAccount newEa) {
  446. List<Admin> aids=new ArrayList<>();
  447. StringBuffer str=new StringBuffer();
  448. Integer status=in.getStatus();
  449. if (status==1){
  450. str=str.append("您有报销需要审核,").append("报销编号[").append(in.getId()).append("]").append(",请查看并审核。");
  451. //新的流程是当前流程+1
  452. newEa.setProcessStatus(useEa.getProcessStatus()+1);
  453. newEa.setAid(useEa.getAid());
  454. newEa.setCreateTime(useEa.getCreateTime());
  455. pushExamineProcess(newEa,admin);
  456. if (newEa.getProcessStatus()== EAProcessStatus.CWSH.getCode()){
  457. //获取财务负责人
  458. Admin finaceAdmin = adminMapper.selectByPrimaryKey(admin.getDepFinance());
  459. aids.add(finaceAdmin);
  460. newEa.setExamineName(finaceAdmin.getName());
  461. }else if (newEa.getProcessStatus()== EAProcessStatus.ZJLSH.getCode()){
  462. //获取部门负责人
  463. List<Admin> list = adminMapper.listAdminBydepIdAndRoleType(admin.getDepartmentId(), AFTConstants.SALESMAN_ADMIN);
  464. if (!list.isEmpty())aids.addAll(list);
  465. newEa.setExamineName(getAdmins(list));
  466. }else if (newEa.getProcessStatus()== EAProcessStatus.DSZSH.getCode()){
  467. //获取总裁、董事长等审核
  468. List<Admin> list = adminMapper.selectAdminByRoleType(AFTConstants.APPROVAL_DECISION);
  469. if (!list.isEmpty())aids.addAll(list);
  470. newEa.setExamineName(getAdmins(list));
  471. }else if (newEa.getProcessStatus()== EAProcessStatus.WCSH.getCode()){
  472. //获取报销申请人
  473. status =2;
  474. Admin finaceAdmin = adminMapper.selectByPrimaryKey(admin.getDepFinance());
  475. aids.add(finaceAdmin);
  476. str.setLength(0);
  477. str=str.append("您的报销审核已通过,报销编号[").append(in.getId()).append("],请注意查看。");
  478. addNoticeAndEmail(in, status,str.toString(),admin.getName(),aids);
  479. aids.clear();
  480. aids.add(adminMapper.selectByPrimaryKey(admin.getId()));
  481. newEa.setStatus(status);
  482. newEa.setExamineName("");
  483. }
  484. }else {
  485. aids.add(adminMapper.selectByPrimaryKey(admin.getId()));
  486. newEa.setStatus(3);
  487. newEa.setProcessStatus(0);
  488. str.setLength(0);
  489. str=str.append("您的报销审核已驳回,报销编号[").append(in.getId()).append("],请注意查看。");
  490. }
  491. if (newEa.getStatus()==null)newEa.setStatus(1);
  492. addExpenseAccountLog(in.getId(),status,useEa.getProcessStatus(),TokenManager.getAdminId(),in.getReason());
  493. addNoticeAndEmail(in, status,str.toString(),admin.getName(),aids);
  494. }
  495. /**
  496. *
  497. * @param aid
  498. * @param createTime
  499. * @param type 0 双新增 1 未审核减一 2双减 3总数减一
  500. */
  501. private void pushFinanceCount(String aid, Date createTime, Integer type) {
  502. String startTime= DateUtils.formatDate(createTime,AFTConstants.YYYYMMDD);
  503. FinanceCount financeCount = financeCountMapper.selectByAidAndDates(aid, startTime);
  504. FinanceCount newFinance=new FinanceCount();
  505. if (financeCount==null){
  506. newFinance=FinanceCount.initialization(aid,createTime);
  507. newFinance.setExpenseCount(1);
  508. newFinance.setExpenseUnauditedCount(1);
  509. financeCountMapper.insertSelective(newFinance);
  510. }else {
  511. newFinance.setId(financeCount.getId());
  512. if(type==0){
  513. newFinance.setExpenseCount(financeCount.getExpenseCount()+1);
  514. newFinance.setExpenseUnauditedCount(financeCount.getExpenseUnauditedCount()+1);
  515. }else if(type==1){
  516. newFinance.setExpenseUnauditedCount(financeCount.getExpenseUnauditedCount()-1);
  517. }else if(type==2){
  518. newFinance.setExpenseCount(financeCount.getExpenseCount()-1);
  519. newFinance.setExpenseUnauditedCount(financeCount.getExpenseUnauditedCount()-1);
  520. }else if(type==3){
  521. newFinance.setExpenseCount(financeCount.getExpenseCount()-1);
  522. }
  523. financeCountMapper.updateByPrimaryKeySelective(newFinance);
  524. }
  525. }
  526. private void pushExamineProcess( ExpenseAccount newEa,AdminListBo admin) {
  527. //如果到了董事长审核,无需判断了
  528. if (newEa.getProcessStatus()<EAProcessStatus.WCSH.getCode()){
  529. //判断没一个流程是否可以跳过
  530. examineByprocess(newEa,admin);
  531. }
  532. }
  533. /**
  534. * 审核流程推送,只要不是完成状态就继续判断是否跳过
  535. * @param newEa
  536. */
  537. private void examineByprocess( ExpenseAccount newEa,AdminListBo admin) {
  538. //获取当前流程审核人,如果有已审核则跳过当前审核
  539. List<ExpenseAccountExamine> list = expenseAccountExamineMapper.selectByEaidAndProcessStatus(newEa.getId(), newEa.getProcessStatus());
  540. boolean flag=false;
  541. String aid="";
  542. for (ExpenseAccountExamine e : list) {
  543. if (e.getStatus()==1){
  544. flag=true;
  545. aid=e.getAuditor();
  546. break;
  547. }
  548. if(e.getAuditor().equals(newEa.getAid())){
  549. flag=true;
  550. break;
  551. }
  552. }
  553. if (flag){
  554. String str=String.format("重复审核人跳过[%s]。",EAProcessStatus.getDesc(newEa.getProcessStatus()));
  555. Date date=new Date();
  556. date.setTime(date.getTime()+1000l);
  557. addExpenseAccountLog(newEa.getId(),1,newEa.getProcessStatus(),aid,str,date);
  558. if (newEa.getProcessStatus()==EAProcessStatus.CWSH.getCode()){
  559. addFinanceCount(admin.getDepFinance(),newEa.getCreateTime());
  560. }
  561. newEa.setProcessStatus(newEa.getProcessStatus()+1);
  562. if (newEa.getProcessStatus()<EAProcessStatus.WCSH.getCode())examineByprocess(newEa,admin);
  563. }
  564. }
  565. private String getAdmins(List<Admin> list) {
  566. StringBuffer str =new StringBuffer();
  567. for (Admin admin : list) {
  568. str=str.append(admin.getName()).append(",");
  569. }
  570. return str.substring(0,str.length()-1);
  571. }
  572. private void addNoticeAndEmail(ExpenseAccount in, Integer status, String str,String aname, List<Admin> aids) {
  573. Date date= new Date();
  574. Integer noticeStatus=NoticeStatus.EXPENSE_NOTICE.getCode();
  575. if (status==2){
  576. noticeStatus=NoticeStatus.EXPENSE_COMPLETE.getCode();
  577. }
  578. List<Notice> list=new ArrayList<>();
  579. if (aids!=null){
  580. for (Admin a : aids) {
  581. Notice notice=new Notice();
  582. notice.setId(UUID.randomUUID().toString());
  583. notice.setNoticeType(noticeStatus);
  584. notice.setType(NoticeTypes.getType(noticeStatus));
  585. notice.setAid(a.getId());
  586. notice.setCreateTime(date);
  587. notice.setContent(str);
  588. notice.setReaded(0);
  589. list.add(notice);
  590. EmailBo emailBo=new EmailBo(NoticeStatus.getValueByCode(noticeStatus),a.getEmail(),str);
  591. asyncUtils.send(emailBo);
  592. weChatUtils.addExpenseAccountWeChatNotice(a.getExpenseOpenId(),status,in.getId(),date,aname,str);
  593. }
  594. asyncUtils.addNoticeBatch(list);
  595. }
  596. }
  597. @Override
  598. public Object pageList(InputPageListBo in) {
  599. Map<String ,Object> param=new HashMap<>();
  600. addParam(in, param);
  601. Pagination<?> pagePlus = findPagePlus("selectExpenseAccountList",
  602. "selectExpenseAccountCount",
  603. "selectExpenseAccountTotalAmount", param, in.getPageNo(), in.getPageSize());
  604. List<OutExpenseAccountBo> list = (List<OutExpenseAccountBo>) pagePlus.getList();
  605. for (OutExpenseAccountBo e : list) {
  606. e.setExamine(pushExpenseAccountGetExamine(e.getId(), e.getProcessStatus()));
  607. }
  608. return pagePlus;
  609. }
  610. @Override
  611. public Object selectByPrid(InputExpenseAccount in) {
  612. in.setAid(TokenManager.getAdminId());
  613. OutExpenseAccount useEa = expenseAccountMapper.selectByaidAndPrid(in);
  614. return useEa;
  615. }
  616. @Override
  617. public Object selectById(Integer id) {
  618. OutExpenseAccount useEa = expenseAccountMapper.selectByid(id);
  619. //判断当前角色是否审核
  620. pushExpenseAccountGetExamine(useEa);
  621. if (useEa.getDebitId()!=null){
  622. pushExpenseAccountGetDebitAmount(useEa);
  623. }
  624. return useEa;
  625. }
  626. @Override
  627. public Object selectByCheckNo(String checkNo) {
  628. OutExpenseAccount useEa = expenseAccountMapper.selectByCheckNo(checkNo);
  629. return useEa;
  630. }
  631. @Override
  632. public Object pushMerge(List<ExpenseAccount> list) {
  633. //设置付款部门和支付部门
  634. InputExpenseAccount in =new InputExpenseAccount();
  635. in.setCheckNo(idGenerator.getNOgenerateId());
  636. if (in.getAid()==null){
  637. in.setAid(TokenManager.getAdminId());
  638. }
  639. Admin admin = adminMapper.selectByPrimaryKey(in.getAid());
  640. in.setAname(admin.getName());
  641. in.setApplyDep(admin.getDepartmentId());
  642. in.setPayDep(admin.getDepartmentId());
  643. Integer eaaid = expenseAccountPrivateMapper.selectByaidDefault(TokenManager.getAdminId());
  644. in.setEaaid(eaaid);
  645. BigDecimal count = new BigDecimal(0);
  646. List<String> typeList = new ArrayList<String>();
  647. for (ExpenseAccount e : list) {
  648. count=count.add(e.getTotalAmount());
  649. StringBuffer types=new StringBuffer();
  650. if (e.getType()!=0){
  651. types=types.append(EATypes.getDescByCode(e.getType()));
  652. }else {
  653. types=types.append(e.getTypeOther());
  654. }
  655. if (e.getType()==2){
  656. if (e.getSecondaryType()==0){
  657. types=types.append("-").append(e.getSecondaryTypeOther());
  658. }else{
  659. String descByCode = EAsecondaryTypes.getDescByCode(e.getSecondaryType());
  660. types=types.append("-").append(descByCode);
  661. }
  662. }
  663. if (!typeList.contains(types.toString()))typeList.add(types.toString());
  664. }
  665. in.setTotalAmount(count);
  666. in.setRealAmount(count);
  667. in.setExpenseMain(1);
  668. in.setTypeOther(String.join(",",typeList));
  669. expenseAccountMapper.insertSelective(in);
  670. list.stream().forEach(e -> {
  671. ExpenseRelationship er=new ExpenseRelationship();
  672. er.setId(in.getId());
  673. er.setEaId(e.getId());
  674. expenseRelationshipMapper.insertSelective(er);
  675. });
  676. expenseAccountMapper.updateByIds(list,1);
  677. return in.getId();
  678. }
  679. @Override
  680. public MainExpenseAccount mainExpense(Integer id) {
  681. MainExpenseAccount res = expenseAccountMapper.selectByMainId(id);
  682. res.setExamine(0);
  683. if (TokenManager.hasRole(AFTConstants.SALESMAN_MANAGER)&&(res.getProcessStatus()==1)){
  684. res.setExamine(1);
  685. }else if (TokenManager.hasRole(AFTConstants.FINANCE)&&(res.getProcessStatus()==2)){
  686. res.setExamine(1);
  687. }else if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)&&(res.getProcessStatus()==3)){
  688. res.setExamine(1);
  689. }else if (TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)&&(res.getProcessStatus()==4)){
  690. res.setExamine(1);
  691. }
  692. if (res.getAid().equals(TokenManager.getAdminId())){
  693. res.setExamine(2);
  694. }
  695. return res;
  696. }
  697. @Override
  698. public Object updateMain(MainExpenseAccount in) {
  699. OutExpenseAccount outExpenseAccount = expenseAccountMapper.selectByid(in.getId());
  700. if (outExpenseAccount != null&&outExpenseAccount.getStatus()!=0&&outExpenseAccount.getStatus()!=3) {
  701. throw new BusinessException("主项目已发起,不可修改!");
  702. }
  703. ExpenseAccount update =new ExpenseAccount();
  704. if (in.getStatus()==null)in.setStatus(1);
  705. if (in.getStatus()==1){
  706. if (in.getOrderNo()!=null){
  707. update.setOrderNo(in.getOrderNo());
  708. update.setTargetType(1);
  709. }
  710. if (in.getPayDep()!=null)update.setPayDep(in.getPayDep());
  711. if (in.getApplyDep()!=null)update.setApplyDep(in.getApplyDep());
  712. if (in.getRemarks()!=null)update.setRemarks(in.getRemarks());
  713. if (in.getEaaid()!=null)update.setEaaid(in.getEaaid());
  714. addExpenseAccountExamine(in.getId(),update);
  715. if (outExpenseAccount.getStatus()==0){
  716. addExpenseAccountLogAndNoticEmail(outExpenseAccount,outExpenseAccount.getStatus(),"发起报销审核");
  717. }else if (outExpenseAccount.getStatus()==3){
  718. addExpenseAccountLogAndNoticEmail(outExpenseAccount,4,"修改并发起审核");
  719. }
  720. }else if (in.getStatus()==4){
  721. expenseAccountMapper.updateByMainId(in.getId(),0);
  722. addExpenseAccountLog(in.getId(),4,0,in.getAid(),"撤销报销审核");
  723. }
  724. update.setStatus(in.getStatus());
  725. update.setProcessStatus(outExpenseAccount.getProcessStatus());
  726. update.setId(in.getId());
  727. update.setExamineName(outExpenseAccount.getExamineName());
  728. return expenseAccountMapper.updateByPrimaryKeySelective(update);
  729. }
  730. @Override
  731. public Object mainList(ExpenseMainListInput in) {
  732. Map<String,Object> params = new HashMap<>();
  733. if (in.getStatus()!=null) params.put("status",in.getStatus());
  734. if (in.getCheckNo()!=null) params.put("checkNo",in.getCheckNo());
  735. if (in.getListStatus()!=null) params.put("listStatus",in.getListStatus());
  736. //1-3s是不同人的审核
  737. if (in.getProcessStatus()!=null&&in.getProcessStatus()==1){
  738. if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)){
  739. in.setProcessStatus(3);
  740. }else if (TokenManager.hasRole(AFTConstants.FINANCE)){
  741. in.setProcessStatus(2);
  742. }else if (TokenManager.hasRole(AFTConstants.SALESMAN_MANAGER)){
  743. in.setProcessStatus(1);
  744. }else if (TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION_ASSISTANT)){
  745. in.setProcessStatus(4);
  746. }else if(TokenManager.hasRole(AFTConstants.FINANCE_MANAGER)){
  747. //财务经理查看权限
  748. in.setProcessStatus(5);
  749. }else if(TokenManager.hasRole(AFTConstants.FINANCE_ADMIN)||TokenManager.hasRole(AFTConstants.SUPERADMIN)){
  750. //财务管理员和超级管理员查看全部
  751. in.setProcessStatus(6);
  752. }
  753. }else {
  754. in.setProcessStatus(0);
  755. }
  756. if (in.getProcessStatus()!=null){
  757. params.put("aid",TokenManager.getAdminId());
  758. params.put("roleType",in.getProcessStatus());
  759. if (in.getProcessStatus()==3){
  760. params.put("deps",departmentService.selectMyDeps());
  761. }
  762. }
  763. return findPage("ExpenseMainList","ExpenseMainCount",params,in.getPageNo(),in.getPageSize());
  764. }
  765. @Override
  766. public Object detailsList(InputDetailsListBo in) {
  767. Map<String,Object> params = new HashMap<>();
  768. if(in.getStatus()!=null)params.put("status",in.getStatus());
  769. if(in.getType()!=null)params.put("type",in.getType());
  770. if (in.getDepId()!=null)params.put("depId",in.getDepId());
  771. if (in.getContractNo()!=null)params.put("contractNo",in.getContractNo());
  772. if (in.getBuyerName()!=null)params.put("buyerName",in.getBuyerName());
  773. if (in.getAname()!=null)params.put("aname",in.getAname());
  774. if (in.getApplyDep()!=null)params.put("applyDep",in.getApplyDep());
  775. if (in.getPayDep() != null)params.put("payDep",in.getPayDep());
  776. if (in.getStartTime()!=null)params.put("startTime",in.getStartTime());
  777. if (in.getEndTime()!=null)params.put("endTime",in.getEndTime());
  778. if (in.getCheckNo()!=null)params.put("checkNo",in.getCheckNo());
  779. //0=自己 1=财务 2=财务经理 3=超管
  780. if (TokenManager.hasRole(AFTConstants.SUPERADMIN)){
  781. params.put("roleType",3);
  782. }else if (TokenManager.hasRole(AFTConstants.FINANCE_MANAGER)){
  783. params.put("roleType",2);
  784. params.put("aid",TokenManager.getAdminId());
  785. }else if (TokenManager.hasRole(AFTConstants.FINANCE)){
  786. params.put("roleType",1);
  787. params.put("aid",TokenManager.getAdminId());
  788. }else{
  789. params.put("roleType",0);
  790. }
  791. return findPagePlus("ExpenseDetailsList","ExpenseDetailsCount","ExpenseDetailsTotalAmount",params,in.getPageNo(),in.getPageSize());
  792. }
  793. @Override
  794. public List<ExpenseAccount> selectByIds(List<String> list) {
  795. return expenseAccountMapper.selectByIds(list);
  796. }
  797. @Override
  798. public int selectCountByIdsAndType(List<String> list, Integer i) {
  799. return expenseAccountMapper.selectCountByIdsAndType(list,i);
  800. }
  801. @Override
  802. public Object updateRealAmount(Integer type, Integer id, BigDecimal amount) {
  803. ExpenseAccountDetails ea= new ExpenseAccountDetails();
  804. ea.setId(id);
  805. ea.setRealAmount(amount);
  806. expenseAccountDetailsMapper.updateByPrimaryKeySelective(ea);
  807. expenseAccountMapper.updateSonByDetId(id);
  808. expenseAccountMapper.updateMainByDetId(id);
  809. return 1;
  810. }
  811. /**
  812. * 计算当前报销显示抵扣金额
  813. * @param useEa
  814. */
  815. private void pushExpenseAccountGetDebitAmount(OutExpenseAccount useEa) {
  816. ExpenseAccount debit = expenseAccountMapper.selectByPrimaryKey(useEa.getDebitId());
  817. List<ExpenseAccount> list1 = expenseAccountMapper.selectByDebitId(useEa.getDebitId());
  818. BigDecimal count =debit.getTotalAmount();
  819. for (ExpenseAccount e : list1) {
  820. if (!e.getId().equals(useEa.getId())){
  821. count=count.subtract(e.getSettlementAmount());
  822. }
  823. }
  824. if (count.compareTo(new BigDecimal(0))<0)count=new BigDecimal(0);
  825. useEa.setDebitTotalAmount(count);
  826. }
  827. /**
  828. * 判断角色是否已经审核
  829. * @param useEa
  830. */
  831. private void pushExpenseAccountGetExamine(OutExpenseAccount useEa) {
  832. List<ExpenseAccountExamine> list = expenseAccountExamineMapper.selectByEaidAndProcessStatus(useEa.getId(), useEa.getProcessStatus());
  833. boolean flag=false;
  834. for (ExpenseAccountExamine e : list) {
  835. if (e.getAuditor().equals(TokenManager.getAdminId())){
  836. if (e.getStatus()==0){
  837. flag=true;
  838. break;
  839. }
  840. }
  841. }
  842. if (flag){
  843. useEa.setExamine(1);
  844. }else {
  845. useEa.setExamine(0);
  846. }
  847. }
  848. /**
  849. * 判断角色是否已经审核
  850. * @param
  851. */
  852. private Integer pushExpenseAccountGetExamine(Integer id,Integer processStatus) {
  853. List<ExpenseAccountExamine> list = expenseAccountExamineMapper.selectByEaidAndProcessStatus(id, processStatus);
  854. boolean flag=false;
  855. for (ExpenseAccountExamine e : list) {
  856. if (e.getAuditor().equals(TokenManager.getAdminId())){
  857. if (e.getStatus()==0){
  858. flag=true;
  859. break;
  860. }
  861. }
  862. }
  863. if (flag){
  864. return 1;
  865. }else {
  866. return 0;
  867. }
  868. }
  869. @Override
  870. public Object updateType(InputExpenseAccount in) {
  871. expenseAccountDetailsMapper.deleteByeaid(in.getId());
  872. if (in.getType()!=2)in.setSecondaryType(0);
  873. return expenseAccountMapper.updateByPrimaryKeySelective(in);
  874. }
  875. @Override
  876. public List<OutExpenseAccount> selectDebitOrder(String depId,Integer id) {
  877. String aid= TokenManager.getAdminId();
  878. List<OutExpenseAccount> list = expenseAccountMapper.selectByaidAndType(aid, depId, 4,id);
  879. return list;
  880. }
  881. private void addParam(InputPageListBo in, Map<String, Object> param) {
  882. if (in.getStartTime()!=null&&in.getEndTime()!=null){
  883. param.put("startTime",in.getStartTime());
  884. param.put("endTime",in.getEndTime()+" 23:59:59");
  885. }
  886. if (in.getStatus()!=null)param.put("status",in.getStatus());
  887. //如果前端未传 0自己 1审核的参数,默认自己,在审核列表的时候判断Aid,在
  888. if (in.getProcessStatus()==null) in.setProcessStatus(0);
  889. if(in.getProcessStatus()==2){
  890. if(TokenManager.hasRole(AFTConstants.FINANCE_MANAGER)){
  891. //财务经理查看权限
  892. in.setProcessStatus(5);
  893. }else if(TokenManager.hasRole(AFTConstants.FINANCE_ADMIN)||TokenManager.hasRole(AFTConstants.SUPERADMIN)){
  894. //财务管理员和超级管理员查看全部
  895. in.setProcessStatus(6);
  896. }
  897. if (in.getAid()!=null){
  898. param.put("aid",in.getAid());
  899. }
  900. param.put("auditor",TokenManager.getAdminId());
  901. }else if(in.getProcessStatus()==1) {
  902. param.put("auditor",TokenManager.getAdminId());
  903. if (in.getAid()!=null){
  904. param.put("aid",in.getAid());
  905. }
  906. }else {
  907. param.put("aid",TokenManager.getAdminId());
  908. }
  909. param.put("processStatus",in.getProcessStatus());
  910. if (in.getExamineStatus()!=null){
  911. param.put("examineStatus",in.getExamineStatus());
  912. }else {
  913. param.put("examineStatus",0);
  914. }
  915. if (in.getContractNo()!=null)param.put("contractNo",in.getContractNo());
  916. if (in.getDepId()!=null)param.put("depId",in.getDepId());
  917. if (in.getApplyDep()!=null)param.put("applyDep",in.getApplyDep());
  918. if (in.getPayDep()!=null)param.put("payDep",in.getPayDep());
  919. if (in.getType()!=null)param.put("type",in.getType());
  920. if (in.getUsername()!=null)param.put("username",in.getUsername());
  921. if (in.getCheckNo()!=null)param.put("checkNo",in.getCheckNo());
  922. if (in.getSecondaryType()!=null)param.put("secondaryType",in.getSecondaryType());
  923. //固定传0查看子列表
  924. if (in.getExpenseMain()==null)in.setExpenseMain(0);
  925. if (in.getExpenseMain()!=null)param.put("expenseMain",in.getExpenseMain());
  926. }
  927. /**
  928. * 检查权限判断是否是总裁、董事长或超级管理员权限
  929. * @return
  930. */
  931. private boolean checkShiroCED() {
  932. if (TokenManager.hasRole(AFTConstants.CED)||TokenManager.hasRole(AFTConstants.CED_ASSISTANT)||
  933. TokenManager.hasRole(AFTConstants.SUPERADMIN)||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION_ASSISTANT)
  934. ||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)){
  935. return true;
  936. }
  937. return false;
  938. }
  939. @Override
  940. @Cacheable(value = "expenseAccountStatistics#300", key = "'page:'+#name+',depId'+#depId+',startTime'+#startTime+',endTime'+#endTime")
  941. public List<OutExpenseAccountStatistics> statistics(String name,String depId,String startTime,String endTime) {
  942. if (endTime!=null)endTime=endTime+" 23:59:59";
  943. List<OutMyEAStatistics> list = expenseAccountMapper.selectStatistics(name, depId, startTime, endTime);
  944. List<OutExpenseAccountStatistics> results=new ArrayList<>();
  945. Map<String ,Object> map=new HashMap<>();
  946. for (OutMyEAStatistics e : list) {
  947. OutExpenseAccountStatistics out = new OutExpenseAccountStatistics(e.getAid());
  948. out.setAname(e.getAname());
  949. if (e.getStatus() == 2) {
  950. if (e.getType() < 4) {
  951. out.setBx(1);
  952. out.setBxAmount(e.getTotalAmount());
  953. if (!e.getDebitId().equals(0)) {
  954. out.setDk(1);
  955. out.setDkAmount(e.getBxdkAmount());
  956. }
  957. } else if (e.getType() == 4) {
  958. out.setJz(1);
  959. out.setJzAmount(e.getTotalAmount());
  960. } else if (e.getType() == 5) {
  961. out.setDk(1);
  962. out.setDkAmount(e.getTotalAmount());
  963. }
  964. if (e.getTargetType() == 0) {
  965. out.setGdfyAmount(e.getTotalAmount());
  966. } else if (e.getTargetType() == 1) {
  967. out.setBxddAmount(e.getTotalAmount());
  968. }
  969. } else if (e.getStatus() < 2) {
  970. out.setWshs(1);
  971. } else if (e.getStatus() == 3) {
  972. out.setBhs(1);
  973. }
  974. //如果不存在就新增
  975. if (!map.keySet().contains(out.getAid())){
  976. List<OutExpenseAccountStatistics> list2=new ArrayList<>();
  977. list2.add(out);
  978. map.put(out.getAid(),list2);
  979. }else {
  980. List<OutExpenseAccountStatistics> list2 = (List<OutExpenseAccountStatistics>) map.get(e.getAid());
  981. list2.add(out);
  982. }
  983. }
  984. for (String s : map.keySet()) {
  985. List<OutExpenseAccountStatistics> list3 = (List<OutExpenseAccountStatistics>) map.get(s);
  986. if (!list3.isEmpty()){
  987. OutExpenseAccountStatistics out =new OutExpenseAccountStatistics(s);
  988. for (OutExpenseAccountStatistics e : list3) {
  989. if (out.getAname()==null)out.setAname(e.getAname());
  990. if (e.getBx()!=null)out.setBx(out.getBx()+e.getBx());
  991. if (e.getJz()!=null)out.setJz(out.getJz()+e.getJz());
  992. if (e.getDk()!=null)out.setDk(out.getDk()+e.getDk());
  993. if (e.getBxAmount()!=null)out.setBxAmount(out.getBxAmount().add(e.getBxAmount()));
  994. if (e.getJzAmount()!=null)out.setJzAmount(out.getJzAmount().add(e.getJzAmount()));
  995. if (e.getDkAmount()!=null)out.setDkAmount(out.getDkAmount().add(e.getDkAmount()));
  996. if (e.getGdfyAmount()!=null)out.setGdfyAmount(out.getGdfyAmount().add(e.getGdfyAmount()));
  997. if (e.getBxddAmount()!=null)out.setBxddAmount(out.getBxddAmount().add(e.getBxddAmount()));
  998. if (e.getWshs()!=null)out.setWshs(out.getWshs()+e.getWshs());
  999. if (e.getBhs()!=null)out.setBhs(out.getBhs()+e.getBhs());
  1000. }
  1001. BigDecimal totalAmount=new BigDecimal(0);
  1002. totalAmount=totalAmount.add(out.getBxAmount()).add(out.getJzAmount()).subtract(out.getDkAmount());
  1003. out.setTotalAmount(totalAmount);
  1004. results.add(out);
  1005. }
  1006. }
  1007. return results;
  1008. }
  1009. @Override
  1010. @CacheEvict(value = "expenseAccountStatistics#300", allEntries = true)
  1011. public void statisticsEvict(String name, String depId, String startTime, String endTime) {
  1012. LoggerUtils.debug(getClass(), "报销统计缓存清除。");
  1013. }
  1014. @Override
  1015. public Object pushSupplementCheckNo() {
  1016. List<ExpenseAccount> list = expenseAccountMapper.selectByCheckNoNot();
  1017. if (!list.isEmpty()){
  1018. list.forEach(e ->{
  1019. ExpenseAccount ea=new ExpenseAccount();
  1020. ea.setId(e.getId());
  1021. ea.setCheckNo(idGenerator.getNOgenerateId());
  1022. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  1023. });
  1024. return 1;
  1025. }
  1026. return 0;
  1027. }
  1028. @Override
  1029. public void pushAll() {
  1030. List<ExpenseAccount> list = expenseAccountMapper.selectAllMain();
  1031. for (ExpenseAccount ea : list) {
  1032. List<ExpenseAccount> expenseAccounts = expenseAccountMapper.selectListByMainId(ea.getId());
  1033. List<String> typeList=new ArrayList<>();
  1034. for (ExpenseAccount e : expenseAccounts) {
  1035. StringBuffer types=new StringBuffer();
  1036. if (e.getType()!=0){
  1037. types=types.append(EATypes.getDescByCode(e.getType()));
  1038. }else {
  1039. types=types.append(e.getTypeOther());
  1040. }
  1041. if (e.getType()==2){
  1042. if (e.getSecondaryType()==0){
  1043. types=types.append("-").append(e.getSecondaryTypeOther());
  1044. }else{
  1045. String descByCode = EAsecondaryTypes.getDescByCode(e.getSecondaryType());
  1046. types=types.append("-").append(descByCode);
  1047. }
  1048. }
  1049. if (!typeList.contains(types.toString()))typeList.add(types.toString());
  1050. }
  1051. String typeStr = String.join(",", typeList);
  1052. ExpenseAccount update=new ExpenseAccount();
  1053. update.setId(ea.getId());
  1054. update.setTypeOther(typeStr);
  1055. expenseAccountMapper.updateByPrimaryKeySelective(update);
  1056. }
  1057. }
  1058. }