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

import cn.hutool.core.util.StrUtil;
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.module.system.controller.admin.organ.vo.organ.OrganPageReqVO;
import com.cf.imes.module.system.dal.dataobject.organ.OrganizationDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 组织 Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface OrganMapper extends BaseMapperX<OrganizationDO> {

    default PageResult<OrganizationDO> selectPage(OrganPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrganizationDO>()
                .likeIfPresent(OrganizationDO::getContactName, reqVO.getContactName())
                .likeIfPresent(OrganizationDO::getContactMobile, reqVO.getContactMobile())
                .eqIfPresent(OrganizationDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(OrganizationDO::getCreateTime, reqVO.getCreateTime())
                .and(StrUtil.isNotBlank(reqVO.getName()), wrapper ->{
                    wrapper.or(Boolean.TRUE).like(OrganizationDO::getName, reqVO.getName());
                    wrapper.or(StrUtil.isNotBlank(reqVO.getPyAll())).like(OrganizationDO::getPinyinFull, reqVO.getPyAll());
                    wrapper.or(StrUtil.isNotBlank(reqVO.getPyFirstChar())).like(OrganizationDO::getPinyinInitial, reqVO.getPyFirstChar());
                })
                /*.likeIfPresent(OrganizationDO::getName, reqVO.getName())
                .or(StrUtil.isNotBlank(reqVO.getName()))
                .likeIfPresent(OrganizationDO::getPinyinFull, reqVO.getPyAll())
                .or(StrUtil.isNotBlank(reqVO.getName()))
                .likeIfPresent(OrganizationDO::getPinyinInitial, reqVO.getPyFirstChar())*/
                .orderByDesc(OrganizationDO::getId));
    }

    default OrganizationDO selectByName(String name) {
        return selectOne(OrganizationDO::getName, name);
    }

    default OrganizationDO selectByWebsite(String website) {
        return selectOne(OrganizationDO::getWebsite, website);
    }

    default Long selectCountByPackageId(Long packageId) {
        return selectCount(OrganizationDO::getPackageId, packageId);
    }

    default List<OrganizationDO> selectListByPackageId(Long packageId) {
        return selectList(OrganizationDO::getPackageId, packageId);
    }

}
