package com.ruoyi.mxjb.service.impl;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mxjb.mapper.MxjbSelectedGroupMapper;
import com.ruoyi.mxjb.domain.MxjbSelectedGroup;
import com.ruoyi.mxjb.service.IMxjbSelectedGroupService;

/**
 * 科目组合Service业务层处理
 * 
 * @author mingxue
 * @date 2022-11-25
 */
@Service
public class MxjbSelectedGroupServiceImpl implements IMxjbSelectedGroupService 
{
    @Autowired
    private MxjbSelectedGroupMapper mxjbSelectedGroupMapper;

    /**
     * 查询科目组合
     * 
     * @param groupId 科目组合ID
     * @return 科目组合
     */
    @Override
    public MxjbSelectedGroup selectMxjbSelectedGroupById(Integer groupId)
    {
        return mxjbSelectedGroupMapper.selectMxjbSelectedGroupById(groupId);
    }

    /**
     * 查询科目组合列表
     * 
     * @param mxjbSelectedGroup 科目组合
     * @return 科目组合
     */
    @Override
    public List<MxjbSelectedGroup> selectMxjbSelectedGroupList(MxjbSelectedGroup mxjbSelectedGroup)
    {
        return mxjbSelectedGroupMapper.selectMxjbSelectedGroupList(mxjbSelectedGroup);
    }

    /**
     * 新增科目组合
     * 
     * @param mxjbSelectedGroup 科目组合
     * @return 结果
     */
    @Override
    public int insertMxjbSelectedGroup(MxjbSelectedGroup mxjbSelectedGroup)
    {
        return mxjbSelectedGroupMapper.insertMxjbSelectedGroup(mxjbSelectedGroup);
    }

    /**
     * 修改科目组合
     * 
     * @param mxjbSelectedGroup 科目组合
     * @return 结果
     */
    @Override
    public int updateMxjbSelectedGroup(MxjbSelectedGroup mxjbSelectedGroup)
    {
        return mxjbSelectedGroupMapper.updateMxjbSelectedGroup(mxjbSelectedGroup);
    }

    /**
     * 批量删除科目组合
     * 
     * @param groupIds 需要删除的科目组合ID
     * @return 结果
     */
    @Override
    public int deleteMxjbSelectedGroupByIds(Integer[] groupIds)
    {
        return mxjbSelectedGroupMapper.deleteMxjbSelectedGroupByIds(groupIds);
    }

    /**
     * 删除科目组合信息
     * 
     * @param groupId 科目组合ID
     * @return 结果
     */
    @Override
    public int deleteMxjbSelectedGroupById(Integer groupId)
    {
        return mxjbSelectedGroupMapper.deleteMxjbSelectedGroupById(groupId);
    }

    /**
     * 根据“物理,化学,生物”或“物化生”获取groupId
     * @param mode
     * @return
     */
    @Override
    public Integer selectGroupIdByMode(String mode) {
        if(StringUtils.isBlank(mode)){
            return null;
        }
        List<MxjbSelectedGroup> list = selectMxjbSelectedGroupList(new MxjbSelectedGroup());
        if(mode.length()==3){
            //物化生
            char[] courseArray = mode.toCharArray();
            String course0 = String.valueOf(courseArray[0]);//物
            if(courseArray.length==1){
                //文理科的模式
                for (MxjbSelectedGroup mxjbSelectedGroup : list) {
                    if(mxjbSelectedGroup.getName().contains(course0)){
                        return mxjbSelectedGroup.getGroupId();
                    }
                }
            }else {
                String course1 = String.valueOf(courseArray[1]);//化
                String course2 = String.valueOf(courseArray[2]);//生
                for (MxjbSelectedGroup mxjbSelectedGroup : list) {
                    if(mxjbSelectedGroup.getName().contains(course0)
                            &&(mxjbSelectedGroup.getName().contains(course1)
                            &&mxjbSelectedGroup.getName().contains(course2))){
                        return mxjbSelectedGroup.getGroupId();
                    }
                }
            }

        }else {
            String[] courseArray = Convert.toStrArray(mode);
            String course0 = courseArray[0];//物理
            if(courseArray.length==1){
                //文理科的模式
                for (MxjbSelectedGroup mxjbSelectedGroup : list) {
                    if(mxjbSelectedGroup.getCourse0().equals(course0)){
                        return mxjbSelectedGroup.getGroupId();
                    }
                }
            }else {
                String course1 = courseArray[1]+","+courseArray[2];//化学,生物
                String course11 = StringUtils.reverseCourse(course1);//生物,化学
                for (MxjbSelectedGroup mxjbSelectedGroup : list) {
                    if(mxjbSelectedGroup.getCourse0().equals(course0)&&
                            (mxjbSelectedGroup.getCourse1().equals(course1)||mxjbSelectedGroup.getCourse1().equals(course11))){
                        return mxjbSelectedGroup.getGroupId();
                    }
                }
            }
        }

        return null;
    }

    public static void main(String[] args) {
        new MxjbSelectedGroupServiceImpl().selectGroupIdByMode("物化生");
        new MxjbSelectedGroupServiceImpl().selectGroupIdByMode("生物化");
        new MxjbSelectedGroupServiceImpl().selectGroupIdByMode("物理,化学,生物");
        new MxjbSelectedGroupServiceImpl().selectGroupIdByMode("生物,物理,化学");
    }
}
