| | |
| | | import cn.hutool.core.date.DateTime; |
| | | import cn.hutool.core.date.DateUnit; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.support.ExcelTypeEnum; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.gkhy.hazmat.common.api.CommonPage; |
| | | import com.gkhy.hazmat.common.config.IdTableNameHandler; |
| | | import com.gkhy.hazmat.common.domain.entity.SysUser; |
| | | import com.gkhy.hazmat.common.enums.HazmatPackageEnum; |
| | | import com.gkhy.hazmat.common.enums.HazmatStatusEnum; |
| | | import com.gkhy.hazmat.common.enums.UserTypeEnum; |
| | | import com.gkhy.hazmat.common.excel.HazmatBasicUseExcelData; |
| | | import com.gkhy.hazmat.common.exception.ApiException; |
| | | import com.gkhy.hazmat.common.utils.PageUtils; |
| | | import com.gkhy.hazmat.common.utils.SecurityUtils; |
| | |
| | | import com.gkhy.hazmat.system.domain.vo.HzHomeDataVO; |
| | | import com.gkhy.hazmat.system.mapper.*; |
| | | import com.gkhy.hazmat.system.service.HzStatisticService; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLEncoder; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | }else{ |
| | | startDate=startTime; |
| | | endDate=endTime; |
| | | if(DateUtil.between(new Date(endDate),new Date(startTime), DateUnit.DAY)>90){ |
| | | if(DateUtil.between(DateUtil.parse(startDate),DateUtil.parse(startTime), DateUnit.DAY)>90){ |
| | | throw new ApiException("只能查询90天以内数据"); |
| | | } |
| | | } |
| | |
| | | return CommonPage.restPage(hazmatUseVOList); |
| | | } |
| | | |
| | | @Override |
| | | public void importBaiscUse(HttpServletResponse response, String startTime, String endTime) throws IOException { |
| | | SysUser currentUser= SecurityUtils.getLoginUser().getUser(); |
| | | checkUserAllowed(currentUser); |
| | | OutputStream outputStream = response.getOutputStream(); |
| | | try { |
| | | String startDate=""; |
| | | String endDate=""; |
| | | if(StringUtils.isBlank(startTime)&&StringUtils.isBlank(endTime)){ |
| | | Date currentDate=new Date(); |
| | | startDate= DateUtil.format(DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.endOfDay(currentDate), -29)), DatePattern.NORM_DATETIME_FORMAT); |
| | | endDate=DateUtil.format(DateUtil.endOfDay(currentDate),DatePattern.NORM_DATETIME_FORMAT); |
| | | } else if (StringUtils.isBlank(startTime)&&StringUtils.isNotBlank(endTime)) { |
| | | startDate= DateUtil.format(DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.endOfDay(new Date(endTime)), -29)), DatePattern.NORM_DATETIME_FORMAT); |
| | | endDate=endTime; |
| | | }else if (StringUtils.isNotBlank(startTime)&&StringUtils.isBlank(endTime)){ |
| | | startDate=startTime; |
| | | endDate=DateUtil.format(DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.endOfDay(new Date(endTime)), 29)), DatePattern.NORM_DATETIME_FORMAT); |
| | | }else{ |
| | | startDate=startTime; |
| | | endDate=endTime; |
| | | if(DateUtil.between(DateUtil.parse(startDate),DateUtil.parse(startTime), DateUnit.DAY)>90){ |
| | | throw new ApiException("只能查询90天以内数据"); |
| | | } |
| | | } |
| | | //设置分表id |
| | | IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); |
| | | // PageUtils.startPage(); |
| | | List<HzHazmatUseVO> hazmatUseVOList= hazmatMapper.useCountStatic(startDate, endDate,currentUser.getCompanyId()); |
| | | IdTableNameHandler.removeCurrentId(); |
| | | List<HazmatBasicUseExcelData> hazmatBasicUseExcelDatas = new ArrayList<>(); |
| | | if(!hazmatUseVOList.isEmpty()) { |
| | | List<Long> basicIds = hazmatUseVOList.stream().map(HzHazmatUseVO::getBasicId).collect(Collectors.toList()); |
| | | List<HzHazmatBasic> hzHazmatBasicList = hazmatBasicMapper.selectHazmatBasicListByIds(basicIds); |
| | | Map<Long, HzHazmatBasic> hazmatBasicMap = hzHazmatBasicList.stream().collect(Collectors.toMap(HzHazmatBasic::getId, item -> item)); |
| | | Integer index=1; |
| | | for (HzHazmatUseVO hu : hazmatUseVOList) { |
| | | HazmatBasicUseExcelData hazmatBasicUseExcelData = new HazmatBasicUseExcelData(); |
| | | hazmatBasicUseExcelData.setCount(hu.getCount()); |
| | | HzHazmatBasic hazmatBasic = hazmatBasicMap.get(hu.getBasicId()); |
| | | if (hazmatBasic != null) { |
| | | BeanUtils.copyProperties(hazmatBasic,hazmatBasicUseExcelData); |
| | | hazmatBasicUseExcelData.setIndex(index); |
| | | hazmatBasicUseExcelData.setProductType(hazmatBasic.getHazmatType()); |
| | | hazmatBasicUseExcelData.setProductCharacter(hazmatBasic.getHazmatCharacter()); |
| | | hazmatBasicUseExcelData.setProductFormat(hazmatBasic.getHazmatFormat()); |
| | | hazmatBasicUseExcelData.setMinPackage(HazmatPackageEnum.getInfoByCode(hazmatBasic.getMinPackage())); |
| | | } |
| | | index++; |
| | | hazmatBasicUseExcelDatas.add(hazmatBasicUseExcelData); |
| | | } |
| | | } |
| | | Date date = new Date(); |
| | | long time = date.getTime(); |
| | | String fileName = "危化品用量统计_"+time; |
| | | this.setExcelResponseProp(response,fileName); |
| | | |
| | | //使用 EasyExcel 导出数据 |
| | | EasyExcel.write(response.getOutputStream(), HazmatBasicUseExcelData.class) |
| | | .excelType(ExcelTypeEnum.XLSX) |
| | | .sheet("危化品使用记录统计表") // 设置 Sheet 名称 |
| | | .doWrite(hazmatBasicUseExcelDatas); // 写入数据 |
| | | |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | | |
| | | private void setExcelResponseProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException { |
| | | //设置内容类型 |
| | | response.setContentType("application/vnd.vnd.ms-excel"); |
| | | //设置编码格式 |
| | | response.setCharacterEncoding("utf-8"); |
| | | //设置导出文件名称(避免乱码) |
| | | String fileName = URLEncoder.encode(rawFileName.concat(".xlsx"), "UTF-8"); |
| | | // 设置响应头 |
| | | response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName); |
| | | } |
| | | |
| | | public void checkUserAllowed(SysUser user) { |
| | | if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) { |
| | | throw new ApiException("管理员不能操作"); |