package org.jeecg.modules.bytedance.advertise.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.bytedance.advertise.entity.ByteDanceAdvertisePlan;
import org.jeecg.modules.bytedance.advertise.mapper.ByteDanceAdvertisePlanMapper;
import org.jeecg.modules.bytedance.advertise.service.IByteDanceAdvertisePlanService;
import org.jeecg.modules.bytedance.common.entity.CtopOauthToken;
import org.jeecg.modules.bytedance.common.utils.HttpUtils;
import org.jeecg.modules.bytedance.common.utils.PropertiesUtils;
import org.jeecg.modules.bytedance.common.utils.StringUtils;
import org.jeecg.modules.bytedance.report.entity.BytedancePlanHourlyReport;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Slf4j
@Service
public class ByteDanceAdvertisePlanServiceImpl extends ServiceImpl<ByteDanceAdvertisePlanMapper, ByteDanceAdvertisePlan> implements IByteDanceAdvertisePlanService {
    @Resource
    private ByteDanceAdvertisePlanMapper byteDanceAdvertisePlanMapper;
    @Override
    public Map<String, Object> getAdvertiserPlan(CtopOauthToken token, String ids, String date, String updateDate) {
        getAd(token, 1, ids, date, updateDate);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("message", "获取广告计划信息成功");
        return resultMap;
    }


