package com.xjrsoft.module.personnel.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.constant.GlobalConstant;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.organization.entity.UserDeptRelation;
import com.xjrsoft.module.personnel.dto.AddTeacherFaceProcessDto;
import com.xjrsoft.module.personnel.dto.UpdateTeacherFaceProcessDto;
import cn.dev33.satoken.annotation.SaCheckPermission;

import com.xjrsoft.module.personnel.dto.TeacherFaceProcessPageDto;
import com.xjrsoft.module.personnel.entity.CarMessageApply;
import com.xjrsoft.module.personnel.entity.StundentFaceProcess;
import com.xjrsoft.module.personnel.entity.TeacherFaceProcess;
import com.xjrsoft.module.personnel.service.ITeacherFaceProcessService;
import com.xjrsoft.module.personnel.vo.TeacherFaceProcessPageVo;

import com.xjrsoft.module.personnel.vo.TeacherFaceProcessVo;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import com.xjrsoft.module.system.entity.File;
import com.xjrsoft.module.teacher.entity.BaseTeacher;
import com.xjrsoft.module.teacher.entity.XjrUser;
import com.xjrsoft.module.teacher.vo.XjrUserPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
* @title: 人脸信息审核
* @Author dzx
* @Date: 2024-05-10
* @Version 1.0
*/
@RestController
@RequestMapping("/personnel" + "/teacherFaceProcess")
@Api(value = "/personnel"  + "/teacherFaceProcess",tags = "人脸信息审核代码")
@AllArgsConstructor
public class TeacherFaceProcessController {


    private final ITeacherFaceProcessService teacherFaceProcessService;

    @GetMapping(value = "/page")
    @ApiOperation(value="人脸信息审核列表(分页)")
    @SaCheckPermission("teacherfaceprocess:detail")
    public RT<PageOutput<TeacherFaceProcessPageVo>> page(@Valid TeacherFaceProcessPageDto dto){

        if(ObjectUtil.isNull(dto.getStatus())){
            dto.setStatus(1);
        }

        MPJLambdaWrapper<TeacherFaceProcess> queryWrapper =MPJWrappers.<TeacherFaceProcess>lambdaJoin()
                .orderByDesc(TeacherFaceProcess::getId)
                .disableSubLogicDel()
                .like(StrUtil.isNotBlank(dto.getName()),XjrUser::getName,dto.getName())
                .like(StrUtil.isNotBlank(dto.getUserName()),XjrUser::getUserName,dto.getUserName())
                .like(StrUtil.isNotBlank(dto.getMobile()),XjrUser::getMobile,dto.getMobile())
                .eq(TeacherFaceProcess::getExamStatus,dto.getStatus())
                .select(TeacherFaceProcess::getId,TeacherFaceProcess::getUserId,TeacherFaceProcess::getDepId,TeacherFaceProcess::getFacePhoto)
                .innerJoin(XjrUser.class,XjrUser::getId, TeacherFaceProcess::getUserId)
                .leftJoin(File.class,File::getFolderId,TeacherFaceProcess::getFacePhoto)
                .leftJoin(DictionaryDetail.class,DictionaryDetail::getCode,TeacherFaceProcess::getGender, ext->ext.selectAs(DictionaryDetail::getName, TeacherFaceProcessPageVo::getGender))
                .selectAs(XjrUser::getName,TeacherFaceProcessPageVo::getName)
                .select("t2.file_url AS face_photo_url,t1.user_name AS user_name,t1.mobile")
                ;

        if(ObjectUtil.isNotNull(dto.getDepId())&&dto.getDepId()!=0) { // 要用用户关联部门查询
            queryWrapper.leftJoin(UserDeptRelation.class, UserDeptRelation::getUserId, TeacherFaceProcess::getUserId)
                    .eq(UserDeptRelation::getDeptId, dto.getDepId());
        }

        IPage<TeacherFaceProcessPageVo> page=teacherFaceProcessService.selectJoinListPage(ConventPage.getPage(dto),TeacherFaceProcessPageVo.class,queryWrapper);

        PageOutput<TeacherFaceProcessPageVo> pageOutput = ConventPage.getPageOutput(page, TeacherFaceProcessPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询人脸信息审核信息")
    @SaCheckPermission("teacherfaceprocess:detail")
    public RT<TeacherFaceProcessVo> info(@RequestParam Long id){
        TeacherFaceProcess teacherFaceProcess = teacherFaceProcessService.getById(id);
        if (teacherFaceProcess == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(teacherFaceProcess, TeacherFaceProcessVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增人脸信息审核")
    @SaCheckPermission("teacherfaceprocess:add")
    public RT<Boolean> add(@Valid @RequestBody AddTeacherFaceProcessDto dto){
        TeacherFaceProcess teacherFaceProcess = BeanUtil.toBean(dto, TeacherFaceProcess.class);
        boolean isSuccess = teacherFaceProcessService.save(teacherFaceProcess);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改人脸信息审核")
    @SaCheckPermission("teacherfaceprocess:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateTeacherFaceProcessDto dto){

        TeacherFaceProcess teacherFaceProcess = BeanUtil.toBean(dto, TeacherFaceProcess.class);
        return RT.ok(teacherFaceProcessService.updateById(teacherFaceProcess));

    }

    @DeleteMapping
    @ApiOperation(value = "删除人脸信息审核")
    @SaCheckPermission("teacherfaceprocess:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(teacherFaceProcessService.removeBatchByIds(ids));

    }

}