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

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.date.LocalDateTimeUtils;
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.controller.admin.organ.vo.organ.OrganPageReqVO;
import com.cf.imes.module.system.controller.admin.statistics.vo.OrgStatusGroupStatisticsReqVO;
import com.cf.imes.module.system.dal.dataobject.organ.OrganizationDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

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

    /**
     * 忽略逻辑删除查询组织
     * @param id
     * @return
     */
    @Select("SELECT * FROM system_organization WHERE id = #{id}")
    OrganizationDO selectByIdWithDeleted(@Param("id") Long id);

    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())
                .betweenIfPresent(OrganizationDO::getExpireTime, LocalDateTimeUtils.generateTimeSection(reqVO.getExpireTime()))
                .likeIfPresent(OrganizationDO::getRemark, reqVO.getRemark())
                .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())));
    }

    /**
     * 查询今天创建的组织
     *
     * @return
     */
    Integer selectOrgCountAdd(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 查询今天被删除的组织
     *
     * @return
     */
    Integer selectOrgCountDel(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    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") OrgStatusGroupStatisticsReqVO reqVO);

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

    /**
     * 分页查询即将过期的组织
     * @param param
     * @return
     */
    default IPage<OrganizationDO> selectOrgCountExpire(PageParam param) {
        Page<OrganizationDO> page = new Page<>(param.getPageNo(), param.getPageSize());
        LocalDateTime today = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime afterDay = today.plusDays(15).withHour(23).withMinute(59).withSecond(59);
        return selectPage(page, new LambdaQueryWrapperX<OrganizationDO>()
                .eq(OrganizationDO::getDeleted, false)
                .between(OrganizationDO::getExpireTime, today, afterDay)
                .orderByDesc(OrganizationDO::getId));
    }

    /**
     * 分页查询过期的组织
     * @param param
     * @return
     */
    default IPage<OrganizationDO> selectOrgCountExpired(PageParam param) {
        Page<OrganizationDO> page = new Page<>(param.getPageNo(), param.getPageSize());
        return selectPage(page, new LambdaQueryWrapperX<OrganizationDO>()
                .eq(OrganizationDO::getDeleted, false)
                .lt(OrganizationDO::getExpireTime, LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(0))
                .orderByDesc(OrganizationDO::getId));
    }

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

    /**
     * 带产品条件的组织分页查询
     *
     * @param page
     * @param pageReqVO
     * @return
     */
    IPage<OrganizationDO> selectPageWithProductExpire(@Param("page") IPage<OrganizationDO> page, @Param("reqVO") OrganPageReqVO pageReqVO);
}
