package com.clx.performance.dao.impl.settle;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clx.performance.dao.settle.SettlementOwnerDetailDao;
import com.clx.performance.enums.settle.SettlementOwnerDetailEnum;
import com.clx.performance.enums.settle.SettlementOwnerEnum;
import com.clx.performance.mapper.settle.SettlementOwnerDetailMapper;
import com.clx.performance.model.settle.SettlementOwnerDetail;
import com.clx.performance.param.pc.owner.PageCarrierSettlementOwnerDetailParam;
import com.clx.performance.param.pc.owner.PageOwnerSettlementOwnerDetailParam;
import com.msl.common.base.Optional;
import com.msl.common.dao.impl.BaseDaoImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

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

/**
 * @Author: aiqinguo
 * @Description: 货主运单计费明细
 * @Date: 2023-10-11 18:04:09
 * @Version: 1.0
 */
@Repository
public class SettlementOwnerDetailDaoImpl extends BaseDaoImpl<SettlementOwnerDetailMapper, SettlementOwnerDetail, Integer> implements SettlementOwnerDetailDao {

    @Override
    public boolean updateInvoiceType(SettlementOwnerDetail item) {
        return update(lUdWrapper()
                .eq(SettlementOwnerDetail::getId, item.getId())
                .set(SettlementOwnerDetail::getInvoiceType, item.getInvoiceType())
                .set(SettlementOwnerDetail::getInvoiceFreight, item.getInvoiceFreight())
                .set(SettlementOwnerDetail::getSettlementFreight, item.getSettlementFreight())
                .set(SettlementOwnerDetail::getFinalPaymentStatus, item.getFinalPaymentStatus())
                .set(SettlementOwnerDetail::getPrepayFreight, item.getPrepayFreight())
                .set(SettlementOwnerDetail::getLoanFreight, item.getLoanFreight())

                .set(SettlementOwnerDetail::getInvoicingCompanyId, item.getInvoicingCompanyId())
                .set(SettlementOwnerDetail::getInvoicingCompanyShorterName, item.getInvoicingCompanyShorterName())
                .set(SettlementOwnerDetail::getInvoicingCompanyGroupCode, item.getInvoicingCompanyGroupCode())
                .set(SettlementOwnerDetail::getInvoiceServiceFeeRate, item.getInvoiceServiceFeeRate())
                .set(SettlementOwnerDetail::getInvoiceServiceFee, item.getInvoiceServiceFee())
        );
    }

    @Override
    public boolean updateSettlementNo(SettlementOwnerDetail item) {
        return update(lUdWrapper()
                .eq(SettlementOwnerDetail::getId, item.getId())
                .set(SettlementOwnerDetail::getSettlementNo, item.getSettlementNo())
        );
    }

    @Override
    public boolean updateInvoiceStatusBySettlementNo(String settlementNo, Integer invoiceStatus){
        return update(lUdWrapper()
                .eq(SettlementOwnerDetail::getSettlementNo, settlementNo)
                .set(SettlementOwnerDetail::getInvoiceStatus, invoiceStatus)
        );
    }

    @Override
    public boolean updateFinalPaymentStatusBySettlementNo(String settlementNo, Integer finalPaymentStatus){
        return update(lUdWrapper()
                .eq(SettlementOwnerDetail::getSettlementNo, settlementNo)
                .set(SettlementOwnerDetail::getFinalPaymentStatus, finalPaymentStatus)
        );
    }

    @Override
    public Optional<SettlementOwnerDetail> getByChildNo(String childNo) {
        return Optional.of(childNo)
                .map(item -> lQrWrapper()
                        .eq(SettlementOwnerDetail::getChildNo, item)
                )
                .map(super::getOne);
    }
    @Override
    public List<SettlementOwnerDetail> getBySettlementNo(String settlementNo) {
        return list(lQrWrapper().eq(SettlementOwnerDetail::getSettlementNo, settlementNo));
    }

    @Override
    public List<SettlementOwnerDetail> getBySettlementNoAndInvoiceType(String settlementNo, Integer invoiceType, Integer invoiceStatus) {
        return list(lQrWrapper().eq(SettlementOwnerDetail::getSettlementNo, settlementNo)
                .eq(SettlementOwnerDetail::getInvoiceType, invoiceType)
                .eq(SettlementOwnerDetail::getInvoiceStatus, invoiceStatus)
        );
    }


