package com.goafanti.common.utils; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import javax.mail.MessagingException; import com.goafanti.admin.bo.AdminListBo; import com.goafanti.common.bo.OrderOperator; import com.goafanti.common.constant.AFTConstants; import com.goafanti.common.dao.*; import com.goafanti.common.enums.NoticeStatus; import com.goafanti.common.model.*; import com.goafanti.core.shiro.token.TokenManager; import com.goafanti.order.bo.NewOrderChangeBo; import com.goafanti.order.bo.TOrderNewBo; import com.goafanti.order.enums.OrderChangeProcess; import com.goafanti.order.enums.ProcessStatus; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import com.goafanti.common.bo.EmailBo; import com.goafanti.common.enums.NoticeTypes; @Component @Async public class AsyncUtils { @Autowired private NoticeMapper noticeMapper; @Autowired private AdminMapper adminMapper; @Autowired private TOrderNewMapper tOrderNewMapper; @Autowired private NewOrderChangeMapper newOrderChangeMapper; @Autowired private TOrderInvoiceMapper tOrderInvoiceMapper; @Autowired private TOrderTaskMapper tOrderTaskMapper; public void patentSend(EmailBo bo) throws UnsupportedEncodingException, MessagingException { SendEmailUtil.getInstance().patentSend(bo); } public void sendList(List list) throws MessagingException, UnsupportedEncodingException { for (EmailBo emailBo : list) { SendEmailUtil.getInstance().patentSend(emailBo); } } public void send(EmailBo bo){ try { SendEmailUtil.getInstance().send(bo); } catch (UnsupportedEncodingException | MessagingException e) { e.printStackTrace(); } } public void addNotice(Notice n ) { n.setType(NoticeTypes.getType(n.getNoticeType())); noticeMapper.insertSelective(n); } public void addNotic(Integer type, String aid,String str) { Notice n =new Notice(); n.setId(UUID.randomUUID().toString()); n.setAid(aid); n.setNoticeType(type); n.setContent(str); n.setReaded(0);//未读 addNotice(n); } public void addNoticAndEmail(Notice n) { n.setType(NoticeTypes.getType(n.getNoticeType())); AdminListBo a = adminMapper.getDeptNameByAid(n.getAid()); EmailBo bo=new EmailBo(NoticeStatus.getStatus(n.getNoticeType()).getDesc(),a.getEmail(),n.getContent()) ; noticeMapper.insertSelective(n); if (StringUtils.isNotBlank(bo.getAddress())){ send(bo); } } /** * * @param aid 通知对象 * @param noticeType 通知类型 * @param content 通知内容 */ public void addNoticAndEmail(String aid,Integer noticeType,String content) { Notice n=new Notice(); n.setId(UUID.randomUUID().toString()); n.setAid(aid); n.setNoticeType(noticeType); n.setContent(content); n.setReaded(0); n.setType(NoticeTypes.getType(n.getNoticeType())); addNoticAndEmail(n); } public void addNoticeBatch(List ln) { List newList=new ArrayList(); if (ln != null && ln.size() > 0) { for (int i = 0; i < ln.size(); i++) { ln.get(i).setType(NoticeTypes.getType(ln.get(i).getNoticeType())); newList.add(ln.get(i)); if (50 == newList.size() || i == ln.size() - 1) { if (newList.size() > 0) noticeMapper.insertBatch(newList); newList.clear(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } } } public void batchUpdateUnreaded(List dl) { noticeMapper.batchUpdateUnreaded(dl); } /** * 切换审核人员 * @param depId 部门编号 * @param aid 切换后审核人 * @return */ public void updateExamineName( String depId, String aid) { tOrderNewMapper.updateExamineName(3,depId,aid,null); newOrderChangeMapper.updateExamineName(OrderChangeProcess.JSZJ.getCode(),depId,aid,null); tOrderInvoiceMapper.updateExamineName(0,depId,aid,null); } /** * * @param type 1营销管理 2公司管理 3财务管理 4咨询管理 5咨询师更换名称 6咨询师经理更换名称 * @param id 管理员id * @param roleType */ public void updateListExamineName(Integer type, String id, String roleType) { Admin admin = adminMapper.selectByPrimaryKey(id); String depId=admin.getDepartmentId(); String names=""; if (type==1){ names=getNames(type,depId, roleType); tOrderNewMapper.updateExamineName(ProcessStatus.YPYXGLY.getCode(),depId,null,names); newOrderChangeMapper.updateExamineName(OrderChangeProcess.YXGLY.getCode(),depId,null,names); tOrderInvoiceMapper.updateExamineName(1,depId,null,names); } if (type==2){ tOrderInvoiceMapper.updateListExamineName(type,depId,roleType); } if (type==3){ names=getNames(type,depId, roleType); newOrderChangeMapper.updateExamineName(OrderChangeProcess.CWZJ.getCode(),depId,null,names); } if (type==4){ names=getNames(type,depId, roleType); tOrderNewMapper.updateExamineName(ProcessStatus.YPZXSGLY.getCode(),depId,null,names); newOrderChangeMapper.updateExamineName(OrderChangeProcess.JSZJ.getCode(),depId,null,names); } if (type==5){ newOrderChangeMapper.updateTaskExamineName(admin.getId(),admin.getId(),OrderChangeProcess.JSY.getCode()); } if (type==6){ newOrderChangeMapper.updateTaskExamineName(admin.getId(),admin.getId(),OrderChangeProcess.JSJL.getCode()); } } private String getNames(Integer type,String depId, String roleType) { StringBuffer str=new StringBuffer(); List admins = adminMapper.selectAdminByRoleType(roleType); for (Admin admin : admins) { if (type==1&&admin.getDepartmentId().equals(depId)){ str = str.append(admin.getName()).append(","); }else if(type!=1){ str = str.append(admin.getName()).append(","); } } if (str.length()>1)return str.substring(0,str.length()-1); else return "暂无"; } /** * * @param type 发送对象 0发起营销管理员 1 完成通知所有人 2拒绝发送给营销员 * @param sotpType 发送对象 类型 0=项目暂停,1=项目重启 * */ public void addProjectSotpNotic(Integer type, Integer sotpType, String ids, TOrderTask task) { Integer noticeType=0; if (task==null)task = tOrderTaskMapper.selectByPrimaryKey(Integer.valueOf(ids.split(",")[0])); TOrderNewBo tOrderNew = tOrderNewMapper.getOrderNewDetail(task.getOrderNo()); List notes=new ArrayList<>(); StringBuffer emails=new StringBuffer(); if (type==0){ noticeType=sotpType==0?NoticeStatus.PROJECT_SOPT_START.getCode() :NoticeStatus.PROJECT_RENEW_START.getCode(); String str=String.format("客户名称[%s]订单编号[%s]项目编号[%s],请及时审核。",tOrderNew.getUserName(),task.getOrderNo(),ids); List admins = adminMapper.listAdminBydepIdAndRoleType(tOrderNew.getOrderDep(),AFTConstants.SALESMAN_ADMIN); for (Admin admin : admins) { addNotic( noticeType,admin.getId(), str); send(new EmailBo(NoticeStatus.getValueByCode(noticeType),admin.getEmail(),str)); } }else if(type==1){ String str=String.format("客户名称[%s]订单编号[%s]项目编号[%s],已%s请悉知。",tOrderNew.getUserName(),task.getOrderNo(),task.getId(),sotpType==1?"重启":"暂停"); noticeType=sotpType==0?NoticeStatus.PROJECT_SOPT_YES.getCode() :NoticeStatus.PROJECT_RENEW_YES.getCode(); OrderOperator orderOperator = tOrderTaskMapper.selectAidByParam(task.getId()); Listaids=new ArrayList<>(); if (orderOperator.getSalesmanId()!=null&&!aids.contains(orderOperator.getFinanceId())){ aids.add(orderOperator.getSalesmanId()); } if (task.getConsultantId()!=null&&!aids.contains(task.getConsultantId())){ aids.add(task.getConsultantId()); } if (task.getManagerId()!=null&&!aids.contains(task.getManagerId())){ aids.add(task.getManagerId()); } if (orderOperator.getFinanceId()!=null&&!aids.contains(orderOperator.getFinanceId())){ aids.add(orderOperator.getFinanceId()); } List cwgly = adminMapper.selectAdminByRoleType(AFTConstants.FINANCE_ADMIN); for (Admin admin : cwgly) { if (!aids.contains(admin.getId())){ aids.add(admin.getId()); } } List zxsgly = adminMapper.selectAdminByRoleType(AFTConstants.TECH_ADMIN); for (Admin admin : zxsgly) { if (!aids.contains(admin.getId())){ aids.add(admin.getId()); } } List zc = adminMapper.selectAdminByRoleType(AFTConstants.CED); for (Admin admin : zc) { if (!aids.contains(admin.getId())){ aids.add(admin.getId()); } } List dsz = adminMapper.selectAdminByRoleType(AFTConstants.APPROVAL_DECISION); for (Admin admin : dsz) { if (!aids.contains(admin.getId())){ aids.add(admin.getId()); } } if (!aids.isEmpty()){ for (String aid : aids) { pushParam( aid, noticeType, str, notes, emails); } } noticeMapper.insertBatch(notes); send(new EmailBo(NoticeStatus.getValueByCode(noticeType),emails.substring(0,emails.length()-1),str)); }else if (type==2){ noticeType=sotpType==0?NoticeStatus.PROJECT_SOPT_NO.getCode() :NoticeStatus.PROJECT_RENEW_NO.getCode(); String str=String.format("客户名称[%s]订单编号[%s]项目编号[%s],已经被驳回。",tOrderNew.getUserName(),task.getOrderNo(),task.getId()); if (tOrderNew.getSalesmanId()!=null){ Admin admin = adminMapper.selectByPrimaryKey(tOrderNew.getSalesmanId()); addNotic( noticeType,admin.getId(), str); send(new EmailBo(NoticeStatus.getValueByCode(noticeType),admin.getEmail(),str)); } } } private void pushParam(String id, Integer noticeType, String str, List notes, StringBuffer emails) { Admin admin = adminMapper.selectByPrimaryKey(id); pushParam(admin,noticeType,str,notes,emails); } private void pushParam(Admin admin, Integer noticeType, String str, List notes, StringBuffer emails) { notes.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, admin.getId(), noticeType, str)); if (admin!=null &&StringUtils.isNotBlank(admin.getEmail())) { if (!emails.toString().contains(admin.getEmail())){ emails = emails.append(admin.getEmail()).append(","); } } } }