package com.yueqian.xingyun.sc.controller.purchase;

import com.lframework.starter.web.core.controller.DefaultBaseController;
import com.lframework.starter.web.core.components.resp.InvokeResult;
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
import com.yueqian.xingyun.sc.bo.purchase.config.GetPurchaseConfigBo;
import com.yueqian.xingyun.sc.entity.PurchaseConfig;
import com.yueqian.xingyun.sc.service.purchase.PurchaseConfigService;
import com.yueqian.xingyun.sc.vo.purchase.config.UpdatePurchaseConfigVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 采购参数设置
 *
 * @author zmj
 */
@Tag(name = "采购参数设置")
@Validated
@RestController
@RequestMapping("/purchase/config")
public class PurchaseConfigController extends DefaultBaseController {

  @Autowired
  private PurchaseConfigService purchaseConfigService;

  /**
   * 查询详情
   */
  @Operation(summary = "查询详情")
  @GetMapping
  public InvokeResult<GetPurchaseConfigBo> get() {

    PurchaseConfig config = purchaseConfigService.get();
    GetPurchaseConfigBo result = new GetPurchaseConfigBo(config);

    return InvokeResultBuilder.success(result);
  }

  /**
   * 修改
   */
  @Operation(summary = "修改")
  @PutMapping
  public InvokeResult<Void> update(@Valid UpdatePurchaseConfigVo vo) {

    purchaseConfigService.update(vo);

    purchaseConfigService.cleanCacheByKey(null);

    return InvokeResultBuilder.success();
  }
}
