package com.ruoyi.syzy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.google.common.collect.Lists;
import com.ruoyi.util.PageUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.NewgaokaoType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mxjb.domain.MxjbSelectedGroup;
import com.ruoyi.mxjb.mapper.MxjbSelectedGroupMapper;
import com.ruoyi.mxjb.mapper.MxjbSelectedRoundMapper;
import com.ruoyi.syzy.criteria.XkxcCriteria;
import com.ruoyi.syzy.domain.BBusiWishXkcx;
import com.ruoyi.syzy.domain.BBusiWishXkcxCourseGroup;
import com.ruoyi.syzy.domain.BCustomerXkcx;
import com.ruoyi.syzy.domain.BCustomerXkcxCourse;
import com.ruoyi.syzy.dto.XkxcSchemeDTO;
import com.ruoyi.syzy.mapper.*;
import com.ruoyi.syzy.service.IBBusiWishXkcxService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 院校专业分数线Service业务层处理
 *
 * @author mingxue
 * @date 2021-08-18
 */
@Service
public class BBusiWishXkcxServiceImpl implements IBBusiWishXkcxService {
    @Autowired
    private BBusiWishXkcxMapper bBusiWishXkcxMapper;
    @Autowired
    private BBusiWishXkcxCourseGroupMapper courseGroupMapper;
    @Autowired
    private BCustomerXkcxMapper customerXkcxMapper;
    @Autowired
    private BCustomerXkcxCourseMapper customerXkcxCourseMapper;
    @Autowired
    private MxjbSelectedRoundMapper mxjbSelectedRoundMapper;
    @Autowired
    private MxjbSelectedGroupMapper selectedGroupMapper;
    @Autowired
    private BBusiWishUniversitiesMapper bBusiWishUniversitiesMapper;

    /**
     * 查询院校专业分数线
     *
     * @param id 院校专业分数线ID
     * @return 院校专业分数线
     */
    @Override
    public BBusiWishXkcx selectBBusiWishXkcxById(Long id) {
        return bBusiWishXkcxMapper.selectBBusiWishXkcxById(id);
    }

    @Override
    public List<Integer> selectBBusiWishXkcxYears(String location) {
        return bBusiWishXkcxMapper.selectBBusiWishXkcxYears(location);
    }

    @Override
    public Map<String,Object> selectBBusiWishXkcxCourse() {
        String newgaokaoType =SecurityUtils.getLoginUser().getUser().getNewgaokaoType() ;// 1对应3+1+2  ；2对应3+3
        List<MxjbSelectedGroup> list =selectedGroupMapper.selectMxjbSelectedGroupList(new MxjbSelectedGroup().setNewgaokaoType(newgaokaoType));
        Map<String,Object> resultMap = new HashMap<>();
        Set<String> course0Set = new HashSet<>();
        Set<String> course1Set = new HashSet<>();
        if(NewgaokaoType.isNewgaokao312(newgaokaoType)){
            list.forEach(ll->{
                for (String s : StringUtils.split(ll.getCourse0(), ",")) {
                    course0Set.add(s);
                }
                for (String s : StringUtils.split(ll.getCourse1(), ",")) {
                    course1Set.add(s);
                }
            });

            resultMap.put("course0",course0Set);
            resultMap.put("course1",course1Set);
        }else if(NewgaokaoType.isNewgaokao33(newgaokaoType)){
            list.forEach(ll->{
                for (String s : StringUtils.split(ll.getCourse0(), ",")) {
                    course1Set.add(s);
                }
                for (String s : StringUtils.split(ll.getCourse1(), ",")) {
                    course1Set.add(s);
                }
            });

            resultMap.put("course1",course1Set);
        }

        //六科图标

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("物理","https://mingxuejingbang.oss-cn-beijing.aliyuncs.com/courseImgs/icon_wuli.png");
        jsonObject.put("化学","https://mingxuejingbang.oss-cn-beijing.aliyuncs.com/courseImgs/icon_huaxue.png");
        jsonObject.put("生物","https://mingxuejingbang.oss-cn-beijing.aliyuncs.com/courseImgs/icon_shengwu.png");
        jsonObject.put("历史","https://mingxuejingbang.oss-cn-beijing.aliyuncs.com/courseImgs/icon_lishi.png");
        jsonObject.put("政治","https://mingxuejingbang.oss-cn-beijing.aliyuncs.com/courseImgs/icon_zhengzhi.png");
        jsonObject.put("地理","https://mingxuejingbang.oss-cn-beijing.aliyuncs.com/courseImgs/icon_dili.png");

        resultMap.put("courseImgs",jsonObject);

        return resultMap;
    }

