package com.fs.company.controller;

import cn.hutool.json.JSONUtil;
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.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.config.CompanyVoiceCalleeConfig;
import com.fs.company.config.CompanyVoiceCallerConfig;
import com.fs.company.domain.CompanyVoiceConfig;
import com.fs.company.param.CompanyVoiceConfigParam;
import com.fs.company.service.ICompanyService;
import com.fs.company.service.ICompanyVoiceConfigService;
import com.fs.company.vo.CompanyVoiceConfigListVO;
import com.fs.company.vo.CompanyVoiceConfigVO;
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-11-13
 */
@RestController
@RequestMapping("/company/companyVoiceConfig")
public class CompanyVoiceConfigController extends BaseController
{
    @Autowired
    private ICompanyVoiceConfigService companyVoiceConfigService;
    @Autowired
    private ICompanyService companyService;
    /**
     * 查询呼叫频率配置列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyVoiceConfig companyVoiceConfig)
    {
        startPage();
        List<CompanyVoiceConfigListVO> list = companyVoiceConfigService.selectCompanyVoiceConfigListVO(companyVoiceConfig);
        return getDataTable(list);
    }

    /**
     * 导出呼叫频率配置列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceConfig:export')")
    @Log(title = "呼叫频率配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyVoiceConfig companyVoiceConfig)
    {
        List<CompanyVoiceConfig> list = companyVoiceConfigService.selectCompanyVoiceConfigList(companyVoiceConfig);
        ExcelUtil<CompanyVoiceConfig> util = new ExcelUtil<CompanyVoiceConfig>(CompanyVoiceConfig.class);
        return util.exportExcel(list, "companyVoiceConfig");
    }

    /**
     * 获取呼叫频率配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceConfig:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId)
    {
        CompanyVoiceConfig config=companyVoiceConfigService.selectCompanyVoiceConfigById(configId);
        CompanyVoiceCalleeConfig voiceCalleeConfigBO=JSONUtil.toBean(config.getCalleeJson(), CompanyVoiceCalleeConfig.class);
        CompanyVoiceCallerConfig voiceCallerConfigBO=JSONUtil.toBean(config.getCallerJson(), CompanyVoiceCallerConfig.class);


        CompanyVoiceConfigVO vo=new CompanyVoiceConfigVO();
        vo.setCompanyId(config.getCompanyId());
        vo.setConfigId(config.getConfigId());
        vo.setCalleeDay(voiceCalleeConfigBO.getCalleeDay());
        vo.setCalleeHour(voiceCalleeConfigBO.getCalleeHour());
        vo.setCalleeWeek(voiceCalleeConfigBO.getCalleeWeek());
        vo.setCalleeMinute(voiceCalleeConfigBO.getCalleeMinute());
        vo.setCalleeMonth(voiceCalleeConfigBO.getCalleeMonth());

        vo.setCallerDay(voiceCallerConfigBO.getCallerDay());
        vo.setCallerHour(voiceCallerConfigBO.getCallerHour());
        vo.setCallerWeek(voiceCallerConfigBO.getCallerWeek());
        vo.setCallerMonth(voiceCallerConfigBO.getCallerMonth());
        vo.setCallerMinute(voiceCallerConfigBO.getCallerMinute());

        return AjaxResult.success(vo);
    }

    /**
     * 新增呼叫频率配置
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceConfig:add')")
    @Log(title = "呼叫频率配置", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody CompanyVoiceConfigParam companyVoiceConfig)
    {
        //查询企业是否存在
        CompanyVoiceConfig config=companyVoiceConfigService.selectCompanyVoiceConfigByCompanyId(companyVoiceConfig.getCompanyId());
        if(config!=null){
            return R.error("企业已存在");
        }
        CompanyVoiceCalleeConfig voiceCalleeConfigBO=new CompanyVoiceCalleeConfig();
        voiceCalleeConfigBO.setCalleeDay(companyVoiceConfig.getCalleeDay());
        voiceCalleeConfigBO.setCalleeHour(companyVoiceConfig.getCalleeHour());
        voiceCalleeConfigBO.setCalleeMinute(companyVoiceConfig.getCalleeMinute());
        voiceCalleeConfigBO.setCalleeMonth(companyVoiceConfig.getCalleeMonth());
        voiceCalleeConfigBO.setCalleeWeek(companyVoiceConfig.getCalleeWeek());

        CompanyVoiceCallerConfig voiceCallerConfigBO=new CompanyVoiceCallerConfig();
        voiceCallerConfigBO.setCallerDay(companyVoiceConfig.getCallerDay());
        voiceCallerConfigBO.setCallerHour(companyVoiceConfig.getCallerHour());
        voiceCallerConfigBO.setCallerMinute(companyVoiceConfig.getCallerMinute());
        voiceCallerConfigBO.setCallerWeek(companyVoiceConfig.getCallerWeek());
        voiceCallerConfigBO.setCallerMonth(companyVoiceConfig.getCallerMonth());
        config=new CompanyVoiceConfig();
        config.setCompanyId(companyVoiceConfig.getCompanyId());
        config.setCalleeJson(JSONUtil.toJsonStr(voiceCalleeConfigBO));
        config.setCallerJson(JSONUtil.toJsonStr(voiceCallerConfigBO));
        companyVoiceConfigService.insertCompanyVoiceConfig(config);
        return R.ok("操作成功");
    }

    /**
     * 修改呼叫频率配置
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceConfig:edit')")
    @Log(title = "呼叫频率配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody CompanyVoiceConfigParam companyVoiceConfig)
    {

        CompanyVoiceCalleeConfig voiceCalleeConfigBO=new CompanyVoiceCalleeConfig();
        voiceCalleeConfigBO.setCalleeDay(companyVoiceConfig.getCalleeDay());
        voiceCalleeConfigBO.setCalleeHour(companyVoiceConfig.getCalleeHour());
        voiceCalleeConfigBO.setCalleeMinute(companyVoiceConfig.getCalleeMinute());
        voiceCalleeConfigBO.setCalleeMonth(companyVoiceConfig.getCalleeMonth());
        voiceCalleeConfigBO.setCalleeWeek(companyVoiceConfig.getCalleeWeek());

        CompanyVoiceCallerConfig voiceCallerConfigBO=new CompanyVoiceCallerConfig();
        voiceCallerConfigBO.setCallerDay(companyVoiceConfig.getCallerDay());
        voiceCallerConfigBO.setCallerHour(companyVoiceConfig.getCallerHour());
        voiceCallerConfigBO.setCallerMinute(companyVoiceConfig.getCallerMinute());
        voiceCallerConfigBO.setCallerWeek(companyVoiceConfig.getCallerWeek());
        voiceCallerConfigBO.setCallerMonth(companyVoiceConfig.getCallerMonth());
        CompanyVoiceConfig config=new CompanyVoiceConfig();
        config.setConfigId(companyVoiceConfig.getConfigId());
        config.setCalleeJson(JSONUtil.toJsonStr(voiceCalleeConfigBO));
        config.setCallerJson(JSONUtil.toJsonStr(voiceCallerConfigBO));

        companyVoiceConfigService.updateCompanyVoiceConfig(config);
        return R.ok("修改成功");
    }

    /**
     * 删除呼叫频率配置
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoiceConfig:remove')")
    @Log(title = "呼叫频率配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        return toAjax(companyVoiceConfigService.deleteCompanyVoiceConfigByIds(configIds));
    }
}
