package org.eco.als.controller;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import cn.hutool.core.collection.CollUtil;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.eco.common.core.core.domain.CommonResult;
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.core.core.domain.model.LoginUser;
import org.eco.common.web.annotation.RepeatSubmit;
import org.eco.common.excel.utils.ExcelUtil;
import org.eco.common.web.core.BaseController;
import jakarta.annotation.Resource;
import org.eco.als.domain.vo.ResourceAllocationImportVo;
import org.eco.als.domain.vo.ResourceAllocationVo;
import org.eco.als.domain.bo.ResourceAllocationBo;
import org.eco.als.service.IResourceAllocationService;
import org.springframework.web.multipart.MultipartFile;
import org.eco.common.core.core.page.PageResult;

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

/**
 * 维修任务资源分配Controller
 *
 * @author wgk
 * @date 2024-11-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/resourceAllocation")
public class ResourceAllocationController extends BaseController {
    @Resource
    private IResourceAllocationService resourceAllocationService;

    /**
     * 查询维修任务资源分配列表
     */
    @SaCheckPermission("als:resourceAllocation:list")
    @GetMapping("/list")
    public CommonResult<PageResult<ResourceAllocationVo>> list(ResourceAllocationBo resourceAllocationBo) {
        return CommonResult.success(resourceAllocationService.selectPage(resourceAllocationBo));
    }

    /**
     * 导出维修任务资源分配列表
     */
    @SaCheckPermission("als:resourceAllocation:export")
    @Log(title = "维修任务资源分配", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(ResourceAllocationBo resourceAllocationBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<ResourceAllocationVo> list = resourceAllocationService.selectList(resourceAllocationBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        resourceAllocationService.asyncExport(list, "维修任务资源分配", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("als:resourceAllocation:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "维修任务资源分配", ResourceAllocationImportVo.class, response);
    }

    /**
     * 导入维修任务资源分配列表
     */
    @Log(title = "维修任务资源分配", businessType = BusinessType.IMPORT)
    @SaCheckPermission("als:resourceAllocation:import")
    @PostMapping("/importData")
    public CommonResult<Void> importData(MultipartFile file, boolean updateSupport) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        resourceAllocationService.asyncImportData(file, updateSupport, loginUser);
        return CommonResult.success();
    }

    /**
     * 获取维修任务资源分配详细信息
     */
    @SaCheckPermission("als:resourceAllocation:query")
    @GetMapping(value = "/{id}")
    public CommonResult<ResourceAllocationVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(resourceAllocationService.selectById(id));
    }

    /**
     * 新增维修任务资源分配
     */
    @SaCheckPermission("als:resourceAllocation:add")
    @Log(title = "维修任务资源分配", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody ResourceAllocationBo resourceAllocationBo) {
        boolean inserted = resourceAllocationService.insert(resourceAllocationBo);
        if (!inserted) {
            return CommonResult.fail("新增维修任务资源分配记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改维修任务资源分配
     */
    @SaCheckPermission("als:resourceAllocation:edit")
    @Log(title = "维修任务资源分配", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody ResourceAllocationBo resourceAllocationBo) {
        boolean updated = resourceAllocationService.update(resourceAllocationBo);
        if (!updated) {
            return CommonResult.fail("修改维修任务资源分配记录失败!");
        }
        return CommonResult.success();
    }

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