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 org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.comprehensive.mapper.CphFamilyMemberMapper;
import com.srs.comprehensive.domain.CphFamilyMember;
import com.srs.comprehensive.service.ICphFamilyMemberService;

/**
 * 家庭成员Service业务层处理
 *
 * @author srs
 * @date 2024-04-08
 */
@Service
public class CphFamilyMemberServiceImpl extends ServiceImpl<CphFamilyMemberMapper,CphFamilyMember> implements ICphFamilyMemberService {
    @Autowired
    private CphFamilyMemberMapper cphFamilyMemberMapper;

    @Override
    public AjaxResult editOwnFamily(CphFamilyMember param){
        try{
            CphFamilyMember entity = cphFamilyMemberMapper.selectCphFamilyMemberById(param.getId());
            if(entity == null){
                throw new Exception("该记录不存在");
            }

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

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

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


    @Override
    public AjaxResult deleteOwnFamily(CphFamilyMember param){
        try{
            CphFamilyMember entity = cphFamilyMemberMapper.selectCphFamilyMemberById(param.getId());
            if(entity == null){
                throw new Exception("该记录已被删除");
            }

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

            int res = cphFamilyMemberMapper.deleteCphFamilyMemberById(param.getId());
            if(res == 0){
                throw new Exception("删除失败");
            }

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


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


    /**
     * 查询家庭成员
     *
     * @param id 家庭成员主键
     * @return 家庭成员
     */
    @Override
    public CphFamilyMember selectCphFamilyMemberById(Long id) {
        return cphFamilyMemberMapper.selectCphFamilyMemberById(id);
    }

    /**
     * 查询家庭成员列表
     *
     * @param cphFamilyMember 家庭成员
     * @return 家庭成员
     */
    @Override
    public List<CphFamilyMember> selectCphFamilyMemberList(CphFamilyMember cphFamilyMember) {
        return cphFamilyMemberMapper.selectCphFamilyMemberList(cphFamilyMember);
    }

    /**
     * 新增家庭成员
     *
     * @param cphFamilyMember 家庭成员
     * @return 结果
     */
    @Override
    public int insertCphFamilyMember(CphFamilyMember cphFamilyMember) {
                cphFamilyMember.setCreateTime(DateUtils.getNowDate());
            return cphFamilyMemberMapper.insertCphFamilyMember(cphFamilyMember);
    }

    /**
     * 修改家庭成员
     *
     * @param cphFamilyMember 家庭成员
     * @return 结果
     */
    @Override
    public int updateCphFamilyMember(CphFamilyMember cphFamilyMember) {
                cphFamilyMember.setUpdateTime(DateUtils.getNowDate());
        return cphFamilyMemberMapper.updateCphFamilyMember(cphFamilyMember);
    }

    /**
     * 批量删除家庭成员
     *
     * @param ids 需要删除的家庭成员主键
     * @return 结果
     */
    @Override
    public int deleteCphFamilyMemberByIds(Long[] ids) {
        return cphFamilyMemberMapper.deleteCphFamilyMemberByIds(ids);
    }

    /**
     * 删除家庭成员信息
     *
     * @param id 家庭成员主键
     * @return 结果
     */
    @Override
    public int deleteCphFamilyMemberById(Long id) {
        return cphFamilyMemberMapper.deleteCphFamilyMemberById(id);
    }

    @Override
    public AjaxResult deleteMemberByFdy(CphFamilyMember param) {
        try{
        int res = cphFamilyMemberMapper.deleteCphFamilyMemberById(param.getId());
        if(res == 0){
            throw new Exception("删除失败");
        }

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


}
