package org.eco.als.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.SortieBo;
import org.eco.als.domain.vo.SortieVo;
import org.eco.als.service.ISortieService;
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/sortie")
public class SortieController extends BaseController {
    @Resource
    private ISortieService sortieService;

    /**
     * 查询架次列表
     */
    @SaCheckPermission("als:sortie:list")
    @GetMapping("/list")
    public CommonResult<PageResult<SortieVo>> list(SortieBo sortieBo) {
        return CommonResult.success(sortieService.selectPage(sortieBo));
    }

    /**
     * 查询架次列表下拉框
     */
    @SaCheckPermission("als:sortie:list")
    @GetMapping("/select")
    public CommonResult<List<SortieVo>> select(SortieBo sortieBo) {
        return CommonResult.success(sortieService.selectList(sortieBo));
    }

    /**
     * 导出架次列表
     */
    @SaCheckPermission("als:sortie:export")
    @Log(title = "架次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(SortieBo sortieBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<SortieVo> list = sortieService.selectList(sortieBo);
        sortieService.asyncExport(list, "架次", loginUser);
        return CommonResult.success();
    }

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

    /**
     * 获取架次详细信息
     */
    @SaCheckPermission("als:sortie:query")
    @GetMapping(value = "/{id}")
    public CommonResult<SortieVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(sortieService.selectById(id));
    }

    /**
     * 新增架次
     */
    @SaCheckPermission("als:sortie:add")
    @Log(title = "架次", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody SortieBo sortieBo) {
        boolean inserted = sortieService.insert(sortieBo);
        if (!inserted) {
            return CommonResult.fail("新增架次记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改架次
     */
    @SaCheckPermission("als:sortie:edit")
    @Log(title = "架次", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody SortieBo sortieBo) {
        boolean updated = sortieService.update(sortieBo);
        if (!updated) {
            return CommonResult.fail("修改架次记录失败!");
        }
        return CommonResult.success();
    }

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