package com.ruoyi.syzy.service;

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

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.domain.SysMingxueJson;
import com.ruoyi.system.domain.SysMingxueQueryCondition;
import com.ruoyi.system.domain.SysMingxueZhiYuan;
import com.ruoyi.system.mapper.MingxueQueryMapper;
import com.ruoyi.syzy.dto.MingxueRequest;
import com.ruoyi.syzy.dto.MingxueZhiYuan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



/**
 * token验证处理
 *
 * @author ruoyi
 */
@Component
public class MingxueQueryService {
    private static final Logger log = LoggerFactory.getLogger(MingxueQueryService.class);

    @Autowired
    private MingxueQueryMapper mingxueQueryMapper;

    public List<MingxueZhiYuan> queryByCondition(MingxueRequest request) {
        try {
            Integer pageSize = (request.getPageSize() == null) ? 10 : request.getPageSize();
            Integer pageNum = pageSize * (request.getPageNum());
            Integer score = request.getScore();
            String batch = request.getBatch() == null ? "" : request.getBatch().toString();
            String op = (request.getOp() == null) ? "本科" : request.getOp();
            String mode = request.getMode();
            String location = request.getLocation();
            String type = request.getType();
            String marjorRoot = request.getMarjorRoot();

            if (score == null) {
                log.warn("分数填写为空...返回空...");
                return Collections.emptyList();
            }

            Boolean ylxx = request.isYlxx();
            Boolean ylxk = request.isYlxk();
            Boolean yjsy = request.isYjsy();
            Boolean dlxy = request.isDlxy();
            Boolean mbgx = request.isMbgx();

            SysMingxueQueryCondition queryCondition = new SysMingxueQueryCondition();
            queryCondition.setStartNo(pageNum);
            queryCondition.setLimit(pageSize);
            queryCondition.setScore(score);
            queryCondition.setBatch(batch);
            queryCondition.setOp(op);
            queryCondition.setMode(mode);
            queryCondition.setLocation(location);
            queryCondition.setType(type);
            queryCondition.setMarjorRoot(marjorRoot);
            queryCondition.setYlxx(ylxx);
            queryCondition.setYlxk(ylxk);
            queryCondition.setYjsy(yjsy);
            queryCondition.setDlxy(dlxy);
            queryCondition.setMbgx(mbgx);

            List<SysMingxueZhiYuan> sysMingxueZhiYuanList = mingxueQueryMapper.selectMingxueList(queryCondition);
            if(sysMingxueZhiYuanList == null && sysMingxueZhiYuanList.size() == 0){
                log.warn("mingxueQueryMapper.queryListByCondition 未查询到与条件匹配的数据！");
                return Collections.emptyList();
            }

            List<MingxueZhiYuan> mingxueZhiYuanList = new ArrayList<>();
            for(SysMingxueZhiYuan sysMingxueZhiYuan: sysMingxueZhiYuanList){
                MingxueZhiYuan mingxueZhiYuan = new MingxueZhiYuan();
                BeanUtils.copyProperties(sysMingxueZhiYuan, mingxueZhiYuan);
                mingxueZhiYuanList.add(mingxueZhiYuan);
            }

            return mingxueZhiYuanList;
        } catch (Exception e) {
            log.error(e.toString());
            return Collections.emptyList();
        }
    }

    public List<JSONObject> queryJsonByCondition(MingxueRequest request) {
        try {
            Integer pageSize = (request.getPageSize() == null) ? 10 : request.getPageSize();
            Integer pageNum = pageSize * (request.getPageNum());
            Integer score = request.getScore();
            String batch = request.getBatch() == null ? "" : request.getBatch().toString();
            String op = (request.getOp() == null) ? "本科" : request.getOp();
            String mode = request.getMode();
            String location = request.getLocation();
            String type = request.getType();
            String marjorRoot = request.getMarjorRoot();

            if (score == null) {
                log.warn("分数填写为空...返回空...");
                return Collections.emptyList();
            }

            Boolean ylxx = request.isYlxx();
            Boolean ylxk = request.isYlxk();
            Boolean yjsy = request.isYjsy();
            Boolean dlxy = request.isDlxy();
            Boolean mbgx = request.isMbgx();

            SysMingxueQueryCondition queryCondition = new SysMingxueQueryCondition();
            queryCondition.setStartNo(pageNum);
            queryCondition.setLimit(pageSize);
            queryCondition.setScore(score);
            queryCondition.setBatch(batch);
            queryCondition.setOp(op);
            queryCondition.setMode(mode);
            queryCondition.setLocation(location);
            queryCondition.setType(type);
            queryCondition.setMarjorRoot(marjorRoot);
            queryCondition.setYlxx(ylxx);
            queryCondition.setYlxk(ylxk);
            queryCondition.setYjsy(yjsy);
            queryCondition.setDlxy(dlxy);
            queryCondition.setMbgx(mbgx);

            List<SysMingxueZhiYuan> dataList = mingxueQueryMapper.selectMingxueListWithJsonFormat(queryCondition);
            if(dataList == null && dataList.size() == 0){
                log.warn("mingxueQueryMapper.queryListByCondition 未查询到与条件匹配的数据！");
                return Collections.emptyList();
            }

            List<JSONObject> mingxueZhiYuanList = new ArrayList<>();
            for(SysMingxueZhiYuan sysMingxueZhiYuan: dataList){
                try{
                    String uid = sysMingxueZhiYuan.getUniversityId();
                    String year = sysMingxueZhiYuan.getYear();
                    String location1 = sysMingxueZhiYuan.getLocation();
                    SysMingxueJson condition = new SysMingxueJson(uid, year, location1);
                    String json = mingxueQueryMapper.selectSchoolJsonStr(condition);

                    if(json != null && !"".equals(json)){
                        mingxueZhiYuanList.add(JSONObject.parseObject(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            return mingxueZhiYuanList;
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

}
