package cn.pluss.platform.systemConfig.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.entity.AppVersionInfo;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.AppVersionInfoMapper;
import cn.pluss.platform.util.ParametersUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.pluss.platform.entity.SystemConfig;
import cn.pluss.platform.mapper.SystemConfigMapper;
import cn.pluss.platform.systemConfig.SystemConfigService;

@Transactional
@RequiredArgsConstructor
@Service("systemConfigService")
public class SystemConfigServiceImp extends ServiceImpl<SystemConfigMapper, SystemConfig> implements SystemConfigService {

    private final SystemConfigMapper systemConfigMapper;

    private final AppVersionInfoMapper appVersionInfoMapper;

    @Override
    public SystemConfig querySystemConfig(SystemConfig systemConfig) {
        return systemConfigMapper.querySystemConfig(systemConfig);
    }

    @Override
    public List<SystemConfig> querySystemConfigList(SystemConfig systemConfig) {
        return systemConfigMapper.querySystemConfigList(systemConfig);
    }

    @Override
    public void saveSystemConfig(SystemConfig systemConfig) {

        systemConfigMapper.saveSystemConfig(systemConfig);
    }

    @Override
    public void updateSystemConfig(SystemConfig systemConfig) {

        systemConfigMapper.updateSystemConfig(systemConfig);
    }

    @Override
    public void deleteSystemConfig(SystemConfig systemConfig) {

        systemConfigMapper.deleteSystemConfig(systemConfig);
    }

    @Override
    public List<SystemConfig> querySystemConfigPage(Map map) {

        return systemConfigMapper.querySystemConfigPage(map);
    }

    @Override
    public Integer querySystemConfigPageCount(Map map) {

        return systemConfigMapper.querySystemConfigPageCount(map);
    }

    @Override
    public void saveSystemConfigBatch(List<SystemConfig> systemConfigList) {

        systemConfigMapper.saveSystemConfigBatch(systemConfigList);
    }

    @Override
    public String querySystemConfigValue(String key) {
        SystemConfig systemConfig = new SystemConfig();
        systemConfig.setPropertyKey(key);
        systemConfig = systemConfigMapper.querySystemConfig(systemConfig);
        if (systemConfig == null) {
            return null;
        }

        return systemConfig.getPropertyValue();
    }

    @Override
    public String getdoMain() {
        String domain = "";
        Properties prop = System.getProperties();
        String os = prop.getProperty("os.name");
        if (os.startsWith("win") || os.startsWith("Win")) {
            //windows目录
            domain = "http://192.168.0.187:8084";
        } else {
            domain = querySystemConfigValue("domain");
        }
        return domain;
    }

    @Override
    public String getaisleSwitch() {
        SystemConfig systemConfig = new SystemConfig();
        systemConfig.setPropertyKey("aisleSwitch");
        systemConfig = systemConfigMapper.querySystemConfig(systemConfig);
        return systemConfig.getPropertyValue();
    }

    @Override
    public Object updateSystemVersion(int platformCode) {
        if (platformCode == -1) {
            // 老版本安卓更新
            QueryWrapper<AppVersionInfo> queryWrapper = new QueryWrapper<AppVersionInfo>()
                    .eq("platformCode", 0)
                    .orderByDesc("versionCode")
                    .last("limit 1");

            SystemConfig systemConfig2 = new SystemConfig();
            systemConfig2.setPropertyKey("versions");
            systemConfig2 = querySystemConfig(systemConfig2);

            Map<String, Object> result = new HashMap<>(16);
            List<AppVersionInfo> appVersionInfoList = appVersionInfoMapper.selectList(queryWrapper);
            if (appVersionInfoList != null && appVersionInfoList.size() > 0) {
                AppVersionInfo appVersionInfo = appVersionInfoList.get(0);
                result.put("apk_dowload_url", appVersionInfo.getUrl());
            } else {
                result.put("apk_dowload_url", ParametersUtil.apk_dowload_url);
            }

            result.put("versions", systemConfig2.getPropertyValue());
            result.put("propertyIndex", systemConfig2.getPropertyIndex());
            return result;
        }

        QueryWrapper<AppVersionInfo> queryWrapper = new QueryWrapper<AppVersionInfo>()
                .eq("platformCode", platformCode)
                .orderByDesc("versionCode")
                .last("limit 1");

        List<AppVersionInfo> appVersionInfoList = appVersionInfoMapper.selectList(queryWrapper);
        if (appVersionInfoList == null || appVersionInfoList.size() == 0) {
            throw new MsgException("无法获取版本信息");
        }

        return appVersionInfoList.get(0);
    }

}
