package com.zhiqiyun.open.mvc.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiqiyun.open.annotation.Permission;
import com.zhiqiyun.open.core.enmus.YN;
import com.zhiqiyun.open.core.models.app.ApiRequestLog;
import com.zhiqiyun.open.core.models.app.AppKeyInfo;
import com.zhiqiyun.open.core.service.ApiRequestLogService;
import com.zhiqiyun.open.mvc.Result;
import com.zhiqiyun.open.mvc.manager.params.QueryApiRequestLogParams;
import com.zhiqiyun.open.mvc.manager.params.QueryAppKeyInfoParams;
import com.zhiqiyun.open.mvc.manager.params.SaveAppKeyInfoParam;
import com.zhiqiyun.open.core.service.AppKeyInfoService;
import com.zhiqiyun.open.core.service.SequenceService;
import com.zhiqiyun.open.utils.DateUtil;
import com.zhiqiyun.open.utils.RandomUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/appKey")
public class AppKeyInfoController {

    @Autowired
    private AppKeyInfoService appKeyInfoService;

    @Autowired
    private SequenceService sequenceService;

    @Autowired
    private ApiRequestLogService apiRequestLogService;

    @Permission(value = "app.key.find", tags = "查询AppKey")
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryAppKeyInfoParams params) {

        QueryWrapper<AppKeyInfo> queryWrapper = new QueryWrapper<>();
        if (params.getId() != null) {
            queryWrapper.eq("id", params.getId());
        }
        if (params.getIsEnable() != null) {
            queryWrapper.eq("is_enable", params.getIsEnable());
        }
        if (StringUtils.isNotBlank(params.getRemark())) {
            queryWrapper.like("remark", params.getRemark());
        }

        Page<AppKeyInfo> page = params.getPage();
        page.addOrder(OrderItem.desc("created_time"));

        Page<AppKeyInfo> resultData = this.appKeyInfoService.page(page, queryWrapper);

        return Result.instance(Result.Code.SUCCESS).setData(resultData);
    }

    @Permission(value = "app.key.find", tags = "Api调用日志查询")
    @PostMapping("/findRequestLogsPage")
    public Result findRequestLogsPage(@RequestBody QueryApiRequestLogParams params) {

        QueryWrapper<ApiRequestLog> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(params.getAppKey())) {
            queryWrapper.eq("app_key", params.getAppKey());
        }
        if (StringUtils.isNotBlank(params.getMethod())) {
            queryWrapper.eq("method", params.getMethod());
        }
        if (StringUtils.isNotBlank(params.getVersion())) {
            queryWrapper.eq("version", params.getVersion());
        }
        if (StringUtils.isNotBlank(params.getClientIp())) {
            queryWrapper.eq("client_ip", params.getClientIp());
        }
        if (params.getServiceBeginTime() != null) {
            queryWrapper.between("service_begin_time", params.getServiceBeginTime().getStatDate(), params.getServiceBeginTime().getEndDate());
        }


        Page<ApiRequestLog> page = params.getPage();
        page.addOrder(OrderItem.desc("service_begin_time"));

        Page<ApiRequestLog> resultData = this.apiRequestLogService.page(page, queryWrapper);

        return Result.instance(Result.Code.SUCCESS).setData(resultData);
    }

    @Permission(value = "app.key.add", tags = "新建AppKey")
    @PostMapping("/save")
    public Result save(@RequestBody SaveAppKeyInfoParam param) {

        AppKeyInfo appKeyInfo = new AppKeyInfo();
        BeanUtils.copyProperties(param, appKeyInfo);

        Long id = this.sequenceService.nextId();
        String encryptKey = RandomUtil.get(64);
        appKeyInfo.setId(id);
        appKeyInfo.setEncryptKey(encryptKey);
        appKeyInfo.setCreatedTime(DateUtil.current());
        appKeyInfo.setIsEnable(YN.Y);
        this.appKeyInfoService.save(appKeyInfo);

        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

    @Permission(value = "app.key.add", tags = "更新AppKey")
    @PostMapping("/updateById")
    public Result updateById(Long id, @RequestBody SaveAppKeyInfoParam param) {
        AppKeyInfo appKeyInfo = new AppKeyInfo();
        BeanUtils.copyProperties(param, appKeyInfo);
        appKeyInfo.setId(id);
        this.appKeyInfoService.refreshCache(id.toString());
        this.appKeyInfoService.updateById(appKeyInfo);

        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

    @Permission(value = "app.key.disable", tags = "禁用AppKey")
    @PostMapping("/disableByIds")
    public Result disableByIds(@RequestBody List<Long> ids) {
        UpdateWrapper<AppKeyInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids);
        updateWrapper.set("is_enable", YN.N);
        this.appKeyInfoService.update(updateWrapper);
        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

    @Permission(value = "app.key.enable", tags = "启用AppKey")
    @PostMapping("/enableByIds")
    public Result enableByIds(@RequestBody List<Long> ids) {
        UpdateWrapper<AppKeyInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids);
        updateWrapper.set("is_enable", YN.Y);
        this.appKeyInfoService.update(updateWrapper);
        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }
}