    /**
     * 查询院校专业分数线列表
     *
     * @param bBusiWishXkcx 院校专业分数线
     * @return 院校专业分数线
     */
    @Override
    public List<BBusiWishXkcx> selectBBusiWishXkcxList(BBusiWishXkcx bBusiWishXkcx) {
        return bBusiWishXkcxMapper.selectBBusiWishXkcxList(bBusiWishXkcx);
    }

    @Override
    public List<BBusiWishXkcx> selectBBusiWishXkcxRandom(Integer count) {
        return bBusiWishXkcxMapper.selectBBusiWishXkcxRandom(count);
    }

    @Override
    public List<Long> selectUniversityIds(XkxcCriteria criteria) {
        StringBuilder tsb = new StringBuilder();
        processUniversityQueryCondition(tsb, criteria, false);
        if (tsb.length() > 0) {
            return bBusiWishUniversitiesMapper.listForCourses(tsb.toString());
        }
        return null;
    }

    @Override
    public List<BBusiWishXkcx> listByCourses(XkxcCriteria criteria) {
        if(NewgaokaoType.isNotNewgaokao33(SecurityUtils.getLoginUser().getUser().getProvinceName())){
            //3+1+2才需要判断
            if (StringUtils.isBlank(criteria.getCourse0())) {
                throw new CustomException("首选科目不能为空!");
            }
        }

        if (StringUtils.isBlank(criteria.getCourse1())) {
            throw new CustomException("次选科目不能为空!");
        }
        StringBuilder sb = new StringBuilder();
        sb.append(" where status > 0 ");
        // 当前登录用户所有省份
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (null != user.getProvinceName()) {
            sb.append(" and location = '").append(user.getProvinceName()).append("' ");
        }
        //查询学校的5个条件
        if (null != criteria.getUniversityIds()) {
            if (CollectionUtils.isEmpty(criteria.getUniversityIds())) {
                return Lists.newArrayList();
            }
            sb.append(" and universityId in (").append(StringUtils.join(criteria.getUniversityIds(), ",")).append(")");
        }
        StringBuilder temp = new StringBuilder();
        if(NewgaokaoType.isNewgaokao33(SecurityUtils.getLoginUser().getUser().getProvinceName())){
            //山东3+3新高考 只需要处理mode1:历史,地理,物理
            // 科目1
            final String course1 = criteria.getCourse1();
            //匹配course1
            for (String str : course1.split(",")) {
                if (temp.length() != 0) {
                    temp.append(" or ");
                }
                temp.append(String.format("find_in_set('%s',course1)", str));
            }
            temp.append(" or (course1='' and course2='') ");

            sb.append(" and (");
            sb.append(String.format("(%s)", temp.toString()));

            // 历史地理物理：["历史,地理,物理","历史,物理,地理","地理,历史,物理","地理,物理,历史","物理,历史,地理","物理,地理,历史"]
            // 匹配course2  三科：全匹配，两科：模糊匹配
            List<String> courseAssembleList= StringUtils.getAssemble(criteria.getCourse1());

            temp = new StringBuilder();
            for (String str : courseAssembleList) {
                if (temp.length() != 0) {
                    temp.append(" or ");
                }
//                temp.append(String.format("find_in_set(course2,'%s')", str));
                //		OR '物理,化学,政治' like concat('%',course2,'%')
                temp.append(String.format(" '%s' like %s", str,String.format("%s","concat('%',course2,'%')")));
            }
            sb.append(" or ");
            sb.append(String.format("(%s)", temp.toString()));
            sb.append(" )");

//            sb.append(String.format("((%s) or ( course2='%s' or course2='%s'))", temp.toString(), criteria.getCourse1(),StringUtils.getAssemble(criteria.getCourse1())));
        }else {
            //3+1+2模式
            // 科目0
            sb.append(String.format(" and (course0='' or course0 = '%s')", criteria.getCourse0()));
            // 科目1
            final String course1 = criteria.getCourse1();
            for (String str : course1.split(",")) {
                if (temp.length() != 0) {
                    temp.append(" or ");
                }
                temp.append(String.format("find_in_set('%s',course1)", str));
            }
            temp.append(" or course1=''");

            sb.append(" and ");
            sb.append(String.format("((%s) or ( course2='%s' or course2='%s'))", temp.toString(), criteria.getCourse1(),StringUtils.reverseCourse(criteria.getCourse1())));

        }
        // 专业
        if (StringUtils.isNotBlank(criteria.getMarjors())) {
            temp.delete(0, temp.length());
            for (String str : criteria.getMarjors().split(",")) {
                if (temp.length() != 0) {
                    temp.append(" or ");
                }
                temp.append("(marjorName like '%").append(str.trim()).append("%'");
                temp.append(" or marjorBelongs like '%").append(str.trim()).append("%')");
            }
            sb.append(" and (").append(temp.toString()).append(")");
        }
        // 关键字
        if (StringUtils.isNotBlank(criteria.getKeyword())) {
            sb.append(" and (universityName like '%").append(criteria.getKeyword().trim()).append("%' ");
            sb.append(" or marjorName like '%").append(criteria.getKeyword().trim()).append("%')");
        }
        if (null != criteria.getYear()) {
            sb.append(" and year  = ").append(criteria.getYear());
        }

        List<BBusiWishXkcx> list = bBusiWishXkcxMapper.listByCourses(sb.toString());
        processCollect(list);
        return list;
    }

