package com.fs.company.controller.company;

import com.fs.aicall.domain.TaskInfo;
import com.fs.aicall.domain.apiresult.PushIIntentionResult;
import com.fs.aicall.domain.result.EditDialogResult;
import com.fs.aicall.domain.result.GetairobotResult;
import com.fs.aicall.domain.result.QueryCallTaskInfoResult;
import com.fs.aicall.service.AiCallService;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyVoiceRobotic;
import com.fs.company.domain.CompanyVoiceRoboticCallees;
import com.fs.company.domain.CompanyVoiceRoboticWx;
import com.fs.company.service.ICompanyVoiceRoboticCalleesService;
import com.fs.company.service.ICompanyVoiceRoboticService;
import com.fs.company.service.ICompanyVoiceRoboticWxService;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 机器人外呼任务Controller
 *
 * @author fs
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/company/companyVoiceRobotic")
public class CompanyVoiceRoboticController extends BaseController
{
    @Autowired
    private ICompanyVoiceRoboticService companyVoiceRoboticService;
    @Autowired
    private ICompanyVoiceRoboticCalleesService companyVoiceRoboticCalleesService;
    @Autowired
    private AiCallService aiCallService;
    @Autowired
    private ICompanyVoiceRoboticWxService companyVoiceRoboticWxService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询机器人外呼任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:companyVoiceRobotic:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyVoiceRobotic companyVoiceRobotic){
        startPage();
        List<CompanyVoiceRobotic> list = companyVoiceRoboticService.selectCompanyVoiceRoboticListCompany(companyVoiceRobotic);
        return getDataTable(list);
    }
    /**
     * 查询机器人外呼任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:companyVoiceRobotic:list')")
    @GetMapping("/listAll")
    public R listAll(CompanyVoiceRobotic companyVoiceRobotic){
        return R.ok().put("data", companyVoiceRoboticService.selectCompanyVoiceRoboticListCompany(companyVoiceRobotic));
    }
    @PreAuthorize("@ss.hasPermi('system:companyVoiceRobotic:list')")
    @GetMapping("/calleesList")
    public TableDataInfo calleesList(Long id){
        startPage();
        List<CompanyVoiceRoboticCallees> list = companyVoiceRoboticCalleesService.selectCompanyVoiceRoboticCalleesListByRoboticId(id);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:companyVoiceRobotic:list')")
    @GetMapping("/wxList")
    public TableDataInfo wxList(Long id){
        startPage();
        List<CompanyVoiceRoboticWx> list = companyVoiceRoboticWxService.selectWxList(id);
        return getDataTable(list);
    }

    /**
     * 导出机器人外呼任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:companyVoiceRobotic:export')")
    @Log(title = "机器人外呼任务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyVoiceRobotic companyVoiceRobotic)
    {
        List<CompanyVoiceRobotic> list = companyVoiceRoboticService.selectCompanyVoiceRoboticListCompany(companyVoiceRobotic);
        ExcelUtil<CompanyVoiceRobotic> util = new ExcelUtil<CompanyVoiceRobotic>(CompanyVoiceRobotic.class);
        return util.exportExcel(list, "robotic");
    }

    /**
     * 获取机器人外呼任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:companyVoiceRobotic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(companyVoiceRoboticService.selectCompanyVoiceRoboticById(id));
    }

    /**
     * 新增机器人外呼任务
     */
    @PreAuthorize("@ss.hasPermi('system:companyVoiceRobotic:add')")
    @Log(title = "机器人外呼任务", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody CompanyVoiceRobotic companyVoiceRobotic){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyVoiceRobotic.setCreateUser(loginUser.getUser().getUserId());
        return toAjax(companyVoiceRoboticService.insertCompanyVoiceRobotic(companyVoiceRobotic));
    }

    /**
     * 修改机器人外呼任务
     */
    @PreAuthorize("@ss.hasPermi('system:companyVoiceRobotic:edit')")
    @Log(title = "机器人外呼任务", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody CompanyVoiceRobotic companyVoiceRobotic)
    {
        return toAjax(companyVoiceRoboticService.updateCompanyVoiceRobotic(companyVoiceRobotic));
    }

    /**
     * 删除机器人外呼任务
     */
    @PreAuthorize("@ss.hasPermi('system:companyVoiceRobotic:remove')")
    @Log(title = "机器人外呼任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(companyVoiceRoboticService.deleteCompanyVoiceRoboticByIds(ids));
    }
    /**
     * 删除机器人外呼任务
     */
	@GetMapping("/getTypes")
    public R getTypes(){
        List<GetairobotResult> getairobotlist = aiCallService.getairobotlist();
        List<EditDialogResult> editDialogResults = aiCallService.queryDialog();
        return R.ok().put("robot", getairobotlist).put("dialog", editDialogResults);
    }
    /**
     * 查询状态
     */
    @GetMapping("/statusList")
    public R statusList(String ids){
        List<String> list = Arrays.stream(ids.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
        return R.ok().put("data", list.stream().map(e -> aiCallService.queryCallTaskInfo(TaskInfo.builder().taskID(e).build())).collect(Collectors.toMap(QueryCallTaskInfoResult::getTaskID, e -> e)));
    }
    /**
     * 启动任务
     */
	@GetMapping("/startRobotic")
    public R startRobotic(String taskId){
        aiCallService.startCallTask(TaskInfo.builder().taskID(taskId).build());
        return R.ok();
    }
    /**
     * 停止任务
     */
	@GetMapping("/stopRobotic")
    public R stopRobotic(String taskId){
        aiCallService.stopCallTask(TaskInfo.builder().taskID(taskId).build());
        return R.ok();
    }
    /**
     * 员工列表
     */
	@GetMapping("/companyUserList")
    public R qwUserList(){
        return R.ok().put("data", companyVoiceRoboticService.qwUserListCompany(new CompanyVoiceRobotic()));
    }

    /**
     * 外呼回调
     */
	@PostMapping("/callerResult")
    public R callerResult(@RequestBody PushIIntentionResult result){
        companyVoiceRoboticService.callerResult(result);
        return R.ok();
    }
    /**
     * 外呼回调
     */
	@GetMapping("/test")
    public R test(Long id){
//        companyVoiceRoboticService.test(id);
        companyVoiceRoboticService.dispenseWx(id);
        return R.ok();
    }
}
