package com.qmth.exam.reserve.test;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qmth.exam.reserve.ApiApplication;
import com.qmth.exam.reserve.bean.stdapply.MaterialTitleInfo;
import com.qmth.exam.reserve.bean.stdapply.StudentApplyVO;
import com.qmth.exam.reserve.cache.RedisClient;
import com.qmth.exam.reserve.dao.TimePeriodDao;
import com.qmth.exam.reserve.entity.CategoryEntity;
import com.qmth.exam.reserve.entity.TimePeriodEntity;
import com.qmth.exam.reserve.enums.CategoryLevel;
import com.qmth.exam.reserve.service.CategoryService;
import com.qmth.exam.reserve.service.MaterialGenerateService;
import com.qmth.exam.reserve.util.DateUtil;
import com.qmth.exam.reserve.weixin.OauthAccessTokenRequest;
import com.qmth.exam.reserve.weixin.response.OauthAccessTokenResponseJson;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.redisson.api.RQueue;
import org.redisson.codec.JsonJacksonCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

@SpringBootTest(classes = ApiApplication.class)
@RunWith(SpringRunner.class)
public class ServerTest {

    @Autowired
    private MaterialGenerateService generateService;

    @Autowired
    private TimePeriodDao timePeriodDao;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisClient redisClient;

    // @Test
    public void testRedisQueue() {
        RQueue<Object> queue = redisClient.getRedissonClient().getQueue("test", new JsonJacksonCodec());
        for (int i = 0; i < 10; i++) {
            queue.offer("a" + i);
        }
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            System.out.println(queue.poll());
        }
    }

    // @Test
    public void updateTeachingCapacity() {
        categoryService.updateTeachingCapacity(5L);
    }

    // @Test
    public void testTeaching() {
        LambdaQueryWrapper<CategoryEntity> lm = new LambdaQueryWrapper<>();
        lm.eq(CategoryEntity::getEnable, Boolean.TRUE);
        lm.eq(CategoryEntity::getLevel, CategoryLevel.TEACHING.getValue());
        List<CategoryEntity> categoryList = categoryService.list(lm);
        Map<String, Long> map = categoryList.stream().collect(Collectors.toMap(CategoryEntity::getName, CategoryEntity::getId));
        System.out.println(map);
    }

    // @Test
    public void testTimePeriod() {
        LambdaQueryWrapper<TimePeriodEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TimePeriodEntity::getApplyTaskId, 27);
        List<TimePeriodEntity> timeList = timePeriodDao.selectList(queryWrapper);
        Long longToday = DateUtil.getLongTimeByDate(DateUtil.formatShortSplitDateString(new Date()) + " 00:00:00");
        Date today = new Date(longToday);
        Date otherDay = DateUtil.addValues(today, Calendar.DAY_OF_MONTH, 1);
        Long longOtherDay = DateUtil.getLongTimeByDate(DateUtil.formatShortSplitDateString(otherDay) + " 23:59:59");
        System.out.println("大小：" + timeList.size());
        List<TimePeriodEntity> list = timeList.stream().filter(time -> time.getStartTime() > longOtherDay).collect(Collectors.toList());
        System.out.println(list.size());
    }

    // @Test
    public void testNoFinish() {
        OauthAccessTokenRequest request = new OauthAccessTokenRequest("1212121212121", "wxb501ca68bdeb52df",
                "aca244f836a76bdfa498a26be93e30a1");
        OauthAccessTokenResponseJson response = request.request();
        System.out.println(response.getAccess_token());

        System.out.println(response.getErrcode() + ":" + response.getErrmsg());
    }

    // @Test
    public void testTask() throws Exception {
        MaterialTitleInfo title = new MaterialTitleInfo();
        title.setTaskName("广东开放大学2024年6月期末考试");
        title.setSiteName("东莞开放大学");
        title.setRoomCode("A");
        title.setRoomName("考场01");
        title.setAddress("11栋301教室");
        title.setTimePeriod("2024年6月1日8:00-11:30");

        List<StudentApplyVO> studentList = new ArrayList<>();
        for (int i = 1; i <= 200; i++) {
            StudentApplyVO vo = new StudentApplyVO();
            vo.setName("测试" + i);
            vo.setStudentCode("12345678901234");
            vo.setSeatNumber(StringUtils.leftPad(String.valueOf(i), 3, '0'));
            studentList.add(vo);
        }
        File tempFolder = new File("temp");
        if (!tempFolder.exists()) {
            tempFolder.mkdir();
        }

        File file = generateService.generateSignInForm(title, studentList);
        File toFile = new File("temp/" + System.currentTimeMillis() + ".pdf");
        System.out.println(file.getAbsolutePath() + " " + toFile.getAbsolutePath());
        FileUtils.moveFile(file, toFile);
    }

    // public static void main(String[] args) throws IOException {
    //     System.out.println(DateUtil.getLongTimeByDate("2024-10-23 09:00:00") + "**" + DateUtil.getLongTimeByDate("2024-10-23 12:00:00"));
    //     System.out.println(DateUtil.getLongTimeByDate("2024-10-23 13:00:00") + "**" + DateUtil.getLongTimeByDate("2024-10-23 17:00:00"));
    //     System.out.println(DateUtil.getLongTimeByDate("2024-10-24 09:00:00") + "**" + DateUtil.getLongTimeByDate("2024-10-24 12:00:00"));
    //     System.out.println(DateUtil.getLongTimeByDate("2024-10-24 13:00:00") + "**" + DateUtil.getLongTimeByDate("2024-10-24 17:00:00"));
    // }

}
