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.domain.model.LoginUser;
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.CompanyTrafficRecordLog;
import com.fs.company.param.CompanyTrafficRecordLogQueryParam;
import com.fs.company.service.ICompanyTrafficRecordLogService;
import com.fs.course.config.CourseConfig;
import com.fs.framework.web.service.TokenService;
import com.fs.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/company/trafficLog")
public class CompanyTrafficLogController extends BaseController {

    @Autowired
    private ICompanyTrafficRecordLogService companyTrafficRecordLogService;

    @Autowired
    private TokenService tokenService;


    @Autowired
    private ISysConfigService configService;


    /** 流量充值记录查询 */
    @PreAuthorize("@ss.hasPermi('company:trafficLog:list')")
    @GetMapping(value = "/list")
    public TableDataInfo list(CompanyTrafficRecordLogQueryParam param) {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String json = configService.selectConfigByKey("course.config");
        CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
        if(!loginUser.isAdmin() && config.getIsBound() != null && config.getIsBound()){
            param.setDeptId(loginUser.getDeptId());
        }
        List<CompanyTrafficRecordLog> list = companyTrafficRecordLogService.selectList(param);
        return getDataTable(list);
    }


    /** 导出 */
    @PreAuthorize("@ss.hasPermi('company:trafficLog:export')")
    @Log(title = "流量充值记录导出", businessType = BusinessType.EXPORT)
    @GetMapping(value = "/export")
    public AjaxResult export(CompanyTrafficRecordLogQueryParam param) {
        List<CompanyTrafficRecordLog> list = companyTrafficRecordLogService.selectList(param);
        ExcelUtil<CompanyTrafficRecordLog> util = new ExcelUtil<>(CompanyTrafficRecordLog.class);
        return util.exportExcel(list, "流量充值记录");
    }



}
