package cn.com.ctop.kuaishou.modules.batch.service.impl;


import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
import cn.com.ctop.common.module.utils.PropertiesUtils;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouAccountBudget;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouAccountBudgetMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouAccountBudgetService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 账户日预算
 * @author jeecg-boot
 * @date   2020-04-26
 * @version V1.0
 */
@Slf4j
@Service
public class KuaishouAccountBudgetServiceImpl extends ServiceImpl<KuaishouAccountBudgetMapper, KuaishouAccountBudget> implements IKuaishouAccountBudgetService {


    @Autowired
    KuaishouAccountBudgetMapper accountBudgetMapper;


    /**
     * 获取账户日预算结果
     * @param advertiserId
     * @param accessToken
     * @return
     */
    @Override
    public void getAccountBudget(Long advertiserId, String accessToken) {
        try {
            String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.ACCOUNT_BUDGET;
            Map<String, String> headers = new HashMap<>();
            headers.put("Access-Token", accessToken);
            headers.put("Content-Type", "application/json");
            JSONObject json = new JSONObject();
            json.put("advertiser_id", advertiserId);
            String result = HttpUtils.kuaiShouhttpPostRequest(url, json.toJSONString(), headers);
            JSONObject resultJson = JSONObject.parseObject(result);
            if (!Check.isNull(resultJson)) {
                Integer code = resultJson.getInteger("code");
                if (code == 0) {
                    Map<String, Object> deleteMap = new HashMap<>();
                    deleteMap.put("account_id", advertiserId);
                    accountBudgetMapper.deleteByMap(deleteMap);
                    JSONObject data = resultJson.getJSONObject("data");
                    if (!Check.isNull(data)) {
                        KuaishouAccountBudget budget = new KuaishouAccountBudget();
                        budget.setAccountId(advertiserId);
                        budget.setDayBudget(data.get("day_budget").toString());
                        JSONArray jsonArr = data.getJSONArray("day_budget_schedule");
                        if (!Check.isNull(jsonArr)) {
                            budget.setDayBudgetSchedule(jsonArr.toString());
                        }
                        budget.setCreateTime(new Date());
                        accountBudgetMapper.insert(budget);

                    }
                } else {
                    log.error("获取账户日预算失败，返回信息：{}", resultJson);
                }
            } else {
                log.error("获取账户日预算返回结果为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
