package com.cf.imes.module.system.dal.mysql.user;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.framework.security.core.LoginUser;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import com.cf.imes.module.system.controller.admin.user.vo.user.UserPageReqVO;
import com.cf.imes.module.system.dal.dataobject.user.AdminUserDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

@Mapper
public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {

    default AdminUserDO selectByUsername(String username, Long organId) {
        organId = Objects.isNull(organId) ? SecurityFrameworkUtils.getLoginUser().getOrganId(): organId;
       return selectOne(new LambdaQueryWrapperX<AdminUserDO>()
               .eq(AdminUserDO::getUsername, username)
               .eqIfPresent(AdminUserDO::getOrganId, organId)
       );
    }

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

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

    default PageResult<AdminUserDO> selectPage(UserPageReqVO reqVO, Collection<Long> deptIds) {
        LambdaQueryWrapperX<AdminUserDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        assert loginUser != null;
        Boolean isSupAdmin = loginUser.getIsSupAdmin();
        if(isSupAdmin && !Objects.isNull(reqVO.getOrganId())) {
            lambdaQueryWrapperX.eqIfPresent(AdminUserDO::getOrganId, reqVO.getOrganId());
        }else{
            lambdaQueryWrapperX.eqIfPresent(AdminUserDO::getOrganId, loginUser.getOrganId());
        }
        return selectPage(reqVO, lambdaQueryWrapperX
                .likeIfPresent(AdminUserDO::getUsername, reqVO.getUsername())
                .likeIfPresent(AdminUserDO::getMobile, reqVO.getMobile())
                .eqIfPresent(AdminUserDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(AdminUserDO::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(AdminUserDO::getDeptId, deptIds)
                .and(StrUtil.isNotBlank(reqVO.getNickname()), wrapper -> {
                    if (StrUtil.isNotBlank(reqVO.getPyAll())) {
                        wrapper.or(Boolean.TRUE).like(AdminUserDO::getPinyinFull, reqVO.getPyAll()) ;
                    }
                    if (StrUtil.isNotBlank(reqVO.getPyFirstChar())) {
                        wrapper.or(Boolean.TRUE).like(AdminUserDO::getPinyinInitial, reqVO.getPyFirstChar()) ;
                    }
                    if(StrUtil.isNotBlank(reqVO.getNickname())) {
                        wrapper.or(Boolean.TRUE).like(AdminUserDO::getNickname, reqVO.getNickname()) ;
                    }
                })
                .orderByDesc(AdminUserDO::getId));
    }

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

    default List<AdminUserDO> selectListByStatus(Integer status, Long organId, Long deptId) {
        return selectList(new LambdaQueryWrapperX<AdminUserDO>()
                .eqIfPresent(AdminUserDO::getOrganId, organId)
                .eqIfPresent(AdminUserDO::getDeptId, deptId)
                .eq(AdminUserDO::getStatus, status));
       // return selectList(AdminUserDO::getStatus, status);
    }

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

    default List<AdminUserDO> selectListByTerms(Integer status, String namePyAll,String namePyFirstChar,String name) {
        return selectList(new LambdaQueryWrapperX<AdminUserDO>()
                .eqIfPresent(AdminUserDO::getStatus, status)
                .likeIfPresent(AdminUserDO::getNickname, name)
                .or(StrUtil.isNotBlank(name))
                .likeIfPresent(AdminUserDO::getPinyinFull, namePyAll)
                .or(StrUtil.isNotBlank(namePyAll))
                .likeIfPresent(AdminUserDO::getPinyinInitial, namePyFirstChar)
                .orderByDesc(AdminUserDO::getId));
    }
}
