package com.xhpc.general.service;

import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * program: ruoyi
 * User: HongYun
 * Date:2021-08-12 14
 */
@Service
public class XhpcServiceDataUpdateServiceImpl implements IXhpcServiceDataUpdateService{

    @Autowired
    private RedisService redisService;
    @Autowired
    private TokenService tokenService;

    @Override
    public Map<String, Object> list() {
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        Map<String,Object> list=new HashMap<>();
        String cacheSOC = redisService.getCacheObject("global:"+tenantId+":SOC");
        String cacheVersion = redisService.getCacheObject("global:"+tenantId+":version");
        String cachePhone = redisService.getCacheObject("global:"+tenantId+":phone");
        //String cacheAliyunMessageCount = redisService.getCacheObject("global:AliyunMessageCount");
        list.put("SOC",cacheSOC);
        list.put("version",cacheVersion);
        list.put("phone", cachePhone);
        //list.put("aliyunMessageCount", cacheAliyunMessageCount);
        return list;
    }


    @Override
    public AjaxResult update(String SOC, String version, String phone, String aliyunMessageCount,String tenantId) {

        if (SOC != null) {
            int soc = Integer.parseInt(SOC);
            if (!(soc >= 1 && soc <= 100)) {
                return AjaxResult.error("范围应为[1-100]");
            }
        }
        if (phone != null) {
            if (phone.length() > 20) {
                return AjaxResult.error("号码格式错误");
            }
        }
//        if (aliyunMessageCount == null && "".equals(aliyunMessageCount)) {
//            return AjaxResult.error("短信条数为空");
//        }

        redisService.setCacheObject("global:"+tenantId+":SOC", SOC);
        redisService.setCacheObject("global:"+tenantId+":version", version);
        redisService.setCacheObject("global:"+tenantId+":phone", phone);
       // redisService.setCacheObject("global:AliyunMessageCount", aliyunMessageCount);


        return AjaxResult.success();
    }

    @Override
    public AjaxResult pilePromptUpdate(String prompt) {
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        if("".equals(prompt) || prompt ==null){
            try {
                Integer pilePrompts = redisService.getCacheObject("pilePrompt:"+tenantId);
                if(pilePrompts==null){
                    return AjaxResult.success(1);
                }
                return AjaxResult.success(pilePrompts);
            }catch (Exception e){
                return AjaxResult.success(1);
            }
        }else{

            redisService.setCacheObject("pilePrompt:"+tenantId, Integer.parseInt(prompt));

            return AjaxResult.success();
        }
    }

    @Override
    public AjaxResult placeholderUpdate(String prompt) {
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        if("".equals(prompt) || prompt ==null){
            try {
                Integer pilePrompts = redisService.getCacheObject("placeholder:"+tenantId);
                if(pilePrompts==null){
                    return AjaxResult.success(1);
                }
                return AjaxResult.success(pilePrompts);
            }catch (Exception e){
                return AjaxResult.success(1);
            }
        }else{

            redisService.setCacheObject("pilePrompt:"+tenantId, Integer.parseInt(prompt));

            return AjaxResult.success();
        }
    }
}
