package com.crm.ucard.service.impl;

import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.ucard.*;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.ucard.CardPageEntity;
import com.crm.rely.backend.core.pojo.table.ucard.CardGlobalPaymentOrderTable;
import com.crm.rely.backend.core.pojo.table.ucard.CardMerchantUserTable;
import com.crm.rely.backend.core.pojo.table.ucard.CardTransacOrderTable;
import com.crm.rely.backend.core.pojo.table.ucard.UcardCardTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.ucard.dao.mapper.CardTypeMapper;
import com.crm.ucard.dao.mapper.UcardCardMapper;
import com.crm.ucard.dao.repository.CardGlobalPaymentOrderRepository;
import com.crm.ucard.dao.repository.CardMerchantUserRepository;
import com.crm.ucard.dao.repository.CardTransacOrderRepository;
import com.crm.ucard.dao.repository.UcardCardRepository;
import com.crm.ucard.service.UcardApiService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @program: crm-backend
 * @description:
 * @author: houn
 * @create: 2019-07-25 16:14
 */

@Service
public class UardApiServiceImpl implements UcardApiService {

    @Autowired
    private UcardCardRepository cardRepository;
    @Autowired
    private CardTransacOrderRepository cardTransacOrderRepository;
    @Autowired
    private CardGlobalPaymentOrderRepository globalPaymentOrderRepository;
    @Autowired
    private CardMerchantUserRepository merchantUsereRepository;
    @Autowired
    private CardTypeMapper cardTypeMapper;
    @Autowired
    private UcardCardMapper cardMapper;

    @Override
    public List<UcardCardDto> getCardList(CardPageEntity entity) {
        entity.setIsDisplayCancel("1");
        return cardMapper.pageList(entity);
    }

    @Override
    public UcardCardDto getCardSingle(SingleLongEntity entity) throws ServiceException{
        UcardCardTable table = cardRepository.getFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        UcardCardDto dto = new UcardCardDto();
        BeanUtils.copyProperties(table, dto);

        CardTypeDto typeDto = cardTypeMapper.getByCardTypeId(table.getCardTypeId());
        dto.setRechargeCurrency(typeDto.getRechargeCurrency());
        dto.setRechargeMinQuota(typeDto.getRechargeMinQuota());
        dto.setRechargeMaxQuota(typeDto.getRechargeMaxQuota());
        dto.setRechargeDigital(typeDto.getRechargeDigital());
        dto.setRechargeFixedFee(typeDto.getRechargeFixedFee());
        CardMerchantUserTable userTable = merchantUsereRepository.getByUniqueId(table.getUniqueId());
        if (userTable.getCardRechargeFeeRate() !=  null){
            dto.setRechargeFeeRate(userTable.getCardRechargeFeeRate());
        } else {
            dto.setRechargeFeeRate(typeDto.getRechargeFeeRate());
        }
        return dto;
    }

    @Override
    public CardTransacOrderDto getTransactionSingle(SingleLongEntity entity) throws ServiceException {
        CardTransacOrderTable table = cardTransacOrderRepository.getFirstById(entity.getId());

        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        CardTransacOrderDto dto = new CardTransacOrderDto();
        BeanUtils.copyProperties(table, dto);
        return dto;
    }

    @Override
    public GlobalPaymentOrderDto getTransferPaymentSingle(SingleLongEntity entity) throws ServiceException  {

        CardGlobalPaymentOrderTable table = globalPaymentOrderRepository.getFirstById(entity.getId());

        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        GlobalPaymentOrderDto dto = new GlobalPaymentOrderDto();
        BeanUtils.copyProperties(table, dto);
        return dto;
    }

    @Override
    public MerchantUserDto merchantUserSingle(Long cId) throws ServiceException {
        CardMerchantUserTable table = merchantUsereRepository.findByCId(cId);
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        MerchantUserDto dto = new MerchantUserDto();
        BeanUtils.copyProperties(table, dto);
        return dto;
    }

}

