package com.goafanti.expenseAccount.service.impl; import com.goafanti.admin.bo.AdminListBo; import com.goafanti.admin.service.DepartmentService; import com.goafanti.common.bo.AdminNoticeBo; import com.goafanti.common.bo.EmailBo; import com.goafanti.common.constant.AFTConstants; import com.goafanti.common.dao.*; import com.goafanti.common.enums.NoticeStatus; import com.goafanti.common.enums.NoticeTypes; import com.goafanti.common.error.BusinessException; import com.goafanti.common.model.*; import com.goafanti.common.utils.AsyncUtils; import com.goafanti.common.utils.DateUtils; import com.goafanti.common.utils.WeChatUtils; import com.goafanti.core.mybatis.BaseMybatisDao; import com.goafanti.core.shiro.token.TokenManager; import com.goafanti.expenseAccount.Enums.EAProcessStatus; import com.goafanti.expenseAccount.bo.*; import com.goafanti.expenseAccount.service.ExpenseAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.util.*; @Service public class ExpenseAccountServiceImpl extends BaseMybatisDao implements ExpenseAccountService { @Autowired private AdminMapper adminMapper; @Autowired private DepartmentService departmentService; @Autowired private ExpenseAccountMapper expenseAccountMapper; @Autowired private PublicReleaseMapper publicReleaseMapper; @Autowired private ExpenseAccountLogMapper expenseAccountLogMapper; @Autowired private AsyncUtils asyncUtils; @Autowired private WeChatUtils weChatUtils; @Autowired private ExpenseAccountDetailsMapper expenseAccountDetailsMapper; @Autowired private ExpenseAccountPrivateMapper expenseAccountPrivateMapper; @Override public Integer add(InputExpenseAccount in) { if (in.getAid()!=null){ Admin admin = adminMapper.selectByPrimaryKey(in.getAid()); in.setAname(admin.getName()); in.setApplyDep(admin.getDepartmentId()); in.setPayDep(admin.getDepartmentId()); } if (in.getPrid()!=null){ PublicRelease pr = publicReleaseMapper.selectByPrimaryKey(in.getPrid()); in.setReleaseStart(pr.getReleaseStart()); in.setReleaseEnd(pr.getReleaseEnd()); in.setDistrictName(pr.getDistrictName()); in.setUserNames(pr.getUserNames()); in.setDuration(pr.getDuration()); } Integer eaaid = expenseAccountPrivateMapper.selectByaidDefault(in.getAid()); in.setEaaid(eaaid); expenseAccountMapper.insertSelective(in); addExpenseAccountLogAndNoticEmail(in,0,"创建报销"); return in.getId(); } /** * * @param eaid 报销编号 * @param status 状态 0创建 1发起 2通过 3驳回 4修改 * @param str 备注 */ private void addExpenseAccountLog(Integer eaid, Integer status,String aid, String str) { if (aid==null)aid=TokenManager.getAdminId(); ExpenseAccountLog eaLog=new ExpenseAccountLog(); eaLog.setEaid(eaid); eaLog.setStatus(status); eaLog.setRemarks(str); eaLog.setAuditor(aid); expenseAccountLogMapper.insertSelective(eaLog); } private void addExpenseAccountLogAndNoticEmail(InputExpenseAccount in, Integer status, String str) { Date date=new Date(); String aid=TokenManager.getAdminId(); addExpenseAccountLog(in.getId(),status,aid,str); //日志写入用操作人,邮箱、微信订阅与站内信要的是发送方消息,即收件人 AdminNoticeBo an = adminMapper.selectMyAndSuperBySuperId(aid); StringBuffer sb =new StringBuffer(); if (status>0){ in.setExamineName(an.getSuperName()); sb=sb.append("[").append(an.getName()).append("]").append(str).append(",请及时审核。"); asyncUtils.addNoticAndEmail(NoticeStatus.EXPENSE_NOTICE.getCode(), an.getSuperId(),sb.toString()); weChatUtils.addExpenseAccountWeChatNotice(an.getExpenseOpenId(), 1, in.getId(), date, an.getName(),sb.toString()); } } @Override public int update(InputExpenseAccount in) { OutExpenseAccount useEA = expenseAccountMapper.selectByid(in.getId()); if (useEA.getStatus()!=0&&useEA.getStatus()!=3){ throw new BusinessException("不能操作审核中与完成的订单"); } if (in.getStartTimeStr()!=null&&in.getEndTimeStr()!=null){ in.setReleaseStart(DateUtils.StringToDate(in.getStartTimeStr(), AFTConstants.YYYYMMDDHHMMSS)); in.setReleaseEnd(DateUtils.StringToDate(in.getEndTimeStr(), AFTConstants.YYYYMMDDHHMMSS)); } if(in.getStatus()==1){ in.setProcessStatus(1); } //计算总金额 in.setTotalAmount(expenseAccountDetailsMapper.selectAmountCountByEAid(in.getId())); if (useEA.getStatus()==0){ addExpenseAccountLogAndNoticEmail(in,1,"发起报销审核"); }else if (useEA.getStatus()==3){ addExpenseAccountLogAndNoticEmail(in,4,"修改并发起审核"); } return expenseAccountMapper.updateByPrimaryKeySelective(in); } @Override public int addDetails(InputExpenseAccountDetails in) { return expenseAccountDetailsMapper.insertSelective(in); } @Override public int deleteDetails(Integer id) { return expenseAccountDetailsMapper.deleteByPrimaryKey(id); } @Override public List detailsList(Integer eaid) { return expenseAccountDetailsMapper.selectByEAid(eaid); } @Override public Object logList(Integer eaid) { return expenseAccountLogMapper.selectByEaid(eaid); } @Override public ExpenseAccountExamineLog examineLog(Integer eaid) { return expenseAccountLogMapper.selectExamineLog(eaid); } @Override public int updateExamine(InputExpenseAccount in) { ExpenseAccount useEa = expenseAccountMapper.selectByPrimaryKey(in.getId()); ExpenseAccount newEa=new ExpenseAccount(); newEa.setId(useEa.getId()); if (useEa.getStatus()==0)throw new BusinessException("审核状态错误"); if (useEa.getProcessStatus()!=in.getProcessStatus())throw new BusinessException(String.format("审核流程错误use=%s",useEa.getProcessStatus())); //审核通知 获取发送人 addExamineExpenseAccountLog(in, useEa, newEa); //完成申请 if (in.getStatus()==2){ //如果是借支则订单新增 if (in.getType()==4){ }else { //不然就看是否挂载借支订单进行抵扣 if (useEa.getDebitId()!=null){ ExpenseAccount useDebit=expenseAccountMapper.selectByPrimaryKey(useEa.getDebitId()); ExpenseAccount debit=new ExpenseAccount(); debit.setId(useEa.getDebitId()); BigDecimal settlementAmount = useDebit.getSettlementAmount(); settlementAmount=settlementAmount.add(useEa.getTotalAmount()); //如果不大于金额 if (settlementAmount.compareTo(useDebit.getTotalAmount())<1){ //计算抵扣后的已付 debit.setSettlementAmount(settlementAmount); //设置当前报销已付全额 newEa.setSettlementAmount(useEa.getTotalAmount()); }else { BigDecimal subtract = settlementAmount.subtract(useDebit.getTotalAmount()); newEa.setSettlementAmount(subtract); debit.setSettlementAmount(useDebit.getTotalAmount()); } expenseAccountMapper.updateByPrimaryKeySelective(debit); } } } expenseAccountMapper.updateByPrimaryKeySelective(newEa); return 1; } private void addExamineExpenseAccountLog(InputExpenseAccount in, ExpenseAccount useEa, ExpenseAccount newEa) { List aids=new ArrayList<>(); StringBuffer str=new StringBuffer(); Integer status=in.getStatus(); AdminListBo admin = adminMapper.getDeptNameByAid(useEa.getAid()); if (status==1){ str=str.append("您有报销需要审核,").append("报销编号[").append(in.getId()).append("]").append(",请查看并审核。"); if(useEa.getProcessStatus()== EAProcessStatus.FQ.getCode()){ //获取上级 Admin superAdmin = adminMapper.selectByPrimaryKey(admin.getSuperiorId()); aids.add(superAdmin); newEa.setProcessStatus(EAProcessStatus.SJSH.getCode()); newEa.setUserNames(superAdmin.getName()); }else if (useEa.getProcessStatus()== EAProcessStatus.SJSH.getCode()){ //获取财务负责人 Admin finaceAdmin = adminMapper.selectByPrimaryKey(admin.getDepFinance()); aids.add(finaceAdmin); newEa.setProcessStatus(EAProcessStatus.CWSH.getCode()); newEa.setExamineName(finaceAdmin.getName()); }else if (useEa.getProcessStatus()== EAProcessStatus.CWSH.getCode()){ //获取部门负责人 List list = adminMapper.listAdminBydepIdAndRoleType(admin.getDepartmentId(), AFTConstants.SALESMAN_ADMIN); if (!list.isEmpty())aids.addAll(list); newEa.setProcessStatus(EAProcessStatus.ZJLSH.getCode()); newEa.setExamineName(getAdmins(list)); }else if (useEa.getProcessStatus()== EAProcessStatus.ZJLSH.getCode()){ //获取总裁、董事长等审核 List list = adminMapper.selectAdminByRoleType(AFTConstants.All_CED_AND_CHAIRMAN); if (!list.isEmpty())aids.addAll(list); newEa.setProcessStatus(EAProcessStatus.DSZSH.getCode()); newEa.setExamineName(getAdmins(list)); }else if (useEa.getProcessStatus()== EAProcessStatus.DSZSH.getCode()){ //获取报销申请人 status =2; aids.add(adminMapper.selectByPrimaryKey(admin.getId())); newEa.setStatus(status); newEa.setExamineName(""); str.setLength(0); str=str.append("您的报销审核已通过,报销编号[").append(in.getId()).append("],请注意查看。"); } }else { aids.add(adminMapper.selectByPrimaryKey(admin.getId())); newEa.setStatus(3); str.setLength(0); str=str.append("您的报销审核已驳回,报销编号[").append(in.getId()).append("],请注意查看。"); } if (newEa.getStatus()==null)newEa.setStatus(1); addLogAndNoticeAndEmail(in, status,str.toString(),admin.getName(),aids); } private String getAdmins(List list) { StringBuffer str =new StringBuffer(); for (Admin admin : list) { str=str.append(admin.getName()).append(","); } return str.substring(0,str.length()-1); } private void addLogAndNoticeAndEmail(InputExpenseAccount in, Integer status, String str,String aname, List aids) { Date date= new Date(); addExpenseAccountLog(in.getId(),status,null,in.getReason()); Integer noticeStatus=NoticeStatus.EXPENSE_NOTICE.getCode(); if (status==2){ noticeStatus=NoticeStatus.EXPENSE_COMPLETE.getCode(); } List list=new ArrayList<>(); if (aids!=null){ for (Admin a : aids) { Notice notice=new Notice(); notice.setId(UUID.randomUUID().toString()); notice.setNoticeType(noticeStatus); notice.setType(NoticeTypes.getType(noticeStatus)); notice.setAid(a.getId()); notice.setCreateTime(date); notice.setContent(str); notice.setReaded(0); list.add(notice); EmailBo emailBo=new EmailBo(NoticeStatus.getValueByCode(noticeStatus),a.getEmail(),str); asyncUtils.send(emailBo); weChatUtils.addExpenseAccountWeChatNotice(a.getExpenseOpenId(),status,in.getId(),date,aname,str); } asyncUtils.addNoticeBatch(list); } } @Override public Object pageList(InputPageListBo in) { Map param=new HashMap<>(); addParam(in, param); return findPage("selectExpenseAccountList", "selectExpenseAccountCount", param, in.getPageNo(), in.getPageSize()); } @Override public Object selectByPrid(InputExpenseAccount in) { Integer id=null; in.setAid(TokenManager.getAdminId()); OutExpenseAccount useEa = expenseAccountMapper.selectByaidAndPrid(in); if (useEa!=null&&useEa.getId()!=null)id=useEa.getId(); return id; } @Override public Object selectById(Integer id) { OutExpenseAccount useEa = expenseAccountMapper.selectByid(id); return useEa; } @Override public Object updateType(InputExpenseAccount in) { expenseAccountDetailsMapper.deleteByeaid(in.getId()); return expenseAccountMapper.updateByPrimaryKeySelective(in); } @Override public List selectDebitOrder(String depId) { String aid= TokenManager.getAdminId(); return expenseAccountMapper.selectByaidAndType(aid,depId,4); } private void addParam(InputPageListBo in, Map param) { param.put("aid",TokenManager.getAdminId()); if (in.getStartTime()!=null&&in.getEndTime()!=null){ param.put("startTime",in.getStartTime()); param.put("endTime",in.getEndTime()+" 23:59:59"); } if (in.getStatus()!=null)param.put("status",in.getStatus()); if (in.getProcessStatus()!=null){ Integer processStatus=in.getProcessStatus(); //默认自己 设置上级,判断管理员与总裁等顶级审核 if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)){ processStatus=3; List list = departmentService.selectMyDeps(); param.put("deps",list); }else if (TokenManager.hasRole(AFTConstants.FINANCE)){ processStatus=2; }else if (checkShiroCED()){ processStatus=4; } param.put("processStatus",processStatus); }else { param.put("processStatus",0); } if (in.getContractNo()!=null)param.put("contractNo",in.getContractNo()); if (in.getDepId()!=null)param.put("depId",in.getDepId()); if (in.getUsername()!=null)param.put("username",in.getUsername()); if (in.getApplyDep()!=null)param.put("applyDep",in.getApplyDep()); if (in.getPayDep()!=null)param.put("payDep",in.getPayDep()); if (in.getType()!=null)param.put("type",in.getContractNo()); } /** * 检查权限判断是否是总裁、董事长或超级管理员权限 * @return */ private boolean checkShiroCED() { if (TokenManager.hasRole(AFTConstants.CED)||TokenManager.hasRole(AFTConstants.CED_ASSISTANT)|| TokenManager.hasRole(AFTConstants.SUPERADMIN)||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION_ASSISTANT) ||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)){ return true; } return false; } @Override public Object statistics(String name,String depId,String startTime,String endTime) { if (endTime!=null)endTime=endTime+" 23:59:59"; List list = expenseAccountMapper.selectStatistics(name, depId, startTime, endTime); List results=new ArrayList<>(); for (OutExpenseAccount e : list) { } return null; } }