| package com.ruoyi.web.controller.api; | 
|   | 
|   | 
| import cn.afterturn.easypoi.excel.ExcelExportUtil; | 
| import cn.afterturn.easypoi.excel.entity.ExportParams; | 
| 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.common.utils.WebUtils; | 
| import com.ruoyi.framework.web.service.TokenService; | 
| import com.ruoyi.system.dto.TCrmBranchDTO; | 
| import com.ruoyi.system.export.TCrmBranchExport; | 
| import com.ruoyi.system.export.TSysAppUserClinicExport; | 
| import com.ruoyi.system.export.TSysAppUserExport; | 
| import com.ruoyi.system.model.*; | 
| import com.ruoyi.system.query.TCrmBranchQuery; | 
| import com.ruoyi.system.query.TSysAppUserQuery; | 
| import com.ruoyi.system.service.*; | 
| import com.ruoyi.system.vo.TCrmBranchVO; | 
| import io.swagger.annotations.Api; | 
| import io.swagger.annotations.ApiOperation; | 
| import org.apache.poi.ss.usermodel.Workbook; | 
| import org.springframework.beans.BeanUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.util.CollectionUtils; | 
| import org.springframework.validation.annotation.Validated; | 
| import org.springframework.web.bind.annotation.*; | 
|   | 
| import javax.servlet.ServletOutputStream; | 
| import javax.servlet.http.HttpServletResponse; | 
| import java.io.IOException; | 
| import java.net.URLEncoder; | 
| import java.text.SimpleDateFormat; | 
| import java.util.List; | 
| import java.util.Objects; | 
| import java.util.stream.Collectors; | 
|   | 
| /** | 
|  * <p> | 
|  * crm分公司管理 前端控制器 | 
|  * </p> | 
|  * | 
|  * @author xiaochen | 
|  * @since 2025-08-20 | 
|  */ | 
| @Api(tags = "crm分公司管理") | 
| @RestController | 
| @RequestMapping("/t-crm-branch") | 
| public class TCrmBranchController { | 
|   | 
|     private final TCrmBranchService crmBranchService; | 
|     private final TCrmBranchAreaService crmBranchAreaService; | 
|     private final TCrmBranchSalaryService crmBranchSalaryService; | 
|     private final ISysUserService sysUserService; | 
|     private final TCrmChangePointsService crmChangePointsService; | 
|     private final TCrmPositionService crmPositionService; | 
|     private final TCrmSalespersonService crmSalespersonService; | 
|     private final TokenService tokenService; | 
|     @Autowired | 
|     public TCrmBranchController(TCrmBranchService crmBranchService, TCrmBranchAreaService crmBranchAreaService, TCrmBranchSalaryService crmBranchSalaryService, ISysUserService sysUserService, TCrmChangePointsService crmChangePointsService, TCrmPositionService crmPositionService, TCrmSalespersonService crmSalespersonService, TokenService tokenService) { | 
|         this.crmBranchService = crmBranchService; | 
|         this.crmBranchAreaService = crmBranchAreaService; | 
|         this.crmBranchSalaryService = crmBranchSalaryService; | 
|         this.sysUserService = sysUserService; | 
|         this.crmChangePointsService = crmChangePointsService; | 
|         this.crmPositionService = crmPositionService; | 
|         this.crmSalespersonService = crmSalespersonService; | 
|         this.tokenService = tokenService; | 
|     } | 
|   | 
|     /** | 
|      * 获取crm分公司管理管理列表 | 
|      */ | 
|     @ApiOperation(value = "获取crm分公司管理分页列表") | 
|     @PostMapping(value = "/pageList") | 
|     public R<PageInfo<TCrmBranchVO>> pageList(@RequestBody TCrmBranchQuery query) { | 
|         return R.ok(crmBranchService.pageList(query)); | 
|     } | 
|   | 
|     /** | 
|      * 获取crm分公司管理管理列表 | 
|      */ | 
|     @ApiOperation(value = "获取crm分公司管理列表") | 
|     @PostMapping(value = "/list") | 
|     public R<List<TCrmBranch>> list() { | 
|         Integer roleType = tokenService.getLoginUser().getUser().getRoleType(); | 
|         Long userId = tokenService.getLoginUser().getUserId(); | 
|         LambdaQueryWrapper<TCrmBranch> wrapper = new LambdaQueryWrapper<>(); | 
|         if (roleType == 2) { | 
|             wrapper.eq(TCrmBranch::getUserId, userId); | 
|         } | 
|         if(roleType == 3){ | 
|             // 查询业务员信息 | 
|             TCrmSalesperson crmSalesperson = crmSalespersonService.getOne(Wrappers.lambdaQuery(TCrmSalesperson.class) | 
|                     .eq(TCrmSalesperson::getUserId, userId) | 
|                     .last("LIMIT 1")); | 
|             if(Objects.nonNull(crmSalesperson)){ | 
|                 wrapper.eq(TCrmBranch::getId, crmSalesperson.getBranchId()); | 
|             } | 
|         } | 
|         wrapper.eq(TCrmBranch::getStatus,1).orderByDesc(TCrmBranch::getCreateTime); | 
|         return R.ok(crmBranchService.list(wrapper)); | 
|     } | 
|   | 
|     /** | 
|      * 添加crm分公司管理管理 | 
|      */ | 
|     @Log(title = "crm分公司管理信息-新增crm分公司管理", businessType = BusinessType.INSERT) | 
|     @ApiOperation(value = "添加crm分公司管理") | 
|     @PostMapping(value = "/add") | 
|     public R<Boolean> add(@Validated @RequestBody TCrmBranchDTO dto) { | 
|         return crmBranchService.addBranch(dto); | 
|     } | 
|   | 
|     /** | 
|      * 修改crm分公司管理 | 
|      */ | 
|     @Log(title = "crm分公司管理信息-修改crm分公司管理", businessType = BusinessType.UPDATE) | 
|     @ApiOperation(value = "修改crm分公司管理") | 
|     @PostMapping(value = "/update") | 
|     public R<Boolean> update(@Validated @RequestBody TCrmBranchDTO dto) { | 
|         return crmBranchService.updateBranch(dto); | 
|     } | 
|   | 
|     /** | 
|      * 查看crm分公司管理详情 | 
|      */ | 
|     @ApiOperation(value = "查看crm分公司管理详情") | 
|     @GetMapping(value = "/getDetailById") | 
|     public R<TCrmBranchVO> getDetailById(@RequestParam String id) { | 
|         TCrmBranch crmBranch = crmBranchService.getById(id); | 
|         TCrmBranchVO crmBranchVO = new TCrmBranchVO(); | 
|         BeanUtils.copyProperties(crmBranch, crmBranchVO); | 
|         // 查询区域 | 
|         List<TCrmBranchArea> crmBranchAreas = crmBranchAreaService.list(Wrappers.lambdaQuery(TCrmBranchArea.class).eq(TCrmBranchArea::getBranchId, id)); | 
|         crmBranchVO.setCrmBranchAreas(crmBranchAreas); | 
|         // 职位薪资 | 
|         List<TCrmPosition> crmPositions = crmPositionService.list(); | 
|         List<TCrmBranchSalary> crmBranchSalaries = crmBranchSalaryService.list(Wrappers.lambdaQuery(TCrmBranchSalary.class).eq(TCrmBranchSalary::getBranchId, id)); | 
|         for (TCrmBranchSalary crmBranchSalary : crmBranchSalaries) { | 
|             crmPositions.stream().filter(crmPosition -> Objects.equals(crmPosition.getId(), crmBranchSalary.getPositionId())).findFirst().ifPresent(crmPosition -> crmBranchSalary.setPositionName(crmPosition.getPositionName())); | 
|         } | 
|         crmBranchVO.setCrmBranchSalaries(crmBranchSalaries); | 
|         return R.ok(crmBranchVO); | 
|     } | 
|   | 
|     /** | 
|      * 删除crm分公司管理 | 
|      */ | 
|     @Log(title = "crm分公司管理信息-删除crm分公司管理", businessType = BusinessType.DELETE) | 
|     @ApiOperation(value = "删除crm分公司管理") | 
|     @DeleteMapping(value = "/deleteById") | 
|     public R<Boolean> deleteById(@RequestParam String id) { | 
|         // 删除区域 | 
|         crmBranchAreaService.remove(Wrappers.lambdaQuery(TCrmBranchArea.class).eq(TCrmBranchArea::getBranchId, id)); | 
|         // 删除职位薪资 | 
|         crmBranchSalaryService.remove(Wrappers.lambdaQuery(TCrmBranchSalary.class).eq(TCrmBranchSalary::getBranchId, id)); | 
|         // 删除账号 | 
|         TCrmBranch crmBranch = crmBranchService.getById(id); | 
|         sysUserService.deleteUserById(crmBranch.getUserId()); | 
|         return R.ok(crmBranchService.removeById(id)); | 
|     } | 
|   | 
|     /** | 
|      * 批量删除crm分公司管理 | 
|      */ | 
|     @Log(title = "crm分公司管理信息-删除crm分公司管理", businessType = BusinessType.DELETE) | 
|     @ApiOperation(value = "批量删除crm分公司管理") | 
|     @DeleteMapping(value = "/deleteByIds") | 
|     public R<Boolean> deleteByIds(@RequestBody List<String> ids) { | 
|         // 删除区域 | 
|         crmBranchAreaService.remove(Wrappers.lambdaQuery(TCrmBranchArea.class).in(TCrmBranchArea::getBranchId, ids)); | 
|         // 删除职位薪资 | 
|         crmBranchSalaryService.remove(Wrappers.lambdaQuery(TCrmBranchSalary.class).in(TCrmBranchSalary::getBranchId, ids)); | 
|         // 删除账号 | 
|         List<TCrmBranch> crmBranchList = crmBranchService.list(Wrappers.lambdaQuery(TCrmBranch.class).in(TCrmBranch::getId, ids)); | 
|         List<Long> userIds = crmBranchList.stream().map(TCrmBranch::getUserId).collect(Collectors.toList()); | 
|         sysUserService.deleteUserByIds(userIds); | 
|         return R.ok(crmBranchService.removeByIds(ids)); | 
|     } | 
|   | 
|     /** | 
|      * 批量删除crm分公司管理 | 
|      */ | 
|     @Log(title = "crm分公司管理信息-分公司管理解冻冻结", businessType = BusinessType.UPDATE) | 
|     @ApiOperation(value = "分公司管理解冻冻结",notes = "状态 1=使用中 2=冻结") | 
|     @PutMapping(value = "/thawOrFreeze") | 
|     public R<Boolean> thawOrFreeze(@RequestParam(value = "id")String id, | 
|                                    @RequestParam(value = "status")Integer status) { | 
|         TCrmBranch crmBranch = crmBranchService.getById(id); | 
|         crmBranch.setStatus(status); | 
|         crmBranchService.updateById(crmBranch); | 
|         // 查询用户 | 
|         SysUser sysUser = sysUserService.selectUserById(crmBranch.getUserId()); | 
|         switch (status){ | 
|             case 1: | 
|                 // 解冻 | 
|                 sysUser.setStatus("0"); | 
|                 sysUserService.updateUser(sysUser); | 
|                 break; | 
|             case 2: | 
|                 // 冻结 | 
|                 sysUser.setStatus("1"); | 
|                 sysUserService.updateUser(sysUser); | 
|                 // 查询业务员 | 
|                 List<TCrmSalesperson> crmSalespersons = crmSalespersonService.list(Wrappers.lambdaQuery(TCrmSalesperson.class) | 
|                         .eq(TCrmSalesperson::getBranchId, id)); | 
|                 if(!CollectionUtils.isEmpty(crmSalespersons)){ | 
|                     List<Long> userIds = crmSalespersons.stream().map(TCrmSalesperson::getUserId).collect(Collectors.toList()); | 
|                     sysUserService.updateUserStatusByUserIds(userIds, "1"); | 
|                 } | 
|                 break; | 
|         } | 
|         return R.ok(); | 
|     } | 
|   | 
|     /** | 
|      * 批量删除crm分公司管理 | 
|      */ | 
|     @Log(title = "crm分公司管理信息-分公司管理修改积分", businessType = BusinessType.DELETE) | 
|     @ApiOperation(value = "分公司管理修改积分") | 
|     @PutMapping(value = "/changePoints") | 
|     public R<Boolean> changePoints(@RequestBody TCrmChangePoints changePoints) { | 
|         // 查询分公司信息 | 
|         TCrmBranch crmBranch = crmBranchService.getById(changePoints.getBranchSalespersonId()); | 
|         if (Objects.isNull(crmBranch)) { | 
|             return R.fail("未查询到该分公司"); | 
|         } | 
|         switch (changePoints.getChangeType()){ | 
|             case 1: | 
|                 // 增加积分 | 
|                 crmBranch.setUserPoints(crmBranch.getUserPoints() + changePoints.getChangeValue()); | 
|                 break; | 
|             case 2: | 
|                 // 减少积分 | 
|                 if (crmBranch.getUserPoints() < changePoints.getChangeValue()) { | 
|                     return R.fail("操作失败,剩余积分数不足"); | 
|                 } | 
|                 crmBranch.setUserPoints(crmBranch.getUserPoints() - changePoints.getChangeValue()); | 
|                 break; | 
|         } | 
|         crmBranchService.updateById(crmBranch); | 
|         changePoints.setUserType(1); | 
|         crmChangePointsService.save(changePoints); | 
|         return R.ok(); | 
|     } | 
|   | 
|     @Log(title = "crm分公司管理信息-分公司管理信息导出", businessType = BusinessType.EXPORT) | 
|     @ApiOperation(value = "分公司管理信息导出") | 
|     @PostMapping("/exportList") | 
|     public void exportList(@RequestBody TCrmBranchQuery query){ | 
|         List<TCrmBranchExport> crmBranchExports = crmBranchService.exportList(query); | 
|         Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TCrmBranchExport.class, crmBranchExports); | 
|         HttpServletResponse response = WebUtils.response(); | 
|         assert response != null; | 
|         response.setContentType("application/vnd.ms-excel"); | 
|         response.setCharacterEncoding("utf-8"); | 
|         ServletOutputStream outputStream = null; | 
|         try { | 
|             String fileName = URLEncoder.encode("分公司管理信息.xls", "utf-8"); | 
|             response.setHeader("Content-Disposition", "attachment;filename=" + fileName); | 
|             response.setContentType("application/vnd.ms-excel;charset=UTF-8"); | 
|             response.setHeader("Pragma", "no-cache"); | 
|             response.setHeader("Cache-Control", "no-cache"); | 
|             outputStream = response.getOutputStream(); | 
|             workbook.write(outputStream); | 
|         } catch (IOException e) { | 
|             e.printStackTrace(); | 
|         } finally { | 
|             try { | 
|                 outputStream.close(); | 
|             } catch (IOException e) { | 
|                 e.printStackTrace(); | 
|             } | 
|         } | 
|     } | 
|   | 
| } |