123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- package com.ydd.module.service.impl;
- import com.aliyun.oss.OSSClient;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.google.zxing.common.BitMatrix;
- import com.ydd.common.constant.UserConstants;
- import com.ydd.common.core.domain.entity.SysUser;
- import com.ydd.common.enums.UserTypeEnums;
- import com.ydd.common.enums.UserTypeJoinRoleEnums;
- import com.ydd.common.exception.CustomException;
- import com.ydd.common.oss.cloud.CloudStorageConfig;
- import com.ydd.common.oss.cloud.OSSFactory;
- import com.ydd.common.utils.SecurityUtils;
- import com.ydd.ecloud.core.utils.OSSHelper;
- import com.ydd.module.domain.*;
- import com.ydd.module.dto.BaseConfgMinDto;
- import com.ydd.module.enums.PackageCityConfigPackageTypeEnum;
- import com.ydd.module.enums.StatusEnum;
- import com.ydd.module.expection.CustomWebException;
- import com.ydd.module.mapper.AgentMapper;
- import com.ydd.module.request.AgentBindRulePackageRequest;
- import com.ydd.module.service.*;
- import com.ydd.module.utils.BarCodeUtils;
- import com.ydd.module.utils.QRCodeUtil;
- import com.ydd.system.service.ISysUserService;
- import lombok.RequiredArgsConstructor;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import java.awt.image.BufferedImage;
- import java.io.InputStream;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Objects;
- /**
- * 代理商Service业务层处理
- *
- * @author douya
- * @date 2021-02-01
- */
- @RequiredArgsConstructor(onConstructor_ = @Autowired)
- @Service
- public class AgentServiceImpl extends ServiceImpl<AgentMapper, Agent> implements IAgentService {
- @Resource
- private IDspConfigService iDspConfigService;
- @Resource
- private IAreaService iAreaService;
- @Resource
- private ISysUserService userService;
- @Resource
- private IAgentLevelService iAgentLevelService;
- private final CloudStorageConfig cloudStorageConfig;
- @Resource
- private IPackageDiscountService iPackageDiscountService;
- @Resource
- private IPackageCommissionService iPackageCommissionService;
- @Override
- public List<Agent> queryList(Agent agent, SysUser user) {
- LambdaQueryWrapper<Agent> lqw = Wrappers.lambdaQuery();
- //处理不同身份权限
- if (user.getType().equals(UserTypeEnums.DSP.getCode())) {
- agent.setDspId(user.getDspId().intValue());
- }
- if (agent.getDspId() != null) {
- lqw.eq(Agent::getDspId, agent.getDspId());
- }
- if (agent.getPid() != null) {
- lqw.eq(Agent::getPid, agent.getPid());
- } else {
- if (user.getType().equals(UserTypeEnums.AGENT.getCode())) {
- lqw.eq(Agent::getPid, user.getAgentId()).or().eq(Agent::getId, user.getAgentId());
- }
- }
- if (StringUtils.isNotBlank(agent.getName())) {
- lqw.like(Agent::getName, agent.getName());
- }
- if (agent.getProvinceId() != null) {
- lqw.eq(Agent::getProvinceId, agent.getProvinceId());
- }
- if (agent.getCityId() != null) {
- lqw.eq(Agent::getCityId, agent.getCityId());
- }
- if (agent.getDistrictId() != null) {
- lqw.eq(Agent::getDistrictId, agent.getDistrictId());
- }
- if (agent.getAgentLevel() != null) {
- lqw.eq(Agent::getAgentLevel, agent.getAgentLevel());
- }
- if (agent.getCommissionRate() != null) {
- lqw.eq(Agent::getCommissionRate, agent.getCommissionRate());
- }
- if (StringUtils.isNotBlank(agent.getContactName())) {
- lqw.like(Agent::getContactName, agent.getContactName());
- }
- if (StringUtils.isNotBlank(agent.getAvatar())) {
- lqw.eq(Agent::getAvatar, agent.getAvatar());
- }
- if (StringUtils.isNotBlank(agent.getMobile())) {
- lqw.like(Agent::getMobile, agent.getMobile());
- }
- if (StringUtils.isNotBlank(agent.getAddress())) {
- lqw.eq(Agent::getAddress, agent.getAddress());
- }
- if (StringUtils.isNotBlank(agent.getProvinceName()) && StringUtils.isBlank(agent.getCityName()) && StringUtils.isBlank(agent.getDistrictName())){
- List<Agent> provinceList = baseMapper.selectList(new QueryWrapper<Agent>().like("province_name", agent.getProvinceName()).isNull("pid"));
- List<Long> provinceIds = new ArrayList<>();
- provinceList.forEach(province -> {
- provinceIds.add(province.getId());
- });
- if (provinceIds.size() > 0) {
- lqw.and(wrapper -> wrapper.in(Agent::getId, provinceIds).or().in(Agent::getPid, provinceIds));
- }else {
- lqw.eq(Agent::getId, - 1);
- }
- // lqw.like(Agent::getProvinceName ,agent.getProvinceName());
- }
- if (StringUtils.isNotBlank(agent.getCityName()) && StringUtils.isBlank(agent.getDistrictName())){
- List<Agent> cityList = baseMapper.selectList(new QueryWrapper<Agent>().like("province_name", agent.getProvinceName())
- .like("city_name", agent.getCityName()).isNull("pid"));
- List<Long> cityIds = new ArrayList<>();
- cityList.forEach(city -> {
- cityIds.add(city.getId());
- });
- if (cityIds.size() > 0) {
- lqw.and(wrapper -> wrapper.in(Agent::getId, cityIds).or().in(Agent::getPid, cityIds));
- }else {
- lqw.eq(Agent::getId, - 1);
- }
- // lqw.like(Agent::getCityName ,agent.getCityName());
- }
- if (StringUtils.isNotBlank(agent.getDistrictName())){
- List<Agent> districtList = baseMapper.selectList(new QueryWrapper<Agent>().like("province_name", agent.getProvinceName())
- .like("city_name", agent.getCityName())
- .like("district_name", agent.getDistrictName()).isNull("pid"));
- List<Long> districtIds = new ArrayList<>();
- districtList.forEach(district -> {
- districtIds.add(district.getId());
- });
- if (districtIds.size() > 0) {
- lqw.and(wrapper -> wrapper.in(Agent::getId, districtIds).or().in(Agent::getPid, districtIds));
- }else {
- lqw.eq(Agent::getId, - 1);
- }
- // lqw.like(Agent::getDistrictName ,agent.getDistrictName());
- }
- if (agent.getStartTime() != null) {
- lqw.ge(Agent::getCreateTime, agent.getStartTime());
- }
- if (agent.getEndTime() != null) {
- lqw.le(Agent::getCreateTime, agent.getEndTime());
- }
- lqw.orderByDesc(Agent::getCreateTime);
- List<Agent> list = this.list(lqw);
- List<AgentLevel> agentLevelList = iAgentLevelService.queryList(new AgentLevel(), user);
- list.stream().forEach(agent1 -> {
- agent1.setDspName(iDspConfigService.getById(agent1.getDspId()).getName());
- Agent parentAgent = this.getById(agent1.getPid());
- if (parentAgent != null) {
- agent1.setPAgentName(parentAgent.getName());
- }
- //判断查看代理商用户是否拥有编辑权限
- if (user.getType().equals(UserTypeEnums.AGENT.getCode())) {
- // agent1.setIsEdit(agent1.getPid().equals(user.getAgentId()) ? 1 : 0);
- agent1.setIsEdit(agent1.getPid() == null ? 1 : 0);
- } else {
- //非代理商角色拥有所有编辑权限
- agent1.setIsEdit(1);
- }
- //遍历代理商等级名称
- for (AgentLevel agentLevel : agentLevelList) {
- if (agentLevel.getLevel().equals(agent1.getAgentLevel())) {
- agent1.setLevelName(agentLevel.getName());
- break;
- }
- }
- // 下级代理商省市区为上级
- if (agent1.getPid() != null) {
- Agent superAgent = baseMapper.getByPid(agent1.getPid());
- if (superAgent != null && superAgent.getPid() == null) {
- agent1.setProvinceName(superAgent.getProvinceName());
- agent1.setCityName(superAgent.getCityName());
- agent1.setDistrictName(superAgent.getDistrictName());
- }else if (superAgent != null && superAgent.getPid() != null) {
- Agent superLowAgent = this.getById(superAgent.getPid());
- agent1.setProvinceName(superLowAgent.getProvinceName());
- agent1.setCityName(superLowAgent.getCityName());
- agent1.setDistrictName(superLowAgent.getDistrictName());
- }
- }
- if (agent1.getPackageDiscountId() != null) {
- PackageDiscount discount = iPackageDiscountService.getById(agent1.getPackageDiscountId());
- if (discount != null) {
- agent1.setPackageDiscountName(discount.getName());
- }
- }
- if (agent1.getPackageCommissionId() != null) {
- PackageCommission commission = iPackageCommissionService.getById(agent1.getPackageCommissionId());
- if (commission != null) {
- agent1.setPackageCommissionName(commission.getName());
- }
- }
- });
- return list;
- }
- @Override
- public List<Agent> selectAgnetList(Agent agent, SysUser user) {
- List<Agent> list = new ArrayList<>();
- if (user.getType().equals(UserTypeEnums.AGENT.getCode())) {
- list = baseMapper.getAgentAll(user.getAgentId());
- }else {
- list = baseMapper.selectAll();
- }
- return list;
- }
- @Override
- public List<Agent> getAgentAll( SysUser user) {
- return baseMapper.getAgentAll(user.getAgentId());
- }
- @Override
- @Transactional(rollbackFor = Exception.class)
- public Boolean createAgent(Agent agent, SysUser user) {
- Agent agents = baseMapper.selectByMobile(agent.getMobile(), null);
- // Integer gentLevel = null;
- if (agents != null) {
- throw new CustomException("代理商已存在,请勿重复创建!");
- }
- if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(agent.getMobile()))) {
- throw new CustomException("该手机号码为系统管理员账号,创建失败!");
- }
- if (agent.getMobile().length() > 11) {
- throw new CustomException("手机号过长,规定为11位!");
- }
- if (user.getType().equals(UserTypeEnums.DSP.getCode())) {
- agent.setDspId(user.getDspId().intValue());
- // gentLevel = iAgentLevelService.getMaxLevelByDspId(user.getDspId().intValue(),null);
- } else if (user.getType().equals(UserTypeEnums.AGENT.getCode())) {
- Agent parentAgent = this.getById(user.getAgentId());
- if (parentAgent != null) {
- agent.setPid(parentAgent.getId());
- agent.setDspId(parentAgent.getDspId());
- agent.setType(parentAgent.getType());
- // gentLevel = iAgentLevelService.getMaxLevelByDspId(user.getDspId().intValue(),parentAgent.getAgentLevel().intValue());
- }
- } else {
- throw new CustomException("风险操作,已被系统拦截!");
- }
- // agent.setProvinceName(iAreaService.getName(agent.getProvinceId().intValue()));
- // agent.setCityName(iAreaService.getName(agent.getCityId().intValue()));
- // agent.setDistrictName(iAreaService.getName(agent.getDistrictId().intValue()));
- agent.setProvinceName(agent.getProvinceName());
- agent.setCityName(agent.getCityName());
- agent.setDistrictName(agent.getDistrictName());
- this.save(agent);
- String path = "http://h5.liebaoai.cn/register.html?id=" + agent.getId();
- if (!agent.getId().equals(null)) {
- BufferedImage bufferedImage = BarCodeUtils.generateBarcodeWithoutWhite(path, null);
- InputStream inputStream = BarCodeUtils.uploadFileToOss(bufferedImage, null);
- String url = OSSFactory.build(cloudStorageConfig).uploadSuffix(inputStream, "." + "jpg");
- if (StringUtils.isNotBlank(url)) {
- agent.setQrCode(url);
- baseMapper.updateById(agent);
- }
- }
- //创建代理商后台账号
- SysUser agentUser = new SysUser();
- agentUser.setUserName(agent.getMobile());
- agentUser.setNickName(agent.getMobile());
- agentUser.setType(UserTypeEnums.AGENT.getCode());
- agentUser.setPhonenumber(agent.getMobile());
- agentUser.setAgentId(agent.getId());
- agentUser.setDspId(agent.getDspId().longValue());
- agentUser.setPassword(agent.getMobile().substring(6));
- agentUser.setCreateBy(SecurityUtils.getUsername());
- agentUser.setPassword(SecurityUtils.encryptPassword(agentUser.getPassword()));
- Long[] roleIds = {UserTypeJoinRoleEnums.AGENT.getRoleId()};
- agentUser.setRoleIds(roleIds);
- userService.insertUser(agentUser);
- return true;
- }
- @Override
- @Transactional(rollbackFor = Exception.class)
- public Boolean updateAgent(Agent agent) {
- Agent agents = baseMapper.selectByMobile(agent.getMobile(), agent.getId());
- if (agents != null) {
- throw new CustomException("代理商已存在,请勿重复创建!");
- }
- this.updateById(agent);
- SysUser user = userService.selectUserByUserName(agent.getMobilePhone());
- if (user != null && !agent.getMobile().equals(agent.getMobilePhone())) {
- user.setUserName(agent.getMobile());
- user.setNickName(agent.getMobile());
- user.setPhonenumber(agent.getMobile());
- user.setPassword(agent.getMobile().substring(6));
- user.setCreateBy(SecurityUtils.getUsername());
- user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
- Long[] roleIds = {UserTypeJoinRoleEnums.AGENT.getRoleId()};
- user.setRoleIds(roleIds);
- userService.updateUser(user);
- }
- return true;
- }
- @Override
- public BigDecimal getMaxRete(SysUser user) {
- if (user.getType().equals(UserTypeEnums.PLATFORM.getCode())) {
- return new BigDecimal(100);
- } else if (user.getType().equals(UserTypeEnums.DSP.getCode())) {
- DspConfig dspConfig = iDspConfigService.getById(user.getDspId());
- if (dspConfig == null) {
- throw new CustomException("未找到登录信息,请您重新登录!");
- }
- return dspConfig.getCommissionRate();
- } else if (user.getType().equals(UserTypeEnums.AGENT.getCode())) {
- Agent agent = this.getById(user.getAgentId());
- if (agent == null) {
- throw new CustomException("未找到登录信息,请您重新登录!");
- }
- return agent.getCommissionRate();
- }
- return null;
- }
- @Override
- public Agent getInfoById(Long id) {
- Agent agent = this.getById(id);
- if(agent != null) {
- if (agent.getPid() == null) {
- agent.setIsPid(0);
- }else {
- Agent superAgent = this.getById(agent.getPid());
- if (superAgent != null && superAgent.getPid() == null) {
- agent.setProvinceName(superAgent.getProvinceName());
- agent.setCityName(superAgent.getCityName());
- agent.setDistrictName(superAgent.getDistrictName());
- }else if (superAgent != null && superAgent.getPid() != null) {
- Agent superLowAgent = this.getById(superAgent.getPid());
- agent.setProvinceName(superLowAgent.getProvinceName());
- agent.setCityName(superLowAgent.getCityName());
- agent.setDistrictName(superLowAgent.getDistrictName());
- }
- agent.setIsPid(agent.getPid().intValue());
- }
- }
- if (agent != null) {
- AgentLevel level = iAgentLevelService.getByLevel(agent.getAgentLevel().intValue(), agent.getDspId());
- if (level != null) {
- agent.setLevelName(level.getName());
- }
- DspConfig dspConfig = iDspConfigService.getById(agent.getDspId());
- if (dspConfig != null) {
- agent.setDspName(dspConfig.getName());
- }
- }
- return agent;
- }
- @Override
- public Agent getInfo(Long id, SysUser user) {
- Agent agent = this.getById(id);
- if(agent != null) {
- if (agent.getPid() == null) {
- agent.setIsPid(0);
- }else {
- Agent superAgent = this.getById(agent.getPid());
- if (superAgent != null && superAgent.getPid() == null) {
- agent.setProvinceName(superAgent.getProvinceName());
- agent.setCityName(superAgent.getCityName());
- agent.setDistrictName(superAgent.getDistrictName());
- }else if (superAgent != null && superAgent.getPid() != null) {
- Agent superLowAgent = this.getById(superAgent.getPid());
- agent.setProvinceName(superLowAgent.getProvinceName());
- agent.setCityName(superLowAgent.getCityName());
- agent.setDistrictName(superLowAgent.getDistrictName());
- }
- agent.setIsPid(agent.getPid().intValue());
- }
- }
- if (agent != null) {
- AgentLevel level = iAgentLevelService.getByLevel(agent.getAgentLevel().intValue(), agent.getDspId());
- if (level != null) {
- agent.setLevelName(level.getName());
- }
- DspConfig dspConfig = iDspConfigService.getById(agent.getDspId());
- if (dspConfig != null) {
- agent.setDspName(dspConfig.getName());
- }
- }
- // 判断当前账号是否拥有查看详情权限
- if (user.getType().equals(UserTypeEnums.AGENT.getCode())) {
- List<Long> agentIds = baseMapper.listAgent(user.getAgentId());
- if (agentIds.size() > 0) {
- Boolean flag = false;
- List<Long> agentAllIds = baseMapper.listLowAgent(agentIds, user.getAgentId());
- for (Long agentId : agentAllIds) {
- if (agentId.longValue() == id.longValue()) {
- flag = true;
- }
- }
- if (flag == true) {
- agent.setIsPermission(true);
- }else {
- agent.setIsPermission(false);
- }
- }
- }
- if (agent != null && agent.getPid() != null) {
- Agent supper = baseMapper.getByPid(agent.getPid());
- agent.setBindingThirdMax(supper.getBindingThirdCommission());
- }
- return agent;
- }
- @Override
- public List<Agent> selectAgentListByStatus(Integer status) {
- return baseMapper.selectAgentListByStatus(status);
- }
- @Override
- public List<Agent> selectByPid(Integer id) {
- return baseMapper.selectByPid(id);
- }
- @Override
- public Integer getSAgentNum(Integer pid, String todayStartDate) {
- return baseMapper.getSAgentNum(pid, todayStartDate);
- }
- @Override
- public void updateAgentListAmount(List<Agent> list) {
- this.updateBatchById(list, list.size());
- }
- @Override
- public List<Integer> getAgentIdsByPid(Integer id) {
- return baseMapper.getAgentIdsByPid(id);
- }
- @Override
- public List<Long> listAgent(Long id) {
- return baseMapper.listAgent(id);
- }
- /**
- * 查询自己和下级以及下下级的id
- *
- * @param id
- * @return
- */
- @Override
- public List<Integer> getIds(Integer id) {
- List<Integer> agentIds = new ArrayList<>();
- List<Integer> ids = baseMapper.getAgentIdsByPid(id);
- for (int i = 0; i < ids.size(); i++) {
- agentIds.add(ids.get(i));
- agentIds.addAll(baseMapper.getAgentIdsByPid(ids.get(i)));
- }
- return agentIds;
- }
- /**
- * 根据渠道商查找代理商的id
- *
- * @param dspId
- * @return
- */
- @Override
- public List<Integer> getAgentIdsByDspId(Integer dspId) {
- return baseMapper.getAgentIdsByDspId(dspId);
- }
- /**
- * 按区域查询代理商(一级代理商)
- *
- * @param cityName 市
- * @param districtName 区
- * @return
- */
- @Override
- public Agent findByArea(String cityName, String districtName) {
- return baseMapper.findByArea(cityName, districtName);
- }
- /**
- * 根据pid查找一级代理商
- *
- * @param pid
- * @return
- */
- @Override
- public Agent getByPid(Long pid) {
- return baseMapper.getByPid(pid);
- }
- /**
- * 查询代理商的下级代理商
- * @param pid
- * @return
- */
- @Override
- public List<Long> selectByPid(Long pid) {
- return baseMapper.lowerAgent(pid);
- }
- @Override
- @Transactional(rollbackFor = Exception.class)
- public void updateAgentStatus(Integer id){
- Agent agent = this.getById(id);
- if (agent != null){
- if (agent.getPid() != null && agent.getStatus().equals(StatusEnum.SHOW.status)) {
- agent.setBindingThirdCommission(BigDecimal.ZERO);
- agent.setHuolalaCommission(BigDecimal.ZERO);
- agent.setCommissionRate(BigDecimal.ZERO);
- }
- agent.setStatus(agent.getStatus().equals(0) ? 1 : 0);
- this.updateById(agent);
- }
- }
- @Override
- public BaseConfgMinDto getBaseConfigMin(Integer type) {
- return baseMapper.getBaseConfigMin(type);
- }
- @Override
- public BigDecimal getMinBindFee(String cityId) {
- if (cityId.equals("0")) {
- return BigDecimal.ZERO;
- }else {
- return baseMapper.getMinBindFee(cityId);
- }
- }
- @Override
- public void bindValuationRulePackage(AgentBindRulePackageRequest request) {
- Agent agent = baseMapper.selectById(request.getAgentId());
- if (Objects.isNull(agent)) {
- throw new CustomWebException("代理商不存在!");
- }
- UpdateWrapper<Agent> updateWrapper = new UpdateWrapper<>();
- if (PackageCityConfigPackageTypeEnum.DISCOUNT.type.equals(request.getPackageType())) {
- updateWrapper.set("package_discount_id", request.getPackageId())
- .eq("id", agent.getId());
- // agent.setPackageDiscountId(request.getPackageId());
- } else if (PackageCityConfigPackageTypeEnum.COMMISSION.type.equals(request.getPackageType())) {
- updateWrapper.set("package_commission_id", request.getPackageId())
- .eq("id", agent.getId());
- // agent.setPackageCommissionId(request.getPackageId());
- } else {
- throw new CustomWebException("无效的包类型!");
- }
- baseMapper.update(agent, updateWrapper);
- }
- }
|