TaxDocumentController.java 15.3 KB
Newer Older
chase's avatar
chase committed
1 2
package pwc.taxtech.atms.controller;

chase's avatar
chase committed
3 4
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
chase's avatar
chase committed
5 6
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
chase's avatar
chase committed
7
import net.sf.json.JSONNull;
chase's avatar
chase committed
8
import org.apache.commons.collections.CollectionUtils;
chase's avatar
chase committed
9
import org.apache.commons.lang3.StringUtils;
chase's avatar
chase committed
10 11 12 13 14 15
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
chase's avatar
chase committed
16 17 18 19 20 21
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pwc.taxtech.atms.common.PageResultVo;
import pwc.taxtech.atms.common.util.DateUtils;
chase's avatar
chase committed
22
import pwc.taxtech.atms.constant.enums.FileUploadEnum;
chase's avatar
chase committed
23
import pwc.taxtech.atms.dpo.OrgSelectDto;
chase's avatar
chase committed
24 25
import pwc.taxtech.atms.dto.TaxDocumentDto;
import pwc.taxtech.atms.entity.TaxDocument;
chase's avatar
chase committed
26
import pwc.taxtech.atms.service.impl.DidiFileUploadService;
chase's avatar
chase committed
27
import pwc.taxtech.atms.service.impl.OrganizationServiceImpl;
chase's avatar
chase committed
28 29
import pwc.taxtech.atms.service.impl.TaxDocumentServiceImpl;
import pwc.taxtech.atms.thirdparty.ExcelUtil;
chase's avatar
chase committed
30
import pwc.taxtech.atms.vat.entity.FileUpload;
chase's avatar
chase committed
31 32

import javax.servlet.http.HttpServletResponse;
chase's avatar
chase committed
33 34 35
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
chase's avatar
chase committed
36
import java.text.SimpleDateFormat;
chase's avatar
chase committed
37
import java.util.*;
chase's avatar
chase committed
38
import java.util.stream.Collectors;
chase's avatar
chase committed
39 40 41 42 43 44 45 46

@Controller
@RequestMapping("/api/v1/taxDoc")
public class TaxDocumentController {

    @Autowired
    private TaxDocumentServiceImpl taxDocumentService;

chase's avatar
chase committed
47 48
    @Autowired
    private DidiFileUploadService didiFileUploadService;
chase's avatar
chase committed
49 50
    @Autowired
    private OrganizationServiceImpl organizationService;
chase's avatar
chase committed
51 52 53
    @PostMapping("selectList")
    @ResponseBody
    public PageResultVo<TaxDocument> selectTaxDocumentList(@RequestBody TaxDocumentDto taxDocumentDto) {
chase's avatar
chase committed
54 55 56 57
        List<OrgSelectDto> orgList = organizationService.getMyOrgList();
        if(CollectionUtils.isEmpty(orgList)){
            return new PageResultVo<>();
        }
chase's avatar
chase committed
58
        PageHelper.startPage(taxDocumentDto.getCurrentPage(), taxDocumentDto.getPageSize());
chase's avatar
chase committed
59 60
        PageInfo<TaxDocument> taxDocumentPageInfo =  new PageInfo<>(taxDocumentService.selectTaxDocumentList(taxDocumentDto,orgList.stream()
                .map(o -> o.getId()).collect(Collectors.toList())));
chase's avatar
chase committed
61 62 63 64 65 66
        List<TaxDocument> list = taxDocumentPageInfo.getList();
        return PageResultVo.getPageResultVo(taxDocumentPageInfo, list);
    }

    @PostMapping("/queryWhetherData")
    @ResponseBody
chase's avatar
chase committed
67
    public TaxDocument queryWhetherData(@RequestBody TaxDocument taxDocument) {
chase's avatar
chase committed
68 69 70 71 72
        return taxDocumentService.queryWhetherData(taxDocument);
    }

    @PostMapping("add")
    @ResponseBody
chase's avatar
chase committed
73 74 75
    public boolean addTaxDocument(@RequestParam(value = "file") MultipartFile file,
                                  @RequestParam(required = false) String modual,
                                  TaxDocument taxDocument) {
chase's avatar
chase committed
76
        return taxDocumentService.addTaxDocumentList(file,taxDocument);
chase's avatar
chase committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    }

