liujie
2025-10-13 5d4ebe6d0b7dda230b19f4913bdeb60556b46678
ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TCrmBranchController.java
@@ -1,24 +1,40 @@
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;
@@ -42,14 +58,18 @@
    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) {
    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;
    }
    /**
@@ -67,9 +87,23 @@
    @ApiOperation(value = "获取crm分公司管理列表")
    @PostMapping(value = "/list")
    public R<List<TCrmBranch>> list() {
        return R.ok(crmBranchService.list(Wrappers.lambdaQuery(TCrmBranch.class)
                .eq(TCrmBranch::getStatus,1)
                .orderByDesc(TCrmBranch::getCreateTime)));
        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));
    }
    /**
@@ -172,6 +206,13 @@
                // 冻结
                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();
@@ -207,6 +248,36 @@
        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();
            }
        }
    }
}