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.CarryChecklistBo;
import org.eco.als.domain.vo.CarryChecklistImportVo;
import org.eco.als.domain.vo.CarryChecklistVo;
import org.eco.als.service.ICarryChecklistService;
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/carryChecklist")
public class CarryChecklistController extends BaseController {
    @Resource
    private ICarryChecklistService carryChecklistService;

    /**
     * 查询任务携行列表
     */
    @SaCheckPermission("als:carryChecklist:list")
    @GetMapping("/list")
    public CommonResult<PageResult<CarryChecklistVo>> list(CarryChecklistBo carryChecklistBo) {
        return CommonResult.success(carryChecklistService.selectPage(carryChecklistBo));
    }

    /**
     * 导出任务携行列表
     */
    @SaCheckPermission("als:carryChecklist:export")
    @Log(title = "任务携行", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(CarryChecklistBo carryChecklistBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<CarryChecklistVo> list = carryChecklistService.selectList(carryChecklistBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        carryChecklistService.asyncExport(list, "任务携行", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("als:carryChecklist:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "任务携行", CarryChecklistImportVo.class, response);
    }

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

    /**
     * 获取任务携行详细信息
     */
    @SaCheckPermission("als:carryChecklist:query")
    @GetMapping(value = "/{id}")
    public CommonResult<CarryChecklistVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(carryChecklistService.selectById(id));
    }

    /**
     * 新增任务携行
     */
    @SaCheckPermission("als:carryChecklist:add")
    @Log(title = "任务携行", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody CarryChecklistBo carryChecklistBo) {
        boolean inserted = carryChecklistService.insert(carryChecklistBo);
        if (!inserted) {
            return CommonResult.fail("新增任务携行记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改任务携行
     */
    @SaCheckPermission("als:carryChecklist:edit")
    @Log(title = "任务携行", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody CarryChecklistBo carryChecklistBo) {
        boolean updated = carryChecklistService.update(carryChecklistBo);
        if (!updated) {
            return CommonResult.fail("修改任务携行记录失败!");
        }
        return CommonResult.success();
    }

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