
package com.tmerclub.cloud.platform.feign;

import com.tmerclub.cloud.api.platform.constant.OfflineHandleEventStatus;
import com.tmerclub.cloud.api.platform.dto.OfflineHandleEventDTO;
import com.tmerclub.cloud.api.platform.feign.OfflineHandleEventFeignClient;
import com.tmerclub.cloud.api.platform.vo.OfflineHandleEventVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.platform.model.OfflineHandleEvent;
import com.tmerclub.cloud.platform.service.OfflineHandleEventService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author tmerclub
 * @date 2020/12/30
 */
@DubboService
public class OfflineHandleEventFeignController implements OfflineHandleEventFeignClient {

    @Autowired
    private OfflineHandleEventService offlineHandleEventService;

    @Override
    public ServerResponseEntity<Void> save(OfflineHandleEventDTO offlineHandleEventDto) {
        int count = offlineHandleEventService.offlineCount(offlineHandleEventDto.getHandleType(), offlineHandleEventDto.getHandleId(), offlineHandleEventDto.getSysType());
        if (count > 0) {
            throw new LuckException("该活动状态异常不能下线，请刷新页面！");
        }
        OfflineHandleEvent offlineHandleEvent = BeanUtil.map(offlineHandleEventDto, OfflineHandleEvent.class);
        offlineHandleEventService.save(offlineHandleEvent);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<OfflineHandleEventVO> getProcessingEventByHandleTypeAndHandleId(Integer handleType, Long handleId, Integer sysType) {
        OfflineHandleEventVO offlineHandleEventVO = offlineHandleEventService.getProcessingEventByHandleTypeAndHandleId(handleType, handleId, sysType);
        return ServerResponseEntity.success(offlineHandleEventVO);
    }

    @Override
    public ServerResponseEntity<Void> auditOfflineEvent(OfflineHandleEventDTO offlineHandleEventDto) {
        offlineHandleEventService.auditOfflineEvent(offlineHandleEventDto);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> updateToApply(OfflineHandleEventDTO offlineHandleEventDto) {
        offlineHandleEventService.updateToApply(offlineHandleEventDto.getEventId(), offlineHandleEventDto.getReapplyReason());
        return ServerResponseEntity.success();
    }

    @Override
    public void update(OfflineHandleEventDTO offlineHandleEventDto) {
        offlineHandleEventService.update(offlineHandleEventDto);
    }

    @Override
    public ServerResponseEntity<Void> deleteByHandleTypeAndHandleId(Integer handleType, Long handleId) {
        offlineHandleEventService.deleteByHandleTypeAndHandleId(handleType, handleId);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> batchSave(List<OfflineHandleEventDTO> offlineHandleEventList) {
        for (OfflineHandleEventDTO offlineHandleEvent : offlineHandleEventList) {
            offlineHandleEvent.setShopId(AuthUserContext.get().getTenantId());
            offlineHandleEvent.setHandlerId(AuthUserContext.get().getUserId());
            offlineHandleEvent.setStatus(OfflineHandleEventStatus.OFFLINE_BY_PLATFORM.getValue());
        }
        offlineHandleEventService.batchSave(offlineHandleEventList);
        return ServerResponseEntity.success();
    }
}
