package cn.com.ctop.report.controller;

import cn.com.ctop.common.utils.ResultMapUtils;
import cn.com.ctop.common.utils.StatusCode;
import cn.com.ctop.report.entity.AccountAttributionInfo;
import cn.com.ctop.report.service.AccountAttributionInfoService;
import cn.com.ctop.report.service.IKuaishouAccountReportDailyDwService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("account/report")
@Slf4j
public class KuaishouAccountReportDailyDwController {
    @Autowired
    IKuaishouAccountReportDailyDwService accountReportDailyDwService;
    @Autowired
    private AccountAttributionInfoService accountAttributionInfoService;
    @PostMapping("getProjectAccountByUserId")
    public Map<String,Object> getProjectAccount(@RequestBody JSONObject params) {
        Map<String,Object> resultMap = new HashMap<>();
        String userId = params.getString("userId");
        String type = params.getString("type");
        if(null == userId||"".equals(userId.trim())||null == type||"".equals(type.trim())){
            ResultMapUtils.setResultMap(resultMap, StatusCode.COMMON_PARAM_ERROR);
            return resultMap;
        }
        JSONArray dataList = accountAttributionInfoService.getProjectAccountByUserId(userId, type);
        resultMap.put("message","获取数据成功");
        resultMap.put("code","0");
        resultMap.put("success",true);
        resultMap.put("data",dataList);
        return resultMap;
    }

    /**
     * 查询账户报表数据
     */
    @PostMapping("/kuaishou/list")
    public Result<List<JSONObject>> getListReport(@RequestBody JSONObject requestBody) throws ParseException {
        Result<List<JSONObject>> result = new Result<>();
        if (requestBody.isEmpty()) {
            log.error("request body is empty");
            result.setSuccess(false);
        }
        BigDecimal discount = requestBody.getBigDecimal("disscount");
        JSONArray accounts = requestBody.getJSONArray("accountIds");
        String userId = requestBody.getString("userId");
        if (accounts.isEmpty()) {
            List<AccountAttributionInfo> accountAttributionInfos = accountAttributionInfoService.getAccountInfoList(userId,"kuaishou");
            accounts = new JSONArray();
            for (AccountAttributionInfo account:accountAttributionInfos) {
                accounts.add(account.getAccountId());
            }
        }
        String startDate = requestBody.getString("startDate");
        String endDate = requestBody.getString("endDate");
        Integer hour = requestBody.getInteger("hour");
        if(null == hour){
            hour = DateUtils.getNowHour();
        }
        String target = requestBody.getString("target");
        String order = requestBody.getString("order");
        try {
            List<JSONObject> listData = accountReportDailyDwService.getListDataByParams("1", discount, accounts, startDate, endDate, hour, target, order);
            result.setResult(listData);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 查询素材报表chart数据
     */
    @PostMapping("/kuaishou/chart")
    public Result<Map<String, Object>> getAccountChartReport(@RequestBody JSONObject requestBody) {
        Result<Map<String, Object>> resultBody = new Result<>();
        Map<String, Object> result = new HashMap<>();
        if (requestBody.isEmpty()) {
            log.error("request body is empty");
            resultBody.setSuccess(false);
        }
        String mediaId = requestBody.getString("mediaId");
        BigDecimal discount = requestBody.getBigDecimal("disscount");
        JSONArray accounts = requestBody.getJSONArray("accountIds");
        if (accounts.isEmpty()) {
            return resultBody;
        }
        String startDate = requestBody.getString("startDate");
        String endDate = requestBody.getString("endDate");
        try {
            Map<String, Object> sumData = accountAttributionInfoService.getSumDataBy(mediaId, discount, accounts, startDate, endDate);
            List<JSONObject> chartData = accountAttributionInfoService.getChartDataBy(mediaId, discount, accounts, startDate, endDate);
            result.put("sumData", sumData);
            result.put("chartData", chartData);
            resultBody.setResult(result);
            resultBody.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            resultBody.setSuccess(false);
        }
        return resultBody;
    }
}
