package com.yueqian.xingyun.sc.controller.stock.take;

import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.web.core.annotations.security.HasPermission;
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.stock.take.config.GetTakeStockConfigBo;
import com.yueqian.xingyun.sc.entity.TakeStockConfig;
import com.yueqian.xingyun.sc.service.stock.take.TakeStockConfigService;
import com.yueqian.xingyun.sc.vo.stock.take.config.UpdateTakeStockConfigVo;
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;

/**
 * 盘点参数 Controller
 *
 * @author zmj
 */
@Tag(name = "盘点参数")
@Validated
@RestController
@RequestMapping("/stock/take/config")
public class TakeStockConfigController extends DefaultBaseController {

    @Autowired
    private TakeStockConfigService takeStockConfigService;

    /**
     * 根据ID查询
     */
    @Operation(summary = "根据ID查询")
    @HasPermission({"stock:take:config:modify", "stock:take:plan:handle:diff"})
    @GetMapping
    public InvokeResult<GetTakeStockConfigBo> get() {

        TakeStockConfig data = takeStockConfigService.get();
        if (data == null) {
            throw new DefaultClientException("盘点参数不存在！");
        }

        GetTakeStockConfigBo result = new GetTakeStockConfigBo(data);

        return InvokeResultBuilder.success(result);
    }

    /**
     * 修改
     */
    @Operation(summary = "修改")
    @HasPermission({"stock:take:config:modify"})
    @PutMapping
    public InvokeResult<Void> update(@Valid UpdateTakeStockConfigVo vo) {

        takeStockConfigService.update(vo);

        takeStockConfigService.cleanCacheByKey(null);

        return InvokeResultBuilder.success();
    }
}
