|
@@ -0,0 +1,115 @@
|
|
|
+package com.malk.zhixingtongde.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
+import cn.hutool.core.util.ReflectUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.malk.server.aliwork.YDConf;
|
|
|
+import com.malk.server.aliwork.YDParam;
|
|
|
+import com.malk.service.aliwork.YDClient;
|
|
|
+import com.malk.utils.UtilEnv;
|
|
|
+import com.malk.utils.UtilExcel;
|
|
|
+import com.malk.utils.UtilMap;
|
|
|
+import com.malk.zhixingtongde.entity.Profit;
|
|
|
+import com.malk.zhixingtongde.service.ZxtdReportService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class ZxtdReportServiceImpl implements ZxtdReportService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private YDClient ydClient;
|
|
|
+
|
|
|
+ private static String[] PROFIT_CLASSIFYS={"收入","销售费用","管理费用","研发费用","财务费用"};
|
|
|
+ private static int[] PROFIT_NUMBERS={15,25,25,25,10};
|
|
|
+
|
|
|
+ String _matchFormUuid(String code) {
|
|
|
+ Map<String, String> formUuid = UtilMap.empty();
|
|
|
+ if (UtilEnv.getActiveProfile().equals(UtilEnv.ENV_PROD)) {
|
|
|
+ formUuid.put("PROFIT", "FORM-32937639BC314882AF45201131EAE4C0YD2L"); // 利润表
|
|
|
+ formUuid.put("DOMAIN", "https://nxcyz4.aliwork.com/"); // 宜搭域名
|
|
|
+ } else {
|
|
|
+ formUuid.put("DOMAIN", "https://nxcyz4.aliwork.com/");
|
|
|
+ }
|
|
|
+ return formUuid.get(code);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void exportProfit(HttpServletResponse response,String year) {
|
|
|
+ // 1.获取宜搭数据
|
|
|
+ List<Profit> dataList=getData(year);
|
|
|
+ UtilExcel.exportListByTemplate(response,dataList, Profit.class, "利润", "Template_profit.xlsx");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按月获取利润表数据
|
|
|
+ private List<Profit> getData(String year){
|
|
|
+ // 查询宜搭底表数据
|
|
|
+ List<Map> list = (List<Map>) ydClient.queryData(YDParam.builder().formUuid(_matchFormUuid("PROFIT")).searchCondition(
|
|
|
+ JSONObject.toJSONString(UtilMap.map("textField_lqvp89br",year))
|
|
|
+ ).build(), YDConf.FORM_QUERY.retrieve_list).getData();
|
|
|
+ Map<String,Map<String,Profit>> dataMap=new HashMap<>();
|
|
|
+ // 数据预处理(金额数据累加,根据 类型,科目,年月)
|
|
|
+ for (Map map:list){
|
|
|
+ JSONObject formData=JSONObject.parseObject(String.valueOf(map.get("formData")));
|
|
|
+ String classify=formData.getString("textField_lqvp89bt");// 类型
|
|
|
+ String subject=formData.getString("textField_lqvp89bu");// 科目
|
|
|
+ String amt=formData.getString("numberField_lqqhf8yn");// 金额
|
|
|
+ String date=formData.getString("textField_lqvp89br");// 年月 2024-01
|
|
|
+ String month=date.split("-")[1].replaceFirst("^0+(?!$)", "");// 月份(去除前缀0)
|
|
|
+ // 数据累加
|
|
|
+ if(dataMap.containsKey(classify)){
|
|
|
+ Map<String,Profit> classifyMap=dataMap.get(classify);
|
|
|
+ if(classifyMap.containsKey(subject)){
|
|
|
+ Profit profit=classifyMap.get(subject);
|
|
|
+ // 获取已有数据
|
|
|
+ String amtTemp=ReflectUtil.invoke(profit,"getAmt"+month);
|
|
|
+ ReflectUtil.invoke(profit, "setAmt"+month, NumberUtil.add(amt,amtTemp).toString());
|
|
|
+ dataMap.get(classify).put(subject, profit);
|
|
|
+ }else{
|
|
|
+ Profit profit=new Profit(subject);
|
|
|
+ ReflectUtil.invoke(profit, "setAmt"+month, amt);
|
|
|
+ dataMap.get(classify).put(subject,profit);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ Profit profit=new Profit(subject);
|
|
|
+ ReflectUtil.invoke(profit, "setAmt"+month, amt);
|
|
|
+ Map<String,Profit> classifyMap=new HashMap<>();
|
|
|
+ classifyMap.put(subject,profit);
|
|
|
+ dataMap.put(classify,classifyMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<Profit> profitList=new ArrayList<>();
|
|
|
+ Profit nullProfit=new Profit();
|
|
|
+ // 根据 类型 处理数据
|
|
|
+ for (int i = 0; i < PROFIT_CLASSIFYS.length; i++) {
|
|
|
+ List<Profit> list1=new ArrayList<>();
|
|
|
+ Profit totalProfit=new Profit(PROFIT_CLASSIFYS[i]+(i==0?"合计":"小计"));
|
|
|
+ if(dataMap.containsKey(PROFIT_CLASSIFYS[i])){
|
|
|
+ for (String str:dataMap.get(PROFIT_CLASSIFYS[i]).keySet()){
|
|
|
+ list1.add(dataMap.get(PROFIT_CLASSIFYS[i]).get(str).buildTotal());
|
|
|
+ for (int j = 1; j < 13; j++) {
|
|
|
+ String amtTemp=ReflectUtil.invoke(dataMap.get(PROFIT_CLASSIFYS[i]).get(str),"getAmt"+j);
|
|
|
+ String amt=ReflectUtil.invoke(totalProfit,"getAmt"+j);
|
|
|
+ ReflectUtil.invoke(totalProfit, "setAmt"+j, NumberUtil.add(amt,amtTemp).toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 补充空数据
|
|
|
+ for (int j = list1.size(); j < PROFIT_NUMBERS[i]; j++) {
|
|
|
+ list1.add(nullProfit);
|
|
|
+ }
|
|
|
+ // 添加小计
|
|
|
+ list1.add(totalProfit.buildTotal());
|
|
|
+ profitList.addAll(list1);
|
|
|
+ }
|
|
|
+ return profitList;
|
|
|
+ }
|
|
|
+}
|