package com.ruoyi.syzy.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
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.UniversityTdxCriteria;
import com.ruoyi.syzy.criteria.ZytbUniversityCriteria;
import com.ruoyi.syzy.domain.BBusiWishUniversitySubmits;
import com.ruoyi.syzy.mapper.BBusiWishUniversitySubmitsMapper;
import com.ruoyi.syzy.service.IBBusiWishUniversitySubmitsService;

/**
 * 志愿院校录取分数线Service业务层处理
 * 
 * @author mingxue
 * @date 2021-08-20
 */
@Service
public class BBusiWishUniversitySubmitsServiceImpl implements IBBusiWishUniversitySubmitsService {
    @Autowired
    private BBusiWishUniversitySubmitsMapper bBusiWishUniversitySubmitsMapper;


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

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

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

    /**
     * 查询志愿院校录取分数线
     * 
     * @param id 志愿院校录取分数线ID
     * @return 志愿院校录取分数线
     */
    @Override
    public BBusiWishUniversitySubmits selectBBusiWishUniversitySubmitsById(Long id) {
        return bBusiWishUniversitySubmitsMapper.selectBBusiWishUniversitySubmitsById(id);
    }

    /**
     * 查询志愿院校录取分数线列表
     * 
     * @param bBusiWishUniversitySubmits 志愿院校录取分数线
     * @return 志愿院校录取分数线
     */
    @Override
    public List<BBusiWishUniversitySubmits> selectBBusiWishUniversitySubmitsList(BBusiWishUniversitySubmits bBusiWishUniversitySubmits) {
        return bBusiWishUniversitySubmitsMapper.selectBBusiWishUniversitySubmitsList(bBusiWishUniversitySubmits);
    }

    @Override
    public List<BBusiWishUniversitySubmits> search(UniversityTdxCriteria criteria) {
        final List<BBusiWishUniversitySubmits> list = bBusiWishUniversitySubmitsMapper.search(criteria.toMap());
//        list.forEach(row -> {
//            row.setBatchName(BatchUtil.getLabel(row.getBatch()));
//        });
        return list;
    }

    @Override
    public List<JSONObject> zytb(ZytbUniversityCriteria criteria) {
        StringBuilder sb = new StringBuilder();
        sb.append("select b.*,a.*,a.id as id,b.id as submitId,a.location as universityLocation,a.type as universityType ");
        sb.append("from b_busi_wish_universities a,b_busi_wish_university_submits b ");
        sb.append("where a.status>0 and b.status>0 and a.id = b.universityId ");
        if (StringUtils.isNotBlank(criteria.getLocation())) {
            // 地域
            sb.append("and a.location like '%").append(criteria.getLocation().trim()).append("%' ");
        }
        //用户地区
        String ulocation = SecurityUtils.getLoginUser().getUser().getLocation();
        if(StringUtils.isNotBlank(ulocation)) {
            sb.append("and b.location like '%").append(ulocation.trim()).append("%' ");
        }
        if (StringUtils.isNotBlank(criteria.getType())) {
            // 院校类型
            sb.append("and a.type like '%").append(criteria.getType().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())) {
            sb.append("and b.type = '").append(criteria.getMode().trim()).append("' ");
            /** 在上面已经处理过mode
            if (StringUtils.equalsAnyIgnoreCase(criteria.getMode(), "文科", "理科")) {
                sb.append("and b.type = '").append(criteria.getMode().trim()).append("' ");
            } else {
                String type = StringUtils.split(criteria.getMode(), ",")[0];//物理或历史
                type = type.contains("历史")?"文科":"理科";
                sb.append("and b.type = '").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 b.year desc,b.score desc,a.id");
        return bBusiWishUniversitySubmitsMapper.query(sb.toString());
    }

    /**
     * 新增志愿院校录取分数线
     * 
     * @param bBusiWishUniversitySubmits 志愿院校录取分数线
     * @return 结果
     */
    @Override
    public int insertBBusiWishUniversitySubmits(BBusiWishUniversitySubmits bBusiWishUniversitySubmits) {
        bBusiWishUniversitySubmits.setCreateTime(DateUtils.getNowDate());
        return bBusiWishUniversitySubmitsMapper.insertBBusiWishUniversitySubmits(bBusiWishUniversitySubmits);
    }

    /**
     * 修改志愿院校录取分数线
     * 
     * @param bBusiWishUniversitySubmits 志愿院校录取分数线
     * @return 结果
     */
    @Override
    public int updateBBusiWishUniversitySubmits(BBusiWishUniversitySubmits bBusiWishUniversitySubmits) {
        return bBusiWishUniversitySubmitsMapper.updateBBusiWishUniversitySubmits(bBusiWishUniversitySubmits);
    }

    /**
     * 批量删除志愿院校录取分数线
     * 
     * @param ids 需要删除的志愿院校录取分数线ID
     * @return 结果
     */
    @Override
    public int deleteBBusiWishUniversitySubmitsByIds(Long[] ids) {
        return bBusiWishUniversitySubmitsMapper.deleteBBusiWishUniversitySubmitsByIds(ids);
    }

    /**
     * 删除志愿院校录取分数线信息
     * 
     * @param id 志愿院校录取分数线ID
     * @return 结果
     */
    @Override
    public int deleteBBusiWishUniversitySubmitsById(Long id) {
        return bBusiWishUniversitySubmitsMapper.deleteBBusiWishUniversitySubmitsById(id);
    }
}
