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

import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
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.common.util.BeanUtil;
import com.tmerclub.cloud.multishop.constant.RenovationType;
import com.tmerclub.cloud.multishop.dto.ShopRenovationDTO;
import com.tmerclub.cloud.multishop.model.ShopRenovation;
import com.tmerclub.cloud.multishop.service.ShopRenovationService;
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.*;

import java.util.Objects;

/**
 * 店铺装修信息
 * @author Orange
 */
@RestController("adminShopRenovationController")
@RequestMapping("/mp/shopRenovation")
@Tag(name = "店铺装修信息")
public class ShopRenovationController {

    @Autowired
    private ShopRenovationService shopRenovationService;


    @GetMapping("/page")
    @Operation(summary = "根据类型分页获取店铺装修信息列表", description = "分页获取店铺装修信息列表")
    public ServerResponseEntity<PageVO<ShopRenovation>> page(@Valid PageDTO pageDTO, ShopRenovationDTO shopRenovation) {
        shopRenovation.setShopId(AuthUserContext.get().getTenantId());
        PageVO<ShopRenovation> shopRenovationPage = shopRenovationService.page(pageDTO, BeanUtil.map(shopRenovation, ShopRenovation.class));
        return ServerResponseEntity.success(shopRenovationPage);
    }

    @GetMapping("/pageH5")
    @Operation(summary = "根据类型分页获取店铺装修信息列表", description = "分页获取店铺装修信息列表")
    public ServerResponseEntity<PageVO<ShopRenovation>> pageH5(@Valid PageDTO pageDTO, ShopRenovationDTO shopRenovation) {
        shopRenovation.setShopId(AuthUserContext.get().getTenantId());
        PageVO<ShopRenovation> shopRenovationPage = shopRenovationService.page(pageDTO, BeanUtil.map(shopRenovation, ShopRenovation.class));
        return ServerResponseEntity.success(shopRenovationPage);
    }

    @GetMapping
    @Operation(summary = "获取店铺装修信息", description = "根据renovationId获取店铺装修信息")
    public ServerResponseEntity<ShopRenovation> getByRenovationId(@RequestParam Long renovationId) {
        ShopRenovation shopRenovation = shopRenovationService.getByRenovationId(renovationId);
        if (!Objects.equals(shopRenovation.getShopId(), AuthUserContext.get().getTenantId())) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        return ServerResponseEntity.success(shopRenovation);
    }

    @GetMapping("/getH5")
    @Operation(summary = "获取店铺装修信息", description = "根据renovationId获取店铺装修信息")
    public ServerResponseEntity<ShopRenovation> getH5ByRenovationId(@RequestParam Long renovationId) {
        ShopRenovation shopRenovation = shopRenovationService.getByRenovationId(renovationId);
        if (!Objects.equals(shopRenovation.getShopId(), AuthUserContext.get().getTenantId())) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        return ServerResponseEntity.success(shopRenovation);
    }

    @PostMapping("/savePc")
    @Operation(summary = "保存店铺pc端装修信息", description = "保存店铺装修信息")
    public ServerResponseEntity<Long> save(@Valid @RequestBody ShopRenovationDTO shopRenovationDTO) {
        ShopRenovation shopRenovation = BeanUtil.map(shopRenovationDTO, ShopRenovation.class);
        shopRenovation.setShopId(AuthUserContext.get().getTenantId());
        shopRenovation.setRenovationId(null);
        shopRenovation.setRenovationType(RenovationType.PC.value());
        shopRenovationService.save(shopRenovation);
        return ServerResponseEntity.success(shopRenovation.getRenovationId());
    }

    @PostMapping("/saveH5")
    @Operation(summary = "保存店铺移动端装修信息", description = "保存店铺装修信息")
    public ServerResponseEntity<Long> saveH5(@Valid @RequestBody ShopRenovationDTO shopRenovationDTO) {
        ShopRenovation shopRenovation = BeanUtil.map(shopRenovationDTO, ShopRenovation.class);
        shopRenovation.setShopId(AuthUserContext.get().getTenantId());
        shopRenovation.setRenovationId(null);
        shopRenovation.setRenovationType(RenovationType.H5.value());
        shopRenovationService.save(shopRenovation);
        return ServerResponseEntity.success(shopRenovation.getRenovationId());
    }
}
