package org.jeecg.modules.adweb.site.controller;


import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.config.security.utils.SecureUtil;
import org.jeecg.modules.adweb.common.util.AdwebRedisUtil;
import org.jeecg.modules.adweb.site.service.SelfWebSiteService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @autor jerry
 * @date: 2022/1/11 20:45
 */
@Slf4j
@RestController
@RequestMapping("/wpWebSite")
public class WPSiteController {

    @Resource
    private SelfWebSiteService selfWebSiteService;

    @Resource
    private AdwebRedisUtil adwebRedisUtil;

    /**
     * 创建站点
     *
     * @param templateId 模板id
     * @param planId     域名
     * @param name       站点名称
     * @param uid        站点所属用户名称
     * @param request    请求信息
     * @return 创建的站点信息
     */
    @RequestMapping("addWebsite")
    public Result<?> createWebSiteByTempId(HttpServletRequest request, Long templateId, String planId, String name, String uid, Integer giveDay, Integer compensateDay) {
        log.info("新建站点模板：" + templateId + "; 站点名称：" + name + "; 套餐id：" + planId);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 如果2分钟内有创建过站点，则不允许再次创建站点
        String flag = adwebRedisUtil.getString(templateId + "_" + uid);
        if (StringUtils.isNotBlank(flag)) {
            log.info("2分钟内重复拉站点");
            try {
                Thread.sleep(5 * 60 * 1000);
            } catch (InterruptedException e) {
                log.info("重复拉站点的睡眠出现异常");
            }
            return Result.OK("ignore");
        }
        adwebRedisUtil.set(templateId + "_" + uid, "flag", 2 * 60);

        LoginUser user = SecureUtil.currentUser();
        // 建站服务
        selfWebSiteService.createSite(user, templateId, uid, name, planId, sysUser, giveDay, compensateDay);

        return Result.OK("申请创建站点成功，请等待通知，或者去站点管理查看结果！");
    }

    /**
     * 发布站点
     *
     * @param domain 域名
     * @param siteId 站点ID
     * @return
     */
    @RequestMapping("releaseWebsite")
    public Result<?> releaseWebsite(HttpServletRequest request, String domain, Integer siteId) {
        return selfWebSiteService.releaseSite(request, domain, siteId);
    }

    /**
     * 重新发布站点
     *
     * @param domain 域名
     * @param siteId 站点ID
     * @return
     */
    @RequestMapping("delReleaseWebsite")
    public Result<?> delReleaseWebsite(String domain, Integer siteId) {
        return selfWebSiteService.delReleaseWebsite(domain, siteId);
    }


    /**
     * 站点 启动/停止
     *
     * @param siteId 站点Id
     * @param status 站点启动（local）：1；停止：2; 站点启动（prod）：3；停止：4;
     * @return
     */
    @RequestMapping("siteStartOrStop")
    public Result<?> siteStartOrStop(Integer siteId, Integer status) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        boolean flag = selfWebSiteService.siteStartOrStop(sysUser, siteId, status);
        String result = "";
        if (flag) {
            if (status.equals(1) || status.equals(3)) {
                result = "站点启动成功！";
            }
            if (status.equals(2) || status.equals(4)) {
                result = "站点停止成功！";
            }
        } else {
            if (status.equals(1) || status.equals(3)) {
                result = "站点启动失败！";
            }
            if (status.equals(2) || status.equals(4)) {
                result = "站点停止失败！";
            }
        }
        return Result.OK(result);
    }

}
