package com.jeeplus.human.enrollment.enrollmentRegistration.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentFamilyInfo;
import com.jeeplus.human.enrollment.enrollmentRegistration.mapper.EnrollmentFamilyInfoMapper;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0
 * @date 2023-10-17 8:57
 */
@Service
@Transactional
public class EnrollmentFamilyInfoService extends ServiceImpl<EnrollmentFamilyInfoMapper, EnrollmentFamilyInfo> {

    @Resource
    private EnrollmentFamilyInfoMapper mapper;

    public void deleteByRegistrationId(String id){
        QueryWrapper<EnrollmentFamilyInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enrollment_registration_id",id);
        mapper.delete(queryWrapper);
    }

    public EnrollmentFamilyInfo findById(String id) throws Exception {
        // 查询基础信息表
        EnrollmentFamilyInfo info = mapper.getById(id);
        return info;
    }

    /**
     * 根据关联id查询所有数据
     * @return
     */
    public List<EnrollmentFamilyInfo> findByUserId(String id,String status)  {
        QueryWrapper<EnrollmentFamilyInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(id)) {
            queryWrapper.eq("user_id", id);
        }
        if(StringUtils.isNotBlank(status)){
            if("5".equals(status)){
                queryWrapper.and(wrapper ->
                        wrapper.eq("status", "5")
                                .or().isNull("status")
                                .or().eq("status", "")
                                .or().eq("status", "6")
                );
            } else if("1".equals(status)) {
                queryWrapper.and(wrapper ->
                        wrapper.eq("status", "5")
                                .or().isNull("status")
                                .or().eq("status", "")
                                .or().eq("status", "1")
                                .or().eq("status", "4")
                );
            } else if("2".equals(status)) {
                queryWrapper.and(wrapper ->
                        wrapper.eq("status", "2")
                );
            }else{
                queryWrapper.and(wrapper ->
                        wrapper.eq("status", "2")
                                .or().eq("status", "4")
                                .or().eq("status", "1")
                );
            }
        }
        List<EnrollmentFamilyInfo> infos = mapper.selectList(queryWrapper);
        return infos;
    }

    public void saveOrUpdateInfo(EnrollmentFamilyInfo info) {
        if (info == null) {
            return ;
        }
        if (!StringUtils.isNotBlank(info.getId())) {
            // 新增
            add(info);
        } else {
            // 更新
            update(info);
        }
    }

    /**
     * 新增
     * @param info
     */
    public void add(EnrollmentFamilyInfo info){
        mapper.insert(info);
    }

    public void update(EnrollmentFamilyInfo info){
        UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        mapper.updateById(info);
    }

}
