package com.fs.company.controller.aiSipCall;

import com.fs.aiSipCall.domain.AiSipCallPhone;
import com.fs.aiSipCall.service.AsyncAddSipCallRecordService;
import com.fs.aiSipCall.service.IAiSipCallPhoneService;
import com.fs.aiSipCall.utils.DateUtils;
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.Calendar;
import java.util.List;

/**
 * aiSIP外呼通话记录Controller
 *
 * @author fs
 * @date 2026-03-06
 */
@RestController
@RequestMapping("/company/aiSipCall/phone")
public class AiSipCallPhoneController extends BaseController
{
    @Autowired
    private IAiSipCallPhoneService aiSipCallPhoneService;
    @Autowired
    private AsyncAddSipCallRecordService asyncAddSipCallRecordService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询aiSIP外呼通话记录列表
     */
    @PreAuthorize("@ss.hasPermi('company:aiSipCall:phone:list')")
    @GetMapping("/list")
    public TableDataInfo list(AiSipCallPhone aiSipCallPhone)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        aiSipCallPhone.setCompanyId(loginUser.getUser().getCompanyId());
        aiSipCallPhone.setCompanyUserId(loginUser.getUser().getUserId());
        startPage();
        List<AiSipCallPhone> list = aiSipCallPhoneService.selectAiSipCallPhoneList(aiSipCallPhone);
        return getDataTable(list);
    }


    /**
     * 导出aiSIP外呼通话记录列表
     */
    @PreAuthorize("@ss.hasPermi('company:aiSipCall:phone:export')")
    @Log(title = "aiSIP外呼通话记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AiSipCallPhone aiSipCallPhone)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        aiSipCallPhone.setCompanyId(loginUser.getUser().getCompanyId());
        aiSipCallPhone.setCompanyUserId(loginUser.getUser().getUserId());
        List<AiSipCallPhone> list = aiSipCallPhoneService.selectAiSipCallPhoneList(aiSipCallPhone);
        ExcelUtil<AiSipCallPhone> util = new ExcelUtil<>(AiSipCallPhone.class);
        return util.exportExcel(list, "aiSIP外呼通话记录数据");
    }

    /**
     * 获取aiSIP外呼通话记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:aiSipCall:phone:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(aiSipCallPhoneService.selectAiSipCallPhoneById(id));
    }

    /**
     * 新增aiSIP外呼通话记录
     */
    @PreAuthorize("@ss.hasPermi('company:aiSipCall:phone:add')")
    @Log(title = "aiSIP外呼通话记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AiSipCallPhone aiSipCallPhone)
    {
        return toAjax(aiSipCallPhoneService.insertAiSipCallPhone(aiSipCallPhone));
    }

    /**
     * 修改aiSIP外呼通话记录
     */
    @PreAuthorize("@ss.hasPermi('company:aiSipCall:phone:edit')")
    @Log(title = "aiSIP外呼通话记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AiSipCallPhone aiSipCallPhone)
    {
        return toAjax(aiSipCallPhoneService.updateAiSipCallPhone(aiSipCallPhone));
    }

    /**
     * 删除aiSIP外呼通话记录
     */
    @PreAuthorize("@ss.hasPermi('company:aiSipCall:phone:remove')")
    @Log(title = "aiSIP外呼通话记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(aiSipCallPhoneService.deleteAiSipCallPhoneByIds(ids));
    }

    /**
     * 手动同步记录
     */
    @GetMapping("/manualPull")
    public AjaxResult manualPull()
    {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String startTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", calendar.getTime());

        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        String endTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", calendar.getTime());
        asyncAddSipCallRecordService.getAutomaticCallRecord(startTime,endTime,"manualPull");
        return success();
    }
}
