package com.ktg.web.controller.system;

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.poi.ExcelUtil;
import com.ktg.common.vo.FaceCutVO;
import com.ktg.framework.config.ServerConfig;
import com.ktg.iscs.domain.IsSystemAttribute;
import com.ktg.iscs.service.IIsSystemAttributeService;
import com.ktg.system.domain.SysUserCharacteristic;
import com.ktg.system.service.ISysUserCharacteristicService;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用户特征(指纹、面部)Controller
 *
 * @author cgj
 * @date 2025-03-10
 */
@Api(tags = "用户特征(指纹、面部)")
@Slf4j
@RestController
@RequestMapping("/system/user/characteristic")
public class SysUserCharacteristicController extends BaseController
{
    @Autowired
    private ISysUserCharacteristicService sysUserCharacteristicService;
    @Autowired
    private IIsSystemAttributeService isSystemAttributeService;
    @Autowired
    private ServerConfig serverConfig;

    @ApiOperation("查询用户特征(指纹、面部)-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "sysUserCharacteristic", description = "实体参数")
    })
    @GetMapping("/getSysUserCharacteristicPage")
    public CommonResult<Page<SysUserCharacteristic>> getSysUserCharacteristicPage(Page<SysUserCharacteristic> page, SysUserCharacteristic sysUserCharacteristic)
    {
        Page<SysUserCharacteristic> result = sysUserCharacteristicService.getSysUserCharacteristicPage(page, sysUserCharacteristic);
        return CommonResult.success(result);
    }

    @ApiOperation("导出用户特征(指纹、面部)列表")
    @Parameter(name = "sysUserCharacteristic", description = "实体参数")
    @Log(title = "用户特征(指纹、面部)", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSysUserCharacteristic")
    public void exportSysUserCharacteristic(HttpServletResponse response, SysUserCharacteristic sysUserCharacteristic)
    {
        Page<SysUserCharacteristic> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(1);
        List<SysUserCharacteristic> list = sysUserCharacteristicService.page(page, Wrappers.<SysUserCharacteristic>lambdaQuery()
                .orderByDesc(SysUserCharacteristic::getRecordId)).getRecords();
        ExcelUtil<SysUserCharacteristic> util = new ExcelUtil<SysUserCharacteristic>(SysUserCharacteristic.class);
        util.exportExcel(response, list, "用户特征(指纹、面部)数据");
    }

    @ApiOperation("获取用户特征(指纹、面部)详细信息")
    @Parameter(name = "recordId", description = "recordId")
    @GetMapping(value = "/selectSysUserCharacteristicById")
    public CommonResult<SysUserCharacteristic> selectSysUserCharacteristicById(Long recordId)
    {
        return CommonResult.success(sysUserCharacteristicService.getById(recordId));
    }

    @ApiOperation("新增指纹录入-指纹图片转成dat存储")
    // @PreAuthorize("@ss.hasPermi('iscs:characteristic:add')")
    @Log(title = "新增指纹录入-指纹图片转成dat存储", businessType = BusinessType.INSERT)
    @PostMapping("/insertUserFingerprintDat")
    public CommonResult<Long> insertUserFingerprintDat(MultipartFile file, String userName, String group) throws IOException {
        IsSystemAttribute one = isSystemAttributeService.getIsSystemAttributeByKey("sys.fingerprint.limit");
        String sysAttrValue = null;
        if (one != null) {
            sysAttrValue = one.getSysAttrValue();
        }
        String url = serverConfig.getUrl();
        return CommonResult.success(sysUserCharacteristicService.insertUserFingerprintDat(file, userName, sysAttrValue, url, group));
        // return CommonResult.success(sysUserCharacteristicService.insertUserFace(file, userName, sysAttrValue, url));
    }
    @ApiOperation("修改用户特征(指纹、面部)")
    @Log(title = "用户特征(指纹、面部)", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSysUserCharacteristic")
    public CommonResult<Boolean> updateSysUserCharacteristic(@RequestBody @Parameter(name = "sysUserCharacteristic", description = "修改数据类,放到body") SysUserCharacteristic sysUserCharacteristic)
    {
        return CommonResult.success(sysUserCharacteristicService.updateById(sysUserCharacteristic));
    }

    @ApiOperation("删除用户特征(指纹、面部)")
    @Log(title = "用户特征(指纹、面部)", businessType = BusinessType.DELETE)
	@PostMapping("/deleteSysUserCharacteristicByRecordIds")
    public CommonResult<Boolean> deleteSysUserCharacteristicByRecordIds(String recordIds)
    {
        return CommonResult.success(sysUserCharacteristicService.deleteSysUserCharacteristicByRecordIds(recordIds));
    }

    /*@ApiOperation("删除用户特征(指纹、面部)")
    @PreAuthorize("@ss.hasPermi('iscs:characteristic:remove')")
    @Log(title = "用户特征(指纹、面部)", businessType = BusinessType.DELETE)
    @GetMapping("/removeSysUserCharacteristicByRecordIds")
    public CommonResult<Boolean> removeSysUserCharacteristicByRecordIds(String recordIds)
    {
        return CommonResult.success(sysUserCharacteristicService.deleteSysUserCharacteristicByRecordIds(recordIds));
    }*/

    @ApiOperation("新增人脸录入-人脸识别后裁剪存储-opencv")
    // @PreAuthorize("@ss.hasPermi('iscs:characteristic:add')")
    @Log(title = "新增人脸录入-人脸识别后裁剪存储", businessType = BusinessType.INSERT)
    @PostMapping("/insertUserCutFace")
    public CommonResult<FaceCutVO> insertUserCutFace(MultipartFile file, String userName) throws IOException {
        IsSystemAttribute one = isSystemAttributeService.getIsSystemAttributeByKey("sys.face.limit");
        String sysAttrValue = null;
        if (one != null) {
            sysAttrValue = one.getSysAttrValue();
        }
        String url = serverConfig.getUrl();
        return CommonResult.success(sysUserCharacteristicService.insertUserCutFace(file, userName, sysAttrValue, url));
    }

    @ApiOperation("新增人脸录入-人脸识别后裁剪存储-arcsoft")
    // @PreAuthorize("@ss.hasPermi('iscs:characteristic:add')")
    @Log(title = "新增人脸录入-人脸识别后裁剪存储-arcsoft", businessType = BusinessType.INSERT)
    @PostMapping("/insertUserFace")
    public CommonResult<FaceCutVO> insertUserFace(MultipartFile file, String userName) throws IOException {
        IsSystemAttribute one = isSystemAttributeService.getIsSystemAttributeByKey("sys.face.limit");
        String sysAttrValue = null;
        if (one != null) {
            sysAttrValue = one.getSysAttrValue();
        }
        String url = serverConfig.getUrl();
        return CommonResult.success(sysUserCharacteristicService.insertUserFace(file, userName, sysAttrValue, url));
    }

}
