package com.fs.company.controller.aiSipCall;

import com.fs.aiSipCall.domain.AiSipCallUser;
import com.fs.aiSipCall.service.IAiSipCallUserService;
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.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
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.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * sip用户信息Controller
 * 
 * @author fs
 * @date 2026-03-13
 */
@RestController
@RequestMapping("/company/aiSipCall/aiSipCallUser")
public class AiSipCallUserController extends BaseController
{
    @Autowired
    private IAiSipCallUserService aiSipCallUserService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询sip用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('company:aiSipCall:aiSipCallUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(AiSipCallUser aiSipCallUser)
    {
        startPage();
        List<AiSipCallUser> list = aiSipCallUserService.selectAiSipCallUserList(aiSipCallUser);
        return getDataTable(list);
    }

    /**
     * 导出sip用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('company:aiSipCall:aiSipCallUser:export')")
    @Log(title = "sip用户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AiSipCallUser aiSipCallUser)
    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        aiSipCallUser.setCompanyId(loginUser.getCompany().getCompanyId());
        List<AiSipCallUser> list = aiSipCallUserService.selectAiSipCallUserList(aiSipCallUser);
        ExcelUtil<AiSipCallUser> util = new ExcelUtil<>(AiSipCallUser.class);
        return util.exportExcel(list, "sip用户信息数据");
    }

    /**
     * 获取sip用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:aiSipCall:aiSipCallUser:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(aiSipCallUserService.selectAiSipCallUserByUserId(userId));
    }

    /**
     * 新增sip用户信息
     */
    @PreAuthorize("@ss.hasPermi('company:aiSipCall:aiSipCallUser:add')")
    @Log(title = "sip用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AiSipCallUser aiSipCallUser)
    {
        if(aiSipCallUser.getCompanyUserId() == null){
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            aiSipCallUser.setCompanyId(loginUser.getCompany().getCompanyId());
            aiSipCallUser.setCompanyUserId(loginUser.getUser().getUserId());
            aiSipCallUser.setCreateBy(loginUser.getUser().getUserName());
        }
        aiSipCallUser.setCreateTime(new Date());
        return toAjax(aiSipCallUserService.insertAiSipCallUser(aiSipCallUser));
    }

    /**
     * 修改sip用户信息
     */
    @PreAuthorize("@ss.hasPermi('company:aiSipCall:aiSipCallUser:edit')")
    @Log(title = "sip用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AiSipCallUser aiSipCallUser)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        aiSipCallUser.setUpdateBy(loginUser.getUser().getUserName());
        aiSipCallUser.setUpdateTime(new Date());
        return toAjax(aiSipCallUserService.updateAiSipCallUser(aiSipCallUser));
    }

    /**
     * 删除sip用户信息
     */
    @PreAuthorize("@ss.hasPermi('company:aiSipCall:aiSipCallUser:remove')")
    @Log(title = "sip用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(aiSipCallUserService.deleteAiSipCallUserByUserIds(userIds));
    }

    /**
     * 获取未绑定的分机列表
     */
    @GetMapping("/getUnBindExtnum")
    public AjaxResult getUnBindExtnum()
    {
        return aiSipCallUserService.getUnBindExtnum();
    }

    /**
     * 查询登录销售的sip用户信息列表
     */
    @GetMapping("/myCallUser")
    public AjaxResult myCallUser(AiSipCallUser aiSipCallUser)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        aiSipCallUser.setCompanyUserId(loginUser.getUser().getUserId());
        startPage();
        List<AiSipCallUser> list = aiSipCallUserService.selectAiSipCallUserList(aiSipCallUser);
        if(!list.isEmpty()){
            return AjaxResult.success(list.get(0));
        }else{
            return AjaxResult.error("未创建sip角色");
        }
    }

    /**
     * 查询aiSIP工具条基础配置参数
     * @param param 参数
     * @return AjaxResult 结果
     */
    @PostMapping("/getToolbarBasicParam")
    public AjaxResult getToolbarBasicParam(@RequestBody Map<String,String> param)
    {
        String extNum = param.get("extNum");
        if(extNum == null){
            return AjaxResult.error("分机号参数缺失");
        }
        return aiSipCallUserService.getToolbarBasicParam(param);
    }

}
