package cn.ey.biz.module.system.dal.mapper.user;

import cn.ey.biz.common.enums.CommonStatusEnum;
import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.framework.mybatis.core.mapper.BaseMapperX;
import cn.ey.biz.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ey.biz.module.system.controller.admin.user.vo.user.UserExportReqVO;
import cn.ey.biz.module.system.controller.admin.user.vo.user.UserPageReqVO;
import cn.ey.biz.module.system.controller.admin.user.vo.user.UserPageTreeReqVO;
import cn.ey.biz.module.system.dal.dataobject.user.AdminUserDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

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

@Repository
@Mapper
public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {

    default AdminUserDO selectByUsername(String username) {
        return selectOne(new LambdaQueryWrapper<AdminUserDO>().eq(AdminUserDO::getUsername, username)
                .eq(AdminUserDO::getStatus, CommonStatusEnum.ENABLE.getStatus()));
    }

    default AdminUserDO selectByEmail(String email) {
        return selectOne(new LambdaQueryWrapper<AdminUserDO>().eq(AdminUserDO::getBusinessEmail, email));
    }

    default AdminUserDO selectByUserCode(String userCode) {
        return selectOne(new LambdaQueryWrapper<AdminUserDO>()
                .eq(AdminUserDO::getUserCode, userCode)
                .eq(AdminUserDO::getStatus, CommonStatusEnum.ENABLE.getStatus())
        );
    }

    default AdminUserDO selectByMobile(String mobile) {
        return selectOne(new LambdaQueryWrapper<AdminUserDO>().eq(AdminUserDO::getMobile, mobile));
    }

    default PageResult<AdminUserDO> selectPage(UserPageReqVO reqVO, Collection<Long> deptIds) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AdminUserDO>()
                .likeIfPresent(AdminUserDO::getUsername, reqVO.getUsername())
                .likeIfPresent(AdminUserDO::getMobile, reqVO.getMobile())
                .eq(StringUtils.isNotBlank(reqVO.getMrCode()), AdminUserDO::getMgmtRegion, reqVO.getMrCode())
                .eq(StringUtils.isNotBlank(reqVO.getOuCode()), AdminUserDO::getOperatingUnit, reqVO.getOuCode())
                .eq(StringUtils.isNotBlank(reqVO.getBusinessEmail()), AdminUserDO::getBusinessEmail, reqVO.getBusinessEmail())
                .eq(StringUtils.isNotBlank(reqVO.getGpn()), AdminUserDO::getGpn, reqVO.getGpn())
                .eq(StringUtils.isNotBlank(reqVO.getLpn()), AdminUserDO::getLpn, reqVO.getLpn())
                .eq(StringUtils.isNotBlank(reqVO.getGui()), AdminUserDO::getGui, reqVO.getGui())
                .eq(StringUtils.isNotBlank(reqVO.getEmployeeStatus()), AdminUserDO::getEmployeeStatus, reqVO.getEmployeeStatus())
                .eq(StringUtils.isNotBlank(reqVO.getEmployeeClass()), AdminUserDO::getEmployeeClass, reqVO.getEmployeeClass())
                .eq(StringUtils.isNotBlank(reqVO.getEmployeeType()), AdminUserDO::getEmployeeType, reqVO.getEmployeeType())
                .eqIfPresent(AdminUserDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(AdminUserDO::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(AdminUserDO::getDeptId, deptIds)
                .orderByDesc(AdminUserDO::getId));
    }

    default List<AdminUserDO> selectList(UserExportReqVO reqVO, Collection<Long> deptIds) {
        return selectList(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));
    }

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

    default List<AdminUserDO> selectListByUsername(String username) {
        return selectList(new LambdaQueryWrapperX<AdminUserDO>().like(AdminUserDO::getUsername, username));
    }

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

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

    IPage<AdminUserDO> getUserPageList(IPage page, @Param("pageReqVO") UserPageTreeReqVO reqVO);
}
