package com.gkhy.safePlatform.targetDuty.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; 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.targetDuty.entity.RewardPunishmentStandard; import com.gkhy.safePlatform.targetDuty.model.dto.req.RewardPunishmentStandardImportExcel; import com.gkhy.safePlatform.targetDuty.model.dto.req.RewardPunishmentStandardQueryCriteria; import com.gkhy.safePlatform.targetDuty.model.dto.resp.RewardPunishmentStandardExcel; import com.gkhy.safePlatform.targetDuty.service.RewardPunishmentStandardService; 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.util.*; import java.util.stream.Collectors; /** * (RewardPunishmentStandard)表控制层 * * @author xurui * @since 2022-07-21 10:20:10 */ @RestController @RequestMapping("rewardPunishmentStandard") public class RewardPunishmentStandardController { /** * 服务对象 */ @Resource private RewardPunishmentStandardService rewardPunishmentStandardService; @Autowired public HttpServletRequest request; @Autowired public HttpServletResponse response; /** * 分页查询所有数据 * * @param pageQuery 查询实体 * @return 所有数据 */ @PostMapping(value = "/page/list") public ResultVO selectAll(@RequestBody PageQuery pageQuery){ PageUtils.checkCheck(pageQuery); return this.rewardPunishmentStandardService.queryAll(pageQuery); } /** * 通过主键查询单条数据 * * @param id 主键 * @return 单条数据 */ @GetMapping(value = "/selectOne/{id}") public ResultVO selectOne(@PathVariable Serializable id) { return new ResultVO<>(ResultCodes.OK,this.rewardPunishmentStandardService.getById(id)); } /** * 新增或者修改数据 * * @param rewardPunishmentStandard 实体对象 * @return 修改结果 */ @PostMapping(value = "/addOrUpdate") public ResultVO update(@RequestBody RewardPunishmentStandard rewardPunishmentStandard) { if(rewardPunishmentStandard.getStandardType() == null){ return new ResultVO<>(ResultCodes.CLIENT_PARAM_ILLEGAL,"缺少standardType"); } if(!StringUtils.hasText(rewardPunishmentStandard.getqName())){ return new ResultVO<>(ResultCodes.CLIENT_PARAM_ILLEGAL,"缺少qName"); } if (rewardPunishmentStandard.getId() == null) { return new ResultVO<>(ResultCodes.OK,rewardPunishmentStandardService.save(rewardPunishmentStandard)); } else { rewardPunishmentStandardService.update(rewardPunishmentStandard,new UpdateWrapper().eq("id",rewardPunishmentStandard.getId())); return new ResultVO<>(ResultCodes.OK); } } /** * 删除数据 * * @param ids 主键结合 * @return 删除结果 */ @RequestMapping(value = "/delete",method = RequestMethod.POST) public ResultVO delete(@RequestBody Long[] ids) { if(ids == null){ return new ResultVO<>(ResultCodes.CLIENT_PARAM_ILLEGAL); } List idList = Arrays.asList(ids); this.rewardPunishmentStandardService.removeByIds(idList); return new ResultVO<>(ResultCodes.OK); } /** * 下载模板 * */ @GetMapping(value = "/exportTemplate") public void exportTemplate() throws IOException { Map map = new LinkedHashMap<>(); map.put("1","奖惩名称"); map.put("2","奖惩类型 1:奖励 2:惩罚"); map.put("3","奖惩内容"); map.put("4","依据"); map.put("5","备注"); 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(RewardPunishmentStandardQueryCriteria queryCriteria) throws IOException { Map map = new LinkedHashMap<>(); map.put("1","奖惩名称"); map.put("2","奖惩类型"); map.put("3","奖惩内容"); map.put("4","依据"); map.put("5","备注"); 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 respList = BeanCopyUtils.copyBeanList(rewardPunishmentStandardService.queryAll(queryCriteria), RewardPunishmentStandardExcel.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 importExcel = ExcelUtil.importExcel(RewardPunishmentStandardImportExcel.class, file.getInputStream(), "yyyy-MM-dd HH:mm:ss", new ExcelLogs() , 0); if (CollectionUtils.isEmpty(importExcel)) { return new ResultVO<>(ResultCodes.OK); } List respList = BeanCopyUtils.copyBeanList((List)importExcel, RewardPunishmentStandard.class); rewardPunishmentStandardService.saveBatch(respList); return new ResultVO<>(ResultCodes.OK); } public static void main(String[] args) { RewardPunishmentStandard mng = new RewardPunishmentStandard(); mng.setStandardType(1); mng.setContent("1"); mng.setqName("11"); mng.setReason("111"); mng.setMemo("1111"); System.out.println(JSONObject.toJSONString(mng)); } }