package com.yc.ship.module.ota.api;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.tenant.core.aop.TenantIgnore;
import com.yc.ship.module.ota.api.dto.*;
import com.yc.ship.module.ota.dal.dataobject.distributor.DistributorDO;
import com.yc.ship.module.ota.service.distributor.DistributorService;
import com.yc.ship.module.ota.service.distributorproduct.DistributorProductService;
import com.yc.ship.module.ota.service.distributorrecharge.DistributorRechargeService;
import com.yc.ship.module.ota.service.distributorselfrecharge.DistributorSelfRechargeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.yc.ship.framework.common.pojo.CommonResult.success;

@Service
public class OtaDistributorApiImpl implements OtaDistributorApi{

    @Resource
    private DistributorService distributorService;

    @Resource
    private DistributorProductService distributorProductService;

    @Resource
    private DistributorRechargeService distributorRechargeService;
    @Resource
    private DistributorSelfRechargeService distributorSelfRechargeService;

    @Override
    public DistributorRespDTO getDistributorByLoginUserId(Long loginUserId) {
        DistributorDO distributorDO = distributorService.getDistributorByLoginUserId(loginUserId);
        if(distributorDO == null) {
            return null;
        }
        return BeanUtils.toBean(distributorDO, DistributorRespDTO.class);
    }

    @Override
    public CommonResult<DistributorRechargeRespDTO> tradeRecharge(DistributorRechargeReqDTO rechargeReqDTO) {
        return distributorRechargeService.tradeRecharge(rechargeReqDTO);
    }



    @Override
    public DistributorRespDTO getDistributorById(Long distributorId) {
        DistributorDO distributor = distributorService.getDistributorById(distributorId);
        return BeanUtils.toBean(distributor, DistributorRespDTO.class);
    }

    @Override
    public CommonResult<?> distributorCMBRecharge(DistributorSelfRechargeReqDTO payNotifyReqDTO) {
        Long id = distributorSelfRechargeService.distributorCMBRecharge(payNotifyReqDTO.getDistributorId(),payNotifyReqDTO.getOrderId(), payNotifyReqDTO.getPayOrderId(), payNotifyReqDTO.getPayStatus());
        return CommonResult.success(id);
    }

    @Override
    public CommonResult<DistributorProductRespDTO> getDistributorProductById(Long distributorProductId) {
        return distributorProductService.getDistributorProductById(distributorProductId);
    }
    @Override
    public CommonResult<List<DistributorProductRespDTO.DistributorProjectDTO>> getProductByDistributor(Long distributortId, Long productId, String distributorProductId) {
        List<DistributorProductRespDTO.DistributorProjectDTO> list =  distributorProductService.getProductByDistributor(distributortId, productId,distributorProductId);
        return success(list);
    }

    @Override
    public CommonResult<DistributorProductRespDTO> getCalendarProductById(Long distributorProductId, String useDate) {
        return distributorProductService.getDistributorCalendarProductById(distributorProductId, useDate);
    }

    @Override
    public CommonResult<DistributorRespDTO> getDistributorByOtaAccount(String otaAccount, String typeCode) {
        return distributorService.getDistributorByOtaAccount(otaAccount, typeCode);
    }

    @Override
    public CommonResult<DistributorProductRespDTO> getAgencyCalendarRangeProductById(Long distributorProductId, String startDate, String endDate) {
        return distributorProductService.getAgencyCalendarRangeProductById(distributorProductId, startDate, endDate);
    }

    @Override
    public CommonResult<DistributorProductRespDTO> getDistributorProductByProductAndDistributor(Long productId, Long distributorId) {
        return distributorProductService.getDistributorProductByProductAndDistributor(productId, distributorId);
    }

    @Override
    public CommonResult<List<DistributorProductRespDTO>> getProductListPageByDistributorId(Long distributorId, int pageIndex, int pageSize) {
        return distributorProductService.getProductListPageByDistributorId(distributorId, pageIndex, pageSize);
    }
    @Override
    @TenantIgnore
    public CommonResult<DistributorRespDTO> getDistributorByAccountTenantId(Long accountTenantId) {
        return  distributorService.getDistributorByAccountTenantId(accountTenantId);
    }
    @Override
    public CommonResult<?> distributorSelfRecharge(DistributorSelfRechargeReqDTO payNotifyReqDTO) {
        Long id = distributorSelfRechargeService.distributorSelfRecharge(payNotifyReqDTO.getOrderId(), payNotifyReqDTO.getPayOrderId(), payNotifyReqDTO.getPayStatus());
        return CommonResult.success(id);
    }
    @Override
    public CommonResult<DistributorRespDTO> getDistributorByLoginUser(String loginName) {
        DistributorDO distributor = distributorService.getDistributorByLoginUser(loginName);
        return CommonResult.success(BeanUtils.toBean(distributor, DistributorRespDTO.class));
    }

    @Override
    public List<DistributorRespDTO> getDistributorList(Integer type) {
        return BeanUtils.toBean(distributorService.selectListByType(type), DistributorRespDTO.class);
    }

}
