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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.renren.common.utils.PageUtils;
import io.renren.modules.job.dao.ScheduleJobDao;
import io.renren.modules.job.service.ScheduleJobService;
import io.renren.modules.sys.dao.dataset.DataSetDyMapper;
import io.renren.modules.sys.dao.dataset.DataSetDyVoMapper;
import io.renren.modules.sys.entity.algs.CategoryEntity;
import io.renren.modules.sys.entity.dataset.DataSetDy;
import io.renren.modules.sys.entity.dataset.DataSetDy_Vo;
import io.renren.modules.sys.entity.dataset.DataSet_Vo;
import io.renren.modules.sys.service.CategoryService;
import io.renren.modules.sys.service.DataSetDyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author allen
 * @since 2021-08-04
 */
@Service
public class DataSetDyServiceImpl extends ServiceImpl<DataSetDyMapper, DataSetDy> implements DataSetDyService {

    @Autowired
    DataSetDyVoMapper dataSetDyVoMapper;
    @Autowired
    CategoryService categoryService;
    @Autowired
    ScheduleJobService scheduleJobService;
    @Override
    public PageUtils queryPageOnUse(Map<String, Object> params) {
        //获取搜索框搜索的模板名称
        String datasetName = (String)params.get("datasetName");
        String classificationtag = (String)params.get("classificationtag");
        byte status = 0;
        if(params.get("status") !=null){
            String t = (String) params.get("status");
            if(t.equals("1"))
                status = 1;
            else status = 2;
        }
        Long categoryId = categoryService.getByName(classificationtag);
        //获取当前登录的用户,如果是管理员的话，uid就为null
        Long uid = (Long) params.get("currentUserId");
        List<DataSetDy_Vo> datasetInfo = new ArrayList<>();
        List<DataSetDy_Vo> datasetAllInfo = new ArrayList<>();
        int pageSize = Integer.valueOf((String) params.get("limit"));
        int pageIndex = Integer.valueOf((String) params.get("page"));

//        此方法如果修改,则也需要修改下面的获取总页数的方法
        datasetInfo = dataSetDyVoMapper.getModelInfo(new QueryWrapper<DataSetDy_Vo>()
                        .like(StringUtils.isNotBlank(datasetName), "dataset_name", datasetName)
                        //根据登录的用户展示他自己的模板
                        .eq(uid != null, "uid", uid)
                        .eq(classificationtag != null, "category_id", categoryId)
                // 用于查询数据状态 区分动态和静态数据
//                        .eq(params.get("status") != null, "dataset_status", 2)
//                使用limit用于分页,下面的函数没有limit进行分页
                        .last("limit " + (pageIndex - 1) * pageSize + "," + pageSize + ";")
        );

        //用于查全部，获取总页数。
        datasetAllInfo = dataSetDyVoMapper.getModelInfo(new QueryWrapper<DataSetDy_Vo>()
                .like(StringUtils.isNotBlank(datasetName), "dataset_name", datasetName)
                //根据登录的用户展示他自己的模板
                .eq(uid != null, "uid", uid)
                .eq(classificationtag != null, "category_id", categoryId)
//                .eq(params.get("status") != null, "dataset_status", status)
        );

        for (DataSetDy_Vo vo : datasetInfo) {
            final CategoryEntity categoryServiceById = categoryService.getById(vo.getCategoryId());
            if(categoryServiceById != null){
                vo.setCategoryName(categoryServiceById.getCategoryName());
            }else {
                vo.setCategoryName("默认分类");
            }

            vo.setCronExpression(scheduleJobService.getById(vo.getJobId()).getCronExpression());
            vo.setStatus(scheduleJobService.getById(vo.getJobId()).getStatus());
        }

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

    }
}
