package com.tuoheng.service.op.Impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuoheng.enums.MarkTypeEnum;
import com.tuoheng.mapper.op.PermissionsMapper;
import com.tuoheng.mapper.op.RoleMenuMapper;
import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.model.entity.Menu;
import com.tuoheng.model.entity.Permissions;
import com.tuoheng.model.entity.RoleMenu;
import com.tuoheng.model.query.MenuQuery;
import com.tuoheng.model.vo.MenuindexVo;
import com.tuoheng.model.vo.MenuVo;
import com.tuoheng.service.op.MenusService;
import com.tuoheng.mapper.op.MenusMapper;
import com.tuoheng.until.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 小影
 * @description 针对表【op_menus(菜单表)】的数据库操作Service实现
 * @createDate 2023-03-20 14:50:28
 */
@Service
public class MenusServiceImpl extends ServiceImpl<MenusMapper, Menu>
        implements MenusService {

    @Autowired
    private PermissionsMapper permissionsMapper;
    @Autowired
    private MenusMapper menusMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;


    /**
     * 获取菜单列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    public JsonResult getList(MenuQuery query) {
        MenuindexVo vo = new MenuindexVo();
        //获取权限/按钮列表
        List<Permissions> permissions = permissionsMapper.selectList(Wrappers.<Permissions>lambdaQuery()
                .like(ObjectUtil.isNotEmpty(query.getName()), Permissions::getName, query.getName())
                .eq(Permissions::getMark, MarkTypeEnum.VALID.getCode()));

        vo.setPermissionList(permissions);
        //获取菜单列表
        List<Menu> menus = menusMapper.selectList(Wrappers.<Menu>lambdaQuery()
                .like(ObjectUtil.isNotEmpty(query.getName()), Menu::getName, query.getName())
                .eq(Menu::getMark, MarkTypeEnum.VALID.getCode())
                .orderByDesc(Menu::getSort));
        //封装
        List<MenuVo> collect = menus.stream().map(item -> {
            MenuVo menuListVo = new MenuVo();
            BeanUtils.copyProperties(item, menuListVo);
            return menuListVo;
        }).collect(Collectors.toList());

        vo.setMenuListVoList(collect);
        return JsonResult.success(vo);
    }

    /**
     * 添加或编辑菜单
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult edit(Menu entity, LoginUser loginUser) {
        if (entity == null) {
            return JsonResult.error("实体对象不存在");
        }
        boolean result = false;
        if (entity.getId() != null && entity.getId() > 0) {
            // 修改记录
            entity.setUpdateUser(loginUser.getUserId().intValue());
            entity.setUpdateTime(DateUtil.date());
            result = this.updateById(entity);
            if (!result) {
                return JsonResult.error();
            }
        } else {
            // 新增记录
            entity.setCreateUser(loginUser.getUserId().intValue());
            entity.setCreateTime(DateUtil.date());
            entity.setMark(1);
            result = this.save(entity);
            if (!result) {
                return JsonResult.error();
            }
            //新增菜单直接讲菜单跟当前用户的租户进行关联
            RoleMenu roleMenu = new RoleMenu();
            //查询出所有有效租户
            roleMenu.setRoleId(loginUser.getRoleId());
            roleMenu.setMenuId(entity.getId());
            roleMenuMapper.insert(roleMenu);
        }
        return JsonResult.success();
    }

}




