package com.fs.store.controller;

import com.fs.FSAdminApplication;
import com.fs.common.core.domain.R;
import com.fs.store.domain.FsCouponSchedule;
import com.fs.store.domain.FsMiniprogramSubNotifyTask;
import com.fs.store.domain.FsStorePayment;
import com.fs.store.enums.IcgProcessStatusEnum;
import com.fs.store.mapper.FsMiniprogramSubNotifyTaskMapper;
import com.fs.store.service.IFsCouponScheduleService;
import com.fs.task.MiniProgramSubTask;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = FSAdminApplication.class)
@RequiredArgsConstructor
@Slf4j
public class FsStorePaymentControllerTest {

    @Autowired
    private FsStorePaymentController fsStorePaymentController;

    @Autowired
    private IFsCouponScheduleService fsCouponScheduleService;

    @Autowired
    private MiniProgramSubTask miniProgramSubTask;
    @Test
    public void returnPayStatus() throws Exception {

        Authentication authentication = new UsernamePasswordAuthenticationToken(
                "admin",
                "sadmin#258",
                Collections.singletonList(new SimpleGrantedAuthority("ADMIN"))
        );

        // Set the Authentication in the SecurityContext
        SecurityContextHolder.getContext().setAuthentication(authentication);


        FsStorePayment fsStorePayment = new FsStorePayment();
        fsStorePayment.setPaymentId(01L);
        fsStorePayment.setStatus(1);
        fsStorePayment.setOrderId(1899279950347763712L);
        fsStorePayment.setPayTypeCode("weixin");
        fsStorePayment.setPayMoney(new BigDecimal("0.01"));
        fsStorePayment.setPayTime(new Date());
        fsStorePayment.setTradeNo("002900TOP3B250311101453P779ac1369bc00000");
        fsStorePayment.setUserId(520391L);
        fsStorePayment.setOpenId("obmfC6y5ZOuvq9b4UlL7GUayuYQE");
        fsStorePayment.setCompanyId(12L);
        fsStorePayment.setCompanyUserId(42L);
        fsStorePayment.setDeptId(144L);
        fsStorePayment.setBankSerialNo("1899279950347763712L");
        fsStorePayment.setPayMode("hf");
        R r = fsStorePaymentController.returnPayStatus(fsStorePayment);
    }

    @Test
    public void test() throws Exception {
        FsCouponSchedule fsCouponSchedule = new FsCouponSchedule();
        fsCouponSchedule.setOrderId(1899279950347763712L);
        fsCouponSchedule.setSetmealId(1800L);
        fsCouponSchedule.setSetmealTitle("甘草干姜汤（北京同仁堂）1盒");
        fsCouponSchedule.setMonth(4L);
        fsCouponSchedule.setCount(0L);
        fsCouponSchedule.setStatus(IcgProcessStatusEnum.PENDING.getCode());
        fsCouponSchedule.setOrderTime(LocalDateTime.now());
        fsCouponSchedule.setUserId(520391L);
        // 如果上次发送时间为空，那么预计发送时间等于=下单时间+1月
        fsCouponSchedule.setSendTime(fsCouponSchedule.getOrderTime().plusMonths(1L));
        fsCouponSchedule.setRetryCount(0);

        fsCouponScheduleService.insertFsCouponSchedule(fsCouponSchedule);
    }

    @Test
    public void issueCoupon() throws Exception {
        fsCouponScheduleService.issueCoupon();
    }

    @Test
    public void notifyMiniAppSub(){
        miniProgramSubTask.notifyMiniAppSub();
    }


}
