package com.fdkankan.ucenter.service.impl;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.ucenter.entity.IncrementAutoOrder;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.entity.UserIncrement;
import com.fdkankan.ucenter.mapper.IIncrementAutoOrderMapper;
import com.fdkankan.ucenter.pay.paypal.sdk.AutoPaypalVo;
import com.fdkankan.ucenter.pay.strategy.impl.IncrementOrderImpl;
import com.fdkankan.ucenter.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2023-11-09
 */
@Service
public class IncrementAutoOrderServiceImpl extends ServiceImpl<IIncrementAutoOrderMapper, IncrementAutoOrder> implements IIncrementAutoOrderService {

    @Autowired
    IUserIncrementService userIncrementService;
    @Autowired
    IIncrementOrderService incrementOrderService;
    @Autowired
    IncrementOrderImpl incrementOrder;
    @Autowired
    IMailTemplateService mailTemplateService;
    @Autowired
    IUserService userService;

    @Override
    public void addOrder(Long userId, Integer incrementTypeId, Long incrementId, String orderSn,String agentKey) {
        IncrementAutoOrder autoOrder = new IncrementAutoOrder();
        autoOrder.setUserId(userId);
        autoOrder.setIncrementTypeId(incrementTypeId);
        autoOrder.setUserIncrementId(incrementId);
        autoOrder.setOrderSn(orderSn);
        autoOrder.setAgentKey(agentKey);
        this.save(autoOrder);
    }

    @Override
    public IncrementAutoOrder getByIncrementId(Long incrementId) {
        LambdaQueryWrapper<IncrementAutoOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IncrementAutoOrder::getUserIncrementId,incrementId);
        return this.getOne(wrapper);
    }

    @Override
    public void subOrder(String orderSn,String subscriptionId) {
        LambdaUpdateWrapper<IncrementAutoOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(IncrementAutoOrder::getOrderSn,orderSn);
        wrapper.set(IncrementAutoOrder::getSubscriptionId,subscriptionId);
        this.update(wrapper);
    }

    @Override
    public IncrementAutoOrder getBySubId(String subscriptionId) {
        LambdaUpdateWrapper<IncrementAutoOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(IncrementAutoOrder::getSubscriptionId,subscriptionId);
        return this.getOne(wrapper);
    }

    @Override
    public void paySuccess(IncrementAutoOrder incrementAutoOrder, AutoPaypalVo order) throws Exception {
        incrementOrderService.addNewOrderByOrder(incrementAutoOrder,order);

        incrementOrder.handleOrder(order.getOrderSn(),order.getTradeNo(),order.getOrderSn(),order.getPayType(),null);

    }

    @Override
    public void payError(IncrementAutoOrder incrementAutoOrder, AutoPaypalVo order) {
        User user = userService.getById(incrementAutoOrder.getUserId());
        if(user != null){
            List<UserIncrement> userIncrements = userIncrementService.getByAutoOrderSn(order.getSubscriptionId());
            if(userIncrements.size() >0){
                mailTemplateService.sendPayErrorMail(user.getUserName(),userIncrements.get(0).getMemberLevels());
            }
        }
    }

    @Override
    public void subscriptionCancelled(IncrementAutoOrder incrementAutoOrder, AutoPaypalVo order) {
        List<UserIncrement> userIncrements = userIncrementService.getByAutoOrderSn(order.getSubscriptionId());
        for (UserIncrement increment : userIncrements) {
            userIncrementService.cancelSubscriptions(increment);
            User user = userService.getById(increment.getUserId());
            if(user != null){
                mailTemplateService.sendSubcriptionCancelledMail(user.getUserName(),increment.getMemberLevels());
            }
        }
        this.removeById(incrementAutoOrder.getId());
    }

    @Override
    public void setIncrementId(IncrementAutoOrder incrementAutoOrder, Long incrmentId) {
        if(incrementAutoOrder != null){
            LambdaUpdateWrapper<IncrementAutoOrder> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(IncrementAutoOrder::getId,incrementAutoOrder.getId());
            wrapper.set(IncrementAutoOrder::getUserIncrementId,incrmentId);
            this.update(wrapper);
        }
    }

    @Override
    public void delByIncrementId(Long incrementId) {
        LambdaUpdateWrapper<IncrementAutoOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(IncrementAutoOrder::getUserIncrementId,incrementId);
        this.remove(wrapper);
    }

    @Override
    public IncrementAutoOrder getByOrderSn(String orderSn) {
        LambdaUpdateWrapper<IncrementAutoOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(IncrementAutoOrder::getOrderSn,orderSn);
        List<IncrementAutoOrder> list = this.list(wrapper);
        if(list == null || list.size()<=0){
            return null;
        }
        return list.get(0);
    }

    @Override
    public IncrementAutoOrder getDelOrder(String subscriptionOrderSn) {
        return getBaseMapper().getDelOrder(subscriptionOrderSn);
    }

    @Override
    public void updateDelStatus(Integer id) {
        getBaseMapper().updateDelStatus(id);
    }
}
