yangzian 4 年 前
コミット
ac229e5c55

+ 22 - 0
jeecg-boot-bytedance/src/main/java/org/jeecg/modules/bytedance/report/entity/vo/ReportCostVo.java

@@ -29,4 +29,26 @@ public class ReportCostVo implements Serializable {
     private String time;
 
 
+    //计划id
+    private String adId;
+    //计划名称
+    private String adName;
+    //出价
+    private BigDecimal cpaBid;
+    //平均千次展显费用 = 总花费/展示数 * 1000
+    private String averageShow;
+    //平均点击单价 = 花费 / 点击数
+    private String averageClick;
+    //点击率 = 展示 / 点击数
+    private String clickRate;
+    //转化成本 = 总花费 / 转化数
+    private String conver;
+    //转化率 = 转化数 / 点击数 * 100
+    private String converRate;
+    //环比
+    private String compare;
+
+    private String updateTime;
+
+
 }

+ 7 - 0
jeecg-boot-bytedance/src/main/java/org/jeecg/modules/bytedance/report/mapper/BytedanceAdvertiserHourlyReportMapper.java

@@ -8,6 +8,7 @@ import org.jeecg.modules.bytedance.report.entity.vo.ReportCostVo;
 
 import java.math.BigDecimal;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @Description: 广告主小时级别报表信息
@@ -21,4 +22,10 @@ public interface BytedanceAdvertiserHourlyReportMapper extends BaseMapper<Byteda
     List<ReportCostVo> getReportCostInfo(@Param("accountId") String accountId, @Param("startTime") String startTime, @Param("endTime") String endTime);
     List<ReportCostVo> getReportCostInfoStage(@Param("accountId") String accountId, @Param("startTime") String startTime, @Param("endTime") String endTime);
 
+    Map<String,Object> selAcountIdCostMax(@Param("userId") String userId, @Param("type") String type);
+
+    Map<String,Object> selectAccountCost(@Param("accountId") String accountId);
+
+    List<ReportCostVo> selectReportPlanCostInfo(@Param("accountId") String accountId, @Param("planId") String planId, @Param("startTime") String startTime, @Param("endTime") String endTime);
+
 }

+ 70 - 0
jeecg-boot-bytedance/src/main/java/org/jeecg/modules/bytedance/report/mapper/xml/BytedanceAdvertiserHourlyReportMapper.xml

@@ -51,9 +51,77 @@
             stat_datetime
         ORDER BY
             stat_datetime
+    </select>
+
+<!-- 查询用户下 花费最高的 账户-->
+    <select id="selAcountIdCostMax" resultType="java.util.HashMap">
+        SELECT
+            cah.advertiser_id AS accountId,
+            SUM( cah.cost ) cost,
+            cua.auth_name AS authName
+        FROM
+            ctop_bytedance_report_advertiser_hourly cah
+        LEFT JOIN ctop_user_allocation cua ON cah.advertiser_id = cua.account_id
+        WHERE
+            1 = 1
+        <if test="type != null and type !=''  and type !='bytedance'.toString()">
+              AND ( cua.media_id = '1' OR cua.media_id = '3' )
+        </if>
+        <if test="userId !=null and userId != ''">
+              AND cua.user_id = #{userId}
+        </if>
+        GROUP BY
+            cah.advertiser_id
+        ORDER BY
+            cost DESC
+        LIMIT 1
+    </select>
 
+<!-- 查询账户花费-->
+    <select id="selectAccountCost" resultType="java.util.HashMap">
+        SELECT
+            cua.account_status AS accountStatus,
+            cua.auth_name AS authName,
+            rda.cost AS cost,
+            rda.valid_balance AS validBalance,
+            cua.account_id AS accountId
+        FROM
+            ctop_user_allocation cua
+                LEFT JOIN ctop_rule_data_account rda ON cua.account_id = rda.account_id
+        WHERE
+            1 = 1
+          AND cua.account_id = #{accountId}
+    </select>
 
 
+<!-- 查询 计划时报 数据-->
+    <select id="selectReportPlanCostInfo" resultType="org.jeecg.modules.bytedance.report.entity.vo.ReportCostVo">
+        SELECT
+        stat_datetime,
+        sum(show_num) AS showNum,
+        sum(click) AS click,
+        sum(convert_num) AS convertNum,
+        sum(cost) AS cost,
+        ad_id,
+        ad_name,
+        update_time
+        FROM
+        ctop_bytedance_report_plan_hourly
+        where 1 =1
+        <if test="accountId !=null and accountId != ''">
+            and advertiser_id = #{accountId}
+        </if>
+        <if test="planId !=null and planId != ''">
+            and ad_id = #{planId}
+        </if>
+        <if test="startTime !=null and startTime != ''">
+            and stat_datetime &gt;= #{startTime}
+        </if>
+        <if test="endTime !=null and endTime != ''">
+            and stat_datetime &lt;= #{endTime}
+        </if>
+        GROUP BY ad_id
+        ORDER BY cost DESC
 
     </select>
 
