package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.modules.sys.dao.algs.AlgorithmMapper;
import io.renren.modules.sys.dao.algs.AlgorithmModelVoMapper;
import io.renren.modules.sys.dao.algs.AlgorithmVoMapper;
import io.renren.modules.sys.entity.algs.Algorithm;
import io.renren.modules.sys.entity.algs.AlgorithmModel;
import io.renren.modules.sys.entity.algs.AlgorithmModel_Vo;
import io.renren.modules.sys.entity.algs.Algorithm_Vo;
import io.renren.modules.sys.service.AlgsService;
import io.renren.modules.sys.service.CategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author: Ivan Q
 * @Date: 2021/6/10 20:31
 */
@Service("algorithmService")
public class AlgsServiceImpl extends ServiceImpl<AlgorithmMapper, Algorithm> implements AlgsService {
    @Autowired
    AlgorithmVoMapper algorithmVoMapper;
    @Autowired
    AlgorithmMapper algorithmMapper;
    @Autowired
    CategoryService categoryService;


    //获取模板列表,使用的是@select连表查询
    //但是怎么把搜索功能加进去呢？？？？
    @Override
    public PageUtils queryPageOnUse(Map<String, Object> params) {
        //获取搜索框搜索的模板名称.
        String algorithmName = (String)params.get("algorithmName");
        String classificationtag = (String)params.get("classificationtag");

        Long categoryId = categoryService.getByName(classificationtag);
        //获取当前登录的用户,如果是管理员的话，uid就为null
        Long uid = (Long) params.get("currentUserId");
        List<Algorithm_Vo> algsInfo;
        List<Algorithm_Vo> algsAllInfo;
        int pageSize = Integer.valueOf((String) params.get("limit"));
        int pageIndex = Integer.valueOf((String) params.get("page"));

//        此方法如果修改,则也需要修改下面的获取总页数的方法
        algsInfo = algorithmVoMapper.getAlgorithmInfo(new QueryWrapper<Algorithm_Vo>()
                        .like(StringUtils.isNotBlank(algorithmName), "algorithm_name", algorithmName)
                        //根据登录的用户展示他自己的模板
                        .eq(uid != null, "uid", uid)
                        .eq(classificationtag != null, "category_id", categoryId)
                        .orderByDesc("algorithm_id")
//                使用limit用于分页,下面的函数没有limit进行分页
                        .last("limit " + (pageIndex - 1) * pageSize + "," + pageSize + ";")

        );

        //用于查全部，获取总页数。
        algsAllInfo = algorithmVoMapper.getAlgorithmInfo(new QueryWrapper<Algorithm_Vo>()
                .like(StringUtils.isNotBlank(algorithmName), "algorithm_name", algorithmName)
                //根据登录的用户展示他自己的模板
                .eq(uid != null, "uid", uid)
                .eq(classificationtag != null, "category_id", categoryId)
        );

        for (Algorithm_Vo vo : algsInfo) {
            vo.setCategoryName(categoryService.getById(vo.getCategoryId()).getCategoryName());
        }

        //弃用的方法
        //这个方法：根据当前登录的用户id返回只有当前用户的数据，但是没有结合搜索框搜索
        //templateInfo = algorithmTemplateVoMapper.getTemplateInfoById(uid);
        return new PageUtils(algsInfo, algsAllInfo.size(), pageSize, pageIndex);

    }



    @Override
    public void deleteBatch(Long[] alglIds) {
        this.removeByIds(Arrays.asList(alglIds));
    }

    @Override
    public Long querycategoryId(Long algId) {
        return null;
    }

    @Override
    @Transactional
    public void update(Algorithm alg) {
        this.updateById(alg);
    }

    @Override
    public Algorithm selectByAlgName(String algorithmName) {
        return algorithmMapper.selectByAlgName(algorithmName);
    }

    /**
     * 创建训练任务时显示算法，用去区分传统算法与智能算法和算法类别
     * @param params
     * @return
     */
    @Override
    public List<Algorithm_Vo> selectOnCreateMission(Map<String, Object> params) {
        String classificationtag = (String)params.get("classificationtag");
        String frameId = (String)params.get("frameId");
        Long categoryId = categoryService.getByName(classificationtag);

        //获取当前登录的用户,如果是管理员的话，uid就为null
        Long uid = (Long) params.get("currentUserId");
        List<Algorithm_Vo> algsInfo;
        System.out.println("当前frameId为："+frameId);
        if("-1".equals(frameId)){
            algsInfo = algorithmVoMapper.getAlgorithmInfo(new QueryWrapper<Algorithm_Vo>()
                    //根据登录的用户展示他自己的模板
                    .eq(uid != null, "uid", uid)
                    .eq(classificationtag != null, "category_id", categoryId)
                    .eq(frameId != null,"frame_id",frameId)
            );
        }else {
            algsInfo = algorithmVoMapper.getAlgorithmInfo(new QueryWrapper<Algorithm_Vo>()
                    //根据登录的用户展示他自己的模板
                    .eq(uid != null, "uid", uid)
                    .eq(classificationtag != null, "category_id", categoryId)
                    .ne(frameId != null,"frame_id",-1)
            );
        }
        for (Algorithm_Vo vo : algsInfo) {
            vo.setCategoryName(categoryService.getById(vo.getCategoryId()).getCategoryName());
        }
        return algsInfo;
    }


}
