
package com.tmerclub.cloud.group.feign;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.group.feign.GroupFeignClient;
import com.tmerclub.cloud.common.product.vo.GroupActivitySpuVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.group.constant.GroupActivityStatusEnum;
import com.tmerclub.cloud.group.service.GroupActivityService;
import com.tmerclub.cloud.group.vo.GroupActivityVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author tmerclub
 * @date 2020/12/8
 */
@DubboService
public class GroupFeignController implements GroupFeignClient {

    @Autowired
    private GroupActivityService groupActivityService;


    @Override
    public ServerResponseEntity<Date> getActivityStartTime(Long activityId) {
        GroupActivityVO groupActivity = groupActivityService.getByGroupActivityId(activityId);
        if (!Objects.equals(groupActivity.getStatus(), GroupActivityStatusEnum.ENABLE.value())) {
            return ServerResponseEntity.success();
        }
        Date date = new Date();
        Date startTime = null;
        // 活动结束时间小于等于当前时间，
        if (groupActivity.getEndTime().getTime() <= System.currentTimeMillis()) {
            return ServerResponseEntity.success(null);
        }
        // 预热活动的开始时间设为当前
        if (Objects.equals(groupActivity.getIsPreheat(), 1)) {
            startTime = new Date();
        }
        // 非预热活动的开始时间为活动开始时间
        else if (date.getTime() < groupActivity.getEndTime().getTime()) {
            startTime = groupActivity.getStartTime();
        }
        return ServerResponseEntity.success(startTime);
    }

    @Override
    public ServerResponseEntity<com.tmerclub.cloud.api.group.vo.GroupActivityVO> getActivityInfo(Long activityId) {
        GroupActivityVO groupActivity = groupActivityService.getByGroupActivityId(activityId);
        com.tmerclub.cloud.api.group.vo.GroupActivityVO groupActivityVO = BeanUtil.map(groupActivity, com.tmerclub.cloud.api.group.vo.GroupActivityVO.class);
        return ServerResponseEntity.success(groupActivityVO);
    }

    @Override
    public ServerResponseEntity<List<GroupActivitySpuVO>> groupSpuListBySpuIds(List<Long> spuIds) {
        if (CollUtil.isEmpty(spuIds)) {
            return ServerResponseEntity.success(new ArrayList<>(0));
        }
        List<GroupActivitySpuVO> groupActivitySpuList = groupActivityService.groupSpuListBySpuIds(spuIds);
        return ServerResponseEntity.success(groupActivitySpuList);
    }

    @Override
    public ServerResponseEntity<Void> offlineGroupBySpuIds(List<Long> spuIds) {
        groupActivityService.offlineGroupBySpuIds(spuIds);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> updateGroupActivityPrice(List<Long> skuIds, Long spuId) {
        groupActivityService.updateGroupActivityPrice(skuIds, spuId);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> updateGroupActivityStatus(Long spuId) {
        groupActivityService.updateGroupActivityStatus(spuId);
        return ServerResponseEntity.success();
    }
}
