package com.fs.his.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.gtPush.domain.UniPushLog;
import com.fs.gtPush.domain.PushResult;
import com.fs.gtPush.service.UniPushLogService;
import com.fs.gtPush.service.uniPush2Service;
import com.fs.gtPush.utils.PushUtils;
import com.fs.his.enums.FsStoreOrderStatusEnum;
import com.fs.his.enums.PushLogDesTypeEnum;
import com.fs.his.enums.PushLogTypeEnum;
import com.fs.his.service.IFsPackageOrderService;
import com.fs.his.service.IFsStoreOrderService;
import com.fs.his.vo.FsStoreOrderAndUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fs.gtPush.domain.PushReqBean;

import java.util.*;

@Component("appPushMsgTask")
public class AppPushMsgTask {
    @Autowired
    private IFsStoreOrderService orderService;
    @Autowired
    private UniPushLogService logService;
    @Autowired
    private uniPush2Service uniPush2Service;
    @Autowired
    private IFsPackageOrderService packageOrderService;

    //订单 通知类型
    //1.订单未支付
    public void unPayPush() {
        //处方
        unPayPushList(PushLogTypeEnum.ORDER_STORE.getValue());
        //服务包
        unPayPushList(PushLogTypeEnum.ORDER_PACKAGE.getValue());


    }

    /**
     * 查询列表发送
     *
     * @param type
     */
    private void unPayPushList(Float type) {
        //查询已通知的订单
        UniPushLog uniPushLog = new UniPushLog();
        uniPushLog.setType(type);
        uniPushLog.setDesType(PushLogDesTypeEnum.ORDER_UN_PAY.getValue());
        uniPushLog.setPushRes(1);
        List<UniPushLog> uniPushLogs = logService.selectUniPushLogList(uniPushLog);
        List<FsStoreOrderAndUserVo> orderList = null;
        if (Objects.equals(type, PushLogTypeEnum.ORDER_STORE.getValue())) {
            //查询超时未支付订单
            orderList = orderService.selectOrderListAndUser(
                    FsStoreOrderStatusEnum.STATUS_1.getValue(),//未支付
                    10,//10分钟
                    uniPushLogs);
        } else if (Objects.equals(type, PushLogTypeEnum.ORDER_PACKAGE.getValue())) {
            //查询超时未支付服务包订单
            orderList = packageOrderService.selectOrderListAndUser(
                    FsStoreOrderStatusEnum.STATUS_1.getValue(),//未支付
                    10,//10分钟
                    uniPushLogs);
        } else {
            return;
        }

        pushOrderList(orderList, type);
    }

    /**
     * 拆分发送
     *
     * @param orderList
     * @param type
     */
    private void pushOrderList(List<FsStoreOrderAndUserVo> orderList, Float type) {
        //官方要求一次不大于1000
        if (orderList != null && !orderList.isEmpty()) {
            ArrayList<List<FsStoreOrderAndUserVo>> arrayList = new ArrayList<>();
            //分片
            while (true) {
                if (orderList.size() > 1000) {
                    List<FsStoreOrderAndUserVo> tempList = orderList.subList(0, 1000);
                    arrayList.add(tempList);
                    orderList = orderList.subList(1000, orderList.size());
                } else {
                    if (!orderList.isEmpty()) {
                        arrayList.add(orderList);
                    }
                    break;
                }
            }
            arrayList.forEach(list -> pushUnPayMsg(list, type));
        }
    }

    /**
     * 发送未支付通知
     *
     * @param orderList
     * @param type
     */
    private void pushUnPayMsg(List<FsStoreOrderAndUserVo> orderList, Float type) {
        if (orderList != null && !orderList.isEmpty()) {
            //cids
            List<String> cids = new ArrayList<>();
            ArrayList<UniPushLog> addLogs = new ArrayList<>();
            orderList.forEach(order -> {
                UniPushLog pushAddLog = new UniPushLog();
                pushAddLog.setJpushId(order.getJpushId());
                pushAddLog.setType(order.getType() == null ? PushLogTypeEnum.ORDER.getValue() : order.getType());
                pushAddLog.setDesType(PushLogDesTypeEnum.ORDER_UN_PAY.getValue());
                pushAddLog.setUserId(order.getUserId());
                pushAddLog.setBusinessId(order.getOrderId());
                pushAddLog.setPushMsg(
                        "{" +
                                "\"push_clientid\":\"" + order.getJpushId() +
                                "\",\"title\":\"" + PushLogDesTypeEnum.ORDER_UN_PAY.getDesc() +
                                "\",\"content\":\"您有订单未支付\"" +
                                "\",\"force_notification\":true\"" +
                                "\",\"category\":{\"harmony\":\"EXPRESS\", \"huawei\":\"EXPRESS\", \"vivo\":\"ORDER\"}" +
                                "\",\"payload\":{\"url\":\"订单详情页\"}"
                        + "}"
                );
                pushAddLog.setCreateTime(DateUtils.getNowDate());
                addLogs.add(pushAddLog);
                cids.add(order.getJpushId());
            });

            HashMap<String, Object> map = new HashMap<>();
            HashMap<String, Object> map2 = new HashMap<>();
            if (Objects.equals(type, PushLogTypeEnum.ORDER_STORE.getValue())) {
                map.put("url", "/pages/store/storeOrderList?status=1"); //处方
            } else if (Objects.equals(type, PushLogTypeEnum.ORDER_PACKAGE.getValue())) {
                map.put("url", "/pages/store/packageOrderList?status=1"); //服务包
            }

            Map<String, Object> xmConfig = new HashMap<>();
            String channel = "113892"; //订单
            xmConfig.put("/extra.channel_id", channel);
            map2.put("XM", xmConfig);


            PushReqBean pushReqBean = new PushReqBean(cids, PushLogDesTypeEnum.ORDER_UN_PAY.getDesc(), "您有订单未支付", map, map2);
            PushResult pushResult = uniPush2Service.pushMessage(pushReqBean);
            ArrayList<UniPushLog> logs = PushUtils.returnArrayMsg(pushResult, addLogs);
            //更新日志结果
            logService.saveBatch(logs, 1000);

        }
    }


    //健康管理
    //营销
}
