package com.propertyManagement.web.controller.business;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.propertyManagement.business.domain.bo.WxRepairsBo;
import com.propertyManagement.business.domain.vo.WxRepairsVo;
import com.propertyManagement.business.service.IWxRepairsService;
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.helper.LoginHelper;
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 liushuai
 * @date 2025-05-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/repairs")
public class WxRepairsController extends BaseController {

    private final IWxRepairsService iWxRepairsService;

    /**
     * 查询报修列表
     */
    @SaCheckPermission("business:repairs:list")
    @GetMapping("/list")
    public TableDataInfo<WxRepairsVo> list(WxRepairsBo bo, PageQuery pageQuery) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        return iWxRepairsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出报修列表
     */
    @SaCheckPermission("business:repairs:export")
    @Log(title = "报修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxRepairsBo bo, HttpServletResponse response) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        List<WxRepairsVo> list = iWxRepairsService.queryList(bo);
        ExcelUtil.exportExcel(list, "报修", WxRepairsVo.class, response);
    }

    /**
     * 获取报修详细信息
     *
     * @param repairsId 主键
     */
    @SaCheckPermission("business:repairs:query")
    @GetMapping("/{repairsId}")
    public R<WxRepairsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long repairsId) {
        return R.ok(iWxRepairsService.queryById(repairsId));
    }

    /**
     * 新增报修
     */
    @SaCheckPermission("business:repairs:add")
    @Log(title = "报修", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxRepairsBo bo) {
        return toAjax(iWxRepairsService.insertByBo(bo));
    }

    /**
     * 修改报修
     */
    @SaCheckPermission("business:repairs:edit")
    @Log(title = "报修", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxRepairsBo bo) {
        return toAjax(iWxRepairsService.updateByBo(bo));
    }

    /**
     * 删除报修
     *
     * @param repairsIds 主键串
     */
    @SaCheckPermission("business:repairs:remove")
    @Log(title = "报修", businessType = BusinessType.DELETE)
    @DeleteMapping("/{repairsIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] repairsIds) {
        return toAjax(iWxRepairsService.deleteWithValidByIds(Arrays.asList(repairsIds), true));
    }
}
