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

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.common.vo.ShopSubstituteSalesVO;
import com.tmerclub.cloud.multishop.dto.ShopSubstituteSalesDTO;
import com.tmerclub.cloud.multishop.model.ShopSubstituteSales;
import com.tmerclub.cloud.multishop.service.ShopSubstituteSalesService;
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 lhd
 * @date 2022-06-22 17:09:12
 */
@RestController("multishopShopSubstituteSalesController")
@RequestMapping("/m/substituteSales")
@Tag(name = "代销设置表")
public class ShopSubstituteSalesController {

    @Autowired
    private ShopSubstituteSalesService shopSubstituteSalesService;

    @GetMapping
    @Operation(summary = "获取代销设置表", description = "获取代销设置表")
    public ServerResponseEntity<ShopSubstituteSalesVO> getByShopSubstituteSalesId() {
        Long shopId = AuthUserContext.get().getTenantId();
        return ServerResponseEntity.success(shopSubstituteSalesService.getByShopSubstituteSalesId(shopId));
    }

    @PostMapping
    @Operation(summary = "保存代销设置表", description = "保存代销设置表")
    public ServerResponseEntity<Void> save(@Valid @RequestBody ShopSubstituteSalesDTO shopSubstituteSalesDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        shopSubstituteSalesDTO.setShopId(shopId);
        ShopSubstituteSales shopSubstituteSales = BeanUtil.map(shopSubstituteSalesDTO, ShopSubstituteSales.class);
        //获取代销设置
        ShopSubstituteSalesVO shopSubstituteSalesId = shopSubstituteSalesService.getByShopSubstituteSalesId(shopId);
        if (Objects.isNull(shopSubstituteSalesId)) {
            shopSubstituteSales.setShopSubstituteSalesId(null);
            shopSubstituteSalesService.save(shopSubstituteSales);
        } else {
            shopSubstituteSalesService.update(shopSubstituteSales);
        }
        return ServerResponseEntity.success();
    }
}
