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

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.feign.SearchSpuFeignClient;
import com.tmerclub.cloud.api.multishop.bo.EsShopDetailBO;
import com.tmerclub.cloud.api.multishop.constant.ShopStatus;
import com.tmerclub.cloud.api.multishop.feign.ShopDetailFeignClient;
import com.tmerclub.cloud.api.platform.dto.OfflineHandleEventDTO;
import com.tmerclub.cloud.api.platform.vo.OfflineHandleEventVO;
import com.tmerclub.cloud.api.product.feign.SpuFeignClient;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.product.vo.search.SpuSearchVO;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.discount.constant.DiscountStatusEnum;
import com.tmerclub.cloud.discount.dto.DiscountDTO;
import com.tmerclub.cloud.discount.service.DiscountService;
import com.tmerclub.cloud.discount.service.DiscountSpuService;
import com.tmerclub.cloud.discount.vo.DiscountVO;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * @author lhd
 * @date 2020-12-17 15:10:00
 */
@RestController("adminDiscountController")
@RequestMapping("/mp/discount")
public class DiscountController {

    @Autowired
    private DiscountService discountService;
    @DubboReference
    private SearchSpuFeignClient searchSpuFeignClient;
    @DubboReference
    private SpuFeignClient spuFeignClient;
    @Autowired
    private DiscountSpuService discountSpuService;
    @DubboReference
    private ShopDetailFeignClient shopDetailFeignClient;

    @GetMapping("/page")
    @Operation(summary = "获取满减活动列表", description = "获取满减活动列表")
    public ServerResponseEntity<PageVO<DiscountVO>> page(@Valid PageDTO pageDTO, DiscountDTO transportDTO) {
        Long tenantId = AuthUserContext.get().getTenantId();
        transportDTO.setShopId(tenantId);
        PageVO<DiscountVO> transports = discountService.page(pageDTO, transportDTO);
        return ServerResponseEntity.success(transports);
    }

