package com.goafanti.weChat.service.impl; import java.text.ParseException; import java.util.*; import com.goafanti.common.dao.*; import com.goafanti.order.service.OrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.web.socket.TextMessage; import com.goafanti.admin.bo.AdminListBo; import com.goafanti.common.bo.EmailBo; import com.goafanti.common.constant.AFTConstants; import com.goafanti.common.enums.NoticeStatus; import com.goafanti.common.error.BusinessException; import com.goafanti.common.model.Admin; import com.goafanti.common.model.Notice; import com.goafanti.common.model.OrganizationContactBook; import com.goafanti.common.model.PublicRelease; import com.goafanti.common.model.PublicReleaseLog; import com.goafanti.common.model.User; import com.goafanti.common.utils.AsyncUtils; import com.goafanti.common.utils.DateUtils; import com.goafanti.common.utils.SendEmailUtil; 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.customer.bo.FollowBusinessBo; import com.goafanti.customer.service.CustomerService; import com.goafanti.weChat.bo.InputPublicDtails; import com.goafanti.weChat.bo.InputPublicRelease; import com.goafanti.weChat.bo.InputPublicReleaseList; import com.goafanti.weChat.bo.InputPublicStatistics; import com.goafanti.weChat.bo.OutPublicDtails; import com.goafanti.weChat.bo.OutPublicRelease; import com.goafanti.weChat.bo.OutPublicReleaseList; import com.goafanti.weChat.bo.OutPublicStatistics; import com.goafanti.weChat.bo.outPublicReleaseLog; import com.goafanti.weChat.service.PublicReleaseService; import com.goafanti.core.websocket.SystemWebSocketHandler; import cn.jiguang.common.utils.StringUtils; @Service public class PublicReleaseServiceImpl extends BaseMybatisDao implements PublicReleaseService { @Autowired private PublicReleaseMapper publicReleaseMapper; @Autowired private PublicReleaseLogMapper publicReleaseLogMapper; @Autowired private CustomerService customerService; @Autowired private OrderService orderService; @Autowired private WeChatUtils weChatUtils; @Autowired private AdminMapper adminMapper; @Autowired private AsyncUtils asyncUtils; @Autowired private UserMapper userMapper; @Autowired private OrganizationContactBookMapper organizationContactBookMapper; @Autowired private SystemWebSocketHandler systemWebSocketHandler; @Autowired private TOrderNewMapper tOrderNewMapper; @Override public Map addPublicRelease(InputPublicRelease in) { Date date =new Date(); in.setCreateTime(date); in.setStatus(1); in.setAid(TokenManager.getAdminId()); in.setNewUser(0); User u=userMapper.selectByPrimaryKey(in.getUid()); if (u!=null&&u.getShareType()==2){ in.setNewUser(1); } if (u.getAid().equals(TokenManager.getAdminId())){ in.setMainStatus(1); } if (in.getAssist()==1)in.setAssistAidName(getAssistAidName(in.getAssistAid())); publicReleaseMapper.insertSelective(in); AdminListBo my = adminMapper.getDeptNameByAid(TokenManager.getAdminId()); Admin a=adminMapper.selectByPrimaryKey(my.getReviewer()); PublicReleaseLog log=new PublicReleaseLog(in.getId(),TokenManager.getAdminId(),PublicReleaseLog.states.fq.getCode(),"发起外出申请",new Date()); publicReleaseLogMapper.insertSelective(log); String openid=a.getOpenId(); String str="["+my.getName()+"]发起["+u.getNickname()+"]的外出申请,请及时审核。"; sendNoticeAndSoucket(a.getId(),NoticeStatus.PUBLIC_RELEASE_START.getCode(),str); Mapmap=new HashMap(); map.put("data", publicReleaseMapper.selectDtails(in.getId(),null)); if (openid!=null) { Integer res= weChatUtils.addNotice(openid, in.getStatus(),in.getId(),date,my.getName(),"["+my.getName()+"]发起外出申请"); if (res!=0) { sendEmail(my, a,1); } map.put("code", res); }else { sendEmail(my, a,1); map.put("code", 2); } return map; } private String getAssistAidName(String assistAid) { String [] ss=assistAid.split(","); StringBuffer sb=new StringBuffer(); for (String s : ss) { Admin a=adminMapper.selectByPrimaryKey(s); if (a!=null&&a.getName()!=null)sb=sb.append(a.getName()).append(","); } if (sb.length()<1)return ""; return sb.deleteCharAt(sb.length() - 1).toString(); } private void sendNoticeAndSoucket(String id,Integer type,String content) { systemWebSocketHandler.sendMessageToUser(id, new TextMessage("unread")); Notice n =new Notice(); n.setId(UUID.randomUUID().toString()); n.setAid(id); n.setNoticeType(type); n.setContent(content); n.setReaded(0);//未读 asyncUtils.addNotice(n); } private void sendEmail(AdminListBo my, Admin a,Integer type) { String title="外出申请提醒"; if (type==0) title="外出申请驳回提醒"; else if (type==2) title="外出申请同意提醒"; if (SendEmailUtil.isEmail(a.getEmail())) { EmailBo email=new EmailBo(title,a.getEmail() , my.getDepartmentName(), my.getName(),"详情请登录微信打卡小程序查看,谢谢!") ; asyncUtils.send(email); } } @Override public int updatePublicRelease(InputPublicRelease in) { try { if(in.getReleaseStarts()!=null)in.setReleaseStart(DateUtils.parseDate(in.getReleaseStarts(), AFTConstants.YYYYMMDDHHMMSS)); if(in.getReleaseEnds()!=null)in.setReleaseEnd(DateUtils.parseDate(in.getReleaseEnds(), AFTConstants.YYYYMMDDHHMMSS)); } catch (ParseException e) { e.printStackTrace(); throw new BusinessException("转换异常"); } User u=userMapper.selectByPrimaryKey(in.getUid()); if (u!=null&&u.getShareType()==2){ in.setNewUser(1); } if (u!=null&&u.getAid().equals(TokenManager.getAdminId())){ in.setMainStatus(1); } PublicReleaseLog log=new PublicReleaseLog(in.getId(),TokenManager.getAdminId(),null,in.getReason(),new Date()); if(in.getStatus()==null||in.getStatus()!=3) { in.setStatus(1); log.setStatus(PublicReleaseLog.states.xg.getCode()); in.setUpdateStatus(1); if(in.getAssist()==1)in.setAssistAidName(getAssistAidName(in.getAssistAid())); }else { log.setStatus(PublicReleaseLog.states.cx.getCode()); List list = publicReleaseMapper.selectByMainId(in.getId()); for (OutPublicRelease out : list) { PublicReleaseLog log2 = new PublicReleaseLog(out.getId(), TokenManager.getAdminId(), PublicReleaseLog.states.sc.getCode(), "修改撤销协单", new Date()); publicReleaseLogMapper.insertSelective(log2); } publicReleaseMapper.updateStatusByMainId(3,in.getId()); } publicReleaseLogMapper.insertSelective(log); return publicReleaseMapper.updateByPrimaryKeySelective(in); } @SuppressWarnings("unchecked") @Override public Pagination listPublicRelease(InputPublicReleaseList in) { Map map =new HashMap(); map.put("type", in.getType()==null?0:in.getType()); if (!TokenManager.hasRole(AFTConstants.TECH_FINANCE_DIRECTOR)){ map.put("aid", TokenManager.getAdminId()); } if (StringUtils.isNotEmpty(in.getClockTime())) map.put("clockTime", in.getClockTime()); if(StringUtils.isNotEmpty(in.getReleaseStarts())) map.put("releaseStarts", in.getReleaseStarts()); if(StringUtils.isNotEmpty(in.getReleaseEnds())) map.put("publicEnd", in.getReleaseEnds()+" 23:59:59"); if(StringUtils.isNotEmpty(in.getUserName())) map.put("userName", in.getUserName()); if(in.getStatus()!=null) map.put("status", in.getStatus()); if(in.getClockIn()!=null) map.put("clockIn", in.getClockIn()); Pagination p=(Pagination) findPage("listPublicRelease", "countPublicRelease", map, in.getPageNo(), in.getPageSize()); return p; } @Override public int pushExaminePublicRelease(Integer id, Integer status, String remarks,Double duration) { PublicRelease p=new PublicRelease(); Date date =new Date(); String aid=TokenManager.getAdminId(); p.setId(id); p.setStatus(status); PublicRelease use=publicReleaseMapper.selectByPrimaryKey(id); if (use.getStatus()!=1) { throw new BusinessException("只能修改已发起的状态"); } StringBuffer str=new StringBuffer(); Admin ad=adminMapper.selectByPrimaryKey(aid); str=str.append("(").append(ad.getName()).append(")"); if (status==0){ str=str.append("驳回,"); }else if(status==2){ str=str.append("同意,"); //如果是协单公出主公出,在审核通过 if (use.getAssist()==1){ pushPublicReleaseAssist(use); }else if (use.getAssist()==0){ publicReleaseMapper.updateStatusByMainId(3,use.getId()); } } str=str.append(remarks); p.setAuditInfo(str.toString()); publicReleaseMapper.updateByPrimaryKeySelective(p); PublicReleaseLog log=new PublicReleaseLog(id,TokenManager.getAdminId(),status,remarks,new Date()); publicReleaseLogMapper.insertSelective(log); AdminListBo my = adminMapper.getDeptNameByAid(TokenManager.getAdminId()); Admin a=adminMapper.selectByPrimaryKey(use.getAid()); Integer type=0; String content=""; User u=userMapper.selectByPrimaryKey(use.getUid()); if (status==0) { type=NoticeStatus.PUBLIC_RELEASE_NO.getCode(); content="["+u.getNickname()+"]公司的外出申请,未通过审核!"; }else if(status==2) { type=NoticeStatus.PUBLIC_RELEASE_YES.getCode(); content="["+u.getNickname()+"]公司的外出申请,已通过审核!"; } sendNoticeAndSoucket(a.getId(),type,content); if (my.getOpenId()!=null) { if (remarks.length()>19) { remarks=remarks.substring(0, 15)+"..."; } Integer res=weChatUtils.addNotice(a.getOpenId(),p.getStatus(),p.getId(),date,my.getName(),remarks); if (res!=0) { sendEmail( my, a, status); } return res; } return 1; } private void pushPublicReleaseAssist(PublicRelease use) { Integer useId=use.getId(); if (use.getAssistAid()!=null){ List list = publicReleaseMapper.selectByMainId(use.getId()); String []ss=use.getAssistAid().split(","); //数据库与传入比对,如果不存在就标记撤销 ,如果存在就修改成 for (OutPublicRelease out : list) { boolean flag=false; for (String s : ss) { if (out.getAid().equals(s)){ flag=true; } } if (!flag){ PublicRelease in = new PublicRelease(); in.setId(out.getId()); in.setStatus(3); publicReleaseMapper.updateByPrimaryKey(in); PublicReleaseLog log = new PublicReleaseLog(in.getId(), TokenManager.getAdminId(), PublicReleaseLog.states.sc.getCode(), "修改撤销协单", new Date()); publicReleaseLogMapper.insertSelective(log); }else { use.setId(out.getId()); use.setClockIn(null); use.setClockInTime(null); publicReleaseMapper.updateByPrimaryKey(use); } } for (String s : ss) { boolean flag2 = false; for (OutPublicRelease o : list) { if (s.equals(o.getAid())) { flag2 = true; } } if (!flag2) { use.setAssist(2); use.setAssistAid(null); use.setAid(s); use.setStatus(2); use.setType(3); use.setMainId(useId); use.setId(null); publicReleaseMapper.insert(use); PublicReleaseLog log = new PublicReleaseLog(use.getId(), TokenManager.getAdminId(), PublicReleaseLog.states.ty.getCode(), "公出协单通过产生公出信息", new Date()); publicReleaseLogMapper.insertSelective(log); } } } } @Override public List listPublicReleaseLog(Integer id,String ufid) { List list=publicReleaseLogMapper.listPublicReleaseLog(id,ufid); return list; } @Override public int pushPublicReleaseClockIn(Integer id,String photoUrl) { PublicRelease p=new PublicRelease(); PublicRelease use=publicReleaseMapper.selectByPrimaryKey(id); String aid=TokenManager.getAdminId(); p.setId(id); p.setClockIn(1); p.setPhotoUrl(photoUrl); p.setClockInTime(new Date()); String str=""; if (use.getClockIn()==0) { FollowBusinessBo fbb=new FollowBusinessBo(); fbb.setUid(use.getUid()); fbb.setContactType("5"); fbb.setResult(use.getRemarks()); OrganizationContactBook ub=organizationContactBookMapper.getMajor(use.getUid(),aid); if (ub !=null) { fbb.setOcbId(ub.getId()); } fbb.setFollowTime(DateUtils.formatDate(p.getClockInTime(), AFTConstants.YYYYMMDDHHMMSS)); String ufid=UUID.randomUUID().toString(); p.setUfid(ufid); customerService.addFollow(fbb,ufid,use.getMainStatus()); str="公出人员打卡"; }else { str="刷新打卡"; } PublicReleaseLog log=new PublicReleaseLog(id,aid,PublicReleaseLog.states.dk.getCode(),str,new Date()); log.setPhotoUrl(photoUrl); publicReleaseLogMapper.insertSelective(log); publicReleaseMapper.updateByPrimaryKeySelective(p); return 1; } @Override public OutPublicRelease dtails(Integer id) { //如果不是本人和审核人员则不返回信息 OutPublicRelease out=publicReleaseMapper.selectDtails(id,null); if(out!=null){ boolean flag=false; //自己、审核人员与部门负责人可以查看 AdminListBo ab=adminMapper.getDeptNameByAid(out.getAid()); if (!TokenManager.getAdminId().equals( out.getAid())&& !TokenManager.getAdminId().equals(out.getReviewer())&& !TokenManager.getAdminId().equals(ab.getDepManager())&& !TokenManager.hasRole(AFTConstants.TECH_FINANCE_DIRECTOR)){ flag=true; } if(flag){ out=null; }else { if(TokenManager.getAdminId().equals(out.getAid())) { out.setPermission(0); }else { out.setPermission(1); } } } return out; } @Override public OutPublicRelease followDtails(String id) { return publicReleaseMapper.selectDtails(null,id); } @SuppressWarnings("unchecked") @Override @Cacheable(value = "publicReleaseStatistics#300", key = "'publicReleaseStatistics:" + "aid:'+#in.aid+'depId:'+#in.depId+'clockStart:'+#in.clockStart+" + "'clockEnd:'+#in.clockEnd+'createStart:'+#in.createStart+'createEnd'+#in.createEnd+" + "'pageNo:'+#in.pageNo+'pageSzie:'+#in.pageSize") public Object publicReleaseStatistics(InputPublicStatistics in) { Map map =new HashMap(); if(in.getAid()!=null)map.put("aid", in.getAid()); if(in.getDepId()!=null)map.put("depId", in.getDepId()); if(in.getClockStart()!=null)map.put("clockStart", in.getClockStart()); if(in.getClockEnd()!=null)map.put("clockEnd", in.getClockEnd()+" 23:59:59"); if(in.getCreateStart()!=null)map.put("createStart", in.getCreateStart()); if(in.getCreateEnd()!=null)map.put("createEnd", in.getCreateEnd()+" 23:59:59"); return (Pagination) findPage("listPublicStatistics", "countPublicStatistics", map, in.getPageNo(), in.getPageSize()); } @SuppressWarnings("unchecked") @Override public List publicReleaseStatisticsList(InputPublicStatistics in) { Map map =new HashMap(); if(in.getAid()!=null)map.put("aid", in.getAid()); if (TokenManager.hasRole(AFTConstants.SUPERADMIN)|| TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)|| TokenManager.hasRole(AFTConstants.CED)|| TokenManager.hasRole(AFTConstants.CED_ASSISTANT)){ if(in.getDepId()!=null)map.put("depId", in.getDepId()); }else if (TokenManager.hasRole(AFTConstants.COMPANY_MANAGER)||TokenManager.hasRole(AFTConstants.PERSONNEL_AUDITOR)){ } if(in.getClockStart()!=null)map.put("clockStart", in.getClockStart()); if(in.getClockEnd()!=null)map.put("clockEnd", in.getClockEnd()+" 23:59:59"); if(in.getCreateStart()!=null)map.put("createStart", in.getCreateStart()); if(in.getCreateEnd()!=null)map.put("createEnd", in.getCreateEnd()+" 23:59:59"); return (List) findList("listPublicStatistics",map, in.getPageNo(), 99999); } @SuppressWarnings("unchecked") @Override public Object publicReleaseListDtails(InputPublicDtails in) { Map map =new HashMap(); addParams(in, map); Date date = new Date(); Pagination page = (Pagination) findPage("listPublicDtails", "countPublicDtails", map, in.getPageNo(), in.getPageSize()); return page; } private void addParams(InputPublicDtails in, Map map) { if(in.getAid()!=null) map.put("aid", in.getAid()); if(in.getSid()!=null) map.put("sid", in.getSid()); if(in.getDepId()!=null) map.put("depId", in.getDepId()); if(in.getReleaseStart()!=null) map.put("releaseStart", in.getReleaseStart()); if(in.getReleaseEnd()!=null) map.put("releaseEnd", in.getReleaseEnd()+" 23:59:59"); if(in.getUid()!=null) map.put("uid", in.getUid()); if(in.getStatus()!=null) map.put("status", in.getStatus()); if(in.getClockIn()!=null) map.put("clockIn", in.getClockIn()); if(in.getPublicType()!=null)map.put("publicType",in.getPublicType()); //type 0私有 1管理权限 2所有权限 if(TokenManager.hasRole(AFTConstants.SUPERADMIN)||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION) ||TokenManager.hasRole(AFTConstants.CED_ASSISTANT )||TokenManager.hasRole(AFTConstants.PERSONNEL_AUDITOR) ||TokenManager.hasRole(AFTConstants.FINANCE_ADMIN)){ in.setType(2); }else if(TokenManager.hasRole(AFTConstants.COMPANY_MANAGER)||TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)){ in.setType(1); map.put("deps", orderService.selectMyDeps()); }else{ in.setType(0); if (in.getAid()==null) map.put("aid",TokenManager.getAdminId()); } map.put("type", in.getType()); } @SuppressWarnings("unchecked") @Override public List publicReleaseListDtailsList(InputPublicDtails in) { Map map =new HashMap(); addParams(in, map); return (List) findList("listPublicDtails", map, in.getPageNo(), 99999); } @Override public int addSupplement(Integer id, String supplement, String nextPlan) { PublicRelease pr=new PublicRelease(); pr.setId(id); pr.setSupplement(supplement); pr.setNextPlan(nextPlan); return publicReleaseMapper.updateByPrimaryKeySelective(pr); } @Override public boolean checkTime(InputPublicRelease in) { try { in.setReleaseStart(DateUtils.parseDate(in.getReleaseStarts(), AFTConstants.YYYYMMDDHHMMSS)); in.setReleaseEnd(DateUtils.parseDate(in.getReleaseEnds(), AFTConstants.YYYYMMDDHHMMSS)); } catch (ParseException e) { e.printStackTrace(); throw new BusinessException("转换异常"); } List is=publicReleaseMapper.checkTime(TokenManager.getAdminId(),in.getReleaseStart(),in.getReleaseEnd()); is.forEach(System.out::println); System.out.println(in.getId()); if (is.size()>1) { return true; }else if(is.size()==1){ System.out.println(is.get(0)); if (in.getId()!=null && !in.getId().equals(is.get(0))){ return true; } } return false; } @Override public List> selectOrderByUid(String uid) { return tOrderNewMapper.selectOrderByuid(uid); } @Override public List publicByOrder(String orderNo) { return publicReleaseMapper.publicByOrder(orderNo); } }