package com.sptg.gateway.controller.permissions;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.SysLogConstant;
import com.sptg.common.core.util.WebUtils;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.permissions.api.constant.PermissionsSecurityToken;
import com.sptg.permissions.api.dto.MenuDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Menu;
import com.sptg.permissions.api.feigns.MenuFeign;
import com.sptg.statistics.api.constant.SysLogMenuConstant;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * title: MenuManageController
 * package: com.sptg.gateway.controller.permissions
 * description: 菜单管理-控制器
 *
 *                  
 *                
 */
@RestController
@RequestMapping(value = "/permissions/menu")
@AllArgsConstructor
public class MenuManageController {

    /* -------------------------------------------------- 接口注入 -------------------------------------------------- */

    private final MenuFeign menuFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final SysOperationLogFeign sysOperationLogFeign;

    /* --------------------------------------------------- 控制器 --------------------------------------------------- */

    /**
     * 功能描述: 获取树形菜单列表
     * 需要当前登陆用户拥有操作的菜单的权限，才可以访问
     * @return ResultBean<Menu>
     *                  
     * date:  2018/11/2 0:37
     */
    @GetMapping(value = "/getMenuTreeList")
    @Security(token = PermissionsSecurityToken.MENU_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Menu> getMenuTreeList(){
        return this.menuFeign.getMenuTreeWithRoot();
    }

    /**
     * 功能描述: 新增菜单
     * @param menuDto 菜单信息
     * @return ResultBean<Boolean> ：{ true：成功,false：失败 }
     *                  
     *                     
     */
    @PostMapping(value = "/insert")
    @Security(token = PermissionsSecurityToken.MENU_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> insertMenu(MenuDto menuDto){
        //  数据校验 非空，菜单名称，菜单类型校验
        if (null == menuDto || StringUtils.isEmpty(menuDto.getName()) || null == menuDto.getType()){
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        //  获取当前登陆用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        menuDto.setOnLineUser(onLineUser);
        ResultBean<Boolean> resultBean=this.menuFeign.insertMenu(menuDto);
        if(null != resultBean && resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.MENU_MANAGE, SysLogConstant.ADD_EVENT,
                    WebUtils.getIp(),menuDto,onLineUser,menuDto);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述: 修改菜单
     * @param menuDto 菜单信息
     * @return ResultBean<Boolean> ：{ true：成功,false：失败 }
     *                  
     * date:  2018/11/4 18:55
     */
    @PostMapping(value = "/update")
    @Security(token = PermissionsSecurityToken.MENU_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> updateMenu(MenuDto menuDto){
        //  数据校验
        if (null == menuDto || null == menuDto.getId() || StringUtils.isEmpty(menuDto.getName()) || null == menuDto.getType()){
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        Menu oldObj = this.menuFeign.selectById(menuDto.getId());
        //  获取当前登陆用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        menuDto.setOnLineUser(onLineUser);
        ResultBean<Boolean> resultBean=this.menuFeign.updateMenu(menuDto);
        if(null != resultBean && resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            Menu newObj = this.menuFeign.selectById(menuDto.getId());
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.MENU_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),menuDto,onLineUser,oldObj,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述: 根据Id删除菜单
     * @param id 菜单Id
     * @return ResultBean<Boolean> ：{ true：成功,false：失败 }
     *                  
     *                      
     */
    @GetMapping(value = "/delete/{id}")
    @Security(token = PermissionsSecurityToken.MENU_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteMenu(@PathVariable(value = "id")Long id){
        MenuDto menuDto = new MenuDto();
        menuDto.setId(id);
        Menu oldObj = this.menuFeign.selectById(menuDto.getId());
        //  获取当前登陆用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        menuDto.setOnLineUser(onLineUser);
        ResultBean<Boolean> resultBean=this.menuFeign.deleteMenu(menuDto);
        if(null != resultBean && resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.MENU_MANAGE, SysLogConstant.DELETE_EVENT,
                    WebUtils.getIp(),menuDto,onLineUser,oldObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

}
