package com.yonge.cooleshow.admin.controller;

import com.yonge.cooleshow.biz.dal.enums.YesOrNoEnum;
import com.yonge.cooleshow.biz.dal.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.SysManualVo;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.exception.BizException;
import com.yonge.toolset.mybatis.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.SysManual;
import com.yonge.cooleshow.biz.dal.queryInfo.SysManualQueryInfo;
import com.yonge.cooleshow.biz.dal.service.SysManualService;
import com.yonge.cooleshow.common.controller.BaseController;

/**
 * (SysManual)表控制层
 *
 * @author makejava
 * @since 2021-12-20 14:23:36
 */
@RestController
@Api(tags = "帮助手册")
@RequestMapping("/sysManual")
public class SysManualController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private SysManualService sysManualService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "查询操作手册")
    @RequestMapping("/listMenuIds")
    public Object listMenuIds() {
        return succeed(sysManualService.listMenuIds());
    }

    @ApiOperation(value = "查询操作手册")
    @RequestMapping("/list")
    public HttpResponseResult<PageInfo<SysManualVo>> list( @RequestBody SysManualQueryInfo queryInfo) {
        return succeed(PageUtil.pageInfo(sysManualService.query(PageUtil.getPage(queryInfo),queryInfo)));
    }

    @ApiOperation(value = "新增帮助手册")
    @PostMapping("/add")
    public HttpResponseResult<Boolean> add(@RequestBody SysManual sysManual) throws Exception {
        this.check(sysManual);
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        sysManual.setStatus(YesOrNoEnum.NO);
        return succeed(sysManualService.add(sysManual, sysUser.getId()));
    }

    @ApiOperation(value = "修改帮助手册")
    @PostMapping("/update")
    public  HttpResponseResult<Boolean>  update(@RequestBody SysManual sysManual) throws Exception {
        this.check(sysManual);
        if (sysManual.getId() == null) {
            throw new Exception("更新必须有id");
        }
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        return succeed(sysManualService.update(sysManual, sysUser.getId()) >0);
    }

    private void check(SysManual sysManual) throws Exception {
        if (sysManual.getMenuId() == null) {
            throw new Exception("菜单Id是必须");
        }
        if (sysManual.getName() == null) {
            throw new Exception("菜单名是必填");
        }
        if (sysManual.getName().length() > 256) {
            throw new Exception("菜单名不能超过256");
        }
        if (sysManual.getOpFlow() == null) {
            throw new Exception("操作流程必填");
        }
        if (sysManual.getOpFlow().length() > 8000) {
            throw new Exception("操作流程不能超过8000字节");
        }
        if (sysManual.getFunRule() == null) {
            throw new Exception("功能规则必填");
        }
        if (sysManual.getFunRule().length() > 8000) {
            throw new Exception("功能规则不能超过8000字节");
        }
    }

    @ApiOperation(value = "删除帮助手册")
    @PostMapping("/remove")
    public Object remove(@RequestBody SysManual sysManual) throws Exception {
        sysManualService.removeById(sysManual.getId());
        return succeed();
    }

}