/** * Copyright 2018 人人开源 https://www.renren.io *

* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. *

* This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. *

* You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package com.epmet.controller; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; import cn.afterturn.easypoi.excel.export.ExcelExportService; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.epmet.commons.tools.annotation.LoginUser; import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.commons.tools.utils.ExcelUtils; import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.AssertUtils; import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.commons.tools.validator.group.AddGroup; import com.epmet.commons.tools.validator.group.DefaultGroup; import com.epmet.commons.tools.validator.group.UpdateGroup; import com.epmet.dto.IcResiUserDTO; import com.epmet.dto.form.CustomerFormQueryDTO; import com.epmet.dto.form.IcResiDetailFormDTO; import com.epmet.dto.form.IcResiUserFormDTO; import com.epmet.dto.form.IcResiUserPageFormDTO; import com.epmet.dto.result.CustomerFormResultDTO; import com.epmet.dto.result.FormGroupDTO; import com.epmet.dto.result.FormItem; import com.epmet.excel.IcResiUserExcel; import com.epmet.feign.OperCustomizeOpenFeignClient; import com.epmet.service.IcResiUserService; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Name; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.FileOutputStream; import java.util.*; /** * 用户基础信息 * * @author generator generator@elink-cn.com * @since v1.0.0 2021-10-26 */ @RestController @RequestMapping("icresiuser") public class IcResiUserController { private static final String BASE_TABLE_NAME = "ic_resi_user"; @Autowired private OperCustomizeOpenFeignClient operCustomizeOpenFeignClient; @Autowired private IcResiUserService icResiUserService; @GetMapping("page") public Result> page(@RequestParam Map params){ PageData page = icResiUserService.page(params); return new Result>().ok(page); } @GetMapping("{id}") public Result get(@PathVariable("id") String id){ IcResiUserDTO data = icResiUserService.get(id); return new Result().ok(data); } @PostMapping public Result save(@RequestBody IcResiUserDTO dto){ //效验数据 ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class); icResiUserService.save(dto); return new Result(); } @PutMapping public Result update(@RequestBody IcResiUserDTO dto){ //效验数据 ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class); icResiUserService.update(dto); return new Result(); } @DeleteMapping public Result delete(@RequestBody String[] ids){ //效验数据 AssertUtils.isArrayEmpty(ids, "id"); icResiUserService.delete(ids); return new Result(); } @GetMapping("export") public void export(@RequestParam Map params, HttpServletResponse response) throws Exception { List list = icResiUserService.list(params); ExcelUtils.exportExcelToTarget(response, null, list, IcResiUserExcel.class); } /** * @Author sun * @Description 党建互联平台--保存居民信息 **/ @PostMapping("add") public Result add(@LoginUser TokenDto tokenDto, @RequestBody List formDTO) { icResiUserService.add(tokenDto, formDTO); return new Result(); } /** * @Author sun * @Description 党建互联平台--修改居民信息 **/ @PostMapping("edit") public Result edit(@LoginUser TokenDto tokenDto, @RequestBody List formDTO) { icResiUserService.edit(tokenDto, formDTO); return new Result(); } @GetMapping("download/template") public void downloadTemplate(@RequestParam String customerId) throws Exception { CustomerFormQueryDTO queryDTO = new CustomerFormQueryDTO(); queryDTO.setFormCode("resi_base_info"); queryDTO.setCustomerId(customerId); Result resultForm = operCustomizeOpenFeignClient.getCustomerForm(queryDTO); if (resultForm == null || !resultForm.success() ||resultForm.getData() == null){ throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode()); } System.out.println(JSON.toJSONString(resultForm.getData())); XSSFWorkbook workbook = new XSSFWorkbook(); Map> sheetHeaderMap = buildHeaderByItem(resultForm); //Workbook workbook = null; for (Map.Entry> entry : sheetHeaderMap.entrySet()) { String sheetName = entry.getKey(); List headers = entry.getValue(); ExportParams exportParams = new ExportParams(); exportParams.setSheetName(sheetName); exportParams.setAutoSize(true); new ExcelExportService().createSheetForMap(workbook, exportParams, headers,new ArrayList<>()); } List allNames = workbook.getAllNames(); System.out.println("======"+allNames); FileOutputStream fos = new FileOutputStream("//Users/liujianjun/Downloads/基础信息表/Dow.tt.xls"); workbook.write(fos); fos.close(); } @NotNull private Map> buildHeaderByItem(Result resultForm) { //form中的itemlist 为一级表头 但是要排除每个item中含有childGroup的 List itemList = resultForm.getData().getItemList(); List groupList = resultForm.getData().getGroupList(); Map> everySheetHeaderMap = new LinkedHashMap<>(); List firstSheetHeaderList = new ArrayList<>(); itemList.forEach(item->{ if (StringUtils.isBlank(item.getColumnName())){ return; } ExcelExportEntity header = new ExcelExportEntity(item.getLabel(),item.getColumnName().concat(String.valueOf(item.getColumnNum())),30); header.setNeedMerge(true); if (item.getChildGroup() == null){ System.out.println(item.getLabel()); firstSheetHeaderList.add(header); return; } everySheetHeaderMap.putIfAbsent(resultForm.getData().getFormName(),firstSheetHeaderList); //这些是动态的 formGroup if (item.getChildGroup() != null){ //baseTableName单独的一个sheet System.out.println("childGroup:"+item.getLabel()); if (BASE_TABLE_NAME.equals(item.getTableName())){ header = new ExcelExportEntity(item.getChildGroup().getLabel(),item.getChildGroup().getTableName()); header.setNeedMerge(true); List otherSheetHeaderList = new ArrayList<>(); List secondHeaderList = new ArrayList<>(); //这里是设置除基础信息之外的sheet的表头 item.getChildGroup().getItemList().forEach(item2->{ if (!BASE_TABLE_NAME.equals(item2.getTableName())){ everySheetHeaderMap.putIfAbsent(item.getLabel(),otherSheetHeaderList); } ExcelExportEntity secondHeader = new ExcelExportEntity(item2.getLabel(),item2.getColumnName().concat(String.valueOf(item2.getColumnNum()))); secondHeader.setNeedMerge(true); secondHeaderList.add(secondHeader); if (!item2.getItemType().equals("radio") && com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(item2.getOptions())){ List thirdHeaderList = new ArrayList<>(); item2.getOptions().forEach(child->{ ExcelExportEntity thirdHeader = new ExcelExportEntity(child.getLabel()); thirdHeaderList.add(thirdHeader); }); secondHeader.setList(thirdHeaderList); } }); header.setList(secondHeaderList); otherSheetHeaderList.add(header); } } }); //List firstSheetHeaderList = new ArrayList<>(); groupList.forEach(item->{ /* if (!"兴趣爱好".equals(item.getLabel()) && !"宗教信仰".equals(item.getLabel())){ return; }*/ if (!BASE_TABLE_NAME.equals(item.getTableName())){ System.out.println(item.getLabel()+"--"+item.getTableName()); return; } ExcelExportEntity header = new ExcelExportEntity(item.getLabel(),item.getTableName()); header.setNeedMerge(true); List secondHeaderList = new ArrayList<>(); item.getItemList().forEach(item2->{ ExcelExportEntity secondHeader = new ExcelExportEntity(item2.getLabel(),item2.getColumnName().concat(String.valueOf(item2.getColumnNum()))); secondHeader.setNeedMerge(true); secondHeaderList.add(secondHeader); if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(item2.getOptions())){ List thirdHeaderList = new ArrayList<>(); item2.getOptions().forEach(child->{ ExcelExportEntity thirdHeader = new ExcelExportEntity(child.getLabel(),child.getValue()+ new Random(1).nextInt(2)); thirdHeader.setNeedMerge(true); thirdHeaderList.add(thirdHeader); }); secondHeader.setList(thirdHeaderList); } }); header.setList(secondHeaderList); firstSheetHeaderList.add(header); System.out.println(JSON.toJSONString(firstSheetHeaderList)); }); return everySheetHeaderMap; } private void buildHeader(Map> everySheetHeaderMap, FormItem item, ExcelExportEntity header) { List firstSheetHeaderList = new ArrayList<>(); List secondHeaderList = new ArrayList<>(); item.getChildGroup().getItemList().forEach(item2->{ if (!BASE_TABLE_NAME.equals(item2.getTableName())){ everySheetHeaderMap.putIfAbsent(item.getLabel(),firstSheetHeaderList); } ExcelExportEntity secondHeader = new ExcelExportEntity(item2.getLabel(),item2.getColumnName().concat(String.valueOf(item2.getColumnNum()))); secondHeader.setNeedMerge(true); secondHeaderList.add(secondHeader); if (CollectionUtils.isNotEmpty(item2.getOptions())){ List thirdHeaderList = new ArrayList<>(); item2.getOptions().forEach(child->{ ExcelExportEntity thirdHeader = new ExcelExportEntity(child.getLabel()); thirdHeaderList.add(thirdHeader); }); secondHeader.setList(thirdHeaderList); } }); header.setList(secondHeaderList); firstSheetHeaderList.add(header); everySheetHeaderMap.putIfAbsent(item.getLabel(),firstSheetHeaderList); } @PostMapping("listresi") public Result>> queryListResi1(@LoginUser TokenDto tokenDto, @RequestBody IcResiUserPageFormDTO pageFormDTO){ //pageFormDTO.setCustomerId("45687aa479955f9d06204d415238f7cc"); pageFormDTO.setCustomerId(tokenDto.getCustomerId()); ValidatorUtils.validateEntity(pageFormDTO,IcResiUserPageFormDTO.AddUserInternalGroup.class); return new Result>>().ok(icResiUserService.pageResiMap(pageFormDTO)); } /** * 编辑页面,显示居民信息详情 * * @param pageFormDTO * @return com.epmet.commons.tools.utils.Result * @author yinzuomei * @date 2021/10/28 10:29 上午 */ @PostMapping("detail") public Result queryIcResiDetail(@LoginUser TokenDto tokenDto,@RequestBody IcResiDetailFormDTO pageFormDTO){ //pageFormDTO.setCustomerId("45687aa479955f9d06204d415238f7cc"); pageFormDTO.setCustomerId(tokenDto.getCustomerId()); ValidatorUtils.validateEntity(pageFormDTO,IcResiDetailFormDTO.AddUserInternalGroup.class); return new Result().ok(icResiUserService.queryIcResiDetail(pageFormDTO)); } }