@@ -62,4 +130,6 @@
 
 
 
+
+
 </mapper>

+ 6 - 0
jeecg-boot-bytedance/src/main/java/org/jeecg/modules/bytedance/report/service/IBytedanceAdvertiserHourlyReportService.java

@@ -16,6 +16,12 @@ public interface IBytedanceAdvertiserHourlyReportService extends IService<Byteda
 
     Result getReportYearOnYearCost(String accountId, String startTime, String endTime)throws Exception;
 
+    Result initAccountIdAndTime(String userId,String type)throws Exception;
+
+    Result getAccountCost(String accountId)throws Exception;
+
+    Result getReportPlanCost(String accountId, String startTime, String endTime,Integer pageNum,Integer pageSize)throws Exception;
+
 
 
 }

+ 134 - 5
jeecg-boot-bytedance/src/main/java/org/jeecg/modules/bytedance/report/service/impl/BytedanceAdvertiserHourlyReportServiceImpl.java

@@ -1,11 +1,16 @@
 package org.jeecg.modules.bytedance.report.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.modules.bytedance.advertise.entity.AiBytedanceAdvertiserStrategy;
+import org.jeecg.modules.bytedance.advertise.entity.ByteDanceAdvertisePlan;
 import org.jeecg.modules.bytedance.advertise.entity.RuleDataAccount;
 import org.jeecg.modules.bytedance.advertise.mapper.AiBytedanceAdvertiserStrategyMapper;
+import org.jeecg.modules.bytedance.advertise.mapper.ByteDanceAdvertisePlanMapper;
 import org.jeecg.modules.bytedance.common.utils.Check;
 import org.jeecg.modules.bytedance.report.entity.BytedanceAdvertiserHourlyReport;
 import org.jeecg.modules.bytedance.report.entity.vo.ReportCostVo;
@@ -18,10 +23,8 @@ import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
 import java.text.DecimalFormat;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.text.SimpleDateFormat;
+import java.util.*;
 
 /**
  * @Description: 广告主小时级别报表信息
@@ -41,6 +44,9 @@ public class BytedanceAdvertiserHourlyReportServiceImpl extends ServiceImpl<Byte
     @Resource
     private AiBytedanceAdvertiserStrategyMapper aiBytedanceAdvertiserStrategyMapper;
 
+    @Resource
+    private ByteDanceAdvertisePlanMapper byteDanceAdvertisePlanMapper;
+
 
     /**
      *
@@ -117,6 +123,11 @@ public class BytedanceAdvertiserHourlyReportServiceImpl extends ServiceImpl<Byte
     }
 
 
+    /**
+     * 获取 数据
+     * @param list
+     * @return
+     */
     public Map<String,Object> getCost(List<ReportCostVo> list){
         Map<String,Object> map = new HashMap<>();
 
@@ -138,12 +149,130 @@ public class BytedanceAdvertiserHourlyReportServiceImpl extends ServiceImpl<Byte
         int showNum = list.stream().mapToInt(ReportCostVo::getShowNum).sum();
         double show = (cost / showNum) * 1000 ;
         map.put("averageShow", String.format("%.2f", show));
+        return map;
+    }
 
 
 
-        return map;
+    /**
+     *
+     * @description:
+     *
+     * @param userId 当前登录用户id
+     * @param type 类型 kuaishou bytedance
+     * @return: org.jeecg.common.api.vo.Result
+     * @author: zianY
+     */
+    @Override
+    public Result initAccountIdAndTime(String userId, String type) throws Exception {
+        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
+        Map<String,Object> resultMap = new HashMap<>();
+        Map<String,Object> map = bytedanceAdvertiserHourlyReportMapper.selAcountIdCostMax(userId,type);
+        resultMap.put("startTime", formatter.format(new Date()));
+        resultMap.put("endTime", formatter.format(new Date()));
+        resultMap.put("account", map);
+        return Result.successMsg("成功",resultMap);
+    }
+
+
+    /**
+     *
+     * @description: 查询账户余额;花费
+     *
+     * @param accountId
+     * @return: org.jeecg.common.api.vo.Result
+     * @author: zianY
+     */
+    @Override
+    public Result getAccountCost(String accountId) throws Exception {
+
+        Map<String,Object> resultMap = new HashMap<>();
+        Map<String,Object> accountMap = bytedanceAdvertiserHourlyReportMapper.selectAccountCost(accountId);
+        //获取账户预算
+        QueryWrapper<AiBytedanceAdvertiserStrategy> strategyQueryWrapper = new QueryWrapper<>();
+        strategyQueryWrapper.eq("account_id",accountId);
+        strategyQueryWrapper.eq("status",0);
+        strategyQueryWrapper.last("limit 1");
+        AiBytedanceAdvertiserStrategy strategy = aiBytedanceAdvertiserStrategyMapper.selectOne(strategyQueryWrapper);
+        double accountBudget = Check.isNull(strategy) ? 0 : strategy.getAccountBudget();
+        resultMap.put("accountMap", accountMap);
+        resultMap.put("accountBudget", accountBudget);
+
+        return Result.successMsg("成功。", resultMap);
     }
 
 
 
