package com.ruoyi.syzy.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.content.VistorContextHolder;
import com.ruoyi.system.service.ISysConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.syzy.criteria.MarjorTdxCriteria;
import com.ruoyi.syzy.criteria.ZytbMarjorCriteria;
import com.ruoyi.syzy.domain.BBusiWishUniversitySubmitMarjors;
import com.ruoyi.syzy.mapper.BBusiWishUniversitySubmitMarjorsMapper;
import com.ruoyi.syzy.service.IBBusiWishUniversitySubmitMarjorsService;

/**
 * 院校专业分数线Service业务层处理
 *
 * @author mingxue
 * @date 2021-08-20
 */
@Service
public class BBusiWishUniversitySubmitMarjorsServiceImpl implements IBBusiWishUniversitySubmitMarjorsService {
    @Autowired
    private BBusiWishUniversitySubmitMarjorsMapper bBusiWishUniversitySubmitMarjorsMapper;

    @Autowired
    private ISysConfigService configService;
    /**
     * 查询院校专业分数线
     *
     * @param id 院校专业分数线ID
     * @return 院校专业分数线
     */
    @Override
    public BBusiWishUniversitySubmitMarjors selectBBusiWishUniversitySubmitMarjorsById(Long id) {
        return bBusiWishUniversitySubmitMarjorsMapper.selectBBusiWishUniversitySubmitMarjorsById(id);
    }

    @Override
    public List<BBusiWishUniversitySubmitMarjors> selectBBusiWishUniversitySubmitMarjorsByIds(List<Long> ids) {
        return bBusiWishUniversitySubmitMarjorsMapper.selectBBusiWishUniversitySubmitMarjorsByIds(ids);
    }

    /**
     * 查询院校专业分数线列表
     *
     * @param exam 院校专业分数线
     * @return 院校专业分数线
     */
    @Override
    public List<BBusiWishUniversitySubmitMarjors> selectBBusiWishUniversitySubmitMarjorsList(BBusiWishUniversitySubmitMarjors exam) {
        return bBusiWishUniversitySubmitMarjorsMapper.selectBBusiWishUniversitySubmitMarjorsList(exam);
    }

    @Override
    public List<BBusiWishUniversitySubmitMarjors> search(MarjorTdxCriteria criteria) {
        return bBusiWishUniversitySubmitMarjorsMapper.search(criteria.toMap());
    }

    /**
     * 填报批次-选择专业
     */
    @Override
    public List<BBusiWishUniversitySubmitMarjors> zytb(ZytbMarjorCriteria criteria) {
        StringBuilder sb = new StringBuilder();
        sb.append("select * from b_busi_wish_university_submit_marjors b ");
        sb.append("where b.STATUS > 0 ");
        sb.append("and b.universityId = ").append(criteria.getUniversityId().trim()).append(" ");
        //用户地区
        String ulocation = VistorContextHolder.getLocation();
        if(StringUtils.isNotBlank(ulocation)) {
            sb.append("and b.location like '%").append(ulocation.trim()).append("%' ");
        }
        if (StringUtils.isNotBlank(criteria.getMarjor())) {
            // 专业类别
            sb.append("and b.marjorName in(select distinct name from sy_major where 1=1 ");
            sb.append("and code like '").append(criteria.getMarjor().trim()).append("%')");
        }

        if (null != criteria.getBatch()) {
            sb.append("and b.level like '%").append(criteria.getLevel()).append("%' ");
        }
        if (null != criteria.getPickType() && null != criteria.getScore()) {
            sb.append("and b.score >= ").append(criteria.getScore() - criteria.getMaxScore()).append(" ");
            sb.append("and b.score < ").append(criteria.getScore() - criteria.getMinScore()).append(" ");
        }
        if (StringUtils.isNotBlank(criteria.getMode())) {
            if (StringUtils.equalsAnyIgnoreCase(criteria.getMode(), "文科", "理科")) {
                sb.append("and b.type = '").append(criteria.getMode().trim()).append("' ");
            } else {
                sb.append("and b.course0 = '").append(StringUtils.split(criteria.getMode(), ",")[0]).append("' ");
            }
        }
        if (StringUtils.isNotBlank(criteria.getYear())) {
            sb.append("and b.year = '").append(criteria.getYear().trim()).append("' ");
        }
        sb.append("order by marjorBelongs,score desc ");
        return bBusiWishUniversitySubmitMarjorsMapper.query(sb.toString());
    }

    @Override
    public List<String> levels(String location, Integer year) {
        return bBusiWishUniversitySubmitMarjorsMapper.levels( location,  year);
    }

    @Override
    public List<String> locations() {
        List<String> list = bBusiWishUniversitySubmitMarjorsMapper.locations();
        String[] strArray = configService.selectConfigByKey("system.open.province").split(",");
        List<String> strList = new ArrayList<>(strArray.length);
        Collections.addAll(strList, strArray);
        List<String> intersection = list.stream().filter(strList::contains).collect(Collectors.toList());
        return intersection;
    }

    @Override
    public List<String> types(String location, Integer year) {
        return bBusiWishUniversitySubmitMarjorsMapper.types( location,  year);
    }

    @Override
    public List<String> years(String location, String type, String level) {
        return bBusiWishUniversitySubmitMarjorsMapper.years( location,  type,  level);
    }

    @Override
    public List<String> getUniversityNameText(String universityCode,String location, Integer year, String type, String level) {
        return bBusiWishUniversitySubmitMarjorsMapper.getUniversityNameText(universityCode, location,  year,  type,  level);
    }
    /**
     * 新增院校专业分数线
     *
     * @param bBusiWishUniversitySubmitMarjors 院校专业分数线
     * @return 结果
     */
    @Override
    public int insertBBusiWishUniversitySubmitMarjors(BBusiWishUniversitySubmitMarjors bBusiWishUniversitySubmitMarjors) {
        bBusiWishUniversitySubmitMarjors.setCreateTime(DateUtils.getNowDate());
        return bBusiWishUniversitySubmitMarjorsMapper.insertBBusiWishUniversitySubmitMarjors(bBusiWishUniversitySubmitMarjors);
    }

    /**
     * 修改院校专业分数线
     *
     * @param bBusiWishUniversitySubmitMarjors 院校专业分数线
     * @return 结果
     */
    @Override
    public int updateBBusiWishUniversitySubmitMarjors(BBusiWishUniversitySubmitMarjors bBusiWishUniversitySubmitMarjors) {
        return bBusiWishUniversitySubmitMarjorsMapper.updateBBusiWishUniversitySubmitMarjors(bBusiWishUniversitySubmitMarjors);
    }

    /**
     * 批量删除院校专业分数线
     *
     * @param ids 需要删除的院校专业分数线ID
     * @return 结果
     */
    @Override
    public int deleteBBusiWishUniversitySubmitMarjorsByIds(Long[] ids) {
        return bBusiWishUniversitySubmitMarjorsMapper.deleteBBusiWishUniversitySubmitMarjorsByIds(ids);
    }

    /**
     * 删除院校专业分数线信息
     *
     * @param id 院校专业分数线ID
     * @return 结果
     */
    @Override
    public int deleteBBusiWishUniversitySubmitMarjorsById(Long id) {
        return bBusiWishUniversitySubmitMarjorsMapper.deleteBBusiWishUniversitySubmitMarjorsById(id);
    }
}
