package org.dromara.elevator.controller;

import java.time.LocalDate;
import java.util.List;
import java.util.Set;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
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.EAnnualPlanVo;
import org.dromara.elevator.domain.bo.EAnnualPlanBo;
import org.dromara.elevator.service.IEAnnualPlanService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 年检计划;
 *
 * @author zy
 * @date 2024-09-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/annual/annualPlan")
public class EAnnualPlanController extends BaseController {

    private final IEAnnualPlanService eAnnualPlanService;

    /**
     * 查询年检计划;列表
     */
    @SaCheckPermission("elevator:annualPlan:list")
    @GetMapping("/list")
    public TableDataInfo<EAnnualPlanVo> list(EAnnualPlanBo bo, PageQuery pageQuery) {
        return eAnnualPlanService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询超期年检数据
     */
    @SaCheckPermission("elevator:annualPlan:list")
    @GetMapping("/overdue/list")
    public TableDataInfo<EAnnualPlanVo> overdueList(EAnnualPlanBo bo, PageQuery pageQuery) {
        return eAnnualPlanService.overduePageList(bo, pageQuery);
    }

    /**
     * 导出年检计划;列表
     */
    @SaCheckPermission("elevator:annualPlan:export")
    @Log(title = "年检计划;", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EAnnualPlanBo bo, HttpServletResponse response) {
        List<EAnnualPlanVo> list = eAnnualPlanService.queryList(bo);
        ExcelUtil.exportExcel(list, "年检计划;", EAnnualPlanVo.class, response);
    }

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

    /**
     * 获取电梯年检日历
     * @return
     */
    @SaCheckPermission("elevator:annualPlan:query")
    @GetMapping("/getDate")
    public R<Set<LocalDate>> getDateList() {
        return R.ok(eAnnualPlanService.getDateList());
    }

    /**
     * 根据日期获取电梯年检信息
     * @return
     */
    @SaCheckPermission("elevator:annualPlan:query")
    @GetMapping("/getByDate/{planDate}")
    public R<List<EAnnualPlanVo>> getAnnualByDate(@PathVariable LocalDate planDate) {
        return R.ok(eAnnualPlanService.getAnnualByDate(planDate));
    }

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

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

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