+    /**
+     *
+     * @description:查询计划数据
+     *
+     * @param accountId 账户id
+     * @param startTime 开始时间
+     * @param endTime 结束时间
+     * @return: org.jeecg.common.api.vo.Result
+     * @author: zianY
+     */
+    @Override
+    public Result getReportPlanCost(String accountId, String startTime, String endTime,Integer pageNum,Integer pageSize) throws Exception {
+        DecimalFormat df = new DecimalFormat("0.00%");
+        //根据本阶段时间 获取 上阶段 开始-截至 时间
+        Map<String,String> lastTimeMap = TimeStartAndEndUtil.getStartEndTime(startTime,endTime);
+        PageHelper.startPage(pageNum,pageSize);
+        //查询计划数据时报
+       List<ReportCostVo> reportCostVoList = bytedanceAdvertiserHourlyReportMapper.selectReportPlanCostInfo(accountId, null, startTime,endTime);
+       reportCostVoList.forEach(reportCostVo -> {
+           //出价
+           QueryWrapper<ByteDanceAdvertisePlan> planQueryWrapper = new QueryWrapper<>();
+           planQueryWrapper.eq("account_id",accountId);
+           planQueryWrapper.last("limit 1");
+           ByteDanceAdvertisePlan planInfo = byteDanceAdvertisePlanMapper.selectOne(planQueryWrapper);
+           reportCostVo.setCpaBid(planInfo.getCpaBid());
+
+           // 平均千次展显费用 = 总花费/展示数 * 1000
+           double show = 0;
+           if (reportCostVo.getShowNum() != 0){
+               show = (reportCostVo.getCost() / reportCostVo.getShowNum()) * 1000 ;
+           }
+           reportCostVo.setAverageShow(String.format("%.2f", show));
+
+           double avgClick = 0;
+           double clickRate = 0;
+           double convetRate = 0;
+           if (reportCostVo.getClick() != 0){
+               //平均点击单价 = 花费 / 点击数
+               avgClick = reportCostVo.getCost() / reportCostVo.getClick();
+               //点击率 = 展示 / 点击数
+               clickRate = reportCostVo.getShowNum() / reportCostVo.getClick();
+               //转化率 = 转化数 / 点击数 * 100
+               convetRate = Double.valueOf(reportCostVo.getConvertNum()) /  Double.valueOf(reportCostVo.getClick()) * 100;
+           }
+
+           reportCostVo.setAverageClick(String.format("%.2f", avgClick));
+           reportCostVo.setClickRate(df.format(clickRate));
+           reportCostVo.setConverRate(String.format("%.2f", convetRate));
+
+           //转化成本 = 总花费 / 转化数
+           double conver = 0;
+           if (reportCostVo.getConvertNum() != 0){
+               conver = reportCostVo.getCost() / reportCostVo.getConvertNum();
+           }
+           reportCostVo.setConver(String.format("%.2f", conver));
+
+           // 计划id 和 上阶段 时间 查询 上阶段数据
+           List<ReportCostVo> lastReportCostVoList = bytedanceAdvertiserHourlyReportMapper.selectReportPlanCostInfo(accountId, reportCostVo.getAdId(), lastTimeMap.get("lastTimeStart"),lastTimeMap.get("lastTimeEnd"));
+           double compare = 0;
+           if (lastReportCostVoList.size() > 0){
+               if (lastReportCostVoList.get(0).getCost() !=0){
+                   //环比 =(当前花费 - 上阶段花费 )/ 上阶段花费
+                   compare = (reportCostVo.getCost() - lastReportCostVoList.get(0).getCost()) / lastReportCostVoList.get(0).getCost();
+               }
+           }
+           reportCostVo.setCompare(df.format(compare));
+       });
+        PageInfo<ReportCostVo> pageInfo = new PageInfo<ReportCostVo>(reportCostVoList);
+        return Result.successMsg("成功。", pageInfo);
+    }
+
+
 }

