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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.sys.dao.algs.AlgorithmTemplateMapper;
import io.renren.modules.sys.dao.algs.AlgorithmTemplateVoMapper;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.algs.AlgorithmModel;
import io.renren.modules.sys.entity.algs.AlgorithmTemplate;
import io.renren.modules.sys.entity.algs.AlgorithmTemplate_Vo;
import io.renren.modules.sys.service.AlgsTemplsService;
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.*;

/**
 * @Author: Ivan Q
 * @Date: 2021/5/25 15:52
 */
@Service("algsTemplsService")
public class AlgsTemplsServiceImpl extends ServiceImpl<AlgorithmTemplateMapper, AlgorithmTemplate> implements AlgsTemplsService {
    @Autowired
    AlgorithmTemplateVoMapper algorithmTemplateVoMapper;

    @Autowired
    AlgorithmTemplateMapper algorithmTemplateMapper;

    @Autowired
    CategoryService categoryService;

    //此方法不再使用,因为不能连表查询
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //获取搜索框搜索的模板名称
        String tmpName = (String)params.get("tmpname");
        //获取当前登录的用户
        Long uid = (Long)params.get("currentUserId");
            IPage<AlgorithmTemplate> page = this.page(
                    new Query<AlgorithmTemplate>().getPage(params),
                    new QueryWrapper<AlgorithmTemplate>()
                            .like(StringUtils.isNotBlank(tmpName),"tmp_name", tmpName)
                            .eq(uid != null,"uid", uid)
            );

            return new PageUtils(page);

    }

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

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

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

            //用于查全部，获取总页数。
            templateAllInfo = algorithmTemplateVoMapper.getTemplateInfo(new QueryWrapper<AlgorithmTemplate_Vo>()
                    .like(StringUtils.isNotBlank(tmpName), "tmp_name", tmpName)
                    //根据登录的用户展示他自己的模板
                    .eq(uid != null, "uid", uid)
                    .eq(classificationtag != null, "category_id", categoryId)
            );

            for (AlgorithmTemplate_Vo vo : templateInfo) {
                vo.setCategoryName(categoryService.getById(vo.getCategoryId()).getCategoryName());
            }

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

    }

    /**
     * Description 查询所有的模板信息，用于新建算法时的下拉选框
     * @param params
     * @return
     */
    @Override
    public List<AlgorithmTemplate_Vo> queryPageAll(Map<String, Object> params){
        String tmpName = (String)params.get("tmpname");
        Long uid = (Long) params.get("currentUserId");
        List<AlgorithmTemplate_Vo> templateAllInfo = new ArrayList<>();
        templateAllInfo = algorithmTemplateVoMapper.getTemplateInfo(new QueryWrapper<AlgorithmTemplate_Vo>()
                .like(StringUtils.isNotBlank(tmpName), "tmp_name", tmpName)
                //根据登录的用户展示他自己的模板
                .eq(uid != null, "uid", uid)
        );
        return templateAllInfo;
    }

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

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

    @Override
    @Transactional
    public void update(AlgorithmTemplate tmp) {
        this.updateById(tmp);
    }

    @Override
    public AlgorithmTemplate selectByTmpName(String temName) {
        return algorithmTemplateMapper.selectByTmpName(temName);
    }


}
