
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.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.product.dto.SpuSupplierChangeLogDTO;
import com.tmerclub.cloud.product.model.SpuSupplierChangeLog;
import com.tmerclub.cloud.product.service.SpuSupplierChangeLogService;
import com.tmerclub.cloud.product.vo.SpuSupplierChangeLogVO;
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.List;

/**
 * 商品供应商变更记录
 *
 * @author lhd
 * @date 2022-06-22 17:14:22
 */
@RestController("multishopSpuSupplierChangeLogController")
@RequestMapping("/m/spu_supplier_change_log")
@Tag(name = "商品供应商变更记录")
public class SpuSupplierChangeLogController {

    @Autowired
    private SpuSupplierChangeLogService spuSupplierChangeLogService;


    @GetMapping("/page")
    @Operation(summary = "获取商品供应商变更记录列表", description = "分页获取商品供应商变更记录列表")
    public ServerResponseEntity<PageVO<SpuSupplierChangeLogVO>> page(@Valid PageDTO pageDTO) {
        PageVO<SpuSupplierChangeLogVO> spuSupplierChangeLogPage = spuSupplierChangeLogService.page(pageDTO);
        return ServerResponseEntity.success(spuSupplierChangeLogPage);
    }

    @GetMapping
    @Operation(summary = "获取商品供应商变更记录", description = "根据spuChangeId获取商品供应商变更记录")
    public ServerResponseEntity<SpuSupplierChangeLog> getBySpuChangeId(@RequestParam Long spuChangeId) {
        return ServerResponseEntity.success(spuSupplierChangeLogService.getBySpuChangeId(spuChangeId));
    }

    @PostMapping
    @Operation(summary = "保存商品供应商变更记录", description = "保存商品供应商变更记录")
    public ServerResponseEntity<Void> save(@Valid @RequestBody SpuSupplierChangeLogDTO spuSupplierChangeLogDTO) {
        SpuSupplierChangeLog spuSupplierChangeLog = BeanUtil.map(spuSupplierChangeLogDTO, SpuSupplierChangeLog.class);
        spuSupplierChangeLog.setSpuChangeId(null);
        spuSupplierChangeLogService.save(spuSupplierChangeLog);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新商品供应商变更记录", description = "更新商品供应商变更记录")
    public ServerResponseEntity<Void> update(@Valid @RequestBody SpuSupplierChangeLogDTO spuSupplierChangeLogDTO) {
        SpuSupplierChangeLog spuSupplierChangeLog = BeanUtil.map(spuSupplierChangeLogDTO, SpuSupplierChangeLog.class);
        spuSupplierChangeLogService.update(spuSupplierChangeLog);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除商品供应商变更记录", description = "根据商品供应商变更记录id删除商品供应商变更记录")
    public ServerResponseEntity<Void> delete(@RequestParam Long spuChangeId) {
        spuSupplierChangeLogService.deleteById(spuChangeId);
        return ServerResponseEntity.success();
    }

    @PostMapping("/batchSync")
    @Operation(summary = "变更信息批量同步", description = "变更信息批量同步")
    public ServerResponseEntity<Void> batchSync(@RequestBody List<SpuSupplierChangeLogDTO> spuSupplierChangeLog) {
        Long shopId = AuthUserContext.get().getTenantId();
        spuSupplierChangeLogService.syncSpu(shopId, spuSupplierChangeLog);
        return ServerResponseEntity.success();
    }
}
