package org.dromara.elevator.controller;

import java.util.ArrayList;
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.common.excel.core.ExcelResult;
import org.dromara.elevator.domain.bo.EMaintenanceStatisticsBo;
import org.dromara.elevator.domain.vo.EMaintenanceInfoImportVo;
import org.dromara.elevator.domain.vo.EMaintenanceStatisticsVo;
import org.dromara.elevator.listener.EMaintenanceInfoImportListener;
import org.springframework.http.MediaType;
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.EMaintenanceInfoVo;
import org.dromara.elevator.domain.bo.EMaintenanceInfoBo;
import org.dromara.elevator.service.IEMaintenanceInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 维保单位基础信息;
 *
 * @author zy
 * @date 2024-07-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/maintenance/maintenanceInfo")
public class EMaintenanceInfoController extends BaseController {

    private final IEMaintenanceInfoService eMaintenanceInfoService;

    /**
     * 查询维保单位基础信息;列表
     */
    @SaCheckPermission("elevator:maintenanceInfo:list")
    @GetMapping("/list")
    public TableDataInfo<EMaintenanceInfoVo> list(EMaintenanceInfoBo bo, PageQuery pageQuery) {
        return eMaintenanceInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出维保单位基础信息;列表
     */
    @SaCheckPermission("elevator:maintenanceInfo:export")
    @Log(title = "维保单位基础信息;", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EMaintenanceInfoBo bo, HttpServletResponse response) {
        List<EMaintenanceInfoVo> list = eMaintenanceInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "维保单位基础信息;", EMaintenanceInfoVo.class, response);
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "维保单位基础数据", EMaintenanceInfoImportVo.class, response);
    }

    /**
     * 导入维保单位基础数据
     *
     * @param file          维保单位基础数据
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "维保单位基础信息", businessType = BusinessType.IMPORT)
    @SaCheckPermission("maintenance:maintenanceInfo:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<EMaintenanceInfoImportVo> result = ExcelUtil.importExcel(file.getInputStream(), EMaintenanceInfoImportVo.class, new EMaintenanceInfoImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取维保单位基础信息;详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("elevator:maintenanceInfo:query")
    @GetMapping("/{id}")
    public R<EMaintenanceInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(eMaintenanceInfoService.queryById(id));
    }

    /**
     * 新增维保单位基础信息;
     */
    @SaCheckPermission("elevator:maintenanceInfo:add")
    @Log(title = "维保单位基础信息;", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EMaintenanceInfoBo bo) {
        return toAjax(eMaintenanceInfoService.insertByBo(bo));
    }

    /**
     * 修改维保单位基础信息;
     */
    @SaCheckPermission("elevator:maintenanceInfo:edit")
    @Log(title = "维保单位基础信息;", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EMaintenanceInfoBo bo) {
        return toAjax(eMaintenanceInfoService.updateByBo(bo));
    }

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

    /**
     * 查询维保统计信息;列表
     */
    @SaCheckPermission("elevator:maintenanceInfo:list")
    @GetMapping("/statistics/list")
    public TableDataInfo<EMaintenanceStatisticsVo> list(EMaintenanceStatisticsBo bo, PageQuery pageQuery) {
        return eMaintenanceInfoService.queryPageStatisticsList(bo, pageQuery);
    }
}
