package org.eco.als.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.PreProcessingBo;
import org.eco.als.domain.vo.PreProcessingVo;
import org.eco.als.service.IPreProcessingService;
import org.eco.common.core.core.domain.CommonResult;
import org.eco.common.core.core.domain.model.LoginUser;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.log.annotation.Log;
import org.eco.common.log.enums.BusinessType;
import org.eco.common.security.utils.LoginHelper;
import org.eco.common.web.annotation.RepeatSubmit;
import org.eco.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 预处理Controller
 *
 * @author wgk
 * @date 2024-07-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/preProcessing")
public class PreProcessingController extends BaseController {
    @Resource
    private IPreProcessingService preProcessingService;

    /**
     * 查询预处理列表
     */
    @SaCheckPermission("als:preProcessing:list")
    @GetMapping("/list")
    public CommonResult<PageResult<PreProcessingVo>> list(PreProcessingBo preProcessingBo) {
        return CommonResult.success(preProcessingService.selectPage(preProcessingBo));
    }

    /**
     * 导出预处理列表
     */
    @SaCheckPermission("als:preProcessing:export")
    @Log(title = "预处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(PreProcessingBo preProcessingBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<PreProcessingVo> list = preProcessingService.selectList(preProcessingBo);
        preProcessingService.asyncExport(list, "预处理", loginUser);
        return CommonResult.success();
    }

    /**
     * 获取预处理详细信息
     */
    @SaCheckPermission("als:preProcessing:query")
    @GetMapping(value = "/{id}")
    public CommonResult<PreProcessingVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(preProcessingService.selectById(id));
    }

    /**
     * 新增预处理
     */
    @SaCheckPermission("als:preProcessing:add")
    @Log(title = "预处理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody PreProcessingBo preProcessingBo) {
        boolean inserted = preProcessingService.insert(preProcessingBo);
        if (!inserted) {
            return CommonResult.fail("新增预处理记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改预处理
     */
    @SaCheckPermission("als:preProcessing:edit")
    @Log(title = "预处理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody PreProcessingBo preProcessingBo) {
        boolean updated = preProcessingService.update(preProcessingBo);
        if (!updated) {
            return CommonResult.fail("修改预处理记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除预处理
     */
    @SaCheckPermission("als:preProcessing:remove")
    @Log(title = "预处理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = preProcessingService.deleteByIds(ids);
        if (!deleted) {
            return CommonResult.fail("删除预处理记录失败!");
        }
        return CommonResult.success();
    }
}