    /**
     * 查询学校的5个条件
     * @param sb
     * @param criteria
     * @return
     */
    private StringBuilder processUniversityQueryCondition(StringBuilder sb,XkxcCriteria criteria, boolean forCourse){
        //查询学校的5个条件
        if (StringUtils.isNotBlank(criteria.getLocation())||StringUtils.isNotBlank(criteria.getNatureTypeCN())||
                StringUtils.isNotBlank(criteria.getType())||StringUtils.isNotBlank(criteria.getLevel())||
                StringUtils.isNotBlank(criteria.getFeatures())){
            //初始
            if (forCourse) {
                sb.append(" and universityId in (select id from b_busi_wish_universities where status>0 ");
            }
            // 院校所在省份
            if (StringUtils.isNotBlank(criteria.getLocation())){
//                sb.append(" and (location like '%").append(criteria.getLocation().trim()).append("%')");
                String[] locations = StringUtils.split(criteria.getLocation(), ",");
                getMultipleQuery(sb,locations,"location");
//                sb.append(" and (");
//                for ( int i=0;i<locations.length;i++ ) {
//                    if(i!=0){
//                        sb.append(" or ");
//                    }
//                    String location = locations[i];
//                    sb.append(" location like '%").append(location.trim()).append("%'");
//                }
//                sb.append(")");
            }
            //院校办学方式："private"私立, "aw_ga"中外/港澳, "public"公立
            if (StringUtils.isNotBlank(criteria.getNatureTypeCN())) {
//                sb.append(" and (natureTypeCN like '%").append(criteria.getNatureTypeCN().trim()).append("%')");
                String[] natureTypeCNs = StringUtils.split(criteria.getNatureTypeCN(), ",");
                getMultipleQuery(sb,natureTypeCNs,"natureTypeCN");
            }
            // 院校类别 体育,其他,理工,军事,农林,医药,政法,民族,综合,师范,艺术,语言,财经
            if (StringUtils.isNotBlank(criteria.getType())) {
//                sb.append(" and (type like '%").append(criteria.getType().trim()).append("%')");
                String[] types = StringUtils.split(criteria.getType(), ",");
                getMultipleQuery(sb,types,"type");
            }
            // 院校层级 专科/本科
            if (StringUtils.isNotBlank(criteria.getLevel())) {
                //level:本科、高职专科   高职专科-->处理为“专科”
                String level = criteria.getLevel().trim();
                if(level.contains("专科")) {
                    level=level.replace("高职专科","专科").replace("高职(专科)","专科");
                }
//                sb.append(" and (level like '%").append(level).append("%')");
                String[] levels = StringUtils.split(level, ",");
                getMultipleQuery(sb,levels,"level");
            }
            // 院校特色 985,211,双一流A,国重点,一流学科建设
            if (StringUtils.isNotBlank(criteria.getFeatures())) {
//                sb.append(" and (features like '%").append(criteria.getFeatures().trim()).append("%')");
                String[] featuresSS = StringUtils.split(criteria.getFeatures(), ",");
                getMultipleQuery(sb,featuresSS,"features");
            }

            //结束
            if (forCourse) {
                sb.append(")");
            }
        }
        return sb;
    }

