
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.exception.LuckException;
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.TemplateType;
import com.tmerclub.cloud.multishop.dto.ShopTemplateDTO;
import com.tmerclub.cloud.multishop.model.ShopTemplate;
import com.tmerclub.cloud.multishop.service.ShopTemplateService;
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.Date;
import java.util.Objects;

/**
 * 商品装修模板表
 * @author Orange
 */
@RestController("multishopShopTemplateController")
@RequestMapping("/mp/shopTemplate")
@Tag(name = "店铺装修模板")
public class ShopTemplateController {

    @Autowired
    private ShopTemplateService shopTemplateService;


    @GetMapping("/page")
    @Operation(summary = "根据类型获取商品装修模板表列表", description = "根据类型分页获取商品装修模板表列表")
    public ServerResponseEntity<PageVO<ShopTemplate>> page(@Valid PageDTO pageDTO, ShopTemplateDTO shopTemplateDTO) {
        shopTemplateDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<ShopTemplate> shopTemplatePage = shopTemplateService.page(pageDTO, BeanUtil.map(shopTemplateDTO, ShopTemplate.class));
        return ServerResponseEntity.success(shopTemplatePage);
    }

    @GetMapping("/pageH5")
    @Operation(summary = "根据类型获取商品装修模板表列表", description = "根据类型分页获取商品装修模板表列表")
    public ServerResponseEntity<PageVO<ShopTemplate>> pageH5(@Valid PageDTO pageDTO, ShopTemplateDTO shopTemplateDTO) {
        shopTemplateDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<ShopTemplate> shopTemplatePage = shopTemplateService.page(pageDTO, BeanUtil.map(shopTemplateDTO, ShopTemplate.class));
        return ServerResponseEntity.success(shopTemplatePage);
    }

    @GetMapping
    @Operation(summary = "获取商品装修模板表", description = "根据templateId获取商品装修模板表")
    public ServerResponseEntity<ShopTemplate> getByTemplateId(@RequestParam Long templateId) {
        return ServerResponseEntity.success(shopTemplateService.getByTemplateId(templateId));
    }

    @GetMapping("/getH5")
    @Operation(summary = "获取商品装修模板表", description = "根据templateId获取商品装修模板表")
    public ServerResponseEntity<ShopTemplate> getH5ByTemplateId(@RequestParam Long templateId) {
        return ServerResponseEntity.success(shopTemplateService.getByTemplateId(templateId));
    }

    @PostMapping("/savePc")
    @Operation(summary = "保存商品pc装修模板表", description = "保存商品pc装修模板表")
    public ServerResponseEntity<Long> save(@Valid @RequestBody ShopTemplateDTO shopTemplateDTO) {
        ShopTemplate shopTemplate = BeanUtil.map(shopTemplateDTO, ShopTemplate.class);
        shopTemplate.setShopId(AuthUserContext.get().getTenantId());
        shopTemplate.setType(TemplateType.PC.value());
        shopTemplate.setTemplateId(null);
        shopTemplateService.save(shopTemplate);
        return ServerResponseEntity.success(shopTemplate.getTemplateId());
    }

    @PostMapping("/saveH5")
    @Operation(summary = "保存商品h5装修模板表", description = "保存商品h5装修模板表")
    public ServerResponseEntity<Long> saveH5(@Valid @RequestBody ShopTemplateDTO shopTemplateDTO) {
        ShopTemplate shopTemplate = BeanUtil.map(shopTemplateDTO, ShopTemplate.class);
        shopTemplate.setShopId(AuthUserContext.get().getTenantId());
        shopTemplate.setType(TemplateType.H5.value());
        shopTemplate.setTemplateId(null);
        shopTemplateService.save(shopTemplate);
        return ServerResponseEntity.success(shopTemplate.getTemplateId());
    }

    @PostMapping("/copyPc")
    @Operation(summary = "PC端复制模板信息", description = "PC端复制模板信息")
    public ServerResponseEntity<Long> copy(@RequestParam Long templateId) {
        ShopTemplate shopTemplate = copyTemplate(templateId);
        return ServerResponseEntity.success(shopTemplate.getTemplateId());
    }

    @PostMapping("/copyH5")
    @Operation(summary = "移动端复制模板信息", description = "移动端复制模板信息")
    public ServerResponseEntity<Long> copyH5(@RequestParam Long templateId) {
        ShopTemplate shopTemplate = copyTemplate(templateId);
        return ServerResponseEntity.success(shopTemplate.getTemplateId());
    }

    private ShopTemplate copyTemplate(Long templateId) {
        ShopTemplate shopTemplate = shopTemplateService.getByTemplateId(templateId);
        if (Objects.isNull(shopTemplate)) {
            throw new LuckException("模板不存在，请刷新页面重试");
        }
        if (!Objects.equals(shopTemplate.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopTemplate.setTemplateId(null);
        shopTemplate.setName(shopTemplate.getName() + "副本");
        shopTemplate.setCreateTime(new Date());
        shopTemplate.setUpdateTime(new Date());
        shopTemplateService.save(shopTemplate);
        return shopTemplate;
    }
}
