package com.younike.system.task;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.younike.common.core.domain.entity.SysUser;
import com.younike.common.utils.sms.AliSmsUtil;
import com.younike.common.utils.sms.AliTemplateCode;
import com.younike.system.domain.Order;
import com.younike.system.domain.SysConfig;
import com.younike.system.domain.TaskLog;
import com.younike.system.domain.vo.OrderVo;
import com.younike.system.domain.vo.OvertimeOrderVo;
import com.younike.system.service.IOrderService;
import com.younike.system.service.ISysConfigService;
import com.younike.system.service.ISysUserService;
import com.younike.system.service.ITaskLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * 定时任务，订单超过三天短信通知
 */
@Component("overtimeOrderSmsNoticeTask")
public class OvertimeOrderSmsNoticeTask {
    @SuppressWarnings("all")
    private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(OvertimeOrderSmsNoticeTask.class);
    /**
     * 任务日志
     */
    @Autowired
    private ITaskLogService taskLogService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private AliSmsUtil smsUtil;
    @Autowired
    private ISysUserService userService;

    public void settle() {
        TaskLog taskLog = new TaskLog();
        taskLog.setTaskName("订单申请退款超过三天未处理短信通知");
        taskLog.setExecutionTime(LocalDateTime.now());
        try {
            //查询超过三天未处理的订单数据，包括商家以及商家联系电话
            Order order = new Order();
            LocalDateTime minus = LocalDateTime.now().minus(3, ChronoUnit.DAYS);
            order.setUpdateTime(minus);
            List<OvertimeOrderVo> listorder = orderService.overtimeOrder(order);
            //查询平台服务人员联系方式
            if (listorder.size() > 0) {
                //循环数据，给商家发送短信
//                for (OvertimeOrderVo vo:listorder) {
//                    if(StringUtils.isNoneBlank(vo.getServerPhone())){
//                        JSONObject jsonObject = new JSONObject();
//                        jsonObject.put("count",vo.getOrderCount());
//                        log.warn("顾客申请退款三天未处理短信通知商家客服--短信发送参数:" + JSONObject.toJSONString(jsonObject));
//                        log.warn("顾客申请退款三天未处理短信通知商家客服--短信接收手机号:" + vo.getServerPhone());
//                        SendSmsResponse sendSmsResponse = smsUtil.sendSms(vo.getServerPhone(), jsonObject, AliTemplateCode.MERCHANT_REFUND_OVERTIME_CODE);
//                        log.warn("顾客申请退款三天未处理短信通知商家客服--短信返回结果:" + JSONObject.toJSONString(sendSmsResponse));
//                    }else {
//                        log.error("顾客申请退款三天未处理短信通知商家客服--未查询到商家客服电话");
//                    }
//                }
                //查询平台联系电话
                String hotline = sysConfigService.selectConfigByKey("home.complaint.hotline");
                int sum = listorder.stream().mapToInt(OvertimeOrderVo::getOrderCount).sum();
                //给运营人员发送短信
                if (StringUtils.isNotBlank(hotline)) {
                    // 顾客申请退款短信通知平台客服
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("count", sum);
                    log.warn("顾客申请退款三天未处理短信通知平台客服--短信发送参数:" + JSONObject.toJSONString(jsonObject));
                    log.warn("顾客申请退款三天未处理短信通知平台客服--短信接收手机号:" + hotline);
                    SendSmsResponse sendSmsResponse = smsUtil.sendSms(hotline, jsonObject, AliTemplateCode.PLATFORM_REFUND_OVERTIME_CODE);
                    log.warn("顾客申请退款三天未处理短信通知平台客服--短信返回结果:" + JSONObject.toJSONString(sendSmsResponse));
                } else {
                    log.error("顾客申请退款三天未处理短信通知平台客服--未查询到平台客服电话");
                }
                //获得所有平台运营人员信息
                List<SysUser> list = userService.selectOperatorUserList();
                if (list.size() > 0) {
                    for (SysUser user : list) {
                        if (StringUtils.isNotBlank(user.getPhonenumber())) {
                            // 顾客申请退款短信通知平台客服
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("count", sum);
                            log.warn("顾客申请退款三天未处理短信通知运营人员--短信发送参数:" + JSONObject.toJSONString(jsonObject));
                            log.warn("顾客申请退款三天未处理短信通知运营人员--短信接收手机号:" + user.getPhonenumber());
                            SendSmsResponse sendSmsResponse = smsUtil.sendSms(user.getPhonenumber(), jsonObject, AliTemplateCode.OPERATOR_REFUND_OVERTIME_CODE);
                            log.warn("顾客申请退款三天未处理短信通知运营人员--短信返回结果:" + JSONObject.toJSONString(sendSmsResponse));
                        } else {
                            log.error("顾客申请退款三天未处理短信通知运营人员--未查询到运营人员电话");
                        }
                    }
                }

            }
            taskLog.setTaskResult(1);
        } catch (Exception e) {
            log.error("定时任务三天未处理发送短信给运营人员--发送短信通知出现错误!错误信息error=" + e.getMessage());
            taskLog.setTaskResult(0);
            taskLog.setErrorLog(e.getMessage());
        } finally {
            taskLogService.insertTaskLog(taskLog);
        }
    }
}
