package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.CustomerServiceReply;
import com.ruoyi.system.service.ICustomerServiceReplyService;

/**
 * 客服回复规则App接口Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/system/customerServiceReply/app")
public class CustomerServiceReplyAppController extends BaseController
{
    @Autowired
    private ICustomerServiceReplyService customerServiceReplyService;

    /**
     * 根据用户输入获取最佳回复
     */
    @PostMapping("/getReply")
    public AjaxResult getReply(@RequestBody CustomerServiceReply customerServiceReply)
    {
        try {
            String userInput = customerServiceReply.getKeyword();
            if (userInput == null || userInput.trim().isEmpty()) {
                return AjaxResult.error("用户输入不能为空");
            }
            
            CustomerServiceReply reply = customerServiceReplyService.getBestReply(userInput.trim());
            return AjaxResult.success(reply);
        } catch (Exception e) {
            logger.error("获取回复失败", e);
            return AjaxResult.error("获取回复失败：" + e.getMessage());
        }
    }

    /**
     * 根据用户输入获取最佳回复（GET方式）
     */
    @GetMapping("/getReply")
    public AjaxResult getReplyGet(@RequestParam("keyword") String keyword)
    {
        try {
            if (keyword == null || keyword.trim().isEmpty()) {
                return AjaxResult.error("用户输入不能为空");
            }
            
            CustomerServiceReply reply = customerServiceReplyService.getBestReply(keyword.trim());
            return AjaxResult.success(reply);
        } catch (Exception e) {
            logger.error("获取回复失败", e);
            return AjaxResult.error("获取回复失败：" + e.getMessage());
        }
    }

    /**
     * 根据关键字查询回复列表
     */
    @PostMapping("/getReplyList")
    public AjaxResult getReplyList(@RequestBody CustomerServiceReply customerServiceReply)
    {
        try {
            String keyword = customerServiceReply.getKeyword();
            if (keyword == null || keyword.trim().isEmpty()) {
                return AjaxResult.error("关键字不能为空");
            }
            
            List<CustomerServiceReply> list = customerServiceReplyService.selectCustomerServiceReplyByKeyword(keyword.trim());
            return AjaxResult.success(list);
        } catch (Exception e) {
            logger.error("查询回复列表失败", e);
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据关键字查询回复列表（GET方式）
     */
    @GetMapping("/getReplyList")
    public AjaxResult getReplyListGet(@RequestParam("keyword") String keyword, 
                                     @RequestParam(value = "limit", defaultValue = "10") Integer limit)
    {
        try {
            if (keyword == null || keyword.trim().isEmpty()) {
                return AjaxResult.error("关键字不能为空");
            }
            
            List<CustomerServiceReply> list = customerServiceReplyService.selectCustomerServiceReplyByKeyword(keyword.trim());
            
            // 限制返回数量
            if (limit != null && limit > 0 && list.size() > limit) {
                list = list.subList(0, limit);
            }
            
            return AjaxResult.success(list);
        } catch (Exception e) {
            logger.error("查询回复列表失败", e);
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取所有客服回复规则列表
     */
    @GetMapping("/list")
    public AjaxResult list()
    {
        try {
            CustomerServiceReply query = new CustomerServiceReply();
            query.setIsActive(1); // 只查询启用状态的数据
            List<CustomerServiceReply> list = customerServiceReplyService.selectCustomerServiceReplyList(query);
            return AjaxResult.success(list);
        } catch (Exception e) {
            logger.error("查询客服回复规则列表失败", e);
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据分类获取客服回复规则列表
     */
    @GetMapping("/listByCategory")
    public AjaxResult listByCategory(@RequestParam("category") String category)
    {
        try {
            CustomerServiceReply query = new CustomerServiceReply();
            query.setIsActive(1); // 只查询启用状态的数据
            query.setCategory(category);
            List<CustomerServiceReply> list = customerServiceReplyService.selectCustomerServiceReplyList(query);
            return AjaxResult.success(list);
        } catch (Exception e) {
            logger.error("根据分类查询客服回复规则列表失败", e);
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取默认回复
     */
    @GetMapping("/getDefaultReply")
    public AjaxResult getDefaultReply()
    {
        try {
            CustomerServiceReply reply = customerServiceReplyService.selectDefaultReply();
            return AjaxResult.success(reply);
        } catch (Exception e) {
            logger.error("获取默认回复失败", e);
            return AjaxResult.error("获取默认回复失败：" + e.getMessage());
        }
    }
} 