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

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.renren.modules.generator.dao.ApplicationVoDao;
import io.renren.modules.generator.entity.ApplicationsEntity_Vo;
import io.renren.modules.generator.service.EdgenodesService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
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.generator.dao.ApplicationsDao;
import io.renren.modules.generator.entity.ApplicationsEntity;
import io.renren.modules.generator.service.ApplicationsService;


@Service("applicationsService")
public class ApplicationsServiceImpl extends ServiceImpl<ApplicationsDao, ApplicationsEntity> implements ApplicationsService {
    @Autowired
    ApplicationVoDao applicationVoDao;
    @Autowired
    ApplicationsDao applicationsDao;
    @Autowired
    EdgenodesService edgenodesService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String applicationName=(String) params.get("applicationName");
        String applicationType= (String) params.get("applicationType");
        IPage<ApplicationsEntity> page = this.page(
                new Query<ApplicationsEntity>().getPage(params),
                new QueryWrapper<ApplicationsEntity>()
                .eq(StringUtils.isNotBlank(applicationName),"application_name",applicationName)
                .eq(StringUtils.isNotBlank(applicationType),"application_type",applicationType)
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageOnUse(Map<String, Object> params) {
        String applicationName=(String) params.get("applicationName");
        String deployDeviceIdString=(String) params.get("deployDeviceId");
        int deployDeviceId=0;
        if (deployDeviceIdString!=null&&deployDeviceIdString.length()>0){
            deployDeviceId=Integer.valueOf(deployDeviceIdString);
        }
        int pageSize = Integer.valueOf((String) params.get("limit"));
        int pageIndex = Integer.valueOf((String) params.get("page"));

        List<ApplicationsEntity_Vo> appsInfo;
        List<ApplicationsEntity_Vo> appsAllInfo;
        appsInfo=applicationVoDao.getAppInfo(new QueryWrapper<ApplicationsEntity_Vo>()
                .like(StringUtils.isNotBlank(applicationName),"application_name",applicationName)
                .eq(deployDeviceIdString!=null&&deployDeviceIdString.length()>0,"deploy_device_id",deployDeviceId)
                .last("limit " + (pageIndex - 1) * pageSize + "," + pageSize + ";")
            );
        appsAllInfo=applicationVoDao.getAppInfo(new QueryWrapper<ApplicationsEntity_Vo>()
                .like(StringUtils.isNotBlank(applicationName),"application_name",applicationName)
                .eq(deployDeviceIdString!=null&&deployDeviceIdString.length()>0,"deploy_device_id",deployDeviceId));
        for (ApplicationsEntity_Vo vo:appsInfo){
            if (vo.getDeployDeviceId()==0){
                vo.setDeployDeviceName("中心云");
            }else {
                vo.setDeployDeviceName(edgenodesService.getById(vo.getDeployDeviceId()).getEdgenodesName());
            }

        }
        return new PageUtils(appsInfo,appsAllInfo.size(),pageSize,pageIndex);
    }

    @Override
    public int selectByDeployDeviceId(Integer deployDeviceId) {
        return applicationsDao.selectByDeployDeviceId(deployDeviceId);
    }


}