package com.zd.laboratory.controller;

import com.zd.common.core.domain.R;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.enums.AuditStatusEnum;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.system.api.domain.LabStudentsInfo;
import com.zd.laboratory.domain.dto.LabStudentsInfoAuditDTO;
import com.zd.laboratory.domain.vo.LabStudentsInfoVO;
import com.zd.laboratory.service.ILabStudentsInfoService;
import com.zd.system.api.RemoteFileService;
import com.zd.system.api.domain.SysFile;
import com.zd.system.api.face.RemoteFaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生信息Controller
 *
 * @author zhoupan
 * @date 2021-09-10
 */
@RestController
@Api(tags = "【学生信息】")
@RequestMapping("/studentinfo")
public class LabStudentsInfoController extends BaseController {
    @Autowired
    private ILabStudentsInfoService labStudentsInfoService;

    @Autowired
    private RemoteFileService remoteFileService;

    @Autowired
    private RemoteFaceService remoteFaceService;

    /**
     * 查询学生信息列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_STUDENTINFO + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询学生信息列表")
    public TableDataInfo<LabStudentsInfo> list(LabStudentsInfo labStudentsInfo) {
        startPage();
        List<LabStudentsInfo> list = labStudentsInfoService.selectLabStudentsInfoList(labStudentsInfo);
        return getDataTable(list);
    }

    /**
     * 查询学生卡审核信息列表vo-按审核分类
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_STUDENTINFO + PerFun.LIST)
    @GetMapping("/list/{type}")
    @ApiOperation(value = "查询学生信息列表vo")
    public TableDataInfo<LabStudentsInfoVO> auditTypelist(@PathVariable AuditStatusEnum type) {
        LabStudentsInfo labStudentsInfo = new LabStudentsInfo();
        labStudentsInfo.setAuditStatus(type);
        startPage();
        List<LabStudentsInfoVO> list = labStudentsInfoService.selectStudentsInfoListVO(labStudentsInfo);
        return getDataTable(list);
    }


    /**
     * 获取学生信息详细信息
     */
    @ApiOperation(value = "获取学生信息详细信息")
//    @PreAuthorize(hasPermi = "laboratory:studentinfo:query")
    @GetMapping(value = "/{joinStudentsId}")
    public ResultData<LabStudentsInfo> getInfo(@PathVariable("joinStudentsId") Long joinStudentsId) {
        return ResultData.success(labStudentsInfoService.selectLabStudentsInfoByJoinStudentsId(joinStudentsId));
    }


    /**
     * 获取当前用户-信息详细信息
     */
    @ApiOperation(value = "获取当前用户信息")
//    @PreAuthorize(hasPermi = "laboratory:studentinfo:query")
    @GetMapping(value = "/my")
    public ResultData<LabStudentsInfo> getMyInfo() {
        if(1==1){
             logger.info("我是一个小可爱");
        }
        LabStudentsInfo labStudentsInfo = labStudentsInfoService.selectLabStudentsInfoByJoinStudentsId(SecurityUtils.getUserIdEx());
        if(labStudentsInfo!=null)
        {
            if(labStudentsInfo.getFaceFeature()!=null)
            {
                labStudentsInfo.setIfFaceFeature(true);
            }
            else
            {
                labStudentsInfo.setIfFaceFeature(false);
            }
            if(labStudentsInfo.getAuditStatus()==null)
            {
                labStudentsInfo.setAuditStatus(AuditStatusEnum.NO_COMMIT);
            }
        }
        else {
            labStudentsInfo = new LabStudentsInfo();
            labStudentsInfo.setIfFaceFeature(false);
            labStudentsInfo.setAuditStatus(AuditStatusEnum.NO_COMMIT);

        }
        return ResultData.success(labStudentsInfo);
    }

    /**
     * 审核通过
     */
    @ApiOperation(value = "审核通过")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_STUDENTINFO + PerFun.EDIT)
    @PutMapping("/audit/success")
    public ResultData auditSuccess(@RequestBody @Validated LabStudentsInfoAuditDTO studentsInfoAuditDTO) {

        studentsInfoAuditDTO.setAuditStatus(AuditStatusEnum.SUCCESS);
        return ResultData.result(labStudentsInfoService.audit(studentsInfoAuditDTO));
    }

    /**
     * 审核驳回
     */
    @ApiOperation(value = "审核驳回")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_STUDENTINFO + PerFun.EDIT)
    @PutMapping("/audit/fail")
    public ResultData auditFail(@RequestBody @Validated LabStudentsInfoAuditDTO studentsInfoAuditDTO) {
        studentsInfoAuditDTO.setAuditStatus(AuditStatusEnum.FAIL);
        return ResultData.result(labStudentsInfoService.audit(studentsInfoAuditDTO));
    }

    /**
     * 提交学生卡
     */
    @ApiOperation(value = "提交学生卡信息")
    @PostMapping ("/commit/crad")
    public ResultData commitCard(@RequestParam("file") MultipartFile file) {

        if (!file.isEmpty()) {
            R<SysFile> fileResult = remoteFileService.upload(file);
            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData())) {
                return ResultData.fail("文件服务异常，请联系管理员");
            }
            String url = fileResult.getData().getUrl();
            if (labStudentsInfoService.commitMyCard(url) == 1) {
                Map data = new HashMap(2);
                data.put("cardUrl", url);
                return ResultData.success(data);
            }
        }
        return ResultData.fail("上传图片异常，请联系管理员");

    }

    /**
     * 提交人脸
     */
    @ApiOperation(value = "提交人脸信息")
    @PostMapping("/commit/face")
    public ResultData edit(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {


            R< byte[]> fileResult1 = remoteFaceService.upload(file);
            if(fileResult1.getCode()!=200){
                return   ResultData.fail("图片人脸信息检测失败，请重新上传！");
            }
            //保存
            R<SysFile> fileResult = remoteFileService.upload(file);
            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData())) {
                return ResultData.fail("文件服务异常，请联系管理员");
            }
            String url = fileResult.getData().getUrl();
            if (labStudentsInfoService.commitMyFace(url,fileResult1.getData()) == 1) {
                Map data = new HashMap(2);
                data.put("cardUrl", url);
                return ResultData.success(data);
            }
        }
        return ResultData.fail("上传图片异常，请联系管理员");
    }


    /**
     * 获取学生登录用户信息
     */
    @ApiOperation(value = "获取学生登录用户信息")
    @GetMapping(value = "/mystudent")
    public ResultData<LabStudentsInfo> myStudent() {
        return ResultData.success(labStudentsInfoService.selectLabStudentsInfoByJoinStudentsId(SecurityUtils.getUserId()));
    }

}
