package com.codvision.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.codvision.auth.entity.UserOrgRole;
import com.codvision.auth.entity.vo.UserOrgRoleVO;
import com.codvision.auth.mapper.UserOrgRoleMapper;
import com.codvision.auth.service.IUserOrgRoleService;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.service.impl.BaseServiceImpl;
import com.codvision.webcore.util.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 组织用户关联信息 服务实现类
 * </p>
 *
 * @author lingee
 * @since 2023-11-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserOrgRoleServiceImpl extends BaseServiceImpl<UserOrgRoleMapper, UserOrgRole, UserOrgRoleVO>
        implements IUserOrgRoleService {

    @Override
    public UserOrgRoleVO getDetail(Long id) {
        UserOrgRoleVO vo = new UserOrgRoleVO();
        UserOrgRole po = this.getById(id);
        if (null != po) {
            BeanUtil.copyProperties(po, vo);
        }
        return vo;
    }

    @Override
    public IPage<UserOrgRoleVO> getPageResult(QueryPage<UserOrgRole> queryPage) {
        QueryWrapper<UserOrgRole> queryWrapper = queryPage.getWrapper();
        IPage<UserOrgRoleVO> pageResult = this.getBasePage(queryPage.getPage(), queryWrapper);
        return pageResult;
    }

    @Override
    public List<UserOrgRoleVO> listAll(UserOrgRole queryBean) {
        QueryWrapper<UserOrgRole> wrapper = PageUtil.initWrapper(queryBean);
        List<UserOrgRole> list = this.list(wrapper);
        return BeanUtil.copyToList(list, UserOrgRoleVO.class);
    }

    @Override
    public UserOrgRole getUserOrgRole(Integer uid, Integer oid, Integer roleId) {
        return this.getOne(Wrappers.<UserOrgRole>lambdaQuery().eq(UserOrgRole::getUid, uid)
                .eq(UserOrgRole::getOid, oid).eq(UserOrgRole::getRoleId, roleId));
    }

    @Override
    public boolean save(UserOrgRole orgUserMapper) {
        Assert.notNull(orgUserMapper, "组织用户关联信息不能为空");
        getBaseMapper().insert(orgUserMapper);
        return true;
    }

    @Override
    public boolean updateById(UserOrgRole orgUserMapper) {
        Assert.notNull(orgUserMapper, "组织用户关联信息不能为空");
        getBaseMapper().updateById(orgUserMapper);
        return true;
    }

    @Override
    public boolean removeById(Serializable id) {
        UserOrgRole orgUserMapper = getById(id);
        Assert.notNull(orgUserMapper, "组织用户关联信息不存在");
        getBaseMapper().deleteById(id);
        return true;
    }
}
