package com.goafanti.admin.service.impl; import java.io.UnsupportedEncodingException; 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 javax.mail.MessagingException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.goafanti.admin.bo.AdminListBo; import com.goafanti.admin.bo.InputPublicDtails; import com.goafanti.admin.bo.InputPublicRelease; import com.goafanti.admin.bo.InputPublicReleaseList; import com.goafanti.admin.bo.InputPublicStatistics; import com.goafanti.admin.bo.OutPublicDtails; import com.goafanti.admin.bo.OutPublicRelease; import com.goafanti.admin.bo.OutPublicReleaseList; import com.goafanti.admin.bo.OutPublicStatistics; import com.goafanti.admin.bo.outPublicReleaseLog; import com.goafanti.admin.service.PublicReleaseService; import com.goafanti.common.bo.EmailBo; import com.goafanti.common.constant.AFTConstants; import com.goafanti.common.dao.AdminMapper; import com.goafanti.common.dao.PublicReleaseLogMapper; import com.goafanti.common.dao.PublicReleaseMapper; import com.goafanti.common.error.BusinessException; import com.goafanti.common.model.Admin; import com.goafanti.common.model.PublicRelease; import com.goafanti.common.model.PublicReleaseLog; 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 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 WeChatUtils weChatUtils; @Autowired private AdminMapper adminMapper; @Autowired private AsyncUtils asyncUtils; @Override public int addPublicRelease(InputPublicRelease in) { Date date =new Date(); in.setCreateTime(date); in.setStatus(1); in.setAid(TokenManager.getAdminId()); 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("转换异常"); } 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(); if (openid!=null) { Integer res= weChatUtils.addNotice(openid, in.getStatus(),date,my.getName(),"["+my.getName()+"]发起外出审核"); if (res!=0) { senEmail(my, a,1); } return res; }else { //成功但是对方收不到消息 return 2; } } private void senEmail(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(),"详情请登录微信打卡小程序查看,谢谢!") ; try { asyncUtils.send(email); } catch (UnsupportedEncodingException | MessagingException e) { e.printStackTrace(); } } } @Override public int updatePublicRelease(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("转换异常"); } in.setStatus(1); 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()); } 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(); p.setId(id); p.setStatus(status); PublicRelease use=publicReleaseMapper.selectByPrimaryKey(id); if (use.getStatus()!=1) { throw new BusinessException("只能修改已发起的状态"); } 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()); if (my.getOpenId()!=null) { Integer res=weChatUtils.addNotice(a.getOpenId(),p.getStatus(),date,my.getName(),remarks); if (res!=0) { senEmail( my, a, status); } return res; } return 1; } @Override public List listPublicReleaseLog(Integer id) { return publicReleaseLogMapper.listPublicReleaseLog(id); } @Override public int pushPublicReleaseClockIn(Integer id,String photoUrl) { PublicRelease p=new PublicRelease(); PublicRelease use=publicReleaseMapper.selectByPrimaryKey(id); p.setId(id); p.setClockIn(1); p.setPhotoUrl(photoUrl); p.setClockInTime(new Date()); if (use.getClockIn()==0) { FollowBusinessBo fbb=new FollowBusinessBo(); fbb.setUid(use.getUid()); fbb.setContactType("0"); fbb.setResult(use.getRemarks()); fbb.setFollowTime(DateUtils.formatDate(p.getClockInTime(), AFTConstants.YYYYMMDDHHMMSS)); String ufid=UUID.randomUUID().toString(); p.setUfid(ufid); customerService.addFollow(fbb,ufid); } publicReleaseMapper.updateByPrimaryKeySelective(p); return 1; } @Override public OutPublicRelease dtails(Integer id) { return publicReleaseMapper.selectByid(id); } @Override public OutPublicRelease followDtails(String id) { return publicReleaseMapper.selectByUfid(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 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()); return (Pagination) findPage("listPublicDtails", "countPublicDtails", map, in.getPageNo(), in.getPageSize()); } }