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

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.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.product.dto.SpuPriceLogDTO;
import com.tmerclub.cloud.product.model.SpuPriceLog;
import com.tmerclub.cloud.product.service.SpuPriceLogService;
import com.tmerclub.cloud.product.vo.SpuPriceLogVO;
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.*;

/**
 * 商品调价记录
 *
 * @author lhd
 * @date 2022-06-29 17:48:25
 */
@RestController("supplierSpuPriceLogController")
@RequestMapping("/s/spu_price_log")
@Tag(name = "supplier商品调价记录")
public class SpuPriceLogController {

    @Autowired
    private SpuPriceLogService spuPriceLogService;


    @GetMapping("/page")
    @Operation(summary = "获取商品调价记录列表", description = "分页获取商品调价记录列表")
    public ServerResponseEntity<PageVO<SpuPriceLogVO>> page(@Valid PageDTO pageDTO, SpuPriceLogDTO spuPriceLogDTO) {
        spuPriceLogDTO.setSupplierId(AuthUserContext.get().getTenantId());
        PageVO<SpuPriceLogVO> spuPriceLogPage = spuPriceLogService.page(pageDTO, spuPriceLogDTO);
        return ServerResponseEntity.success(spuPriceLogPage);
    }

    @GetMapping
    @Operation(summary = "获取商品调价记录", description = "根据id获取商品调价记录")
    public ServerResponseEntity<SpuPriceLog> getById(@RequestParam Long id) {
        return ServerResponseEntity.success(spuPriceLogService.getById(id));
    }

    @PostMapping
    @Operation(summary = "保存商品调价记录", description = "保存商品调价记录")
    public ServerResponseEntity<Void> save(@Valid @RequestBody SpuPriceLogDTO spuPriceLogDTO) {
        SpuPriceLog spuPriceLog = BeanUtil.map(spuPriceLogDTO, SpuPriceLog.class);
        spuPriceLog.setId(null);
        spuPriceLogService.save(spuPriceLog);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新商品调价记录", description = "更新商品调价记录")
    public ServerResponseEntity<Void> update(@Valid @RequestBody SpuPriceLogDTO spuPriceLogDTO) {
        SpuPriceLog spuPriceLog = BeanUtil.map(spuPriceLogDTO, SpuPriceLog.class);
        spuPriceLogService.update(spuPriceLog);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除商品调价记录", description = "根据商品调价记录id删除商品调价记录")
    public ServerResponseEntity<Void> delete(@RequestParam Long id) {
        spuPriceLogService.deleteById(id);
        return ServerResponseEntity.success();
    }
}
