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.CommunicationBo;
import org.eco.als.domain.vo.CommunicationImportVo;
import org.eco.als.domain.vo.CommunicationVo;
import org.eco.als.service.ICommunicationService;
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.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 通讯管理Controller
 *
 * @author wgk
 * @date 2024-12-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/communication")
public class CommunicationController extends BaseController {
    @Resource
    private ICommunicationService communicationService;

    /**
     * 查询通讯管理列表
     */
    @SaCheckPermission("als:communication:list")
    @GetMapping("/list")
    public CommonResult<PageResult<CommunicationVo>> list(CommunicationBo communicationBo) {
        return CommonResult.success(communicationService.selectPage(communicationBo));
    }

    /**
     * 导出通讯管理列表
     */
    @SaCheckPermission("als:communication:export")
    @Log(title = "通讯管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(CommunicationBo communicationBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<CommunicationVo> list = communicationService.selectList(communicationBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        communicationService.asyncExport(list, "通讯管理", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("als:communication:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "通讯管理", CommunicationImportVo.class, response);
    }

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

    /**
     * 获取通讯管理详细信息
     */
    @SaCheckPermission("als:communication:query")
    @GetMapping(value = "/{id}")
    public CommonResult<CommunicationVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(communicationService.selectById(id));
    }

    /**
     * 新增通讯管理
     */
    @SaCheckPermission("als:communication:add")
    @Log(title = "通讯管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody CommunicationBo communicationBo) {
        boolean inserted = communicationService.insert(communicationBo);
        if (!inserted) {
            return CommonResult.fail("新增通讯管理记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改通讯管理
     */
    @SaCheckPermission("als:communication:edit")
    @Log(title = "通讯管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody CommunicationBo communicationBo) {
        boolean updated = communicationService.update(communicationBo);
        if (!updated) {
            return CommonResult.fail("修改通讯管理记录失败!");
        }
        return CommonResult.success();
    }

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