package cn.iocoder.yudao.module.system.dal.mysql.biz;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ContractPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyContractDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;


/**
 * 长者合同管理 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface ElderlyContractMapper extends BaseMapperX<ElderlyContractDO> {


    List<ElderlyContractDO> findPage(Page<ElderlyContractDO> page,@Param("params") ContractPageReqVO reqVO);

    List<ElderlyContractDO> selectMessageList();

    List<ElderlyContractDO> selectInProgressByElderIds(@Param("elderIds") List<Long> elderIds);

    int batchUpdateStatusToNotStarted(@Param("tenantId") Long tenantId, @Param("nowDate") Date nowDate);

    int batchUpdateStatusToExpired(@Param("tenantId") Long tenantId, @Param("nowDate") Date nowDate);

    List<ElderlyContractDO> selectExpiringWithinOneMonth(@Param("tenantId") Long tenantId,
                                                         @Param("startDate") Date startDate,
                                                         @Param("endDate") Date endDate);

    default ElderlyContractDO selectValidContract(String contractNumber) {
        return selectOne(new LambdaQueryWrapperX<ElderlyContractDO>()
                        .eq(ElderlyContractDO::getContractNumber, contractNumber)
                        .eq(ElderlyContractDO::getTenantId, TenantContextHolder.getTenantId())
                        .eq(ElderlyContractDO::getStatus, 1)
                        .orderByDesc(ElderlyContractDO::getCreatedTime)
                        .last("LIMIT 1")
        );
    }
}