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

import cn.hutool.core.text.CharSequenceUtil;
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.executor.enums.OrderDeletedEnum;
import com.cf.imes.module.system.api.organ.dto.OrgStatisticsIsLapseRespDTO;
import com.cf.imes.module.system.api.organ.dto.OrgStatisticsReqDTO;
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 org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
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())
                .eqIfPresent(OrganizationDO::getName, reqVO.getExactName())
                .eqIfPresent(OrganizationDO::getProvince, reqVO.getProvince())
                .eqIfPresent(OrganizationDO::getCity, reqVO.getCity())
                .eqIfPresent(OrganizationDO::getCounty, reqVO.getCounty())
                .and(CharSequenceUtil.isNotBlank(reqVO.getName()), wrapper ->{
                    wrapper.or(Boolean.TRUE).like(OrganizationDO::getName, reqVO.getName());
                    wrapper.or(CharSequenceUtil.isNotBlank(reqVO.getPyAll())).like(OrganizationDO::getPinyinFull, reqVO.getPyAll());
                    wrapper.or(CharSequenceUtil.isNotBlank(reqVO.getPyFirstChar())).like(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);
    }

    List<OrganizationDO> selectByContactMobile(@Param("contactMobile") String contactMobile,@Param("organId") Long organId);

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

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


    default Integer selectOrgCount() {
        return Math.toIntExact(selectCount(new LambdaQueryWrapperX<OrganizationDO>()
                .eq(OrganizationDO::getDeleted, OrderDeletedEnum.NOT_DELETED.getStatus())));
    }

    default Integer selectOrgCountAdd() {
        return Math.toIntExact(selectCount(new LambdaQueryWrapperX<OrganizationDO>()
                .eq(OrganizationDO::getDeleted, OrderDeletedEnum.NOT_DELETED.getStatus())
                .between(OrganizationDO::getCreateTime, LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT),
                        LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59)))));
    }

    default Integer selectOrgCountDel() {
        return Math.toIntExact(selectCount(new LambdaQueryWrapperX<OrganizationDO>()
                .eq(OrganizationDO::getDeleted, OrderDeletedEnum.DELETED.getStatus())
                .between(OrganizationDO::getDeleted, LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT),
                        LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59)))));
    }

    Integer selectOrgSilentCount(@Param("dateTime")LocalDateTime dateTime);


    default List<OrganizationDO> selectOrganList(Integer status) {
        return selectList(new LambdaQueryWrapperX<OrganizationDO>()
                .eqIfPresent(OrganizationDO::getDeleted,false)
                .eqIfPresent(OrganizationDO::getStatus,status));
    }


    List<OrganizationDO> selectOrganDataSource(@Param("filed") String filed,@Param("organId") Long organId);


    /**
     * 组织按时间统计新增数量
     */
    List<OrgStatisticsIsLapseRespDTO> selectOrgCountAddByOrderDate(@Param("req") OrgStatisticsReqDTO reqVO);

    /**
     * 组织按时间分组统计注销数量
     */
    List<OrgStatisticsIsLapseRespDTO> selectOrgCountLapseByOrderDate(@Param("req") OrgStatisticsReqDTO reqVO);

    default List<OrganizationDO> selectOrgCountExpire(LocalDateTime today, LocalDateTime afterDay){
        return selectList(new LambdaQueryWrapperX<OrganizationDO>()
                .eq(OrganizationDO::getDeleted,false)
                .between(OrganizationDO::getExpireTime,today,afterDay));
    }

    default List<OrganizationDO> selectOrgCountExpired(LocalDateTime alertDay){
        return selectList(new LambdaQueryWrapperX<OrganizationDO>()
                .eq(OrganizationDO::getDeleted,false)
                .lt(OrganizationDO::getExpireTime,alertDay));
    }
    default List<OrganizationDO> selectOrganListByName(String organName) {
        return selectList(new LambdaQueryWrapperX<OrganizationDO>()
                .eqIfPresent(OrganizationDO::getDeleted,false)
                .likeIfPresent(OrganizationDO::getName,organName)
                .select(OrganizationDO::getId));
    }


}