    /**
     * 处理学校多选
     * @param sb
     * @param locations
     * @param queryCondition
     * @return
     */
    private StringBuilder getMultipleQuery(StringBuilder sb,String[] locations,String queryCondition){
        sb.append(" and (");
        for ( int i=0;i<locations.length;i++ ) {
            if(i!=0){
                sb.append(" or ");
            }
            String location = locations[i];
            sb.append(" "+queryCondition+" like '%").append(location.trim()).append("%'");
        }
        sb.append(") ");

        return sb;
    }



    /**
     * 处理收藏及选科收藏
     * @param list
     * @return
     */
    private List<BBusiWishXkcx> processCollect(List<BBusiWishXkcx> list){
        List<Long> collectIds = getCollectIdsByUser();
        List<Long> collectSelectIds = getCollectSelectIdsByUser();

        list.forEach(row -> {
            row.setCollect(collectIds.contains(row.getId()));
            row.setIsSelectCourse(collectSelectIds.contains(row.getId()));
        });

        return list;
    }

    @Override
    public List<BBusiWishXkcx> listByMarjors(XkxcCriteria criteria) {
        StringBuilder sb = new StringBuilder();
        sb.append(" where status > 0 ");
        // 当前登录用户所有省份
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (null != user.getProvinceName()) {
            sb.append(" and location = '").append(user.getProvinceName()).append("' ");
        }

        StringBuilder temp = new StringBuilder();
        //查询学校的5个条件
        if (null != criteria.getUniversityIds()) {
            if (CollectionUtils.isEmpty(criteria.getUniversityIds())) {
                return Lists.newArrayList();
            }
            sb.append(" and universityId in (").append(StringUtils.join(criteria.getUniversityIds(), ",")).append(")");
        }
        if (StringUtils.isNotBlank(criteria.getLevel())) {
            if (StringUtils.isNotBlank(criteria.getLevel())) {
                //level:本科、高职专科   高职专科-->处理为“专科”
                String level = criteria.getLevel().trim();
                if(level.contains("专科")) {
                    level=level.replace("高职专科","专科").replace("高职(专科)","专科");
                }
//                sb.append(" and (level like '%").append(level).append("%')");
                String[] levels = StringUtils.split(level, ",");
                getMultipleQuery(sb,levels,"level");
            }
        }
            // 专业
        if (StringUtils.isNotBlank(criteria.getMarjors())) {
            temp.delete(0, temp.length());
            for (String str : criteria.getMarjors().split(",")) {
                if (temp.length() != 0) {
                    temp.append(" or ");
                }
                temp.append("(marjorName like '%").append(str.trim()).append("%'");
                temp.append(" or marjorBelongs like '%").append(str.trim()).append("%')");
            }
            sb.append(" and (").append(temp.toString()).append(")");
        }
        // 关键字
        if (StringUtils.isNotBlank(criteria.getKeyword())) {
            sb.append(" and (universityName like '%").append(criteria.getKeyword().trim()).append("%' ");
            sb.append(" or marjorName like '%").append(criteria.getKeyword().trim()).append("%')");
        }
        if (null != criteria.getYear()) {
            sb.append(" and year  = '").append(criteria.getYear()).append("'");
        }
        sb.append(" order by universityCode asc ");

        List<BBusiWishXkcx> list = bBusiWishXkcxMapper.listByCourses(sb.toString());
        processCollect(list);

        return list;
    }



    @Override
    public List<BBusiWishXkcx> listCoursesByUser(XkxcCriteria criteria) {
        Long schoolId = SecurityUtils.getLoginUser().getSchoolId();
        String ucode = SecurityUtils.getLoginUser().getUser().getCode();

        /**
         * 我的收藏：使用原方法
         */
        if (null != criteria.getCollect()&&criteria.getCollect()) {
            List<Long> collectIds = getCollectIdsByUser();
            if (CollectionUtils.isEmpty(collectIds)) {
                return Collections.emptyList();
            }
            StringBuilder sb = new StringBuilder();
            sb.append(" where 1=1 ");
            sb.append(" and status > 0 ");

            sb.append(" and id in(").append(StringUtils.join(collectIds, ",")).append(") ");
            List<BBusiWishXkcx> list = bBusiWishXkcxMapper.listByCourses(sb.toString());
            processCollect(list);
            if (collectIds instanceof Page) {
                return PageUtil.getDtoListWithPageable(collectIds, list);
            }
            return list;
        }else if (null != criteria.getIsSelectCourse()&&criteria.getIsSelectCourse()) {
            //我的选科收藏
            BCustomerXkcx query = new BCustomerXkcx();
            query.setCustomerCode(ucode);
            query.setIsSelectCourse(1);
            if(null!=criteria.getQuerySource()&&criteria.getQuerySource()==2){
                query.setRoundId(criteria.getRoundId());
            }
            List<BBusiWishXkcx> list = bBusiWishXkcxMapper.listByCourses2(query);
            processCollect(list);
            return list;
        }

        return Collections.emptyList();
    }

