package com.fs.company.controller;

import com.fs.common.annotation.DataScope;
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.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 com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.crm.vo.CrmLineCustomerListQueryVO;
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;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询通话记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CompanyVoiceLogsParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        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);
        if (list != null) {
            for (CompanyVoiceLogsVO vo : list) {
                if(vo.getDisplayCalleeNumber()!=null){
                    vo.setDisplayCalleeNumber(vo.getDisplayCalleeNumber().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }
                if(vo.getCalleePhone()!=null){
                    vo.setDisplayCalleeNumber(vo.getDisplayCalleeNumber().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出通话记录列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceLogs:export')")
    @Log(title = "通话记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @DataScope(deptAlias = "u",userAlias = "u")
    public AjaxResult export(CompanyVoiceLogsParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        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) {
            if(vo.getDisplayCalleeNumber()!=null){
                vo.setDisplayCalleeNumber(vo.getDisplayCalleeNumber().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
            if(vo.getCalleePhone()!=null){
                vo.setDisplayCalleeNumber(vo.getDisplayCalleeNumber().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }

        }
        ExcelUtil<CompanyVoiceLogsExportVO> util = new ExcelUtil<CompanyVoiceLogsExportVO>(CompanyVoiceLogsExportVO.class);
        return util.exportExcel(list, "通话记录");

    }
}
