package org.dromara.elevator.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.elevator.domain.vo.EMaintenanceStateStatisticsVo;
import org.dromara.elevator.domain.vo.WorkStateStatisticsVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.elevator.domain.vo.EMaintenancePlanVo;
import org.dromara.elevator.domain.bo.EMaintenancePlanBo;
import org.dromara.elevator.service.IEMaintenancePlanService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 维保计划;
 *
 * @author zy
 * @date 2024-09-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/maintenancePlan/maintenancePlan")
public class EMaintenancePlanController extends BaseController {

    private final IEMaintenancePlanService eMaintenancePlanService;

    /**
     * 查询维保计划;列表
     */
    @SaCheckPermission("elevator:maintenancePlan:list")
    @GetMapping("/list")
    public TableDataInfo<EMaintenancePlanVo> list(EMaintenancePlanBo bo, PageQuery pageQuery) {
        return eMaintenancePlanService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出维保计划;列表
     */
    @SaCheckPermission("elevator:maintenancePlan:export")
    @Log(title = "维保计划;", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EMaintenancePlanBo bo, HttpServletResponse response) {
        List<EMaintenancePlanVo> list = eMaintenancePlanService.queryList(bo);
        ExcelUtil.exportExcel(list, "维保计划;", EMaintenancePlanVo.class, response);
    }

    /**
     * 获取维保计划;详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("elevator:maintenancePlan:query")
    @GetMapping("/{id}")
    public R<EMaintenancePlanVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(eMaintenancePlanService.queryById(id));
    }

    /**
     * 新增维保计划;
     */
    @SaCheckPermission("elevator:maintenancePlan:add")
    @Log(title = "维保计划;", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EMaintenancePlanBo bo) {
        return toAjax(eMaintenancePlanService.insertByBo(bo));
    }

    /**
     * 修改维保计划;
     */
    @SaCheckPermission("elevator:maintenancePlan:edit")
    @Log(title = "维保计划;", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EMaintenancePlanBo bo) {
        return toAjax(eMaintenancePlanService.updateByBo(bo));
    }

    /**
     * 删除维保计划;
     *
     * @param ids 主键串
     */
    @SaCheckPermission("elevator:maintenancePlan:remove")
    @Log(title = "维保计划;", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(eMaintenancePlanService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 生成维保计划;
     */
    @SaCheckPermission("elevator:maintenancePlan:add")
    @Log(title = "维保计划;", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/create")
    public R<Void> createMaintenancePlan(@Validated(AddGroup.class) @RequestBody EMaintenancePlanBo bo) {
        return toAjax(eMaintenancePlanService.createMaintenancePlan(bo));
    }

    /**
     * 查询维保状态统计
     */
    @SaCheckPermission("elevator:maintenancePlan:list")
    @GetMapping("/statistics/list")
    public R<List<EMaintenanceStateStatisticsVo>> statisticsList(EMaintenancePlanBo bo) {
        return R.ok(eMaintenancePlanService.statisticsList(bo));
    }

    /**
     * 查询维保完成数量统计
     */
    @SaCheckPermission("elevator:maintenancePlan:list")
    @GetMapping("/complete/statistics/list")
    public R<List<WorkStateStatisticsVo>> completeStatisticsList(EMaintenancePlanBo bo) {
        return R.ok(eMaintenancePlanService.completeStatisticsList(bo));
    }
}
