package com.srs.comprehensive.service.impl;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.DateUtils;
import com.srs.comprehensive.domain.CphFamilyMember;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.comprehensive.mapper.SrsEducationExperienceMapper;
import com.srs.comprehensive.domain.SrsEducationExperience;
import com.srs.comprehensive.service.ISrsEducationExperienceService;

/**
 * 教育经历
 * Service业务层处理
 *
 * @author srs
 * @date 2024-04-08
 */
@Service
public class SrsEducationExperienceServiceImpl extends ServiceImpl<SrsEducationExperienceMapper, SrsEducationExperience> implements ISrsEducationExperienceService {
    @Autowired
    private SrsEducationExperienceMapper srsEducationExperienceMapper;


    @Override
    public AjaxResult editOwnExp(SrsEducationExperience param){
        try{
            SrsEducationExperience entity = srsEducationExperienceMapper.selectSrsEducationExperienceByEduId(param.getEduId());
            if(entity == null){
                throw new Exception("该记录不存在");
            }

            if(!Objects.equals(param.getStuNo(), entity.getStuNo())){
                throw new Exception("你是谁?");
            }

            int res = srsEducationExperienceMapper.updateSrsEducationExperience(param);
            if(res == 0){
                throw new Exception("保存失败");
            }

            return AjaxResult.success();
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }


    @Override
    public AjaxResult deleteOwnExp(SrsEducationExperience param){
        try{
            SrsEducationExperience entity = srsEducationExperienceMapper.selectSrsEducationExperienceByEduId(param.getEduId());
            if(entity == null){
                throw new Exception("该记录已被删除");
            }

            if(!Objects.equals(entity.getStuNo(), param.getStuNo())){
                throw new Exception("你是谁?");
            }

            int res = srsEducationExperienceMapper.deleteSrsEducationExperienceByEduId(param.getEduId());
            if(res == 0){
                throw new Exception("删除失败");
            }

            return AjaxResult.success();
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }

    @Override
    public List<SrsEducationExperience> getStuExp(String stuNo){
        QueryWrapper<SrsEducationExperience> query = new QueryWrapper<>();
        query.eq("stu_no",stuNo);
        List<SrsEducationExperience> list = srsEducationExperienceMapper.selectList(query);
        return list;
    }


    /**
     * 查询教育经历
     *
     * @param eduId 教育经历
     *              主键
     * @return 教育经历
     */
    @Override
    public SrsEducationExperience selectSrsEducationExperienceByEduId(Long eduId) {
        return srsEducationExperienceMapper.selectSrsEducationExperienceByEduId(eduId);
    }

    /**
     * 查询教育经历
     * 列表
     *
     * @param srsEducationExperience 教育经历
     * @return 教育经历
     */
    @Override
    public List<SrsEducationExperience> selectSrsEducationExperienceList(SrsEducationExperience srsEducationExperience) {
        return srsEducationExperienceMapper.selectSrsEducationExperienceList(srsEducationExperience);
    }

    /**
     * 新增教育经历
     *
     * @param srsEducationExperience 教育经历
     * @return 结果
     */
    @Override
    public int insertSrsEducationExperience(SrsEducationExperience srsEducationExperience) {
        srsEducationExperience.setCreateTime(DateUtils.getNowDate());
        return srsEducationExperienceMapper.insertSrsEducationExperience(srsEducationExperience);
    }

    /**
     * 修改教育经历
     *
     * @param srsEducationExperience 教育经历
     * @return 结果
     */
    @Override
    public int updateSrsEducationExperience(SrsEducationExperience srsEducationExperience) {
        srsEducationExperience.setUpdateTime(DateUtils.getNowDate());
        return srsEducationExperienceMapper.updateSrsEducationExperience(srsEducationExperience);
    }

    /**
     * 批量删除教育经历
     *
     * @param eduIds 需要删除的教育经历
     *               主键
     * @return 结果
     */
    @Override
    public int deleteSrsEducationExperienceByEduIds(Long[] eduIds) {
        return srsEducationExperienceMapper.deleteSrsEducationExperienceByEduIds(eduIds);
    }

    /**
     * 删除教育经历
     * 信息
     *
     * @param eduId 教育经历
     *              主键
     * @return 结果
     */
    @Override
    public int deleteSrsEducationExperienceByEduId(Long eduId) {
        return srsEducationExperienceMapper.deleteSrsEducationExperienceByEduId(eduId);
    }
}
