package org.eco.als.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.SmTaskRecordBo;
import org.eco.als.domain.vo.SmTaskRecordImportVo;
import org.eco.als.domain.vo.SmTaskRecordVo;
import org.eco.als.service.ISmTaskRecordService;
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.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.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 org.springframework.web.multipart.MultipartFile;

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

/**
 * 定检任务记录Controller
 *
 * @author wgk
 * @date 2024-07-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/smTaskRecord")
public class SmTaskRecordController extends BaseController {
    @Resource
    private ISmTaskRecordService smTaskRecordService;

    /**
     * 查询定检任务记录列表
     */
    @SaCheckPermission("als:smTaskRecord:list")
    @GetMapping("/list")
    public CommonResult<PageResult<SmTaskRecordVo>> list(SmTaskRecordBo smTaskRecordBo) {
        return CommonResult.success(smTaskRecordService.selectPage(smTaskRecordBo));
    }

    /**
     * 导出定检任务记录列表
     */
    @SaCheckPermission("als:smTaskRecord:export")
    @Log(title = "定检任务记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(SmTaskRecordBo smTaskRecordBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<SmTaskRecordVo> list = smTaskRecordService.selectList(smTaskRecordBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        smTaskRecordService.asyncExport(list, "定检任务记录", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("als:smTaskRecord:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "定检任务记录", SmTaskRecordImportVo.class, response);
    }

    /**
     * 导入定检任务记录列表
     */
    @Log(title = "定检任务记录", businessType = BusinessType.IMPORT)
    @SaCheckPermission("als:smTaskRecord:import")
    @PostMapping("/importData")
    public CommonResult<Void> importData(MultipartFile file, boolean updateSupport) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        smTaskRecordService.asyncImportData(file, updateSupport, loginUser);
        return CommonResult.success();
    }

    /**
     * 获取定检任务记录详细信息
     */
    @SaCheckPermission("als:smTaskRecord:query")
    @GetMapping(value = "/{id}")
    public CommonResult<SmTaskRecordVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(smTaskRecordService.selectById(id));
    }

    /**
     * 新增定检任务记录
     */
    @SaCheckPermission("als:smTaskRecord:add")
    @Log(title = "定检任务记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody SmTaskRecordBo smTaskRecordBo) {
        boolean inserted = smTaskRecordService.insert(smTaskRecordBo);
        if (!inserted) {
            return CommonResult.fail("新增定检任务记录记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改定检任务记录
     */
    @SaCheckPermission("als:smTaskRecord:edit")
    @Log(title = "定检任务记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody SmTaskRecordBo smTaskRecordBo) {
        boolean updated = smTaskRecordService.update(smTaskRecordBo);
        if (!updated) {
            return CommonResult.fail("修改定检任务记录记录失败!");
        }
        return CommonResult.success();
    }

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