package com.ruoyi.web.controller.api;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.TCrmSalespersonDTO;
import com.ruoyi.system.model.TCrmBranch;
import com.ruoyi.system.model.TCrmChangePoints;
import com.ruoyi.system.model.TCrmSalesperson;
import com.ruoyi.system.query.TCrmSalespersonQuery;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.TCrmBranchService;
import com.ruoyi.system.service.TCrmChangePointsService;
import com.ruoyi.system.service.TCrmSalespersonService;
import com.ruoyi.system.vo.TCrmSalespersonVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
*
* crm业务员管理 前端控制器
*
*
* @author xiaochen
* @since 2025-08-20
*/
@Api(tags = "crm业务员管理")
@RestController
@RequestMapping("/t-crm-salesperson")
public class TCrmSalespersonController {
private final TCrmSalespersonService crmSalespersonService;
private final ISysUserService sysUserService;
private final TCrmChangePointsService crmChangePointsService;
private final TCrmBranchService crmBranchService;
private final TokenService tokenService;
@Autowired
public TCrmSalespersonController(TCrmSalespersonService crmSalespersonService, ISysUserService sysUserService, TCrmChangePointsService crmChangePointsService, TCrmBranchService crmBranchService, TokenService tokenService) {
this.crmSalespersonService = crmSalespersonService;
this.sysUserService = sysUserService;
this.crmChangePointsService = crmChangePointsService;
this.crmBranchService = crmBranchService;
this.tokenService = tokenService;
}
/**
* 获取crm业务员管理管理列表
*/
@ApiOperation(value = "获取crm业务员管理分页列表")
@PostMapping(value = "/pageList")
public R> pageList(@RequestBody TCrmSalespersonQuery query) {
Long userId = tokenService.getLoginUser().getUser().getUserId();
Integer roleType = tokenService.getLoginUser().getUser().getRoleType();
if(roleType == 2){
// 查询分公司信息
TCrmBranch crmBranch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
.eq(TCrmBranch::getUserId, userId)
.last("LIMIT 1"));
query.setBranchId(crmBranch.getId());
}
return R.ok(crmSalespersonService.pageList(query));
}
/**
* 获取crm业务员管理管理列表
*/
@ApiOperation(value = "获取crm业务员管理列表")
@PostMapping(value = "/list")
public R> list() {
Long userId = tokenService.getLoginUser().getUser().getUserId();
Integer roleType = tokenService.getLoginUser().getUser().getRoleType();
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
if(roleType == 2){
// 查询分公司信息
TCrmBranch crmBranch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
.eq(TCrmBranch::getUserId, userId)
.last("LIMIT 1"));
wrapper.eq(TCrmSalesperson::getBranchId,crmBranch.getId());
}
wrapper.eq(TCrmSalesperson::getStatus,1)
.orderByDesc(TCrmSalesperson::getCreateTime);
return R.ok(crmSalespersonService.list(wrapper));
}
/**
* 添加crm业务员管理管理
*/
@Log(title = "crm业务员管理信息-新增crm业务员管理", businessType = BusinessType.INSERT)
@ApiOperation(value = "添加crm业务员管理")
@PostMapping(value = "/add")
public R add(@Validated @RequestBody TCrmSalespersonDTO dto) {
return crmSalespersonService.addSalesperson(dto);
}
/**
* 修改crm业务员管理
*/
@Log(title = "crm业务员管理信息-修改crm业务员管理", businessType = BusinessType.UPDATE)
@ApiOperation(value = "修改crm业务员管理")
@PostMapping(value = "/update")
public R update(@Validated @RequestBody TCrmSalespersonDTO dto) {
return crmSalespersonService.updateSalesperson(dto);
}
/**
* 查看crm业务员管理详情
*/
@ApiOperation(value = "查看crm业务员管理详情")
@GetMapping(value = "/getDetailById")
public R getDetailById(@RequestParam String id) {
TCrmSalesperson crmSalesperson = crmSalespersonService.getById(id);
TCrmSalespersonVO crmSalespersonVO = new TCrmSalespersonVO();
BeanUtils.copyProperties(crmSalesperson, crmSalespersonVO);
// 查询分公司
TCrmBranch crmBranch = crmBranchService.getById(crmSalesperson.getBranchId());
if (Objects.nonNull(crmBranch)) {
crmSalespersonVO.setBranchName(crmBranch.getBranchName());
}
return R.ok(crmSalespersonVO);
}
/**
* 删除crm业务员管理
*/
@Log(title = "crm业务员管理信息-删除crm业务员管理", businessType = BusinessType.DELETE)
@ApiOperation(value = "删除crm业务员管理")
@DeleteMapping(value = "/deleteById")
public R deleteById(@RequestParam String id) {
// 删除账号
TCrmSalesperson crmBranch = crmSalespersonService.getById(id);
sysUserService.deleteUserById(crmBranch.getUserId());
return R.ok(crmSalespersonService.removeById(id));
}
/**
* 批量删除crm业务员管理
*/
@Log(title = "crm业务员管理信息-删除crm业务员管理", businessType = BusinessType.DELETE)
@ApiOperation(value = "批量删除crm业务员管理")
@DeleteMapping(value = "/deleteByIds")
public R deleteByIds(@RequestBody List ids) {
// 删除账号
List crmBranchList = crmSalespersonService.list(Wrappers.lambdaQuery(TCrmSalesperson.class).in(TCrmSalesperson::getId, ids));
List userIds = crmBranchList.stream().map(TCrmSalesperson::getUserId).collect(Collectors.toList());
sysUserService.deleteUserByIds(userIds);
return R.ok(crmSalespersonService.removeByIds(ids));
}
/**
* 批量删除crm业务员管理
*/
@Log(title = "crm业务员管理信息-业务员管理解冻冻结", businessType = BusinessType.UPDATE)
@ApiOperation(value = "业务员管理解冻冻结",notes = "状态 1=使用中 2=冻结")
@PutMapping(value = "/thawOrFreeze")
public R thawOrFreeze(@RequestParam(value = "id")String id,
@RequestParam(value = "status")Integer status) {
TCrmSalesperson crmSalesperson = crmSalespersonService.getById(id);
crmSalesperson.setStatus(status);
crmSalespersonService.updateById(crmSalesperson);
// 查询用户
SysUser sysUser = sysUserService.selectUserById(crmSalesperson.getUserId());
switch (status){
case 1:
// 解冻
sysUser.setStatus("0");
sysUserService.updateUser(sysUser);
break;
case 2:
// 冻结
sysUser.setStatus("1");
sysUserService.updateUser(sysUser);
break;
}
return R.ok();
}
/**
* 批量删除crm业务员管理
*/
@Log(title = "crm业务员管理信息-业务员管理修改积分", businessType = BusinessType.DELETE)
@ApiOperation(value = "业务员管理修改积分")
@PutMapping(value = "/changePoints")
public R changePoints(@RequestBody TCrmChangePoints changePoints) {
// 查询分公司信息
TCrmSalesperson crmSalesperson = crmSalespersonService.getById(changePoints.getBranchSalespersonId());
if (Objects.isNull(crmSalesperson)) {
return R.fail("未查询到该业务员");
}
switch (changePoints.getChangeType()){
case 1:
// 增加积分
crmSalesperson.setUserPoints(crmSalesperson.getUserPoints() + changePoints.getChangeValue());
break;
case 2:
// 减少积分
if (crmSalesperson.getUserPoints() < changePoints.getChangeValue()) {
return R.fail("操作失败,剩余积分数不足");
}
crmSalesperson.setUserPoints(crmSalesperson.getUserPoints() - changePoints.getChangeValue());
break;
}
crmSalespersonService.updateById(crmSalesperson);
changePoints.setUserType(2);
crmChangePointsService.save(changePoints);
return R.ok();
}
}