package com.goafanti.weChat.service.impl; import java.text.ParseException; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import com.goafanti.order.service.OrderService; import org.springframework.beans.factory.annotation.Autowired; 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.dao.AdminMapper; import com.goafanti.common.dao.OrganizationContactBookMapper; import com.goafanti.common.dao.PublicReleaseLogMapper; import com.goafanti.common.dao.PublicReleaseMapper; import com.goafanti.common.dao.UserMapper; 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; @Override public Map addPublicRelease(InputPublicRelease in) { Date date =new Date(); in.setCreateTime(date); in.setStatus(1); in.setAid(TokenManager.getAdminId()); publicReleaseMapper.insertSelectiveGetId(in); AdminListBo my = adminMapper.getDeptNameByAid(TokenManager.getAdminId()); Admin a=adminMapper.selectByPrimaryKey(my.getSuperiorId()); PublicReleaseLog log=new PublicReleaseLog(in.getId(),TokenManager.getAdminId(),1,"发起外出申请",new Date()); publicReleaseLogMapper.insertSelective(log); String openid=a.getOpenId(); User u=userMapper.selectByPrimaryKey(in.getUid()); String str="["+my.getName()+"]发起["+u.getNickname()+"]的外出申请,请及时审核。"; sendNoticeAndSoucket(a.getId(),NoticeStatus.PUBLIC_RELEASE_START.getCode(),str); Mapmap=new HashMap(); map.put("id", in.getId()); 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 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("转换异常"); } if(in.getStatus()==null||in.getStatus()!=3) { in.setStatus(1); }else { PublicReleaseLog log=new PublicReleaseLog(in.getId(),TokenManager.getAdminId(),4,in.getReason(),new Date()); publicReleaseLogMapper.insertSelective(log); } publicReleaseMapper.updateByPrimaryKeySelective(in); return 1; } @SuppressWarnings("unchecked") @Override public Pagination listPublicRelease(InputPublicReleaseList in) { Map map =new HashMap(); map.put("type", in.getType()==null?0:in.getType()); 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()); return (Pagination) findPage("listPublicRelease", "countPublicRelease", map, in.getPageNo(), in.getPageSize()); } @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("同意,");} 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; } @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); str="公出人员打卡"; }else { str="刷新打卡"; } PublicReleaseLog log=new PublicReleaseLog(id,aid,3,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&&!TokenManager.getAdminId().equals( out.getAid())&&!TokenManager.getAdminId().equals(out.getSuprId())) { out=null; }else if(TokenManager.getAdminId().equals(out.getSuprId())) { out.setPermission(1); }else { out.setPermission(0); } return out; } @Override public OutPublicRelease followDtails(String id) { return publicReleaseMapper.selectDtails(null,id); } @SuppressWarnings("unchecked") @Override 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(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 (List) findList("listPublicStatistics",map, in.getPageNo(), 99999); } @SuppressWarnings("unchecked") @Override public Object publicReleaseListDtails(InputPublicDtails in) { Map map =new HashMap(); if(in.getAid()!=null)map.put("aid", in.getAid()); 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(TokenManager.hasRole(AFTConstants.SUPERADMIN)||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)){in.setType(2);} else if(TokenManager.hasRole(AFTConstants.COMPANY_MANAGER)){ 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()); return (Pagination) findPage("listPublicDtails", "countPublicDtails", map, in.getPageNo(), in.getPageSize()); } @SuppressWarnings("unchecked") @Override public List publicReleaseListDtailsList(InputPublicDtails in) { Map map =new HashMap(); if(in.getAid()!=null)map.put("aid", in.getAid()); 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()); return (List) findList("listPublicDtails", map, in.getPageNo(), 99999); } @Override public int addSupplement(Integer id,String supplement) { PublicRelease pr=new PublicRelease(); pr.setId(id); pr.setSupplement(supplement); 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("转换异常"); } int i=publicReleaseMapper.checkTime(TokenManager.getAdminId(),in.getReleaseStart(),in.getReleaseEnd()); if (i>0) { return true; } return false; } }