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

import cn.com.ctop.common.module.service.ISendMessageService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouAgentAccountList;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouAgentAccountListMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouAgentAccountListService;
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 java.util.*;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-15
 */
@Service
@Slf4j
public class KuaishouAgentAccountListServiceImpl implements IKuaishouAgentAccountListService {
    @Autowired
    private KuaishouAgentAccountListMapper kuaishouAgentAccountListMapper;
    @Autowired
    private ISendMessageService sendMessageService;


    @Override
    public List<String> getCookie() {
        return kuaishouAgentAccountListMapper.getCookie();
    }

    @Override
    public void kuaishouAgentAccountListService(String cookie, Integer currentPage, Integer pageSize, Long createBeginTime, Long createEndTime) {
        JSONObject requestJson = new JSONObject();
        if (!Check.isNull(createBeginTime)) {
            requestJson.put("createBeginTime", createBeginTime);
        }
        if (!Check.isNull(createEndTime)) {
            requestJson.put("createEndTime", createEndTime);
        }
        JSONObject pageInfo = new JSONObject();
        pageInfo.put("currentPage", currentPage);
        pageInfo.put("pageSize", pageSize);
        requestJson.put("pageInfo", pageInfo);
        requestJson.put("accountExpireCertTag", "ALL");
        requestJson.put("asc", false);
        requestJson.put("belongType", "ALL");
        requestJson.put("creatorAgentId", null);
        requestJson.put("creatorName", null);
        requestJson.put("creatorUserId", null);
        requestJson.put("ownerKeyword", "");
        requestJson.put("ownerType", "ALL");
        requestJson.put("reviewStatus", "-1");
        requestJson.put("secondaryAgentKeyword", "");
        requestJson.put("selectType", "1");
        requestJson.put("selectValue", "");
        requestJson.put("sortKey", "");
        requestJson.put("ucType", "ALL");
        Map<String, String> map = new HashMap<>();
        map.put("Cookie", cookie);
        map.put("Content-Type:", "application/json");
        String s = HttpUtils.httpPostRequest("https://jinfu.e.kuaishou.com/rest/dsp/agent/account/list", requestJson, map);
        JSONObject jsonObject = JSONObject.parseObject(s);
        if (Check.isNull(jsonObject)) {
            log.error("获取代理商账户列表返回数据为空");
            return;
        }
        Integer result = jsonObject.getInteger("result");
        if (result == 1) {
            JSONArray adDspAccountList = jsonObject.getJSONArray("adDspAccountList");
            if (!Check.isNull(adDspAccountList)) {
                List<KuaishouAgentAccountList> addList = new ArrayList<>();
                for (int i = 0; i < adDspAccountList.size(); i++) {
                    JSONObject accountJson = adDspAccountList.getJSONObject(i);
                    KuaishouAgentAccountList account = new KuaishouAgentAccountList();
                    account.setAccountId(accountJson.getLong("accountId"));
                    account.setUserId(accountJson.getLong("userId"));
                    account.setAccountName(accountJson.getString("accountName"));
                    account.setUserName(accountJson.getString("userName"));
                    account.setAccountCreateTime(new Date(accountJson.getLong("createTime")));
                    account.setProductName(accountJson.getString("productName"));
                    account.setIndustry(accountJson.getString("industry"));
                    account.setSecondIndustry(accountJson.getString("secondIndustry"));
                    account.setCorporationName(accountJson.getString("corporationName"));
                    addList.add(account);
                }
                kuaishouAgentAccountListMapper.addBatch(addList);
                if (adDspAccountList.size() >= pageSize) {
                    kuaishouAgentAccountListService(cookie, currentPage + 1, pageSize, createBeginTime, createEndTime);
                }
            }
        } else {
            log.info("快分销数据返回异常：", jsonObject);
            sendMessageService.sendMessage("113dee46c7df464da78c07a985e92cd1", "磁力金牛cookin失效，请及时更新。媒体返回信息：" + jsonObject);
        }
    }
}