package org.dromara.quality.controller;

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.quality.domain.dto.qualityinspection.QltQualityInspectionCreateReq;
import org.dromara.quality.domain.dto.qualityinspection.QltQualityInspectionGisReq;
import org.dromara.quality.domain.dto.qualityinspection.QltQualityInspectionQueryReq;
import org.dromara.quality.domain.dto.qualityinspection.QltQualityInspectionUpdateReq;
import org.dromara.quality.domain.vo.qualityinspection.QltQualityInspectionVo;
import org.dromara.quality.domain.vo.qualityinspection.QltQualityInspectionListGisVo;
import org.dromara.quality.service.IQltQualityInspectionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 质量-检查工单
 *
 * @author lcj
 * @date 2025-04-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/quality/qualityInspection")
public class QltQualityInspectionController extends BaseController {

    private final IQltQualityInspectionService qualityInspectionService;

    /**
     * 查询质量-检查工单列表
     */
    @SaCheckPermission("quality:qualityInspection:list")
    @GetMapping("/list")
    public TableDataInfo<QltQualityInspectionVo> list(QltQualityInspectionQueryReq req, PageQuery pageQuery) {
        return qualityInspectionService.queryPageList(req, pageQuery);
    }

    /**
     * 导出质量-检查工单列表
     */
    @SaCheckPermission("quality:qualityInspection:export")
    @Log(title = "质量-检查工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(QltQualityInspectionQueryReq req, HttpServletResponse response) {
        List<QltQualityInspectionVo> list = qualityInspectionService.queryList(req);
        ExcelUtil.exportExcel(list, "质量-检查工单", QltQualityInspectionVo.class, response);
    }

    /**
     * 查询大屏质量信息
     */
    @GetMapping("/gis")
    public R<QltQualityInspectionListGisVo> queryGisList(QltQualityInspectionGisReq req) {
        return R.ok(qualityInspectionService.queryGisList(req));
    }

    /**
     * 根据主键导出质量-检查工单
     */
    @SaCheckPermission("quality:qualityInspection:export")
    @Log(title = "质量-检查工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export/word")
    public void exportWordById(@NotNull(message = "主键不能为空") Long id,
                               HttpServletResponse response) {
        qualityInspectionService.exportWordById(id, response);
    }

    /**
     * 获取质量-检查工单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("quality:qualityInspection:query")
    @GetMapping("/{id}")
    public R<QltQualityInspectionVo> getInfo(@NotNull(message = "主键不能为空")
                                             @PathVariable Long id) {
        return R.ok(qualityInspectionService.queryById(id));
    }

    /**
     * 新增质量-检查工单
     */
    @SaCheckPermission("quality:qualityInspection:add")
    @Log(title = "质量-检查工单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody QltQualityInspectionCreateReq req) {
        return R.ok(qualityInspectionService.insertByBo(req));
    }

    /**
     * 修改质量-检查工单
     */
    @SaCheckPermission("quality:qualityInspection:edit")
    @Log(title = "质量-检查工单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QltQualityInspectionUpdateReq req) {
        return toAjax(qualityInspectionService.updateByBo(req));
    }

    /**
     * 删除质量-检查工单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("quality:qualityInspection:remove")
    @Log(title = "质量-检查工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(qualityInspectionService.deleteWithValidByIds(List.of(ids), true));
    }
}
