package org.eco.als.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.LifePredictionResultBo;
import org.eco.als.domain.vo.LifePredictionResultVo;
import org.eco.als.service.ILifePredictionResultService;
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-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/lifePredictionResult")
public class LifePredictionResultController extends BaseController {
    @Resource
    private ILifePredictionResultService lifePredictionResultService;

    /**
     * 查询寿命预测结果列表
     */
    @SaCheckPermission("als:lifePredictionResult:list")
    @GetMapping("/list")
    public CommonResult<PageResult<LifePredictionResultVo>> list(LifePredictionResultBo lifePredictionResultBo) {
        return CommonResult.success(lifePredictionResultService.selectPage(lifePredictionResultBo));
    }

    /**
     * 导出寿命预测结果列表
     */
    @SaCheckPermission("als:lifePredictionResult:export")
    @Log(title = "寿命预测结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(LifePredictionResultBo lifePredictionResultBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<LifePredictionResultVo> list = lifePredictionResultService.selectList(lifePredictionResultBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        lifePredictionResultService.asyncExport(list, "寿命预测结果", loginUser);
        return CommonResult.success();
    }

    /**
     * 获取寿命预测结果详细信息
     */
    @SaCheckPermission("als:lifePredictionResult:query")
    @GetMapping(value = "/{id}")
    public CommonResult<LifePredictionResultVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(lifePredictionResultService.selectById(id));
    }

    /**
     * 新增寿命预测结果
     */
    @SaCheckPermission("als:lifePredictionResult:add")
    @Log(title = "寿命预测结果", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody LifePredictionResultBo lifePredictionResultBo) {
        boolean inserted = lifePredictionResultService.insert(lifePredictionResultBo);
        if (!inserted) {
            return CommonResult.fail("新增寿命预测结果记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改寿命预测结果
     */
    @SaCheckPermission("als:lifePredictionResult:edit")
    @Log(title = "寿命预测结果", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody LifePredictionResultBo lifePredictionResultBo) {
        boolean updated = lifePredictionResultService.update(lifePredictionResultBo);
        if (!updated) {
            return CommonResult.fail("修改寿命预测结果记录失败!");
        }
        return CommonResult.success();
    }

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