package com.fs.company.controller;

import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.company.domain.CompanyUser;
import com.fs.company.param.CompanyVoiceCallerImportParam;
import com.fs.company.param.CompanyVoiceCallerParam;
import com.fs.company.param.CompanyVoiceCallerRecoverParam;
import com.fs.company.vo.CompanyVoiceCallerListVO;
import com.fs.core.security.LoginUser;
import com.fs.crm.param.CrmCustomeRecoverParam;
import com.fs.crm.param.CrmLineCustomerImportParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.company.domain.CompanyVoiceCaller;
import com.fs.company.service.ICompanyVoiceCallerService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 企业用户坐席Controller
 *
 * @author fs
 * @date 2023-02-23
 */
@RestController
@RequestMapping("/company/companyVoiceCaller")
public class CompanyVoiceCallerController extends BaseController
{
    @Autowired
    private ICompanyVoiceCallerService companyVoiceCallerService;

    /**
     * 查询企业用户坐席列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceCaller:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyVoiceCallerParam param)
    {
        startPage();
        List<CompanyVoiceCallerListVO> list = companyVoiceCallerService.selectCompanyVoiceCallerListVO(param);
        for(CompanyVoiceCallerListVO vo :list){
            vo.setMobile(ParseUtils.parsePhone(vo.getMobile()));
        }
        return getDataTable(list);
    }

    /**
     * 导出企业用户坐席列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceCaller:export')")
    @Log(title = "企业用户坐席", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyVoiceCallerParam param)
    {
        List<CompanyVoiceCallerListVO> list = companyVoiceCallerService.selectCompanyVoiceCallerListVO(param);
        ExcelUtil<CompanyVoiceCallerListVO> util = new ExcelUtil<CompanyVoiceCallerListVO>(CompanyVoiceCallerListVO.class);
        return util.exportExcel(list, "companyVoiceCaller");
    }

    /**
     * 获取企业用户坐席详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceCaller:query')")
    @GetMapping(value = "/{callingId}")
    public AjaxResult getInfo(@PathVariable("callingId") Long callingId)
    {
        return AjaxResult.success(companyVoiceCallerService.selectCompanyVoiceCallerById(callingId));
    }

    /**
     * 新增企业用户坐席
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceCaller:add')")
    @Log(title = "企业用户坐席", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyVoiceCaller companyVoiceCaller)
    {
        return toAjax(companyVoiceCallerService.insertCompanyVoiceCaller(companyVoiceCaller));
    }

    /**
     * 修改企业用户坐席
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceCaller:edit')")
    @Log(title = "企业用户坐席", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyVoiceCaller companyVoiceCaller)
    {
        return toAjax(companyVoiceCallerService.updateCompanyVoiceCaller(companyVoiceCaller));
    }

    /**
     * 删除企业用户坐席
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceCaller:remove')")
    @Log(title = "企业用户坐席", businessType = BusinessType.DELETE)
	@DeleteMapping("/{callingIds}")
    public AjaxResult remove(@PathVariable Long[] callingIds)
    {
        return toAjax(companyVoiceCallerService.deleteCompanyVoiceCallerByIds(callingIds));
    }


    @Log(title = "导入分机号", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('company:companyVoiceCaller:import')")
    @PostMapping("/importData")
    public AjaxResult importLineData(MultipartFile file) throws Exception
    {
        ExcelUtil<CompanyVoiceCallerImportParam> util = new ExcelUtil<CompanyVoiceCallerImportParam>(CompanyVoiceCallerImportParam.class);
        List<CompanyVoiceCallerImportParam> list = util.importExcel(file.getInputStream());
        String message = companyVoiceCallerService.importCompanyVoiceCaller(list);
        return AjaxResult.success(message);
    }
    //下载模板
    @GetMapping("/importTemplate")
    public AjaxResult importLineTemplate()
    {
        ExcelUtil<CompanyVoiceCallerImportParam> util = new ExcelUtil<CompanyVoiceCallerImportParam>(CompanyVoiceCallerImportParam.class);
        return util.importTemplateExcel("分机号数据");
    }

    //回收
    @PostMapping("/recover")
    public R recover(@RequestBody CompanyVoiceCallerRecoverParam param)
    {
        CompanyVoiceCaller caller=companyVoiceCallerService.selectCompanyVoiceCallerById(param.getCallerId());
        caller.setCompanyId(0l);
        caller.setCompanyUserId(0l);
        caller.setMobile("");
        caller.setStatus(1);
        companyVoiceCallerService.updateCompanyVoiceCaller(caller);
        return R.ok();

    }
}
