package com.goafanti.expenseAccount.service.impl; import com.goafanti.admin.bo.AdminListBo; import com.goafanti.admin.bo.NoticeBo; 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.mybatis.page.Pagination; 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 com.sun.corba.se.spi.ior.iiop.IIOPFactories; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; @Service public class ExpenseAccountServiceImpl extends BaseMybatisDao implements ExpenseAccountService { @Autowired private AdminMapper adminMapper; @Autowired private ExpenseAccountMapper expenseAccountMapper; @Autowired private PublicReleaseMapper publicReleaseMapper; @Autowired private ExpenseAccountLogMapper expenseAccountLogMapper; @Autowired private AsyncUtils asyncUtils; @Autowired private WeChatUtils weChatUtils; @Autowired private ExpenseAccountDetailsMapper expenseAccountDetailsMapper; @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()); } expenseAccountMapper.insertSelective(in); addExpenseAccountLog(in.getId(),0,"创建报销"); return in.getId(); } /** * * @param eaid 报销编号 * @param status 状态 0创建 1发起 2通过 3驳回 4修改 * @param str 备注 */ private void addExpenseAccountLog(Integer eaid, Integer status, String str) { String aid=TokenManager.getAdminId(); Date date=new Date(); ExpenseAccountLog eaLog=new ExpenseAccountLog(); eaLog.setEaid(eaid); eaLog.setStatus(status); eaLog.setRemarks(str); eaLog.setAuditor(aid); Admin admin = adminMapper.selectByPrimaryKey(aid); StringBuffer sb =new StringBuffer(); if (status>0){ if (status==1){ sb.append(str).append("[").append(admin.getName()).append("]").append("发起报销申请,请及时审核。"); }else if (status==4){ sb.append(str).append("[").append(admin.getName()).append("]").append("修改并重新发起,请重新审核。"); } asyncUtils.addNoticAndEmail(NoticeStatus.EXPENSE_NOTICE.getCode(), aid,sb.toString()); weChatUtils.addExpenseAccountWeChatNotice(admin.getOpenId(), 1, eaid, date, admin.getName(),sb.toString()); } expenseAccountLogMapper.insertSelective(eaLog); } @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)); } in.setStatus(1); in.setProcessStatus(1); //计算总金额 in.setTotalAmount(expenseAccountDetailsMapper.selectAmountCountByEAid(in.getId())); if (useEA.getStatus()==0){ addExpenseAccountLog(useEA.getId(),1,"发起报销审核"); }else if (useEA.getStatus()==3){ addExpenseAccountLog(useEA.getId(),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()!=1)throw new BusinessException("审核状态错误"); if (useEa.getProcessStatus()!=in.getProcessStatus())throw new BusinessException("审核流程错误"); //审核通知 获取发送人 List aids=new ArrayList<>(); Integer status=1; StringBuffer str=new StringBuffer(); str=str.append("您有报销需要审核,请查看并审核。"); AdminListBo admin = adminMapper.getDeptNameByAid(useEa.getAid()); if(useEa.getProcessStatus()== EAProcessStatus.FQ.getCode()){ //获取上级 aids.add(adminMapper.selectByPrimaryKey(admin.getSuperiorId())); newEa.setProcessStatus(EAProcessStatus.SJSH.getCode()); }else if (useEa.getProcessStatus()== EAProcessStatus.SJSH.getCode()){ //获取负责人 List list = adminMapper.listAdminBydepIdAndRoleType(admin.getId(), admin.getDepartmentId()); if (!list.isEmpty())aids.addAll(list); newEa.setProcessStatus(EAProcessStatus.ZJLSH.getCode()); }else if (useEa.getProcessStatus()== EAProcessStatus.ZJLSH.getCode()){ //获取负责人 aids.add(adminMapper.selectByPrimaryKey(admin.getDepFinance())); newEa.setProcessStatus(EAProcessStatus.CWSH.getCode()); }else if (useEa.getProcessStatus()== EAProcessStatus.CWSH.getCode()){ //获取负责人 List admins = adminMapper.selectAdminByRoleType(AFTConstants.All_CED_AND_CHAIRMAN); if (!admins.isEmpty())aids.addAll(admins); newEa.setProcessStatus(EAProcessStatus.DSZSH.getCode()); }else if (useEa.getProcessStatus()== EAProcessStatus.DSZSH.getCode()){ //获取负责人 status=2; aids.add(adminMapper.selectByPrimaryKey(admin.getId())); newEa.setStatus(status); str=new StringBuffer("您的报销审核已通过,请注意查看。"); } addLogAndNoticeAndEmail(in,useEa,status,str.toString(),admin.getName(),aids); expenseAccountMapper.updateByPrimaryKeySelective(newEa); return 1; } private void addLogAndNoticeAndEmail(InputExpenseAccount in, ExpenseAccount useEa, Integer status, String str,String aname, List aids) { addExpenseAccountLog(useEa.getId(),status,in.getReason()); Date date= new Date(); 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.addNotice(a.getOpenId(),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; } 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=2; }else if (checkShiroCED()){ processStatus=4; } param.put("processStatus",processStatus); }else { param.put("processStatus",0); } } /** * 检查权限判断是否是总裁、董事长或超级管理员权限 * @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; } }