
package com.tmerclub.cloud.product.controller.platform;

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.common.constant.Constant;
import com.tmerclub.cloud.common.constant.StatusEnum;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.product.constant.SpuType;
import com.tmerclub.cloud.common.product.constant.SupplierSpuType;
import com.tmerclub.cloud.common.product.dto.ProductSearchDTO;
import com.tmerclub.cloud.common.product.dto.SpuDTO;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.ExcelUtil;
import com.tmerclub.cloud.product.service.SpuExcelService;
import com.tmerclub.cloud.product.service.SpuService;
import com.tmerclub.cloud.product.vo.PlatformSpuExcelVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
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 lth
 * @Date 2021/7/19 16:42
 */
@RestController("platformSpuController")
@RequestMapping("/p/spu")
@Tag(name = "platform-spu信息")
public class SpuController {

    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuExcelService spuExcelService;
    @DubboReference
    private ShopDetailFeignClient shopDetailFeignClient;

    @PutMapping("/to_top")
    @Operation(summary = "通过商品id设置是否置顶", description = "通过商品id设置是否置顶")
    public ServerResponseEntity<Void> toTopBySpuId(@RequestParam("spuId") Long spuId) {
        SpuVO spuVO = spuService.getBySpuId(spuId);
        if (!Objects.equals(spuVO.getStatus(), StatusEnum.ENABLE.value()) && Objects.equals(spuVO.getIsTop(), StatusEnum.DISABLE.value())) {
            throw new LuckException("只能置顶已上架的商品");
        }
        spuService.toTopBySpuId(spuId);
        return ServerResponseEntity.success();
    }

    @PostMapping("/score_save")
    @Operation(summary = "保存积分spu信息", description = "保存积分spu信息")
    public ServerResponseEntity<Long> save(@Valid @RequestBody SpuDTO spuDTO) {
        // 发布商品时，需要根据店铺状态判断是否可以发布商品
        ServerResponseEntity<EsShopDetailBO> shopRequest = shopDetailFeignClient.getShopByShopId(AuthUserContext.get().getTenantId());
        EsShopDetailBO shopDetail = shopRequest.getData();
        if (Objects.equals(shopDetail.getShopStatus(), ShopStatus.OFFLINE.value())) {
            throw new LuckException("店铺处于违规下线状态，不能发布新商品");
        }
        spuDTO.setShopCategoryId(Constant.DEFAULT_ID);
        spuDTO.setSpuType(SpuType.SCORE.value());
        spuDTO.setStatus(StatusEnum.ENABLE.value());
        spuDTO.setSupplierSpuType(SupplierSpuType.NO.value());
        spuDTO.setShopId(0L);
        spuService.save(spuDTO);
        return ServerResponseEntity.success(spuDTO.getSpuId());
    }

    @PutMapping("/auditSpu")
    @Operation(summary = "商品审核", description = "商品审核")
    public ServerResponseEntity<Void> auditSpu(@RequestBody OfflineHandleEventDTO eventDTO) {
        eventDTO.setHandlerId(AuthUserContext.get().getUserId());
        spuService.auditSpu(eventDTO);
        // 移除缓存
        spuService.removeSpuCacheBySpuId(eventDTO.getHandleId());
        return ServerResponseEntity.success();
    }

    @PostMapping("/soldExcel")
    @Operation(summary = "导出excel", description = " 导出商品excel")
    public ServerResponseEntity<Void> spuSoldExcel(HttpServletResponse response, @RequestBody ProductSearchDTO productSearchDTO) {
        List<PlatformSpuExcelVO> list = spuExcelService.excelPlatformSpuList(productSearchDTO);
        ExcelUtil.soleExcel(response, list, PlatformSpuExcelVO.EXCEL_NAME, PlatformSpuExcelVO.MERGE_ROW_INDEX, PlatformSpuExcelVO.MERGE_COLUMN_INDEX, PlatformSpuExcelVO.class);
        return ServerResponseEntity.success();
    }
}
