package com.ylx.servicecategory.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.annotation.Log;
import com.ylx.common.core.domain.R;
import com.ylx.common.enums.BusinessType;
import com.ylx.common.exception.ServiceException;
import com.ylx.servicecategory.domain.ServiceCategory;
import com.ylx.servicecategory.service.ServiceCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务类目控制层
 */
@Slf4j
@RestController
@Api(tags = {"服务类目管理"})
@RequestMapping("serviceCategory")
public class ServiceCategoryController {

    @Resource
    private ServiceCategoryService serviceCategoryService;

    /**
     * H5查询首页服务类目
     *
     * @return H5首页服务类目列表
     */
    @GetMapping("h5List")
    @ApiOperation("H5查询首页服务类目")
    public R<List<ServiceCategory>> h5List() {
        try {
            return R.ok(serviceCategoryService.listH5ServiceCategory());
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("H5查询首页服务类目异常", e);
            return R.fail("H5查询首页服务类目失败");
        }
    }

    /**
     * 根据主键ID查询服务类目详情
     *
     * @param id 服务类目ID
     * @return 服务类目详情
     */
    @GetMapping("detail")
    @ApiOperation("根据主键ID查询服务类目详情")
    public R<ServiceCategory> detail(@RequestParam("id") Long id) {
        try {
            return R.ok(serviceCategoryService.getServiceCategoryDetail(id));
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询服务类目详情异常", e);
            return R.fail("查询服务类目详情失败");
        }
    }

    /**
     * 分页查询服务类目
     *
     * @param page            分页参数
     * @param serviceCategory 查询条件
     * @return 服务类目分页数据
     */
    @GetMapping("page")
    @ApiOperation("分页查询服务类目")
    public R<Page<ServiceCategory>> page(Page<ServiceCategory> page, ServiceCategory serviceCategory) {
        try {
            return R.ok(serviceCategoryService.pageServiceCategory(page, serviceCategory));
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("分页查询服务类目异常", e);
            return R.fail("分页查询服务类目失败");
        }
    }

    /**
     * 新增服务类目
     *
     * @param serviceCategory 服务类目
     * @return R<Long> 新增后的服务类目ID
     */
    @PostMapping("add")
    @ApiOperation("新增服务类目")
    @Log(title = "新增服务类目", businessType = BusinessType.INSERT)
    public R<Integer> add(@RequestBody ServiceCategory serviceCategory) {
        try {
            return R.ok(serviceCategoryService.addServiceCategory(serviceCategory));
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("新增服务类目异常", e);
            return R.fail("新增服务类目失败");
        }
    }

    /**
     * 修改服务类目
     *
     * @param serviceCategory 服务类目
     * @return 修改结果
     */
    @PostMapping("update")
    @ApiOperation("修改服务类目")
    @Log(title = "修改服务类目", businessType = BusinessType.UPDATE)
    public R<Boolean> update(@RequestBody ServiceCategory serviceCategory) {
        try {
            return R.ok(serviceCategoryService.updateServiceCategory(serviceCategory));
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("修改服务类目异常", e);
            return R.fail("修改服务类目失败");
        }
    }

    /**
     * 删除服务类目
     *
     * @param id 服务类目ID
     * @return 删除结果
     */
    @PostMapping("delete")
    @ApiOperation("删除服务类目")
    @Log(title = "删除服务类目", businessType = BusinessType.DELETE)
    public R<Boolean> delete(@RequestParam("id") Long id) {
        try {
            return R.ok(serviceCategoryService.deleteServiceCategory(id));
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("删除服务类目异常", e);
            return R.fail("删除服务类目失败");
        }
    }
}
