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.ShipinjiaoxueInfo;
import com.example.dao.ShipinjiaoxueInfoDao;
import com.example.service.ShipinjiaoxueInfoService;
import com.example.exception.CustomException;
import com.example.common.ResultCode;
import com.example.vo.EchartsData;
import com.example.vo.ShipinjiaoxueInfoVo;
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 = "/shipinjiaoxueInfo")
public class ShipinjiaoxueInfoController {
@Resource
private ShipinjiaoxueInfoService shipinjiaoxueInfoService;
@Resource
private ShipinjiaoxueInfoDao shipinjiaoxueInfoDao;
@PostMapping
public Result<ShipinjiaoxueInfo> add(@RequestBody ShipinjiaoxueInfoVo shipinjiaoxueInfo) {
//mixmajixami
shipinjiaoxueInfoService.add(shipinjiaoxueInfo);
return Result.success(shipinjiaoxueInfo);
}
//youtixing1
//youtixing2
@GetMapping("/getByDiqu")
public Result<List<Map<String,String>>> qidu() {
return Result.success(shipinjiaoxueInfoService.findByDiqu());
}
@DeleteMapping("/{id}")
public Result delete(@PathVariable Long id) {
shipinjiaoxueInfoService.delete(id);
return Result.success();
}
@PutMapping
public Result update(@RequestBody ShipinjiaoxueInfoVo shipinjiaoxueInfo) {
shipinjiaoxueInfoService.update(shipinjiaoxueInfo);
return Result.success();
}
//@PutMapping("/update2")
// public Result update2(@RequestBody ShipinjiaoxueInfoVo shipinjiaoxueInfo) {
// shipinjiaoxueInfoService.update2(shipinjiaoxueInfo);
// return Result.success();
// }
@GetMapping("/{id}")
public Result<ShipinjiaoxueInfo> detail(@PathVariable Long id) {
ShipinjiaoxueInfo shipinjiaoxueInfo = shipinjiaoxueInfoService.findById(id);
return Result.success(shipinjiaoxueInfo);
}
@GetMapping("/changeStatus/{id}")
public Result<ShipinjiaoxueInfo> changeStatus(@PathVariable Long id) {
shipinjiaoxueInfoService.changeStatus(id);
return Result.success();
}
@GetMapping
public Result<List<ShipinjiaoxueInfoVo>> all() {
return Result.success(shipinjiaoxueInfoService.findAll());
}
@GetMapping("/page/{name}")
public Result<PageInfo<ShipinjiaoxueInfoVo>> page(@PathVariable String name,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "5") Integer pageSize,
HttpServletRequest request) {
return Result.success(shipinjiaoxueInfoService.findPage(name, pageNum, pageSize, request));
}
@GetMapping("/pageqt/{name}")
public Result<PageInfo<ShipinjiaoxueInfoVo>> pageqt(@PathVariable String name,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "8") Integer pageSize,
HttpServletRequest request) {
return Result.success(shipinjiaoxueInfoService.findPageqt(name, pageNum, pageSize, request));
}
// @PostMapping("/register")
// public Result<ShipinjiaoxueInfo> register(@RequestBody ShipinjiaoxueInfo shipinjiaoxueInfo) {
// if (StrUtil.isBlank(shipinjiaoxueInfo.getName()) || StrUtil.isBlank(shipinjiaoxueInfo.getPassword())) {
// throw new CustomException(ResultCode.PARAM_ERROR);
// }
// return Result.success(shipinjiaoxueInfoService.add(shipinjiaoxueInfo));
// }
/**
* 批量通过excel添加信息
* @param file excel文件
* @throws IOException
*/
@PostMapping("/upload")
public Result upload(MultipartFile file) throws IOException {
List<ShipinjiaoxueInfo> infoList = ExcelUtil.getReader(file.getInputStream()).readAll(ShipinjiaoxueInfo.class);
if (!CollectionUtil.isEmpty(infoList)) {
// 处理一下空数据
List<ShipinjiaoxueInfo> resultList = infoList.stream().filter(x -> ObjectUtil.isNotEmpty(x.getShipinmingcheng())).collect(Collectors.toList());
for (ShipinjiaoxueInfo info : resultList) {
shipinjiaoxueInfoService.add(info);
}
}
return Result.success();
}
@GetMapping("/get/shipinjiaoxue_tj_suoshuleibie")
Result<List<EchartsData>> shipinjiaoxue_tj_suoshuleibie() {
List<EchartsData> list = new ArrayList<>();
List<Map<String, Object>> shipinjiaoxue_tj_suoshuleibieList = shipinjiaoxueInfoDao.shipinjiaoxue_tj_suoshuleibie();
Map<String, Double> typeMap = new HashMap<>();
for (Map<String, Object> map : shipinjiaoxue_tj_suoshuleibieList) {
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("shipinbianhao", "A视频编号");
row.put("shipinmingcheng", "A视频名称");
row.put("suoshuleibie", "A所属类别");
row.put("shipin", "A视频");
row.put("xuyaojinbi", "A需要金币");
row.put("shipinjianjie", "A视频简介");
row.put("status", "是");
row.put("level", "shipinjiaoxue");
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=shipinjiaoxueInfoModel.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("shipinbianhao", "A视频编号");
row.put("shipinmingcheng", "A视频名称");
row.put("suoshuleibie", "A所属类别");
row.put("shipin", "A视频");
row.put("xuyaojinbi", "A需要金币");
row.put("shipinjianjie", "A视频简介");
row.put("status", "是");
row.put("level", "权限");
List<Map<String, Object>> list = CollUtil.newArrayList(row);
List<Map<String, Object>> daochuexcellist = shipinjiaoxueInfoDao.daochuexcel();
Map<String, Double> typeMap = new HashMa