package org.eco.als.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.LifetimeSparePartsBo;
import org.eco.als.domain.vo.LifetimeSparePartsImportVo;
import org.eco.als.domain.vo.LifetimeSparePartsVo;
import org.eco.als.listener.LifetimeSparePartsImportListener;
import org.eco.als.service.ILifetimeSparePartsService;
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.excel.core.ExcelResult;
import org.eco.common.excel.utils.ExcelUtil;
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.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 有寿件Controller
 *
 * @author wgk
 * @date 2024-12-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/lifetimeSpareParts")
public class LifetimeSparePartsController extends BaseController {
    @Resource
    private ILifetimeSparePartsService lifetimeSparePartsService;

    /**
     * 查询有寿件列表
     */
    @SaCheckPermission("als:lifetimeSpareParts:list")
    @GetMapping("/list")
    public CommonResult<PageResult<LifetimeSparePartsVo>> list(LifetimeSparePartsBo lifetimeSparePartsBo) {
        return CommonResult.success(lifetimeSparePartsService.selectPage(lifetimeSparePartsBo));
    }

    /**
     * 导出有寿件列表
     */
    @SaCheckPermission("als:lifetimeSpareParts:export")
    @Log(title = "有寿件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, LifetimeSparePartsBo lifetimeSparePartsBo) {
        List<LifetimeSparePartsVo> list = lifetimeSparePartsService.selectList(lifetimeSparePartsBo);
        ExcelUtil.exportExcel(list, "有寿件", LifetimeSparePartsVo.class, response);
    }

    @SaCheckPermission("als:lifetimeSpareParts:import")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "有寿件", LifetimeSparePartsImportVo.class, response);
    }

    /**
     * 导入有寿件列表
     */
    @Log(title = "有寿件", businessType = BusinessType.IMPORT)
    @SaCheckPermission("als:lifetimeSpareParts:import")
    @PostMapping("/importData")
    public CommonResult<Void> importData(MultipartFile file, boolean updateSupport) throws Exception {
        LoginUser loginUser = LoginHelper.getLoginUser();
        ExcelResult<LifetimeSparePartsImportVo> result = ExcelUtil.importExcel(file.getInputStream(), LifetimeSparePartsImportVo.class, new LifetimeSparePartsImportListener(updateSupport, loginUser));
        return CommonResult.success(result.getAnalysis().getLogInfo());
    }

    /**
     * 获取有寿件详细信息
     */
    @SaCheckPermission("als:lifetimeSpareParts:query")
    @GetMapping(value = "/{id}")
    public CommonResult<LifetimeSparePartsVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(lifetimeSparePartsService.selectById(id));
    }

    /**
     * 新增有寿件
     */
    @SaCheckPermission("als:lifetimeSpareParts:add")
    @Log(title = "有寿件", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody LifetimeSparePartsBo lifetimeSparePartsBo) {
        boolean inserted = lifetimeSparePartsService.insert(lifetimeSparePartsBo);
        if (!inserted) {
            return CommonResult.fail("新增有寿件记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改有寿件
     */
    @SaCheckPermission("als:lifetimeSpareParts:edit")
    @Log(title = "有寿件", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody LifetimeSparePartsBo lifetimeSparePartsBo) {
        boolean updated = lifetimeSparePartsService.update(lifetimeSparePartsBo);
        if (!updated) {
            return CommonResult.fail("修改有寿件记录失败!");
        }
        return CommonResult.success();
    }

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