package com.tuoheng.admin.service;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tuoheng.admin.entity.InspectionFile;
import com.tuoheng.admin.enums.FileTypeEnum;
import com.tuoheng.admin.enums.QuestionEnum;
import com.tuoheng.admin.enums.SourceEnum;
import com.tuoheng.admin.enums.SourcesEnum;
import com.tuoheng.admin.mapper.InspectionFileMapper;
import com.tuoheng.admin.request.inspectionfile.QueryInspectionFilePageListRequest;
import com.tuoheng.admin.service.inspectionfile.IInspectionFileService;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
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.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class InspectionFileServiceTest {

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    @Autowired
    private IInspectionFileService iInspectionFileService;

    @Test
    public void testPageList() {
        QueryInspectionFilePageListRequest request = new QueryInspectionFilePageListRequest();
        request.setKey("");
        request.setQuestionCode("1");
        request.setStatus(15);
        request.setDeptId("");
        request.setBeginTime(DateUtils.now());
        request.setEndTime(DateUtils.now());
        request.setPage(1);
        request.setLimit(2);

        JSONObject json = (JSONObject) JSONObject.toJSON(request);
        log.info("-----> 获取分页列表请求参数：json={}", json);

        JsonResult result = iInspectionFileService.getPageList(request);
        System.out.println(">>>>>>>>>>>>>" + result);
    }

    @Test
    public void testAddList() {
        List<InspectionFile> thirstyQuestionFiles = new ArrayList<>();
        InspectionFile inspectionFile;
        for (int i = 1; i < 4; i++) {
            inspectionFile = new InspectionFile();
            inspectionFile.setQuestionCode(i + "");
            inspectionFile.setTenantId("1");
            inspectionFile.setInspectionId("1");
            // 文件类型
            inspectionFile.setFileType(FileTypeEnum.IMAGE.getCode());
            // 文件编码
            inspectionFile.setFileCode(inspectionFile.getFileCode());
            // 文件名称
            inspectionFile.setFileName(inspectionFile.getFileName());
            //图片大小，单位MB，保留两位小数，目前没有获取图片大小的字段
            inspectionFile.setFileSize(0.00);

            // 问题图片审核状态
            inspectionFile.setStatus(QuestionEnum.NOTREVIEWED.getCode());
            // 问题图片来源
            inspectionFile.setSource(SourceEnum.AI.getCode());
            inspectionFile.setCreateUser("1"); // 设置默认用户
            inspectionFile.setCreateTime(DateUtils.now());
            thirstyQuestionFiles.add(inspectionFile);
        }

        String id = RandomUtil.randomString(36);
        System.out.println(id);
        System.out.println(id.length());

//        CommonUtils.batchOperate((x) -> inspectionFileMapper.addBatch(x), thirstyQuestionFiles, 1000);
    }

    @Test
    public void testUpdate() {
        //查询当前问题
        InspectionFile entity = inspectionFileMapper.selectOne(new LambdaQueryWrapper<InspectionFile>()
                .eq(InspectionFile::getId, "5l7pr9kuhfiuf8opi0h79qy2ghv6kb5093yq")
                .eq(InspectionFile::getMark, 1));

        LambdaUpdateWrapper<InspectionFile> qw = new LambdaUpdateWrapper<>();
        qw.eq(InspectionFile::getId, entity.getId());
        qw.eq(InspectionFile::getMark, 1);
        qw.set(InspectionFile::getStatus, 15);
        qw.set(InspectionFile::getCheckUser, "aa");
        qw.set(InspectionFile::getCheckTime, DateUtils.now());
        int count = inspectionFileMapper.update(entity, qw);
    }
}
