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.HuiyuanxinxiInfo;
import com.example.dao.HuiyuanxinxiInfoDao;
import com.example.service.HuiyuanxinxiInfoService;
import com.example.exception.CustomException;
import com.example.common.ResultCode;
import com.example.vo.EchartsData;
import com.example.vo.HuiyuanxinxiInfoVo;
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 = "/huiyuanxinxiInfo")
public class HuiyuanxinxiInfoController {
@Resource
private HuiyuanxinxiInfoService huiyuanxinxiInfoService;
@Resource
private HuiyuanxinxiInfoDao huiyuanxinxiInfoDao;
@PostMapping
public Result<HuiyuanxinxiInfo> add(@RequestBody HuiyuanxinxiInfoVo huiyuanxinxiInfo) {
huiyuanxinxiInfo.setMima(SecureUtil.md5(huiyuanxinxiInfo.getMima()));
huiyuanxinxiInfoService.add(huiyuanxinxiInfo);
return Result.success(huiyuanxinxiInfo);
}
//youtixing1
//youtixing2
@DeleteMapping("/{id}")
public Result delete(@PathVariable Long id) {
huiyuanxinxiInfoService.delete(id);
return Result.success();
}
@PutMapping
public Result update(@RequestBody HuiyuanxinxiInfoVo huiyuanxinxiInfo) {
huiyuanxinxiInfoService.update(huiyuanxinxiInfo);
return Result.success();
}
//@PutMapping("/update2")
// public Result update2(@RequestBody HuiyuanxinxiInfoVo huiyuanxinxiInfo) {
// huiyuanxinxiInfoService.update2(huiyuanxinxiInfo);
// return Result.success();
// }
@GetMapping("/{id}")
public Result<HuiyuanxinxiInfo> detail(@PathVariable Long id) {
HuiyuanxinxiInfo huiyuanxinxiInfo = huiyuanxinxiInfoService.findById(id);
return Result.success(huiyuanxinxiInfo);
}
@GetMapping("/changeStatus/{id}")
public Result<HuiyuanxinxiInfo> changeStatus(@PathVariable Long id) {
huiyuanxinxiInfoService.changeStatus(id);
return Result.success();
}
@GetMapping
public Result<List<HuiyuanxinxiInfoVo>> all() {
return Result.success(huiyuanxinxiInfoService.findAll());
}
@GetMapping("/page/{name}")
public Result<PageInfo<HuiyuanxinxiInfoVo>> page(@PathVariable String name,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "5") Integer pageSize,
HttpServletRequest request) {
return Result.success(huiyuanxinxiInfoService.findPage(name, pageNum, pageSize, request));
}
@GetMapping("/pageqt/{name}")
public Result<PageInfo<HuiyuanxinxiInfoVo>> pageqt(@PathVariable String name,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "8") Integer pageSize,
HttpServletRequest request) {
return Result.success(huiyuanxinxiInfoService.findPageqt(name, pageNum, pageSize, request));
}
// @PostMapping("/register")
// public Result<HuiyuanxinxiInfo> register(@RequestBody HuiyuanxinxiInfo huiyuanxinxiInfo) {
// if (StrUtil.isBlank(huiyuanxinxiInfo.getName()) || StrUtil.isBlank(huiyuanxinxiInfo.getPassword())) {
// throw new CustomException(ResultCode.PARAM_ERROR);
// }
// return Result.success(huiyuanxinxiInfoService.add(huiyuanxinxiInfo));
// }
/**
* 批量通过excel添加信息
* @param file excel文件
* @throws IOException
*/
@PostMapping("/upload")
public Result upload(MultipartFile file) throws IOException {
List<HuiyuanxinxiInfo> infoList = ExcelUtil.getReader(file.getInputStream()).readAll(HuiyuanxinxiInfo.class);
if (!CollectionUtil.isEmpty(infoList)) {
// 处理一下空数据
List<HuiyuanxinxiInfo> resultList = infoList.stream().filter(x -> ObjectUtil.isNotEmpty(x.getXingming())).collect(Collectors.toList());
for (HuiyuanxinxiInfo info : resultList) {
huiyuanxinxiInfoService.add(info);
}
}
return Result.success();
}
@GetMapping("/get/huiyuanxinxi_tj_xingbie")
Result<List<EchartsData>> huiyuanxinxi_tj_xingbie() {
List<EchartsData> list = new ArrayList<>();
List<Map<String, Object>> huiyuanxinxi_tj_xingbieList = huiyuanxinxiInfoDao.huiyuanxinxi_tj_xingbie();
Map<String, Double> typeMap = new HashMap<>();
for (Map<String, Object> map : huiyuanxinxi_tj_xingbieList) {
typeMap.put((String)map.get("aa"), (Double.valueOf((String)map.get("bb").toString())));
}
getPieData("会员信息按性别统计", list, typeMap);
getBarData("会员信息按性别统计", list, typeMap);
return Result.success(list);
}
@GetMapping("/get/huiyuanxinxi_tj_leixing")
Result<List<EchartsData>> huiyuanxinxi_tj_leixing() {
List<EchartsData> list = new ArrayList<>();
List<Map<String, Object>> huiyuanxinxi_tj_leixingList = huiyuanxinxiInfoDao.huiyuanxinxi_tj_leixing();
Map<String, Double> typeMap = new HashMap<>();
for (Map<String, Object> map : huiyuanxinxi_tj_leixingList) {
typeMap.put((String)map.get("aa"), (Double.valueOf((String)map.get("bb").toString())));
}
getPieData("会员信息按类型统计", list, typeMap);
getBarData("会员信息按类型统计", list, typeMap);
return Result.success(list);
}
@GetMapping("/getExcelModel")
public void getExcelModel(HttpServletResponse response) throws IOException {
// 1. 生成excel
Map<String, Object> row = new LinkedHashMap<>();
row.put("yonghuming", "A用户名");
row.put("mima", "A密码");
row.put("xingming", "A姓名");
row.put("xingbie", "A性别");
row.put("dianhua", "A电话");
row.put("shenfenzheng", "A身份证");
row.put("zhaopian", "A照片");
row.put("leixing", "A类型");
row.put("zhekou", "A折扣");
row.put("beizhu", "A备注");
row.put("status", "是");
row.put("level", "huiyuanxinxi");
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=huiyuanxinxiInfoModel.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> ro