    @PostMapping("delete")
    @ResponseBody
    public boolean deleteTaxDocument(@RequestBody TaxDocument taxDocument) {
        return taxDocumentService.deleteTaxDocument(taxDocument.getId());
    }

    @PostMapping("/batchDelete")
    @ResponseBody
    public boolean batchDelete(@RequestBody TaxDocumentDto taxDocumentDto) {
        return taxDocumentService.batchDelete(taxDocumentDto.getIds());
    }

    @PostMapping("edit")
    @ResponseBody
chase's avatar
chase committed
93
    public boolean editTaxDocument(TaxDocument taxDocument,
chase's avatar
chase committed
94
                                   @RequestParam(value = "file",required = false) MultipartFile file,
chase's avatar
chase committed
95 96 97 98 99 100 101
                                   @RequestParam(required = false) String modual) {
        //TODO 判断file是否为null 为null则不做上传和设置新url地址值 若不为null 则上传文件并设置文件url地址值
        if(file!=null&&!file.isEmpty()){
            FileUpload fileUpload = didiFileUploadService.uploadFile(file,file.getOriginalFilename(), FileUploadEnum.BizSource.RECORD_UPLOAD.name());
            taxDocument.setFileUploadId(fileUpload.getUid());
            taxDocument.setFilePositionUrl(fileUpload.getViewHttpUrl());
        }
chase's avatar
chase committed
102 103
        return taxDocumentService.editFilesType(taxDocument);
    }
chase's avatar
chase committed
104 105 106 107 108 109 110

    @GetMapping("/multipalInitData")
    @ResponseBody
    public Map<String,Object> multipalInitData(String address){
        //地址示例: D://multipaiInitData
        return taxDocumentService.multipalInitData(address);
    }
chase's avatar
chase committed
111 112

