package org.dromara.quality.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
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.qualityconstructionlog.QltQualityConstructionLogCreateReq;
import org.dromara.quality.domain.dto.qualityconstructionlog.QltQualityConstructionLogQueryReq;
import org.dromara.quality.domain.dto.qualityconstructionlog.QltQualityConstructionLogUpdateReq;
import org.dromara.quality.domain.vo.qualityconstructionlog.QltQualityConstructionLogVo;
import org.dromara.quality.service.IQltQualityConstructionLogService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 质量-施工日志
 *
 * @author lcj
 * @date 2025-04-16
 */
@Validated
@RestController
@RequestMapping("/quality/qualityConstructionLog")
public class QltQualityConstructionLogController extends BaseController {

    @Resource
    private IQltQualityConstructionLogService qualityConstructionLogService;

    /**
     * 查询质量-施工日志列表
     */
    @SaCheckPermission("quality:qualityConstructionLog:list")
    @GetMapping("/list")
    public TableDataInfo<QltQualityConstructionLogVo> list(QltQualityConstructionLogQueryReq req, PageQuery pageQuery) {
        return qualityConstructionLogService.queryPageList(req, pageQuery);
    }

    /**
     * 导出质量-施工日志列表
     */
    @SaCheckPermission("quality:qualityConstructionLog:export")
    @Log(title = "质量-施工日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(QltQualityConstructionLogQueryReq req, HttpServletResponse response) {
        List<QltQualityConstructionLogVo> list = qualityConstructionLogService.queryList(req);
        ExcelUtil.exportExcel(list, "质量-施工日志", QltQualityConstructionLogVo.class, response);
    }

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

    /**
     * 获取质量-施工日志详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("quality:qualityConstructionLog:query")
    @GetMapping("/{id}")
    public R<QltQualityConstructionLogVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable Long id) {
        return R.ok(qualityConstructionLogService.queryById(id));
    }

    /**
     * 新增质量-施工日志
     */
    @SaCheckPermission("quality:qualityConstructionLog:add")
    @Log(title = "质量-施工日志", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody QltQualityConstructionLogCreateReq req) {
        return R.ok(qualityConstructionLogService.insertByBo(req));
    }

    /**
     * 修改质量-施工日志
     */
    @SaCheckPermission("quality:qualityConstructionLog:edit")
    @Log(title = "质量-施工日志", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QltQualityConstructionLogUpdateReq req) {
        return toAjax(qualityConstructionLogService.updateByBo(req));
    }

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