| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- package cn.com.goldenwater.dcproj.service.impl.accunt;
- import cn.com.goldenwater.dcproj.dao.BisInspAccuntDao;
- import cn.com.goldenwater.dcproj.model.BisInspAccunt;
- import cn.com.goldenwater.dcproj.param.BisInspAccuntParam;
- import cn.com.goldenwater.dcproj.service.BisInspAccuntService;
- import cn.com.goldenwater.core.service.AbstractCrudService;
- import cn.com.goldenwater.dcproj.utils.BisInspAccuntVerify;
- import cn.com.goldenwater.dcproj.utils.ExcelVerifyUtil;
- import cn.com.goldenwater.dcproj.utils.ImportUtil;
- import cn.com.goldenwater.dcproj.utils.StringUtils;
- import org.apache.poi.ss.usermodel.*;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import cn.com.goldenwater.id.util.UuidUtil;
- import org.springframework.web.multipart.MultipartFile;
- import javax.servlet.http.HttpServletResponse;
- import java.io.*;
- import java.net.URLEncoder;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.List;
- import java.util.Date;
- /**
- * @author lhc
- * @date 2022-3-17
- */
- @Service
- @Transactional
- public class BisInspAccuntServiceImpl extends AbstractCrudService<BisInspAccunt, BisInspAccuntParam> implements BisInspAccuntService {
- @Autowired
- private BisInspAccuntDao bisInspAccuntDao;
- @Value("${export.templatePath}")
- private String templatePath;
- @Autowired
- BisInspAccuntVerify accuntVerify;
- private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- public BisInspAccuntServiceImpl(BisInspAccuntDao bisInspAccuntDao) {
- super(bisInspAccuntDao);
- this.bisInspAccuntDao = bisInspAccuntDao;
- }
- @Override
- public int insert(BisInspAccunt bisInspAccunt) {
- String uuid = UuidUtil.uuid(); // 生成uuid
- bisInspAccunt.setId(uuid);
- bisInspAccunt.setIntm(new Date());
- bisInspAccunt.setUptm(new Date());
- bisInspAccunt.setDataStat("0");
- return this.bisInspAccuntDao.insert(bisInspAccunt);
- }
- @Override
- public int update(BisInspAccunt bisInspAccunt) {
- bisInspAccunt.setUptm(new Date());
- return this.bisInspAccuntDao.update(bisInspAccunt);
- }
- @Override
- public int delete(String id) {
- return this.bisInspAccuntDao.delete(id);
- }
- @Override
- public void batch(List<BisInspAccunt> bisInspAccunt) {
- for(int i = 0;i<bisInspAccunt.size();i++){
- int ret = update(bisInspAccunt.get(i));
- }
- }
- @Override
- public void export(HttpServletResponse response, BisInspAccuntParam bisInspAccuntParam) {
- String templateFilePath = templatePath+ File.separator +"zgjgtz.xls";
- File file = new File(templateFilePath);
- if(file.exists()){
- try{
- InputStream inputStream = new FileInputStream(file);
- Workbook workbook = WorkbookFactory.create(inputStream);
- exportFile(workbook,bisInspAccuntParam);
- writeToResponse(response,workbook,"追责结果台账.xls");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- @Override
- public List<String> importFile(MultipartFile file, BisInspAccunt account) throws Exception {
- ImportUtil<BisInspAccunt> util = new ImportUtil<BisInspAccunt>(){
- @Override
- public void before(BisInspAccunt bisInspAccunt) {
- bisInspAccunt.setProvince(account.getProvince());
- bisInspAccunt.setOrgId(account.getOrgId());
- bisInspAccunt.setPersId(account.getPersId());
- }
- };
- List<BisInspAccunt> list;
- try {
- list = util.fileToList(file, BisInspAccunt.class);
- } catch (Exception e) { throw new RuntimeException(e); }
- ExcelVerifyUtil verifyUtil = new ExcelVerifyUtil<>(list, accuntVerify);
- List<BisInspAccunt> rightList = verifyUtil.getRightList();
- rightList.forEach(a -> this.insert(a));
- return verifyUtil.getErrorList();
- }
- private void exportFile(Workbook xssfWorkbook, BisInspAccuntParam bisInspAccuntParam) throws ParseException {
- List<BisInspAccunt> list = bisInspAccuntDao.findList(bisInspAccuntParam);
- Sheet sheet = xssfWorkbook.getSheetAt(0);
- String title = "追责结果台账";
- if(StringUtils.isNotBlank(bisInspAccuntParam.getYear())){
- title = title+"("+bisInspAccuntParam.getYear()+")";
- }
- CellStyle styleColor = sheet.getRow(1).getCell(0).getCellStyle();
- CellStyle styleContent = xssfWorkbook.createCellStyle();
- styleColor.setAlignment(HorizontalAlignment.CENTER);
- styleColor.setVerticalAlignment(VerticalAlignment.CENTER);
- styleContent.setAlignment(HorizontalAlignment.CENTER);
- styleContent.setVerticalAlignment(VerticalAlignment.CENTER);
- setCellValue(sheet, 0, 1, title);
- if(list!=null && list.size()>0){
- for(int i = 0;i<=list.size();i++){
- Row row = sheet.createRow(2+i);
- int cols = 9;
- for(int j = 0;j<=cols;j++){
- Cell cell = row.createCell(j);
- cell.setCellStyle(styleContent);
- if(i==list.size()){
- cell.setCellStyle(styleColor);
- }
- }
- }
- for(int i = 0;i<list.size();i++){
- BisInspAccunt bisInspAccunt = list.get(i);
- setAllCell(sheet,i,bisInspAccunt);
- }
- }
- }
- private void setAllCell(Sheet sheet, int i, BisInspAccunt bisInspAccunt) {
- setCellValue(sheet, 2 + i, 0, String.valueOf(i + 1));
- setCellValue(sheet, 2 + i, 1, bisInspAccunt.getAdName());
- setCellValue(sheet, 2 + i, 2, bisInspAccunt.getAccuntPrp().equals("1")?"被上级追责":bisInspAccunt.getAccuntPrp().equals("2")?"对下级追责":"");
- setCellValue(sheet, 2 + i, 3, bisInspAccunt.getAccuntMttr());
- setCellValue(sheet, 2 + i, 4, bisInspAccunt.getAccuntMprp().equals("1")?"责任单位":bisInspAccunt.getAccuntMprp().equals("1")?"责任人":"");
- setCellValue(sheet, 2 + i, 5, bisInspAccunt.getUnitName());
- setCellValue(sheet, 2 + i, 6, bisInspAccunt.getAccuntName());
- setCellValue(sheet, 2 + i, 7, bisInspAccunt.getAccuntItem());
- setCellValue(sheet, 2 + i, 8, bisInspAccunt.getAccuntKind());
- setCellValue(sheet, 2 + i, 9, simpleDateFormat.format(bisInspAccunt.getAccuntTm()));
- }
- private void setCellValue(Sheet sheet, int rownum, int cellnum, String value) {
- Cell cell = sheet.getRow(rownum).getCell(cellnum);
- cell.setCellValue(value);
- }
- private void writeToResponse(HttpServletResponse response, Workbook workbook, String name) throws IOException {
- // 修改模板内容导出新模板
- response.setContentType("application/x-msdownload");
- String fileNameURL = URLEncoder.encode(name, "UTF-8");
- response.setHeader("Content-disposition", "attachment;filename=" + fileNameURL + ";filename*=utf-8''" + fileNameURL);
- OutputStream out = response.getOutputStream();
- workbook.write(out);
- out.flush();
- out.close();
- }
- }
|