xuhy
2026-01-06 3d8e6a16f98bc01d3cee3a2733fa9a4eff8db2c7
ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TCrmClinicController.java
@@ -7,14 +7,18 @@
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.constant.WxConstant;
import com.ruoyi.common.core.controller.FileController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.TCrmClinicAuditDTO;
import com.ruoyi.system.dto.TCrmClinicDTO;
import com.ruoyi.system.dto.UnbindDTO;
import com.ruoyi.system.export.TCrmChangePointsBranchExport;
import com.ruoyi.system.export.TCrmClinicAuditExport;
import com.ruoyi.system.export.TCrmClinicListExport;
@@ -22,27 +26,35 @@
import com.ruoyi.system.query.TCrmChangePointsQuery;
import com.ruoyi.system.query.TCrmClinicQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.utils.util.ObsUploadUtil;
import com.ruoyi.system.utils.wx.model.WeixinProperties;
import com.ruoyi.system.utils.wx.tools.WxAppletTools;
import com.ruoyi.system.utils.xiMaUtil.XimaAIUtil;
import com.ruoyi.system.vo.TCrmClinicVO;
import com.ruoyi.system.vo.TSysAppUserVO;
import com.ruoyi.web.controller.tool.MsgUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -53,6 +65,7 @@
 * @author xiaochen
 * @since 2025-08-20
 */