    /**
chase's avatar
chase committed
113
     * 读取PDF 转换为图片
chase's avatar
chase committed
114 115 116 117
     *
     */
    @PostMapping("/previewPDF")
    @ResponseBody
chase's avatar
chase committed
118 119 120
    public void previewPDF(HttpServletResponse response ,@RequestBody TaxDocumentDto taxDocumentDto) {
//        return taxDocumentService.previewPDF2(response,taxDocumentDto.getPath());
        taxDocumentService.previewPDF(response,taxDocumentDto.getPath());
chase's avatar
chase committed
121
        /*InputStream is = null;
chase's avatar
chase committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        ByteArrayOutputStream os = null;
        try {
            //根据url地址 获取文件输入流
            is = getInputStreamByUrl(taxDocumentDto.getPath());
            os = new ByteArrayOutputStream();
            byte[] buff = new byte[1024];
            int len = 0;
            String dUrlData="";
            while ((len = is.read(buff)) != -1) {
                os.write(buff, 0, len);
            }
            //刷新此输出流并强制写出所有缓冲的输出字节,必须这行代码,否则有可能有问题
            os.flush();
            os.toByteArray();
            dUrlData= Base64.encode(os.toByteArray());
            return dUrlData;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
chase's avatar
chase committed
152 153 154
        }*/


chase's avatar
chase committed
155
    }
chase's avatar
chase committed
156 157
    @RequestMapping("exportExcel")
    @ResponseBody
chase's avatar
chase committed
158
    public void exportExcelFile(HttpServletResponse response, @RequestBody TaxDocumentDto taxDocumentDto) {
chase's avatar
chase committed
159
        try {
chase's avatar
chase committed
160 161 162 163 164 165 166 167 168
            Map<String, String> headers = new LinkedHashMap<>();
            headers.put("Id", "序号");
            headers.put("FileAttr", "档案属性");
            headers.put("FileType", "档案类型");
            headers.put("FileName", "档案名称");
            headers.put("BusinessLine", "业务线");
            headers.put("CompanyName", "公司名称");
            headers.put("TaxType", "税种");
            headers.put("FileTime", "文件生效日期");
chase's avatar
chase committed
169 170 171 172
            headers.put("OwnTime", "所属期间");
            headers.put("EffectiveTime", "到期日");
            headers.put("AuditStatus", "审批状态");
            headers.put("PhysicalIndexNumber", "实物索引号");
chase's avatar
chase committed
173 174
            headers.put("StorageArea", "实物存放地点");
            headers.put("Keeper", "保管人");
chase's avatar
chase committed
175 176
            headers.put("UploadTime", "上传日期");
            headers.put("Creator", "创建人");
chase's avatar
chase committed
177
            headers.put("Remark", "档案备注");
chase's avatar
chase committed
178 179 180 181 182 183 184 185
            List<TaxDocument> TaxDocument =null;
                    List<OrgSelectDto> orgList = organizationService.getMyOrgList();
            if(CollectionUtils.isEmpty(orgList)){
                TaxDocument = new ArrayList<>();
            }else{
                TaxDocument = taxDocumentService.selectTaxDocumentList(taxDocumentDto,orgList.stream()
                        .map(o -> o.getId()).collect(Collectors.toList()));
            }
chase's avatar
chase committed
186
            response.setContentType("multipart/form-data");
chase's avatar
chase committed
187
            response.setHeader("Content-Disposition", "attachment;fileName=" + new String("taxDocument.xlsx".getBytes("GB2312"), "ISO-8859-1"));
chase's avatar
chase committed
188 189
            OutputStream ouputStream = response.getOutputStream();
            ExcelUtil.exportExcel(headers, TaxDocument, ouputStream);
chase's avatar
chase committed
190
            ouputStream.close();
chase's avatar
chase committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件上传接口 createByZhangzezheng
     *
     * @param picture 图片文件
     * @param modual  模块名
     * @return
     */
    @RequestMapping("upload")
    @ResponseBody
    public String upload(@RequestPart("file") MultipartFile picture, @RequestParam(required = false) String modual) {
chase's avatar
chase committed
206 207 208 209 210 211
        return getUploadUrl(picture, modual);
    }

    /**
     * 生成上传url
     *
chase's avatar
chase committed
212
     * @param file
chase's avatar
chase committed
213 214 215
     * @param modual
     * @return
     */
chase's avatar
chase committed
216 217
    private String getUploadUrl(MultipartFile file, String modual) {
        String fileName = file.getOriginalFilename();
chase's avatar
chase committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        String pictureName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
        String dir = DateUtils.getStringDateShort();
        String typePath = "";
        try {
            String fileSavePath = File.separator + "images";
            if (StringUtils.isBlank(modual)) {
                modual = "default";
            }
            if (StringUtils.isNotBlank(modual)) {
                typePath = modual + File.separator + dir;
            }
            File basePath = new File(fileSavePath + File.separator + typePath);
            if (!basePath.exists()) {
                basePath.mkdirs();
            }
chase's avatar
chase committed
233
            file.transferTo(new File(fileSavePath + File.separator + typePath + File.separator + pictureName));
chase's avatar
chase committed
234 235 236 237 238
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "images" + File.separator + typePath + File.separator + pictureName;
    }
chase's avatar
chase committed
239 240


chase's avatar
chase committed
241 242
    /**
     * 读取Excel转换成 Json
chase's avatar
chase committed
243
     *
chase's avatar
chase committed
244 245 246
     */
    @PostMapping("/previewExcelToJson")
    @ResponseBody
chase's avatar
chase committed
247
    public String previewExcel(@RequestBody TaxDocumentDto taxDocumentDto) {
chase's avatar
chase committed
248 249
        try {
            JSONArray dataArray = new JSONArray();
chase's avatar
chase committed
250
            //根据url地址 获取文件输入流
chase's avatar
chase committed
251
            InputStream is = taxDocumentService.getInputStreamByUrl(taxDocumentDto.getPath());
chase's avatar
chase committed
252 253
            InputStream inStream =is;
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inStream);
chase's avatar
chase committed
254 255
            // 循环工作表Sheet 将数据解析后 存入json对象
            workForSheet(dataArray, xssfWorkbook);
chase's avatar
chase committed
256
            return dataArray.toString();
chase's avatar
chase committed
257
        } catch (Exception e) {
chase's avatar
chase committed
258 259 260 261 262
            e.printStackTrace();
            return JSONNull.getInstance().toString();
        }
    }

chase's avatar
chase committed
263

chase's avatar
chase committed
264 265 266 267 268 269 270
    /**
     * 下载全部附件
     */
    @PostMapping(value = "/downloadAllFile")
    public void  downloadAllFile(HttpServletResponse response, @RequestBody TaxDocumentDto taxDocumentDto) {
        taxDocumentService.downloadAllFile(response,taxDocumentDto.getIds());
    }
chase's avatar
chase committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

    /**
     * 解析json
     * @param cell
     * @return
     */
    private static Object toString(XSSFCell cell) {
        switch (cell.getCellTypeEnum()) {
            case _NONE:
                cell.setCellType(CellType.STRING);
                return "";
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                    return sdf.format(cell.getDateCellValue());
                }
                cell.setCellType(CellType.STRING);
                return cell.getStringCellValue();
            case STRING:
                String val = cell.getStringCellValue();
                if ("无".equalsIgnoreCase(val)) return "";
                //将其中的map格式和数组格式的字符串,转化为相应的数据类型
                if (val.indexOf("{") > -1) {
//                    JSONObject jsonObject = JSONObject.parseObject(val);
                    Map<String, Integer> mapJson = JSONObject.parseObject(val,HashMap.class);
                    return mapJson;
                }
                if (val.indexOf("[") > -1) {
                    val = val.substring(1, val.length() - 1);
                    String[] array = val.split(",");
                    return array;
                }
                return val;
            case FORMULA:
                return cell.getCellFormula();
            case BLANK:
                return "";
            case BOOLEAN:
                return cell.getBooleanCellValue() + "";
            case ERROR:
                return "非法字符";
            default:
                return "未知字符";
        }
    }

    /**
     * 输出数据
     */
    private static JSONArray shuffleData(JSONArray sheetArr) {
        JSONArray array = new JSONArray();
        for (int i = 0; i < sheetArr.size(); i++) {
            JSONObject object = sheetArr.getJSONObject(i);
            int count = 0;
            int length = 0;
            for (Object key : object.keySet()) {
                Object o = object.get((String) key);
                length++;
                boolean b = StringUtils.isEmpty(o.toString());
                if (b) {
                    count++;
                }
            }
            if (count != length) {
                array.add(object);
            }
        }
        return array;
    }
chase's avatar
chase committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

    /**
     * 读取sheet数据 添加到json数据中
     * @param dataArray
     * @param xssfWorkbook
     */
    private void workForSheet(JSONArray dataArray, XSSFWorkbook xssfWorkbook) {
        for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
            String sheetName = xssfSheet.getSheetName();
            if (xssfSheet == null) {
                continue;
            }
            //当前sheet的json文件
            JSONObject sheetJson = new JSONObject();
            //当前sheet的array,作为sheetJson 的value值
            JSONArray sheetArr = new JSONArray();
            //sheet的第一行,获取作为json的key值
            JSONArray key = new JSONArray();
            int xssfLastRowNum = xssfSheet.getLastRowNum();
            // 循环行Row
            for (int rowNum = 0; rowNum <= xssfLastRowNum; rowNum++) {
                XSSFRow xssfRow = xssfSheet.getRow(rowNum);
                if (xssfRow == null) {
                    continue;
                }

                // 循环列Cell,在这里组合json文件
                int firstCellNum = xssfRow.getFirstCellNum();
                int lastCellNum = xssfRow.getLastCellNum();
                JSONObject rowJson = new JSONObject();
                for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
                    XSSFCell cell = null;
                    try {
                        cell = xssfRow.getCell(cellNum);
                        if (cell == null) {
                            rowJson.put(key.getString(cellNum), "");
                            continue;
                        }
                        if (rowNum == 0)
                            key.add(toString(cell));
                        else {
                            //若是列号超过了key的大小,则跳过
                            if (cellNum >= key.size()) continue;
                            rowJson.put(key.getString(cellNum), toString(cell));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (rowJson.keySet().size() > 0)
                    sheetArr.add(rowJson);
            }
            sheetJson.put(sheetName, shuffleData(sheetArr));
            dataArray.add(sheetJson);
        }
    }
chase's avatar
chase committed
397
}