package com.propertyManagement.web.controller.business.api;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.propertyManagement.business.domain.bo.ConvenienceServiceBo;
import com.propertyManagement.business.domain.vo.ConvenienceServiceVo;
import com.propertyManagement.business.service.IConvenienceServiceService;
import com.propertyManagement.common.annotation.Anonymous;
import com.propertyManagement.common.annotation.Log;
import com.propertyManagement.common.annotation.RepeatSubmit;
import com.propertyManagement.common.core.controller.BaseController;
import com.propertyManagement.common.core.domain.PageQuery;
import com.propertyManagement.common.core.domain.R;
import com.propertyManagement.common.core.page.TableDataInfo;
import com.propertyManagement.common.core.validate.AddGroup;
import com.propertyManagement.common.core.validate.EditGroup;
import com.propertyManagement.common.enums.BusinessType;
import com.propertyManagement.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 便民服务
 *
 * @author Ashe
 * @date 2025-08-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/converienceService")
public class ApiConvenienceServiceController extends BaseController {

    private final IConvenienceServiceService iConvenienceServiceService;

    /**
     * 查询便民服务列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo<ConvenienceServiceVo> list(ConvenienceServiceBo bo, PageQuery pageQuery) {
        return iConvenienceServiceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出便民服务列表
     */
    @Log(title = "便民服务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ConvenienceServiceBo bo, HttpServletResponse response) {
        List<ConvenienceServiceVo> list = iConvenienceServiceService.queryList(bo);
        ExcelUtil.exportExcel(list, "便民服务", ConvenienceServiceVo.class, response);
    }

    /**
     * 获取便民服务详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ConvenienceServiceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iConvenienceServiceService.queryById(id));
    }

    /**
     * 新增便民服务
     */
    @Log(title = "便民服务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ConvenienceServiceBo bo) {
        return toAjax(iConvenienceServiceService.insertByBo(bo));
    }

    /**
     * 修改便民服务
     */
    @Log(title = "便民服务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ConvenienceServiceBo bo) {
        return toAjax(iConvenienceServiceService.updateByBo(bo));
    }

    /**
     * 删除便民服务
     *
     * @param ids 主键串
     */
    @Log(title = "便民服务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iConvenienceServiceService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
