package com.liquidnet.service.dragon.channel.strategy.biz;

import com.liquidnet.commons.lang.util.BeanUtil;
import com.liquidnet.commons.lang.util.HttpUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.service.base.SqlMapping;
import com.liquidnet.service.dragon.bo.PayNotifyReqBo;
import com.liquidnet.service.dragon.constant.DragonConstant;
import com.liquidnet.service.dragon.dto.DragonOrdersDto;
import com.liquidnet.service.dragon.dto.DragonPayBaseReqDto;
import com.liquidnet.service.dragon.dto.DragonPayBaseRespDto;
import com.liquidnet.service.dragon.dto.PayNotifyDto;
import com.liquidnet.service.dragon.entity.DragonOrders;
import com.liquidnet.service.dragon.utils.DataUtils;
import com.liquidnet.service.dragon.utils.MqHandleUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;

/**
 * @author AnJiabin <anjiabin@zhengzai.tv>
 * @version V1.0
 * @Description: TODO
 * @class: DragonPayBiz
 * @Package com.liquidnet.service.dragon.channel.strategy.biz
 * @Copyright: LightNet @ Copyright (c) 2021
 * @date 2021/7/14 14:12
 */
@Slf4j
public abstract class DragonPayBiz {
    @Autowired
    private MqHandleUtil mqHandleUtil;

    @Autowired
    private DataUtils dataUtils;

    public DragonOrders buildPayOrders(DragonPayBaseReqDto dragonPayBaseReqDto, DragonPayBaseRespDto respDto){
        //构造订单
        DragonOrders orders = new DragonOrders();
//        orders.setMid();
        orders.setOrderId(IDGenerator.nextTimeId());
        orders.setStatus(Integer.valueOf(DragonConstant.PayStatusEnum.STATUS_UNPAID.getCode()));
        orders.setCode(respDto.getCode());
        orders.setType(dragonPayBaseReqDto.getType());
        orders.setPrice(dragonPayBaseReqDto.getPrice());
        orders.setName(dragonPayBaseReqDto.getName());
        orders.setDetail(dragonPayBaseReqDto.getDetail());
        orders.setOrderCode(dragonPayBaseReqDto.getOrderCode());
        orders.setClientIp(dragonPayBaseReqDto.getClientIp());
        orders.setNotifyUrl(dragonPayBaseReqDto.getNotifyUrl());
        orders.setPaymentType((dragonPayBaseReqDto.getDeviceFrom()+dragonPayBaseReqDto.getPayType()).toUpperCase());
//        orders.setPaymentId();
        orders.setPaymentAt(LocalDateTime.now());
//        orders.setFinishedAt();
        orders.setCreatedAt(LocalDateTime.now());
//        orders.setUpdatedAt();

        DragonOrdersDto ordersDto = new DragonOrdersDto();
        BeanUtil.copy(orders,ordersDto);
        //放到redis缓存中
        dataUtils.createPayOrder(orders.getOrderCode(),orders.getCode(),ordersDto);
        // 持久化到数据库
        boolean insertResult = mqHandleUtil.sendMySqlRedis(
                SqlMapping.get("dragon_orders.insert"),
                new Object[]{orders.getOrderId(),orders.getStatus(), orders.getCode(), orders.getType()
                        , orders.getPrice(), orders.getName(), orders.getDetail()
                        , orders.getOrderCode(), orders.getClientIp()
                        , orders.getNotifyUrl(), orders.getPaymentType(),
                        orders.getPaymentId(), orders.getPaymentAt()
                        , orders.getFinishedAt(), orders.getCreatedAt()
                        , orders.getUpdatedAt(),orders.getDeletedAt()}
                        ,DragonConstant.MysqlRedisQueueEnum.DRAGON_PAY_KEY.getCode()
        );
        return orders;
    }

     public PayNotifyReqBo buildPayNotifyReqBo(DragonOrdersDto dragonOrdersDto){
        PayNotifyReqBo payNotifyReqBo = new PayNotifyReqBo();
        payNotifyReqBo.setNotifyUrl(dragonOrdersDto.getNotifyUrl());
        PayNotifyDto payNotifyDto = new PayNotifyDto();
        payNotifyDto.setStatus(dragonOrdersDto.getStatus());
        payNotifyDto.setType(dragonOrdersDto.getType());
        payNotifyDto.setCode(dragonOrdersDto.getCode());
        payNotifyDto.setPaymentId(dragonOrdersDto.getPaymentId());
        payNotifyDto.setOrderCode(dragonOrdersDto.getOrderCode());
        payNotifyDto.setPrice(dragonOrdersDto.getPrice());
        payNotifyDto.setPaymentType(dragonOrdersDto.getPaymentType());
        payNotifyReqBo.setPayNotifyDto(payNotifyDto);
        return payNotifyReqBo;
    }

    public void createDragonOrderLogs(String paymentType,String content){
        try {
            String orderId = IDGenerator.nextTimeId();
            LocalDateTime createAt = LocalDateTime.now();
            LocalDateTime updateAt = null;
            LocalDateTime deleteAt = null;
            boolean insertResult = mqHandleUtil.sendMySqlRedis(
                    SqlMapping.get("dragon_order_logs.insert"),
                    new Object[]{orderId,paymentType,content, createAt, updateAt,deleteAt}
                    ,DragonConstant.MysqlRedisQueueEnum.DRAGON_PAY_NOTIFY_KEY.getCode()
            );
        } catch (Exception e) {
            log.error("dragon:createOrderLog:error msg:{}",e);
            e.printStackTrace();
        }
    }

    public void sendNotify(PayNotifyReqBo payNotifyReqBo){
        PayNotifyDto payNotifyDto = payNotifyReqBo.getPayNotifyDto();
        LocalDateTime nowTime = LocalDateTime.now();
        MultiValueMap<String, String> params = new LinkedMultiValueMap();
        params.add("status", payNotifyDto.getStatus().toString());
        params.add("type", payNotifyDto.getType());
        params.add("code", payNotifyDto.getCode());
        params.add("paymentId", payNotifyDto.getPaymentId());
        params.add("orderCode", payNotifyDto.getOrderCode());
        params.add("price", payNotifyDto.getPrice().toString());
        params.add("paymentType", payNotifyDto.getPaymentType());
        String response = HttpUtil.post(payNotifyReqBo.getNotifyUrl(), params);
        log.debug("PAY RESPONSE=" + response);
        if (response.equals("success")) {

        } else {
//            sendMySqlRedis(
//                    SqlMapping.get("dragon_order_pay.update"),
//                    new Object[]{nowTime, DateUtil.Formatter.yyyyMMddHHmmss.format(nowTime), DragonConstant.RefundStatusEnum.STATUS_FAIL.getCode(), notifyUrlDto.getRefundCode()}
//            );
        }
    }

    public String getPaymentType(String payType,String deviceFrom){
        return (deviceFrom+payType).toUpperCase();
    }
}