    @GetMapping("/platformPage")
    @Operation(summary = "平台满减活动管理列表", description = "平台满减活动管理列表")
    public ServerResponseEntity<PageVO<DiscountVO>> platformPage(@Valid PageDTO pageDTO, DiscountDTO transportDTO) {
        PageVO<DiscountVO> transports = discountService.platformPage(pageDTO, transportDTO);
        return ServerResponseEntity.success(transports);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{discountId}")
    public ServerResponseEntity<DiscountVO> info(@PathVariable("discountId") Long discountId) {
        Long shopId = AuthUserContext.get().getTenantId();
        DiscountVO discount = discountService.getDiscountAndSpu(discountId);
        if (Objects.isNull(discount)) {
            throw new LuckException("该活动不存在或者已删除");
        } else if (!Objects.equals(shopId, discount.getShopId()) && !Objects.equals(Constant.PLATFORM_SHOP_ID, shopId)) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        // 指定商品
        if (CollUtil.isNotEmpty(discount.getSpuIds())) {
            ServerResponseEntity<List<SpuSearchVO>> spuResponse = searchSpuFeignClient.listSpuBySpuIds(discount.getSpuIds());
            discount.setSpuList(spuResponse.getData());
        }
        return ServerResponseEntity.success(discount);
    }

    /**
     * 保存
     */
    @PostMapping
    public ServerResponseEntity<String> save(@RequestBody @Valid DiscountDTO discountDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        discountDTO.setShopId(shopId);
        discountService.insertDiscountAndItemAndSpu(discountDTO);
        discountService.removeDiscountCache(discountDTO.getDiscountId(), shopId);
        spuFeignClient.removeSpuActivityCache(discountDTO.getShopId(), discountDTO.getSpuIds());
        return ServerResponseEntity.success();
    }


    /**
     * 修改
     */
    @PutMapping
    public ServerResponseEntity<Void> update(@RequestBody @Valid DiscountDTO discountDTO) {
        DiscountVO discountDb = discountService.getDiscountAndSpu(discountDTO.getDiscountId());
        Long shopId = AuthUserContext.get().getTenantId();
        if (!Objects.equals(discountDb.getShopId(), shopId)) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        ServerResponseEntity<EsShopDetailBO> shopResponEntity = shopDetailFeignClient.getShopByShopId(shopId);
        EsShopDetailBO shopDetail = shopResponEntity.getData();
        if (!Objects.equals(shopDetail.getShopStatus(), ShopStatus.OPEN.value())) {
            throw new LuckException("店铺状态异常，无法开启满减活动");
        }
        List<Long> spuIds = discountSpuService.listSpuIdByDiscountId(discountDb.getDiscountId());
        if (discountDb.getStatus() > DiscountStatusEnum.RUN.getValue() && discountDb.getStatus() < DiscountStatusEnum.OFFLINE.getValue()) {
            // 该活动已被平台下线，不能再更改状态
            throw new LuckException("该活动已被平台下线，不能再更改状态");
        }
        discountService.updateDiscountAndItemAndSpu(discountDTO);
        discountService.removeDiscountCache(discountDb.getDiscountId(), discountDb.getShopId());

        // 适用商品类型发生改变，清除店铺所有商品的缓存（全部商品转指定商品， 指定商品转全部商品）
        if (!Objects.equals(discountDb.getSuitableSpuType(), discountDTO.getSuitableSpuType())) {
            spuIds.clear();
        }
        if (CollUtil.isNotEmpty(spuIds)) {
            CollUtil.addAllIfNotContains(spuIds, discountDTO.getSpuIds());
        }
        spuFeignClient.removeSpuActivityCache(discountDTO.getShopId(), spuIds);
        return ServerResponseEntity.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{discountId}")
    public ServerResponseEntity<Void> delete(@PathVariable Long discountId) {
        DiscountVO discount = discountService.getDiscountAndSpu(discountId);
        discountService.deleteDiscountsAndItemsAndSpuList(discountId, discount.getShopId());
        discountService.removeDiscountCache(discountId, discount.getShopId());
        spuFeignClient.removeSpuActivityCache(discount.getShopId(), discount.getSpuIds());
        return ServerResponseEntity.success();
    }


    @PostMapping("/offline")
    @Operation(summary = "下线活动", description = "下线活动")
    public ServerResponseEntity<Void> offline(@RequestBody OfflineHandleEventDTO offlineHandleEventDto) {
        if (!Objects.equals(Constant.PLATFORM_SHOP_ID, AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        DiscountVO discount = discountService.getDiscountAndSpu(offlineHandleEventDto.getHandleId());
        if (Objects.isNull(discount)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        }
        discountService.offline(offlineHandleEventDto);
        // 清除缓存
        discountService.removeDiscountCache(discount.getDiscountId(), discount.getShopId());
        return ServerResponseEntity.success();
    }

    @GetMapping("/getOfflineHandleEvent/{discountId}")
    @Operation(summary = "通过discountId获取最新下线的事件", description = "通过discountId获取最新下线的事件")
    public ServerResponseEntity<OfflineHandleEventVO> getOfflineHandleEvent(@PathVariable Long discountId) {
        return ServerResponseEntity.success(discountService.getOfflineHandleEvent(discountId));
    }

    @PostMapping("/audit")
    @Operation(summary = "审核活动", description = "审核活动")
    public ServerResponseEntity<Void> audit(@RequestBody OfflineHandleEventDTO offlineHandleEventDto) {
        if (!Objects.equals(Constant.PLATFORM_SHOP_ID, AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        DiscountVO discount = discountService.getDiscountAndSpu(offlineHandleEventDto.getHandleId());
        discountService.audit(offlineHandleEventDto);
        // 清除缓存
        discountService.removeDiscountCache(discount.getDiscountId(), discount.getShopId());
        return ServerResponseEntity.success();
    }

    @PostMapping("/auditApply")
    @Operation(summary = "违规活动提交审核", description = "违规活动提交审核")
    public ServerResponseEntity<Void> auditApply(@RequestBody OfflineHandleEventDTO offlineHandleEventDto) {
        discountService.auditApply(offlineHandleEventDto);
        return ServerResponseEntity.success();
    }
}
