package org.dromara.erp.controller;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.erp.domain.query.ErpClientQuery;
import org.dromara.erp.domain.vo.ErpClientVo;
import org.dromara.erp.service.IErpClientService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * ERP 客户档案控制器
 *
 * @Author: Antigravity
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erp/client")
public class ErpClientController {

    private final IErpClientService erpClientService;

    /**
     * 分页查询客户列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpClientVo> list(ErpClientQuery query, PageQuery pageQuery) {
        return erpClientService.queryPageList(query, pageQuery);
    }

    /**
     * 根据名称模糊检索客户（全量，不分页）
     */
    @GetMapping("/search")
    public R<List<ErpClientVo>> search(String name) {
        return R.ok(erpClientService.searchByName(name));
    }

    /**
     * 获取客户详情
     */
    @GetMapping("/{rowId}")
    public R<ErpClientVo> getInfo(@PathVariable("rowId") String rowId) {
        return R.ok(erpClientService.queryById(rowId));
    }

    /**
     * 根据 ID 列表批量查询客户
     */
    @GetMapping("/listByIds")
    public R<List<ErpClientVo>> listByIds(@RequestParam("rowIds") String rowIds) {
        List<String> ids = Arrays.asList(rowIds.split(","));
        return R.ok(erpClientService.queryByIds(ids));
    }

}