    @Override
    public IPage<SettlementOwnerDetail> pageSettlementOwnerDetail(
            PageCarrierSettlementOwnerDetailParam param) {
        LambdaQueryWrapper<SettlementOwnerDetail> query = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(param.getChildNo())){
            query.eq(SettlementOwnerDetail :: getChildNo,param.getChildNo());
        }
        if(StringUtils.isNotBlank(param.getOrderNo())){
            query.eq(SettlementOwnerDetail :: getOrderNo,param.getOrderNo());
        }
        if(Objects.nonNull(param.getOwnerUserNo())){
            query.eq(SettlementOwnerDetail :: getOwnerUserNo,param.getOwnerUserNo());
        }
        if(Objects.nonNull(param.getOwnerUserName())){
            query.like(SettlementOwnerDetail :: getOwnerName,param.getOwnerUserName());
        }
        query.orderByDesc(SettlementOwnerDetail ::getCreateTime);
        return baseMapper.selectPage(Page.of(param.getPage(), param.getPageSize()),query);
    }

    @Override
    public Integer getInvoiceType(String orderNo) {
        return baseMapper.getInvoiceType(orderNo);
    }

    @Override
    public void updateSettlementByChildNoList(List<String> childNoList) {
         update(lUdWrapper()
                .in(SettlementOwnerDetail::getChildNo, childNoList)
                .set(SettlementOwnerDetail::getInvoiceStatus, SettlementOwnerDetailEnum.InvoiceStatus.YES.getCode())
        );
    }

    @Override
    public void updateFinalPaymentStatusBySettlementNoForOnline(String settlementNo, Integer finalPaymentStatus) {
        update(lUdWrapper()
                .eq(SettlementOwnerDetail::getSettlementNo, settlementNo)
                .eq(SettlementOwnerDetail::getFinalPaymentStatus, SettlementOwnerDetailEnum.FinalPaymentStatus.NO.getCode())
                .set(SettlementOwnerDetail::getFinalPaymentStatus, finalPaymentStatus)

        );
    }

    @Override
    public List<SettlementOwnerDetail> selectListByOrderNo(String orderNo) {
        return baseMapper.selectList(lQrWrapper().eq(SettlementOwnerDetail::getOrderNo, orderNo)
                .isNull(SettlementOwnerDetail::getSettlementNo)
        );
    }

    @Override
    public IPage<SettlementOwnerDetail> pageOwnerSettlementOrderDetailList(PageOwnerSettlementOwnerDetailParam param) {
        LambdaQueryWrapper<SettlementOwnerDetail> query = new LambdaQueryWrapper<>();
        query.eq(SettlementOwnerDetail :: getSettlementNo,param.getSettlementNo());
        query.eq(StringUtils.isNotBlank(param.getChildNo()),SettlementOwnerDetail :: getChildNo,param.getChildNo());
        query.eq(StringUtils.isNotBlank(param.getOrderNo()),SettlementOwnerDetail :: getOrderNo,param.getOrderNo());
        query.eq(StringUtils.isNotBlank(param.getSettlementSubNo()),SettlementOwnerDetail :: getSettlementSubNo,param.getSettlementSubNo());
        return baseMapper.selectPage(Page.of(param.getPage(), param.getPageSize()),query);
    }

    @Override
    public void updateSettlementMerge(String settlementNo, List<String> settlementNos) {
         update(lUdWrapper()
                .in(SettlementOwnerDetail::getSettlementNo, settlementNos)
                .setSql("settlement_sub_no = settlement_no")
                .set(SettlementOwnerDetail :: getSettlementNo,settlementNo)
        );
    }

    @Override
    public List<SettlementOwnerDetail> listInvoiceStatusSync() {
        return list(lUdWrapper()
                .gt(SettlementOwnerDetail::getCreateTime, LocalDateTime.now().minusMonths(3))
                .eq(SettlementOwnerDetail::getInvoiceChannel, SettlementOwnerDetailEnum.InvoiceChannel.BROKER.getCode())
                .eq(SettlementOwnerDetail::getInvoiceStatus, SettlementOwnerDetailEnum.InvoiceStatus.NO.getCode())
                .eq(SettlementOwnerDetail::getInvoiceType, SettlementOwnerEnum.InvoiceType.ONLINE.getCode())
        );
    }

}
