package com.dayou.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dayou.entity.DyoaRecord;
import com.dayou.entity.FinanceInvoice;
import com.dayou.enums.MainBusinessEnum;
import com.dayou.exception.ErrorCode;
import com.dayou.mapper.DyoaRecordMapper;
import com.dayou.service.IDyoaRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.utils.LoginContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 数据同步记录 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2024-07-25
 */
@DS("mbs")
@Service
public class DyoaRecordServiceImpl extends ServiceImpl<DyoaRecordMapper, DyoaRecord> implements IDyoaRecordService {


    @Autowired
    private DyoaRecordMapper dyoaRecordMapper;

    @Override
    @SuppressWarnings("unchecked")
    public Page<DyoaRecord> selectPage(Page page,DyoaRecord dyoaRecord){
        return this.page(page, new QueryWrapper<DyoaRecord>(dyoaRecord));
    }

    @Override
    public void insert(MainBusinessEnum businessEnum,Long mbsId,String dyoaId,Date orderTime){
        DyoaRecord dyoaRecord = new DyoaRecord();
        dyoaRecord.setDyoaId(dyoaId);
        dyoaRecord.setMbsId(mbsId);
        dyoaRecord.setMbsType(businessEnum.name());
        dyoaRecord.setUserId(1L);
        dyoaRecord.setOrderDatetime(orderTime);
        this.save(dyoaRecord);
    }

    @Override
    public void isExist(MainBusinessEnum businessEnum,String dyodId){
        DyoaRecord record = this.getOne(new LambdaQueryWrapper<DyoaRecord>().eq(DyoaRecord::getMbsType, businessEnum.name()
        ).eq(DyoaRecord::getDyoaId, dyodId).isNotNull(DyoaRecord::getMbsId));

        if (record!=null){
            ErrorCode.throwBusinessException(ErrorCode.CUSTOM_ERROR,"数据已同步。");
        }
    }

    @Override
    public Date getMaxOrderTime() {
        return dyoaRecordMapper.getMaxOrderTime();
    }

    @Override
    public Date getMaxPersonalTargetTime() {
        return dyoaRecordMapper.getMaxPersonalTargetTime();
    }

    @Override
    public Date getMaxRealAmountCreatedTime() {
        return dyoaRecordMapper.getMaxRealAmountCreatedTime();
    }

    @Override
    public Date getMaxClaimDate() {
        return dyoaRecordMapper.getMaxClaimDate();
    }

    @Override
    public List<DyoaRecord> selectOrderFundIdByDyoaOrderId() {
        return dyoaRecordMapper.selectOrderFundIdByDyoaOrderId();
    }

    @Override
    public Date getMaxFinanceInvoiceCreatedTime() {
        return dyoaRecordMapper.getMaxFinanceInvoiceCreatedTime();
    }

    @Override
    public Long getMbsIdByDyOAId(String id) {
        return dyoaRecordMapper.getMbsIdByDyOAId(id);
    }

    @Override
    public Long getPersonalTargetId(String id) {
        return dyoaRecordMapper.getPersonalTargetId(id);
    }

    @Override
    public Long getMbsIdByDyOAIdForAssets(String id) {
        return dyoaRecordMapper.getMbsIdByDyOAIdForAssets(id);
    }

    @Override
    public FinanceInvoice getMbsIdByDyOAiDForInvoice(String dyoaId) {
        return dyoaRecordMapper.getMbsIdByDyOAiDForInvoice(dyoaId);
    }

    @Override
    public Long getMbsRealAmountId(String incomeId, String name) {
        return dyoaRecordMapper.getMbsRealAmountId(incomeId,name);
    }

    @Override
    public String getMembersAndSignerInDyoa(Long id) {
        return dyoaRecordMapper.getMembersAndSignerInDyoa(id);
    }


}
