package com.yc.ship.module.trade.dal.mysql.insurance;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.mybatis.core.mapper.BaseMapperX;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.trade.controller.admin.insurance.vo.InsurancePageReqVO;
import com.yc.ship.module.trade.controller.admin.insurance.vo.InsuranceRespVO;
import com.yc.ship.module.trade.dal.dataobject.insurance.InsuranceDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 订单保险信息 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface InsuranceMapper extends BaseMapperX<InsuranceDO> {

    default PageResult<InsuranceDO> selectPage(InsurancePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<InsuranceDO>()
                .eqIfPresent(InsuranceDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(InsuranceDO::getInsuranceNo, reqVO.getInsuranceNo())
                .eqIfPresent(InsuranceDO::getInsuranceStatus, reqVO.getInsuranceStatus())
                .eqIfPresent(InsuranceDO::getPremium, reqVO.getPremium())
                .eqIfPresent(InsuranceDO::getAmount, reqVO.getAmount())
                .eqIfPresent(InsuranceDO::getInsuredNum, reqVO.getInsuredNum())
                .eqIfPresent(InsuranceDO::getPolicyNo, reqVO.getPolicyNo())
                .eqIfPresent(InsuranceDO::getProposalNo, reqVO.getProposalNo())
                .eqIfPresent(InsuranceDO::getElectronicPolicy, reqVO.getElectronicPolicy())
                .eqIfPresent(InsuranceDO::getRationType, reqVO.getRationType())
                .eqIfPresent(InsuranceDO::getRiskCode, reqVO.getRiskCode())
                .likeIfPresent(InsuranceDO::getHolderName, reqVO.getHolderName())
                .eqIfPresent(InsuranceDO::getHolderNo, reqVO.getHolderNo())
                .betweenIfPresent(InsuranceDO::getInsuranceEffectDate, reqVO.getInsuranceEffectDate())
                .betweenIfPresent(InsuranceDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(InsuranceDO::getId));
    }

    IPage<InsuranceRespVO> queryPage(IPage<InsuranceDO> iPage, @Param("param")InsurancePageReqVO pageReqVO);
    default void  updateAddOrderId(Long orderId,Long mainOrderId) {
        LambdaUpdateWrapper<InsuranceDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(InsuranceDO::getOrderId, orderId);
        updateWrapper.set(InsuranceDO::getOrderId, mainOrderId);
        update(updateWrapper);
    }

    default InsuranceDO selectByOrderId(Long orderId) {
        return selectOne(new LambdaQueryWrapper<InsuranceDO>().eq(InsuranceDO::getOrderId, orderId).last("limit 1"));
    }

    default void deleteByOrderId(Long orderId) {
        delete(new LambdaQueryWrapper<InsuranceDO>().eq(InsuranceDO::getOrderId, orderId));
    }

    default void deleteByVisitorId(Long id) {
        delete(new LambdaQueryWrapper<InsuranceDO>().eq(InsuranceDO::getVisitorId, id));
    }

    default InsuranceDO selectByVisitorId(Long id) {
        return selectOne(new LambdaQueryWrapper<InsuranceDO>().eq(InsuranceDO::getVisitorId, id).last("limit 1"));
    }

    default List<InsuranceDO> selectListByVisitorIds(List<Long> visitorIds) {
        return selectList(new LambdaQueryWrapper<InsuranceDO>().in(InsuranceDO::getVisitorId, visitorIds));
    }
}