package com.sptg.permissions.biz.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.device.api.dto.DeviceDto;
import com.sptg.device.api.entity.Device;
import com.sptg.permissions.api.dto.AppPackageDto;
import com.sptg.permissions.api.entity.AppPackage;
import com.sptg.permissions.biz.mapper.AppPackageMapper;
import com.sptg.permissions.biz.service.AppPackageServiceInterface;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * title: AppPackageServiceImpl
 * package: com.sptg.permissions.biz.service.impl
 * description:app安装包-服务层接口实现类
 *
 *
 *
 */
@Service
public class AppPackageServiceImpl extends ServiceImpl<AppPackageMapper, AppPackage> implements AppPackageServiceInterface {

    @Override
    public Page<AppPackage> pagingQuery(PageBean<AppPackage, AppPackageDto> pageBean) {
        AppPackageDto dto = pageBean.getDto();
        Wrapper<AppPackage> wrapper=new EntityWrapper<AppPackage>();
        if(dto!=null){
            //设备类型
            Integer deviceType = dto.getDeviceType();
            if(deviceType!=null){
                wrapper.eq("i_device_type",deviceType);
            }
            //机构位置
            if(StringUtils.isNotBlank(dto.getOrgPosition())){
                wrapper.like("s_org_position",dto.getOrgPosition(), SqlLike.RIGHT);
            }
            if (StringUtils.isNotEmpty(dto.getDeviceFactory())){
                wrapper.eq("s_device_factory",dto.getDeviceFactory());
            }
            if (StringUtils.isNotEmpty(dto.getDeviceModelNumber())){
                wrapper.eq("s_device_model_number",dto.getDeviceModelNumber());
            }
            //正常状态
            wrapper.notIn("i_status", OperationConstant.MINUS_ONE);
            //版本号倒序
            wrapper.orderBy("t_create_date",false);
//            wrapper.orderBy("i_is_enable",true);
        }
        Page<AppPackage> page = this.selectPage(pageBean,wrapper);
        return page;
    }

    @Override
    public List<AppPackage> getListByLikeVersion(AppPackageDto dto) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("i_status",0);
        if (StringUtils.isNotEmpty(dto.getDeviceFactory())){
            wrapper.eq("s_device_factory",dto.getDeviceFactory());
        }
        if (StringUtils.isNotEmpty(dto.getVersion())){
            wrapper.like("l_version",dto.getVersion(),SqlLike.RIGHT);
        }
        if (StringUtils.isNotEmpty(dto.getSmallVersion())){
            wrapper.ge("l_version",dto.getSmallVersion());
        }
        if (StringUtils.isNotEmpty(dto.getBigVersion())){
            wrapper.lt("l_version",dto.getBigVersion());
        }
        List<AppPackage> list = this.selectList(wrapper);
        return list;
    }

    @Override
    public List<AppPackage> getListByModelAndFactory(String model, String factory) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("i_status",0);
        wrapper.eq("s_device_model_number", model);
        wrapper.eq("s_device_factory", factory);
        List<AppPackage> list = this.selectList(wrapper);
        return list;
    }

    @Override
    public List<AppPackage> getListByVersion(String version) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("i_status",0);
        if (StringUtils.isNotEmpty(version)){
            wrapper.eq("l_version",version);
        }
        List<AppPackage> list = this.selectList(wrapper);
        return list;
    }

    @Override
    public List<AppPackage> getListByPackageName(String packageName) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("i_status",0);
        if (StringUtils.isNotEmpty(packageName)){
            wrapper.eq("s_package_name",packageName);
        }
        List<AppPackage> list = this.selectList(wrapper);
        return list;
    }

    @Override
    public AppPackage getRestoreWithModelAndFactory(String model, String factory) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("i_status",0);
        wrapper.eq("s_device_model_number", model);
        wrapper.eq("s_device_factory", factory);
        wrapper.eq("i_restore", OperationConstant.ONE);
        return this.selectOne(wrapper);
    }
}
