package com.shanghaichengdi.ghjgitem.controller;

import com.shanghaichengdi.ghjgitem.constant.Constants;
import com.shanghaichengdi.ghjgitem.domain.SysDatashareUser;
import com.shanghaichengdi.ghjgitem.service.SysDatashareUserService;
import com.shanghaichengdi.ghjgitem.util.Encodes;
import com.shanghaigeography.Util.DateUtils;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@Slf4j
public class CommonController {

  @Resource
  SysDatashareUserService sysDatashareUserService;

  public Boolean checkDbKey(String key) {
    List<String> keyList = sysDatashareUserService.list(null).stream().map(SysDatashareUser::getKey)
        .collect(Collectors.toList());
    return keyList.contains(key);
  }

  public String getParamJsonData(String data) {
    return new String(Encodes.aesDecrypt(
        Encodes.decodeBase64(data),
        Constants.ENCODES_KEY.getBytes(),
        Constants.ENCODES_KEY.getBytes()), StandardCharsets.UTF_8);
  }

  public String resultEncode(String jsonString) {
    return Encodes.encodeBase64(
        Encodes.aesEncrypt(jsonString.getBytes(StandardCharsets.UTF_8),
            Constants.ENCODES_KEY.getBytes(),
            Constants.ENCODES_KEY.getBytes()));
  }
}
