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

import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.multishop.vo.ShopDetailVO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.multishop.constant.RenovationType;
import com.tmerclub.cloud.multishop.dto.ShopDetailDTO;
import com.tmerclub.cloud.multishop.dto.ShopSearchDTO;
import com.tmerclub.cloud.multishop.model.ShopRenovation;
import com.tmerclub.cloud.multishop.service.ShopDetailService;
import com.tmerclub.cloud.multishop.service.ShopRenovationService;
import com.tmerclub.cloud.multishop.vo.ShopDetailAppVO;
import com.tmerclub.cloud.multishop.vo.ShopHeadInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 店铺详情
 *
 * @author tmerclub
 * @date 2020-12-05 15:50:25
 */
@RestController("appShopDetailController")
@RequestMapping("/ua/shopDetail")
@Tag(name = "app-店铺详情")
public class ShopDetailController {

    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private ShopRenovationService shopRenovationService;

    @GetMapping("/page")
    @Operation(summary = "搜索店铺列表")
    public ServerResponseEntity<PageVO<ShopDetailAppVO>> page(@Valid PageDTO pageDTO, ShopDetailDTO shopDetailDTO) {
        return ServerResponseEntity.success(shopDetailService.shopSearchPage(pageDTO, shopDetailDTO));
    }

    @GetMapping("/getShopHeadInfo")
    @Operation(summary = "店铺头部信息", description = "获取的店铺头部信息")
    public ServerResponseEntity<ShopHeadInfoVO> getShopHeadInfo(Long shopId) {
        ShopHeadInfoVO shopHeadInfoDto = new ShopHeadInfoVO();
        ShopDetailVO shopDetail = shopDetailService.getShoExtensionsByShopId(shopId);
        if (Objects.isNull(shopDetail)) {
            return ServerResponseEntity.showFailMsg("店铺不存在！");
        }
        shopHeadInfoDto.setShopStatus(shopDetail.getShopStatus());
        shopHeadInfoDto.setContractStartTime(shopDetail.getContractStartTime());
        shopHeadInfoDto.setContractEndTime(shopDetail.getContractEndTime());
        if (!Objects.equals(shopDetail.getShopStatus(), 1)) {
            return ServerResponseEntity.success(shopHeadInfoDto);
        }
        ShopRenovation shopHomeRenovation = shopRenovationService.getShopHomeRenovation(shopId, RenovationType.H5.value());
        shopHeadInfoDto.setRenovationId(Objects.nonNull(shopHomeRenovation) ? shopHomeRenovation.getRenovationId() : null);
        shopHeadInfoDto.setShopId(shopId);
        shopHeadInfoDto.setType(shopDetail.getType());
        shopHeadInfoDto.setIntro(shopDetail.getIntro());
        shopHeadInfoDto.setShopLogo(shopDetail.getShopLogo());
        shopHeadInfoDto.setShopName(shopDetail.getShopName());
        shopHeadInfoDto.setIsPreferred(shopDetail.getIsPreferred());
        shopHeadInfoDto.setCollectionNum(shopDetail.getCollectionNum());
        shopHeadInfoDto.setPcBackgroundPic(shopDetail.getPcBackgroundPic());
        shopHeadInfoDto.setMobileBackgroundPic(shopDetail.getMobileBackgroundPic());
        return ServerResponseEntity.success(shopHeadInfoDto);
    }

    @GetMapping("/queryShops")
    @Operation(summary = "app搜索店铺", description = "根据店铺名称搜索店铺")
    public ServerResponseEntity<PageVO<ShopHeadInfoVO>> queryShops(PageDTO pageDTO, ShopSearchDTO shopSearchDTO) {
        PageVO<ShopHeadInfoVO> shopPage = shopDetailService.renovationShopPage(pageDTO, shopSearchDTO, SysTypeEnum.ORDINARY.value());
        return ServerResponseEntity.success(shopPage);
    }

    @GetMapping("/getRenovationShopList")
    @Operation(summary = "获取装修店铺列表信息", description = "获取装修店铺列表信息")
    public ServerResponseEntity<List<ShopHeadInfoVO>> getRenovationShopList(ShopSearchDTO shopSearchDTO) {
        List<ShopHeadInfoVO> shopList = shopDetailService.listRenovationShop(shopSearchDTO);
        return ServerResponseEntity.success(shopList);
    }
}
