package com.ruoyi.ibs.customerselect.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ibs.customerselect.service.CustCsvTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @Author 吴凯程
 * @Date 2025/11/28
 **/
@Api(tags = "CSV客户标签")
@RestController
@RequestMapping("/csv/tag")
public class CustCsvTagController extends BaseController {

    @Resource
    private CustCsvTagService custCsvTagService;

    @PostMapping("/upload")
    @Log(title = "CSV客户标签-上传CSV文件并更新")
    @ApiOperation("上传CSV文件并更新")
    public AjaxResult uploadCsv(MultipartFile file) {
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        if (!suffix.equals("csv")) {
            return AjaxResult.error("文件格式错误，请上传CSV文件");
        }
        custCsvTagService.processCsvInBatches(file, SecurityUtils.getUsername(), SecurityUtils.getHeadId());
        return AjaxResult.success("文件完成上传，开始更新标签");
    }

    @GetMapping("/search")
    @Log(title = "CSV客户标签-查询客户标签")
    @ApiOperation("查询客户标签")
    public AjaxResult searchCsv(@RequestParam String custIdc) {
        return AjaxResult.success(custCsvTagService.getCsvTagJson(custIdc));
    }
}
