futian.liu
2023-12-02 8e41786892a4bd7cff2d63bde8cb0636cdb0650c
consum-base/src/main/java/com/consum/base/controller/LWhFormOutputController.java
@@ -7,7 +7,9 @@
import com.consum.base.core.utils.CommonUtil;
import com.consum.base.core.utils.MapUtils;
import com.consum.base.core.utils.MapperUtil;
import com.consum.base.core.utils.PageUtil;
import com.consum.base.pojo.LWhFormOutputParam;
import com.consum.base.pojo.excel.OutputExcelTemplate;
import com.consum.base.pojo.query.LWhFormOutputQry;
import com.consum.base.pojo.response.FormOutputGoodsVO;
import com.consum.base.pojo.response.FormOutputTemplateInfoVO;
@@ -16,7 +18,6 @@
import com.consum.base.pojo.response.LWhFormOutputExtendVO;
import com.consum.base.service.LWarehouseFlowService;
import com.consum.base.service.LWhFormOutputServiceImpl;
import com.consum.base.service.LWhGoodsService;
import com.consum.base.service.LWhProcureModelService;
import com.consum.model.po.FinSysTenantUser;
import com.consum.model.po.LWhFormOutput;
@@ -29,21 +30,20 @@
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -63,8 +63,6 @@
    private LWhFormOutputServiceImpl lWhFormOutputService;
    @Autowired
    private LWhProcureModelService lWhProcureModelService;
    @Autowired
    private LWhGoodsService lWhGoodsService;
    @Resource
    private LWarehouseFlowService lWarehouseFlowService;
@@ -162,6 +160,7 @@
        LWhFormOutput lWhFormOutput = lWhFormOutputService.get(new LWhFormOutput(id));
        LWhFormOutputExtendVO lWhFormOutputExtendVO = new LWhFormOutputExtendVO();
        BeanUtils.copyProperties(lWhFormOutput, lWhFormOutputExtendVO);
        lWhFormOutputExtendVO.setDoc(lWhFormOutput.getOutputDoc());
        String sql = "SELECT bgt.id, CATEGORY_ID, CATEGORY_NAME, GOODS_NAME "
            + "FROM l_wh_procure_model pm LEFT JOIN base_goods_models bgm ON bgm.id = pm.BASE_GOODS_MODELS_ID "
@@ -203,76 +202,52 @@
        if (currentUser == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
        GenericPager<Map<String, Object>> genericPager = lWarehouseFlowService.queryAllBusinessFlow(formOutputQry);
        PageUtil genericPager = lWarehouseFlowService.queryAllBusinessFlow(formOutputQry);
        return ResponseValue.success(genericPager);
    }
    @ApiOperation(value = "出库单导出", notes = "出库单导出")
    @ApiImplicitParam(name = "id", value = "出库单id", required = true, dataType = "Long", paramType = "query")
    @GetMapping("/list/export")
    public void export(Long id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        TemplateExportParams params = new TemplateExportParams(
            "import/采购退货单.xls");
    public void export(Long id, HttpServletResponse response) throws Exception {
        FinSysTenantUser sysInfo = this.getSysInfo();
        String userName = sysInfo.getUserName();
        if (sysInfo == null) {
            throw new RuntimeException("登录用户信息不存在");
        }
        ClassPathResource classPathResource = new ClassPathResource("import/出库单.xls");
        // 获取文件输入流
        InputStream inputStream = classPathResource.getInputStream();
        Workbook wb = new HSSFWorkbook(inputStream);
        TemplateExportParams params = new TemplateExportParams();
        params.setTemplateWb(wb);
        params.setHeadingStartRow(2);
        String sql = "SELECT\n"
            + "\tfo.BUSINESS_FORM_CODE,\n"
            + "\tfo.AGENCY_NAME tenantName,\n"
            + "\tGOODS_NAME goodsName,\n"
            + "\tpm.BASE_GOODS_MODELS_NAME goodsModeName,\n"
            + "\tcounts num,\n"
            + "\ttotal_amount sqje,\n"
            + "\tBEIZ remark\n"
            + "FROM\n"
            + "\tl_wh_procure_model pm\n"
            + "\tLEFT JOIN base_goods_models bgm ON pm.BASE_GOODS_MODELS_ID = bgm.id\n"
            + "\tLEFT JOIN base_goods_template bgt ON bgm.GOODS_TEMPLATES_ID = bgt.id\n"
            + "\tLEFT JOIN l_wh_form_output fo ON fo.ID = pm.BUSINESS_ID\n"
            + "WHERE\n"
            + "\tpm.BUSINESS_ID = 1393373943914496";
        List<OutputExcelTemplate> exportList = lWhFormOutputService.getExportList(id);
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        List<Map<String, Object>> select = lWhFormOutputService.select(sql, param, new MapperUtil());
        if (CollectionUtils.isEmpty(exportList)) {
            throw new RuntimeException("数据为空");
        }
        int countNum = exportList.stream().filter(item -> item.getNum() != null).mapToInt(OutputExcelTemplate::getNum).sum();
        int totalAmount = exportList.stream().filter(item -> item.getTotalAmount() != null).mapToInt(OutputExcelTemplate::getTotalAmount).sum();
        Optional<OutputExcelTemplate> first = exportList.stream().findFirst();
        OutputExcelTemplate templateExcelExport = first.get();
        String businessFormCode = templateExcelExport.getBusinessFormCode();
        Long createTime = templateExcelExport.getCreateTime();
        String operatorName = templateExcelExport.getOperatorName();
        String dateForHuman = DateUtils.getDateForHuman(System.currentTimeMillis());
        Map<String, Object> map = new HashMap<>();
        //获取select中的BUSINESS_FORM_CODE值,
        for (Map<String, Object> item : select) {
            String businessFormCode = (String) item.get("businessFormCode");
            map.put("code", businessFormCode);
            Object num = item.get("num");
            map.put("numCount", 100);
            map.put("amountCount", 100);
        }
        List<Map<String, String>> listMap = new ArrayList<>();
        map.put("code", "贰佰万");
        map.put("date", dateForHuman);
        map.put("name", userName);
//        for (int i = 0; i < 4; i++) {
//            Map<String, String> lm = new HashMap<>();
//            lm.put("tenantName", i + 1 + "");
//            lm.put("goodsName", "A001");
//            lm.put("goodsModeName", "设计");
//            lm.put("num", "EasyPoi " + i + "期");
//            lm.put("price", "开源项目");
//            lm.put("sqje", i * 10000 + "");
//            lm.put("remark", i * 10000 + "");
//            listMap.add(lm);
//        }
        map.put("maplist", select);
        map.put("code", businessFormCode);
        map.put("date", DateUtils.toShowDate(createTime));
        map.put("name", operatorName);
        map.put("countNum", countNum);
        map.put("totalAmount", totalAmount);
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        Workbook workbook = ExcelExportUtil.exportExcel(params, OutputExcelTemplate.class, exportList, map);
        downLoadExcel("出库单", response, workbook);
        try (OutputStream outputStream = response.getOutputStream()) {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("采购退货单.xls", "utf-8"));
            workbook.write(outputStream);
            workbook.close();
        }
    }
}