package com.fs.company.controller;

import java.util.List;

import com.fs.company.vo.CompanyVoiceListVO;
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 cn.hutool.core.util.ObjectUtil;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.company.cache.ICompanyCacheService;
import com.fs.company.domain.Company;
import com.fs.company.param.CompanyPackageBuyParam;
import com.fs.company.service.ICompanyVoicePackageOrderService;
import com.fs.company.service.impl.CompanyVoicePackageOrderServiceImpl;
import com.fs.core.security.LoginUser;
import org.apache.http.util.Asserts;
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.CompanyVoice;
import com.fs.company.service.ICompanyVoiceService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 企业通话Controller
 *
 * @author fs
 * @date 2023-02-23
 */
@RestController
@RequestMapping("/company/companyVoice")
public class CompanyVoiceController extends BaseController
{
    @Autowired
    private ICompanyVoiceService companyVoiceService;
    @Autowired
    private ICompanyVoicePackageOrderService voicePackageOrderService;
    /**
     * 查询企业通话列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoice:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyVoice companyVoice)
    {
        startPage();
        List<CompanyVoiceListVO> list = companyVoiceService.selectCompanyVoiceListVO(companyVoice);
        for (CompanyVoiceListVO vo : list){
            vo.setUseTimes(vo.getTotalTimes()-vo.getTimes());
        }
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('company:companyVoice:buy')")
    @PostMapping("/buy")
    @RepeatSubmit
    public R buy(@RequestBody CompanyPackageBuyParam param)
    {
        logger.info("购买语音套餐请求参数: companyId={}, packageId={}", param.getCompanyId(), param.getPackageId());
        Asserts.notNull(param,"请求参数不能为空!");
        Asserts.notNull(param.getCompanyId(),"公司id不能为空!");
        Asserts.notNull(param.getPackageId(),"套餐id不能为空!");

        return voicePackageOrderService.buy(param);
    }

    /**
     * 导出企业通话列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoice:export')")
    @Log(title = "企业通话", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyVoice companyVoice)
    {
        List<CompanyVoice> list = companyVoiceService.selectCompanyVoiceList(companyVoice);
        ExcelUtil<CompanyVoice> util = new ExcelUtil<CompanyVoice>(CompanyVoice.class);
        return util.exportExcel(list, "companyVoice");
    }

    /**
     * 获取企业通话详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoice:query')")
    @GetMapping(value = "/{voiceId}")
    public AjaxResult getInfo(@PathVariable("voiceId") Long voiceId)
    {
        return AjaxResult.success(companyVoiceService.selectCompanyVoiceById(voiceId));
    }

    /**
     * 新增企业通话
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoice:add')")
    @Log(title = "企业通话", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyVoice companyVoice)
    {
        return toAjax(companyVoiceService.insertCompanyVoice(companyVoice));
    }

    /**
     * 修改企业通话
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoice:edit')")
    @Log(title = "企业通话", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyVoice companyVoice)
    {
        return toAjax(companyVoiceService.updateCompanyVoice(companyVoice));
    }

    /**
     * 删除企业通话
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoice:remove')")
    @Log(title = "企业通话", businessType = BusinessType.DELETE)
	@DeleteMapping("/{voiceIds}")
    public AjaxResult remove(@PathVariable Long[] voiceIds)
    {
        return toAjax(companyVoiceService.deleteCompanyVoiceByIds(voiceIds));
    }
}
