package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.ExportExcelUtils;
import cn.com.ctop.common.module.utils.HttpUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.ctop.service.IMaterialTopService;
import org.jeecg.modules.ctop.vo.MaterialTopVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 *  Created by JQ.bi on 2020.5.21
 */
@Slf4j
@RestController
@RequestMapping("/ctop/material")
public class MaterialTopCtrl {

    @Autowired
    IMaterialTopService materialTopService;

    @PostMapping("/top/cost")
    public Result<IPage<MaterialTopVO>> getCostTop(@RequestBody JSONObject requestBody,
                                                   @RequestParam(defaultValue = "cost") String target,
                                                   @RequestParam(defaultValue = "desc") String order,
                                                   @RequestParam(defaultValue = "1") int pageNo,
                                                   @RequestParam(defaultValue = "10") int pageSize) {
        Result<IPage<MaterialTopVO>> result = new Result<>();
        if(requestBody.isEmpty()){
            log.error("request body is empty");
            result.setSuccess(false);
        }
        try {
            String startDate= requestBody.getString("startDate");
            String endDate= requestBody.getString("endDate");
            if(startDate.isEmpty()||endDate.isEmpty()){
                log.error("startDate or endDate is empty");
                result.setSuccess(false);
            }
            IPage<MaterialTopVO> voiPage = materialTopService.selectByPage(endDate, startDate, target, order, pageNo, pageSize);
            result.setResult(voiPage);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping("/top/excel")
    public void getExcelReport(@RequestBody JSONObject requestBody,
                               @RequestParam(defaultValue = "cost") String target,
                               @RequestParam(defaultValue = "desc") String order,
                               HttpServletRequest request,
                               HttpServletResponse response) {

        String startDate= requestBody.getString("startDate");
        String endDate= requestBody.getString("endDate");
        List<MaterialTopVO> excelData= materialTopService.selectList(endDate,startDate, target, order);

        List<List<Object>> excelList= new ArrayList<>();
        excelData.forEach(data->{
            List<Object> temp= new ArrayList<>();
            temp.add(data.getUrl());
            temp.add(data.getName());
            temp.add(data.getCost());
            temp.add(data.getClick());
            temp.add(data.getShowNum());
            temp.add(data.getPlay3s());
            temp.add(data.getPlay10s());
            temp.add(data.getActive());
            temp.add(data.getPay());
            temp.add(data.getResidue());
            temp.add(data.getRegister());
            excelList.add(temp);
        });
        try{
            String[] headers = {"视频","素材名","消耗","点击数","展示数","3s播放数","10s播放数","行为数","支付数","次留数","注册数"};
            OutputStream os = response.getOutputStream();
            ExportExcelUtils eeu = new ExportExcelUtils();
            XSSFWorkbook workbook = new XSSFWorkbook();
            eeu.exportExcel(workbook, 0, "top素材", headers, excelList);
            HttpUtils.setResponseHeader(response, "top素材.xlsx");
            workbook.write(os);
            os.flush();
            os.close();
        }catch (IOException e){
            log.error(e.getMessage());
        }
    }
}
