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.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyVoicePackage;
import com.fs.company.param.CompanyPackageBuyParam;
import com.fs.company.param.CompanyVoicePackageOrderListParam;
import com.fs.company.service.ICompanyVoicePackageOrderService;
import com.fs.company.service.ICompanyVoicePackageService;
import com.fs.company.vo.CompanyVoicePackageOrderListVO;
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.*;

import java.util.List;

/**
 * 套餐订单Controller
 *
 * @author fs
 * @date 2021-10-04
 */
@RestController
@RequestMapping("/company/companyVoicePackageOrder")
public class CompanyVoicePackageOrderController extends BaseController
{
    @Autowired
    private ICompanyVoicePackageOrderService voicePackageOrderService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ICompanyVoicePackageService packageService;

    @GetMapping("/getVoicePackagelist")
    public R getVoicePackagelist()
    {
        CompanyVoicePackage map=new CompanyVoicePackage();
        map.setStatus(1);
        List<CompanyVoicePackage> packages=packageService.selectCompanyVoicePackageList(map);
        return R.ok().put("data",packages);
    }

    /**
     * 查询套餐订单列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyVoicePackageOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyVoicePackageOrderListParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyVoicePackageOrderListVO> list = voicePackageOrderService.selectCompanyVoicePackageOrderListVO(param);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('company:companyPackageOrder:buy')")
    @PostMapping("/buy")
    public R buy(@RequestBody CompanyPackageBuyParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
         //处理企业余额是否购
        param.setCreateBy(loginUser.getUser().getNickName());
        return voicePackageOrderService.buy(param);
    }

    /**
     * 导出套餐订单列表
     */
    @PreAuthorize("@ss.hasPermi('company:companyPackageOrder:export')")
    @Log(title = "套餐订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyVoicePackageOrderListParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CompanyVoicePackageOrderListVO> list = voicePackageOrderService.selectCompanyVoicePackageOrderListVO(param);
        ExcelUtil<CompanyVoicePackageOrderListVO> util = new ExcelUtil<CompanyVoicePackageOrderListVO>(CompanyVoicePackageOrderListVO.class);
        return util.exportExcel(list, "companyPackageOrder");
    }




}
