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

import com.tmerclub.cloud.api.product.feign.SpuFeignClient;
import com.tmerclub.cloud.common.constant.StatusEnum;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
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.BeanUtil;
import com.tmerclub.cloud.multishop.dto.IndexImgDTO;
import com.tmerclub.cloud.multishop.model.IndexImg;
import com.tmerclub.cloud.multishop.service.IndexImgService;
import com.tmerclub.cloud.multishop.vo.IndexImgVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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.Objects;

/**
 * 轮播图
 *
 * @author YXF
 * @date 2020-11-24 16:38:32
 */
@RestController("adminIndexImgController")
@RequestMapping("/mp/indexPic")
@Tag(name = "admin-轮播图")
public class IndexImgController {

    @Autowired
    private IndexImgService indexImgService;
    @DubboReference
    private SpuFeignClient spuFeignClient;

    @GetMapping("/page")
    @Operation(summary = "获取轮播图列表", description = "分页获取轮播图列表")
    public ServerResponseEntity<PageVO<IndexImgVO>> page(@Valid PageDTO pageDTO, IndexImgDTO indexImgDTO) {
        indexImgDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<IndexImgVO> indexImgPage = indexImgService.page(pageDTO, indexImgDTO);
        return ServerResponseEntity.success(indexImgPage);
    }

    @GetMapping
    @Operation(summary = "获取轮播图", description = "根据imgId获取轮播图")
    public ServerResponseEntity<IndexImgVO> getByImgId(@RequestParam Long imgId) {
        IndexImgVO indexImg = indexImgService.getByImgId(imgId);
        if (Objects.nonNull(indexImg.getSpuId())) {
            // 已被删除的商品不显示
            ServerResponseEntity<SpuVO> spuResponse = spuFeignClient.getById(indexImg.getSpuId());
            SpuVO spuVO;
            if (spuResponse.isSuccess()
                    && (spuVO = spuResponse.getData()) != null
                    && spuVO.getStatus() != -1) {
                indexImg.setSpu(spuVO);
            } else {
                indexImg.setSpuId(null);
            }
        }
        return ServerResponseEntity.success(indexImg);
    }

    @PostMapping
    @Operation(summary = "保存轮播图", description = "保存轮播图")
    public ServerResponseEntity<Void> save(@Valid @RequestBody IndexImgDTO indexImgDTO) {
        IndexImg indexImg = BeanUtil.map(indexImgDTO, IndexImg.class);
        indexImg.setImgId(null);
        indexImg.setShopId(AuthUserContext.get().getTenantId());
        if (Objects.isNull(indexImgDTO.getStatus())) {
            indexImg.setStatus(StatusEnum.ENABLE.value());
        } else {
            indexImg.setStatus(indexImgDTO.getStatus());
        }
        indexImgService.save(indexImg);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新轮播图", description = "更新轮播图")
    public ServerResponseEntity<Void> update(@Valid @RequestBody IndexImgDTO indexImgDTO) {
        IndexImg indexImg = BeanUtil.map(indexImgDTO, IndexImg.class);
        indexImg.setShopId(AuthUserContext.get().getTenantId());
        indexImgService.update(indexImg);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除轮播图", description = "根据轮播图id删除轮播图")
    public ServerResponseEntity<Void> delete(@RequestParam Long imgId) {
        Integer imgType = indexImgService.getByImgId(imgId).getImgType();
        indexImgService.deleteById(imgId, AuthUserContext.get().getTenantId(), imgType);
        return ServerResponseEntity.success();
    }
}