+ 92 - 2
jeecg-boot-module-system/src/main/java/org/jeecg/modules/bytedance/advertise/controller/BytedanceReportController.java

@@ -4,18 +4,24 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.jeecg.common.api.vo.Result;
+import org.jeecg.modules.bytedance.advertise.dockapi.MarketingService;
+import org.jeecg.modules.bytedance.common.entity.CtopOauthToken;
+import org.jeecg.modules.bytedance.common.service.ICtopOauthTokenService;
 import org.jeecg.modules.bytedance.common.service.IMaterialInfoService;
+import org.jeecg.modules.bytedance.common.utils.Check;
 import org.jeecg.modules.bytedance.report.service.IBytedanceAdvertiserHourlyReportService;
 import org.jeecg.modules.bytedance.report.service.IBytedancePlanHourlyReportService;
 import org.jeecg.modules.bytedance.report.service.IBytedanceReportMaterialDailyService;
+import org.jeecg.modules.system.service.ISysRoleService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.util.Arrays;
-import java.util.List;
+import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.util.*;
 
 /**
  * 投放数据
@@ -37,6 +43,40 @@ public class BytedanceReportController {
 	@Autowired
 	private IBytedanceReportMaterialDailyService bytedanceReportMaterialDailyService;
 
+	@Autowired
+	private ISysRoleService sysRoleService;
+
+	@Autowired
+	private MarketingService marketingService;
+
+	@Autowired
+	private ICtopOauthTokenService tokenService;
+
+
+
+
+	@ApiOperation(value="投放数据-获取当前用户花费最高的的账户", notes="投放数据-获取当前用户花费最高的的账户")
+	@GetMapping(value = "/initAccountIdAndTime")
+	public Result initAccountIdAndTime(@RequestParam("userId") String userId,
+										 @RequestParam(value = "type",defaultValue = "bytedance") String type) {
+		try {
+			//查询用户角色
+			String roleCode = sysRoleService.getRoleCodeByUserId(userId);
+			if (Check.isNull(roleCode)){
+				return Result.errorMsg("请输入正确的用户id。");
+			}
+			//角色为 管理员 || 销售 || 媒介 查询全部
+			if ("admin".equals(roleCode) || roleCode.contains("sale") || "meidaManager".equals(roleCode)) {
+				userId = null;
+			}
+			return bytedanceAdvertiserHourlyReportService.initAccountIdAndTime(userId,type);
+		}catch (Exception e){
+			log.error("投放数据-获取当前用户花费最高的的账户异常",e);
+			return Result.error("请求失败,请联系开发人员!");
+		}
+	}
+
+
 
 
 
@@ -56,6 +96,56 @@ public class BytedanceReportController {
 
 
 
+	@ApiOperation(value="投放数据-查询账户余额", notes="投放数据-查询账户余额")
+	@GetMapping(value = "/getAccountCost")
+	public Result getAccountCost(@RequestParam("accountId") String accountId) {
+		try {
+			return bytedanceAdvertiserHourlyReportService.getAccountCost(accountId);
+		}catch (Exception e){
+			log.error("投放数据-获取花费和环比异常",e);
+			return Result.error("请求失败,请联系开发人员!");
+		}
+	}
+
+
+	@ApiOperation(value="投放数据-查询计划数据", notes="投放数据-查询计划数据")
+	@GetMapping(value = "/getReportPlanCost")
+	public Result getReportPlanCost(@RequestParam("accountId") String accountId,
+									@RequestParam("startTime") String startTime,
+									@RequestParam("endTime") String endTime,
+									@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
+									@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
+		try {
+			return bytedanceAdvertiserHourlyReportService.getReportPlanCost(accountId,startTime,endTime,pageNum,pageSize);
+		}catch (Exception e){
+			log.error("投放数据-获取花费和环比异常",e);
+			return Result.error("请求失败,请联系开发人员!");
+		}
+	}
+
+
+	@ApiOperation(value="投放数据-更新计划出价", notes="投放数据-更新计划出价")
+	@GetMapping(value = "/updateADBid")
+	public Result updateADBid(@RequestParam("accountId") String accountId,
+									@RequestParam("adId") String adId,
+									@RequestParam("bid") BigDecimal bid) {
+		try {
+			CtopOauthToken token = tokenService.getOauthTokenByAccountId(accountId);
+			List<Map<String, Object>> listMap = new ArrayList<>();
+			Map<String, Object> map = new HashMap<>();
+			map.put("ad_id",adId);
+			map.put("bid",bid);
+			listMap.add(map);
+			return marketingService.updatePlanBid(token,accountId,listMap);
+		}catch (Exception e){
+			log.error("投放数据-获取花费和环比异常",e);
+			return Result.error("请求失败,请联系开发人员!");
+		}
+	}
+
+
+
+