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.LifePredictionBo;
import org.eco.als.domain.vo.LifePredictionVo;
import org.eco.als.service.ILifePredictionService;
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/lifePrediction")
public class LifePredictionController extends BaseController {
    @Resource
    private ILifePredictionService lifePredictionService;

    /**
     * 查询寿命预测列表
     */
    @SaCheckPermission("als:lifePrediction:list")
    @GetMapping("/list")
    public CommonResult<PageResult<LifePredictionVo>> list(LifePredictionBo lifePredictionBo) {
        return CommonResult.success(lifePredictionService.selectPage(lifePredictionBo));
    }

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

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

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

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

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