package com.boman.web.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.boman.common.core.web.controller.BaseController;
import com.boman.domain.Fxry;
import com.boman.domain.IpColumns;
import com.boman.domain.IpTimes;
import com.boman.domain.TableDataInfo;
import com.boman.domain.dto.AjaxResult;
import com.boman.web.core.domain.SysRegion;
import com.boman.web.core.mapper.SysRegionMapper;
import com.boman.web.core.service.fxry.FxryService;
import com.boman.web.core.service.ip.IpColumnsService;
import com.boman.web.core.service.ip.IpTimesService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.boman.web.core.utils.RSAUtil.*;

/**
 * @author shiqian
 * @description 给外部用的接口的限制点配置
 * @date 2022年01月05日 15:39
 **/
@RestController
@RequestMapping
public class IpTimesColumnsController extends BaseController {

    private static final String APP_KEY = "";
    private static final String APP_SECRET = "";

    @Resource
    private IpTimesService ipTimesService;


    /**
     * 功能描述: 外部访问接口 IpTimes
     *
     * @param ip    ip
     * @param times times
     * @return com.boman.domain.TableDataInfo
     */
    @GetMapping("/ipTimes/list")
    public AjaxResult list(String ip, String times) {
        List<JSONObject> list = ipTimesService.list(ip, times);
        return AjaxResult.success(list);
    }

    /**
     * 功能描述: 根据表明获取字段名称和comment
     *
     * @param tableName tableName
     * @return AjaxResult
     */
    @GetMapping("/ipTimes/{tableName}")
    public AjaxResult getAllColumns(@PathVariable("tableName") String tableName) {
        List<JSONObject> allColumns = ipTimesService.getAllColumns(tableName);
        return AjaxResult.success(allColumns);
    }

    @GetMapping("/ipTimes/get/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        JSONObject jsonObject = ipTimesService.getById(id);
        return AjaxResult.success(jsonObject);
    }

    @PostMapping("/ipTimes/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody JSONObject jsonObject) {
        return ipTimesService.saveOrUpdate(jsonObject);
    }

    @PostMapping("/ipTimes/delete")
    public AjaxResult delete(@RequestBody Long[] idArr) {
        int rows = ipTimesService.delete(idArr);
        return toAjax(rows);
    }


    /**
     * 初始化生成公钥私钥
     */
    @PostMapping("/ipTimes/getKey")
    public AjaxResult getKey() {
        String appId = getUUID32();
        Map<String, Object> init = init();
        String privateKey = getPrivateKey(init);
        String publicKey = getPublicKey(init);
        Map<String, Object> map = new HashMap<>();
        map.put("appId",appId);
        map.put("appKey",publicKey);
        map.put("appSecret",privateKey);
        return AjaxResult.success(map);
    }
}
