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.ContactsBo;
import org.eco.als.domain.vo.ContactsImportVo;
import org.eco.als.domain.vo.ContactsVo;
import org.eco.als.service.IContactsService;
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-11-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/contacts")
public class ContactsController extends BaseController {
    @Resource
    private IContactsService contactsService;

    /**
     * 查询通讯录列表
     */
    @SaCheckPermission("als:contacts:list")
    @GetMapping("/list")
    public CommonResult<PageResult<ContactsVo>> list(ContactsBo contactsBo) {
        return CommonResult.success(contactsService.selectPage(contactsBo));
    }

    /**
     * 导出通讯录列表
     */
    @SaCheckPermission("als:contacts:export")
    @Log(title = "通讯录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(ContactsBo contactsBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<ContactsVo> list = contactsService.selectList(contactsBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        contactsService.asyncExport(list, "通讯录", loginUser);
        return CommonResult.success();
    }

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

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

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

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

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

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