package com.sunyard.chsm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.constant.AuditLogConst;
import com.sunyard.chsm.dto.AppQuery;
import com.sunyard.chsm.dto.AppSave;
import com.sunyard.chsm.dto.AppView;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.service.ApplicationService;
import com.sunyard.ssp.common.annotation.AuditControllerLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 应用管理接口
 *
 * @author liulu
 * @since 2024/10/29
 */
@Slf4j
@RestController
@RequestMapping("/application")
public class ApplicationController {

    @Resource
    private ApplicationService applicationService;

    /**
     * 分页查询应用列表
     *
     * @param query 查询条件
     * @return 应用列表
     */
    @GetMapping("/pageList")
    public R<Page<AppView>> queryPageList(AppQuery query) {

        Page<AppView> page = applicationService.selectPageList(query);

        return R.data(page);
    }

    /**
     * 创建应用
     *
     * @param save 参数
     * @return id
     */
    @PostMapping
    @AuditControllerLog(description = "创建应用", operateType = AuditLogConst.ADD)
    public R<String> save(@Valid @RequestBody AppSave save) {
        Long id = applicationService.save(save);
        return R.data(String.valueOf(id));
    }

    /**
     * 修改应用
     *
     * @param update 参数
     * @return 应用
     */
    @PutMapping
    @AuditControllerLog(description = "修改应用", operateType = AuditLogConst.UPDATE)
    public R<Void> update(@Valid @RequestBody AppSave update) {
        Assert.notNull(update.getId(), "应用id不能为空");
        applicationService.update(update);
        return R.ok();
    }

    /**
     * 启用应用
     *
     * @param id 应用id
     * @return
     */
    @AuditControllerLog(description = "启用应用", operateType = AuditLogConst.UPDATE)
    @PostMapping("/enable")
    public R<Void> enable(Long id) {
        Assert.notNull(id, "应用id不能为空");
        applicationService.enable(id);
        return R.ok();
    }

    /**
     * 停用应用
     *
     * @param id 应用id
     * @return
     */
    @AuditControllerLog(description = "停用应用", operateType = AuditLogConst.UPDATE)
    @PostMapping("/disable")
    public R<Void> disable(Long id) {
        Assert.notNull(id, "应用id不能为空");
        applicationService.disable(id);
        return R.ok();
    }

    /**
     * 更新访问凭证
     *
     * @param id 应用id
     * @return
     */
    @PostMapping("/reset/credential")
    @AuditControllerLog(description = "更新访问凭证", operateType = AuditLogConst.UPDATE)
    public R<Void> resetCredential(Long id) {
        Assert.notNull(id, "应用id不能为空");
        applicationService.resetCredential(id);
        return R.ok();
    }


    /**
     * 删除应用
     *
     * @param id 应用id
     * @return
     */
    @DeleteMapping
    @AuditControllerLog(description = "删除应用", operateType = AuditLogConst.DELETE)
    public R<Void> delete(Long id) {
        Assert.notNull(id, "应用id不能为空");
        applicationService.delete(id);
        return R.ok();
    }

}
