package com.cico.nest.lcdp.boot.core.system.dal.mysql.user;

import com.cico.nest.lcdp.boot.core.system.controller.admin.permission.vo.role.RoleRespVO;
import com.cico.nest.lcdp.boot.core.system.controller.admin.user.vo.user.UserPageReqVO;
import com.cico.nest.lcdp.boot.core.system.controller.admin.user.vo.user.UserRespVO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.dept.DeptDO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.permission.RoleDO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.permission.UserRoleDO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.user.AdminUserDO;
import com.cico.nest.lcdp.boot.core.system.service.user.dto.AdminUserRoleDeptDTO;
import com.cico.nest.lcdp.framework.common.pojo.PageParam;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import com.cico.nest.lcdp.framework.mybatis.core.mapper.BaseMapperX;
import com.cico.nest.lcdp.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

@Mapper
public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {

    default AdminUserDO selectByUsername(String username) {
        return selectOne(AdminUserDO::getUsername, username);
    }

    default AdminUserDO selectByUsername(String username,Long tenantId) {
        return selectOne(AdminUserDO::getUsername, username,AdminUserDO::getTenantId,tenantId);
    }

    default AdminUserDO selectOne(Long id,Long tenantId){
        return selectOne(AdminUserDO::getId, id,AdminUserDO::getTenantId,tenantId);
    }

    default AdminUserDO selectByEmail(String email) {
        return selectOne(AdminUserDO::getEmail, email);
    }

    default AdminUserDO selectByMobile(String mobile) {
        return selectOne(AdminUserDO::getMobile, mobile);
    }

    default AdminUserDO selectByMobile(String mobile,Long tenantId) {
        return selectOne(AdminUserDO::getMobile, mobile,AdminUserDO::getTenantId,tenantId);
    }

    default PageResult<AdminUserDO> selectPage(UserPageReqVO reqVO, Collection<String> deptIds) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AdminUserDO>()
                .likeIfPresent(AdminUserDO::getUsername, reqVO.getUsername())
                .likeIfPresent(AdminUserDO::getMobile, reqVO.getMobile())
                .eqIfPresent(AdminUserDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(AdminUserDO::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(AdminUserDO::getDeptId, deptIds)
                .orderByDesc(AdminUserDO::getId));
    }

    default PageResult<UserRespVO> selectPageV2(UserPageReqVO reqVO ) {
        MPJLambdaWrapper<AdminUserDO> wrapper = new MPJLambdaWrapper<AdminUserDO>()
                .selectAll(AdminUserDO.class)
                .selectAs(DeptDO::getName, UserRespVO::getDeptName)
                .leftJoin(DeptDO.class,DeptDO::getId,AdminUserDO::getDeptId)
                .likeIfExists(AdminUserDO::getNickname, reqVO.getNickname())
                .likeIfExists(AdminUserDO::getUsername, reqVO.getUsername())
                .likeIfExists(DeptDO::getName, reqVO.getDeptName())
                .eq(AdminUserDO::getDeleted, 0)
                .orderByDesc(AdminUserDO::getId);
        PageResult<UserRespVO> result = selectJoinPage(reqVO,UserRespVO.class,wrapper);
        return result ;

    }

    default List<AdminUserDO> selectListByNickname(String nickname) {
        return selectList(new LambdaQueryWrapperX<AdminUserDO>().like(AdminUserDO::getNickname, nickname));
    }

    default List<AdminUserDO> selectListByStatus(Integer status) {
        return selectList(AdminUserDO::getStatus, status);
    }

    default List<AdminUserDO> selectListByDeptIds(Collection<String> deptIds) {
        return selectList(AdminUserDO::getDeptId, deptIds);
    }

    default PageResult<AdminUserDO> selectPageByNickname(PageParam pageParam, String nickname){
        return selectPage(pageParam, new LambdaQueryWrapperX<AdminUserDO>()
                .likeIfPresent(AdminUserDO::getNickname, nickname));
    }

    default List<AdminUserRoleDeptDTO> selectJoinRoleAndDept(Long eqUserId){
        return this.selectJoinRoleAndDept(eqUserId,null);
    }

    default List<AdminUserRoleDeptDTO> selectJoinRoleAndDept(Long eqUserId,List<Long> inUserIds){
        MPJLambdaWrapper<AdminUserDO> joinWrapper = new MPJLambdaWrapper<AdminUserDO>()
                .selectAll(AdminUserDO.class)
                //一对多查询
                .selectCollection(RoleDO.class,AdminUserRoleDeptDTO::getRoleList)
                //一对一查询
                .selectAssociation(DeptDO.class,AdminUserRoleDeptDTO::getDept)
                .leftJoin(UserRoleDO.class,UserRoleDO::getUserId,AdminUserDO::getId)
                .leftJoin(RoleDO.class,RoleDO::getId,UserRoleDO::getRoleId)
                .leftJoin(DeptDO.class,DeptDO::getId,AdminUserDO::getDeptId);
        Optional.ofNullable(eqUserId).ifPresent(userId -> joinWrapper.eq(AdminUserDO::getId,userId));
        Optional.ofNullable(inUserIds).ifPresent(userIds -> joinWrapper.in(AdminUserDO::getId,userIds));
        return this.selectJoinList(AdminUserRoleDeptDTO.class,joinWrapper);
    }

    default List<AdminUserRoleDeptDTO> selectJoinDept(Long eqUserId){
        return this.selectJoinDept(eqUserId,null);
    }

    default List<AdminUserRoleDeptDTO> selectJoinDept(Long eqUserId,List<Long> inUserIds){
        MPJLambdaWrapper<AdminUserDO> joinWrapper = new MPJLambdaWrapper<AdminUserDO>()
                .selectAll(AdminUserDO.class)
                //一对一查询
                .selectAssociation(DeptDO.class,AdminUserRoleDeptDTO::getDept)
                .leftJoin(DeptDO.class,DeptDO::getId,AdminUserDO::getDeptId);
        Optional.ofNullable(eqUserId).ifPresent(userId -> joinWrapper.eq(AdminUserDO::getId,userId));
        Optional.ofNullable(inUserIds).ifPresent(userIds -> joinWrapper.in(AdminUserDO::getId,userIds));
        return this.selectJoinList(AdminUserRoleDeptDTO.class,joinWrapper);
    }

    default List<AdminUserRoleDeptDTO> selectJoinRoleAndDeptByRoleId(Long eqRoleId){
        return this.selectJoinRoleAndDeptByRoleId(eqRoleId,null);
    }

    default List<AdminUserRoleDeptDTO> selectJoinRoleAndDeptByRoleId(Long eqRoleId,List<Long> inRoleIds){
        MPJLambdaWrapper<AdminUserDO> joinWrapper = new MPJLambdaWrapper<AdminUserDO>()
                .selectAll(AdminUserDO.class)
                //一对多查询
                .selectCollection(RoleDO.class,AdminUserRoleDeptDTO::getRoleList)
                //一对一查询
                .selectAssociation(DeptDO.class,AdminUserRoleDeptDTO::getDept)
                .leftJoin(UserRoleDO.class,UserRoleDO::getUserId,AdminUserDO::getId)
                .leftJoin(RoleDO.class,RoleDO::getId,UserRoleDO::getRoleId)
                .leftJoin(DeptDO.class,DeptDO::getId,AdminUserDO::getDeptId);
        Optional.ofNullable(eqRoleId).ifPresent(roleId -> joinWrapper.eq(RoleDO::getId,roleId));
        Optional.ofNullable(inRoleIds).ifPresent(roleIds -> joinWrapper.in(RoleDO::getId,roleIds));
        return this.selectJoinList(AdminUserRoleDeptDTO.class,joinWrapper);
    }

    default List<AdminUserRoleDeptDTO> selectJoinDeptByRoleId(Long eqRoleId){
        return this.selectJoinDeptByRoleId(eqRoleId,null);
    }

    default List<AdminUserRoleDeptDTO> selectJoinDeptByRoleId(Long eqRoleId,List<Long> inRoleIds){
        MPJLambdaWrapper<AdminUserDO> joinWrapper = new MPJLambdaWrapper<AdminUserDO>()
                .selectAll(AdminUserDO.class)
                //一对一查询
                .selectAssociation(DeptDO.class,AdminUserRoleDeptDTO::getDept)
                .leftJoin(UserRoleDO.class,UserRoleDO::getUserId,AdminUserDO::getId)
                .leftJoin(RoleDO.class,RoleDO::getId,UserRoleDO::getRoleId)
                .leftJoin(DeptDO.class,DeptDO::getId,AdminUserDO::getDeptId);
        Optional.ofNullable(eqRoleId).ifPresent(roleId -> joinWrapper.eq(RoleDO::getId,roleId));
        Optional.ofNullable(inRoleIds).ifPresent(roleIds -> joinWrapper.in(RoleDO::getId,roleIds));
        return this.selectJoinList(AdminUserRoleDeptDTO.class,joinWrapper);
    }



}
