package org.dromara.biz.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.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.biz.domain.vo.BizTaskWorkVo;
import org.dromara.biz.domain.bo.BizTaskWorkBo;
import org.dromara.biz.service.IBizTaskWorkService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 任务协作单
 *
 * @author chenle
 * @date 2025-05-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/biz/taskWork")
public class BizTaskWorkController extends BaseController {

    private final IBizTaskWorkService bizTaskWorkService;

    /**
     * 查询任务协作单列表
     */
    @GetMapping("/list")
    public TableDataInfo<BizTaskWorkVo> list(BizTaskWorkBo bo, PageQuery pageQuery) {
        return bizTaskWorkService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出任务协作单列表
     */
    @Log(title = "任务协作单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BizTaskWorkBo bo, HttpServletResponse response) {
        List<BizTaskWorkVo> list = bizTaskWorkService.queryList(bo);
        ExcelUtil.exportExcel(list, "任务协作单", BizTaskWorkVo.class, response);
    }

    /**
     * 获取任务协作单详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<BizTaskWorkVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(bizTaskWorkService.queryById(id));
    }

    /**
     * 新增任务协作单
     */
    @Log(title = "任务协作单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BizTaskWorkBo bo) {
        return toAjax(bizTaskWorkService.insertByBo(bo));
    }

    /**
     * 修改任务协作单
     */
    @Log(title = "任务协作单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BizTaskWorkBo bo) {
        return toAjax(bizTaskWorkService.updateByBo(bo));
    }

    /**
     * 删除任务协作单
     *
     * @param ids 主键串
     */
    @Log(title = "任务协作单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(bizTaskWorkService.deleteWithValidByIds(List.of(ids), true));
    }
}
