package io.renren.modules.generator.controller;

import com.jcraft.jsch.JSchException;
import io.renren.common.utils.FTPUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.generator.entity.ApplicationsEntity;
import io.renren.modules.generator.service.ApplicationsService;
import lgh.springboot.starter.hbase.template.HBaseTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 应用管理的表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-07-30 15:08:06
 */
@RestController
@RequestMapping("generator/applications")
public class ApplicationsController {
    @Autowired
    private ApplicationsService applicationsService;
    @Autowired
    HBaseTemplate hBaseTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("generator:applications:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = applicationsService.queryPageOnUse(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{applicationId}")
    public R info(@PathVariable("applicationId") Integer applicationId) {
        ApplicationsEntity applications = applicationsService.getById(applicationId);

        return R.ok().put("applications", applications);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("generator:applications:save")
    public R save(@RequestBody ApplicationsEntity applications) {
        applicationsService.save(applications);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("generator:applications:update")
    public R update(@RequestBody ApplicationsEntity applications) {
        applicationsService.updateById(applications);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("generator:applications:delete")
    public R delete(@RequestBody Integer[] applicationIds) {
        try {
            for(int id:applicationIds){
                String appName=applicationsService.getById(id).getApplicationName();
                hBaseTemplate.deleteTable(appName);
                hBaseTemplate.deleteTable(appName+"log");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        applicationsService.removeByIds(Arrays.asList(applicationIds));

        return R.ok();
    }

    @GetMapping("/appRunningLog/{appName}")
    public R appRunningLog(@PathVariable("appName")String appName) throws JSchException {
        String command = "docker ps -aq --filter name="+appName;
        String containerId= FTPUtils.execCmd(command).substring(0,12);
        String logCmd="docker logs "+containerId;
        String log=FTPUtils.execCmd(logCmd);
        return R.ok().put("log",log);
    }

}
