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.KuaiShouActivityInfo;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouActivityInfoMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouActivityInfoService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 快分销-招商信息
 *
 * @author jeecg-boot
 * 2023-01-10
 * @version V1.0
 */
@Slf4j
@Service
public class KuaiShouActivityInfoServiceImpl extends ServiceImpl<KuaiShouActivityInfoMapper, KuaiShouActivityInfo> implements IKuaiShouActivityInfoService {
    private static String activityUrl = "https://cps.kwaixiaodian.com/distribute/pc/investment/activity/list";
    @Autowired
    private ISendMessageService sendMessageService;
    @Autowired
    private KuaiShouActivityInfoMapper activityInfoMapper;

    @Override
    public void getActivityInfo(Long regimentalId,String cookie, Integer offset) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("limit", 1000);
        params.put("offset", offset);

        Map<String, String> headers = new HashMap<>();
        headers.put("Cookie", cookie);

        String resultStr = HttpUtils.KuaiShouttpGetRequest(activityUrl, params, headers);
        JSONObject result = JSONObject.parseObject(resultStr);
        if (Check.isNull(result)) {
            log.error("快分销招商数据返回为空");
            return;
        }

        Integer code = result.getInteger("result");
        if (code != 1) {
            log.info("快分销招商数据返回异常：", resultStr);
            sendMessageService.sendMessage("113dee46c7df464da78c07a985e92cd1", "快手供应链cookie失效，请及时更新。媒体返回信息：" + resultStr);
            return;
        }
        JSONArray dataArray = result.getJSONArray("data");
        if (Check.isNull(dataArray)) {
            log.error("快分销数据返回为data数据为空");
            return;
        }
        List<KuaiShouActivityInfo> activityInfoList = new ArrayList<>();
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject jsonObject = dataArray.getJSONObject(i);
            KuaiShouActivityInfo activityInfo = new KuaiShouActivityInfo();
            activityInfo.setActivityId(jsonObject.getLong("activityId"));
            activityInfo.setRegimentalId(regimentalId);
            activityInfo.setActivityTitle(jsonObject.getString("activityTitle"));
            activityInfo.setActivityType(jsonObject.getInteger("activityType"));
            activityInfo.setActivityBeginTime(DateUtils.tempToDate(jsonObject.getLong("activityBeginTime")));
            activityInfo.setActivityEndTime(DateUtils.tempToDate(jsonObject.getLong("activityEndTime")));
            activityInfo.setActivityStatus(jsonObject.getInteger("activityStatus"));
            JSONObject activityItemDataInfo = jsonObject.getJSONObject("activityItemDataInfo");
            if (!Check.isNull(activityItemDataInfo)) {
                activityInfo.setApplyItemCount(activityItemDataInfo.getInteger("applyItemCount"));
                activityInfo.setWaitAuditItemCount(activityItemDataInfo.getInteger("waitAuditItemCount"));
                activityInfo.setAuditPassItemCount(activityItemDataInfo.getInteger("auditPassItemCount"));
            }
            activityInfoList.add(activityInfo);
        }
        if (!Check.isNull(activityInfoList)) {
            activityInfoMapper.replace(activityInfoList);
        }
        log.info("快分销招商数据更新数量:{}", activityInfoList.size());
        if (dataArray.size() >= 1000) {
            getActivityInfo(regimentalId,cookie, offset + 1000);
        }
    }

    @Override
    public List<JSONObject> getActivityIdList(Long id ) {
        return activityInfoMapper.getActivityIdList(id);
    }

    @Override
    public Map<Long, Integer> getItemMap() {
        List<JSONObject> itemList = activityInfoMapper.itemList();
        Map<Long, Integer> map = new HashMap<>();
        if (!Check.isNull(itemList)) {
            for (int i = 0; i < itemList.size(); i++) {
                JSONObject jsonObject = itemList.get(i);
                if (!Check.isNull(jsonObject)) {
                    map.put(jsonObject.getLong("itemId"), jsonObject.getInteger("activityItemStatus"));
                }
            }
        }
        return map;
    }
}
