package com.qmth.distributed.print;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.distributed.print.business.bean.dto.examObject.ExamObjectDto;
import com.qmth.distributed.print.business.bean.marking.CardJpgResult;
import com.qmth.distributed.print.business.bean.result.TemplatePrintInfoResult;
import com.qmth.distributed.print.business.entity.ExamCardDetail;
import com.qmth.distributed.print.business.entity.ExamPrintPlan;
import com.qmth.distributed.print.business.entity.ExamStudent;
import com.qmth.distributed.print.business.enums.MessageEnum;
import com.qmth.distributed.print.business.mapper.ExamStudentMapper;
import com.qmth.distributed.print.business.service.*;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicAttachment;
import com.qmth.teachcloud.common.entity.BasicCourse;
import com.qmth.teachcloud.common.entity.BasicPrintConfig;
import com.qmth.teachcloud.common.service.BasicCourseService;
import com.qmth.teachcloud.common.service.BasicVerifyCodeService;
import com.qmth.teachcloud.common.util.ConvertUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: service层测试
 * @Author: CaoZixuan
 * @Date: 2021-04-05
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class ServiceTest {
    @Resource
    private ExamPrintPlanService examPrintPlanService;
    @Resource
    private BasicTemplateOrgService basicTemplateOrgService;
    @Resource
    private ExamDetailService examDetailService;
    @Resource
    private BasicMessageService basicMessageService;
    @Resource
    private BasicVerifyCodeService basicVerifyCodeService;
    @Resource
    private BasicCourseService basicCourseService;
    @Resource
    private ExamTaskService examTaskService;
    @Resource
    private PrintCommonService printCommonService;
    @Resource
    private ExamCardDetailService examCardDetailService;
    @Resource
    private BasicPrintConfigService basicPrintConfigService;
    @Resource
    private ExamStudentMapper examStudentMapper;
    @Resource
    private ExamPaperStructureService examPaperStructureService;


    @Test
    public void printPlanTemplateDatasource() {
        examPrintPlanService.printPlanTemplateDatasource(1L, 2L);
    }

    @Test
    public void mapper() {
        List<Long> ids = new ArrayList<>();
        ids.add(1L);
        ids.add(9L);
        List<TemplatePrintInfoResult> result = basicTemplateOrgService.findTemplateInfoByOrgIds(ids);
        for (TemplatePrintInfoResult templatePrintInfoResult : result) {
            System.out.println(templatePrintInfoResult);
        }
    }

    @Test
    public void readExcelTest() {
        examDetailService.findExaminationFields(101L);
    }

    @Test
    public void date() {
        Long startTime = 1618308000000L;
        Long endTime = 1618315200000L;

        Map<String, Object> map = ConvertUtil.analyzeDateAndTime(startTime, endTime);
        String date = String.valueOf(map.get("date"));
        String time = String.valueOf(map.get("time"));
        System.out.println("date = " + date);
        System.out.println("time = " + time);

    }

    @Test
    public void sendMessage() {
        Long userId = 1L;
        String mobileNumber = "18903719928";
        Long businessId = 1L;
        Map<String, String> jsonMap = new HashMap<>();
        jsonMap.put("userName", "李四");
        jsonMap.put("courseName", "数学");
        jsonMap.put("paperNumber", "SX001");
        String variableParams = JSON.toJSONString(jsonMap);
//        String variableParams = "{\"code\":\"" + "123456" + "\"}";
        Long createId = 1L;
        MessageEnum messageType = MessageEnum.NOTICE_OF_AUDIT_PASS;

//        basicMessageService.saveMessageSendLog(userId, "李四", mobileNumber, businessId, variableParams, createId, messageType, null);
    }

    @Test
    public void sendVerifyCode() {
        basicVerifyCodeService.sendVeirfyCode("18903719928", null);
    }

    @Test
    public void like() {
        QueryWrapper<BasicCourse> queryWrapper = new QueryWrapper<>();
        String name = "_";
        queryWrapper.lambda().like(BasicCourse::getName, SystemConstant.translateSpecificSign(name));
        List<BasicCourse> basicCourseList = basicCourseService.list(queryWrapper);
        System.out.println(JSON.toJSONString(basicCourseList));
    }

    @Test
    public void findByExamDetailId(){
        Long examDetailId = 1L;
        ExamPrintPlan examPrintPlan = examPrintPlanService.findByExamDetailId(examDetailId);
        System.out.println(JSON.toJSONString(examPrintPlan));
    }

    @Test
    public void approveForm(){
        Long examTask = 167675409079468032L;
        System.out.println(JSON.toJSONString(examTaskService.findExamTaskApprovalForm(examTask)));
    }

    @Test
    public void saveHtmlAndPdf() throws IOException {
        Long id = 165147096339447808L;
        ExamCardDetail examCardDetail = examCardDetailService.getById(id);
        String htmlContent = examCardDetail.getHtmlContent();
        BasicAttachment basicAttachment = printCommonService.saveAttachmentHtmlAndPdf("那不勒斯",htmlContent,1L);
        System.out.println(JSON.toJSONString(basicAttachment));
    }

    @Test
    public void findExamObject(){
        String paperNumber = "cn004-0906-001";
        ExamObjectDto examObjectDto = examTaskService.findExamObjectDtoByPaperNumber(paperNumber);
        System.out.println(JSON.toJSONString(examObjectDto));
    }

    @Test
    public void findBasicPrintConfig(){
        Long examId = 228554176387354624L;
        String courseCode = "gd-gdsx";
//        BasicPrintConfig basicPrintConfig = basicPrintConfigService.getByExamIdAndCourseCode(examId,courseCode);
        BasicPrintConfig basicPrintConfig = basicPrintConfigService.getById(228554329928241152L);
        List<String> tempList = JSONObject.parseArray(basicPrintConfig.getPrintContent(), String.class);
        for (String s : tempList) {
            System.out.println(s);
        }
        String var = basicPrintConfig.getVariableContent();
        System.out.println(var);
    }

    @Test
    public void findExamStudent(){
        Long schoolId = 2L;
        String clazzId = "225944699725156352";
        List<ExamStudent> examStudentList = examStudentMapper.listExamStudentBySchoolIdAndClazzId(schoolId,clazzId);
        System.out.println(JSON.toJSONString(examStudentList));
    }

    @Test
    public void findCardJpgFileByPaperNumber(){
        List<CardJpgResult> resultList = examPaperStructureService.findCardJpgFileByPaperNumber(237292182900310016L,"yznwl","yznwl-0415-001");
        System.out.println(JSON.toJSONString(resultList));
    }

}
