ExpenseAccountServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package com.goafanti.expenseAccount.service.impl;
  2. import com.goafanti.admin.bo.AdminListBo;
  3. import com.goafanti.admin.service.DepartmentService;
  4. import com.goafanti.common.bo.AdminNoticeBo;
  5. import com.goafanti.common.bo.EmailBo;
  6. import com.goafanti.common.constant.AFTConstants;
  7. import com.goafanti.common.dao.*;
  8. import com.goafanti.common.enums.NoticeStatus;
  9. import com.goafanti.common.enums.NoticeTypes;
  10. import com.goafanti.common.error.BusinessException;
  11. import com.goafanti.common.model.*;
  12. import com.goafanti.common.utils.AsyncUtils;
  13. import com.goafanti.common.utils.DateUtils;
  14. import com.goafanti.common.utils.WeChatUtils;
  15. import com.goafanti.core.mybatis.BaseMybatisDao;
  16. import com.goafanti.core.shiro.token.TokenManager;
  17. import com.goafanti.expenseAccount.Enums.EAProcessStatus;
  18. import com.goafanti.expenseAccount.bo.*;
  19. import com.goafanti.expenseAccount.service.ExpenseAccountService;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import java.math.BigDecimal;
  23. import java.util.*;
  24. @Service
  25. public class ExpenseAccountServiceImpl extends BaseMybatisDao<ExpenseAccountMapper> implements ExpenseAccountService {
  26. @Autowired
  27. private AdminMapper adminMapper;
  28. @Autowired
  29. private DepartmentService departmentService;
  30. @Autowired
  31. private ExpenseAccountMapper expenseAccountMapper;
  32. @Autowired
  33. private PublicReleaseMapper publicReleaseMapper;
  34. @Autowired
  35. private ExpenseAccountLogMapper expenseAccountLogMapper;
  36. @Autowired
  37. private AsyncUtils asyncUtils;
  38. @Autowired
  39. private WeChatUtils weChatUtils;
  40. @Autowired
  41. private ExpenseAccountDetailsMapper expenseAccountDetailsMapper;
  42. @Autowired
  43. private ExpenseAccountPrivateMapper expenseAccountPrivateMapper;
  44. @Override
  45. public Integer add(InputExpenseAccount in) {
  46. if (in.getAid()!=null){
  47. Admin admin = adminMapper.selectByPrimaryKey(in.getAid());
  48. in.setAname(admin.getName());
  49. in.setApplyDep(admin.getDepartmentId());
  50. in.setPayDep(admin.getDepartmentId());
  51. }
  52. if (in.getPrid()!=null){
  53. PublicRelease pr = publicReleaseMapper.selectByPrimaryKey(in.getPrid());
  54. in.setReleaseStart(pr.getReleaseStart());
  55. in.setReleaseEnd(pr.getReleaseEnd());
  56. in.setDistrictName(pr.getDistrictName());
  57. in.setUserNames(pr.getUserNames());
  58. in.setDuration(pr.getDuration());
  59. }
  60. Integer eaaid = expenseAccountPrivateMapper.selectByaidDefault(in.getAid());
  61. in.setEaaid(eaaid);
  62. expenseAccountMapper.insertSelective(in);
  63. addExpenseAccountLogAndNoticEmail(in,0,"创建报销");
  64. return in.getId();
  65. }
  66. /**
  67. *
  68. * @param eaid 报销编号
  69. * @param status 状态 0创建 1发起 2通过 3驳回 4修改
  70. * @param str 备注
  71. */
  72. private void addExpenseAccountLog(Integer eaid, Integer status,String aid, String str) {
  73. if (aid==null)aid=TokenManager.getAdminId();
  74. ExpenseAccountLog eaLog=new ExpenseAccountLog();
  75. eaLog.setEaid(eaid);
  76. eaLog.setStatus(status);
  77. eaLog.setRemarks(str);
  78. eaLog.setAuditor(aid);
  79. expenseAccountLogMapper.insertSelective(eaLog);
  80. }
  81. private void addExpenseAccountLogAndNoticEmail(InputExpenseAccount in, Integer status, String str) {
  82. Date date=new Date();
  83. String aid=TokenManager.getAdminId();
  84. addExpenseAccountLog(in.getId(),status,aid,str);
  85. //日志写入用操作人,邮箱、微信订阅与站内信要的是发送方消息,即收件人
  86. AdminNoticeBo an = adminMapper.selectMyAndSuperBySuperId(aid);
  87. StringBuffer sb =new StringBuffer();
  88. if (status>0){
  89. in.setExamineName(an.getSuperName());
  90. sb=sb.append("[").append(an.getName()).append("]").append(str).append(",请及时审核。");
  91. asyncUtils.addNoticAndEmail(NoticeStatus.EXPENSE_NOTICE.getCode(), an.getSuperId(),sb.toString());
  92. weChatUtils.addExpenseAccountWeChatNotice(an.getExpenseOpenId(), 1, in.getId(), date, an.getName(),sb.toString());
  93. }
  94. }
  95. @Override
  96. public int update(InputExpenseAccount in) {
  97. OutExpenseAccount useEA = expenseAccountMapper.selectByid(in.getId());
  98. if (useEA.getStatus()!=0&&useEA.getStatus()!=3){
  99. throw new BusinessException("不能操作审核中与完成的订单");
  100. }
  101. if (in.getStartTimeStr()!=null&&in.getEndTimeStr()!=null){
  102. in.setReleaseStart(DateUtils.StringToDate(in.getStartTimeStr(), AFTConstants.YYYYMMDDHHMMSS));
  103. in.setReleaseEnd(DateUtils.StringToDate(in.getEndTimeStr(), AFTConstants.YYYYMMDDHHMMSS));
  104. }
  105. if(in.getStatus()==1){
  106. in.setProcessStatus(1);
  107. }
  108. //计算总金额
  109. in.setTotalAmount(expenseAccountDetailsMapper.selectAmountCountByEAid(in.getId()));
  110. if (useEA.getStatus()==0){
  111. addExpenseAccountLogAndNoticEmail(in,1,"发起报销审核");
  112. }else if (useEA.getStatus()==3){
  113. addExpenseAccountLogAndNoticEmail(in,4,"修改并发起审核");
  114. }
  115. return expenseAccountMapper.updateByPrimaryKeySelective(in);
  116. }
  117. @Override
  118. public int addDetails(InputExpenseAccountDetails in) {
  119. return expenseAccountDetailsMapper.insertSelective(in);
  120. }
  121. @Override
  122. public int deleteDetails(Integer id) {
  123. return expenseAccountDetailsMapper.deleteByPrimaryKey(id);
  124. }
  125. @Override
  126. public List<OutExpenseAccountDetails> detailsList(Integer eaid) {
  127. return expenseAccountDetailsMapper.selectByEAid(eaid);
  128. }
  129. @Override
  130. public Object logList(Integer eaid) {
  131. return expenseAccountLogMapper.selectByEaid(eaid);
  132. }
  133. @Override
  134. public ExpenseAccountExamineLog examineLog(Integer eaid) {
  135. return expenseAccountLogMapper.selectExamineLog(eaid);
  136. }
  137. @Override
  138. public int updateExamine(InputExpenseAccount in) {
  139. ExpenseAccount useEa = expenseAccountMapper.selectByPrimaryKey(in.getId());
  140. ExpenseAccount newEa=new ExpenseAccount();
  141. newEa.setId(useEa.getId());
  142. if (useEa.getStatus()==0)throw new BusinessException("审核状态错误");
  143. if (useEa.getProcessStatus()!=in.getProcessStatus())throw new BusinessException(String.format("审核流程错误use=%s",useEa.getProcessStatus()));
  144. //审核通知 获取发送人
  145. addExamineExpenseAccountLog(in, useEa, newEa);
  146. //完成申请
  147. if (in.getStatus()==2){
  148. //如果是借支则订单新增
  149. if (in.getType()==4){
  150. }else {
  151. //不然就看是否挂载借支订单进行抵扣
  152. if (useEa.getDebitId()!=null){
  153. ExpenseAccount useDebit=expenseAccountMapper.selectByPrimaryKey(useEa.getDebitId());
  154. ExpenseAccount debit=new ExpenseAccount();
  155. debit.setId(useEa.getDebitId());
  156. BigDecimal settlementAmount = useDebit.getSettlementAmount();
  157. settlementAmount=settlementAmount.add(useEa.getTotalAmount());
  158. //如果不大于金额
  159. if (settlementAmount.compareTo(useDebit.getTotalAmount())<1){
  160. //计算抵扣后的已付
  161. debit.setSettlementAmount(settlementAmount);
  162. //设置当前报销已付全额
  163. newEa.setSettlementAmount(useEa.getTotalAmount());
  164. }else {
  165. BigDecimal subtract = settlementAmount.subtract(useDebit.getTotalAmount());
  166. newEa.setSettlementAmount(subtract);
  167. debit.setSettlementAmount(useDebit.getTotalAmount());
  168. }
  169. expenseAccountMapper.updateByPrimaryKeySelective(debit);
  170. }
  171. }
  172. }
  173. expenseAccountMapper.updateByPrimaryKeySelective(newEa);
  174. return 1;
  175. }
  176. private void addExamineExpenseAccountLog(InputExpenseAccount in, ExpenseAccount useEa, ExpenseAccount newEa) {
  177. List<Admin> aids=new ArrayList<>();
  178. StringBuffer str=new StringBuffer();
  179. Integer status=in.getStatus();
  180. AdminListBo admin = adminMapper.getDeptNameByAid(useEa.getAid());
  181. if (status==1){
  182. str=str.append("您有报销需要审核,").append("报销编号[").append(in.getId()).append("]").append(",请查看并审核。");
  183. if(useEa.getProcessStatus()== EAProcessStatus.FQ.getCode()){
  184. //获取上级
  185. Admin superAdmin = adminMapper.selectByPrimaryKey(admin.getSuperiorId());
  186. aids.add(superAdmin);
  187. newEa.setProcessStatus(EAProcessStatus.SJSH.getCode());
  188. newEa.setUserNames(superAdmin.getName());
  189. }else if (useEa.getProcessStatus()== EAProcessStatus.SJSH.getCode()){
  190. //获取财务负责人
  191. Admin finaceAdmin = adminMapper.selectByPrimaryKey(admin.getDepFinance());
  192. aids.add(finaceAdmin);
  193. newEa.setProcessStatus(EAProcessStatus.CWSH.getCode());
  194. newEa.setExamineName(finaceAdmin.getName());
  195. }else if (useEa.getProcessStatus()== EAProcessStatus.CWSH.getCode()){
  196. //获取部门负责人
  197. List<Admin> list = adminMapper.listAdminBydepIdAndRoleType(admin.getDepartmentId(), AFTConstants.SALESMAN_ADMIN);
  198. if (!list.isEmpty())aids.addAll(list);
  199. newEa.setProcessStatus(EAProcessStatus.ZJLSH.getCode());
  200. newEa.setExamineName(getAdmins(list));
  201. }else if (useEa.getProcessStatus()== EAProcessStatus.ZJLSH.getCode()){
  202. //获取总裁、董事长等审核
  203. List<Admin> list = adminMapper.selectAdminByRoleType(AFTConstants.All_CED_AND_CHAIRMAN);
  204. if (!list.isEmpty())aids.addAll(list);
  205. newEa.setProcessStatus(EAProcessStatus.DSZSH.getCode());
  206. newEa.setExamineName(getAdmins(list));
  207. }else if (useEa.getProcessStatus()== EAProcessStatus.DSZSH.getCode()){
  208. //获取报销申请人
  209. status =2;
  210. aids.add(adminMapper.selectByPrimaryKey(admin.getId()));
  211. newEa.setStatus(status);
  212. newEa.setExamineName("");
  213. str.setLength(0);
  214. str=str.append("您的报销审核已通过,报销编号[").append(in.getId()).append("],请注意查看。");
  215. }
  216. }else {
  217. aids.add(adminMapper.selectByPrimaryKey(admin.getId()));
  218. newEa.setStatus(3);
  219. str.setLength(0);
  220. str=str.append("您的报销审核已驳回,报销编号[").append(in.getId()).append("],请注意查看。");
  221. }
  222. if (newEa.getStatus()==null)newEa.setStatus(1);
  223. addLogAndNoticeAndEmail(in, status,str.toString(),admin.getName(),aids);
  224. }
  225. private String getAdmins(List<Admin> list) {
  226. StringBuffer str =new StringBuffer();
  227. for (Admin admin : list) {
  228. str=str.append(admin.getName()).append(",");
  229. }
  230. return str.substring(0,str.length()-1);
  231. }
  232. private void addLogAndNoticeAndEmail(InputExpenseAccount in, Integer status, String str,String aname, List<Admin> aids) {
  233. Date date= new Date();
  234. addExpenseAccountLog(in.getId(),status,null,in.getReason());
  235. Integer noticeStatus=NoticeStatus.EXPENSE_NOTICE.getCode();
  236. if (status==2){
  237. noticeStatus=NoticeStatus.EXPENSE_COMPLETE.getCode();
  238. }
  239. List<Notice> list=new ArrayList<>();
  240. if (aids!=null){
  241. for (Admin a : aids) {
  242. Notice notice=new Notice();
  243. notice.setId(UUID.randomUUID().toString());
  244. notice.setNoticeType(noticeStatus);
  245. notice.setType(NoticeTypes.getType(noticeStatus));
  246. notice.setAid(a.getId());
  247. notice.setCreateTime(date);
  248. notice.setContent(str);
  249. notice.setReaded(0);
  250. list.add(notice);
  251. EmailBo emailBo=new EmailBo(NoticeStatus.getValueByCode(noticeStatus),a.getEmail(),str);
  252. asyncUtils.send(emailBo);
  253. weChatUtils.addExpenseAccountWeChatNotice(a.getExpenseOpenId(),status,in.getId(),date,aname,str);
  254. }
  255. asyncUtils.addNoticeBatch(list);
  256. }
  257. }
  258. @Override
  259. public Object pageList(InputPageListBo in) {
  260. Map<String ,Object> param=new HashMap<>();
  261. addParam(in, param);
  262. return findPage("selectExpenseAccountList", "selectExpenseAccountCount", param, in.getPageNo(), in.getPageSize());
  263. }
  264. @Override
  265. public Object selectByPrid(InputExpenseAccount in) {
  266. Integer id=null;
  267. in.setAid(TokenManager.getAdminId());
  268. OutExpenseAccount useEa = expenseAccountMapper.selectByaidAndPrid(in);
  269. if (useEa!=null&&useEa.getId()!=null)id=useEa.getId();
  270. return id;
  271. }
  272. @Override
  273. public Object selectById(Integer id) {
  274. OutExpenseAccount useEa = expenseAccountMapper.selectByid(id);
  275. return useEa;
  276. }
  277. @Override
  278. public Object updateType(InputExpenseAccount in) {
  279. expenseAccountDetailsMapper.deleteByeaid(in.getId());
  280. return expenseAccountMapper.updateByPrimaryKeySelective(in);
  281. }
  282. @Override
  283. public List<OutExpenseAccount> selectDebitOrder(String depId) {
  284. String aid= TokenManager.getAdminId();
  285. return expenseAccountMapper.selectByaidAndType(aid,depId,4);
  286. }
  287. private void addParam(InputPageListBo in, Map<String, Object> param) {
  288. param.put("aid",TokenManager.getAdminId());
  289. if (in.getStartTime()!=null&&in.getEndTime()!=null){
  290. param.put("startTime",in.getStartTime());
  291. param.put("endTime",in.getEndTime()+" 23:59:59");
  292. }
  293. if (in.getStatus()!=null)param.put("status",in.getStatus());
  294. if (in.getProcessStatus()!=null){
  295. Integer processStatus=in.getProcessStatus();
  296. //默认自己 设置上级,判断管理员与总裁等顶级审核
  297. if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)){
  298. processStatus=3;
  299. List<String> list = departmentService.selectMyDeps();
  300. param.put("deps",list);
  301. }else if (TokenManager.hasRole(AFTConstants.FINANCE)){
  302. processStatus=2;
  303. }else if (checkShiroCED()){
  304. processStatus=4;
  305. }
  306. param.put("processStatus",processStatus);
  307. }else {
  308. param.put("processStatus",0);
  309. }
  310. if (in.getContractNo()!=null)param.put("contractNo",in.getContractNo());
  311. if (in.getDepId()!=null)param.put("depId",in.getDepId());
  312. if (in.getUsername()!=null)param.put("username",in.getUsername());
  313. if (in.getApplyDep()!=null)param.put("applyDep",in.getApplyDep());
  314. if (in.getPayDep()!=null)param.put("payDep",in.getPayDep());
  315. if (in.getType()!=null)param.put("type",in.getContractNo());
  316. }
  317. /**
  318. * 检查权限判断是否是总裁、董事长或超级管理员权限
  319. * @return
  320. */
  321. private boolean checkShiroCED() {
  322. if (TokenManager.hasRole(AFTConstants.CED)||TokenManager.hasRole(AFTConstants.CED_ASSISTANT)||
  323. TokenManager.hasRole(AFTConstants.SUPERADMIN)||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION_ASSISTANT)
  324. ||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)){
  325. return true;
  326. }
  327. return false;
  328. }
  329. @Override
  330. public Object statistics(String name,String depId,String startTime,String endTime) {
  331. if (endTime!=null)endTime=endTime+" 23:59:59";
  332. List<OutExpenseAccount> list = expenseAccountMapper.selectStatistics(name, depId, startTime, endTime);
  333. List<OutExpenseAccountStatistics> results=new ArrayList<>();
  334. for (OutExpenseAccount e : list) {
  335. }
  336. return null;
  337. }
  338. }