@Slf4j
@Api(tags = "crm诊所管理")
@RestController
@RequestMapping("/t-crm-clinic")
@@ -68,8 +81,11 @@
    private final TokenService tokenService;
    private final MsgUtils msgUtils;
    private final TSysMessageTemplateService sysMessageTemplateService;
    private final RedisCache redisCache;
    private final RestTemplate restTemplate;
    private final WeixinProperties wxConfig;
    @Autowired
    public TCrmClinicController(TCrmClinicService crmClinicService, TCrmSupplierService crmSupplierService, TCrmBranchService crmBranchService, TCrmWarehouseService crmWarehouseService, TCrmSalespersonService crmSalespersonService, ISysUserService sysUserService, TCrmDeviceService crmDeviceService, TokenService tokenService, MsgUtils msgUtils, TSysMessageTemplateService sysMessageTemplateService) {
    public TCrmClinicController(TCrmClinicService crmClinicService, TCrmSupplierService crmSupplierService, TCrmBranchService crmBranchService, TCrmWarehouseService crmWarehouseService, TCrmSalespersonService crmSalespersonService, ISysUserService sysUserService, TCrmDeviceService crmDeviceService, TokenService tokenService, MsgUtils msgUtils, TSysMessageTemplateService sysMessageTemplateService, RedisCache redisCache, RestTemplate restTemplate, WeixinProperties wxConfig) {
        this.crmClinicService = crmClinicService;
        this.crmSupplierService = crmSupplierService;
        this.crmBranchService = crmBranchService;
@@ -80,6 +96,9 @@
        this.tokenService = tokenService;
        this.msgUtils = msgUtils;
        this.sysMessageTemplateService = sysMessageTemplateService;
        this.redisCache = redisCache;
        this.restTemplate = restTemplate;
        this.wxConfig = wxConfig;
    }
    /**
@@ -97,7 +116,38 @@
            TCrmBranch crmBranch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
                    .eq(TCrmBranch::getUserId,userId)
                    .last("LIMIT 1"));
            query.setBranchId(crmBranch.getId());
            List<String> branchIds = new ArrayList<>();
            if(crmBranch != null && crmBranch.getPersonType() == 1){
                List<TCrmBranch> crmBranches = crmBranchService.list(Wrappers.lambdaQuery(TCrmBranch.class)
                        .eq(TCrmBranch::getPersonType, 2)
                        .eq(TCrmBranch::getCorrelationPersonId, userId));
                if(!CollectionUtils.isEmpty(crmBranches)){
                    branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
                    List<Long> userIds = crmBranches.stream().map(TCrmBranch::getUserId).collect(Collectors.toList());
                    List<TCrmBranch> crmBranches1 = crmBranchService.list(Wrappers.lambdaQuery(TCrmBranch.class)
                            .eq(TCrmBranch::getPersonType, 3)
                            .in(TCrmBranch::getCorrelationPersonId, userIds));
                    if(!CollectionUtils.isEmpty(crmBranches1)){
                        List<String> childrenIds = crmBranches1.stream().map(TCrmBranch::getId).collect(Collectors.toList());
                        branchIds.addAll(childrenIds);
                    }
                }
            }
            if(crmBranch != null && crmBranch.getPersonType() == 2){
                List<TCrmBranch> crmBranches = crmBranchService.list(Wrappers.lambdaQuery(TCrmBranch.class)
                        .eq(TCrmBranch::getPersonType, 3)
                        .eq(TCrmBranch::getCorrelationPersonId, userId));
                if(!CollectionUtils.isEmpty(crmBranches)){
                    branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
                }
                branchIds.add(crmBranch.getId());
            }
            if(crmBranch != null && crmBranch.getPersonType() == 3){
                branchIds.add(crmBranch.getId());
            }
//            query.setBranchId(crmBranch.getId());
            branchIds = branchIds.stream().distinct().collect(Collectors.toList());
            query.setBranchIds(branchIds);
        }
        if(roleType == 3){
            query.setUserId(userId);
@@ -107,6 +157,9 @@
                    .eq(TCrmSalesperson::getUserId,userId)
                    .last("LIMIT 1"));
            query.setSalespersonId(crmSalesperson.getId());
        }
        if (roleType == 1){
            query.setRoleType(roleType);
        }
        return R.ok(crmClinicService.pageList(query));
    }
@@ -148,14 +201,37 @@
    @Log(title = "crm诊所管理信息-新增crm诊所管理", businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加crm诊所管理")
    @PostMapping(value = "/add")
    public R<Boolean> add(@Validated @RequestBody TCrmClinicDTO dto) {
    public R<String> add(@Validated @RequestBody TCrmClinicDTO dto) {
        Integer roleType = tokenService.getLoginUser().getUser().getRoleType();
        if (roleType == 1){
            dto.setStatus(3);
        }else {
            dto.setStatus(1);
        }
        return crmClinicService.addClinic(dto);
        WxAppletTools wxAppletTools = new WxAppletTools(restTemplate, wxConfig, redisCache);
        // 获取access_token
        String accessToken = wxAppletTools.getAccessToken();
        // 替换CREATE_CODE_URL
        String url = WxConstant.CREATE_CODE_URL.replace("ACCESS_TOKEN", accessToken);
        // 发送请求
        try {
            HashMap<String, String> map = new HashMap<>();
            map.put("path", "");
            map.put("scene", "clinicId=" + dto.getId());
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity requestEntity = new HttpEntity(map, headers);
            ResponseEntity<byte[]> rep = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class);
            byte[] bytes = rep.getBody();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            MultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), byteArrayInputStream);
            String stringApiResult = ObsUploadUtil.obsUpload(file);
            dto.setQrCode(stringApiResult);
        } catch (Exception e) {
            log.error("生成小程序码失败:{}", e.getMessage());
        }
        crmClinicService.addClinic(dto);
        return R.ok();
    }
    /**
@@ -274,6 +350,62 @@
    @ApiOperation(value = "诊所管理导出")
    @PostMapping("/exportList")
    public void exportList(@RequestBody TCrmClinicQuery query){
        Integer roleType = tokenService.getLoginUser().getUser().getRoleType();
        Long userId = tokenService.getLoginUser().getUserId();
        if(roleType == 2){
            query.setUserId(userId);
            query.setRoleType(roleType);
            // 查询账号绑定的分公司
            TCrmBranch crmBranch = crmBranchService.getOne(Wrappers.lambdaQuery(TCrmBranch.class)
                    .eq(TCrmBranch::getUserId,userId)
                    .last("LIMIT 1"));
            List<String> branchIds = new ArrayList<>();
            if(crmBranch != null && crmBranch.getPersonType() == 1){
                List<TCrmBranch> crmBranches = crmBranchService.list(Wrappers.lambdaQuery(TCrmBranch.class)
                        .eq(TCrmBranch::getPersonType, 2)
                        .eq(TCrmBranch::getCorrelationPersonId, userId));
                if(!CollectionUtils.isEmpty(crmBranches)){
                    branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
                    List<Long> userIds = crmBranches.stream().map(TCrmBranch::getUserId).collect(Collectors.toList());
                    List<TCrmBranch> crmBranches1 = crmBranchService.list(Wrappers.lambdaQuery(TCrmBranch.class)
                            .eq(TCrmBranch::getPersonType, 3)
                            .in(TCrmBranch::getCorrelationPersonId, userIds));
                    if(!CollectionUtils.isEmpty(crmBranches1)){
                        List<String> childrenIds = crmBranches1.stream().map(TCrmBranch::getId).collect(Collectors.toList());
                        branchIds.addAll(childrenIds);
                    }
                }
            }
            if(crmBranch != null && crmBranch.getPersonType() == 2){
                List<TCrmBranch> crmBranches = crmBranchService.list(Wrappers.lambdaQuery(TCrmBranch.class)
                        .eq(TCrmBranch::getPersonType, 3)
                        .eq(TCrmBranch::getCorrelationPersonId, userId));
                if(!CollectionUtils.isEmpty(crmBranches)){
                    branchIds = crmBranches.stream().map(TCrmBranch::getId).collect(Collectors.toList());
                }
                branchIds.add(crmBranch.getId());
            }
            if(crmBranch != null && crmBranch.getPersonType() == 3){
                branchIds.add(crmBranch.getId());
            }
//            query.setBranchId(crmBranch.getId());
            branchIds = branchIds.stream().distinct().collect(Collectors.toList());
            query.setBranchIds(branchIds);
        }
        if(roleType == 3){
            query.setUserId(userId);
            query.setRoleType(roleType);
            // 查询账号绑定的业务员
            TCrmSalesperson crmSalesperson = crmSalespersonService.getOne(Wrappers.lambdaQuery(TCrmSalesperson.class)
                    .eq(TCrmSalesperson::getUserId,userId)
                    .last("LIMIT 1"));
            query.setSalespersonId(crmSalesperson.getId());
        }
        if (roleType == 1){
            query.setRoleType(roleType);
        }
        List<TCrmClinicListExport> crmClinicListExports = crmClinicService.exportList(query);
        for (TCrmClinicListExport crmClinicListExport : crmClinicListExports) {
            if(Objects.nonNull(crmClinicListExport.getLowProcurementMoney())){