package org.dromara.work.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.work.domain.bo.TpClientStaffBo;
import org.dromara.work.domain.vo.TpClientStaffVo;
import org.dromara.work.service.ITpClientStaffService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客服客户管理
 *
 * @author Maosw
 * @date 2024-08-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/clientStaff")
public class TpClientStaffController extends BaseController {

    private final ITpClientStaffService tpClientStaffService;

    /**
     * 查询客服客户管理列表
     */
    @SaCheckPermission("work:clientStaff:list")
    @GetMapping("/list")
    public TableDataInfo<TpClientStaffVo> list(TpClientStaffBo bo, PageQuery pageQuery) {
        return tpClientStaffService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出客服客户列表
     */
    @SaCheckPermission("work:clientStaff:export")
    @Log(title = "导出客服客户列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpClientStaffBo bo, HttpServletResponse response) {
        List<TpClientStaffVo> list = tpClientStaffService.queryList(bo);
        ExcelUtil.exportExcel(list, "导出客服客户列表", TpClientStaffVo.class, response);
    }

    /**
     * 获取客服客户管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:clientStaff:query")
    @GetMapping("/{id}")
    public R<TpClientStaffVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpClientStaffService.queryById(id));
    }

    /**
     * 新增客服客户管理
     */
    @SaCheckPermission("work:clientStaff:add")
    @Log(title = "客服客户管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpClientStaffBo bo) {
        return toAjax(tpClientStaffService.insertByBo(bo));
    }

    /**
     * 修改客服客户管理
     */
    @SaCheckPermission("work:clientStaff:edit")
    @Log(title = "客服客户管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpClientStaffBo bo) {
        return toAjax(tpClientStaffService.updateByBo(bo));
    }

    /**
     * 删除客服客户管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("work:clientStaff:remove")
    @Log(title = "客服客户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tpClientStaffService.deleteWithValidByIds(List.of(ids), true));
    }
}
