package com.ktg.iscs.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.pojo.CommonResult;
import com.ktg.common.utils.FingerprintComparisonByImg;
import com.ktg.common.utils.MessageUtils;
import com.ktg.common.utils.poi.ExcelUtil;
import com.ktg.common.vo.VerificationVO;
import com.ktg.iscs.domain.TestIscs;
import com.ktg.iscs.service.IIsTicketOperLogService;
import com.ktg.iscs.service.ITestIscsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * 测试用Controller
 *
 * @author cgj
 * @date 2024-12-18
 */
@Api(tags = "测试用")
@RestController
@RequestMapping("/iscs/iscs")
public class TestIscsController extends BaseController {
    @Autowired
    private ITestIscsService testIscsService;
    @Autowired
    private IIsTicketOperLogService isTicketOperLogService;

    /**
     * 查询测试用分页
     */
    @ApiOperation("查询测试用-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "testIscs", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:iscs:list')")
    @GetMapping("/getTestIscsPage")
    public CommonResult<Page<TestIscs>> getTestIscsPage(Page<TestIscs> page, TestIscs testIscs) {
        Page<TestIscs> result = testIscsService.page(page, Wrappers.<TestIscs>lambdaQuery()
                .orderByDesc(TestIscs::getId));
        return CommonResult.success(result);
    }

    /**
     * 导出测试用列表
     */
    @ApiOperation("导出测试用列表")
    @Parameter(name = "testIscs", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:iscs:export')")
    @Log(title = "测试用", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTestIscs")
    public void exportTestIscs(HttpServletResponse response, TestIscs testIscs) {
        Page<TestIscs> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(1);
        List<TestIscs> list = testIscsService.page(page, Wrappers.<TestIscs>lambdaQuery()
                .orderByDesc(TestIscs::getId)).getRecords();
        ExcelUtil<TestIscs> util = new ExcelUtil<TestIscs>(TestIscs.class);
        util.exportExcel(response, list, "测试用数据");
    }

    /**
     * 获取测试用详细信息
     */
    @ApiOperation("获取测试用详细信息")
    @Parameter(name = "id", description = "id")
    @PreAuthorize("@ss.hasPermi('iscs:iscs:query')")
    @GetMapping(value = "/selectTestIscsById")
    public CommonResult<TestIscs> selectTestIscsById(Long id) {
        return CommonResult.success(testIscsService.getById(id));
    }

    /**
     * 新增测试用
     */
    @ApiOperation("新增测试用")
    @PreAuthorize("@ss.hasPermi('iscs:iscs:add')")
    @Log(title = "测试用", businessType = BusinessType.INSERT)
    @PostMapping("/insertTestIscs")
    public CommonResult<Boolean> insertTestIscs(@RequestBody @Parameter(name = "testIscs", description = "新增数据类,放到body") TestIscs testIscs) {
        return CommonResult.success(testIscsService.save(testIscs));
    }

    /**
     * 修改测试用
     */
    @ApiOperation("修改测试用")
    @PreAuthorize("@ss.hasPermi('iscs:iscs:edit')")
    @Log(title = "测试用", businessType = BusinessType.UPDATE)
    @PostMapping("/updateTestIscs")
    public CommonResult<Boolean> updateTestIscs(@RequestBody @Parameter(name = "testIscs", description = "修改数据类,放到body") TestIscs testIscs) {
        return CommonResult.success(testIscsService.updateById(testIscs));
    }

    /**
     * 删除测试用
     */
    @ApiOperation("删除测试用")
    @PreAuthorize("@ss.hasPermi('iscs:iscs:remove')")
    @Log(title = "测试用", businessType = BusinessType.DELETE)
    @PostMapping("/deleteTestIscsByIds")
    public CommonResult<Boolean> deleteTestIscsByIds(String ids) {
        Assert.notBlank(ids, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(ids);
        return CommonResult.success(testIscsService.removeBatchByIds(Arrays.asList(longIds)));
    }

    @ApiOperation("指纹测试-----------")
    @GetMapping(value = "/testFingerprint")
    public CommonResult<VerificationVO> testFingerprint(String a, String b) {
        // a = "C:\\Users\\车车\\Desktop\\指纹\\6.png";
        // b = "C:\\Users\\车车\\Desktop\\指纹\\1.jpg,C:\\Users\\车车\\Desktop\\指纹\\2.jpg,C:\\Users\\车车\\Desktop\\指纹\\3.jpg,C:\\Users\\车车\\Desktop\\指纹\\4.jpg,C:\\Users\\车车\\Desktop\\指纹\\5.png,C:\\Users\\车车\\Desktop\\指纹\\6.png,C:\\Users\\车车\\Desktop\\指纹\\7.jpg";
        String[] strArray = Convert.toStrArray(b);
        List<String> list = Arrays.asList(strArray);
        VerificationVO verificationVO = FingerprintComparisonByImg.completableFutureComparison(a, new HashSet<>(list));
        return CommonResult.success(verificationVO);
    }

    @ApiOperation("测试websocket-----------")
    @GetMapping(value = "/testWebsocketLog")
    public CommonResult<Boolean> testWebsocketLog(Long id, String name, String logType) {
        if (logType.equals("启动作业")) {
            Boolean b = isTicketOperLogService.addLog1(id, name);
        }
        if (logType.equals("启动上锁")) {
            Boolean b = isTicketOperLogService.addLog4(id, name);
        }

        return CommonResult.success(true);
    }


   /* @ApiOperation("测试生成离线虹软所需硬件信息包-----------")
    @GetMapping(value = "/testPrintSystem")
    public CommonResult<String> testWebsocketLog() {

        //人脸识别引擎库存放路径
        FaceEngine faceEngine = new FaceEngine("D:\\work\\app\\appinstall\\ArcSoft_ArcFacePro_windows_x64_java_V4.2\\libs\\WIN64");
        ActiveDeviceInfo activeDeviceInfo = new ActiveDeviceInfo();
        //采集设备信息（可离线）
        int errorCode = faceEngine.getActiveDeviceInfo(activeDeviceInfo);
        System.out.println("采集设备信息errorCode:" + errorCode);
        String deviceInfo = activeDeviceInfo.getDeviceInfo();
        System.out.println("设备信息:" + deviceInfo);
        return CommonResult.success("设备信息:" + deviceInfo);
    }*/


    @ApiOperation("测试多语言-----------")
    @GetMapping(value = "/testLanguage")
    public CommonResult<Boolean> testLanguage() {
        Assert.notNull(null, MessageUtils.message("no.exit.key"));

        return CommonResult.success(true);
    }

}
