package cn.freemud.demo.manager.promotion;

import cn.freemud.demo.adapter.manager.PromotionBO2DTOAdapter;
import cn.freemud.demo.adapter.manager.PromotionDTO2BOAdapter;
import cn.freemud.demo.entities.bo.goods.CalculationDiscountBO;
import cn.freemud.demo.entities.bo.goods.QueryActivityByTypeDTO;
import cn.freemud.demo.entities.bo.promotion.ActivityBO;
import cn.freemud.demo.entities.bo.promotion.GetActivityBO;
import cn.freemud.demo.entities.bo.promotion.GetCalculationDiscountBO;
import cn.freemud.entities.dto.ActivityResponseDto;
import cn.freemud.entities.dto.activity.ActivityQueryResponseDto;
import cn.freemud.entities.dto.calculate.ActivityCalculationDiscountRequestDto;
import cn.freemud.entities.dto.calculate.ActivityCalculationDiscountResponseDto;
import cn.freemud.service.ActivityService;
import cn.freemud.service.thirdparty.ActivityClient;
//import cn.freemud.service.thirdparty.CalculationClient;
import com.alibaba.fastjson.JSON;
import com.freemud.application.sdk.api.log.ApiLog;
import com.freemud.application.sdk.api.log.ErrorLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("KgdPromotionService")
public class KgdPromotionServiceImpl implements PromotionService{

//    @Autowired
//    private CalculationClient calculationClient;

    @Autowired
    private ActivityClient activityClient;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private PromotionBO2DTOAdapter promotionBO2DTOAdapter;

    @Autowired
    private PromotionDTO2BOAdapter promotionDTO2BOAdapter;

    @Override
    public CalculationDiscountBO calculationDiscount(GetCalculationDiscountBO getCalculationDiscountBO) {

        ActivityCalculationDiscountRequestDto shareDiscountRequestDto = promotionBO2DTOAdapter.convert2CalculationSharingDiscountRequestDto(getCalculationDiscountBO);
        ActivityCalculationDiscountResponseDto calculationSharingDiscountResponseDto = activityClient.calculationSharingDiscount(shareDiscountRequestDto);
//        try {
//            calculationSharingDiscountResponseDto = activityClient.calculationSharingDiscount(shareDiscountRequestDto);
//        }
//        catch (Exception e) {
//            ErrorLog.errorConvertJson(this.getClass(), JsonUtil.toJSONNoFeatures(calculationSharingDiscountResponseDto), e);
//            throw new ServiceException(ResponseResult.OPERATE_TOO_OFTEN);
//        }

        CalculationDiscountBO calculationDiscountBO = promotionDTO2BOAdapter.convert2CalculationDiscountBO(calculationSharingDiscountResponseDto);

        return calculationDiscountBO;
    }

    @Override
    public ActivityBO queryActivityByType(GetActivityBO getActivityBO) {

        QueryActivityByTypeDTO queryActivityByTypeDTO = promotionBO2DTOAdapter.convert2QueryActivityByTypeDTO(getActivityBO);

        ActivityQueryResponseDto activityQueryResponseDto = activityService.queryActivityByType(queryActivityByTypeDTO.getActivityQueryDto(),
                queryActivityByTypeDTO.getActivityTypeEnums(), queryActivityByTypeDTO.getGoodsBeanDtoList());
        if (activityQueryResponseDto != null && activityQueryResponseDto.getResult()!=null && activityQueryResponseDto.getResult().size()>0) {
            ActivityResponseDto activityResponseDto = activityQueryResponseDto.getResult().get(0);
            ActivityBO activityBO = promotionDTO2BOAdapter.convert2ActivityBO(activityResponseDto);
            return activityBO;
        }
        return null;
    }


}
