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


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.breakcontract.BreakContractSettlementOwnerDao;
import com.clx.performance.enums.BreakContractSettlementOwnerEnum;
import com.clx.performance.mapper.breakcontract.BreakContractSettlementOwnerMapper;
import com.clx.performance.model.breakcontract.BreakContractSettlementOwner;
import com.clx.performance.param.pc.breakcontract.carrier.CarrierBreakContractSavePayeeParam;
import com.clx.performance.param.pc.breakcontract.carrier.PageCarrierBreakContractSettlementOwnerParam;
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.util.Objects;

/**
 * @author liruixin
 * Date 2023-10-24
 * Time 16:02
 */
@Repository
public class BreakContractSettlementOwnerDaoImpl extends BaseDaoImpl<BreakContractSettlementOwnerMapper, BreakContractSettlementOwner, Integer> implements BreakContractSettlementOwnerDao {


    @Override
    public IPage<BreakContractSettlementOwner> pageCarrierBreakContractSettlementOwnerList(PageCarrierBreakContractSettlementOwnerParam param) {
        LambdaQueryWrapper<BreakContractSettlementOwner> query = new LambdaQueryWrapper<>();
        query.eq(StringUtils.isNotBlank(param.getOrderNo()),BreakContractSettlementOwner :: getOrderNo,param.getOrderNo());
        query.eq(Objects.nonNull(param.getOwnerUserNo()),BreakContractSettlementOwner :: getOwnerUserNo,param.getOwnerUserNo());
        query.eq(Objects.nonNull(param.getStatus()),BreakContractSettlementOwner :: getStatus,param.getStatus());
        query.like(StringUtils.isNotBlank(param.getOwnerName()),BreakContractSettlementOwner ::getOwnerName ,param.getOwnerName());
        query.eq(StringUtils.isNotBlank(param.getSettlementNo()),BreakContractSettlementOwner ::getSettlementNo ,param.getSettlementNo());
        query.eq(Objects.nonNull(param.getBreakContractPartyType()),BreakContractSettlementOwner :: getBreakContractPartyType,param.getBreakContractPartyType());
        query.ge(StringUtils.isNotBlank(param.getBeginTime()), BreakContractSettlementOwner:: getCreateTime,param.getBeginTime());
        query.le(StringUtils.isNotBlank(param.getEndTime()),BreakContractSettlementOwner :: getCreateTime,param.getEndTime());
        if(StringUtils.isNotBlank(param.getSearchCondition())){
            query.and(i->i.eq(BreakContractSettlementOwner :: getOrderNo,param.getSearchCondition()).or()
                    .eq(BreakContractSettlementOwner :: getSettlementNo,param.getSearchCondition()).or()
                    .eq(BreakContractSettlementOwner :: getOwnerName,param.getSearchCondition()).or()
                    .eq(BreakContractSettlementOwner :: getOwnerUserNo,param.getSearchCondition())
            );
        }
        query.orderByDesc(BreakContractSettlementOwner::getId);
        return baseMapper.selectPage(Page.of(param.getPage(), param.getPageSize()),query);
    }

    @Override
    public boolean uploadInvoiceImg(Integer id, String invoiceImg) {
        return update(lUdWrapper()
                .eq(BreakContractSettlementOwner::getId, id)
                .eq(BreakContractSettlementOwner::getStatus, BreakContractSettlementOwnerEnum.Status.TO_BE_ACCOUNT.getCode())
                .set(BreakContractSettlementOwner::getInvoiceImg, invoiceImg)
                .set(BreakContractSettlementOwner::getStatus, BreakContractSettlementOwnerEnum.Status.OWNER_TO_BE_CONFIRMED.getCode())
        );
    }

    @Override
    public boolean saveCarrierBreakContractPayee(CarrierBreakContractSavePayeeParam param) {
        return update(lUdWrapper()
                .eq(BreakContractSettlementOwner::getId, param.getId())
                .set(BreakContractSettlementOwner::getPayee, param.getPayee())
                .set(BreakContractSettlementOwner::getPayeeBankName, param.getPayeeBankName())
                .set(BreakContractSettlementOwner::getPayeeBankCardNo, param.getPayeeBankCardNo())
                .set(BreakContractSettlementOwner::getPaymentImg, param.getPaymentImg())
                .set(BreakContractSettlementOwner::getPaymentBankCardNo, param.getPaymentBankCardNo())
                .set(BreakContractSettlementOwner::getPaymentBankName, param.getPaymentBankName())
                .set(BreakContractSettlementOwner::getStatus, param.getStatus())
        );
    }


    @Override
    public boolean updateStatus(Integer id, Integer from, Integer to) {
        return update(lUdWrapper()
                .eq(BreakContractSettlementOwner::getId, id)
                .eq(BreakContractSettlementOwner::getStatus, from)
                .set(BreakContractSettlementOwner::getStatus, to)
        );
    }

    @Override
    public Optional<BreakContractSettlementOwner> getDetailBySettlementNo(String settlementNo) {
        return Optional.of(lQrWrapper()
                .eq(BreakContractSettlementOwner::getSettlementNo, settlementNo))
                .map(super::getOne);
    }
}
