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.TechnicalDocBo;
import org.eco.als.domain.vo.TechnicalDocVo;
import org.eco.als.service.ITechnicalDocService;
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 org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 技术资料Controller
 *
 * @author wgk
 * @date 2024-07-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/technicalDoc")
public class TechnicalDocController extends BaseController {
    @Resource
    private ITechnicalDocService technicalDocService;

    /**
     * 查询技术资料列表
     */
    @SaCheckPermission("als:technicalDoc:list")
    @GetMapping("/list")
    public CommonResult<PageResult<TechnicalDocVo>> list(TechnicalDocBo technicalDocBo) {
        return CommonResult.success(technicalDocService.selectPage(technicalDocBo));
    }

    /**
     * 导出技术资料列表
     */
    @SaCheckPermission("als:technicalDoc:export")
    @Log(title = "技术资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(HttpServletResponse response, TechnicalDocBo technicalDocBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<TechnicalDocVo> list = technicalDocService.selectList(technicalDocBo);
        technicalDocService.asyncExport(list, "技术资料", loginUser);
        return CommonResult.success();
    }

    /**
     * 导入技术资料列表
     */
    @Log(title = "技术资料", businessType = BusinessType.IMPORT)
    @SaCheckPermission("als:technicalDoc:import")
    @PostMapping("/importData")
    public CommonResult<Void> importData(MultipartFile file, boolean updateSupport) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        technicalDocService.asyncImportData(file, updateSupport, loginUser);
        return CommonResult.success();
    }

    /**
     * 获取技术资料详细信息
     */
    @SaCheckPermission("als:technicalDoc:query")
    @GetMapping(value = "/{id}")
    public CommonResult<TechnicalDocVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(technicalDocService.selectById(id));
    }

    /**
     * 新增技术资料
     */
    @SaCheckPermission("als:technicalDoc:add")
    @Log(title = "技术资料", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody TechnicalDocBo technicalDocBo) {
        boolean inserted = technicalDocService.insert(technicalDocBo);
        if (!inserted) {
            return CommonResult.fail("新增技术资料记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改技术资料
     */
    @SaCheckPermission("als:technicalDoc:edit")
    @Log(title = "技术资料", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody TechnicalDocBo technicalDocBo) {
        boolean updated = technicalDocService.update(technicalDocBo);
        if (!updated) {
            return CommonResult.fail("修改技术资料记录失败!");
        }
        return CommonResult.success();
    }

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