|
@@ -0,0 +1,176 @@
|
|
|
+package cn.com.ctop.toutiao.modules.report.service.impl;
|
|
|
+
|
|
|
+import cn.com.ctop.common.module.utils.CtopAdConstant;
|
|
|
+import cn.com.ctop.toutiao.modules.report.mapper.BytedanceAccountReportMapper;
|
|
|
+import cn.com.ctop.toutiao.modules.report.service.IBytedanceAccountReportService;
|
|
|
+import cn.com.ctop.toutiao.modules.utils.LinkUtils;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.jeecg.common.constant.SystemDateConstant;
|
|
|
+import org.jeecg.common.util.DateUtils;
|
|
|
+import org.jeecg.common.util.JsonResourceUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by JQ.bi on 2020.06.05
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class BytedanceAccountReportServiceImpl implements IBytedanceAccountReportService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ BytedanceAccountReportMapper bytedanceAccountReportMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getSumDataBy(String mediaId, BigDecimal discount, JSONArray accounts, String startDate, String endDate) {
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ JSONObject after;
|
|
|
+ JSONObject before;
|
|
|
+ if (startDate.equals(endDate)) {
|
|
|
+ after = bytedanceAccountReportMapper.queryTodaySumReportBy(startDate, accounts);
|
|
|
+ if (after == null) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ before = bytedanceAccountReportMapper.queryTodaySumByHour(DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, startDate, -1), after.getInteger("maxHour"), accounts);
|
|
|
+ } else {
|
|
|
+ after = bytedanceAccountReportMapper.querySumByStartEndDate(endDate, startDate, accounts);
|
|
|
+ if(after==null){
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ before = after;
|
|
|
+ }
|
|
|
+ result.put("Sum", getSumLink(mediaId, discount, after, before));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<JSONObject> getChartDataBy(String mediaId, BigDecimal discount, JSONArray accounts, String startDate, String endDate) {
|
|
|
+ List<JSONObject> result=new ArrayList<>();
|
|
|
+ String filedAll = JsonResourceUtil.joinAllFiled();
|
|
|
+ if (startDate.equals(endDate)) {
|
|
|
+ List<JSONObject> after = bytedanceAccountReportMapper.queryTodayDetailReportBy(filedAll, startDate, accounts);
|
|
|
+ if(after==null){
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ List<JSONObject> afterDis = this.countDiscountCost(mediaId, discount, after);
|
|
|
+ List<JSONObject> before = bytedanceAccountReportMapper.queryTodayDetailReportBy(filedAll, DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, startDate, -1), accounts);
|
|
|
+ List<JSONObject> beforeDis = this.countDiscountCost(mediaId, discount, before);
|
|
|
+ result = LinkUtils.getCompareDate(beforeDis, afterDis, "statHour");
|
|
|
+ } else {
|
|
|
+ List<JSONObject> after;
|
|
|
+ //判断是不是日期间隔大于半年
|
|
|
+ if(DateUtils.isMoreSixMonth(startDate,endDate)){
|
|
|
+ after = bytedanceAccountReportMapper.queryDetailGroupMonthByDate(endDate, startDate, accounts);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ after = bytedanceAccountReportMapper.queryDetailByStartEndDate(endDate, startDate, accounts);
|
|
|
+ }
|
|
|
+ if(after==null){
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ result = this.countDiscountCost(mediaId, discount, after);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<JSONObject> getListDataBy(String mediaId, BigDecimal discount, JSONArray accounts, String startDate, String endDate, int hour, String target, String order) {
|
|
|
+ List<JSONObject> result;
|
|
|
+ String filedAll = JsonResourceUtil.joinAllFiled();
|
|
|
+ if (startDate.equals(endDate)) {
|
|
|
+ result = bytedanceAccountReportMapper.queryTodaySumByHourGroupAccount(filedAll, startDate, hour, accounts, target, order);
|
|
|
+ JSONObject afterSum = bytedanceAccountReportMapper.querySumByHour(filedAll, startDate, hour, accounts);
|
|
|
+ JSONObject beforeSum = bytedanceAccountReportMapper.querySumByHour(filedAll, DateUtils.getAnotherDay(SystemDateConstant.yyyy_MM_dd, startDate, -1), hour, accounts);
|
|
|
+ result.add(afterSum);
|
|
|
+ result.add(beforeSum);
|
|
|
+ result.add(countTotal(afterSum, beforeSum));
|
|
|
+ } else {
|
|
|
+ result = bytedanceAccountReportMapper.querySumByDateGroupAccount(filedAll, endDate, startDate, accounts, target, order);
|
|
|
+ result.add(bytedanceAccountReportMapper.queryAllSumByStartEndDate(filedAll, endDate, startDate, accounts));
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<JSONObject> getExcelDataBy(String filed, String mediaId, JSONArray accounts, String startDate, String endDate, int hour, String target, String order) {
|
|
|
+ List<JSONObject> result;
|
|
|
+ if (startDate.equals(endDate)) {
|
|
|
+ result = bytedanceAccountReportMapper.queryTodaySumByHourGroupAccount(filed, startDate, hour, accounts, target, order);
|
|
|
+ } else {
|
|
|
+ result = bytedanceAccountReportMapper.querySumByDateGroupAccount(filed, endDate, startDate, accounts, target, order);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算汇总数据环比
|
|
|
+ private Map<String, Object> getSumLink(String mediaId, BigDecimal discount, JSONObject after, JSONObject before) {
|
|
|
+
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+
|
|
|
+ JSONObject costLink = new JSONObject();
|
|
|
+ JSONObject discountCostLink = new JSONObject();
|
|
|
+ JSONObject clickLink = new JSONObject();
|
|
|
+ JSONObject showNumLink = new JSONObject();
|
|
|
+ JSONObject convertNumLink = new JSONObject();
|
|
|
+
|
|
|
+ costLink.put("cost", after.getBigDecimal("cost"));
|
|
|
+ costLink.put("CostSumLink", LinkUtils.countLink(after.getBigDecimal("cost"), before.getBigDecimal("cost")));
|
|
|
+ if (mediaId.equals(CtopAdConstant.PLATFORM_TYPE_BYTEDANCE)) {
|
|
|
+ discountCostLink.put("discountCost", after.getBigDecimal("cost").divide(discount, 4, RoundingMode.HALF_UP));
|
|
|
+ discountCostLink.put("discountCostLink", LinkUtils.countLink(after.getBigDecimal("cost").divide(discount, 4, RoundingMode.HALF_UP), before.getBigDecimal("cost").divide(discount, 4, RoundingMode.HALF_UP)));
|
|
|
+ } else {
|
|
|
+ discountCostLink.put("discountCost", after.getBigDecimal("cost").multiply(discount));
|
|
|
+ discountCostLink.put("discountCostLink", LinkUtils.countLink(after.getBigDecimal("cost").multiply(discount), before.getBigDecimal("cost").multiply(discount)));
|
|
|
+ }
|
|
|
+ clickLink.put("click", after.getBigDecimal("click"));
|
|
|
+ clickLink.put("clickLink", LinkUtils.countLink(after.getBigDecimal("click"), before.getBigDecimal("click")));
|
|
|
+ showNumLink.put("showNum", after.getBigDecimal("showNum"));
|
|
|
+ showNumLink.put("showNumLink", LinkUtils.countLink(after.getBigDecimal("showNum"), before.getBigDecimal("showNum")));
|
|
|
+ convertNumLink.put("convertNum", after.getBigDecimal("convertNum"));
|
|
|
+ convertNumLink.put("convertNumLink", LinkUtils.countLink(after.getBigDecimal("convertNum"), before.getBigDecimal("convertNum")));
|
|
|
+ result.put("costLink", costLink);
|
|
|
+ result.put("discountCostLink", discountCostLink);
|
|
|
+ result.put("clickLink", clickLink);
|
|
|
+ result.put("showNumLink", showNumLink);
|
|
|
+ result.put("convertNumLink", convertNumLink);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算折后消耗
|
|
|
+ private List<JSONObject> countDiscountCost(String mediaId, BigDecimal discount, List<JSONObject> data) {
|
|
|
+ if (data.isEmpty()) {
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+ if (mediaId.equals(CtopAdConstant.PLATFORM_TYPE_BYTEDANCE)) {
|
|
|
+ data.forEach(it -> {
|
|
|
+ it.put("discountCost", it.getBigDecimal("cost").divide(discount, 4, RoundingMode.HALF_UP));
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ data.forEach(it -> {
|
|
|
+ it.put("discountCost", it.getBigDecimal("cost").multiply(discount));
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算总计环比
|
|
|
+ private JSONObject countTotal(JSONObject after, JSONObject before) {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ after.forEach((k, v) -> {
|
|
|
+ //去除没法计算的字段
|
|
|
+ if (k.equals("accountId") || k.equals("authName") || v.toString().contains("%")) {
|
|
|
+ jsonObject.put(k, "-");
|
|
|
+ } else {
|
|
|
+ jsonObject.put(k, LinkUtils.countLink(after.getBigDecimal(k), before.getBigDecimal(k)));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+}
|