package com.aiotagro.cattletrade.business.service.impl;

import com.aiotagro.cattletrade.business.dto.SysRoleDto;
import com.aiotagro.cattletrade.business.entity.SysMenu;
import com.aiotagro.cattletrade.business.entity.SysRole;
import com.aiotagro.cattletrade.business.entity.SysRoleMenu;
import com.aiotagro.cattletrade.business.entity.SysUser;
import com.aiotagro.cattletrade.business.mapper.SysMenuMapper;
import com.aiotagro.cattletrade.business.mapper.SysRoleMapper;
import com.aiotagro.cattletrade.business.mapper.SysRoleMenuMapper;
import com.aiotagro.cattletrade.business.mapper.SysUserMapper;
import com.aiotagro.cattletrade.business.service.ISysRoleService;
import com.aiotagro.common.core.utils.StringUtils;
import com.aiotagro.common.core.web.domain.AjaxResult;
import com.aiotagro.common.core.web.domain.PageResultResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 岗位管理表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-12-26
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    @Autowired
    private SysMenuMapper menuMapper;

    @Override
    public PageResultResponse queryList(SysRoleDto dto) {
        Page<SysRole> result = PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getIsDelete, 0).orderByDesc(SysRole::getCreateTime);
        if (StringUtils.hasText(dto.getName())) {
            wrapper.like(SysRole::getName, dto.getName());
        }
        List<SysRole> list = list(wrapper);
        return new PageResultResponse(result.getTotal(), list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveRole(SysRole role) {
        if (role.getId() == null) {
            save(role);
        } else {
            role.setUpdateTime(new Date());
            updateById(role);
        }

        // 先删除岗位菜单
        roleMenuMapper.delete(Wrappers.<SysRoleMenu>query().lambda().eq(SysRoleMenu::getRoleId, role.getId()));
        // 再新增
        for (Integer menuId : role.getMenuIds()) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(role.getId());
            roleMenu.setMenuId(menuId);
            roleMenuMapper.insert(roleMenu);
        }

        return AjaxResult.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delete(Integer id) {
        SysRole role = getById(id);
        if (null == role || role.getIsDelete() == 1) {
            return AjaxResult.error("岗位不存在");
        }
        List<SysUser> users = userMapper.selectList(Wrappers.<SysUser>query().lambda().eq(SysUser::getIsDelete, 0).eq(SysUser::getRoleId, id));
        if (!CollectionUtils.isEmpty(users)) {
            return AjaxResult.error("此岗位关联员工，请移除员工后再删除");
        }
        // 删除岗位/角色
        removeById(id);

        // 删除岗位/角色菜单
        roleMenuMapper.delete(Wrappers.<SysRoleMenu>query().lambda().eq(SysRoleMenu::getRoleId, id));
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getMenus(Integer id) {
        SysRole role = null;
        if (id != null) {
            role = getById(id);
        }
        if (role == null) {
            role = new SysRole();
        }
        List<SysMenu> menus = menuMapper.queryListByRoleId(id);
        role.setMenuList(menus);
        return AjaxResult.success(role);
    }

}