    @Override
    public Map<String, Object> updateAdvertiserPlan(CtopOauthToken token, String landingType, ByteDanceAdvertisePlan byteDanceAdvertisePlan) {

        Map<String, Object> resultMap = new HashMap<>();

        //查询记录，获取修改时间等字段
        List<Long> adId=new ArrayList<>();
        adId.add(byteDanceAdvertisePlan.getId());
        Map<String, Object> map = queryAdByAdId(token, Long.valueOf(byteDanceAdvertisePlan.getAccountId()), adId);
        if(!map.get("code").equals(0)){
            log.error("获取修改时间接口错误");
        }else {
            Object modifyTime = ((Map) ((JSONObject) map.get("data")).getJSONArray("list").get(0)).get("modify_time");
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            headers.put("Access-Token", token.getAccessToken());

            JSONObject params = new JSONObject();
            params.put("advertiser_id", byteDanceAdvertisePlan.getAccountId());
            params.put("ad_id", byteDanceAdvertisePlan.getId());
            params.put("name", byteDanceAdvertisePlan.getName());
            params.put("modify_time", modifyTime);
            params.put("budget", byteDanceAdvertisePlan.getBudget());
            params.put("schedule_type", byteDanceAdvertisePlan.getScheduleType());
            params.put("start_time", byteDanceAdvertisePlan.getStartTime());
            params.put("end_time", byteDanceAdvertisePlan.getEndTime());
            //广告投放时段
            if(!"".equals(byteDanceAdvertisePlan.getScheduleTime())){
                params.put("schedule_time",byteDanceAdvertisePlan.getScheduleTime());
            }
            params.put("bid", byteDanceAdvertisePlan.getBid());
            params.put("cpa_bid", byteDanceAdvertisePlan.getCpaBid());
            params.put("flow_control_mode", byteDanceAdvertisePlan.getFlowControlMode());
            params.put("convert_id", byteDanceAdvertisePlan.getConvertId());
            params.put("deep_bid_type", byteDanceAdvertisePlan.getDeepBidType());
            params.put("deep_cpabid", byteDanceAdvertisePlan.getDeepCpaBid());
            params.put("hide_if_converted", byteDanceAdvertisePlan.getHideIfConverted());
            params.put("hide_if_exists", byteDanceAdvertisePlan.getHideIfExists() == null ? 0 : byteDanceAdvertisePlan.getHideIfExists());
            //params.put("converted_time_duration", byteDanceAdvertisePlan.getConvertedTimeDuration());
            //params.put("luban_roi_goal",byteDanceAdvertisePlan.getLubanRoiGoal()==null?0.00:byteDanceAdvertisePlan.getFloat("lubanRoiGoal"));
            //params.put("roi_goal",byteDanceAdvertisePlan.getFroiGoal")==null?0.00:byteDanceAdvertisePlan.getFloat("roiGoal"));
            //params.put("audience_package_id",byteDanceAdvertisePlan.getLong("audiencePackageId"));
            params.put("adjust_cpa", 0);
            //暂时不需要定向包id
            //params.put("audience_package_id",byteDanceAdvertisePlan.getLong("audiencePackageId"));
        /*if(byteDanceAdvertisePlan.getUseOpenUrl().equals("YES")){
            params.put("open_url", byteDanceAdvertisePlan.getOpenUrl());
        }*/
            //判断推广目的
        /*if (landingType.equals("LINK")) {
            params.put("external_url", byteDanceAdvertisePlan.getExternalUrl());
        }
        if (landingType.equals("APP")) {
            params.put("download_type", byteDanceAdvertisePlan.getDownloadType());
            params.put("download_url", byteDanceAdvertisePlan.getDownloadUrl());
            params.put("package", byteDanceAdvertisePlan.getToutiaoPackage());
            params.put("app_type",byteDanceAdvertisePlan.getAppType());
        }*/
            JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpPostRequest(PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_ad_update"), params, headers));
            if (jsonObject.getInteger("code") != 0) {
                log.error("广告计划修改失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
                resultMap.put("code", -1);
                resultMap.put("message", jsonObject.getString("message"));
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "广告组状态修改成功");
        }

        return resultMap;
    }

    public Map<String, Object> getAdvertiserPlanRejectReason(CtopOauthToken token, Long advertiserId, List<Long> adIds) {

        Map<String, Object> resultMap = new HashMap<>();

        String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_ad_update_reject_reason");
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params = new JSONObject();
        params.put("advertiser_id", advertiserId);
        params.put("ad_ids", adIds);
        String result = HttpUtils.httpPostRequest(url, params, headers);
        JSONObject jsonObject = JSONObject.parseObject(result);
        int code = jsonObject.getInteger("code");
        if (code != 0) {
            log.info("获取计划审核建议接口异常==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        resultMap.put("message", "获取计划审核建议成功");
        return resultMap;
    }


    public void getAd(CtopOauthToken token, int pageNum, String ids, String date, String updateDate) {
        JSONArray getIds = new JSONArray();
        if (null != ids && !"".equals(ids)) {
            String[] idString = ids.split(StringUtils.COMMA);
            if (idString.length > 0) {
                for (String s : idString) {
                    Long getId = Long.parseLong(s);
                    getIds.add(getId);
                }
            }
        }
        // 请求地址
        String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_ad_get");
        // 请求参数
        Map<String, Object> filtering = new HashMap<>();
        if (getIds.size() > 0) {
            filtering.put("ids", getIds);
        }
        if (null != date && !"".equals(date)) {
            filtering.put("ad_create_time", date);
        }
        if (null != updateDate && !"".equals(updateDate)) {
            filtering.put("ad_modify_time", updateDate);
        }

        JSONObject param = new JSONObject();
        param.put("advertiser_id", token.getAccountId());
        param.put("page", pageNum);
        param.put("page_size", 100);
        param.put("filtering", filtering);
        JSONObject resultObject = HttpUtils.bytedanceGetRequest(token.getAccessToken(), url, param);
        Integer code = resultObject.getInteger("code");
        if (code != 0) {
            log.error("获取广告计划信息接口异常==》accountId:{},message:{}", token.getAccountId(), resultObject.getString("message"));
            return;
        }
        JSONArray data = resultObject.getJSONObject("data").getJSONArray("list");
        if (null == data || data.isEmpty()) {
            return;
        }
        for (int i = 0; i < data.size(); i++) {
            JSONObject dataObject = data.getJSONObject(i);
            ByteDanceAdvertisePlan advertisePlan = new ByteDanceAdvertisePlan(dataObject, String.valueOf(token.getAccountId()));
            BigDecimal deepCpaBid = dataObject.getBigDecimal("deep_cpabid");
            if (null != deepCpaBid) {
                advertisePlan.setDeepCpaBid(deepCpaBid);
            }
            this.saveOrUpdate(advertisePlan);
        }
        getAd(token, pageNum + 1, ids, date, updateDate);
    }

    @Override
    public List<String> selectWarningGroup(Long accountId, BigDecimal maxBid) {
        return byteDanceAdvertisePlanMapper.selectWarningGroup(accountId, maxBid);

    }

    @Override
    public List<String> selectWarningCpaBid(Long accountId, BigDecimal maxCapBid) {
        return byteDanceAdvertisePlanMapper.selectWarningCpaBid(accountId, maxCapBid);
    }

    @Override
    public List<ByteDanceAdvertisePlan> getAllPlans(Long accountId,String createDate) {
        QueryWrapper<ByteDanceAdvertisePlan>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",accountId+"");
        if(null!=createDate&&!"".equals(createDate.trim())){
            queryWrapper.ge("ad_create_time",createDate+" 00:00:00");
        }
        queryWrapper.ne("opt_status","AD_STATUS_DELETE");
        queryWrapper.orderByDesc("ad_create_time");
        return this.list(queryWrapper);
    }

    @Override
    public List<ByteDanceAdvertisePlan> getAllPlansByAccountId(Long accountId,String adModifyTime) {
        QueryWrapper<ByteDanceAdvertisePlan>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",accountId+"");
        if(null!=adModifyTime&&!"".equals(adModifyTime.trim())){
            queryWrapper.ge("ad_modify_time",adModifyTime);
            queryWrapper.le("ad_modify_time",new Date());
        }
        queryWrapper.ne("toutiao_status","AD_STATUS_DELETE");
//        queryWrapper.eq("toutiao_status","AD_STATUS_AUDIT_DENY");   //获取通过审核建议
        queryWrapper.orderByDesc("ad_modify_time");
        return this.list(queryWrapper);
    }

    @Override
    public int queryToDayBuiltCount(Long accountId) {
        String date = DateUtil.formatDate(new Date());
        return byteDanceAdvertisePlanMapper.queryTodayBuiltCount(accountId, date);
    }

    private Map<String, Object> queryAdByAdId(CtopOauthToken token, Long accountId, List<Long> adId){

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params = new JSONObject();
        params.put("advertiser_id", accountId);
        Map<String, Object> filtering = new HashMap<>();
        filtering.put("ids",adId);
        params.put("filtering", filtering);
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpGetRequest(PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_ad_get"), headers, params));
        Map<String, Object> resultMap = new HashMap<>();
        if (jsonObject.getInteger("code") == 0) {
            resultMap.put("code", 0);
            resultMap.put("data",jsonObject.getJSONObject("data"));
        } else {
            log.error("广告计划查询失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code",-200);
            resultMap.put("message", jsonObject.getString("message"));
        }
        return resultMap;
    }

    private int getConvertIdByExternalAction(String externalAction){
        int result=0;
        switch (externalAction) {
            case "AD_CONVERT_TYPE_DOWNLOAD_FINISH":
                result = 4;
                break;
            case "AD_CONVERT_TYPE_BUTTON":
                result = 5;
                break;
            case "AD_CONVERT_TYPE_DOWNLOAD_START":
                result = 9;
                break;
            case "AD_CONVERT_TYPE_INSTALL_FINISH":
                result = 15;
                break;
            case "AD_CONVERT_TYPE_REDIRECT":
                result = 95;
                break;
        }
        return result;
    }

    /**
     * 更新广告计划 操作状态 或 定向包id
     * @param byteDanceAdvertisePlan
     * @return
     */
    @Override
    public Integer updatePlanOrPackage (ByteDanceAdvertisePlan byteDanceAdvertisePlan){
       return byteDanceAdvertisePlanMapper.updateAdvertisePlan(byteDanceAdvertisePlan);
    }


}
