package com.fs.company.controller.company;

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.ServletUtils;
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.CompanyVoiceLogsVO;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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());
        List<CompanyVoiceLogsVO> list = companyVoiceLogsService.selectCompanyVoiceLogsVOList(param);
        if (list != null) {
            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() - 8, vo.getRemark().length() - 4, "*****");
                    vo.setRemark(replace.toString());
                }
                if (vo.getVoiceTitle()!=null&&vo.getVoiceTitle().length()>11){
                    StringBuilder replace = new StringBuilder(vo.getVoiceTitle()).replace(vo.getVoiceTitle().length() - 8, vo.getVoiceTitle().length() - 4, "*****");
                    vo.setVoiceTitle(replace.toString());
                }
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出通话记录列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceLogs:export')")
    @Log(title = "通话记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyVoiceLogs companyVoiceLogs)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        companyVoiceLogs.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyVoiceLogs> list = companyVoiceLogsService.selectCompanyVoiceLogsList(companyVoiceLogs);
        for (CompanyVoiceLogs 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() - 8, vo.getRemark().length() - 4, "*****");
                vo.setRemark(replace.toString());
            }
            if (vo.getVoiceTitle()!=null&&vo.getVoiceTitle().length()>11){
                StringBuilder replace = new StringBuilder(vo.getVoiceTitle()).replace(vo.getVoiceTitle().length() - 8, vo.getVoiceTitle().length() - 4, "*****");
                vo.setVoiceTitle(replace.toString());
            }
        }
        ExcelUtil<CompanyVoiceLogs> util = new ExcelUtil<CompanyVoiceLogs>(CompanyVoiceLogs.class);
        return util.exportExcel(list, "companyVoiceLogs");
    }
}
