package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.AiIntelligentAssistant;
import shop.alien.entity.store.StoreCustomerService;
import shop.alien.store.service.StoreCustomerServiceService;

import java.util.List;

/**
 * <p>
 * 智能客服
 * </p>
 *
 * @author ssk
 * @since 2024-12-30
 */
@Api(tags = {"二期-智能客服"})
@Slf4j
@ApiSort(10)
@CrossOrigin
@RestController
@RequestMapping("/storeCustomerService")
@RequiredArgsConstructor
public class StoreCustomerServiceController {

    private final StoreCustomerServiceService storeCustomerServiceService;

    @ApiOperation("通过关键字获取答案")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({@ApiImplicitParam(name = "question", value = "问题关键字", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/getByQuestion")
    public R<StoreCustomerService> getByQuestion(@RequestParam String question) {
        StoreCustomerService result = storeCustomerServiceService.getByQuestion(question);
        if (null == result) {
            return R.fail("暂未搜索到相应答案");
        }
        return R.data(result);
    }

    @ApiOperation("获取随机问题")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "类型(1/商家/2用户)", dataType = "String", paramType = "query", required = true)
    , @ApiImplicitParam(name = "limit", value = "数量", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getRandList")
    public R<List<StoreCustomerService>> getRandList(@RequestParam String type, @RequestParam Integer limit) {
        List<StoreCustomerService> result = storeCustomerServiceService.getRandList(type,limit);
        return R.data(result);
    }

    @ApiOperation("删除问题")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
            })
    @GetMapping("/delStoreCustomerService")
    public R delStoreCustomerService(@RequestParam String id) {
        storeCustomerServiceService.delStoreCustomerService(id);
        return R.data("删除成功");
    }

    @ApiOperation("中台-问题列表")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "question", value = "问题", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "type", value = "类型(1/商家/2用户)", dataType = "String", paramType = "query", required = false),
    })
    @GetMapping("/getStoreCustomerServicePage")
    private R<IPage<StoreCustomerService>> getStoreCustomerServicePage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                       @RequestParam(value = "size", defaultValue = "10") int size,
                                                       @RequestParam(value = "question", required = false) String question,
                                                       @RequestParam(value = "type", required = false) String type) {
        log.info("StoreCustomerServiceController.getStoreCustomerServicePage?page={},size={},question={},type={}", page, size, question, type);
        return R.data(storeCustomerServiceService.getStoreCustomerServicePage(page, size, question, type));
    }

    @ApiOperation("中台-保存问题")
    @ApiOperationSupport(order = 5)
    @PostMapping("/saveStoreCustomerService")
    public R<StoreCustomerService> saveStoreCustomerService(@RequestBody StoreCustomerService storeCustomerService) {
        log.info("StoreCustomerServiceController.saveStoreCustomerService?storeCustomerService={}", storeCustomerService.toString());
        StoreCustomerService saved = storeCustomerServiceService.saveStoreCustomerService(storeCustomerService);
        return R.data(saved);
    }

    @ApiOperation("id查询问题")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getById")
    public R<StoreCustomerService> getById(@RequestParam String id) {
        log.info("StoreCustomerServiceController.getById?id={}", id);
        StoreCustomerService saved = storeCustomerServiceService.getById(id);
        return R.data(saved);
    }

    @ApiOperation("保存聊天")
    @ApiOperationSupport(order = 5)
    @PostMapping("/saveAiIntelligentAssistant")
    public R<List<AiIntelligentAssistant>> saveAiIntelligentAssistant(@RequestBody List<AiIntelligentAssistant> aiIntelligentAssistants) {
        log.info("StoreCustomerServiceController.saveAiIntelligentAssistant?aiIntelligentAssistants={}", aiIntelligentAssistants.toString());
        List<AiIntelligentAssistant> saved = storeCustomerServiceService.saveAiIntelligentAssistant(aiIntelligentAssistants);
        return R.data(saved);
    }

    @ApiOperation("查询聊天记录")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id)", dataType = "String", paramType = "query", required = true)
            , @ApiImplicitParam(name = "time", value = "时间", dataType = "String", paramType = "query", required = true)
            , @ApiImplicitParam(name = "talkSource", value = "1平台使用/2商户运营", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/selectAiIntelligentAssistant")
    public R<List<AiIntelligentAssistant>> selectAiIntelligentAssistant(@RequestParam String userId, @RequestParam String time,@RequestParam Integer talkSource) {
        List<AiIntelligentAssistant> result = storeCustomerServiceService.selectAiIntelligentAssistant(userId,time,talkSource);
        return R.data(result);
    }
}
