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


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentSocietyInfo;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentWorkInfo;
import com.jeeplus.human.enrollment.enrollmentRegistration.mapper.EnrollmentSocietyInfoMapper;
import com.jeeplus.human.enrollment.enrollmentRegistration.mapper.EnrollmentWorkInfoMapper;
import org.apache.commons.lang3.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 EnrollmentSocietyInfoService extends ServiceImpl<EnrollmentSocietyInfoMapper, EnrollmentSocietyInfo> {

    @Resource
    private EnrollmentSocietyInfoMapper mapper;

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

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

    public List<EnrollmentSocietyInfo> findByUserId(String id,String status) {
        QueryWrapper<EnrollmentSocietyInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(id)) {
            queryWrapper.eq("user_id",id);
        }
        if(com.jeeplus.utils.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")
                                .or().eq("status", "4")
                );
            }else{
                queryWrapper.and(wrapper ->
                        wrapper.eq("status", "2")
                                .or().eq("status", "4")
                                .or().eq("status", "1")
                );
            }
        }
        List<EnrollmentSocietyInfo> workInfos = mapper.selectList(queryWrapper);
        return workInfos;
    }

    public void saveOrUpdateWorkInfo(EnrollmentSocietyInfo workInfo) {
        if (workInfo == null) {
            return;
        }

        if (!StringUtils.isNotBlank(workInfo.getId())) {
            // 新增
            add(workInfo);
        } else {
            // 更新
            update(workInfo);
        }
    }


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

    public void update(EnrollmentSocietyInfo workInfo) {
        mapper.updateById(workInfo);
    }
}


