package com.sdtool.business.service;

import com.sdtool.common.api.ConfigService;
import com.sdtool.common.api.ERPService;
import com.sdtool.common.api.NoSqlKeysService;
import com.sdtool.common.base.BaseService;
import com.sdtool.common.datas.RedisKeys;
import com.sdtool.common.entity.base.DataBaseMultiItemEx;
import com.sdtool.common.entity.system.ConfigValue;
import com.dySweetFishPlugin.redis.RedisService;
import com.sweetfish.convert.json.JsonConvert;
import com.sweetfish.service.Local;
import com.sweetfish.util.AutoLoad;
import com.sweetfish.util.ResourceType;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by jlutt on 2020-07-29
 *
 * @author jlutt
 */
@AutoLoad(false)
@Local
@ResourceType(ERPService.class)
public class ERPServiceImpl extends BaseService implements ERPService {

    @Resource
    RedisService redisService;

    @Resource
    NoSqlKeysService keysService;

    @Resource
    ConfigService configService;

    @Resource
    JsonConvert jsonConvert;

    /**
     * 获取平台所有工厂客户列表
     *
     * @return 工厂客户列表
     */
    @Override
    public List<DataBaseMultiItemEx> getERPSupplierList() {
        Map<String, String> redisMap = redisService.hgetAll(keysService.getRedisKey(RedisKeys.KEY_SUPPLIER_PLATFORM));

        return redisMap.values().stream().map((x) -> (DataBaseMultiItemEx) jsonConvert.convertFromO(DataBaseMultiItemEx.class, x)).sorted((v1, v2) -> v1.getShardingKey().compareToIgnoreCase(v2.getShardingKey())).collect(Collectors.toList());
    }

    /**
     * 获取平台工厂客户信息
     *
     * @param id 工厂客户id
     * @return 工厂客户信息
     */
    @Override
    public DataBaseMultiItemEx getERPSupplierInfo(long id) {
        return getRedisDocInfo(keysService.getRedisKey(RedisKeys.KEY_SUPPLIER_PLATFORM), String.valueOf(id), DataBaseMultiItemEx.class);
    }

    /**
     * 获取平台工厂客户信息
     *
     * @param code 工厂客户ShardingKey
     * @return 工厂客户信息
     */
    @Override
    public DataBaseMultiItemEx getERPSupplierInfoBySharding(long code) {
        return getRedisDocInfo(keysService.getRedisKey(RedisKeys.KEY_SUPPLIER_SHARDING_KEY_PLATFORM), String.valueOf(code), DataBaseMultiItemEx.class);
    }

    public DataBaseMultiItemEx getERPSupplierInfoByCode(String code) {
        return getRedisDocInfo(keysService.getRedisKey(RedisKeys.KEY_SUPPLIER_ITEM_CODE_PLATFORM), code, DataBaseMultiItemEx.class);
    }

    @Override
    public int getSystemVersion(String dataSourceId, long supplierCode) {
        ConfigValue versionConfig = configService.getRedisConfigValue(RedisKeys.SUPPLIER_SYSTEM_CONFIGARR[0], dataSourceId, supplierCode);
        if (versionConfig == null) {
            return 0;
        }
        if (StringUtils.isBlank(versionConfig.getConfigValue1())) {
            return 0;
        }
        try {
            return Integer.parseInt(versionConfig.getConfigValue1());
        } catch (NumberFormatException e) {
            return 0;
        }
    }
}
