package com.goafanti.user.service.impl; import java.util.Calendar; import java.util.List; import java.util.UUID; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.goafanti.common.bo.Result; import com.goafanti.common.constant.AFTConstants; import com.goafanti.common.dao.NoticeMapper; import com.goafanti.common.dao.UserIdentityMapper; import com.goafanti.common.dao.UserMapper; import com.goafanti.common.dao.UserRoleMapper; import com.goafanti.common.enums.CertifySubmitType; import com.goafanti.common.enums.IdentityAuditStatus; import com.goafanti.common.enums.NoticeReadStatus; import com.goafanti.common.enums.NoticeStatus; import com.goafanti.common.enums.UserLevel; import com.goafanti.common.model.Notice; import com.goafanti.common.model.User; import com.goafanti.common.model.UserIdentity; import com.goafanti.core.shiro.token.TokenManager; import com.goafanti.user.bo.AuditorUserIdentityDetailBo; import com.goafanti.user.bo.UserIdentityBo; import com.goafanti.user.service.UserIdentityService; @Service public class UserIdentityServiceImpl implements UserIdentityService { @Autowired private UserIdentityMapper userIdentityMapper; @Autowired private UserMapper userMapper; @Autowired private UserRoleMapper userRoleMapper; @Autowired private NoticeMapper noticeMapper; @Override public UserIdentity selectUserIdentityByUserId(String uid) { return userIdentityMapper.selectUserIdentityByUserId(uid); } @Override public UserIdentity insert(UserIdentity userIdentity) { userIdentityMapper.insert(userIdentity); return userIdentity; } @Override public int updateByPrimaryKeySelective(UserIdentity userIdentity) { return userIdentityMapper.updateByPrimaryKeySelective(userIdentity); } @Override public UserIdentityBo selectUserIdentityBoByUserId(String uid) { return userIdentityMapper.selectUserIdentityBoByUserId(uid); } @Override public int updateByPrimaryKey(UserIdentity u) { return userIdentityMapper.updateByPrimaryKey(u); } @Override public int saveUserIdentityProcess(Result res, UserIdentity userIdentity, String uid) { UserIdentity identity = userIdentityMapper.selectUserIdentityByUserId(uid); if (null == identity) { userIdentity.setId(UUID.randomUUID().toString()); userIdentity.setUid(uid); userIdentity.setWrongCount(0); userIdentity.setAuditStatus(0); return userIdentityMapper.insert(userIdentity); } else { userIdentity.setId(identity.getId()); if (null != userIdentity.getAuditStatus() && 5 == userIdentity.getAuditStatus()) { User u = new User(); u.setId(userIdentity.getUid()); u.setLvl(1); userMapper.updateByPrimaryKeySelective(u); } return userIdentityMapper.updateByPrimaryKeySelective(userIdentity); } } @Override public int updateUserDetail(UserIdentity u, String aid, String mid, String saveSign) { if (null != u.getAuditStatus() && 5 == u.getAuditStatus() && !StringUtils.isBlank(u.getUid())) { User user = new User(); user.setId(u.getUid()); user.setLvl(UserLevel.CERTIFIED.getCode()); userMapper.updateByPrimaryKeySelective(user); } User user = userMapper.selectByPrimaryKey(u.getUid()); if (!TokenManager.hasRole(AFTConstants.AUDITORADMIN) && !TokenManager.hasRole(AFTConstants.SUPERADMIN) && CertifySubmitType.SUBMIT.getCode().equals(saveSign)) { user.setAid(TokenManager.getAdminId()); createAuditorNotice(user); } else { if (!StringUtils.isBlank(aid)) { user.setAid(aid); } if (!StringUtils.isBlank(mid)) { user.setMid(mid); } createNotice(user, u.getAuditStatus()); } userMapper.updateByPrimaryKeySelective(user); return userIdentityMapper.updateByPrimaryKeySelective(u); } @Override public UserIdentity insertByAdmin(UserIdentity ui, String aid, String mid, String saveSign) { userIdentityMapper.insert(ui); User u = userMapper.selectByPrimaryKey(ui.getUid()); if (!TokenManager.hasRole(AFTConstants.AUDITORADMIN) && !TokenManager.hasRole(AFTConstants.SUPERADMIN) && CertifySubmitType.SUBMIT.getCode().equals(saveSign)) { u.setAid(TokenManager.getAdminId()); userMapper.updateByPrimaryKeySelective(u); createAuditorNotice(u); } if (TokenManager.hasRole(AFTConstants.AUDITORADMIN) || TokenManager.hasRole(AFTConstants.SUPERADMIN)) { u.setAid(aid); u.setMid(mid); if (IdentityAuditStatus.PASSED.getCode() == ui.getAuditStatus()) { u.setLvl(UserLevel.CERTIFIED.getCode()); } userMapper.updateByPrimaryKeySelective(u); createNotice(u, ui.getAuditStatus()); } return ui; } // 给业务员及客户经理发送通知 private void createNotice(User u, Integer status) { if (!StringUtils.isBlank(u.getAid())) { Notice n = new Notice(); n.setPid(u.getAid()); n.setAid(u.getAid()); n.setUid(u.getId()); notice(n, status); } if (!StringUtils.isBlank(u.getMid())) { Notice n = new Notice(); n.setUid(u.getId()); n.setAid(u.getMid()); if (!StringUtils.isBlank(u.getAid())) { n.setPid(u.getAid()); } notice(n, status); } } // 给所有审核员发送审核通知 private void createAuditorNotice(User u) { List ids = userRoleMapper.listAuditor(); if (null != ids && ids.size() > 0) { for (String s : ids) { Notice n = new Notice(); Calendar now = Calendar.getInstance(); now.set(Calendar.MILLISECOND, 0); n.setId(UUID.randomUUID().toString()); n.setCreateTime(now.getTime()); n.setReaded(NoticeReadStatus.UNREAD.getCode()); if (null != u) { n.setPid(u.getAid()); } n.setUid(u.getId()); n.setAid(s); n.setContent(NoticeStatus.PERSONALCERTIFY.getDesc() + " " + IdentityAuditStatus.COMMITTED.getDesc()); n.setNoticeType(NoticeStatus.PERSONALCERTIFY.getCode()); noticeMapper.insert(n); } } } @Override public AuditorUserIdentityDetailBo selectAuditorUserIdentityByUserId(String uid) { return userIdentityMapper.selectAuditorUserIdentityByUserId(uid); } private void notice(Notice n, Integer status) { Calendar now = Calendar.getInstance(); now.set(Calendar.MILLISECOND, 0); n.setId(UUID.randomUUID().toString()); n.setCreateTime(now.getTime()); n.setReaded(NoticeReadStatus.UNREAD.getCode()); String content = NoticeStatus.PERSONALCERTIFY.getDesc(); if (IdentityAuditStatus.UNCOMMITTED.getCode() == status) { content = content + " " + IdentityAuditStatus.UNCOMMITTED.getDesc(); } else if (IdentityAuditStatus.COMMITTED.getCode() == status) { content = content + " " + IdentityAuditStatus.COMMITTED.getDesc(); } else if (IdentityAuditStatus.UNPAID.getCode() == status) { content = content + " " + IdentityAuditStatus.UNPAID.getDesc(); } else if (IdentityAuditStatus.PAID.getCode() == status) { content = content + " " + IdentityAuditStatus.PAID.getDesc(); } else if (IdentityAuditStatus.PASSED.getCode() == status) { content = content + " " + IdentityAuditStatus.PASSED.getDesc(); } else { content = content + " " + IdentityAuditStatus.NOTPASSED.getDesc(); } n.setContent(content); n.setNoticeType(NoticeStatus.PERSONALCERTIFY.getCode()); noticeMapper.insert(n); } }