package com.example.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.common.Result;
import com.example.common.ResultCode;
import com.example.entity.JiankangziceInfo;
import com.example.dao.JiankangziceInfoDao;
import com.example.service.JiankangziceInfoService;
import com.example.exception.CustomException;
import com.example.common.ResultCode;
import com.example.vo.EchartsData;
import com.example.vo.JiankangziceInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.example.service.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import cn.hutool.core.util.StrUtil;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
@RestController
@RequestMapping(value = "/jiankangziceInfo")
public class JiankangziceInfoController {
@Resource
private JiankangziceInfoService jiankangziceInfoService;
@Resource
private JiankangziceInfoDao jiankangziceInfoDao;
@PostMapping
public Result<JiankangziceInfo> add(@RequestBody JiankangziceInfoVo jiankangziceInfo) {
//mixmajixami
jiankangziceInfoService.add(jiankangziceInfo);
return Result.success(jiankangziceInfo);
}
//youtixing1
//youtixing2
@GetMapping("/getByDiqu")
public Result<List<Map<String,String>>> qidu() {
return Result.success(jiankangziceInfoService.findByDiqu());
}
@DeleteMapping("/{id}")
public Result delete(@PathVariable Long id) {
jiankangziceInfoService.delete(id);
return Result.success();
}
@GetMapping("/getByzhuceyonghuZhanghao/{zhanghao}")
public Result<List<JiankangziceInfo>> getByzhuceyonghuZhanghao(@PathVariable String zhanghao) {
List<JiankangziceInfo> jiankangziceInfo = jiankangziceInfoService.getByzhuceyonghuZhanghao(zhanghao);
return Result.success(jiankangziceInfo);
}
@PutMapping
public Result update(@RequestBody JiankangziceInfoVo jiankangziceInfo) {
jiankangziceInfoService.update(jiankangziceInfo);
return Result.success();
}
//@PutMapping("/update2")
// public Result update2(@RequestBody JiankangziceInfoVo jiankangziceInfo) {
// jiankangziceInfoService.update2(jiankangziceInfo);
// return Result.success();
// }
@GetMapping("/{id}")
public Result<JiankangziceInfo> detail(@PathVariable Long id) {
JiankangziceInfo jiankangziceInfo = jiankangziceInfoService.findById(id);
return Result.success(jiankangziceInfo);
}
@GetMapping("/changeStatus/{id}")
public Result<JiankangziceInfo> changeStatus(@PathVariable Long id) {
jiankangziceInfoService.changeStatus(id);
return Result.success();
}
@GetMapping
public Result<List<JiankangziceInfoVo>> all() {
return Result.success(jiankangziceInfoService.findAll());
}
@GetMapping("/page/{name}")
public Result<PageInfo<JiankangziceInfoVo>> page(@PathVariable String name,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "5") Integer pageSize,
HttpServletRequest request) {
return Result.success(jiankangziceInfoService.findPage(name, pageNum, pageSize, request));
}
@GetMapping("/pageqt/{name}")
public Result<PageInfo<JiankangziceInfoVo>> pageqt(@PathVariable String name,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "8") Integer pageSize,
HttpServletRequest request) {
return Result.success(jiankangziceInfoService.findPageqt(name, pageNum, pageSize, request));
}
// @PostMapping("/register")
// public Result<JiankangziceInfo> register(@RequestBody JiankangziceInfo jiankangziceInfo) {
// if (StrUtil.isBlank(jiankangziceInfo.getName()) || StrUtil.isBlank(jiankangziceInfo.getPassword())) {
// throw new CustomException(ResultCode.PARAM_ERROR);
// }
// return Result.success(jiankangziceInfoService.add(jiankangziceInfo));
// }
/**
* 批量通过excel添加信息
* @param file excel文件
* @throws IOException
*/
@PostMapping("/upload")
public Result upload(MultipartFile file) throws IOException {
List<JiankangziceInfo> infoList = ExcelUtil.getReader(file.getInputStream()).readAll(JiankangziceInfo.class);
if (!CollectionUtil.isEmpty(infoList)) {
// 处理一下空数据
List<JiankangziceInfo> resultList = infoList.stream().filter(x -> ObjectUtil.isNotEmpty(x.getRiqi())).collect(Collectors.toList());
for (JiankangziceInfo info : resultList) {
jiankangziceInfoService.add(info);
}
}
return Result.success();
}
//yoxutonxgjitu
@GetMapping("/getExcelModel")
public void getExcelModel(HttpServletResponse response) throws IOException {
// 1. 生成excel
Map<String, Object> row = new LinkedHashMap<>();
row.put("riqi", "A日期");
row.put("xueya", "A血压");
row.put("xuetang", "A血糖");
row.put("shuimianshijian", "A睡眠时间");
row.put("tizhong", "A体重");
row.put("shengao", "A身高");
row.put("zhanghao", "A账号");
row.put("xingming", "A姓名");
row.put("huifu", "A回复");
row.put("status", "是");
row.put("level", "jiankangzice");
List<Map<String, Object>> list = CollUtil.newArrayList(row);
// 2. 写excel
ExcelWriter writer = ExcelUtil.getWriter(true);
writer.write(list, true);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition","attachment;filename=jiankangziceInfoModel.xlsx");
ServletOutputStream out = response.getOutputStream();
writer.flush(out, true);
writer.close();
IoUtil.close(System.out);
}
@GetMapping("/getExcel")
public void getExcel(HttpServletResponse response) throws IOException {
// 1. 生成excel
Map<String, Object> row = new LinkedHashMap<>();
row.put("riqi", "A日期");
row.put("xueya", "A血压");
row.put("xuetang", "A血糖");
row.put("shuimianshijian", "A睡眠时间");
row.put("tizhong", "A体重");
row.put("shengao", "A身高");
row.put("zhanghao", "A账号");
row.put("xingming", "A姓名");
row.put("huifu", "A回复");
row.put("status", "是");
row.put("level", "权限");
List<Map<String, Object>> list = CollUtil.newArrayList(row);
List<Map<String, Object>> daochuexcellist = jiankangziceInfoDao.daochuexcel();
Map<String, Double> typeMap = new HashMap<>();
for (Map<String, Object> map : daochuexcellist) {
list.add(map);
}
// 2. 写excel
ExcelWriter writer = ExcelUtil.getWriter(true);
writer.write(list, true);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-