package com.zd.laboratory.service.impl;

import java.util.List;
import java.util.Optional;

import com.zd.common.core.enums.AuditStatusEnum;
import com.zd.common.core.utils.Assert;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.common.security.service.TokenService;
import com.zd.laboratory.domain.dto.LabStudentsInfoAuditDTO;

import com.zd.laboratory.domain.vo.LabStudentsInfoVO;
import com.zd.system.api.domain.SysUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.LabStudentsInfoMapper;
import com.zd.system.api.domain.LabStudentsInfo;
import com.zd.laboratory.service.ILabStudentsInfoService;

/**
 * 学生信息Service业务层处理
 *
 * @author zhoupan
 * @date 2021-09-10
 */
@Service
public class LabStudentsInfoServiceImpl implements ILabStudentsInfoService {
    @Autowired
    private LabStudentsInfoMapper labStudentsInfoMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询学生信息
     *
     * @param joinStudentsId 学生信息主键
     * @return 学生信息
     */
    @Override
    public LabStudentsInfo selectLabStudentsInfoByJoinStudentsId(Long joinStudentsId) {
        return labStudentsInfoMapper.selectLabStudentsInfoByJoinStudentsId(joinStudentsId);
    }

    /**
     * 审核
     *
     * @param studentsInfoAuditDTO@return 学生信息
     */
    @Override
    public Boolean audit(LabStudentsInfoAuditDTO studentsInfoAuditDTO) {


        LabStudentsInfo audit = new LabStudentsInfo();
        BeanUtils.copyProperties(studentsInfoAuditDTO, audit);
        audit.setAuditUser(SecurityUtils.getUserId());
        audit.setAuditTime(DateUtils.getNowDate());

        return updateLabStudentsInfo(audit) == 1;
    }

    /**
     * 查询学生信息列表
     *
     * @param labStudentsInfo 学生信息
     * @return 学生信息
     */
    @Override
    @DataScope(deptAlias = "si")
    public List<LabStudentsInfo> selectLabStudentsInfoList(LabStudentsInfo labStudentsInfo) {
        return labStudentsInfoMapper.selectLabStudentsInfoList(labStudentsInfo);
    }

    /**
     * 查询学生信息列表 -vo
     *
     * @param labStudentsInfo 学生信息
     * @return 学生信息集合
     */
    @Override
    @DataScope(deptAlias = "t")
    public List<LabStudentsInfoVO> selectStudentsInfoListVO(LabStudentsInfo labStudentsInfo) {
        return labStudentsInfoMapper.selectStudentsInfoListVO(labStudentsInfo);
    }

    /**
     * 新增学生信息
     *
     * @param labStudentsInfo 学生信息
     * @return 结果
     */
    @Override
    public int insertLabStudentsInfo(LabStudentsInfo labStudentsInfo) {

        SysUser user = tokenService.getLoginUser().getSysUser();

        labStudentsInfo.setCreateTime(DateUtils.getNowDate());

        //没有必要---  错《--- 非常有必要
        labStudentsInfo.setCreateBy(Optional.ofNullable(user.getNickName()).orElse(user.getUserName()));
        labStudentsInfo.setUserId(SecurityUtils.getUserId());
        if(user.getDept() != null){
            labStudentsInfo.setDeptId(user.getDept().getDeptId());
            labStudentsInfo.setDeptName(user.getDept().getDeptName());
        }

        return labStudentsInfoMapper.insertLabStudentsInfo(labStudentsInfo);
    }

    /**
     * 提交我的学生卡
     *
     * @param url 学生卡地址
     * @return 结果
     */
    @Override
    public int commitMyCard(String url) {
        LabStudentsInfo info = new LabStudentsInfo();
        info.setAuditStatus(AuditStatusEnum.TO_AUDIT);
        info.setCard(url);
        //判断当前是否已有数据
        Long id = SecurityUtils.getUserIdEx();
        LabStudentsInfo labStudentsInfo = selectLabStudentsInfoByJoinStudentsId(id);
        info.setCommitTime(DateUtils.getNowDate());
        info.setJoinStudentsId(id);
        //修改数据
        if (labStudentsInfo != null) {
            Assert.isFalse(AuditStatusEnum.SUCCESS.equals(labStudentsInfo.getAuditStatus()), "您已的学生卡已审核通过，请勿重复申请！");
            return updateLabStudentsInfo(info);
        }

        //保存数据
        return insertLabStudentsInfo(info);
    }

    /**
     * 提交我的人脸信息
     *
     * @param url 人脸图片地址
     * @return 结果
     */
    @Override
    public int commitMyFace(String url) {
        return commitMyFace(url,null);
    }
    public int commitMyFace(String url,byte[] f)
    {
        LabStudentsInfo info = new LabStudentsInfo();
        info.setFaceImg(url);
        info.setFaceFeature(f);
        //判断当前是否已有数据
        Long id = SecurityUtils.getUserIdEx();
        LabStudentsInfo labStudentsInfo = selectLabStudentsInfoByJoinStudentsId(id);
        info.setJoinStudentsId(id);
        //修改数据
        if (labStudentsInfo != null) {

            return updateLabStudentsInfo(info);
        }

        //保存数据
        return insertLabStudentsInfo(info);

    }
    /**
     * 修改学生信息
     *
     * @param labStudentsInfo 学生信息
     * @return 结果
     */
    @Override
    public int updateLabStudentsInfo(LabStudentsInfo labStudentsInfo) {
        labStudentsInfo.setUpdateTime(DateUtils.getNowDate());
        labStudentsInfo.setUpdateBy(SecurityUtils.getUsername());
        return labStudentsInfoMapper.updateLabStudentsInfo(labStudentsInfo);
    }

    /**
     * 批量删除学生信息
     *
     * @param joinStudentsIds 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public int deleteLabStudentsInfoByJoinStudentsIds(Long[] joinStudentsIds) {
        return labStudentsInfoMapper.deleteLabStudentsInfoByJoinStudentsIds(joinStudentsIds);
    }

    /**
     * 删除学生信息信息
     *
     * @param joinStudentsId 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteLabStudentsInfoByJoinStudentsId(Long joinStudentsId) {
        return labStudentsInfoMapper.deleteLabStudentsInfoByJoinStudentsId(joinStudentsId);
    }
}
