package com.ruoyi.web.controller.api;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.TSysAppUserQuery;
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.TSysAppUserVO;
import com.ruoyi.system.vo.TSysOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.util.*;
/**
*
* 用户管理 前端控制器
*
*
* @author xiaochen
* @since 2025-08-20
*/
@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/t-sys-app-user")
public class TSysAppUserController {
private final TSysAppUserService sysAppUserService;
private final TSysInspectionService sysInspectionService;
private final TSysOrderService sysOrderService;
private final TSysChronicDiseaseService sysChronicDiseaseService;
private final TokenService tokenService;
private final RedisCache redisCache;
private final XimaAIUtil ximaAIUtil;
private final RestTemplate restTemplate;
private final FileController fileController;
private final WeixinProperties wxConfig;
private final TCrmClinicService crmClinicService;
private final ISysUserService sysUserService;
private final TSysOtherConfigService sysOtherConfigService;
@Autowired
public TSysAppUserController(TSysAppUserService sysAppUserService, TSysInspectionService sysInspectionService, TSysOrderService sysOrderService, TSysChronicDiseaseService sysChronicDiseaseService, TokenService tokenService, RedisCache redisCache, XimaAIUtil ximaAIUtil, RestTemplate restTemplate, FileController fileController, WeixinProperties wxConfig, TCrmClinicService crmClinicService, ISysUserService sysUserService, TSysOtherConfigService sysOtherConfigService) {
this.sysAppUserService = sysAppUserService;
this.sysInspectionService = sysInspectionService;
this.sysOrderService = sysOrderService;
this.sysChronicDiseaseService = sysChronicDiseaseService;
this.tokenService = tokenService;
this.redisCache = redisCache;
this.ximaAIUtil = ximaAIUtil;
this.restTemplate = restTemplate;
this.fileController = fileController;
this.wxConfig = wxConfig;
this.crmClinicService = crmClinicService;
this.sysUserService = sysUserService;
this.sysOtherConfigService = sysOtherConfigService;
}
/**
* 获取用户管理管理列表
*/
@ApiOperation(value = "获取用户管理分页列表")
@PostMapping(value = "/pageList")
public R> pageList(@RequestBody TSysAppUserQuery query) {
return R.ok(sysAppUserService.pageList(query));
}
/**
* 修改用户管理
*/
@ApiOperation(value = "修改用户管理")
@PostMapping(value = "/update")
public R update(@Validated @RequestBody TSysAppUser dto) {
return R.ok(sysAppUserService.updateById(dto));
}
/**
* 查看用户管理详情
*/
@ApiOperation(value = "查看用户管理详情")
@GetMapping(value = "/getDetailById")
public R getDetailById(@RequestParam String id) {
TSysAppUser sysAppUser = sysAppUserService.getById(id);
TSysAppUserVO sysAppUserVO = new TSysAppUserVO();
BeanUtils.copyProperties(sysAppUser, sysAppUserVO);
// 查询慢性病
String chronicDiseaseId = sysAppUserVO.getChronicDiseaseId();
if(StringUtils.isNotBlank(chronicDiseaseId)){
String[] split = chronicDiseaseId.split(",");
List sysChronicDiseases = sysChronicDiseaseService.list(Wrappers.lambdaQuery(TSysChronicDisease.class).in(TSysChronicDisease::getId, Arrays.asList(split)));
sysAppUserVO.setSysChronicDiseases(sysChronicDiseases);
}
// 查询检测信息
List sysInspections = sysInspectionService.list(Wrappers.lambdaQuery(TSysInspection.class).eq(TSysInspection::getAppUserId, id));
sysAppUserVO.setSysInspections(sysInspections);
// 订单信息
List sysOrders = sysOrderService.queryListByAppUserId(id);
sysAppUserVO.setSysOrders(sysOrders);
// 查询诊所账号
SysUser sysUser = sysUserService.selectByPhone(sysAppUserVO.getPhone());
if(Objects.nonNull(sysUser)){
sysAppUserVO.setIsClinicUser(1);
}else {
sysAppUserVO.setIsClinicUser(0);
}
// 查询免费次数
TSysOtherConfig sysOtherConfig = sysOtherConfigService.getById(1);
sysAppUserVO.setAiFreeCount(sysOtherConfig.getAiFreeCount());
return R.ok(sysAppUserVO);
}
/**
* 根据用户id查询检测信息
*/
@ApiOperation(value = "根据用户id查询检测信息")
@GetMapping(value = "/getInspectionByAppUserId")
public R> getInspectionByAppUserId(@RequestParam String appUserId) {
// 订单信息
List sysInspections = sysInspectionService.list(Wrappers.lambdaQuery(TSysInspection.class).eq(TSysInspection::getAppUserId, appUserId));
return R.ok(sysInspections);
}
/**
* 查看用户管理详情
*/
@ApiOperation(value = "查看用户购药详情")
@GetMapping(value = "/getOrderByAppUserId")
public R> getOrderByAppUserId(@RequestParam String appUserId) {
List sysOrders = sysOrderService.queryListByAppUserId(appUserId);
return R.ok(sysOrders);
}
/**
* 退出登录
*/
@ApiOperation(value = "退出登录")
@PostMapping(value = "/loginOut")
public R loginOut() {
String token = tokenService.getLoginUserApplet().getToken();
redisCache.deleteObject(tokenService.getTokenKey(token));
return R.ok();
}
/**
* 获取AI检测页面
*/
@ApiOperation(value = "获取AI检测页面[1229]",notes = "设备类型 deviceType: android ios")
@GetMapping(value = "/getXiMaAiDetection")
public R getXiMaAiDetection(@RequestParam String deviceType) {
String userId = tokenService.getLoginUserApplet().getUserId();
TSysAppUser sysAppUser = sysAppUserService.getById(userId);
if(StringUtils.isBlank(sysAppUser.getNickName())
|| StringUtils.isBlank(sysAppUser.getPhone())
|| Objects.isNull(sysAppUser.getSex())
|| Objects.isNull(sysAppUser.getBirthTime())
|| Objects.isNull(sysAppUser.getUserHeight())
|| Objects.isNull(sysAppUser.getUserWeight())){
return R.fail("请完善健康检测及获取报告必需信息");
}
if(StringUtils.isBlank(sysAppUser.getIdentifierNumber())){
String identifierNumber = UUID.randomUUID().toString().replace("-", "");
sysAppUser.setIdentifierNumber(identifierNumber);
sysAppUserService.updateById(sysAppUser);
}
JSONObject authorizationH5 = ximaAIUtil.getAuthorizationH5(sysAppUser.getIdentifierNumber(), sysAppUser.getNickName(), sysAppUser.getPhone(), sysAppUser.getSex() == 1 ? "0" : "1",sysAppUser.getBirthTime().toString(), sysAppUser.getUserHeight(), sysAppUser.getUserWeight(), deviceType);
return R.ok(authorizationH5);
}
@ApiOperation(value = "查询诊所邀请码[1229]")
@GetMapping("/getClinicQrCode")
public R getClinicQrCode() {
String userId = tokenService.getLoginUserApplet().getUserId();
TSysAppUser sysAppUser = sysAppUserService.getById(userId);
SysUser sysUser = sysUserService.selectByPhone(sysAppUser.getPhone());
if(Objects.isNull(sysUser)){
return R.fail("该用户非诊所用户");
}
TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
.eq(TCrmClinic::getUserId, sysUser.getUserId())
.last("LIMIT 1"));
if(Objects.isNull(crmClinic)){
return R.fail("未找到该诊所信息");
}
if(StringUtils.isBlank(crmClinic.getQrCode())){
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 map = new HashMap<>();
map.put("page", "pages/home/home");
map.put("scene", "clinicId=" + crmClinic.getId());
map.put("check_path", false);
map.put("env_version", "trial");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity requestEntity = new HttpEntity(map, headers);
ResponseEntity 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.obsUploadQrCode(file,"qrcode_"+crmClinic.getId()+".jpg");
crmClinic.setQrCode(stringApiResult);
} catch (Exception e) {
log.error("生成小程序码失败:{}", e.getMessage());
}
crmClinicService.updateById(crmClinic);
}
return R.ok(crmClinic.getQrCode());
}
}