    public List<Long> getCollectIdsByUser() {
        try {
            String ucode = SecurityUtils.getLoginUser().getUser().getCode();
            BCustomerXkcx exam = new BCustomerXkcx();
            exam.setCustomerCode(ucode);
            exam.setStatus(1);

            List<BCustomerXkcx> list = customerXkcxMapper.selectBCustomerXkcxList2(exam);
            Set<Long> set = list.stream().map(t -> t.getSubmitId()).collect(Collectors.toSet());
            if (list instanceof Page) {
                return PageUtil.getDtoListWithPageable(list, new ArrayList<>(set));
            }
            return new ArrayList<>(set);
        } catch (Exception ige) {
        }
        return new ArrayList<>();
    }

    public List<Long> getCollectSelectIdsByUser() {
        List<Long> list = new ArrayList<>();
        try {
            String ucode = SecurityUtils.getLoginUser().getUser().getCode();
            BCustomerXkcx exam = new BCustomerXkcx();
            exam.setCustomerCode(ucode);
            exam.setIsSelectCourse(1);
            customerXkcxMapper.selectBCustomerXkcxList2(exam).forEach(row -> {
                list.add(row.getSubmitId());
            });
        } catch (Exception ige) {
        }
        return list;
    }

    /**
     * 新增院校专业分数线
     *
     * @param bBusiWishXkcx 院校专业分数线
     * @return 结果
     */
    @Override
    public int insertBBusiWishXkcx(BBusiWishXkcx bBusiWishXkcx) {
        bBusiWishXkcx.setCreateTime(DateUtils.getNowDate());
        return bBusiWishXkcxMapper.insertBBusiWishXkcx(bBusiWishXkcx);
    }

    /**
     * 修改院校专业分数线
     *
     * @param bBusiWishXkcx 院校专业分数线
     * @return 结果
     */
    @Override
    public int updateBBusiWishXkcx(BBusiWishXkcx bBusiWishXkcx) {
        return bBusiWishXkcxMapper.updateBBusiWishXkcx(bBusiWishXkcx);
    }

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

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

    @Override
    public void addCollect(Long refId) {
        String ucode = SecurityUtils.getLoginUser().getUser().getCode();
        BCustomerXkcx exam = new BCustomerXkcx();
        exam.setCustomerCode(ucode);
        exam.setSubmitId(refId);
        List<BCustomerXkcx> coll = customerXkcxMapper.selectBCustomerXkcxList(exam);
        if (CollectionUtils.isNotEmpty(coll)) {
            BCustomerXkcx entity = coll.get(0);
            entity.setStatus(1);
            customerXkcxMapper.updateBCustomerXkcx(entity);
        } else {
            BCustomerXkcx entity = new BCustomerXkcx();
            entity.setCustomerCode(ucode);
            entity.setSubmitId(refId);
            entity.setStatus(1);
            entity.setCreateTime(new Date());
            customerXkcxMapper.insertBCustomerXkcx(entity);
        }
    }

    @Override
    public void removeCollect(Long refId) {
        String ucode = SecurityUtils.getLoginUser().getUser().getCode();
        BCustomerXkcx exam = new BCustomerXkcx();
        exam.setCustomerCode(ucode);
        exam.setSubmitId(refId);
        List<BCustomerXkcx> coll = customerXkcxMapper.selectBCustomerXkcxList(exam);
        if (CollectionUtils.isNotEmpty(coll)) {
            BCustomerXkcx entity = coll.get(0);
            entity.setStatus(0);
            customerXkcxMapper.updateBCustomerXkcx(entity);
        }
    }

