package com.fs.crm.controller;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.model.LoginUser;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.crm.domain.Report;
import com.fs.framework.web.service.TokenService;
import com.fs.crm.param.ReportParam;
import com.fs.crm.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/crm/report")
public class ReportController extends BaseController {
    @Autowired
    private ReportService reportService;
    @Autowired
    private TokenService tokenService;
    @GetMapping("/reportList")
    public TableDataInfo getReport(ReportParam param, HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        Long userId = loginUser.getUser().getUserId();
        param.setCompanyUserId(userId);
        List<Report> list = reportService.getReport(param);
        return getDataTable(list);
    }
    @GetMapping("/export")
    public AjaxResult export(ReportParam param,HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);

        Long userId = loginUser.getUser().getUserId();
        param.setCompanyUserId(userId);
        List<Report> list = reportService.getReport(param);
        if (CollectionUtils.isEmpty(list)){
            return AjaxResult.error("请选择导出数据");
        }
        ExcelUtil<Report> util = new ExcelUtil<Report>(Report.class);
        return util.exportExcel(list, "report");
    }

}
