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.DictDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Dict;
import com.sptg.permissions.api.feigns.DictFeign;
import com.sptg.statistics.api.constant.SysLogDictConstant;
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: DictManageController
 * package: com.sptg.gateway.controller.permissions
 * description: 菜单管理-控制器
 *
 *
 *
 */
@RestController
@RequestMapping(value = "/permissions/dict")
@AllArgsConstructor
public class DictManageController {

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

    private final DictFeign dictFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final SysOperationLogFeign sysOperationLogFeign;

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

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

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

    /**
     * 功能描述: 修改菜单
     * @param dictDto 菜单信息
     * @return ResultBean<Boolean> ：{ true：成功,false：失败 }
     *
     * date:  2018/11/4 18:55
     */
    @PostMapping(value = "/update")
    @Security(token = PermissionsSecurityToken.DICT_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> updateDict(DictDto dictDto){
        //  数据校验 非空，菜单名称，菜单类型校验
        if (null == dictDto || StringUtils.isEmpty(dictDto.getCode()) || null == dictDto.getDictValue()|| null == dictDto.getDictKey()){
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        Dict oldObj = this.dictFeign.selectById(dictDto.getId());
        //  获取当前登陆用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        ResultBean<Boolean> resultBean=this.dictFeign.updateDict(dictDto);
        if(null != resultBean && resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            Dict newObj = this.dictFeign.selectById(dictDto.getId());
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogDictConstant.DICT_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),dictDto,onLineUser,oldObj,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

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

    @GetMapping(value = "/getByCode/{code}")
    public ResultBean<Dict> getByCode(@PathVariable(value = "code")String code){
        return this.dictFeign.getByCode(code);
    }

}
