package com.ruoyi.web.controller.api; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; 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.DateUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.WebUtils; import com.ruoyi.framework.web.service.TokenService; import com.ruoyi.system.export.TCrmClinicAuditExport; import com.ruoyi.system.export.TSysAppUserClinicExport; import com.ruoyi.system.export.TSysAppUserExport; import com.ruoyi.system.export.TSysAppUserListExport; import com.ruoyi.system.model.*; import com.ruoyi.system.query.TCrmClinicQuery; import com.ruoyi.system.query.TSysAppUserQuery; import com.ruoyi.system.service.*; import com.ruoyi.system.vo.TSysAppUserVO; import com.ruoyi.system.vo.TSysOrderVO; import com.ruoyi.web.controller.tool.MsgUtils; 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.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import java.util.*; import java.util.stream.Collectors; /** *

* 用户管理 前端控制器 *

* * @author xiaochen * @since 2025-08-20 */ @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 MsgUtils msgUtils; private final TSysMessageTemplateService sysMessageTemplateService; private final TCrmClinicService crmClinicService; private final TokenService tokenService; private final TSysOtherConfigService sysOtherConfigService; private final ISysUserService sysUserService; @Autowired public TSysAppUserController(TSysAppUserService sysAppUserService, TSysInspectionService sysInspectionService, TSysOrderService sysOrderService, TSysChronicDiseaseService sysChronicDiseaseService, MsgUtils msgUtils, TSysMessageTemplateService sysMessageTemplateService, TCrmClinicService crmClinicService, TokenService tokenService, TSysOtherConfigService sysOtherConfigService, ISysUserService sysUserService) { this.sysAppUserService = sysAppUserService; this.sysInspectionService = sysInspectionService; this.sysOrderService = sysOrderService; this.sysChronicDiseaseService = sysChronicDiseaseService; this.msgUtils = msgUtils; this.sysMessageTemplateService = sysMessageTemplateService; this.crmClinicService = crmClinicService; this.tokenService = tokenService; this.sysOtherConfigService = sysOtherConfigService; this.sysUserService = sysUserService; } /** * 获取用户管理管理列表 */ @ApiOperation(value = "获取用户管理分页列表") @PostMapping(value = "/pageList") public R> pageList(@RequestBody TSysAppUserQuery query) { Integer roleType = tokenService.getLoginUser().getUser().getRoleType(); Long userId = tokenService.getLoginUser().getUserId(); query.setRoleType(roleType); if(roleType == 5){ TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class) .eq(TCrmClinic::getUserId, userId) .last("LIMIT 1")); query.setClinicId(crmClinic.getId()); } return R.ok(sysAppUserService.pageList(query)); } /** * 获取用户管理管理列表 */ @ApiOperation(value = "获取慢性病患者分页列表") @PostMapping(value = "/pageChronicDiseaseUserList") public R> pageChronicDiseaseUserList(@RequestBody TSysAppUserQuery query) { Integer roleType = tokenService.getLoginUser().getUser().getRoleType(); Long userId = tokenService.getLoginUser().getUserId(); query.setRoleType(roleType); if(roleType == 5){ TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class) .eq(TCrmClinic::getUserId, userId) .orderByDesc(TCrmClinic::getCreateTime) .last("LIMIT 1")); query.setClinicId(crmClinic.getId()); } return R.ok(sysAppUserService.pageChronicDiseaseUserList(query)); } /** * 用药提醒 */ @ApiOperation(value = "慢性病患者-用药提醒", notes = "userId:患者用户id,templateId:短信模板id") @GetMapping(value = "/medicationReminder") public R medicationReminder(@RequestParam(value = "userId") String userId, @RequestParam(value = "templateId") String templateId) { Long clinicUserId = tokenService.getLoginUser().getUserId(); TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class) .eq(TCrmClinic::getUserId, clinicUserId) .last("LIMIT 1")); // --- if(Objects.isNull(crmClinic)){ SysUser sysUser = sysUserService.selectUserById(clinicUserId); crmClinic = crmClinicService.getById(sysUser.getObjectId()); } if(crmClinic == null){ return R.fail("未找到该诊所信息"); } // 查询其他设置 TSysOtherConfig sysOtherConfig = sysOtherConfigService.getById(1); if(crmClinic.getSendMessageCount()>=sysOtherConfig.getMsgCount()){ return R.fail("该诊所今日发送短信次数已达上限"); } crmClinic.setSendMessageCount(crmClinic.getSendMessageCount() + 1); crmClinicService.updateById(crmClinic); TSysAppUser sysAppUser = sysAppUserService.getById(userId); if(StringUtils.isNotBlank(sysAppUser.getPhone())){ sysAppUser.setLastReminderTime(LocalDateTime.now()); sysAppUserService.updateById(sysAppUser); TSysMessageTemplate messageTemplate = sysMessageTemplateService.getById(templateId); // 发送短信 try { Map params = new HashMap<>(); msgUtils.sendMsg(sysAppUser.getPhone(),params, messageTemplate.getTemplateCode()); } catch (Exception e) { throw new RuntimeException(e); } } return R.ok(); } /** * 获取用户管理管理列表 */ @ApiOperation(value = "获取用户管理列表") @PostMapping(value = "/list") public R> list() { return R.ok(sysAppUserService.list(Wrappers.lambdaQuery(TSysAppUser.class) .eq(TSysAppUser::getStatus,1) .orderByDesc(TSysAppUser::getCreateTime))); } /** * 添加用户管理管理 */ @Log(title = "用户管理信息-新增用户管理", businessType = BusinessType.INSERT) @ApiOperation(value = "添加用户管理") @PostMapping(value = "/add") public R add(@Validated @RequestBody TSysAppUser dto) { if (sysAppUserService.isExit(dto)) { return R.fail("用户管理信息已存在"); } Long userId = tokenService.getLoginUser().getUserId(); TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class) .eq(TCrmClinic::getUserId, userId)); // --- if(Objects.isNull(crmClinic)){ SysUser sysUser = sysUserService.selectUserById(userId); crmClinic = crmClinicService.getById(sysUser.getObjectId()); } String identifierNumber = UUID.randomUUID().toString().replace("-", ""); dto.setIdentifierNumber(identifierNumber); dto.setClinicId(crmClinic.getId()); sysAppUserService.save(dto); return R.ok(dto.getId()); } /** * 修改用户管理 */ @Log(title = "用户管理信息-修改用户管理", businessType = BusinessType.UPDATE) @ApiOperation(value = "修改用户管理") @PostMapping(value = "/update") public R update(@Validated @RequestBody TSysAppUser dto) { if (sysAppUserService.isExit(dto)) { return R.fail("用户管理信息已存在"); } 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) .orderByDesc(TSysInspection::getCreateTime)); if(!CollectionUtils.isEmpty(sysInspections)){ List clinicIds = sysInspections.stream().map(TSysInspection::getClinicId).collect(Collectors.toList()); List clinicList = crmClinicService.list(Wrappers.lambdaQuery(TCrmClinic.class).in(TCrmClinic::getId, clinicIds)); List sysOrderList = sysOrderService.list(Wrappers.lambdaQuery(TSysOrder.class).in(TSysOrder::getClinicId, clinicIds)); for (TSysInspection sysInspection : sysInspections) { TCrmClinic clinic = clinicList.stream().filter(c -> c.getId().equals(sysInspection.getClinicId())).findFirst().orElse(null); if(clinic != null){ sysInspection.setClinicName(clinic.getClinicName()); } List sysOrders = sysOrderList.stream().filter(c -> sysInspection.getId().equals(c.getInspectionId())).collect(Collectors.toList()); if(CollectionUtils.isEmpty(sysOrders)){ sysInspection.setIsPrescribeMedicine(0); }else { sysInspection.setIsPrescribeMedicine(1); sysInspection.setOrderId(sysOrders.get(0).getId()); } } } sysAppUserVO.setSysInspections(sysInspections); // 订单信息 List sysOrders = sysOrderService.queryListByAppUserId(id); for (TSysOrderVO sysOrder : sysOrders) { List tSysInspections = sysInspections.stream().filter(c -> c.getId().equals(sysOrder.getInspectionId())).collect(Collectors.toList()); if(!CollectionUtils.isEmpty(tSysInspections)){ TSysInspection sysInspection = tSysInspections.get(0); sysOrder.setSysInspection(sysInspection); } } sysAppUserVO.setSysOrders(sysOrders); if(sysAppUserVO.getBirthTime() != null){ long age = ChronoUnit.YEARS.between(sysAppUserVO.getBirthTime(), LocalDateTime.now()); sysAppUserVO.setAge(age); } 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); } /** * 删除用户管理 */ @Log(title = "用户管理信息-删除用户管理", businessType = BusinessType.DELETE) @ApiOperation(value = "删除用户管理") @DeleteMapping(value = "/deleteById") public R deleteById(@RequestParam String id) { return R.ok(sysAppUserService.removeById(id)); } /** * 批量删除用户管理 */ @Log(title = "用户管理信息-删除用户管理", businessType = BusinessType.DELETE) @ApiOperation(value = "批量删除用户管理") @DeleteMapping(value = "/deleteByIds") public R deleteByIds(@RequestBody List ids) { return R.ok(sysAppUserService.removeByIds(ids)); } /** * 分公司管理解冻冻结 */ @Log(title = "用户管理信息-用户管理解冻冻结", businessType = BusinessType.UPDATE) @ApiOperation(value = "用户管理解冻冻结",notes = "状态 1=使用中 2=冻结") @PutMapping(value = "/thawOrFreeze") public R thawOrFreeze(@RequestParam(value = "id")String id, @RequestParam(value = "status")Integer status) { TSysAppUser sysAppUser = sysAppUserService.getById(id); sysAppUser.setStatus(status); sysAppUserService.updateById(sysAppUser); if(status == 2){ // TODO 退出账号 冻结 } return R.ok(); } /** * 修改用户慢性病 */ @Log(title = "用户管理信息-修改用户慢性病", businessType = BusinessType.UPDATE) @ApiOperation(value = "修改用户慢性病") @PostMapping(value = "/updateDisease") public R updateDisease(@RequestParam(value = "id") String id, @RequestParam(value = "chronicDiseaseId") String chronicDiseaseId) { TSysAppUser sysAppUser = new TSysAppUser(); sysAppUser.setId(id); sysAppUser.setChronicDiseaseId(chronicDiseaseId); return R.ok(sysAppUserService.updateById(sysAppUser)); } @Log(title = "用户管理信息-慢性病患者导出-平台", businessType = BusinessType.EXPORT) @ApiOperation(value = "慢性病患者导出-平台") @PostMapping("/exportList") public void exportList(@RequestBody TSysAppUserQuery query){ Integer roleType = tokenService.getLoginUser().getUser().getRoleType(); query.setRoleType(roleType); List sysAppUserListExports = sysAppUserService.exportList(query); Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TSysAppUserListExport.class, sysAppUserListExports); 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(); } } } @Log(title = "用户管理信息-慢性病患者导出-诊所", businessType = BusinessType.EXPORT) @ApiOperation(value = "慢性病患者导出-诊所") @PostMapping("/exportListClinic") public void exportListClinic(@RequestBody TSysAppUserQuery query){ Integer roleType = tokenService.getLoginUser().getUser().getRoleType(); query.setRoleType(roleType); List sysAppUserClinicExports = sysAppUserService.exportListClinic(query); // for (TSysAppUserClinicExport sysAppUserClinicExport : sysAppUserClinicExports) { // String lastReminderTime = sysAppUserClinicExport.getLastReminderTime(); // lastReminderTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(lastReminderTime); // sysAppUserClinicExport.setLastReminderTime(lastReminderTime); // } Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TSysAppUserClinicExport.class, sysAppUserClinicExports); 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(); } } } @Log(title = "用户管理信息-用户管理信息导出", businessType = BusinessType.EXPORT) @ApiOperation(value = "用户管理信息导出") @PostMapping("/exportListAppUser") public void exportListAppUser(@RequestBody TSysAppUserQuery query){ List sysAppUserExports = sysAppUserService.exportListAppUser(query); for (TSysAppUserExport sysAppUserExport : sysAppUserExports) { // String lastInspectionTime = sysAppUserExport.getLastInspectionTime(); // if(StringUtils.isNotEmpty(lastInspectionTime)){ // lastInspectionTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(lastInspectionTime); // sysAppUserExport.setLastInspectionTime(lastInspectionTime); // } sysAppUserExport.setCreateTimeStr(sysAppUserExport.getCreateTime().toLocalDate().toString()); } Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TSysAppUserExport.class, sysAppUserExports); 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(); } } } }