package com.matrix.system.fenxiao.service; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.matrix.biz.bean.BizUser; import com.matrix.biz.dao.BizUserDao; import com.matrix.biz.service.BizUserService; import com.matrix.core.exception.GlobleException; import com.matrix.core.tools.StringUtils; import com.matrix.system.common.bean.BusParameterSettings; import com.matrix.system.common.dao.BusParameterSettingsDao; import com.matrix.system.fenxiao.constant.FenxiaoSettingConstant; import com.matrix.system.fenxiao.dao.ShopSalesmanApplyDao; import com.matrix.system.fenxiao.dao.ShopSalesmanGradeDao; import com.matrix.system.fenxiao.dto.ShopSalesmanAppliingDto; import com.matrix.system.fenxiao.dto.ShopSalesmanApplyDto; import com.matrix.system.fenxiao.entity.ShopSalesmanApply; import com.matrix.system.fenxiao.entity.ShopSalesmanGrade; import com.matrix.system.fenxiao.vo.ShopSalesmanAppliingVo; import com.matrix.system.fenxiao.vo.ShopSalesmanApplyVo; import com.matrix.system.fenxiao.vo.ShopSalesmanGradeVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.Date; import java.util.List; /** * @description 推广员申请记录 * @author jyy * @date 2021-03-10 15:22 */ @Service public class ShopSalesmanApplyService extends ServiceImpl{ @Autowired BusParameterSettingsDao busParameterSettingsDao; @Autowired ShopSalesmanApplyDao shopSalesmanApplyDao; @Autowired ShopSalesmanGradeDao shopSalesmanGradeDao; @Autowired private BizUserDao bizUserDao; @Autowired BizUserService bizUserService; /** * 申请成为推广员 * @param openId * @param invitationId * @return */ public ShopSalesmanApply applyToBeAnSalesman(String openId,String gradeId,String invitationId,int applyWay) { BizUser loginUser=bizUserDao.findByOpenId(openId); //校验审核状态,和是否重复发起 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("user_id",loginUser.getOpenId()); ShopSalesmanApply checkApply = shopSalesmanApplyDao.selectOne(queryWrapper); if(checkApply==null|| checkApply.getApplyStatus()==ShopSalesmanApply.APPLY_STATUS_WTG){ ShopSalesmanApply shopSalesmanApply=new ShopSalesmanApply(); shopSalesmanApply.setUserId(openId); shopSalesmanApply.setCreateBy(loginUser.getNickName()); shopSalesmanApply.setApplyWay(ShopSalesmanApply.APPLY_WAY_SELF); shopSalesmanApply.setCompanyId(loginUser.getCompanyId()); shopSalesmanApply.setUpdateBy(loginUser.getNickName()); Date date = new Date(); shopSalesmanApply.setCreateTime(date); shopSalesmanApply.setUpdateTime(date); if(StringUtils.isNotBlank(invitationId)){ shopSalesmanApply.setParentUserId(invitationId); }else if(StringUtils.isNotBlank(loginUser.getParentOpenId())){ //如果曾经是被邀请进来的则自动绑定为之前邀请人的下级 shopSalesmanApply.setParentUserId(loginUser.getParentOpenId()); } if(StringUtils.isNotBlank(gradeId)){ shopSalesmanApply.setGradeId(Long.parseLong(gradeId)); }else{ //获取初始等级ID(公司id,是否为初始等级) Wrapper queryWrapperOrepool = new QueryWrapper<>(); ((QueryWrapper) queryWrapperOrepool).eq("company_id", loginUser.getCompanyId()); ((QueryWrapper) queryWrapperOrepool).eq("is_default", 1); ShopSalesmanGrade shopSalesmanGrade = shopSalesmanGradeDao.selectOne(queryWrapperOrepool); shopSalesmanApply.setGradeId(shopSalesmanGrade.getId()); } shopSalesmanApply.setApplyWay(applyWay); BusParameterSettings busParameterSettings = busParameterSettingsDao.selectCompanyParamByCode(FenxiaoSettingConstant.FX_AUDIT_METHOD, loginUser.getCompanyId()); if(busParameterSettings!=null &&busParameterSettings.getParamValue().equals("1")){ //自动审核 shopSalesmanApply.setApplyStatus(ShopSalesmanApply.APPLY_STATUS_TG); bizUserService.setToBeAnSalesman(loginUser.getOpenId(),invitationId); }else{ shopSalesmanApply.setApplyStatus(ShopSalesmanApply.APPLY_STATUS_DSH); } shopSalesmanApplyDao.insert(shopSalesmanApply); return shopSalesmanApply; }else{ throw new GlobleException("不能重复申请"); } } public IPage findShopSalesmanApplyList(Page page, ShopSalesmanApplyDto shopSalesmanApplyDto) { return shopSalesmanApplyDao.findShopSalesmanApplyList(page,shopSalesmanApplyDto); } public IPage findShopSalesmanAppliingList(Page page, ShopSalesmanAppliingDto shopSalesmanAppliingDto) { return shopSalesmanApplyDao.findShopSalesmanAppliingList(page,shopSalesmanAppliingDto); } public IPage selectBizUserApplyList(Page page, ShopSalesmanAppliingDto shopSalesmanAppliingDto) { return shopSalesmanApplyDao.selectBizUserApplyList(page,shopSalesmanAppliingDto); } public List getShopSalesmanGradeVo(Long companyId) { return shopSalesmanGradeDao.getShopSalesmanGradeVo(companyId); } @Transactional(rollbackFor = Exception.class) public void addSaleManApply(String userId,String gradeId) { BizUser user = bizUserDao.selectById(userId); applyToBeAnSalesman(user.getOpenId(),gradeId, "",ShopSalesmanApply.APPLY_WAY_HAND_ADD); } @Transactional(rollbackFor = Exception.class) public void examineSaleManApply(ShopSalesmanApply shopSalesmanApply, Integer applyState) { String userId = shopSalesmanApply.getUserId(); String parentUserId = shopSalesmanApply.getParentUserId(); BizUser bizUser = bizUserDao.selectById(userId); //修改审核记录 if(ShopSalesmanApply.APPLY_STATUS_TG == applyState) { shopSalesmanApply.setApplyStatus(ShopSalesmanApply.APPLY_STATUS_TG); bizUser.setIsSales(BizUser.IS_SALES); }else { shopSalesmanApply.setApplyStatus(ShopSalesmanApply.APPLY_STATUS_WTG); bizUser.setIsSales(BizUser.NOT_SALES); } shopSalesmanApplyDao.updateById(shopSalesmanApply); //修改USER的状态 bizUser.setParentOpenId(parentUserId); bizUser.setBindingParentTime(new Date()); bizUserDao.updateByModel(bizUser); } }