
package com.tmerclub.cloud.multishop.controller.admin;

import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.multishop.vo.ShopDetailVO;
import com.tmerclub.cloud.api.platform.constant.OfflineHandleEventStatus;
import com.tmerclub.cloud.api.platform.constant.OfflineHandleEventType;
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.constant.StatusEnum;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.multishop.service.ShopDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


/**
 * 下线处理事件
 *
 * @author YXF
 * @date 2021-01-15 17:46:26
 */
@RestController("shopOfflineHandleEventController")
@RequestMapping("/mp/shopOfflineHandleEvent")
@Tag(name = "admin-下线处理事件")
public class ShopOfflineHandleEventController {

    @DubboReference
    private OfflineHandleEventFeignClient offlineHandleEventFeignClient;
    @Autowired
    private ShopDetailService shopDetailService;


    @PostMapping("/offline")
    @Operation(summary = "下线店铺", description = "下线店铺")
    public ServerResponseEntity<Void> offline(@RequestBody OfflineHandleEventDTO offlineHandleEventDto) {
        ShopDetailVO shopDetail = shopDetailService.getByShopId(offlineHandleEventDto.getHandleId());
        if (shopDetail == null) {
            throw new LuckException("该店铺不存在或者已删除");
        }

        // 添加下线处理记录
        offlineHandleEventDto.setHandleType(OfflineHandleEventType.PROD.getValue());
        offlineHandleEventDto.setHandleId(shopDetail.getShopId());
        offlineHandleEventDto.setSysType(SysTypeEnum.MULTISHOP.value());
        offlineHandleEventFeignClient.save(offlineHandleEventDto);
        // 更新店铺状态为下线
        shopDetailService.changeSpuStatus(shopDetail.getShopId(), StatusEnum.OFFLINE.value());

        // 移除缓存
        shopDetailService.removeCacheByShopId(shopDetail.getShopId());
        return ServerResponseEntity.success();
    }

    @GetMapping("/getOfflineHandleEventByProdId/{prodId}")
    @Operation(summary = "获取最新下线店铺的事件", description = "通过prodId获取最新下线店铺的事件")
    public ServerResponseEntity<OfflineHandleEventVO> getOfflineHandleEventByProdId(@PathVariable Long prodId) {
        ServerResponseEntity<OfflineHandleEventVO> offlineHandleEventResponse =
                offlineHandleEventFeignClient.getProcessingEventByHandleTypeAndHandleId(OfflineHandleEventType.SHOP.getValue(), prodId, null);
        return ServerResponseEntity.success(offlineHandleEventResponse.getData());
    }

    @PostMapping("/shopAudit")
    @Operation(summary = "审核店铺", description = "审核店铺")
    public ServerResponseEntity<Void> shopAudit(@RequestBody OfflineHandleEventDTO offlineHandleEventDto) {
        offlineHandleEventFeignClient.auditOfflineEvent(offlineHandleEventDto);
        Long shopId = offlineHandleEventDto.getHandleId();
        // 审核通过,更新店铺为营业状态
        if (Objects.equals(offlineHandleEventDto.getStatus(), OfflineHandleEventStatus.AGREE_BY_PLATFORM.getValue())) {
            shopDetailService.changeSpuStatus(shopId, StatusEnum.ENABLE.value());
        } else if (Objects.equals(offlineHandleEventDto.getStatus(), OfflineHandleEventStatus.DISAGREE_BY_PLATFORM.getValue())) {
            shopDetailService.changeSpuStatus(shopId, StatusEnum.OFFLINE.value());
        }

        // 移除缓存
        shopDetailService.removeCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }

    @PostMapping("/auditApply")
    @Operation(summary = "违规店铺提交审核", description = "违规店铺提交审核")
    public ServerResponseEntity<Void> auditApply(@RequestBody OfflineHandleEventDTO offlineHandleEventDto) {
        Long shopId = offlineHandleEventDto.getHandleId();
        ShopDetailVO shopDetailVO = shopDetailService.getByShopId(offlineHandleEventDto.getHandleId());
        if (shopDetailVO == null) {
            // 店铺信息不存在
            throw new LuckException("店铺信息不存在");
        }
        offlineHandleEventFeignClient.updateToApply(offlineHandleEventDto);
        // 更新店铺为待审核状态
        shopDetailService.changeSpuStatus(shopId, StatusEnum.WAIT_AUDIT.value());
        return ServerResponseEntity.success();
    }
}
