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.QiyexinxiInfo;
import com.example.dao.QiyexinxiInfoDao;
import com.example.service.QiyexinxiInfoService;
import com.example.exception.CustomException;
import com.example.common.ResultCode;
import com.example.vo.EchartsData;
import com.example.vo.QiyexinxiInfoVo;
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 = "/qiyexinxiInfo")
public class QiyexinxiInfoController {
@Resource
private QiyexinxiInfoService qiyexinxiInfoService;
@Resource
private QiyexinxiInfoDao qiyexinxiInfoDao;
@PostMapping
public Result<QiyexinxiInfo> add(@RequestBody QiyexinxiInfoVo qiyexinxiInfo) {
//mixmajixami
qiyexinxiInfoService.add(qiyexinxiInfo);
return Result.success(qiyexinxiInfo);
}
//youtixing1
//youtixing2
@GetMapping("/getByDiqu")
public Result<List<Map<String,String>>> qidu() {
return Result.success(qiyexinxiInfoService.findByDiqu());
}
@DeleteMapping("/{id}")
public Result delete(@PathVariable Long id) {
qiyexinxiInfoService.delete(id);
return Result.success();
}
@PutMapping
public Result update(@RequestBody QiyexinxiInfoVo qiyexinxiInfo) {
qiyexinxiInfoService.update(qiyexinxiInfo);
return Result.success();
}
//@PutMapping("/update2")
// public Result update2(@RequestBody QiyexinxiInfoVo qiyexinxiInfo) {
// qiyexinxiInfoService.update2(qiyexinxiInfo);
// return Result.success();
// }
@GetMapping("/{id}")
public Result<QiyexinxiInfo> detail(@PathVariable Long id) {
QiyexinxiInfo qiyexinxiInfo = qiyexinxiInfoService.findById(id);
return Result.success(qiyexinxiInfo);
}
@GetMapping("/changeStatus/{id}")
public Result<QiyexinxiInfo> changeStatus(@PathVariable Long id) {
qiyexinxiInfoService.changeStatus(id);
return Result.success();
}
@GetMapping
public Result<List<QiyexinxiInfoVo>> all() {
return Result.success(qiyexinxiInfoService.findAll());
}
@GetMapping("/page/{name}")
public Result<PageInfo<QiyexinxiInfoVo>> page(@PathVariable String name,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "5") Integer pageSize,
HttpServletRequest request) {
return Result.success(qiyexinxiInfoService.findPage(name, pageNum, pageSize, request));
}
@GetMapping("/pageqt/{name}")
public Result<PageInfo<QiyexinxiInfoVo>> pageqt(@PathVariable String name,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "8") Integer pageSize,
HttpServletRequest request) {
return Result.success(qiyexinxiInfoService.findPageqt(name, pageNum, pageSize, request));
}
// @PostMapping("/register")
// public Result<QiyexinxiInfo> register(@RequestBody QiyexinxiInfo qiyexinxiInfo) {
// if (StrUtil.isBlank(qiyexinxiInfo.getName()) || StrUtil.isBlank(qiyexinxiInfo.getPassword())) {
// throw new CustomException(ResultCode.PARAM_ERROR);
// }
// return Result.success(qiyexinxiInfoService.add(qiyexinxiInfo));
// }
/**
* 批量通过excel添加信息
* @param file excel文件
* @throws IOException
*/
@PostMapping("/upload")
public Result upload(MultipartFile file) throws IOException {
List<QiyexinxiInfo> infoList = ExcelUtil.getReader(file.getInputStream()).readAll(QiyexinxiInfo.class);
if (!CollectionUtil.isEmpty(infoList)) {
// 处理一下空数据
List<QiyexinxiInfo> resultList = infoList.stream().filter(x -> ObjectUtil.isNotEmpty(x.getQiyemingcheng())).collect(Collectors.toList());
for (QiyexinxiInfo info : resultList) {
qiyexinxiInfoService.add(info);
}
}
return Result.success();
}
@GetMapping("/get/qiyexinxi_tj_qiyeleixing")
Result<List<EchartsData>> qiyexinxi_tj_qiyeleixing() {
List<EchartsData> list = new ArrayList<>();
List<Map<String, Object>> qiyexinxi_tj_qiyeleixingList = qiyexinxiInfoDao.qiyexinxi_tj_qiyeleixing();
Map<String, Double> typeMap = new HashMap<>();
for (Map<String, Object> map : qiyexinxi_tj_qiyeleixingList) {
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/qiyexinxi_tj_suoshuxingye")
Result<List<EchartsData>> qiyexinxi_tj_suoshuxingye() {
List<EchartsData> list = new ArrayList<>();
List<Map<String, Object>> qiyexinxi_tj_suoshuxingyeList = qiyexinxiInfoDao.qiyexinxi_tj_suoshuxingye();
Map<String, Double> typeMap = new HashMap<>();
for (Map<String, Object> map : qiyexinxi_tj_suoshuxingyeList) {
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("qiyebianhao", "A企业编号");
row.put("qiyemingcheng", "A企业名称");
row.put("qiyeleixing", "A企业类型");
row.put("suoshuxingye", "A所属行业");
row.put("fuzeren", "A负责人");
row.put("lianxidianhua", "A联系电话");
row.put("zhucezijin", "A注册资金");
row.put("dizhi", "A地址");
row.put("zhaopian", "A照片");
row.put("qiyejianjie", "A企业简介");
row.put("status", "是");
row.put("level", "qiyexinxi");
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=qiyexinxiInfoModel.xlsx");
ServletOutputStream out = response.getOutputStream();
writer.flush(out, true);
writer.close();
IoUtil.close(System.out);
}
@GetMapping("/getExcel")
public void getExcel(HttpServletResponse response) throws IOException {