package com.xzx.gc.user.service; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.convert.Convert; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; import com.xzx.gc.common.constant.CommonEnum; import com.xzx.gc.common.constant.Constants; import com.xzx.gc.common.constant.RedisKeyConstant; import com.xzx.gc.common.constant.UserEnum; import com.xzx.gc.common.utils.*; import com.xzx.gc.entity.*; import com.xzx.gc.model.user.UserGatherApplyListParam; import com.xzx.gc.model.user.UserGatherAuditParam; import com.xzx.gc.user.mapper.CoreUserMapper; import com.xzx.gc.user.mapper.SysItemUserMapper; import com.xzx.gc.user.mapper.UserGatherApplyMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import tk.mybatis.mapper.entity.Example; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @Service @Transactional public class UserGatherApplyService { @Autowired private UserGatherInfoService userGatherInfoService; @Autowired private IdUtils idUtils; @Autowired private UserService userService; @Autowired private AddressService addressService; @Autowired private UserGatherApplyMapper userGatherApplyMapper; @Autowired private AddressLevelService addressLevelService; @Autowired private AccountService accountService; @Autowired private HttpServletRequest request; @Autowired private RedisUtil redisUtil; @Autowired private TokenUtil tokenUtil; @Autowired private SysEnvironmentalInfoService sysEnvironmentalInfoService; @Autowired private OtherUserService otherUserService; @Autowired private UserRoleService userRoleService; @Autowired private PartnerTraceService partnerTraceService; @Autowired private PartnerGaodeService partnerGaodeService; @Autowired private UserScrapStoreService userScrapStoreService; @Autowired private SysItemUserMapper sysItemUserMapper; @Autowired private UserTargetInfoService userTargetInfoService; @Autowired private PartnerFenceService partnerFenceService; @Autowired private CoreUserMapper coreUserMapper; public UserGatherApply findByRelationId(String relationId){ //可能申请多次 Example example=new Example(UserGatherApply.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("relationId",relationId); example.setOrderByClause("create_time desc"); List userGatherApplies = userGatherApplyMapper.selectByExample(example); if(CollUtil.isNotEmpty(userGatherApplies)){ return userGatherApplies.get(0); } return null; } public void apply(UserGatherApply userGatherApply) { //获取省市区 AddressLevelInfo byAdcode = addressLevelService.findByLocation(userGatherApply.getLongitude(), userGatherApply.getLatitude()); userGatherApply.setProvinceId(byAdcode.getLevel1Id()); userGatherApply.setCityId(byAdcode.getLevel2Id()); userGatherApply.setTownshipId(byAdcode.getLevel3Id()); userGatherApply.setStatus(CommonEnum.待审核.getValue()); //所属推广员ID Short applyOrigin = userGatherApply.getApplyOrigin(); String createUserId = userGatherApply.getCreateUserId(); /** * 设置推广人员 */ //小程序 if(Convert.toShort(1).equals(applyOrigin)){ if(StrUtil.isNotBlank(createUserId)){ String mobilePhone = userService.findById(createUserId).getMobilePhone(); List byMobile = userTargetInfoService.findByMobile(mobilePhone); if(CollUtil.isNotEmpty(byMobile)){ userGatherApply.setRelationUserId(byMobile.get(0).getRealUserId()); } } }else { //安卓 userGatherApply.setRelationUserId(createUserId); } add(userGatherApply); } /** * 审核集货员通过 * @param userGatherApply */ private String auditGather(UserGatherApply userGatherApply){ //添加集货员表 UserGatherInfo userGatherInfo=new UserGatherInfo(); BeanUtil.copyProperties(userGatherApply,userGatherInfo); //查询是否有普通用户身份 UserInfo byMobileForbidden = userService.findByMobileForbidden(userGatherApply.getMobilePhone()); String userId; if(byMobileForbidden!=null){ userId=byMobileForbidden.getUserId(); }else { userId=idUtils.generate("YH",3); } userGatherInfo.setUserId(userId); userGatherInfo.setNickName(StringUtils.encode(userGatherInfo.getName())); UserGatherInfo byId = userGatherInfoService.findById(userId); if(byId==null) { userGatherInfoService.add(userGatherInfo); }else { userGatherInfo.setRegistTime(DateUtil.now()); userGatherInfo.setDelFlag(Constants.DEL_NOT_FLAG); userGatherInfo.setIsProhibit(false); userGatherInfoService.updateById(userGatherInfo); } if(byMobileForbidden!=null){ //更新用户类型 byMobileForbidden.setUserType(CommonEnum.集货员.getValue()); byMobileForbidden.setName(userGatherApply.getName()); userService.updateById(byMobileForbidden); //将原所有地址设为非默认 addressService.updateByUserIdSetNoDefault(userId); //删除缓存 redisUtil.del(RedisKeyConstant.TOKEN+userId); }else { //复制到用户表 UserInfo userInfo = new UserInfo(); BeanUtil.copyProperties(userGatherInfo, userInfo); userService.add(userInfo); //添加账户 AccountInfo accountInfo = accountService.init(userGatherApply.getMobilePhone(), userId); accountService.add(accountInfo); } //添加地址 initAddAddress(userGatherApply,userId); //发送短信 // SmsUtil.sendPoint(userGatherApply.getMobilePhone(), SmsConstant.GATHER_SUCCESS); return userId; } private void initAddAddress(UserGatherApply userGatherApply,String userId){ AddressInfo addressInfo = new AddressInfo(); BeanUtil.copyProperties(userGatherApply, addressInfo); addressInfo.setUserId(userId); addressInfo.setFlag(UserEnum.默认地址.getValue()); addressInfo.setRelaPhone(userGatherApply.getMobilePhone()); addressInfo.setRelaName(userGatherApply.getName()); if(UserEnum.集货员类型.getValue().equals(userGatherApply.getType())) { //将address_area(定位地址) detail_address小区地址 AddressLevelInfo addressLevelInfo = addressLevelService.find(userGatherApply.getProvinceId(), userGatherApply.getCityId(), userGatherApply.getTownshipId()); if(addressLevelInfo!=null){ addressInfo.setAddressArea(addressLevelInfo.getLevel1Name()+addressLevelInfo.getLevel2Name()+addressLevelInfo.getLevel3Name()); } addressInfo.setDetailAddress(userGatherApply.getAddressArea()); //小区名称 addressInfo.setHouseName(userGatherApply.getDetailAddress()); } addressService.add(addressInfo); } /** * 审核成为回收员 * @param userGatherApply */ private String auditOtherUser(UserGatherApply userGatherApply){ String userId=idUtils.generate("USER",5); OtherUserInfo otherUserInfo=new OtherUserInfo(); otherUserInfo.setUserId(userId); otherUserInfo.setMobilePhone(userGatherApply.getMobilePhone()); String salt=RandomUtil.randomString(16); // String password=RandomUtil.randomNumbers(6); String password="123456"; String encrypt = SecurityUtil.encrypt(salt, password); otherUserInfo.setPassword(encrypt); otherUserInfo.setSalt(salt); otherUserInfo.setName(userGatherApply.getName()); otherUserInfo.setUserType(CommonEnum.回收员.getValue()); //获取省市区 otherUserInfo.setProvinceId(Convert.toInt(userGatherApply.getProvinceId())); otherUserInfo.setCityId(Convert.toInt(userGatherApply.getCityId())); otherUserInfo.setTownshipId(Convert.toInt(userGatherApply.getTownshipId())); //角色ID UserRole byCode = userRoleService.findByCode(CommonEnum.回收员.getValue()); if(byCode!=null){ otherUserInfo.setRoleIds(Convert.toStr(byCode.getId())); } otherUserInfo.setIsProhibit(false); otherUserInfo.setOriginType(Convert.toShort(1)); //合伙人相关 // PartnerGaode byTownId = partnerGaodeService.findByTownId(otherUserInfo.getTownshipId().toString()); // if(byTownId!=null){ // String partnerId = byTownId.getPartnerId(); // String terimalId=partnerTraceService.createTerimainal(partnerId,OrderEnum.回收员订单轨迹.getValue(),userId); // String terimalDayId=partnerTraceService.createTerimainal(partnerId,OrderEnum.回收员全天轨迹.getValue(),userId); // otherUserInfo.setTerminalId(terimalId); // otherUserInfo.setTerminalDayId(terimalDayId); // otherUserInfo.setPartnerId(partnerId); // List fences = partnerFenceService.findFences(partnerId); // if(CollUtil.isNotEmpty(fences)){ // otherUserInfo.setTownId(CollUtil.join(fences,",")); // } // } otherUserService.add(otherUserInfo); //新增账户 AccountInfo accountInfo = accountService.init(userGatherApply.getMobilePhone(), userId); accountService.add(accountInfo); //发送短信 // Map map=new HashMap<>(); // map.put("other_number",password); // SmsUtil.sendPointByTemplate(userGatherApply.getMobilePhone(),map,SmsConstant.OTHER_SUUCESS_TEMPLATE, 2); return userId; } /** * 审核成为废品站complaintFirstFlag * @param userGatherApply */ private String auditScrapStore(UserGatherApply userGatherApply){ UserScrapStore userScrapStore=new UserScrapStore(); BeanUtil.copyProperties(userGatherApply,userScrapStore); String id=idUtils.generate("FP",6); userScrapStore.setId(id); userScrapStoreService.add(userScrapStore); //废品分类 String itemTypes = userGatherApply.getItemTypes(); if(StrUtil.isNotBlank(itemTypes)){ List byIds = sysEnvironmentalInfoService.findByIds(itemTypes.split(",")); List list=new ArrayList<>(); for (SysEnvironmentalInfo byId : byIds) { SysItemUser sysItemUser=new SysItemUser(); sysItemUser.setUserId(id); sysItemUser.setItemType(byId.getItemType()); sysItemUser.setCreateTime(DateUtil.now()); sysItemUser.setDelFlag(Constants.DEL_NOT_FLAG); list.add(sysItemUser); } if(CollUtil.isNotEmpty(list)) { sysItemUserMapper.insertList(list); } } //发送短信 // SmsUtil.sendPoint(userGatherApply.getMobilePhone(), SmsConstant.SCRAP_SUCCESS); return id; } public void add(UserGatherApply userGatherApply){ userGatherApply.setCreateTime(DateUtil.now()); userGatherApplyMapper.insertSelective(userGatherApply); } public List findByMobileAndType(String mobile,String type){ UserGatherApply userGatherApply=new UserGatherApply(); userGatherApply.setMobilePhone(mobile); userGatherApply.setType(type); return userGatherApplyMapper.select(userGatherApply); } public List list(UserGatherApplyListParam userGatherApplyListParam) { Example example=new Example(UserGatherApply.class); Example.Criteria criteria = example.createCriteria(); if(StrUtil.isNotBlank(userGatherApplyListParam.getStatus())){ criteria.andEqualTo("status",userGatherApplyListParam.getStatus()); } if(StrUtil.isNotBlank(userGatherApplyListParam.getFilterType())){ criteria.andEqualTo("type",userGatherApplyListParam.getFilterType()); }else { if(CommonEnum.app.getValue().equals(userGatherApplyListParam.getClientType())){ criteria.andIn("type",CollUtil.newArrayList(UserEnum.回收员类型.getValue(),UserEnum.废品店类型.getValue())); } } if(StrUtil.isNotBlank(userGatherApplyListParam.getTownShipId())){ criteria.andEqualTo("townshipId",userGatherApplyListParam.getTownShipId()); } if(StrUtil.isNotBlank(userGatherApplyListParam.getRelationUserId())){ criteria.andEqualTo("relationUserId",userGatherApplyListParam.getRelationUserId()); } boolean createFlag=true; if(StrUtil.isNotBlank(userGatherApplyListParam.getExamineTimeStart())){ createFlag=false; criteria.andCondition("DATE_FORMAT(examine_time,'%Y-%m-%d')>=DATE_FORMAT('"+userGatherApplyListParam.getExamineTimeStart()+"','%Y-%m-%d')"); } if(StrUtil.isNotBlank(userGatherApplyListParam.getExamineTimeEnd())){ createFlag=false; criteria.andCondition("DATE_FORMAT(examine_time,'%Y-%m-%d')<=DATE_FORMAT('"+userGatherApplyListParam.getExamineTimeEnd()+"','%Y-%m-%d')"); } if(StrUtil.isNotBlank(userGatherApplyListParam.getCreateTimeStart())){ criteria.andCondition("DATE_FORMAT(create_time,'%Y-%m-%d')>=DATE_FORMAT('"+userGatherApplyListParam.getCreateTimeStart()+"','%Y-%m-%d')"); } if(StrUtil.isNotBlank(userGatherApplyListParam.getCreateTimeEnd())){ criteria.andCondition("DATE_FORMAT(create_time,'%Y-%m-%d')<=DATE_FORMAT('"+userGatherApplyListParam.getCreateTimeEnd()+"','%Y-%m-%d')"); } if(createFlag) { example.setOrderByClause("create_time desc"); }else { example.setOrderByClause("examine_time desc"); } List userGatherApplies = userGatherApplyMapper.selectByExample(example); for (UserGatherApply userGatherApply : userGatherApplies) { String itemTypes = userGatherApply.getItemTypes(); //分类 if(StrUtil.isNotBlank(itemTypes)){ String[] split = itemTypes.split(","); List byIds = sysEnvironmentalInfoService.findByIds(split); if(CollUtil.isNotEmpty(byIds)) { userGatherApply.setItemTypeNames(byIds.stream().map(x->x.getTitle()).collect(Collectors.joining(","))); } } //所属区域名称 if(StrUtil.isNotBlank(userGatherApply.getTownshipId())) { AddressLevelInfo addressLevelInfo = addressLevelService.find(userGatherApply.getProvinceId(), userGatherApply.getCityId(), userGatherApply.getTownshipId()); if (addressLevelInfo != null) { userGatherApply.setTownshipName(addressLevelInfo.getLevel3Name()); } } //所属推广员名称 if(StrUtil.isNotBlank(userGatherApply.getRelationUserId())){ String name = otherUserService.findById(userGatherApply.getRelationUserId()).getName(); userGatherApply.setRelationUserName(name); } //审核人名称 //所属推广员名称 if(StrUtil.isNotBlank(userGatherApply.getUpdateUserId())) { OtherUserInfo byId = otherUserService.findById(userGatherApply.getUpdateUserId()); if(byId!=null){ userGatherApply.setUpdateUserName(byId.getName()); }else { CoreUser coreUser = coreUserMapper.selectByPrimaryKey(userGatherApply.getUpdateUserId()); if(coreUser!=null){ userGatherApply.setUpdateUserName(coreUser.getName()); } } } String mobilePhone = userGatherApply.getMobilePhone(); UserGatherApply userGatherApply1=new UserGatherApply(); userGatherApply1.setMobilePhone(mobilePhone); int i = userGatherApplyMapper.selectCount(userGatherApply1); if(i>1){ userGatherApply.setIsApply(true); }else{ userGatherApply.setIsApply(false); } } return userGatherApplies; } public UserGatherApply findById(Long id){ return userGatherApplyMapper.selectByPrimaryKey(id); } public void audit(UserGatherAuditParam userGatherAuditParam) { Long id = userGatherAuditParam.getId(); String status = userGatherAuditParam.getStatus(); UserGatherApply userGatherApply=new UserGatherApply(); BeanUtil.copyProperties(userGatherAuditParam,userGatherApply); //审核通过 if(CommonEnum.已审核.getValue().equals(status)){ UserGatherApply byId = findById(id); String type = byId.getType(); if(UserEnum.回收员类型.getValue().equals(type)){ String relationId=auditOtherUser(byId); userGatherApply.setRelationId(relationId); }else if(UserEnum.废品店类型.getValue().equals(type)){ String relationId=auditScrapStore(byId); userGatherApply.setRelationId(relationId); }else if(UserEnum.集货员类型.getValue().equals(type)) { String relationId=auditGather(byId); userGatherApply.setRelationId(relationId); } } //更新审核状态 userGatherApply.setExamineTime(DateUtil.now()); userGatherApplyMapper.updateByPrimaryKeySelective(userGatherApply); } }