package com.zhongshu.card.server.core.controller.devices.deviceProduct;

import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.type.AuthType;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.base.IDParam;
import com.zhongshu.card.client.model.devices.deviceProduct.DeviceProductModel;
import com.zhongshu.card.client.model.devices.deviceProduct.DeviceProductParam;
import com.zhongshu.card.client.model.devices.deviceProduct.DeviceProductSearch;
import com.zhongshu.card.server.core.service.devices.deviceProduct.DeviceProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 项目 产品管理 （产品用的）
 *
 * @author TRX
 * @date 2024/6/5
 */
@RestController
@RequestMapping("/deviceProduct")
@Tag(name = "产品管理-产品管理")
public class DeviceProductController {

    @Autowired
    private DeviceProductService deviceProductService;

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "根据产品数据ID 产品详情", description = "根据产品数据ID 产品详情")
    @RequestMapping(value = "getInfo", method = {RequestMethod.POST})
    public ResultContent<DeviceProductModel> getInfo(@RequestBody IDParam param) {
        return this.deviceProductService.getInfo(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "添加-编辑产品", description = "添加-编辑产品")
    @RequestMapping(value = "saveInfo", method = {RequestMethod.POST})
    public ResultContent saveInfo(@RequestBody DeviceProductParam param) {
        return this.deviceProductService.saveInfo(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除产品", description = "删除产品")
    @RequestMapping(value = "deleteInfo", method = {RequestMethod.POST})
    public ResultContent deleteInfo(@RequestBody IDParam param) {
        return this.deviceProductService.deleteInfo(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "产品列表-分页查询", description = "产品列表-分页查询")
    @RequestMapping(value = {"page"}, method = {RequestMethod.POST})
    public ResultContent<Page<DeviceProductModel>> page(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) DeviceProductSearch param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        return deviceProductService.page(param, pageable);
    }


}
