package com.hzs.activity.tourism.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.activity.base.service.IAcBaseConfigService;
import com.hzs.activity.tourism.mapper.AcTourismDataMapper;
import com.hzs.activity.tourism.param.TourismDataParam;
import com.hzs.activity.tourism.service.IAcTourismDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.activity.tourism.service.IAcTourismExistProductService;
import com.hzs.activity.tourism.vo.TourismDataVo;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.enums.EActType;
import com.hzs.common.domain.activity.base.AcBaseConfig;
import com.hzs.common.domain.activity.tourism.AcTourismData;
import com.hzs.common.domain.activity.tourism.AcTourismExistProduct;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 旅游活动数据表 服务实现类
 *
 * @author hzs
 * @since 2024-02-22
 */
@Service
public class AcTourismDataServiceImpl extends ServiceImpl<AcTourismDataMapper, AcTourismData> implements IAcTourismDataService {

    @Autowired
    private IAcBaseConfigService iAcBaseConfigService;
    @Autowired
    private IAcTourismExistProductService iAcTourismExistProductService;

    @DubboReference
    ICurrencyServiceApi iCurrencyServiceApi;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void handleTourismData() {
        // 所属国家
        Integer pkCountry = CountryConstants.CHINA_COUNTRY;
        // 人民币汇率
        CurrencyDTO currencyDTO = iCurrencyServiceApi.getCurrency(pkCountry).getData();
        // 排除产品编码列表
        List<String> existProductCodeList = new ArrayList<>();
        // 查询排除产品列表
        LambdaQueryWrapper<AcTourismExistProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(AcTourismExistProduct::getProductCode);
        List<AcTourismExistProduct> existProductList = iAcTourismExistProductService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(existProductList)) {
            existProductCodeList = existProductList.stream().map(AcTourismExistProduct::getProductCode).collect(Collectors.toList());
        }

        // 查询进行中的活动（结合重算，防止7天之内订单变化，活动结束时间延后7天）
        List<AcBaseConfig> baseConfigList = iAcBaseConfigService.queryBaseConfigSpecial(EActType.TOURISM.getValue(), CountryConstants.CHINA_COUNTRY);
        if (CollectionUtil.isNotEmpty(baseConfigList)) {
            for (AcBaseConfig acBaseConfig : baseConfigList) {
                // 旅游活动更新积分
                baseMapper.handleTourismData(acBaseConfig.getPkId(), acBaseConfig.getActStartDate(), acBaseConfig.getActEndDate(), existProductCodeList, currencyDTO.getInExchangeRate(), pkCountry);
            }
        }
    }

    /**
     * 查询
     *
     * @param tourismDataParam
     * @return
     */
    @Override
    public List<TourismDataVo> queryList(TourismDataParam tourismDataParam) {
        return baseMapper.queryList(tourismDataParam);
    }

}
