package com.ruixuan.jinniu.service.impl;
import java.util.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.utils.Check;
import com.ruixuan.common.utils.DateUtils;
import com.ruixuan.common.utils.http.HttpUtil;
import com.ruixuan.jinniu.entity.KuaishouAgentAccountList;
import com.ruixuan.jinniu.mapper.KuaishouAgentAccountListMapper;
import com.ruixuan.jinniu.service.IKuaishouAgentAccountListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


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

    /**
     * 查询【请填写功能名称】
     *
     * @param accountId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public KuaishouAgentAccountList selectKuaishouAgentAccountListByAccountId(Long accountId) {
        return kuaishouAgentAccountListMapper.selectKuaishouAgentAccountListByAccountId(accountId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param kuaishouAgentAccountList 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<KuaishouAgentAccountList> selectKuaishouAgentAccountListList(KuaishouAgentAccountList kuaishouAgentAccountList) {
        return kuaishouAgentAccountListMapper.selectKuaishouAgentAccountListList(kuaishouAgentAccountList);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param kuaishouAgentAccountList 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertKuaishouAgentAccountList(KuaishouAgentAccountList kuaishouAgentAccountList) {
        kuaishouAgentAccountList.setCreateTime(DateUtils.getNowDate());
        return kuaishouAgentAccountListMapper.insertKuaishouAgentAccountList(kuaishouAgentAccountList);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param kuaishouAgentAccountList 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateKuaishouAgentAccountList(KuaishouAgentAccountList kuaishouAgentAccountList) {
        kuaishouAgentAccountList.setUpdateTime(DateUtils.getNowDate());
        return kuaishouAgentAccountListMapper.updateKuaishouAgentAccountList(kuaishouAgentAccountList);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param accountIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouAgentAccountListByAccountIds(Long[] accountIds) {
        return kuaishouAgentAccountListMapper.deleteKuaishouAgentAccountListByAccountIds(accountIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param accountId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouAgentAccountListByAccountId(Long accountId) {
        return kuaishouAgentAccountListMapper.deleteKuaishouAgentAccountListByAccountId(accountId);
    }

    @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 = HttpUtil.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);
                }
            }
        }
    }
}