ExpenseAccountServiceImpl.java 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  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.bo.AdminPublicReviewerBo;
  5. import com.goafanti.admin.service.DepartmentService;
  6. import com.goafanti.common.bo.AdminNoticeBo;
  7. import com.goafanti.common.bo.EmailBo;
  8. import com.goafanti.common.constant.AFTConstants;
  9. import com.goafanti.common.dao.*;
  10. import com.goafanti.common.enums.NoticeStatus;
  11. import com.goafanti.common.enums.NoticeTypes;
  12. import com.goafanti.common.error.BusinessException;
  13. import com.goafanti.common.model.*;
  14. import com.goafanti.common.utils.AsyncUtils;
  15. import com.goafanti.common.utils.DateUtils;
  16. import com.goafanti.common.utils.LoggerUtils;
  17. import com.goafanti.common.utils.StringUtils;
  18. import com.goafanti.common.utils.excel.FileUtils;
  19. import com.goafanti.common.utils.excel.NewExcelUtil;
  20. import com.goafanti.common.utils.pdf.PDFUtils;
  21. import com.goafanti.common.utils.weChat.WeChatUtils;
  22. import com.goafanti.core.mybatis.BaseMybatisDao;
  23. import com.goafanti.core.mybatis.JDBCIdGenerator;
  24. import com.goafanti.core.mybatis.page.Pagination;
  25. import com.goafanti.core.shiro.token.TokenManager;
  26. import com.goafanti.expenseAccount.Enums.EAProcessStatus;
  27. import com.goafanti.expenseAccount.Enums.EATypes;
  28. import com.goafanti.expenseAccount.Enums.EAsecondaryTypes;
  29. import com.goafanti.expenseAccount.bo.*;
  30. import com.goafanti.expenseAccount.service.ExpenseAccountService;
  31. import com.goafanti.order.bo.TOrderNewBo;
  32. import com.goafanti.order.service.OrderNewService;
  33. import com.goafanti.organization.bo.OrganizationListOut;
  34. import com.itextpdf.text.Document;
  35. import com.itextpdf.text.DocumentException;
  36. import com.itextpdf.text.Paragraph;
  37. import com.itextpdf.text.Phrase;
  38. import com.itextpdf.text.pdf.PdfPCell;
  39. import com.itextpdf.text.pdf.PdfPRow;
  40. import com.itextpdf.text.pdf.PdfPTable;
  41. import com.itextpdf.text.pdf.PdfWriter;
  42. import org.springframework.beans.factory.annotation.Value;
  43. import org.springframework.cache.annotation.CacheEvict;
  44. import org.springframework.cache.annotation.Cacheable;
  45. import org.springframework.http.MediaType;
  46. import org.springframework.stereotype.Service;
  47. import org.springframework.transaction.annotation.Transactional;
  48. import javax.annotation.Resource;
  49. import javax.servlet.http.HttpServletResponse;
  50. import java.io.FileOutputStream;
  51. import java.io.IOException;
  52. import java.math.BigDecimal;
  53. import java.time.LocalDate;
  54. import java.util.*;
  55. import java.util.stream.Collectors;
  56. @Service
  57. public class ExpenseAccountServiceImpl extends BaseMybatisDao<ExpenseAccountMapper> implements ExpenseAccountService {
  58. @Resource
  59. private AdminMapper adminMapper;
  60. @Resource
  61. private ExpenseAccountMapper expenseAccountMapper;
  62. @Resource
  63. private PublicReleaseMapper publicReleaseMapper;
  64. @Resource
  65. private ExpenseAccountLogMapper expenseAccountLogMapper;
  66. @Resource
  67. private AsyncUtils asyncUtils;
  68. @Resource
  69. private WeChatUtils weChatUtils;
  70. @Resource
  71. private ExpenseAccountDetailsMapper expenseAccountDetailsMapper;
  72. @Resource
  73. private ExpenseAccountPrivateMapper expenseAccountPrivateMapper;
  74. @Resource
  75. private ExpenseAccountExamineMapper expenseAccountExamineMapper;
  76. @Resource
  77. private JDBCIdGenerator idGenerator;
  78. @Resource
  79. private FinanceCountMapper financeCountMapper;
  80. @Resource
  81. private ExpenseRelationshipMapper expenseRelationshipMapper;
  82. @Resource
  83. private DepartmentService departmentService;
  84. @Resource
  85. private DepartmentMapper departmentMapper;
  86. @Resource
  87. private ExpenseConfigDetailsMapper expenseConfigDetailsMapper;
  88. @Resource
  89. private TOrderPublicReleaseCountMapper tOrderPublicReleaseCountMapper;
  90. @Resource
  91. private OrderNewService orderNewService;
  92. @Resource
  93. private OrderYearMaxDurationMapper orderYearMaxDurationMapper;
  94. @Resource
  95. private UserMapper userMapper;
  96. @Resource
  97. private TOrderMidMapper tOrderMidMapper;
  98. @Resource
  99. private AdminPublicReviewerMapper adminPublicReviewerMapper;
  100. @Value(value = "${upload.path}")
  101. private final String uploadPath = null;
  102. @Override
  103. public Integer add(InputExpenseAccount in) {
  104. //如果关联公出,获取公出信息
  105. if (in.getPrid()!=null){
  106. PublicRelease pr = publicReleaseMapper.selectById(in.getPrid());
  107. in.setReleaseStart(pr.getReleaseStart());
  108. in.setReleaseEnd(pr.getReleaseEnd());
  109. in.setDistrictName(pr.getDistrictName());
  110. in.setUserNames(pr.getUserNames());
  111. in.setDuration(pr.getDuration());
  112. if (in.getOrderNo()==null)in.setOrderNo(pr.getOrderNo());
  113. }
  114. //判断报销类型,0=固定费用,1=报销到订单
  115. if (in.getOrderNo()==null){
  116. in.setTargetType(0);
  117. }else {
  118. in.setTargetType(1);
  119. }
  120. if (in.getAid()==null)in.setAid(TokenManager.getAdminId());
  121. Admin admin = adminMapper.selectById(in.getAid());
  122. in.setAname(admin.getName());
  123. in.setApplyDep(admin.getDepartmentId());
  124. in.setPayDep(admin.getDepartmentId());
  125. expenseAccountMapper.insertSelective(in);
  126. return in.getId();
  127. }
  128. /**
  129. * 新增报销审核流程
  130. */
  131. private void addExpenseAccountExamine(Integer id,ExpenseAccount ea) {
  132. List<ExpenseAccountExamine> list=new ArrayList<>();
  133. expenseAccountExamineMapper.deleteByEaid(ea.getId());
  134. String aid = TokenManager.getAdminId();
  135. OrganizationListOut dep = departmentMapper.selectAllById(ea.getApplyDep());
  136. AdminNoticeBo a = adminMapper.selectMyAndSuperBySuperId(aid);
  137. //暂时只发起到财务
  138. for (int i=1;i<4;i++){
  139. ExpenseAccountExamine eae=new ExpenseAccountExamine(id,i);
  140. if (i==1){
  141. if (a.getExpenseSuperExamine()==1){
  142. //新增一级
  143. String examineId=null;
  144. String examineName=null;
  145. List<AdminPublicReviewerBo> exList = adminPublicReviewerMapper.selectByAid(aid).stream().filter(e -> e.getType() == 2).collect(Collectors.toList());
  146. if(a.getExpenseSuperExamine()==1) {
  147. eae.setStatus(0);
  148. } else {
  149. eae.setStatus(1);
  150. }
  151. if (!exList.isEmpty()){
  152. StringBuilder sb = new StringBuilder();
  153. for (AdminPublicReviewerBo e : exList) {
  154. ExpenseAccountExamine eaet=new ExpenseAccountExamine(id,i);
  155. examineId=e.getReviewerId();
  156. examineName=e.getReviewerName();
  157. eaet.setAuditor(examineId);
  158. eaet.setAuditorname(examineName);
  159. if (eaet.getAuditor().equals(aid))eaet.setStatus(1);
  160. else eaet.setStatus(0);
  161. list.add(eaet);
  162. sb.append(examineName).append(",");
  163. }
  164. ea.setExamineName(sb.substring(0,sb.length()-1));
  165. }else {
  166. examineId=a.getId();
  167. examineName=a.getName();
  168. eae.setAuditor(examineId);
  169. eae.setAuditorname(examineName);
  170. ea.setExamineName(examineName);
  171. }
  172. }
  173. }else if (i==2){
  174. if (StringUtils.isBlank(dep.getManagerName())){
  175. throw new BusinessException("报销申请部门[管理员]不存在。");
  176. }
  177. eae.setAuditor(dep.getManagerId());
  178. eae.setAuditorname(dep.getManagerName());
  179. }else {
  180. if (StringUtils.isBlank(dep.getExpenseFinanceName()))throw new BusinessException("报销部门财务审核人不存在。");
  181. eae.setAuditor(dep.getExpenseFinanceId());
  182. eae.setAuditorname(dep.getExpenseFinanceName());
  183. }
  184. if (StringUtils.isNotEmpty(eae.getAuditor())){
  185. if (eae.getAuditor().equals(aid))eae.setStatus(1);
  186. else eae.setStatus(0);
  187. list.add(eae);
  188. }
  189. }
  190. expenseAccountExamineMapper.insertBatch(list);
  191. }
  192. /**
  193. * 新增报销日志
  194. * @param eaid 报销编号
  195. * @param status 状态 0发起 1通过 2完成 3驳回 4修改
  196. * @param str 备注
  197. */
  198. private void addExpenseAccountLog(Integer eaid, Integer status,Integer processStatus,String aid, String str) {
  199. addExpenseAccountLog(eaid,status,processStatus,aid,str,null);
  200. }
  201. private void addExpenseAccountLog(Integer eaid, Integer status,Integer processStatus,String aid, String str,Date date){
  202. addExpenseAccountLog(eaid,status,processStatus,aid,str,date,null);
  203. }
  204. /**
  205. * 新增报销日志
  206. * @param eaid 报销编号
  207. * @param status 状态 0发起 1通过 2完成 3驳回 4修改
  208. * @param str 备注
  209. */
  210. private void addExpenseAccountLog(Integer eaid, Integer status,Integer processStatus,String aid, String str,Date date,Integer showStatus) {
  211. ExpenseAccountLog eaLog=new ExpenseAccountLog();
  212. eaLog.setEaid(eaid);
  213. eaLog.setStatus(status);
  214. eaLog.setRemarks(str);
  215. eaLog.setProcessStatus(processStatus);
  216. eaLog.setAuditor(aid);
  217. if (showStatus!=null){
  218. eaLog.setShowStatus(showStatus);
  219. }else {
  220. eaLog.setShowStatus(0);
  221. }
  222. if (date !=null)eaLog.setCreateTime(date);
  223. expenseAccountLogMapper.insertSelective(eaLog);
  224. }
  225. /**
  226. * 发起、驳回发起流程切换和日志站内信发送
  227. */
  228. private void addExpenseAccountLogAndNoticEmail(ExpenseAccount in, Integer status, String str) {
  229. String aid=TokenManager.getAdminId();
  230. addExpenseAccountLog(in.getId(),status,in.getProcessStatus(),aid,str);
  231. if (status==0)in.setProcessStatus(1);
  232. Admin admin = adminMapper.selectById(aid);
  233. if (admin.getExpenseSuperExamine()==0){
  234. String str2=String.format("未设置[%s]审核。",EAProcessStatus.getDesc(in.getProcessStatus()));
  235. Date date=new Date();
  236. date.setTime(date.getTime()+1000L);
  237. addExpenseAccountLog(in.getId(),1,in.getProcessStatus(),aid,str2,date,2);
  238. in.setProcessStatus(in.getProcessStatus()+1);
  239. }else {
  240. if (admin.getSuperiorId()==null||admin.getId().equals(admin.getSuperiorId())){
  241. String str2=String.format("%s跳过[%s]。",admin.getSuperiorId()==null?"发起人无上级":"发起人与上级同人",EAProcessStatus.getDesc(in.getProcessStatus()));
  242. Date date=new Date();
  243. date.setTime(date.getTime()+1000L);
  244. addExpenseAccountLog(in.getId(),1,in.getProcessStatus(),aid,str2,date,2);
  245. in.setProcessStatus(in.getProcessStatus()+1);
  246. }
  247. }
  248. createExamineByProcess(in);
  249. }
  250. /**
  251. * 判断审核流程与发送日志与邮件
  252. */
  253. private void createExamineByProcess(ExpenseAccount in) {
  254. //审核流程推送到最新
  255. AdminListBo admin = adminMapper.getDeptNameByAid(in.getAid());
  256. OrganizationListOut dep = departmentMapper.selectAllById(in.getApplyDep());
  257. List<ExpenseConfigDetails> configDetailsList = expenseConfigDetailsMapper.selectByDepId(in.getApplyDep());
  258. if (in.getApproval()==null)in.setApproval(0);
  259. examineByprocess(in,admin,dep,configDetailsList);
  260. List<ExpenseAccountExamine> list=expenseAccountExamineMapper.selectByEaidAndProcessStatus(in.getId(), in.getProcessStatus());
  261. List<Admin> aids=new ArrayList<>();
  262. StringBuilder aname =new StringBuilder();
  263. for (ExpenseAccountExamine e : list) {
  264. if (Objects.equals(e.getProcessStatus(), in.getProcessStatus()) &&e.getStatus()==0){
  265. Admin ad = adminMapper.selectById(e.getAuditor());
  266. aids.add(ad);
  267. aname.append(ad.getName()).append(",");
  268. }
  269. }
  270. in.setExamineName(aname.substring(0,aname.length()-1));
  271. String str = "编号[" + in.getCheckNo() + "]需要审核" + ",请查看并审核。";
  272. addNoticeAndEmail(in, EAProcessStatus.SJSH.getCode(), str, admin.getName(),aids);
  273. }
  274. @Override
  275. @Transactional
  276. public int update(InputExpenseAccount in) {
  277. OutExpenseAccount useEA = expenseAccountMapper.selectByid(in.getId());
  278. if (useEA.getStatus()!=0&&useEA.getStatus()!=3){
  279. throw new BusinessException("不能操作审核中与完成的订单");
  280. }
  281. if (in.getStartTimeStr()!=null&&in.getEndTimeStr()!=null){
  282. in.setReleaseStart(DateUtils.StringToDate(in.getStartTimeStr(), AFTConstants.YYYYMMDDHHMMSS));
  283. in.setReleaseEnd(DateUtils.StringToDate(in.getEndTimeStr(), AFTConstants.YYYYMMDDHHMMSS));
  284. }
  285. //计算总金额
  286. if (in.getTotalAmount()==null){
  287. pushExpenseAccountAmount(in);
  288. }
  289. in.setProcessStatus(0);
  290. if (in.getOrderNo()==null){
  291. in.setTargetType(0);
  292. }else {
  293. in.setTargetType(1);
  294. }
  295. //计算本单已扣金额 如果有选择借支单,则需要修改借支状态
  296. if (in.getDebitId()!=null){
  297. //如果以前没有选择借支,修改报销和借支数据
  298. if (useEA.getDebitId()==null){
  299. pushExpenseAccountDebit(in);
  300. }else if (!Objects.equals(useEA.getDebitId(), in.getDebitId())) {
  301. //如果是修改编号
  302. pushExpenseAccountDebit(in);
  303. //并且要处理原编号的问题
  304. pushResettingDebit(in,useEA.getDebitId());
  305. }
  306. }
  307. if (in.getSettlementAmount()==null)in.setSettlementAmount(useEA.getSettlementAmount());
  308. in.setStatus(0);
  309. return expenseAccountMapper.updateByPrimaryKeySelective(in);
  310. }
  311. private void pushExpenseAccountAmount(ExpenseAccount in) {
  312. OutExpenseAccount useEA = expenseAccountMapper.selectByid(in.getId());
  313. List<OutExpenseAccountDetails> list = expenseAccountDetailsMapper.selectByEAid(in.getId());
  314. BigDecimal totalAmount = BigDecimal.ZERO;
  315. BigDecimal realAmount = BigDecimal.ZERO;
  316. for (OutExpenseAccountDetails e : list) {
  317. totalAmount=totalAmount.add(e.getAmount());
  318. realAmount=realAmount.add(e.getRealAmount());
  319. }
  320. in.setTotalAmount(totalAmount);
  321. if (useEA==null||useEA.getSettlementAmount()==null){
  322. in.setAmount(realAmount);
  323. }else {
  324. in.setAmount(realAmount.subtract(useEA.getSettlementAmount()));
  325. }
  326. in.setRealAmount(in.getAmount());
  327. }
  328. @Override
  329. public Object updateDebitId(InputExpenseAccount in) {
  330. OutExpenseAccount useEA = expenseAccountMapper.selectByid(in.getId());
  331. if (useEA.getDebitId()!=null)pushResettingDebit(in,useEA.getDebitId());
  332. expenseAccountMapper.updateDeleteDebitId(in.getId());
  333. return 1;
  334. }
  335. @Override
  336. public Object delete(String ids) {
  337. String[] split = ids.split(",");
  338. for (String s : split) {
  339. Integer id=Integer.valueOf(s);
  340. ExpenseAccount expenseAccount = expenseAccountMapper.selectByPrimaryKey(id);
  341. if (expenseAccount.getStatus()!=0&&expenseAccount.getStatus()!=3){
  342. throw new BusinessException("该费用单状态无法删除");
  343. }
  344. List<OutExpenseAccountLog> logs = expenseAccountLogMapper.selectByEaid(id);
  345. if (logs.isEmpty()){
  346. expenseAccountMapper.deleteByPrimaryKey(id);
  347. }else {
  348. ExpenseAccount update =new ExpenseAccount();
  349. update.setId(id);
  350. update.setStatus(4);
  351. expenseAccountMapper.updateByPrimaryKeySelective(update);
  352. }
  353. }
  354. return 1;
  355. }
  356. @Override
  357. public Object pageListExport(InputPageListBo in) {
  358. List<OutExpenseAccountBo> list = (List<OutExpenseAccountBo>) pageList(in).getList();
  359. for (OutExpenseAccountBo e : list) {
  360. if (e.getType()!=0){
  361. e.setTypeOther(EATypes.getDescByCode(e.getType()));
  362. }else{
  363. e.setTypeOther(String.format("其他(%s)",e.getTypeOther()));
  364. }
  365. }
  366. NewExcelUtil excelUtil=new NewExcelUtil(OutExpenseAccountBo.class);
  367. return excelUtil.exportExcel(list,"费用单列表",uploadPath);
  368. }
  369. @Override
  370. public Object detailsListExport(InputDetailsListBo in) {
  371. List<OutExpenseAccountDetailsList> list = (List<OutExpenseAccountDetailsList>) detailsList(in).getList();
  372. for (OutExpenseAccountDetailsList e : list) {
  373. if (e.getTargetType()!=null){
  374. if (e.getTargetType()==0){
  375. e.setTargetName("固定费用");
  376. }else {
  377. e.setTargetName(e.getContractNo()+"/"+e.getBuyerName());
  378. }
  379. }
  380. if (e.getType()!=0){
  381. e.setTypeOther(EAsecondaryTypes.getDescByCode(e.getType()));
  382. }else{
  383. //如果是其他,又说明显示说明,没有说明显示费用类型
  384. if (StringUtils.isBlank(e.getTypeOther())){
  385. if (e.getSonType()!=0){
  386. e.setTypeOther(EATypes.getDescByCode(e.getSonType()));
  387. }else{
  388. e.setTypeOther(String.format("其他(%s)",e.getSonTypeOther()));
  389. }
  390. }else {
  391. e.setTypeOther(String.format("其他(%s)",e.getTypeOther()));
  392. }
  393. }
  394. if(e.getReleaseStart()!=null&&e.getReleaseEnd()!=null){
  395. e.setReleaseTimeStr(DateUtils.formatDate(e.getReleaseStart(),AFTConstants.YYYYMMDDHHMM)+"~"+DateUtils.formatDate(e.getReleaseEnd(),AFTConstants.YYYYMMDDHHMM));
  396. }
  397. }
  398. NewExcelUtil excelUtil=new NewExcelUtil(OutExpenseAccountDetailsList.class);
  399. return excelUtil.exportExcel(list,"费用详细列表",uploadPath);
  400. }
  401. @Override
  402. public Object getDepDetails(Integer id) {
  403. OutExpenseAccount outExpenseAccount = expenseAccountMapper.selectByid(id);
  404. return departmentMapper.selectAllById(outExpenseAccount.getApplyDep());
  405. }
  406. @Override
  407. public Object updateTypeOther(Integer id, String typeOther,String remarks,String orderNo) {
  408. ExpenseAccount ea = new ExpenseAccount();
  409. ea.setId(id);
  410. if (StringUtils.isNotEmpty(typeOther)){
  411. ea.setTypeOther(typeOther);
  412. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  413. }
  414. if (StringUtils.isNotEmpty(remarks)){
  415. ea.setRemarks(remarks);
  416. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  417. }
  418. if (StringUtils.isBlank(orderNo)){
  419. ea.setTargetType(0);
  420. expenseAccountMapper.updateRemoveOrderNo(ea);
  421. }else {
  422. ea.setOrderNo(orderNo);
  423. ea.setTargetType(1);
  424. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  425. }
  426. if (StringUtils.isNotEmpty(typeOther)){
  427. MainExpenseAccount mainExpenseAccount = expenseAccountMapper.selectbySonId(id);
  428. StringBuilder types=new StringBuilder();
  429. for (SonExpenseAccount e : mainExpenseAccount.getSonList()) {
  430. if (e.getType()!=0){
  431. types.append(EATypes.getDescByCode(e.getType()));
  432. }else {
  433. types.append(e.getTypeOther());
  434. }
  435. if (e.getType()==2){
  436. if (e.getSecondaryType()==0){
  437. types.append("-").append(e.getSecondaryTypeOther());
  438. }else{
  439. String descByCode = EAsecondaryTypes.getDescByCode(e.getSecondaryType());
  440. types.append("-").append(descByCode);
  441. }
  442. }
  443. types.append(",");
  444. }
  445. expenseAccountMapper.updateSonTypeOther(id,types.substring(0,types.length()-1));
  446. }
  447. return 1;
  448. }
  449. @Override
  450. public Object updateDetailsTypeOther(Integer id, String typeOther) {
  451. ExpenseAccountDetails in =new ExpenseAccountDetails();
  452. in.setId(id);
  453. in.setTypeOther(typeOther);
  454. return expenseAccountDetailsMapper.updateByPrimaryKeySelective(in);
  455. }
  456. @Override
  457. public Object mainExpenseExport(Integer id, HttpServletResponse response) {
  458. MainExpenseAccount mainExpenseAccount = mainExpense(id);
  459. pushMainExpenseExport(mainExpenseAccount,response,uploadPath);
  460. return 1;
  461. }
  462. @Override
  463. public void pushExpenseTotalAmount() {
  464. // orderNewService.pushOrderPublicReleaseCount("800326086952370176");
  465. List<TOrderNew> tOrderNews = orderNewService.selectGetAll();
  466. for (TOrderNew e : tOrderNews) {
  467. orderNewService.pushOrderPublicReleaseCount(e);
  468. }
  469. }
  470. public void pushMainExpenseExport(MainExpenseAccount mainExpenseAccount,HttpServletResponse response, String uploadPath) {
  471. String title = "报销明细表";
  472. String attName = String.format("报销明细表_%s.pdf",new Date().getTime());
  473. String realFileName = uploadPath+"/tmp/"+new Date().getTime() + ".pdf";
  474. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  475. Document document=new Document();
  476. try {
  477. FileUtils.setAttachmentResponseHeader(response, title,attName);
  478. FileOutputStream outputStream = new FileOutputStream(realFileName);
  479. PdfWriter.getInstance(document, outputStream);
  480. document.open();
  481. Paragraph paragraph = new Paragraph(title, PDFUtils.getTitleFont());
  482. paragraph.setAlignment(1);
  483. document.add(paragraph);
  484. //默认算60
  485. PDFUtils.pushExpenseTitle(document,mainExpenseAccount);
  486. PdfPTable table = new PdfPTable(12);
  487. table.setWidthPercentage(100);
  488. ArrayList<PdfPRow> rows = table.getRows();
  489. float[] columnWidths ={12,24,14,50,50,42,28,18,12,12,50,50};
  490. table.setWidths(columnWidths);
  491. table.setSpacingBefore(10f);
  492. table.setSpacingAfter(10f);
  493. //填充表格头部
  494. addTitle(rows);
  495. //获取填充数据
  496. List<Map<Integer,String>> mapList = pushMainExpenseAccount(mainExpenseAccount);
  497. //填充数据写入表格
  498. pushTable(rows,mapList);
  499. document.add(table);
  500. document.close();
  501. outputStream.close();
  502. // FileUtils.writeBytes(realFileName, response.getOutputStream());
  503. // FileUtils.deleteFile(realFileName);
  504. } catch (DocumentException | IOException e) {
  505. e.printStackTrace();
  506. }
  507. }
  508. private static void addTitle(ArrayList<PdfPRow> rows) {
  509. PdfPCell[] cells =new PdfPCell[12];
  510. PdfPRow row=new PdfPRow(cells);
  511. cells[0] = new PdfPCell(new Phrase("序号",PDFUtils.getFont(8)));
  512. cells[1] = new PdfPCell(new Phrase("费用时间",PDFUtils.getFont(8)));
  513. cells[2] = new PdfPCell(new Phrase("时长",PDFUtils.getFont(8)));
  514. cells[3] = new PdfPCell(new Phrase("出差地址",PDFUtils.getFont(8)));
  515. cells[4] = new PdfPCell(new Phrase("出差企业",PDFUtils.getFont(8)));
  516. cells[5] = new PdfPCell(new Phrase("合同编号",PDFUtils.getFont(8)));
  517. cells[6] = new PdfPCell(new Phrase("类型",PDFUtils.getFont(8)));
  518. cells[7] = new PdfPCell(new Phrase("详情",PDFUtils.getFont(8)));
  519. cells[8] = new PdfPCell(new Phrase("费用",PDFUtils.getFont(8)));
  520. cells[9] = new PdfPCell(new Phrase("实报",PDFUtils.getFont(8)));
  521. cells[10] = new PdfPCell(new Phrase("出差事由",PDFUtils.getFont(8)));
  522. cells[11] = new PdfPCell(new Phrase("报销订单/部门",PDFUtils.getFont(8)));
  523. rows.add(row);
  524. }
  525. private void pushTable(ArrayList<PdfPRow> rows, List<Map<Integer, String>> mapList) {
  526. for (Map<Integer, String> map : mapList) {
  527. PdfPCell[] cells =new PdfPCell[12];
  528. PdfPRow row=new PdfPRow(cells);
  529. for (int i = 0; i < 12; i++) {
  530. cells[i] = new PdfPCell(new Phrase(map.get(i),PDFUtils.getFont(8)));
  531. }
  532. rows.add(row);
  533. }
  534. }
  535. private List<Map<Integer, String>> pushMainExpenseAccount(MainExpenseAccount mainExpenseAccount) {
  536. List<Map<Integer,String>> mapList = new ArrayList<>();
  537. if (mainExpenseAccount.getSonList()!=null){
  538. int index=0;
  539. for (SonExpenseAccount e : mainExpenseAccount.getSonList()) {
  540. List<SonExpenseAccountDetails> detList = e.getDetList();
  541. //详情计算,详情完成后需要一个统计
  542. int detIndex = 0;
  543. for (SonExpenseAccountDetails det : detList) {
  544. index++;
  545. detIndex++;
  546. Map<Integer, String> map = new HashMap<>();
  547. map.put(0, String.valueOf(index));
  548. if (detIndex==1){
  549. if (e.getPublicReleaseType()!=null){
  550. map.put(1,DateUtils.formatDate(e.getReleaseStart(),AFTConstants.YYYYMMDDHHMM)+"~"+DateUtils.formatDate(e.getReleaseEnd(),AFTConstants.YYYYMMDDHHMM));
  551. map.put(2,e.getDuration());
  552. map.put(3,e.getDistrictName());
  553. map.put(4,e.getUserNames());
  554. map.put(6,getExpenseType(e));
  555. }else {
  556. map.put(1,det.getAgreeTime());
  557. map.put(2,"");
  558. map.put(3,"");
  559. map.put(4,"");
  560. map.put(6,"");
  561. }
  562. }else {
  563. map.put(1,det.getAgreeTime());
  564. map.put(2,"");
  565. map.put(3,"");
  566. map.put(4,"");
  567. map.put(6,"");
  568. }
  569. map.put(5,e.getContractNo());
  570. map.put(7,getExpenseSecondaryType(e));
  571. map.put(8,getBigDecimalZeros(det.getAmount()));
  572. map.put(9,getBigDecimalZeros(det.getRealAmount()));
  573. map.put(10,e.getPlan());
  574. if (index==1){
  575. map.put(11,getOrderNoAndDepartment(e,mainExpenseAccount.getApplyDepName()));
  576. }else {
  577. map.put(11,"");
  578. }
  579. mapList.add(map);
  580. if (detIndex==detList.size()){
  581. index++;
  582. addCount(e.getAmount(),e.getRealAmount(),mapList,index);
  583. }
  584. }
  585. }
  586. //循环结束新增合计
  587. index++;
  588. addCount(mainExpenseAccount.getAmount(),mainExpenseAccount.getRealAmount(),mapList,index,1);
  589. }
  590. return mapList;
  591. }
  592. private String getBigDecimalZeros(BigDecimal amount) {
  593. return amount.stripTrailingZeros().toEngineeringString();
  594. }
  595. private void addCount(BigDecimal amount,BigDecimal realAmount,List<Map<Integer, String>> mapList,int index) {
  596. addCount(amount,realAmount,mapList,index,0);
  597. }
  598. private void addCount(BigDecimal amount,BigDecimal realAmount,List<Map<Integer, String>> mapList,int index,int type) {
  599. String str="小计";
  600. if (type==1)str="合计";
  601. Map<Integer, String> map = new HashMap<>();
  602. for (int i = 0; i < 12; i++){
  603. if (i==0){
  604. map.put(i, String.valueOf(index));
  605. }else if (i==7){
  606. map.put(i, str);
  607. }else if (i==8){
  608. map.put(i, getBigDecimalZeros(amount));
  609. }else if (i==9){
  610. map.put(i, getBigDecimalZeros(realAmount));
  611. }else {
  612. map.put(i, "");
  613. }
  614. }
  615. mapList.add(map);
  616. }
  617. private String getOrderNoAndDepartment(SonExpenseAccount e,String applyDepName) {
  618. return e.getOrderNo()+"\n"+applyDepName;
  619. }
  620. private String getExpenseSecondaryType(SonExpenseAccount e) {
  621. //交通工具待完善
  622. if (e.getSecondaryType()==0){
  623. return EAsecondaryTypes.getDescByCode(e.getSecondaryType())+"("+e.getSecondaryTypeOther()+")";
  624. }
  625. return EAsecondaryTypes.getDescByCode(e.getSecondaryType());
  626. }
  627. private String getExpenseType(SonExpenseAccount e) {
  628. if (e.getType()==0){
  629. return EATypes.getDescByCode(e.getType())+"("+e.getTypeOther()+")";
  630. }
  631. return EATypes.getDescByCode(e.getType());
  632. }
  633. private void pushResettingDebit(InputExpenseAccount in, Integer debitId) {
  634. ExpenseAccount debit = expenseAccountMapper.selectByPrimaryKey(debitId);
  635. List<ExpenseAccount> list = expenseAccountMapper.selectByDebitId(debitId);
  636. ExpenseAccount newDebit=new ExpenseAccount();
  637. newDebit.setId(debitId);
  638. //计算除开当前单,已选择抵扣金额
  639. BigDecimal count=new BigDecimal(0);
  640. for (ExpenseAccount e : list) {
  641. if (!in.getId().equals(e.getId())){
  642. count=count.add(e.getSettlementAmount());
  643. }
  644. }
  645. newDebit.setSettlementAmount(count);
  646. if (count.compareTo(new BigDecimal(0))==0){
  647. newDebit.setLiquidationStatus(0);
  648. }else if (count.compareTo(debit.getRealAmount())<0){
  649. newDebit.setLiquidationStatus(1);
  650. }else if (count.compareTo(debit.getRealAmount())==0){
  651. newDebit.setLiquidationStatus(2);
  652. }
  653. expenseAccountMapper.updateByPrimaryKeySelective(newDebit);
  654. }
  655. private void pushExpenseAccountDebit(InputExpenseAccount in) {
  656. ExpenseAccount debit = expenseAccountMapper.selectByPrimaryKey(in.getDebitId());
  657. ExpenseAccount newDebit=new ExpenseAccount();
  658. newDebit.setId(debit.getId());
  659. //计算剩余需要支付金额
  660. BigDecimal decbitAmount=debit.getRealAmount().subtract(debit.getSettlementAmount());
  661. //本次金额大于等于需要抵扣金额,则变成完全抵扣,抵扣金额变成本次已付金额
  662. if (in.getRealAmount().compareTo(decbitAmount)>=0){
  663. in.setSettlementAmount(decbitAmount);
  664. newDebit.setSettlementAmount(debit.getRealAmount());
  665. newDebit.setLiquidationStatus(2);
  666. }else {
  667. // 本次金额小于需要抵扣,则本次变更全抵扣,借支已付加上本次金额
  668. in.setSettlementAmount(in.getRealAmount());
  669. BigDecimal setAmount=new BigDecimal(0).add(debit.getSettlementAmount()).add(in.getRealAmount());
  670. newDebit.setSettlementAmount(setAmount);
  671. newDebit.setLiquidationStatus(1);
  672. }
  673. expenseAccountMapper.updateByPrimaryKeySelective(newDebit);
  674. }
  675. @Override
  676. public int addDetails(InputExpenseAccountDetails in) {
  677. if (in.getAgreeTimes()!=null){
  678. in.setAgreeTime(DateUtils.StringToDate(in.getAgreeTimes(),AFTConstants.YYYYMMDD));
  679. }
  680. ExpenseAccount expenseAccount = expenseAccountMapper.selectByPrimaryKey(in.getEaid());
  681. if (expenseAccount.getType()==2){
  682. in.setType(expenseAccount.getSecondaryType());
  683. if (in.getType()==0){
  684. in.setTypeOther(expenseAccount.getSecondaryTypeOther());
  685. }
  686. }
  687. in.setRealAmount(in.getAmount());
  688. expenseAccountDetailsMapper.insertSelective(in);
  689. ExpenseAccount ea =new ExpenseAccount();
  690. ea.setId(in.getEaid());
  691. pushExpenseAccountAmount(ea);
  692. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  693. return 1;
  694. }
  695. @Override
  696. public int deleteDetails(Integer id) {
  697. ExpenseAccountDetails expenseAccountDetails = expenseAccountDetailsMapper.selectByPrimaryKey(id);
  698. Integer eaid=expenseAccountDetails.getEaid();
  699. expenseAccountDetailsMapper.deleteByPrimaryKey(id);
  700. ExpenseAccount ea =new ExpenseAccount();
  701. ea.setId(eaid);
  702. pushExpenseAccountAmount(ea);
  703. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  704. return 1;
  705. }
  706. @Override
  707. public List<OutExpenseAccountDetails> detailsList(Integer eaid) {
  708. return expenseAccountDetailsMapper.selectByEAid(eaid);
  709. }
  710. @Override
  711. public Object logList(Integer eaid) {
  712. List<OutExpenseAccountLog> list = expenseAccountLogMapper.selectByEaid(eaid);
  713. if (!list.isEmpty()) {
  714. OutExpenseAccountLog lastout = list.get(list.size() - 1);
  715. ExpenseAccount expenseAccount = expenseAccountMapper.selectByPrimaryKey(eaid);
  716. Integer processStatus = lastout.getProcessStatus();
  717. if (lastout.getStatus() < 2) {
  718. if (processStatus ==0||processStatus ==2||processStatus ==3){
  719. List<OutExpenseAccountLog> outExpenseAccountLogs = expenseAccountExamineMapper.selectNotExamineByEaidAndProcessStatus(eaid, processStatus);
  720. list.addAll(outExpenseAccountLogs);
  721. }else {
  722. List<ExpenseAccountExamine> expenseAccountExamines = expenseAccountExamineMapper.selectByEaidAndProcessStatus(eaid, processStatus);
  723. for (ExpenseAccountExamine e : expenseAccountExamines) {
  724. if (e.getStatus()==0){
  725. OutExpenseAccountLog out =new OutExpenseAccountLog();
  726. out.setEaid(eaid);
  727. out.setProcessStatus(e.getProcessStatus());
  728. out.setAuditorName(e.getAuditorname());
  729. list.add(out);
  730. }
  731. }
  732. }
  733. if (processStatus<8)addDetailsLog(list, expenseAccount,processStatus);
  734. }
  735. }
  736. return list;
  737. }
  738. private void addDetailsLog(List<OutExpenseAccountLog> list, ExpenseAccount expenseAccount,Integer useStatus) {
  739. OrganizationListOut dep = departmentMapper.selectAllById(expenseAccount.getApplyDep());
  740. List<ExpenseConfigDetails> configDetails = expenseConfigDetailsMapper.selectByDepId(expenseAccount.getApplyDep());
  741. for (int processStatus = useStatus; processStatus < 8; processStatus++) {
  742. if (processStatus ==3){
  743. if (dep.getExpenseRetrialFinanceExamine()==1){
  744. List<ExpenseConfigDetails> collect = configDetails.stream().filter(e -> e.getType() == 1).collect(Collectors.toList());
  745. for (ExpenseConfigDetails e : collect) {
  746. addDetailsTypeLog(list, expenseAccount, e);
  747. }
  748. }
  749. }else if (processStatus ==4){
  750. if (expenseAccount.getApproval()==1&&dep.getCeoExamine()==1){
  751. List<ExpenseConfigDetails> collect = configDetails.stream().filter(e -> e.getType() == 2).collect(Collectors.toList());
  752. for (ExpenseConfigDetails e : collect) {
  753. addDetailsTypeLog(list, expenseAccount, e);
  754. }
  755. }
  756. }else if (processStatus ==5){
  757. if (dep.getCfoExamine()==1){
  758. List<ExpenseConfigDetails> collect = configDetails.stream().filter(e -> e.getType() == 3).collect(Collectors.toList());
  759. for (ExpenseConfigDetails e : collect) {
  760. addDetailsTypeLog(list, expenseAccount, e);
  761. }
  762. }
  763. }else if (processStatus ==6){
  764. if (dep.getViceCeoExamine()==1){
  765. List<ExpenseConfigDetails> collect = configDetails.stream().filter(e -> e.getType() == 4).collect(Collectors.toList());
  766. for (ExpenseConfigDetails e : collect) {
  767. addDetailsTypeLog(list, expenseAccount, e);
  768. }
  769. }
  770. }else if (processStatus ==7){
  771. if (dep.getCashierExamine()==1){
  772. List<ExpenseConfigDetails> collect = configDetails.stream().filter(e -> e.getType() == 5).collect(Collectors.toList());
  773. for (ExpenseConfigDetails e : collect) {
  774. addDetailsTypeLog(list, expenseAccount, e);
  775. }
  776. }
  777. }
  778. }
  779. }
  780. private static void addDetailsTypeLog(List<OutExpenseAccountLog> list, ExpenseAccount expenseAccount, ExpenseConfigDetails e) {
  781. OutExpenseAccountLog addLog=new OutExpenseAccountLog();
  782. addLog.setEaid(expenseAccount.getId());
  783. addLog.setAuditor(e.getAid());
  784. addLog.setAuditorName(e.getAdminName());
  785. list.add(addLog);
  786. }
  787. @Override
  788. public int updateExamine(InputExpenseAccount in) {
  789. ExpenseAccount useEa = expenseAccountMapper.selectByPrimaryKey(in.getId());
  790. ExpenseAccount newEa=new ExpenseAccount();
  791. newEa.setId(useEa.getId());
  792. newEa.setApproval(in.getApproval());
  793. in.setCheckNo(useEa.getCheckNo());
  794. in.setProcessStatus(useEa.getProcessStatus());
  795. //审核通知 获取发送人
  796. AdminListBo admin = adminMapper.getDeptNameByAid(useEa.getAid());
  797. //项目推送流程及添加日志
  798. addExamineExpenseAccountLog(admin,in, useEa, newEa);
  799. //完成申请
  800. if (newEa.getStatus()==2){
  801. //如果是借支则订单新增
  802. List<ExpenseAccount> expenseAccounts1 = expenseAccountMapper.selectListByMainId(in.getId());
  803. for (ExpenseAccount e : expenseAccounts1) {
  804. if (e.getType()!=4){
  805. //不然就看是否挂载借支订单进行抵扣
  806. if (e.getDebitId()!=null){
  807. ExpenseAccount useDebit=expenseAccountMapper.selectByPrimaryKey(e.getDebitId());
  808. if (useDebit.getLiquidationStatus()==2){
  809. List<ExpenseAccount> expenseAccounts = expenseAccountMapper.selectByDebitId(e.getDebitId());
  810. boolean flag=true;
  811. for (ExpenseAccount expenseAccount : expenseAccounts) {
  812. if(!expenseAccount.getId().equals(in.getId())&&expenseAccount.getStatus()!=2){
  813. flag=false;
  814. break;
  815. }
  816. }
  817. if (flag){
  818. ExpenseAccount debit=new ExpenseAccount();
  819. debit.setId(e.getDebitId());
  820. debit.setLiquidationStatus(3);
  821. expenseAccountMapper.updateByPrimaryKeySelective(debit);
  822. }
  823. }
  824. }
  825. }
  826. ExpenseAccount updateSon=new ExpenseAccount();
  827. updateSon.setId(e.getId());
  828. updateSon.setStatus(2);
  829. expenseAccountMapper.updateByPrimaryKeySelective(updateSon);
  830. if (e.getOrderNo()!=null){
  831. tOrderMidMapper.updateOExpense(e.getOrderNo());
  832. }
  833. }
  834. }else if (newEa.getStatus()==1){
  835. if (Objects.equals(newEa.getProcessStatus(), EAProcessStatus.BMFZRSH.getCode())){
  836. pushFinanceCount(admin.getDepFinance(),useEa.getCreateTime(),0);
  837. //处理金额修改
  838. if(in.getRealList()!=null){
  839. List<Map> maps = JSONObject.parseArray(in.getRealList(), Map.class);
  840. for (Map map: maps) {
  841. Integer type= (Integer) map.get("type");
  842. Integer id= (Integer) map.get("id");
  843. BigDecimal amount= new BigDecimal(map.get("amount").toString());
  844. //type=1总计,type=2报销金额
  845. if(type==1){
  846. ExpenseAccount ea =new ExpenseAccount();
  847. ea.setId(id);
  848. ea.setRealAmount(amount);
  849. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  850. }else if (type==2){
  851. ExpenseAccountDetails ead=new ExpenseAccountDetails();
  852. ead.setId(id);
  853. ead.setRealAmount(amount);
  854. expenseAccountDetailsMapper.updateByPrimaryKeySelective(ead);
  855. }
  856. }
  857. }
  858. }else if (Objects.equals(newEa.getProcessStatus(), EAProcessStatus.CWSH.getCode())){
  859. pushFinanceCount(admin.getDepFinance(),useEa.getCreateTime(),1);
  860. }
  861. }else if (newEa.getStatus()==3){
  862. if (Objects.equals(useEa.getProcessStatus(), EAProcessStatus.BMFZRSH.getCode())){
  863. pushFinanceCount(admin.getDepFinance(),useEa.getCreateTime(),2);
  864. }else if (newEa.getProcessStatus()>EAProcessStatus.CWSH.getCode()){
  865. pushFinanceCount(admin.getDepFinance(),useEa.getCreateTime(),3);
  866. }
  867. expenseAccountExamineMapper.updateStatusByEaidAndAid(in.getId(),null,0,0);
  868. }
  869. expenseAccountMapper.updateByPrimaryKeySelective(newEa);
  870. expenseAccountMapper.updateSonByMainId(newEa.getId());
  871. return 1;
  872. }
  873. private void addFinanceCount(String aid,Date startTime ) {
  874. String forDate=DateUtils.formatDate(startTime,AFTConstants.YYYYMMDD);
  875. addFinanceCount(aid,forDate);
  876. }
  877. private void addFinanceCount(String aid,String startTime ) {
  878. FinanceCount financeCount = financeCountMapper.selectByAidAndDates(aid, startTime);
  879. if (financeCount ==null){
  880. financeCount =FinanceCount.initialization(aid, startTime);
  881. financeCount.setExpenseCount(1);
  882. financeCount.setExpenseUnauditedCount(1);
  883. financeCountMapper.insertSelective(financeCount);
  884. }else {
  885. FinanceCount newFinanceCount =new FinanceCount();
  886. newFinanceCount.setId(financeCount.getId());
  887. newFinanceCount.setExpenseCount(financeCount.getExpenseCount()+1);
  888. newFinanceCount.setExpenseUnauditedCount(financeCount.getExpenseUnauditedCount()+1);
  889. financeCountMapper.updateByPrimaryKeySelective(newFinanceCount);
  890. }
  891. }
  892. private void addExamineExpenseAccountLog(AdminListBo admin,InputExpenseAccount in, ExpenseAccount useEa, ExpenseAccount newEa) {
  893. List<Admin> aids=new ArrayList<>();
  894. StringBuilder str=new StringBuilder();
  895. Integer status=in.getStatus();
  896. if (status==1){
  897. //审核处理审核表
  898. expenseAccountExamineMapper.updateStatusByEaidAndAid(in.getId(),TokenManager.getAdminId(),1,0);
  899. str.append("编号[").append(in.getCheckNo()).append("]需要审核").append(",请查看并审核。");
  900. newEa.setAid(useEa.getAid());
  901. newEa.setCreateTime(useEa.getCreateTime());
  902. newEa.setApplyDep(useEa.getApplyDep());
  903. //在跳入董事长审核的时候非特批直接跳过,原则上下个流程不可再跳过
  904. OrganizationListOut dep = departmentMapper.selectAllById(newEa.getApplyDep());
  905. List<ExpenseConfigDetails> configDetailsList = expenseConfigDetailsMapper.selectByDepId(newEa.getApplyDep());
  906. if(Objects.equals(useEa.getProcessStatus(), EAProcessStatus.CNSH.getCode())){
  907. expenseAccountExamineMapper.updateStatusByEaidAndAid(in.getId(),TokenManager.getAdminId(),1,1);
  908. boolean expenseExamine = pushExpenseExamine(configDetailsList, newEa, EAProcessStatus.CNSH.getCode());
  909. if (expenseExamine){
  910. //完成后的流程
  911. status =2;
  912. aids.add(adminMapper.selectById(admin.getId()));
  913. newEa.setStatus(status);
  914. newEa.setExamineName("");
  915. }else {
  916. status =1;
  917. List<ExpenseAccountExamine> list=expenseAccountExamineMapper.selectByEaidAndProcessStatus(newEa.getId(), useEa.getProcessStatus());
  918. StringBuilder aname =new StringBuilder();
  919. for (ExpenseAccountExamine e : list) {
  920. if (e.getStatus()==0){
  921. Admin ad=adminMapper.selectById(e.getAuditor());
  922. aids.add(ad);
  923. aname.append(ad.getName()).append(",");
  924. }
  925. }
  926. newEa.setExamineName(aname.substring(0,aname.length()-1));
  927. }
  928. addExpenseAccountLog(in.getId(),status,useEa.getProcessStatus(),TokenManager.getAdminId(),in.getReason());
  929. }else {
  930. //小于为固定单审核,大于可能被配置多人审核
  931. if (useEa.getProcessStatus().equals(EAProcessStatus.BMFZRSH.getCode())||
  932. useEa.getProcessStatus().equals(EAProcessStatus.CWSH.getCode())
  933. ){
  934. boolean flag=false;
  935. List<ExpenseAccountExamine> list=expenseAccountExamineMapper.selectByEaidAndProcessStatus(newEa.getId(), useEa.getProcessStatus());
  936. for (ExpenseAccountExamine e : list) {
  937. if (e.getAuditor().equals(TokenManager.getAdminId())){
  938. flag=true;
  939. }
  940. }
  941. if (!flag)throw new BusinessException("审核角色错误。请刷新页面");
  942. newEa.setProcessStatus(useEa.getProcessStatus()+1);
  943. }else {
  944. newEa.setProcessStatus(useEa.getProcessStatus());
  945. }
  946. addExpenseAccountLog(in.getId(),status,useEa.getProcessStatus(),TokenManager.getAdminId(),in.getReason());
  947. newEa.setStatus(1);
  948. if (newEa.getApproval()==null)newEa.setApproval(useEa.getApproval());
  949. //流程的推动
  950. examineByprocess(newEa,admin,dep,configDetailsList);
  951. //推动到出纳如果设置无需则完成不设置下个人,否则就要设置
  952. if (Objects.equals(newEa.getProcessStatus(), EAProcessStatus.CNSH.getCode())){
  953. if(dep.getCashierExamine()==1){
  954. pushExamineName(newEa, aids);
  955. }else {
  956. //完成后的流程
  957. status =2;
  958. aids.add(adminMapper.selectById(admin.getId()));
  959. newEa.setStatus(status);
  960. newEa.setExamineName("");
  961. }
  962. }else {
  963. pushExamineName(newEa, aids);
  964. }
  965. }
  966. }else {
  967. aids.add(adminMapper.selectById(admin.getId()));
  968. newEa.setStatus(3);
  969. newEa.setProcessStatus(0);
  970. str.setLength(0);
  971. addExpenseAccountLog(in.getId(),newEa.getStatus(),useEa.getProcessStatus(),TokenManager.getAdminId(),in.getReason());
  972. str.append("您的报销审核已驳回,报销编号[").append(in.getCheckNo()).append("],请注意查看。");
  973. }
  974. if (newEa.getStatus()==null)newEa.setStatus(1);
  975. addNoticeAndEmail(in, status,str.toString(),admin.getName(),aids);
  976. }
  977. private void pushExamineName(ExpenseAccount newEa, List<Admin> aids) {
  978. List<ExpenseAccountExamine> list=expenseAccountExamineMapper.selectByEaidAndProcessStatus(newEa.getId(), newEa.getProcessStatus());
  979. StringBuilder aname =new StringBuilder();
  980. for (ExpenseAccountExamine e : list) {
  981. if (Objects.equals(e.getProcessStatus(), newEa.getProcessStatus()) &&e.getStatus()==0){
  982. Admin ad=adminMapper.selectById(e.getAuditor());
  983. aids.add(ad);
  984. aname.append(ad.getName()).append(",");
  985. }
  986. }
  987. newEa.setExamineName(aname.substring(0,aname.length()-1));
  988. }
  989. /**
  990. *
  991. * @param type 0 双新增 1 未审核减一 2双减 3总数减一
  992. */
  993. private void pushFinanceCount(String aid, Date createTime, Integer type) {
  994. String startTime= DateUtils.formatDate(createTime,AFTConstants.YYYYMMDD);
  995. FinanceCount financeCount = financeCountMapper.selectByAidAndDates(aid, startTime);
  996. FinanceCount newFinance=new FinanceCount();
  997. if (financeCount==null){
  998. newFinance=FinanceCount.initialization(aid,createTime);
  999. newFinance.setExpenseCount(1);
  1000. newFinance.setExpenseUnauditedCount(1);
  1001. financeCountMapper.insertSelective(newFinance);
  1002. }else {
  1003. newFinance.setId(financeCount.getId());
  1004. if(type==0){
  1005. newFinance.setExpenseCount(financeCount.getExpenseCount()+1);
  1006. newFinance.setExpenseUnauditedCount(financeCount.getExpenseUnauditedCount()+1);
  1007. }else if(type==1){
  1008. newFinance.setExpenseUnauditedCount(financeCount.getExpenseUnauditedCount()-1);
  1009. }else if(type==2){
  1010. newFinance.setExpenseCount(financeCount.getExpenseCount()-1);
  1011. newFinance.setExpenseUnauditedCount(financeCount.getExpenseUnauditedCount()-1);
  1012. }else if(type==3){
  1013. newFinance.setExpenseCount(financeCount.getExpenseCount()-1);
  1014. }
  1015. financeCountMapper.updateByPrimaryKeySelective(newFinance);
  1016. }
  1017. }
  1018. /**
  1019. * 审核流程推送,只要不是完成状态就继续判断是否跳过
  1020. * @param newEa
  1021. */
  1022. private void examineByprocess( ExpenseAccount newEa,AdminListBo admin,OrganizationListOut dep,List<ExpenseConfigDetails> list) {
  1023. List<ExpenseAccountExamine> examineList;
  1024. //小于财务复审的,走单审核
  1025. if (newEa.getProcessStatus().equals(EAProcessStatus.BMFZRSH.getCode())||
  1026. newEa.getProcessStatus().equals(EAProcessStatus.CWSH.getCode())){
  1027. examineList = expenseAccountExamineMapper.selectByEaidAndProcessStatus(newEa.getId(), newEa.getProcessStatus());
  1028. boolean flag=false;
  1029. String aid="";
  1030. if (!examineList.isEmpty()){
  1031. for (ExpenseAccountExamine e : examineList) {
  1032. if (e.getStatus()==1){
  1033. flag=true;
  1034. aid=e.getAuditor();
  1035. break;
  1036. }
  1037. }
  1038. }
  1039. //如果当前审核人已经审核,则跳过当前审核
  1040. if (flag){
  1041. String str=String.format("重复审核人跳过[%s]。",EAProcessStatus.getDesc(newEa.getProcessStatus()));
  1042. Date date=new Date();
  1043. date.setTime(date.getTime()+ 1000L);
  1044. addExpenseAccountLog(newEa.getId(),1,newEa.getProcessStatus(),aid,str,date,2);
  1045. if (Objects.equals(newEa.getProcessStatus(), EAProcessStatus.BMFZRSH.getCode())){
  1046. addFinanceCount(admin.getDepFinance(),newEa.getCreateTime());
  1047. }
  1048. newEa.setProcessStatus(newEa.getProcessStatus()+1);
  1049. examineByprocess(newEa,admin,dep,list);
  1050. }
  1051. }else {
  1052. //大于等于财务复审的,走多审核
  1053. String str=null;
  1054. //跳过标记
  1055. boolean flag2= false;
  1056. //审核通过标记
  1057. boolean flag3= false;
  1058. Date date2=new Date();
  1059. date2.setTime(date2.getTime()+ 1000L);
  1060. String tip=null;
  1061. String aid=null;
  1062. if (Objects.equals(newEa.getProcessStatus(), EAProcessStatus.SJSH.getCode())){
  1063. //财务复审财务复审
  1064. if (admin.getExpenseSuperExamine()==0){
  1065. flag2=true;
  1066. tip=EAProcessStatus.SJSH.getDesc();
  1067. }else {
  1068. boolean expenseExamine = pushExpenseExamine(list, newEa, EAProcessStatus.SJSH.getCode());
  1069. if (expenseExamine){
  1070. flag3=true;
  1071. }
  1072. }
  1073. }else if (Objects.equals(newEa.getProcessStatus(), EAProcessStatus.CWFS.getCode())){
  1074. //财务复审财务复审
  1075. if (dep.getExpenseRetrialFinanceExamine()==0){
  1076. flag2=true;
  1077. tip=EAProcessStatus.CWFS.getDesc();
  1078. }else {
  1079. boolean expenseExamine = pushExpenseExamine(list, newEa, EAProcessStatus.CWFS.getCode());
  1080. if (expenseExamine){
  1081. flag3=true;
  1082. }
  1083. }
  1084. }else if (Objects.equals(newEa.getProcessStatus(), EAProcessStatus.DSZSH.getCode())){
  1085. if (newEa.getApproval()!=1){
  1086. flag2=true;
  1087. tip="特批(财务选择)";
  1088. }else {
  1089. if (dep.getCeoExamine()==1){
  1090. boolean expenseExamine = pushExpenseExamine(list, newEa, EAProcessStatus.DSZSH.getCode());
  1091. if (expenseExamine){
  1092. flag3=true;
  1093. }
  1094. }else {
  1095. flag2=true;
  1096. tip="特批(部门设置)";
  1097. }
  1098. }
  1099. }else if (Objects.equals(newEa.getProcessStatus(), EAProcessStatus.CWGGLYSH.getCode())){
  1100. if (dep.getExpenseRetrialFinanceExamine()==0){
  1101. flag2=true;
  1102. tip=EAProcessStatus.CWGGLYSH.getDesc();
  1103. aid=null;
  1104. }else {
  1105. boolean expenseExamine = pushExpenseExamine(list, newEa, EAProcessStatus.CWGGLYSH.getCode());
  1106. if (expenseExamine){
  1107. flag3=true;
  1108. }
  1109. }
  1110. }else if (Objects.equals(newEa.getProcessStatus(), EAProcessStatus.GSFSSH.getCode())){
  1111. if (dep.getExpenseRetrialFinanceExamine()==0){
  1112. flag2=true;
  1113. tip=EAProcessStatus.GSFSSH.getDesc();
  1114. }else {
  1115. boolean expenseExamine = pushExpenseExamine(list, newEa, EAProcessStatus.GSFSSH.getCode());
  1116. if (expenseExamine){
  1117. flag3=true;
  1118. }
  1119. }
  1120. }else if (Objects.equals(newEa.getProcessStatus(), EAProcessStatus.CNSH.getCode())){
  1121. if (dep.getExpenseRetrialFinanceExamine()==0){
  1122. flag2=true;
  1123. tip=EAProcessStatus.CNSH.getDesc();
  1124. }else {
  1125. //财务出纳在审核时不与前置审核做同步
  1126. pushExpenseExamine(list, newEa, EAProcessStatus.CNSH.getCode());
  1127. }
  1128. }
  1129. if (flag2){
  1130. if (str==null)str=String.format("未设置[%s]审核,跳过此流程",tip);
  1131. if (newEa.getProcessStatus()<EAProcessStatus.CNSH.getCode()){
  1132. date2.setTime(date2.getTime()+100);
  1133. addExpenseAccountLog(newEa.getId(),1,newEa.getProcessStatus(),aid,str,date2,2);
  1134. newEa.setProcessStatus(newEa.getProcessStatus()+1);
  1135. examineByprocess(newEa,admin,dep,list);
  1136. }else {
  1137. date2.setTime(date2.getTime()+100);
  1138. addExpenseAccountLog(newEa.getId(),2,newEa.getProcessStatus(),aid,str,date2,2);
  1139. newEa.setProcessStatus(newEa.getProcessStatus());
  1140. newEa.setStatus(2);
  1141. }
  1142. }else if (flag3){
  1143. newEa.setProcessStatus(newEa.getProcessStatus()+1);
  1144. examineByprocess(newEa,admin,dep,list);
  1145. }
  1146. }
  1147. }
  1148. private boolean pushExpenseExamine(List<ExpenseConfigDetails> list, ExpenseAccount newEa, Integer status) {
  1149. int finalConfigDetails = getFinalConfigDetails(status);
  1150. //查询审核表,如果为空就新增,不然直接判定审核
  1151. List<ExpenseAccountExamine> expenseAccountExamines = expenseAccountExamineMapper.selectByEaidAndProcessStatus(newEa.getId(), status);
  1152. int count = 0;
  1153. int size;
  1154. if (expenseAccountExamines.isEmpty()){
  1155. List<ExpenseConfigDetails> collect = list.stream().filter(e -> e.getType() == finalConfigDetails).collect(Collectors.toList());
  1156. size= collect.size();
  1157. for (ExpenseConfigDetails e : collect) {
  1158. ExpenseAccountExamine eae=new ExpenseAccountExamine(newEa.getId(),status);
  1159. eae.setAuditor(e.getAid());
  1160. eae.setAuditorname(e.getAdminName());
  1161. eae.setStatus(0);
  1162. Integer i = expenseAccountExamineMapper.selectYesByParam(newEa.getId(), e.getAid());
  1163. if (Objects.equals(status, EAProcessStatus.CNSH.getCode())){
  1164. eae.setStatus(0);
  1165. count++;
  1166. }else {
  1167. if (i>0||e.getAid().equals(TokenManager.getAdminId())) {
  1168. eae.setStatus(1);
  1169. count++;
  1170. }
  1171. }
  1172. expenseAccountExamineMapper.insertSelective(eae);
  1173. }
  1174. }else {
  1175. size = expenseAccountExamines.size();
  1176. for (ExpenseAccountExamine e : expenseAccountExamines) {
  1177. if (e.getStatus()==1){
  1178. count++;
  1179. }
  1180. }
  1181. }
  1182. if (count==size){
  1183. return true;
  1184. }
  1185. return false;
  1186. }
  1187. private int getFinalConfigDetails(Integer status) {
  1188. int configDetails=0;
  1189. if (Objects.equals(status, EAProcessStatus.CWFS.getCode()))configDetails=1;
  1190. else if (Objects.equals(status, EAProcessStatus.DSZSH.getCode()))configDetails=2;
  1191. else if (Objects.equals(status, EAProcessStatus.CWGGLYSH.getCode()))configDetails=3;
  1192. else if (Objects.equals(status, EAProcessStatus.GSFSSH.getCode()))configDetails=4;
  1193. else if (Objects.equals(status, EAProcessStatus.CNSH.getCode()))configDetails=5;
  1194. return configDetails;
  1195. }
  1196. private void addNoticeAndEmail(ExpenseAccount in, Integer status, String str,String aname, List<Admin> aids) {
  1197. Date date= new Date();
  1198. Integer noticeStatus=NoticeStatus.EXPENSE_NOTICE.getCode();
  1199. if (status==2){
  1200. noticeStatus=NoticeStatus.EXPENSE_COMPLETE.getCode();
  1201. str="您提交的费用报销单已通过审核,请及时查看。";
  1202. }
  1203. List<Notice> list=new ArrayList<>();
  1204. if (aids!=null){
  1205. for (Admin a : aids) {
  1206. Notice notice=new Notice();
  1207. notice.setId(UUID.randomUUID().toString());
  1208. notice.setNoticeType(noticeStatus);
  1209. notice.setType(NoticeTypes.getType(noticeStatus));
  1210. notice.setAid(a.getId());
  1211. notice.setCreateTime(date);
  1212. notice.setContent(str);
  1213. notice.setReaded(0);
  1214. list.add(notice);
  1215. EmailBo emailBo=new EmailBo(NoticeStatus.getValueByCode(noticeStatus),a.getEmail(),str);
  1216. asyncUtils.send(emailBo);
  1217. try {
  1218. weChatUtils.addExpenseAccountWeChatNotice(a.getExpenseOpenId(),status,in.getId(),date,aname,str);
  1219. }catch (Exception e){
  1220. throw new BusinessException("网络通信异常,请稍后再试。");
  1221. }
  1222. }
  1223. asyncUtils.addNoticeBatch(list);
  1224. }
  1225. }
  1226. @Override
  1227. public Pagination<?> pageList(InputPageListBo in) {
  1228. Map<String ,Object> param=new HashMap<>();
  1229. addParam(in, param);
  1230. Pagination<?> pagePlus = findPagePlus("selectExpenseAccountList",
  1231. "selectExpenseAccountCount",
  1232. "selectExpenseAccountTotalAmount", param, in.getPageNo(), in.getPageSize());
  1233. List<OutExpenseAccountBo> list = (List<OutExpenseAccountBo>) pagePlus.getList();
  1234. for (OutExpenseAccountBo e : list) {
  1235. //设定是否需要审核
  1236. e.setExamine(pushExpenseAccountGetExamine(e.getId(), e.getProcessStatus()));
  1237. //设定是否完成订单匹配
  1238. List<ExpenseAccount> expenseAccounts = expenseAccountMapper.selectListByMainId(e.getId());
  1239. int count = 0;
  1240. if(!expenseAccounts.isEmpty()){
  1241. for (ExpenseAccount e2 : expenseAccounts) {
  1242. if(e2!=null&& e2.getOrderNo()!=null){
  1243. count++;
  1244. }
  1245. }
  1246. }
  1247. if (count==expenseAccounts.size()){
  1248. e.setFullyAllocate(1);
  1249. }else {
  1250. e.setFullyAllocate(0);
  1251. }
  1252. }
  1253. return pagePlus;
  1254. }
  1255. @Override
  1256. public Object selectByPrid(InputExpenseAccount in) {
  1257. in.setAid(TokenManager.getAdminId());
  1258. OutExpenseAccount useEa = expenseAccountMapper.selectByaidAndPrid(in);
  1259. if (useEa!=null){
  1260. ExpenseRelationship er = expenseRelationshipMapper.selectSonId(useEa.getId());
  1261. if (er!=null){
  1262. throw new BusinessException("公出已经发起报销");
  1263. }
  1264. }
  1265. return useEa;
  1266. }
  1267. @Override
  1268. public Object selectById(Integer id) {
  1269. OutExpenseAccount useEa = expenseAccountMapper.selectByid(id);
  1270. //判断当前角色是否审核
  1271. useEa.setExamine(pushExpenseAccountGetExamine( id, useEa.getProcessStatus()));
  1272. if (useEa.getDebitId()!=null){
  1273. pushExpenseAccountGetDebitAmount(useEa);
  1274. }
  1275. return useEa;
  1276. }
  1277. @Override
  1278. public Object selectByCheckNo(String checkNo) {
  1279. checkNo=checkNo.trim();
  1280. OutExpenseAccount useEa = expenseAccountMapper.selectByCheckNo(checkNo);
  1281. if (useEa==null){
  1282. throw new BusinessException("报销编号不存在,请客对后再查询。");
  1283. }
  1284. if (useEa.getProcessStatus()<3||useEa.getStatus()!=2){
  1285. throw new BusinessException("报销单财务未审核,不可以根据编号查看打印。");
  1286. }
  1287. return useEa;
  1288. }
  1289. @Override
  1290. public Object pushMerge(List<ExpenseAccount> list) {
  1291. //设置付款部门和支付部门
  1292. InputExpenseAccount in =new InputExpenseAccount();
  1293. in.setCheckNo(idGenerator.getNOgenerateId());
  1294. if (in.getAid()==null){
  1295. in.setAid(TokenManager.getAdminId());
  1296. }
  1297. Admin admin = adminMapper.selectById(in.getAid());
  1298. in.setAname(admin.getName());
  1299. in.setApplyDep(admin.getDepartmentId());
  1300. in.setPayDep(admin.getDepartmentId());
  1301. Integer eaaid = expenseAccountPrivateMapper.selectByaidDefault(TokenManager.getAdminId());
  1302. in.setEaaid(eaaid);
  1303. BigDecimal count = new BigDecimal(0);
  1304. BigDecimal amount = new BigDecimal(0);
  1305. BigDecimal realAmount = new BigDecimal(0);
  1306. List<String> typeList = new ArrayList<>();
  1307. for (ExpenseAccount e : list) {
  1308. count=count.add(e.getTotalAmount());
  1309. amount=amount.add(e.getAmount());
  1310. realAmount=realAmount.add(e.getRealAmount());
  1311. StringBuilder types=new StringBuilder();
  1312. if (e.getType()!=0){
  1313. types.append(EATypes.getDescByCode(e.getType()));
  1314. }else {
  1315. types.append(e.getTypeOther());
  1316. }
  1317. if (e.getType()==2){
  1318. if (e.getSecondaryType()==0){
  1319. types.append("-").append(e.getSecondaryTypeOther());
  1320. }else{
  1321. String descByCode = EAsecondaryTypes.getDescByCode(e.getSecondaryType());
  1322. types.append("-").append(descByCode);
  1323. }
  1324. }
  1325. if (!typeList.contains(types.toString()))typeList.add(types.toString());
  1326. }
  1327. in.setTotalAmount(count);
  1328. in.setAmount(amount);
  1329. in.setRealAmount(realAmount);
  1330. in.setExpenseMain(1);
  1331. in.setTypeOther(String.join(",",typeList));
  1332. expenseAccountMapper.insertSelective(in);
  1333. List<OutExpenseAccountLog> newLog=new ArrayList<>();
  1334. list.forEach(e -> {
  1335. ExpenseRelationship er=new ExpenseRelationship();
  1336. er.setId(in.getId());
  1337. er.setEaId(e.getId());
  1338. expenseRelationshipMapper.insertSelective(er);
  1339. List<OutExpenseAccountLog> listLog = expenseAccountLogMapper.selectByEaid(e.getId());
  1340. for (OutExpenseAccountLog log : listLog) {
  1341. if (!newLog.contains(log)){
  1342. log.setEaid(in.getId());
  1343. log.setShowStatus(1);
  1344. newLog.add(log);
  1345. }
  1346. }
  1347. expenseAccountLogMapper.deleteByeaid(e.getId());
  1348. });
  1349. if (!newLog.isEmpty())expenseAccountLogMapper.insertBatch(newLog);
  1350. expenseAccountMapper.updateByIds(list,1);
  1351. return in.getId();
  1352. }
  1353. @Override
  1354. public MainExpenseAccount mainExpense(Integer id) {
  1355. MainExpenseAccount res = expenseAccountMapper.selectByMainId(id);
  1356. pushData(res);
  1357. res.setExamine(0);
  1358. if (res.getAid().equals(TokenManager.getAdminId())){
  1359. res.setExamine(2);
  1360. }else {
  1361. res.setExamine(pushExpenseAccountGetExamine( id, res.getProcessStatus()));
  1362. }
  1363. return res;
  1364. }
  1365. private void pushData(MainExpenseAccount res) {
  1366. List<SonExpenseAccount> sonList = res.getSonList();
  1367. List<String> orderList = new ArrayList<>();
  1368. List<SonExpenseAccount> orderSonList = new ArrayList<>();
  1369. //用来去重
  1370. List<String> strList = new ArrayList<>();
  1371. sonList.forEach(e -> {
  1372. if (e.getOrderNo()==null){
  1373. orderSonList.add(e);
  1374. }else if (!orderList.contains(e.getOrderNo())){
  1375. orderList.add(e.getOrderNo());
  1376. orderSonList.add(e);
  1377. }
  1378. });
  1379. orderList.clear();
  1380. for (SonExpenseAccount e : orderSonList) {
  1381. if (StringUtils.isNotEmpty(e.getUserNames())){
  1382. String userNames = e.getUserNames();
  1383. String districtName = e.getDistrictName();
  1384. int start=userNames.indexOf("[");
  1385. if (start>0){
  1386. userNames = userNames.substring(0,start);
  1387. districtName=districtName.replace(userNames,"");
  1388. e.setUserNames(userNames);
  1389. e.setDistrictName(districtName);
  1390. }else {
  1391. districtName=districtName.replace(userNames,"");
  1392. e.setDistrictName(districtName);
  1393. }
  1394. }
  1395. if (e.getOrderNo()!=null){
  1396. List<ExpenseAccount> expenseAccounts = expenseAccountMapper.selectByOrderNo(e.getOrderNo());
  1397. int year = LocalDate.now().getYear();
  1398. BigDecimal count = BigDecimal.ZERO;
  1399. BigDecimal yxyCount = BigDecimal.ZERO;
  1400. BigDecimal zxsCount = BigDecimal.ZERO;
  1401. BigDecimal qtCount = BigDecimal.ZERO;
  1402. TOrderPublicReleaseCount out = tOrderPublicReleaseCountMapper.selectByOrderNo(e.getOrderNo());
  1403. if(!expenseAccounts.isEmpty()){
  1404. for (ExpenseAccount ea : expenseAccounts) {
  1405. count=count.add(ea.getRealAmount());
  1406. Admin admin = adminMapper.selectById(ea.getAid());
  1407. if (admin.getPosition()!=null){
  1408. if (admin.getPosition().equals("1")){
  1409. yxyCount=yxyCount.add(ea.getRealAmount());
  1410. }else if (admin.getPosition().equals("0")){
  1411. zxsCount=zxsCount.add(ea.getRealAmount());
  1412. }else {
  1413. qtCount=qtCount.add(ea.getRealAmount());
  1414. }
  1415. }
  1416. }
  1417. }
  1418. List<OrderYearMaxDuration> yearMaxDurationList = orderYearMaxDurationMapper.selectByOrderNo(e.getOrderNo());
  1419. OrderYearMaxDuration useOyd = null;
  1420. for (int i = 0; i < yearMaxDurationList.size(); i++) {
  1421. OrderYearMaxDuration oyd = yearMaxDurationList.get(i);
  1422. //年份为0不是会员直接获取跳出循环,否则获取当前年份,获取不到获取最后一年
  1423. if (oyd.getYear()==0){
  1424. useOyd=oyd;
  1425. break;
  1426. }else {
  1427. if (oyd.getYear().equals(year)){
  1428. useOyd = oyd;
  1429. break;
  1430. }
  1431. if(i==yearMaxDurationList.size()-1&&useOyd==null){
  1432. useOyd = yearMaxDurationList.get(i);
  1433. }
  1434. }
  1435. }
  1436. //获取未归集的订单
  1437. List<BigDecimal> orderNoNotCountAmount = expenseAccountMapper.getOrderNoNotCountAmount(e.getOrderNo());
  1438. BigDecimal countAmount = orderNoNotCountAmount.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
  1439. String str = getString(e, out, count, yxyCount, zxsCount,qtCount,useOyd);
  1440. str=str+",未归集:"+countAmount.stripTrailingZeros().toPlainString()+"元";
  1441. orderList.add(str);
  1442. }else if (e.getPrid() !=null){
  1443. //获取未归集的订单
  1444. List<PublicReleaseDetails> list = publicReleaseMapper.selectDetailsByPrid(e.getPrid());
  1445. StringBuilder sb = new StringBuilder();
  1446. for (PublicReleaseDetails e2 : list) {
  1447. //不重复就添加到strList并且计算归集
  1448. if (!strList.contains(e2.getUid())){
  1449. strList.add(e2.getUid());
  1450. User user = userMapper.queryById(e2.getUid());
  1451. List<BigDecimal> orderNoNotCountAmount = expenseAccountMapper.getUidNotCountAmount(e2.getUid());
  1452. BigDecimal countAmount = orderNoNotCountAmount.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
  1453. sb.append("[").append(user.getNickname()).append("]未归集:").append(countAmount.stripTrailingZeros().toPlainString()).append("元,");
  1454. }
  1455. }
  1456. if (sb.length()>1){
  1457. orderList.add(sb.substring(0,sb.length()-1));
  1458. }
  1459. }
  1460. res.setStrList(orderList);
  1461. }
  1462. }
  1463. private String getString(SonExpenseAccount e, TOrderPublicReleaseCount out, BigDecimal count, BigDecimal yxyCount,
  1464. BigDecimal zxsCount, BigDecimal qtCount, OrderYearMaxDuration useOyd) {
  1465. TOrderPublicReleaseCountBo bo = new TOrderPublicReleaseCountBo();
  1466. // if (out==null)return String.format("订单%s统计信息不存在,请要营销员先将订单派单。",e.getOrderNo());
  1467. // bo.setMaxDuration(out.getMaxDuration());
  1468. // BigDecimal maxDuration = BigDecimal.valueOf(out.getMaxDuration());
  1469. // bo.setExceedDuration(maxDuration.subtract(BigDecimal.valueOf(out.getActualDuration())).abs());
  1470. // bo.setActualDuration(out.getActualDuration());
  1471. // bo.setFrequency(out.getFrequency());
  1472. // bo.setPeopleCount(out.getPeopleCount());
  1473. // String maxString="";
  1474. // String actualString=",超过" + bo.getExceedDuration() + "小时,";
  1475. // if (out.getMaxType()==0){
  1476. // maxString=":原则上不能公出";
  1477. // maxString=":原则上不能公出";
  1478. // }else if (out.getMaxType()==1){
  1479. // maxString=":应公出" + bo.getMaxDuration() +"小时";
  1480. // }else if (out.getMaxType()==2){
  1481. // maxString=":无限制公出,预估" + bo.getMaxDuration() +"小时";
  1482. // }
  1483. // if (bo.getExceedDuration().compareTo(BigDecimal.ZERO) <1){
  1484. // actualString="";
  1485. // }
  1486. if (out==null||useOyd==null){
  1487. TOrderNewBo orderNewBo = orderNewService.getOrderNewDetail(e.getOrderNo());
  1488. return String.format("%s+%s派单进行中(无法统计公出信息)",orderNewBo.getNickname(),orderNewBo.getContractNo());
  1489. }
  1490. bo.setMaxDuration(useOyd.getMaxDuration().doubleValue());
  1491. bo.setExceedDuration(useOyd.getMaxDuration().subtract(BigDecimal.valueOf(out.getActualDuration())).abs());
  1492. bo.setActualDuration(out.getActualDuration());
  1493. bo.setFrequency(out.getFrequency());
  1494. bo.setPeopleCount(out.getPeopleCount());
  1495. String yearStr="";
  1496. if (useOyd.getYear()>0){
  1497. yearStr="["+useOyd.getYear()+"年]";
  1498. }
  1499. String maxString=":应公出" + bo.getMaxDuration() +"小时"+yearStr;
  1500. String actualString=",超过" + bo.getExceedDuration() + "小时,";
  1501. if (useOyd.getStatus()==1){
  1502. maxString=":不限制公出小时"+yearStr;
  1503. actualString="";
  1504. }
  1505. StringBuilder expenseString=new StringBuilder().append("报销").append(count.stripTrailingZeros().toPlainString()).append("元");
  1506. if (yxyCount.compareTo(BigDecimal.ZERO)>0 || zxsCount.compareTo(BigDecimal.ZERO)>0||qtCount.compareTo(BigDecimal.ZERO)>0){
  1507. expenseString.append("(");
  1508. if (zxsCount.compareTo(BigDecimal.ZERO) > 0) {
  1509. expenseString.append("技术").append(zxsCount.stripTrailingZeros().toPlainString()).append("元,");
  1510. }
  1511. if (yxyCount.compareTo(BigDecimal.ZERO) > 0){
  1512. expenseString.append("营销").append(yxyCount.stripTrailingZeros().toPlainString()).append("元,");
  1513. }
  1514. if (qtCount.compareTo(BigDecimal.ZERO) > 0){
  1515. expenseString.append("其他").append(qtCount.stripTrailingZeros().toPlainString()).append("元,");
  1516. }
  1517. expenseString.deleteCharAt(expenseString.length() - 1);
  1518. expenseString.append(")");
  1519. }
  1520. if (BigDecimal.valueOf(bo.getMaxDuration()).compareTo(BigDecimal.valueOf(bo.getActualDuration()))>0){
  1521. actualString="";
  1522. }
  1523. return e.getBuyerName() + e.getContractNo() +
  1524. maxString +
  1525. ",已" + out.getPeopleCount() + "人公出," +
  1526. out.getFrequency() + "次" +
  1527. "," + out.getActualDuration() + "小时" +
  1528. actualString+expenseString;
  1529. }
  1530. @Override
  1531. public Object updateMain(MainExpenseAccount in) {
  1532. OutExpenseAccount outExpenseAccount = expenseAccountMapper.selectByid(in.getId());
  1533. if (outExpenseAccount != null&&outExpenseAccount.getStatus()!=0&&outExpenseAccount.getStatus()!=3) {
  1534. throw new BusinessException("主项目已发起,不可修改!");
  1535. }
  1536. ExpenseAccount update =new ExpenseAccount();
  1537. if (in.getStatus()==null)in.setStatus(1);
  1538. if (in.getStatus()==1){
  1539. if (in.getPayDep()!=null)update.setPayDep(in.getPayDep());
  1540. if (in.getApplyDep()!=null)update.setApplyDep(in.getApplyDep());
  1541. if (in.getRemarks()!=null)update.setRemarks(in.getRemarks());
  1542. if (in.getEaaid()!=null)update.setEaaid(in.getEaaid());
  1543. addExpenseAccountExamine(in.getId(),update);
  1544. if (outExpenseAccount != null && outExpenseAccount.getStatus() == 0) {
  1545. addExpenseAccountLogAndNoticEmail(outExpenseAccount, outExpenseAccount.getStatus(), "发起报销审核");
  1546. }
  1547. update.setStatus(in.getStatus());
  1548. if (outExpenseAccount != null) {
  1549. update.setProcessStatus(outExpenseAccount.getProcessStatus());
  1550. }
  1551. update.setId(in.getId());
  1552. if (outExpenseAccount != null) {
  1553. update.setExamineName(outExpenseAccount.getExamineName());
  1554. }
  1555. expenseAccountMapper.updateByPrimaryKeySelective(update);
  1556. // expenseAccountMapper.updateByOrderRemarks(in.getId());
  1557. }else if (in.getStatus()==4){
  1558. expenseAccountMapper.updateByMainId(in.getId(),0);
  1559. addExpenseAccountLog(in.getId(),4,0,TokenManager.getAdminId(),"撤销报销审核");
  1560. List<ExpenseRelationship> list = expenseRelationshipMapper.selectById(in.getId());
  1561. //撤销日志处理,先将日志改在挂子编号上,在新发起的时候再从自编号加到新编号上
  1562. if (list.size()==1){
  1563. ExpenseRelationship e =list.get(0);
  1564. expenseAccountLogMapper.updateUseLogByUseId(e.getEaId(),e.getId());
  1565. }else {
  1566. List<OutExpenseAccountLog> outExpenseAccountLogs = expenseAccountLogMapper.selectByEaid(in.getId());
  1567. for (ExpenseRelationship e : list) {
  1568. for (OutExpenseAccountLog eal : outExpenseAccountLogs) {
  1569. eal.setEaid(e.getEaId());
  1570. }
  1571. expenseAccountLogMapper.insertBatch(outExpenseAccountLogs);
  1572. }
  1573. }
  1574. expenseAccountLogMapper.deleteByeaid(in.getId());
  1575. expenseRelationshipMapper.deleteByPrimaryKey(in.getId());
  1576. expenseAccountMapper.deleteByPrimaryKey(in.getId());
  1577. }
  1578. return 1;
  1579. }
  1580. @Override
  1581. public Object mainList(ExpenseMainListInput in) {
  1582. Map<String,Object> params = new HashMap<>();
  1583. if (in.getStatus()!=null) params.put("status",in.getStatus());
  1584. if (in.getCheckNo()!=null) params.put("checkNo",in.getCheckNo());
  1585. if (in.getListStatus()!=null) params.put("listStatus",in.getListStatus());
  1586. if (in.getUsername() != null)params.put("username",in.getUsername());
  1587. if (in.getStartTime()!=null && in.getEndTime() !=null) {
  1588. params.put("startTime",in.getStartTime());
  1589. params.put("endTime",in.getEndTime()+" 23:59:59");
  1590. }
  1591. //1-3s是不同人的审核
  1592. if (in.getProcessStatus()!=null&&in.getProcessStatus()==1){
  1593. // 财务审核,复审,出纳都是财务与经理
  1594. if(TokenManager.hasRole(AFTConstants.FINANCE_ADMIN)){
  1595. in.setProcessStatus(6);
  1596. }else if (TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION_ASSISTANT)){
  1597. in.setProcessStatus(5);
  1598. }else if(TokenManager.hasRole(AFTConstants.VICE_CEO)){
  1599. //先不做财务查看权限
  1600. in.setProcessStatus(7);
  1601. }else if (TokenManager.hasRole(AFTConstants.SALESMAN_MANAGER)){
  1602. in.setProcessStatus(1);
  1603. }else if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)){
  1604. in.setProcessStatus(2);
  1605. }else if (TokenManager.hasRole(AFTConstants.FINANCE)||TokenManager.hasRole(AFTConstants.FINANCE_MANAGER)){
  1606. in.setProcessStatus(3);
  1607. }
  1608. }else {
  1609. in.setProcessStatus(0);
  1610. }
  1611. if (in.getProcessStatus()!=null){
  1612. params.put("aid",TokenManager.getAdminId());
  1613. params.put("roleType",in.getProcessStatus());
  1614. if (in.getProcessStatus()==3){
  1615. params.put("deps",departmentService.selectMyDeps());
  1616. }
  1617. }
  1618. Pagination<?> page = findPage("ExpenseMainList", "ExpenseMainCount", params, in.getPageNo(), in.getPageSize());
  1619. List<MainExpenseAccount> list = (List<MainExpenseAccount>) page.getList();
  1620. if (!list.isEmpty()){
  1621. List<Integer> collect = list.stream().map(MainExpenseAccount::getId).collect(Collectors.toList());
  1622. List<MainExpenseAccount> mainExpenseAccounts = expenseAccountMapper.selectSonByMainIds(collect);
  1623. for (MainExpenseAccount e : list) {
  1624. for (MainExpenseAccount me : mainExpenseAccounts) {
  1625. if (e.getId().equals(me.getId())){
  1626. e.setSonList(me.getSonList());
  1627. }
  1628. }
  1629. }
  1630. }
  1631. return page;
  1632. }
  1633. @Override
  1634. public Pagination<?> detailsList(InputDetailsListBo in) {
  1635. Map<String,Object> params = new HashMap<>();
  1636. if(in.getStatus()!=null)params.put("status",in.getStatus());
  1637. if(in.getType()!=null)params.put("type",in.getType());
  1638. if (in.getDepId()!=null){
  1639. ArrayList<Object> deps = new ArrayList<>();
  1640. deps.add(in.getDepId());
  1641. params.put("deps",deps);
  1642. }
  1643. if (in.getContractNo()!=null)params.put("contractNo",in.getContractNo());
  1644. if (in.getBuyerName()!=null)params.put("buyerName",in.getBuyerName());
  1645. if (in.getAid()!=null)params.put("aid",in.getAid());
  1646. if (in.getApplyDep()!=null)params.put("applyDep",in.getApplyDep());
  1647. if (in.getPayDep() != null)params.put("payDep",in.getPayDep());
  1648. if (in.getStartTime()!=null)params.put("startTime",in.getStartTime());
  1649. if (in.getEndTime()!=null)params.put("endTime",in.getEndTime());
  1650. if (in.getCheckNo()!=null)params.put("checkNo",in.getCheckNo());
  1651. params.put("current",TokenManager.getAdminId());
  1652. if (in.getMyType()!=null){
  1653. params.put("myType",in.getMyType());
  1654. }else {
  1655. params.put("myType",0);
  1656. }
  1657. //0=自己 1=财务 2=财务经理 3=超管
  1658. if (TokenManager.hasRole(AFTConstants.SUPERADMIN)||TokenManager.hasRole(AFTConstants.FINANCE_ADMIN)){
  1659. params.put("roleType",3);
  1660. }else if (TokenManager.hasRole(AFTConstants.FINANCE_MANAGER)){
  1661. params.put("roleType",2);
  1662. }else if (TokenManager.hasRole(AFTConstants.FINANCE)){
  1663. params.put("roleType",1);
  1664. params.put("current",TokenManager.getAdminId());
  1665. }else{
  1666. params.put("roleType",0);
  1667. params.put("aid",TokenManager.getAdminId());
  1668. }
  1669. return findPagePlus("ExpenseDetailsList","ExpenseDetailsCount","ExpenseDetailsTotalAmount",params,in.getPageNo(),in.getPageSize());
  1670. }
  1671. @Override
  1672. public List<ExpenseAccount> selectByIds(List<String> list) {
  1673. return expenseAccountMapper.selectByIds(list);
  1674. }
  1675. @Override
  1676. public int selectCountByIdsAndType(List<String> list, Integer i) {
  1677. return expenseAccountMapper.selectCountByIdsAndType(list,i);
  1678. }
  1679. @Override
  1680. public Object updateRealAmount(Integer type, Integer id, BigDecimal amount) {
  1681. ExpenseAccountDetails ea= new ExpenseAccountDetails();
  1682. ea.setId(id);
  1683. ea.setRealAmount(amount);
  1684. String name= Objects.requireNonNull(TokenManager.getAdminToken()).getName();
  1685. Integer eaid = expenseAccountDetailsMapper.selectExpenseAccountById(id);
  1686. addExpenseAccountLog(eaid,5,3,TokenManager.getAdminId(),String.format("财务[%s]修改实际报销金额为%s。",name,amount));
  1687. expenseAccountDetailsMapper.updateByPrimaryKeySelective(ea);
  1688. expenseAccountMapper.updateSonByDetId(id);
  1689. expenseAccountMapper.updateMainByDetId(id);
  1690. return 1;
  1691. }
  1692. /**
  1693. * 计算当前报销显示抵扣金额
  1694. */
  1695. private void pushExpenseAccountGetDebitAmount(OutExpenseAccount useEa) {
  1696. ExpenseAccount debit = expenseAccountMapper.selectByPrimaryKey(useEa.getDebitId());
  1697. List<ExpenseAccount> list1 = expenseAccountMapper.selectByDebitId(useEa.getDebitId());
  1698. BigDecimal count =debit.getRealAmount();
  1699. for (ExpenseAccount e : list1) {
  1700. if (!e.getId().equals(useEa.getId())){
  1701. count=count.subtract(e.getSettlementAmount());
  1702. }
  1703. }
  1704. if (count.compareTo(new BigDecimal(0))<0)count=new BigDecimal(0);
  1705. useEa.setDebitTotalAmount(count);
  1706. }
  1707. /**
  1708. * 判断角色是否已经审核
  1709. * @param id 编号
  1710. * @param processStatus 审核状态
  1711. * @return 0 无需审核 1 需要审核
  1712. */
  1713. private Integer pushExpenseAccountGetExamine(Integer id,Integer processStatus) {
  1714. List<ExpenseAccountExamine> list = expenseAccountExamineMapper.selectByEaidAndProcessStatus(id, processStatus);
  1715. boolean flag=false;
  1716. for (ExpenseAccountExamine e : list) {
  1717. if (e.getAuditor().equals(TokenManager.getAdminId())){
  1718. if (e.getStatus()==0){
  1719. flag=true;
  1720. break;
  1721. }
  1722. }
  1723. }
  1724. if (flag){
  1725. return 1;
  1726. }else {
  1727. return 0;
  1728. }
  1729. }
  1730. @Override
  1731. public Object updateType(InputExpenseAccount in) {
  1732. expenseAccountDetailsMapper.deleteByeaid(in.getId());
  1733. if (in.getType()!=2)in.setSecondaryType(0);
  1734. in.setSettlementAmount(new BigDecimal(0));
  1735. return expenseAccountMapper.updateByPrimaryKeySelective(in);
  1736. }
  1737. @Override
  1738. public List<OutExpenseAccount> selectDebitOrder(String depId,Integer id) {
  1739. String aid= TokenManager.getAdminId();
  1740. return expenseAccountMapper.selectByaidAndType(aid, depId, 4,id);
  1741. }
  1742. private void addParam(InputPageListBo in, Map<String, Object> param) {
  1743. if (in.getStartTime()!=null&&in.getEndTime()!=null){
  1744. param.put("startTime",in.getStartTime());
  1745. param.put("endTime",in.getEndTime()+" 23:59:59");
  1746. }
  1747. if (in.getStatus()!=null)param.put("status",in.getStatus());
  1748. //如果前端未传 0自己 1审核的参数,默认自己,在审核列表的时候判断Aid,在
  1749. param.put("auditor",TokenManager.getAdminId());
  1750. if(in.getProcessStatus()==null){
  1751. if(TokenManager.hasRole(AFTConstants.FINANCE_MANAGER)){
  1752. //财务经理查看权限
  1753. in.setProcessStatus(5);
  1754. param.put("auditor",TokenManager.getAdminId());
  1755. }else if(TokenManager.hasRole(AFTConstants.FINANCE_ADMIN)||TokenManager.hasRole(AFTConstants.SUPERADMIN)||
  1756. TokenManager.hasRole(AFTConstants.VICE_CEO)|| TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)){
  1757. //财务管理员和超级管理员查看全部
  1758. in.setProcessStatus(6);
  1759. param.put("auditor",TokenManager.getAdminId());
  1760. if(StringUtils.isNotBlank(in.getAid()))param.put("aid",in.getAid());
  1761. }else if (TokenManager.hasRole(AFTConstants.FINANCE)){
  1762. //0 设置为财务查看
  1763. param.put("auditor",TokenManager.getAdminId());
  1764. //如果是空设置为
  1765. if (in.getProcessStatus()==null)in.setProcessStatus(2);
  1766. }else {
  1767. in.setProcessStatus(0);
  1768. }
  1769. }
  1770. param.put("processStatus",in.getProcessStatus());
  1771. if (in.getRoleType()==null){
  1772. param.put("roleType",0);
  1773. }else {
  1774. param.put("roleType",in.getRoleType());
  1775. }
  1776. if(in.getProcessStatus()==2||in.getProcessStatus()==1){
  1777. if (in.getAid()!=null){
  1778. param.put("aid",in.getAid());
  1779. }
  1780. }
  1781. if (in.getExamineStatus()!=null){
  1782. param.put("examineStatus",in.getExamineStatus());
  1783. }else {
  1784. param.put("examineStatus",0);
  1785. }
  1786. if (in.getContractNo()!=null)param.put("contractNo",in.getContractNo());
  1787. if (in.getDepId()!=null)param.put("depId",in.getDepId());
  1788. if (in.getApplyDep()!=null)param.put("applyDep",in.getApplyDep());
  1789. if (in.getPayDep()!=null)param.put("payDep",in.getPayDep());
  1790. if (in.getType()!=null)param.put("type",in.getType());
  1791. if (in.getUsername()!=null)param.put("username",in.getUsername());
  1792. if (in.getCheckNo()!=null)param.put("checkNo",in.getCheckNo());
  1793. if (in.getSecondaryType()!=null)param.put("secondaryType",in.getSecondaryType());
  1794. //固定传0查看子列表
  1795. if (in.getExpenseMain()==null)in.setExpenseMain(0);
  1796. if (in.getExpenseMain()!=null)param.put("expenseMain",in.getExpenseMain());
  1797. }
  1798. @Override
  1799. @Cacheable(value = "expenseAccountStatistics#300", key = "'page:'+#name+',depId'+#depId+',startTime'+#startTime+',endTime'+#endTime+'lander'+#lander")
  1800. public List<OutExpenseAccountStatistics> statistics(String name,String depId,String startTime,String endTime,String lander) {
  1801. if (endTime != null) endTime = endTime + " 23:59:59";
  1802. List<OutMyEAStatistics> list = expenseAccountMapper.selectStatistics(name, depId, startTime, endTime, lander);
  1803. List<OutExpenseAccountStatistics> results = new ArrayList<>();
  1804. Map<String, Object> map = new HashMap<>();
  1805. for (OutMyEAStatistics e : list) {
  1806. OutExpenseAccountStatistics out = new OutExpenseAccountStatistics(e.getAid());
  1807. out.setAname(e.getAname());
  1808. if (e.getStatus() == 2) {
  1809. if (e.getType() < 4) {
  1810. out.setBx(1);
  1811. out.setBxAmount(e.getTotalAmount());
  1812. if (!e.getDebitId().equals(0)) {
  1813. out.setDk(1);
  1814. out.setDkAmount(e.getBxdkAmount());
  1815. }
  1816. } else if (e.getType() == 4) {
  1817. out.setJz(1);
  1818. out.setJzAmount(e.getTotalAmount());
  1819. } else if (e.getType() == 5) {
  1820. out.setDk(1);
  1821. out.setDkAmount(e.getTotalAmount());
  1822. }
  1823. if (e.getTargetType() == 0) {
  1824. out.setGdfyAmount(e.getTotalAmount());
  1825. } else if (e.getTargetType() == 1) {
  1826. out.setBxddAmount(e.getTotalAmount());
  1827. }
  1828. } else if (e.getStatus() == 1) {
  1829. out.setWshs(1);
  1830. } else if (e.getStatus() == 3) {
  1831. out.setBhs(1);
  1832. }
  1833. //如果不存在就新增
  1834. if (!map.containsKey(out.getAid())) {
  1835. List<OutExpenseAccountStatistics> list2 = new ArrayList<>();
  1836. list2.add(out);
  1837. map.put(out.getAid(), list2);
  1838. } else {
  1839. List<OutExpenseAccountStatistics> list2 = (List<OutExpenseAccountStatistics>) map.get(e.getAid());
  1840. list2.add(out);
  1841. }
  1842. }
  1843. for (String s : map.keySet()) {
  1844. List<OutExpenseAccountStatistics> list3 = (List<OutExpenseAccountStatistics>) map.get(s);
  1845. if (!list3.isEmpty()) {
  1846. OutExpenseAccountStatistics out = new OutExpenseAccountStatistics(s);
  1847. for (OutExpenseAccountStatistics e : list3) {
  1848. if (out.getAname() == null) out.setAname(e.getAname());
  1849. if (e.getBx() != null) out.setBx(out.getBx() + e.getBx());
  1850. if (e.getJz() != null) out.setJz(out.getJz() + e.getJz());
  1851. if (e.getDk() != null) out.setDk(out.getDk() + e.getDk());
  1852. if (e.getBxAmount() != null) out.setBxAmount(out.getBxAmount().add(e.getBxAmount()));
  1853. if (e.getJzAmount() != null) out.setJzAmount(out.getJzAmount().add(e.getJzAmount()));
  1854. if (e.getDkAmount() != null) out.setDkAmount(out.getDkAmount().add(e.getDkAmount()));
  1855. if (e.getGdfyAmount() != null) out.setGdfyAmount(out.getGdfyAmount().add(e.getGdfyAmount()));
  1856. if (e.getBxddAmount() != null) out.setBxddAmount(out.getBxddAmount().add(e.getBxddAmount()));
  1857. if (e.getWshs() != null) out.setWshs(out.getWshs() + e.getWshs());
  1858. if (e.getBhs() != null) out.setBhs(out.getBhs() + e.getBhs());
  1859. }
  1860. BigDecimal totalAmount = new BigDecimal(0);
  1861. totalAmount = totalAmount.add(out.getBxAmount()).add(out.getJzAmount()).subtract(out.getDkAmount());
  1862. out.setTotalAmount(totalAmount);
  1863. results.add(out);
  1864. }
  1865. }
  1866. return results;
  1867. }
  1868. @Override
  1869. @CacheEvict(value = "expenseAccountStatistics#300", allEntries = true)
  1870. public void statisticsEvict(String name, String depId, String startTime, String endTime) {
  1871. LoggerUtils.debug(getClass(), "报销统计缓存清除。");
  1872. }
  1873. @Override
  1874. public Object pushSupplementCheckNo() {
  1875. List<ExpenseAccount> list = expenseAccountMapper.selectByCheckNoNot();
  1876. if (!list.isEmpty()){
  1877. list.forEach(e ->{
  1878. ExpenseAccount ea=new ExpenseAccount();
  1879. ea.setId(e.getId());
  1880. ea.setCheckNo(idGenerator.getNOgenerateId());
  1881. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  1882. });
  1883. return 1;
  1884. }
  1885. return 0;
  1886. }
  1887. @Override
  1888. public void pushAll() {
  1889. List<ExpenseAccount> list = expenseAccountMapper.selectAllMain();
  1890. for (ExpenseAccount ea : list) {
  1891. List<ExpenseAccount> expenseAccounts = expenseAccountMapper.selectListByMainId(ea.getId());
  1892. List<String> typeList=new ArrayList<>();
  1893. for (ExpenseAccount e : expenseAccounts) {
  1894. StringBuilder types=new StringBuilder();
  1895. if (e.getType()!=0){
  1896. types.append(EATypes.getDescByCode(e.getType()));
  1897. }else {
  1898. types.append(e.getTypeOther());
  1899. }
  1900. if (e.getType()==2){
  1901. if (e.getSecondaryType()==0){
  1902. types.append("-").append(e.getSecondaryTypeOther());
  1903. }else{
  1904. String descByCode = EAsecondaryTypes.getDescByCode(e.getSecondaryType());
  1905. types.append("-").append(descByCode);
  1906. }
  1907. }
  1908. if (!typeList.contains(types.toString()))typeList.add(types.toString());
  1909. }
  1910. String typeStr = String.join(",", typeList);
  1911. ExpenseAccount update=new ExpenseAccount();
  1912. update.setId(ea.getId());
  1913. update.setTypeOther(typeStr);
  1914. expenseAccountMapper.updateByPrimaryKeySelective(update);
  1915. }
  1916. }
  1917. }