package com.ruoyi.web.controller.info;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.FilePathSplicingType;
import com.ruoyi.common.filepathsplicing.FilePathSplicing;
import com.ruoyi.info.service.domain.bo.ServiceProductBo;
import com.ruoyi.info.service.domain.vo.ServiceProductVo;
import com.ruoyi.info.service.enums.ServiceUnit;
import com.ruoyi.info.service.service.IServiceProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 服务Controller
 *
 * @author ruoyi
 * @date 2025-09-20
 */
@Validated
@Api(value = "服务控制器", tags = {"服务管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/service/serviceProduct")
public class ServiceProductController extends BaseController {

    private final IServiceProductService iServiceProductService;

    /**
     * 分页查询服务列表
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("查询服务列表")
    @SaCheckPermission("service:serviceProduct:list")
    @GetMapping("/page")
    public TableDataInfo<ServiceProductVo> page(@Validated(QueryGroup.class) ServiceProductBo bo, PageQuery pageQuery) {
        return iServiceProductService.queryPageList(bo, pageQuery);
    }

    @ApiOperation("枚举")
    @GetMapping("/enum")
    public R<Map<String,  Object>> getEnum() {
        Map<String, Object> map = new HashMap<>();
        map.put("serviceUnit", ServiceUnit.toJsonArray());
        return R.ok(map);
    }

    /**
     * 获取服务详细信息
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("获取服务详细信息")
    @SaCheckPermission("service:serviceProduct:query")
    @GetMapping("/info/{id}")
    public R<ServiceProductVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.ok(iServiceProductService.queryById(id));
    }

    /**
     * 新增服务
     */
    @FilePathSplicing(type = FilePathSplicingType.REQUEST)
    @ApiOperation("新增服务")
    @SaCheckPermission("service:serviceProduct:add")
    @Log(title = "服务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ServiceProductBo bo) {
        return toAjax(iServiceProductService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改服务
     */
    @FilePathSplicing(type = FilePathSplicingType.REQUEST)
    @ApiOperation("修改服务")
    @SaCheckPermission("service:serviceProduct:edit")
    @Log(title = "服务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ServiceProductBo bo) {
        return toAjax(iServiceProductService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除服务
     */
    @ApiOperation("删除服务")
    @SaCheckPermission("service:serviceProduct:remove")
    @Log(title = "服务", businessType = BusinessType.DELETE)
    @PostMapping("/del/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iServiceProductService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 修改服务状态
     */
    @ApiOperation("修改服务状态")
    @SaCheckPermission("service:serviceProduct:status")
    @Log(title = "服务", businessType = BusinessType.UPDATE)
    @PostMapping("/setStatus/{id}")
    public R<Void> setStatus(@ApiParam("主键串") @NotNull(message = "主键不能为空")
                             @PathVariable Long id) {
        return toAjax(iServiceProductService.setStatus(id) ? 1 : 0);
    }

}
