package com.fs.company.controller;

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.ParseUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyVoiceLogs;
import com.fs.company.param.CompanyVoiceLogsParam;
import com.fs.company.service.ICompanyVoiceLogsService;
import com.fs.company.vo.CompanyVoiceLogsExportVO;
import com.fs.company.vo.CompanyVoiceLogsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通话记录Controller
 *
 * @author fs
 * @date 2021-10-04
 */
@RestController
@RequestMapping("/company/companyVoiceLogs")
public class CompanyVoiceLogsController extends BaseController
{
    @Autowired
    private ICompanyVoiceLogsService companyVoiceLogsService;

    /**
     * 查询通话记录列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceLogs:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyVoiceLogsParam param)
    {
        startPage();
        if(!StringUtils.isEmpty(param.getCreateTimeRange())){
            param.setCreateTimeList(param.getCreateTimeRange().split("--"));
        }
        if(!StringUtils.isEmpty(param.getDateRange())){
            param.setStartTimeList(param.getDateRange().split("--"));
        }
        List<CompanyVoiceLogsVO> list = companyVoiceLogsService.selectCompanyVoiceLogsVOList(param);
        for(CompanyVoiceLogsVO vo : list){
            vo.setCalleePhone(ParseUtils.parsePhone(vo.getCalleePhone()));
            vo.setCallerPhone(ParseUtils.parsePhone(vo.getCallerPhone()));
            vo.setDisplayCalleeNumber(ParseUtils.parsePhone(vo.getDisplayCalleeNumber()));
            vo.setDisplayCallerNumber(ParseUtils.parsePhone(vo.getDisplayCallerNumber()));


            if (vo.getRemark()!=null&&vo.getRemark().length()>11){
                StringBuilder replace = new StringBuilder(vo.getRemark()).replace(vo.getRemark().length() - 9, vo.getRemark().length() - 4, "*****");
                vo.setRemark(replace.toString());

            }
        }
        return getDataTable(list);
    }

    /**
     * 导出通话记录列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceLogs:export')")
    @Log(title = "通话记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyVoiceLogsParam param)
    {
        if(!StringUtils.isEmpty(param.getCreateTimeRange())){
            param.setCreateTimeList(param.getCreateTimeRange().split("--"));
        }
        if(!StringUtils.isEmpty(param.getDateRange())){
            param.setStartTimeList(param.getDateRange().split("--"));
        }
        List<CompanyVoiceLogsExportVO> list = companyVoiceLogsService.selectCompanyVoiceLogsExportVOList(param);
        for(CompanyVoiceLogsExportVO vo : list){
            vo.setCalleePhone(ParseUtils.parsePhone(vo.getCalleePhone()));
            vo.setCallerPhone(ParseUtils.parsePhone(vo.getCallerPhone()));
            vo.setDisplayCalleeNumber(ParseUtils.parsePhone(vo.getDisplayCalleeNumber()));
            vo.setDisplayCallerNumber(ParseUtils.parsePhone(vo.getDisplayCallerNumber()));
        }
        ExcelUtil<CompanyVoiceLogsExportVO> util = new ExcelUtil<CompanyVoiceLogsExportVO>(CompanyVoiceLogsExportVO.class);
        return util.exportExcel(list, "通话记录");
    }

    /**
     * 获取通话记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceLogs:query')")
    @GetMapping(value = "/{voiceId}")
    public AjaxResult getInfo(@PathVariable("voiceId") Long voiceId)
    {
        return AjaxResult.success(companyVoiceLogsService.selectCompanyVoiceLogsById(voiceId));
    }

    /**
     * 新增通话记录
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceLogs:add')")
    @Log(title = "通话记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyVoiceLogs companyVoiceLogs)
    {
        return toAjax(companyVoiceLogsService.insertCompanyVoiceLogs(companyVoiceLogs));
    }

    /**
     * 修改通话记录
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceLogs:edit')")
    @Log(title = "通话记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyVoiceLogs companyVoiceLogs)
    {
        return toAjax(companyVoiceLogsService.updateCompanyVoiceLogs(companyVoiceLogs));
    }

    /**
     * 删除通话记录
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceLogs:remove')")
    @Log(title = "通话记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{voiceIds}")
    public AjaxResult remove(@PathVariable Long[] voiceIds)
    {
        return toAjax(companyVoiceLogsService.deleteCompanyVoiceLogsByIds(voiceIds));
    }
}