    @Override
    public List<XkxcSchemeDTO> listSchemeBySubmitId(List<String> idList) {
        Set<String> course0 = new LinkedHashSet<>(2);
        Set<String> course1 = new LinkedHashSet<>(2);
        Set<String> course2 = new LinkedHashSet<>(2);
        StringBuilder sb = new StringBuilder();
        sb.append(" where id in(").append(StringUtils.join(idList, ",")).append(")");
        List<BBusiWishXkcx> coll = this.bBusiWishXkcxMapper.listByCourses(sb.toString());
        for (BBusiWishXkcx row : coll) {
            if (StringUtils.isNotBlank(row.getCourse0())) {
                course0.add(row.getCourse0());
            }
            if (StringUtils.isNotBlank(row.getCourse1())) {
                for (String str : StringUtils.split(row.getCourse1(), ",")) {
                    course1.add(str);
                }
            }
            if (StringUtils.isNotBlank(row.getCourse2())) {
                for (String str : StringUtils.split(row.getCourse2(), ",")) {
                    course2.add(str);
                }
            }
        }
        if (course0.isEmpty()) {
            course0.addAll(Arrays.asList("物理", "历史"));
        }
        if (course1.isEmpty()) {
            course1.addAll(Arrays.asList("化学", "生物", "地理", "政治"));
        }
        if (course2.isEmpty()) {
            course2.addAll(Arrays.asList("化学", "生物", "地理", "政治"));
        }
        Set<String> courses = new LinkedHashSet<>(12);
        for (String c0 : course0) {
            for (String c1 : course1) {
                for (String c2 : course2) {
                    if (StringUtils.equals(c1, c2)) {
                        continue;
                    }
                    courses.add(String.format("%s %s %s", c0, c1, c2));
                }
            }
        }

        Map<String, BBusiWishXkcxCourseGroup> groupCache = new HashMap<>(12);
        courseGroupMapper.selectBBusiWishXkcxCourseGroupList(new BBusiWishXkcxCourseGroup().setStatus(1)).forEach(row -> {
            final String key = String.format("%s %s %s", row.getCourse0(), row.getCourse1(), row.getCourse2());
            groupCache.put(key, row);
        });
        Set<Long> groupCollectIds = getGroupCollectIdsByUser();
        List<XkxcSchemeDTO> list = new ArrayList<>(courses.size());
        for (String course : courses) {
            BBusiWishXkcxCourseGroup group = groupCache.get(course);
            if (null == group) {
                continue;
            }
            XkxcSchemeDTO row = new XkxcSchemeDTO();
            final Long groupId = group.getId();
            row.setId(groupId).setCourse(course).setCourseId(groupId);
            row.setMarjorRate(null == group.getMarjorRate() ? 0 : group.getMarjorRate());
            row.setCollect(groupCollectIds.contains(groupId));
            list.add(row);
        }
        return list;
    }

    /**
     * 获取已选择的方案
     *
     * @return
     */
    public Set<Long> getGroupCollectIdsByUser() {
        Set<Long> set = new HashSet<>();
        try {
            String ucode = SecurityUtils.getLoginUser().getUser().getCode();
            BCustomerXkcxCourse exam = new BCustomerXkcxCourse();
            exam.setCustomerCode(ucode);
            exam.setStatus(1);
            customerXkcxCourseMapper.selectBCustomerXkcxCourseList(exam).forEach(row -> {
                set.add(row.getGroupId());
            });
        } catch (Exception ige) {
        }
        return set;
    }

    @Override
    public List<XkxcSchemeDTO> listMyScheme() {
        Map<Long, BBusiWishXkcxCourseGroup> groupCache = new HashMap<>(12);
        courseGroupMapper.selectBBusiWishXkcxCourseGroupList(new BBusiWishXkcxCourseGroup().setStatus(1)).forEach(row -> {
            groupCache.put(row.getId(), row);
        });
        List<XkxcSchemeDTO> list = new ArrayList<>(12);
        String ucode = SecurityUtils.getLoginUser().getUser().getCode();
        BCustomerXkcxCourse exam = new BCustomerXkcxCourse();
        exam.setCustomerCode(ucode);
        exam.setStatus(1);
        customerXkcxCourseMapper.selectBCustomerXkcxCourseList(exam).forEach(row -> {
            BBusiWishXkcxCourseGroup group = groupCache.get(row.getGroupId());
            if (null == group) {
                return;
            }
            XkxcSchemeDTO item = new XkxcSchemeDTO();
            item.setId(row.getId());
            item.setCourseId(row.getGroupId());
            item.setCourse(String.format("%s %s %s", group.getCourse0(), group.getCourse1(), group.getCourse2()));
            item.setMarjorRate(null == group.getMarjorRate() ? 0 : group.getMarjorRate());
            item.setDf(Integer.valueOf(1).equals(row.getDf()));
            list.add(item);
        });
        return list;
    }

}
