fix
songhuangfeng123
2022-07-27 9112f70f645d3521fa490e648cdce70b9a7254f0
goal-manage/goal-manage-service/src/main/java/com/gkhy/safePlatform/targetDuty/controller/TargetMngController.java
@@ -1,30 +1,38 @@
package com.gkhy.safePlatform.targetDuty.controller;
import java.util.Date;
import java.sql.Timestamp;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.targetDuty.entity.TargetDivideDetail;
import com.gkhy.safePlatform.targetDuty.entity.TargetMng;
import com.gkhy.safePlatform.targetDuty.service.TargetDivideDetailService;
import com.gkhy.safePlatform.targetDuty.service.TargetMngService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.utils.BeanCopyUtils;
import com.gkhy.safePlatform.commons.utils.PageUtils;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.targetDuty.entity.TargetDivideDetail;
import com.gkhy.safePlatform.targetDuty.entity.TargetMng;
import com.gkhy.safePlatform.targetDuty.model.dto.req.TargetMngImportExcel;
import com.gkhy.safePlatform.targetDuty.model.dto.req.TargetMngQueryCriteria;
import com.gkhy.safePlatform.targetDuty.model.dto.resp.TargetMngExcel;
import com.gkhy.safePlatform.targetDuty.service.TargetDivideDetailService;
import com.gkhy.safePlatform.targetDuty.service.TargetMngService;
import com.gkhy.safePlatform.targetDuty.utils.DateUtils;
import com.gkhy.safePlatform.targetDuty.utils.poihelper.ExcelLogs;
import com.gkhy.safePlatform.targetDuty.utils.poihelper.ExcelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
/**
 * 目标指标(TargetMng)表控制层
@@ -42,6 +50,11 @@
    private TargetMngService targetMngService;
    @Resource
    private TargetDivideDetailService targetDivideDetailService;
    @Autowired
    public HttpServletRequest request;
    @Autowired
    public HttpServletResponse response;
    /**
@@ -58,7 +71,7 @@
      PageUtils.checkCheck(pageQuery.getPageIndex(), pageQuery.getPageSize());
      return this.targetMngService.queryAll(pageQuery);
    }
    /**
     * 通过主键查询单条数据
@@ -110,26 +123,85 @@
        return new ResultVO<>(ResultCodes.OK);
    }
    /**
     * 下载模板
     *
     */
    @GetMapping(value = "/exportTemplate")
    public void exportTemplate() throws IOException {
        Map<String,String> map = new LinkedHashMap<>();
        map.put("1","安全目标指标");
        map.put("2","目标指标编号");
        map.put("3","指标类型 1:年指标 2:月指标");
        map.put("4","年度");
        map.put("5","指标值");
        map.put("6","指标级别 1:公司级 2:部门分厂级 3:工段班组级");
        map.put("7","完成期限(yyyy-MM-dd HH:mm:ss)");
        map.put("8","备注信息");
//    /**
//     * 导出
//     * @param response /
//     * @throws IOException /
//     */
//    public void download(HttpServletResponse response) throws IOException {
//        List<Map<String, Object>> list = new ArrayList<>();
//        for (OnlineUser user : all) {
//            Map<String, Object> map = new LinkedHashMap<>();
//            map.put("用户名", user.getUserName());
//            map.put("用户昵称", user.getNickName());
//            map.put("登录IP", user.getIp());
//            map.put("登录地点", user.getAddress());
//            map.put("浏览器", user.getBrowser());
//            map.put("登录日期", user.getLoginTime());
//            list.add(map);
//        }
//        FileUtil.downloadExcel(list, response);
//    }
        String fileName = URLEncoder.encode("目标设置数据导入模板.xls", "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        ExcelUtil.exportExcel(map,new ArrayList<>() , response.getOutputStream());
        response.getOutputStream().close();
    }
    /**
     * 导出一览数据
     *
     */
    @GetMapping(value = "/exportData")
    public void exportData(TargetMngQueryCriteria queryCriteria) throws IOException {
        Map<String,String> map = new LinkedHashMap<>();
        map.put("1","安全目标指标");
        map.put("2","目标指标编号");
        map.put("3","年度");
        map.put("4","指标值");
        map.put("5","指标级别");
        map.put("6","指标类型");
        map.put("7","完成期限");
        map.put("8","状态");
        map.put("9","备注信息");
        String key = DateUtils.date2String(new Date(), DateUtils.PATTERN_ALLTIME_NOSIGN) ;
        String fileName = URLEncoder.encode("目标设置"+key+".xls", "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        List<TargetMngExcel> respList = BeanCopyUtils.copyBeanList(targetMngService.queryAll(queryCriteria), TargetMngExcel.class);
        ExcelUtil.exportExcel(map,respList , response.getOutputStream(),DateUtils.PATTERN_STANDARD);
        response.getOutputStream().close();
    }
    /**
     * 导入数据
     *
     */
    @RequestMapping(value = "/importData")
    public ResultVO importData(MultipartFile file) throws IOException {
        String contentType = file.getContentType();
        if(!"application/vnd.ms-excel".equals(contentType)
        && !"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(contentType)) {
            return new ResultVO<>(ResultCodes.CLIENT_PARAM_ILLEGAL, "上传的excel格式错误");
        }
        Collection<TargetMngImportExcel> importExcel = ExcelUtil.importExcel(TargetMngImportExcel.class, file.getInputStream(), "yyyy-MM-dd HH:mm:ss", new ExcelLogs() , 0);
        if (CollectionUtils.isEmpty(importExcel)) {
            return new ResultVO<>(ResultCodes.OK);
        }
        List<TargetMng> respList = BeanCopyUtils.copyBeanList((List<TargetMngImportExcel>)importExcel, TargetMng.class);
        targetMngService.saveBatch(respList);
        return new ResultVO<>(ResultCodes.OK);
    }
    public static void main(String[] args) {
        TargetMng mng = new TargetMng();
@@ -145,4 +217,4 @@
        System.out.println(JSONObject.toJSONString(mng));
    }
}
}