package com.srs.teacher.service.impl;

import java.util.List;

import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.teacher.mapper.SysTeacherKpiFillingAuditRecordMapper;
import com.srs.teacher.domain.SysTeacherKpiFillingAuditRecord;
import com.srs.teacher.service.ISysTeacherKpiFillingAuditRecordService;

/**
 * 业绩考核-个人填报-审核记录Service业务层处理
 *
 * @author chc
 * @date 2024-07-01
 */
@Service
public class SysTeacherKpiFillingAuditRecordServiceImpl extends ServiceImpl<SysTeacherKpiFillingAuditRecordMapper, SysTeacherKpiFillingAuditRecord> implements ISysTeacherKpiFillingAuditRecordService {
    @Autowired
    private SysTeacherKpiFillingAuditRecordMapper sysTeacherKpiFillingAuditRecordMapper;

    /**
     * 查询业绩考核-个人填报-审核记录
     *
     * @param auditRecordId 业绩考核-个人填报-审核记录主键
     * @return 业绩考核-个人填报-审核记录
     */
    @Override
    public SysTeacherKpiFillingAuditRecord selectSysTeacherKpiFillingAuditRecordByAuditRecordId(Long auditRecordId) {
        return sysTeacherKpiFillingAuditRecordMapper.selectSysTeacherKpiFillingAuditRecordByAuditRecordId(auditRecordId);
    }

    @Override
    public SysTeacherKpiFillingAuditRecord selectSysTeacherKpiFillingAuditRecordByFdyName(String fdyName, String fillingYear, String fillingMonth, String auditName) {
        return sysTeacherKpiFillingAuditRecordMapper.selectSysTeacherKpiFillingAuditRecordByFdyName(fdyName, fillingYear, fillingMonth, auditName);
    }

    /**
     * 查询业绩考核-个人填报-审核记录列表
     *
     * @param sysTeacherKpiFillingAuditRecord 业绩考核-个人填报-审核记录
     * @return 业绩考核-个人填报-审核记录
     */
    @Override
    public List<SysTeacherKpiFillingAuditRecord> selectSysTeacherKpiFillingAuditRecordList(SysTeacherKpiFillingAuditRecord sysTeacherKpiFillingAuditRecord) {
        // 看自己的
        sysTeacherKpiFillingAuditRecord.setAuditName(SecurityUtils.getLoginUser().getUser().getNickName());
        return sysTeacherKpiFillingAuditRecordMapper.selectSysTeacherKpiFillingAuditRecordList(sysTeacherKpiFillingAuditRecord);
    }

    /**
     * 新增业绩考核-个人填报-审核记录
     *
     * @param sysTeacherKpiFillingAuditRecord 业绩考核-个人填报-审核记录
     * @return 结果
     */
    @Override
    public int insertSysTeacherKpiFillingAuditRecord(SysTeacherKpiFillingAuditRecord sysTeacherKpiFillingAuditRecord) {
        String auditName = sysTeacherKpiFillingAuditRecord.getAuditName();
        if (sysTeacherKpiFillingAuditRecord.getAuditName() == null) {
            auditName = SecurityUtils.getLoginUser().getUser().getNickName();
        }
        // 查询审核记录有没有生成
        SysTeacherKpiFillingAuditRecord fillingAuditRecord = sysTeacherKpiFillingAuditRecordMapper.selectSysTeacherKpiFillingAuditRecordByFdyName(sysTeacherKpiFillingAuditRecord.getFdyName(), sysTeacherKpiFillingAuditRecord.getFillingYear(), sysTeacherKpiFillingAuditRecord.getFillingMonth(), auditName);
        // 判断审核记录是否存在 存在就更新
        if (fillingAuditRecord != null) {
            sysTeacherKpiFillingAuditRecord.setAuditRecordId(fillingAuditRecord.getAuditRecordId());
            sysTeacherKpiFillingAuditRecord.setAuditTime(DateUtils.getNowDate());
            return sysTeacherKpiFillingAuditRecordMapper.updateSysTeacherKpiFillingAuditRecord(sysTeacherKpiFillingAuditRecord);
        }
        sysTeacherKpiFillingAuditRecord.setAuditName(auditName);
        return sysTeacherKpiFillingAuditRecordMapper.insertSysTeacherKpiFillingAuditRecord(sysTeacherKpiFillingAuditRecord);
    }

    @Override
    public int insertSysTeacherKpiFillingAuditRecordBatch(List<SysTeacherKpiFillingAuditRecord> sysTeacherKpiFillingAuditRecordList) {
        int affectedRows = 0;
        for (SysTeacherKpiFillingAuditRecord record : sysTeacherKpiFillingAuditRecordList) {
            // 给所有审核记录，加上审核人名字
            record.setAuditName(SecurityUtils.getLoginUser().getUser().getNickName());

            // 查询数据库，审核记录是否存在
            SysTeacherKpiFillingAuditRecord existingRecord = sysTeacherKpiFillingAuditRecordMapper.selectSysTeacherKpiFillingAuditRecordByFdyName(
                    record.getFdyName(), record.getFillingYear(), record.getFillingMonth(), SecurityUtils.getLoginUser().getUser().getNickName());

            if (existingRecord != null) {
                // 如果存在执行编辑（更新）审核记录
                record.setAuditRecordId(existingRecord.getAuditRecordId());
                record.setAuditTime(DateUtils.getNowDate());
                int updatedRows = sysTeacherKpiFillingAuditRecordMapper.updateSysTeacherKpiFillingAuditRecord(record);
                affectedRows += updatedRows;
            } else {
                // 如果不存在，插入新记录
                int insertedRows = sysTeacherKpiFillingAuditRecordMapper.insertSysTeacherKpiFillingAuditRecord(record);
                affectedRows += insertedRows;
            }
        }

        // 返回受影响的总行数
        return affectedRows;
    }

    /**
     * 修改业绩考核-个人填报-审核记录
     *
     * @param sysTeacherKpiFillingAuditRecord 业绩考核-个人填报-审核记录
     * @return 结果
     */
    @Override
    public int updateSysTeacherKpiFillingAuditRecord(SysTeacherKpiFillingAuditRecord sysTeacherKpiFillingAuditRecord) {
        return sysTeacherKpiFillingAuditRecordMapper.updateSysTeacherKpiFillingAuditRecord(sysTeacherKpiFillingAuditRecord);
    }

    /**
     * 批量删除业绩考核-个人填报-审核记录
     *
     * @param auditRecordIds 需要删除的业绩考核-个人填报-审核记录主键
     * @return 结果
     */
    @Override
    public int deleteSysTeacherKpiFillingAuditRecordByAuditRecordIds(Long[] auditRecordIds) {
        return sysTeacherKpiFillingAuditRecordMapper.deleteSysTeacherKpiFillingAuditRecordByAuditRecordIds(auditRecordIds);
    }

    /**
     * 删除业绩考核-个人填报-审核记录信息
     *
     * @param auditRecordId 业绩考核-个人填报-审核记录主键
     * @return 结果
     */
    @Override
    public int deleteSysTeacherKpiFillingAuditRecordByAuditRecordId(Long auditRecordId) {
        return sysTeacherKpiFillingAuditRecordMapper.deleteSysTeacherKpiFillingAuditRecordByAuditRecordId(auditRecordId);
    }
}
