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.KuaishouSupplyChain;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaishouSupplyChainSettlement;
import cn.com.ctop.kuaishou.modules.batch.entity.vo.ItemCheck;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaishouSupplyChainMapper;
import cn.com.ctop.kuaishou.modules.batch.mapper.RocketSupplyChainMapper;
import cn.com.ctop.kuaishou.modules.batch.service.RocketSupplyChainService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 快手电商-供应链订单信息
 *
 * @author jeecg-boot
 * 2022-12-01
 * @version V1.0
 */
@Slf4j
@Service
public class RocketSupplyChainServiceImpl extends ServiceImpl<KuaishouSupplyChainMapper, KuaishouSupplyChain> implements RocketSupplyChainService {
    private static String url = "https://cps.kwaixiaodian.com/distribute/pc/investment/order/list";
    @Autowired
    private ISendMessageService sendMessageService;
    @Autowired
    private RocketSupplyChainMapper rocketSupplyChainMapper;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Transactional
    @Override
    public void getOrderList(String cookie, Integer offset, Long orderCreateTimeStart, Long orderCreateTimeEnd, Long regimentalId) throws Exception {
        Thread.sleep(1000);
        log.info("rocket团长抓取订单数据入参，开始时间：{},结束时间：{},数据：{}", DateUtils.tempToLongDateStr(orderCreateTimeStart), DateUtils.tempToLongDateStr(orderCreateTimeEnd), offset);
        Map<String, Object> params = new HashMap<>();
        if (!Check.isNull(orderCreateTimeStart)) {
            params.put("orderCreateTimeStart", orderCreateTimeStart);
        }
        if (!Check.isNull(orderCreateTimeEnd)) {
            params.put("orderCreateTimeEnd", orderCreateTimeEnd);
        }
        params.put("fundType", 1);
        params.put("timeType", 1);
        params.put("limit", 100);
        params.put("offset", offset);

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

        String resultStr = HttpUtils.KuaiShouttpGetRequest(url, params, headers);
        JSONObject result = JSONObject.parseObject(resultStr);
        if (Check.isNull(result)) {
            log.error("快分销数据返回为空");
            return;
        }
        Integer code = result.getInteger("result");
        if (code != 1) {
            log.info("rocket快分销数据返回异常：", resultStr);
            sendMessageService.sendMessage("113dee46c7df464da78c07a985e92cd1", "rocketcookie失效，请及时更新。媒体返回信息：" + resultStr);
            return;
        }
        JSONObject dataJson = result.getJSONObject("data");
        if (Check.isNull(dataJson)) {
            log.error("rocket数据返回为data数据为空");
            return;
        }
        JSONArray list = dataJson.getJSONArray("list");
        if (Check.isNull(list)) {
            log.error("rocket数据返回为list数据为空");
            return;
        }
        log.info("rocket当前数据条数:{}", list.size());
        List<KuaishouSupplyChain> ruixuanSupplyChains = new ArrayList<>();
        List<KuaishouSupplyChain> mgsSupplyChains = new ArrayList<>();
        List<KuaishouSupplyChain> rocketSupplyChains = new ArrayList<>();
        List<KuaishouSupplyChain> yufuSupplyChains = new ArrayList<>();
        List<KuaishouSupplyChain> yuxiuSupplyChains = new ArrayList<>();
        List<KuaishouSupplyChain> zhiShuiSupplyChains = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            JSONObject jsonObject = list.getJSONObject(i);
            KuaishouSupplyChain supplyChain = new KuaishouSupplyChain();
            supplyChain.setRegimentalId(regimentalId);
            supplyChain.setOid(jsonObject.getLong("oid"));
            JSONObject item = jsonObject.getJSONArray("itemList").getJSONObject(0);
            Long itemId = null;
            if (!Check.isNull(item)) {
                itemId = item.getLong("itemId");
                supplyChain.setItemId(itemId);
                supplyChain.setItemTitle(item.getString("itemTitle"));
                supplyChain.setReservePrice(item.getLong("reservePrice"));
                supplyChain.setImageUrl(item.getString("imageUrl"));
            }
            supplyChain.setOrderStatus(jsonObject.getInteger("orderStatus"));
            supplyChain.setOrderCreateTime(jsonObject.getLong("orderCreateTime"));
            supplyChain.setSendStatus(jsonObject.getInteger("sendStatus"));
            supplyChain.setSettlementBizType(jsonObject.getInteger("settlementBizType"));
            supplyChain.setSettlementBizTypeDesc(jsonObject.getString("settlementBizTypeDesc"));
            supplyChain.setSendTime(jsonObject.getLong("sendTime"));
            supplyChain.setRecvTime(jsonObject.getLong("recvTime"));
            supplyChain.setSellerId(jsonObject.getLong("sellerId"));
            supplyChain.setPromoterId(jsonObject.getLong("promoterId"));
            supplyChain.setPromoterNickName(filterSupplement(jsonObject.getString("promoterNickName")));
            supplyChain.setActivityId(jsonObject.getLong("activityId"));
            supplyChain.setOrderAmount(jsonObject.getLong("orderAmount"));
            supplyChain.setPayAmount(jsonObject.getLong("payAmount"));
            supplyChain.setRegimentalPromotionRate(jsonObject.getLong("regimentalPromotionRate"));
            supplyChain.setShareRate(jsonObject.getLong("shareRate"));
            supplyChain.setBaseAmount(jsonObject.getLong("baseAmount"));
            supplyChain.setServiceAmount(jsonObject.getLong("serviceAmount"));
            supplyChain.setRegimentalPromotionAmount(jsonObject.getLong("regimentalPromotionAmount"));
            supplyChain.setTotalRegimentalSettleAmount(jsonObject.getLong("totalRegimentalSettleAmount"));
            supplyChain.setStatDate(DateUtils.tempToDateLong(jsonObject.getLong("orderCreateTime")));
            supplyChain.setStatHour(DateUtils.getHour(jsonObject.getLong("orderCreateTime")));
            Criteria criteria = new Criteria();
            criteria = Criteria.where("_id").is(itemId);
            Query query = new Query(criteria);
            ItemCheck itemCheck = mongoTemplate.findOne(query, ItemCheck.class);
            if (Check.isNull(itemCheck)) {
                rocketSupplyChains.add(supplyChain);
            }
            if (!Check.isNull(itemCheck)) {
                String ownership = itemCheck.getOwnership();
                if ("ruixuan".equals(ownership)) {
                    ruixuanSupplyChains.add(supplyChain);
                }
                if ("miaogousi".equals(ownership)) {
                    mgsSupplyChains.add(supplyChain);
                }
                if ("rocket".equals(ownership)) {
                    rocketSupplyChains.add(supplyChain);
                }
                if ("yufu".equals(ownership)) {
                    yufuSupplyChains.add(supplyChain);
                }
                if ("yuxiu".equals(ownership)) {
                    yuxiuSupplyChains.add(supplyChain);
                }
                if ("zhishui".equals(ownership)) {
                    zhiShuiSupplyChains.add(supplyChain);
                }
            }
        }
        try {
            if (!Check.isNull(ruixuanSupplyChains)) {
                rocketSupplyChainMapper.replaceRuixuanSupplyChains(ruixuanSupplyChains);
            }

            if (!Check.isNull(mgsSupplyChains)) {
                rocketSupplyChainMapper.replaceMgsSupplyChains(mgsSupplyChains);
            }

            if (!Check.isNull(rocketSupplyChains)) {
                rocketSupplyChainMapper.replaceRocketSupplyChains(rocketSupplyChains);
            }

            if (!Check.isNull(yufuSupplyChains)) {
                rocketSupplyChainMapper.replaceYufuSupplyChains(yufuSupplyChains);
            }

            if (!Check.isNull(yuxiuSupplyChains)) {
                rocketSupplyChainMapper.replaceYuXiuSupplyChains(yuxiuSupplyChains);
            }
            if (!Check.isNull(zhiShuiSupplyChains)) {
                rocketSupplyChainMapper.replaceZhiShuiSupplyChains(zhiShuiSupplyChains);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        if (list.size() >= 100) {
            getOrderList(cookie, offset + 100, orderCreateTimeStart, orderCreateTimeEnd, regimentalId);
        }

    }

    @Override
    public List<JSONObject> getCookie() {
        return rocketSupplyChainMapper.getCookie();
    }

    public static String filterSupplement(String content) {
        StringBuilder sb = new StringBuilder();
        for (char ch : content.toCharArray()) {
            if (!Character.isHighSurrogate(ch) && !Character.isLowSurrogate(ch)) {
                sb.append(ch);
            }
        }
        return sb.toString();
    }

}



