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

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.product.vo.GiveawayVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.product.dto.GiveawayDTO;
import com.tmerclub.cloud.product.service.GiveawayService;
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 tmerclub
 * @date 2022-08-16 11:11:58
 */
@RestController("multishopGiveawayController")
@RequestMapping("/m/giveaway")
@Tag(name = "赠品表")
public class GiveawayController {

    @Autowired
    private GiveawayService giveawayService;

    @GetMapping("/page")
    @Operation(summary = "获取赠品表列表", description = "分页获取赠品表列表")
    public ServerResponseEntity<PageVO<GiveawayVO>> page(@Valid PageDTO pageDTO, GiveawayDTO giveawayDTO) {
        giveawayDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<GiveawayVO> giveawayPage = giveawayService.listByShopId(pageDTO, giveawayDTO);
        return ServerResponseEntity.success(giveawayPage);
    }

    @GetMapping
    @Operation(summary = "获取赠品表", description = "根据giveawayId获取赠品表")
    public ServerResponseEntity<GiveawayVO> getByGiveawayId(@RequestParam Long giveawayId) {
        Long shopId = AuthUserContext.get().getTenantId();
        GiveawayVO giveawayVO = giveawayService.getByGiveawayId(giveawayId);
        if (!Objects.equals(shopId, giveawayVO.getShopId())) {
            throw new LuckException("该赠品信息不属于你的店铺");
        }
        return ServerResponseEntity.success(giveawayVO);
    }

    @PostMapping
    @Operation(summary = "保存赠品表", description = "保存赠品表")
    public ServerResponseEntity<Void> save(@Valid @RequestBody GiveawayDTO giveawayDTO) {
        giveawayDTO.setShopId(AuthUserContext.get().getTenantId());
        giveawayDTO.setGiveawayId(null);
        giveawayService.save(giveawayDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新赠品表", description = "更新赠品表")
    public ServerResponseEntity<Void> update(@Valid @RequestBody GiveawayDTO giveawayDTO) {
        giveawayDTO.setShopId(AuthUserContext.get().getTenantId());
        giveawayService.update(giveawayDTO);
        return ServerResponseEntity.success();
    }


    @PutMapping("/change_status")
    @Operation(summary = "修改赠品状态", description = "修改赠品状态")
    public ServerResponseEntity<Void> changeStatus(@RequestParam("status") Integer status, @RequestParam("giveawayId") Long giveawayId) {
        giveawayService.changeStatus(giveawayId, status);
        return ServerResponseEntity.success();
    }


}
