ExpenseAccountServiceImpl.java 52 KB

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