package com.imdroid.beidou.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imdroid.beidou.common.HttpResult;
import com.imdroid.common.util.AppUtils;
import com.imdroid.secapi.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ApiKeyController extends BasicController {
    @Autowired
    ApiKeyMapper apiKeyMapper;
    @Autowired
    TenantMapper tenantMapper;
    @Autowired
    OpLogManager opLogManager;

    /**** 推送页面  *****/
    @RequestMapping("/sys/apikey")
    public String listApiKey(Model m, HttpSession session) {
        initModel(m, session);
        return "/page/api_key";
    }

    @RequestMapping("/sys/apikey/add")
    public String addApiKey(Model m, HttpSession session){
        initModel(m, session);
        QueryWrapper<Tenant> queryWrapper = new QueryWrapper<>();
        Integer tenantId = getTenantId(session);
        if (tenantId != null && tenantId != Tenant.SAAS_PROVIDER_ID) {
            queryWrapper.eq("id", tenantId);
        }
        List<Tenant> tenants = tenantMapper.selectList(queryWrapper);
        m.addAttribute("tenant_list", tenants);

        String apiKey = AppUtils.getAppId();
        String apiSecret = AppUtils.getAppSecret(apiKey);
        m.addAttribute("api_key", apiKey);
        m.addAttribute("api_secret", apiSecret);

        return "/page/table/api_key_add";
    }

    /**** 推送数据  *****/
    @RequestMapping("/sys/apikey/list")
    @ResponseBody
    public JSONObject list(HttpSession session, int page, int limit) {
        Page<ApiKey> pageable = new Page<>(page, limit);
        QueryWrapper<ApiKey> queryWrapper = new QueryWrapper<>();
        Integer tenantId = getTenantId(session);
        if (tenantId != null && tenantId != Tenant.SAAS_PROVIDER_ID) {
            queryWrapper.eq("tenantid", tenantId);
        }
        IPage<ApiKey> cs = apiKeyMapper.selectPage(pageable, queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", cs.getTotal());
        jsonObject.put("data", cs.getRecords());
        return jsonObject;
    }

    @PostMapping("/sys/apikey/update")
    @ResponseBody
    public String update(HttpSession session, @RequestBody JSONObject object) throws Exception {
        // 从请求参数中创建对象
        ApiKey apiKey = JSONObject.toJavaObject(object,ApiKey.class);
        Tenant tenant = tenantMapper.selectById(apiKey.getTenantid());
        int num = 0;
        if(tenant!=null) {
            apiKey.setTenantname(tenant.getName());
            num = apiKeyMapper.insert(apiKey);
        }
        if (num == 0) {
            return HttpResult.failed();
        } else {
            return HttpResult.ok();
        }
    }

    @PostMapping("/sys/apikey/delete")
    @ResponseBody
    public String delete(HttpSession session, @RequestParam Integer id) throws Exception {
        int num = apiKeyMapper.deleteById(id);
        opLogManager.addLog(getLoginUser(session),getTenantId(session),
                OpLogManager.OP_TYPE_DEL,
                OpLogManager.OP_OBJ_SYS,
                 "api key deleted");
        if (num == 0) {
            return HttpResult.failed();
        } else return HttpResult.ok();
    }

}
