/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.system.controller;

import cn.hutool.core.util.IdUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.system.entity.PlatomixApp;
import org.springblade.modules.system.entity.PlatomixAppGroup;
import org.springblade.modules.system.entity.PlatomixAppGroupServer;
import org.springblade.modules.system.service.PlatomixAppGroupServerService;
import org.springblade.modules.system.service.PlatomixAppGroupService;
import org.springblade.modules.system.service.PlatomixAppService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 接口权限控制器
 *
 * @author BladeX
 */
@RestController
@AllArgsConstructor
@Api(value = "应用查询", tags = "应用查询")
@RequestMapping("/app")
public class AppController extends BladeController {

    @Resource
    private PlatomixAppGroupService platomixAppGroupService;
    @Resource
    private PlatomixAppGroupServerService platomixAppGroupServerService;
    @Resource
    private PlatomixAppService platomixAppService;

    @GetMapping("/list")
    @ApiOperation(value = "应用列表", notes = "应用列表")
    public R<List<PlatomixApp>> list() {
        return R.data(platomixAppService.list());
    }


    @PostMapping("/add")
    @ApiOperation(value = "增加应用", notes = "增加应用")
    public R<PlatomixApp> add(@RequestBody PlatomixApp platomixApp) {
        platomixApp.setAppId(IdUtil.getSnowflakeNextIdStr());
        platomixApp.setCreateTime(new Date());
        platomixApp.setCreateId(AuthUtil.getUserId() + "");
        platomixApp.setTenantId(AuthUtil.getTenantId());
        platomixAppService.save(platomixApp);
        return R.data(platomixApp);
    }
    @PostMapping("/updateById")
    @ApiOperation(value = "修改应用", notes = "修改应用")
    public R<PlatomixApp> c(@RequestBody PlatomixApp platomixApp) {
        platomixAppService.updateById(platomixApp);
        return R.data(platomixApp);
    }

    @GetMapping("/group/list/{appId}")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分类列表", notes = "分类列表")
    public R<List<PlatomixAppGroup>> groupList(@PathVariable("appId") String appId) {
        if (StringUtil.isBlank(appId)){
            return R.fail("应用id不能为空");
        }
        List<PlatomixAppGroup> list = platomixAppGroupService.lambdaQuery().eq(PlatomixAppGroup::getAppId, appId).orderByDesc(PlatomixAppGroup::getCreateTime).list();

        for (PlatomixAppGroup platomixAppGroup : list) {
            List<PlatomixAppGroupServer> list2 = platomixAppGroupServerService.lambdaQuery().eq(PlatomixAppGroupServer::getAppGroupId, platomixAppGroup.getAppGroupId()).orderByDesc(PlatomixAppGroupServer::getCreateTime).list();
            platomixAppGroup.setAppGroupServer(list2);
        }

        return R.data(list);
    }

    @PostMapping("/group/add")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "分类新增", notes = "分类新增")
    public R<?> groupAdd( @RequestBody PlatomixAppGroup platomixAppGroup) {
        platomixAppGroup.setAppGroupId(IdUtil.getSnowflakeNextIdStr());
        platomixAppGroup.setCreateTime(new Date());
        platomixAppGroup.setCreateId(AuthUtil.getUserId() + "");
        platomixAppGroup.setTenantId(AuthUtil.getTenantId());
        platomixAppGroupService.save(platomixAppGroup);
        return R.data(platomixAppGroup);
    }
    @PostMapping("/group/updateById")
    @ApiOperation(value = "修改应用分类", notes = "修改应用分类")
    public R<PlatomixAppGroup> c(@RequestBody PlatomixAppGroup platomixApp) {
        platomixAppGroupService.updateById(platomixApp);
        return R.data(platomixApp);
    }
    @GetMapping("/group/server/list/{groupId}")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = " 分类下服务列表", notes = "分类下服务列表")
    public R submit(@PathVariable("groupId") String groupId) {
        if (StringUtil.isBlank(groupId)){
            return R.fail("应用id不能为空");
        }
        List<PlatomixAppGroupServer> list = platomixAppGroupServerService.lambdaQuery().eq(PlatomixAppGroupServer::getAppGroupId, groupId).orderByDesc(PlatomixAppGroupServer::getCreateTime).list();
        return R.data(list);
    }



    @PostMapping("/group/server/add/{groupId}")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增关联", notes = "新增关联")
    public R remove( @RequestBody PlatomixAppGroupServer platomixAppGroup) {
        platomixAppGroup.setAppGroupServerId(IdUtil.getSnowflakeNextIdStr());
        platomixAppGroup.setCreateTime(new Date());
        platomixAppGroup.setCreateId(AuthUtil.getUserId() + "");
        platomixAppGroup.setTenantId(AuthUtil.getTenantId());
        platomixAppGroupServerService.save(platomixAppGroup);
        return R.data(platomixAppGroup);
    }

}
