package org.dromara.controller.propertyRepair;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.domain.propertyRepair.PropertyRepair;
import org.dromara.domain.propertyRepair.bo.PropertyRepairBo;
import org.dromara.domain.propertyRepair.vo.PropertyRepairVo;
import org.dromara.service.IPropertyRepairService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 物业报修
 *
 * @author boman
 * @date 2025-04-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wuYe/repair")
public class PropertyRepairController extends BaseController {

    private final IPropertyRepairService propertyRepairService;

    /**
     * 查询物业报修列表
     */
    @SaCheckPermission("wuYe:repair:list")
    @GetMapping("/list")
    public TableDataInfo<PropertyRepairVo> list(PropertyRepairBo bo, PageQuery pageQuery) {
        return propertyRepairService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出物业报修列表
     */
    @SaCheckPermission("wuYe:repair:export")
    @Log(title = "物业报修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PropertyRepairBo bo, HttpServletResponse response) {
        List<PropertyRepairVo> list = propertyRepairService.queryList(bo);
        ExcelUtil.exportExcel(list, "物业报修", PropertyRepairVo.class, response);
    }

    /**
     * 获取物业报修详细信息
     *
     * @param repairId 主键
     */
    @SaCheckPermission("wuYe:repair:query")
    @GetMapping("/{repairId}")
    public R<PropertyRepairVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long repairId) {
        return R.ok(propertyRepairService.queryById(repairId));
    }

    /**
     * 新增物业报修
     */
    @SaCheckPermission("wuYe:repair:add")
    @Log(title = "物业报修", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PropertyRepairBo bo) {
        return toAjax(propertyRepairService.insertByBo(bo));
    }

    /**
     * 修改物业报修
     */
    @SaCheckPermission("wuYe:repair:edit")
    @Log(title = "物业报修", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PropertyRepairBo bo) {
        return toAjax(propertyRepairService.updateByBo(bo));
    }

    /**
     * 删除物业报修
     *
     * @param repairIds 主键串
     */
    @SaCheckPermission("wuYe:repair:remove")
    @Log(title = "物业报修", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{repairIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] repairIds) {
        return toAjax(propertyRepairService.deleteWithValidByIds(List.of(repairIds), true));
    }

    /**
     * 指派物业报修人员
     */
    @SaCheckPermission("wuYe:repair:assign")
    @PostMapping("/assign")
    public R<Void> assign(@RequestBody PropertyRepair propertyRepair) {
        return propertyRepairService.assign(propertyRepair);
    }

    /**
     * 物业保修一周统计（当前时间往前推7天）
     */
    @SaCheckPermission("wuYe:repair:statistics:qx")
    @GetMapping("/statistics/qx")
    public R<Map<String, Object>> statisticsQx() {
        return R.ok(propertyRepairService.statisticsQx());
    }
}
