package cn.com.ctop.kuaishou.modules.report.controller;


import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.ExportExcelUtils;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.kuaishou.modules.report.service.IMaterialReportService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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;

@Slf4j
@RestController
@RequestMapping("/materialReport")
public class MaterialReportController {
    @Autowired
    private IMaterialReportService materialReportService;


    @PostMapping(value = "/getKuaiShouMaterialMarket")
    public Result<List<JSONObject>> getKuaiShouMaterialMarket(@RequestBody JSONObject json) {
        Result<List<JSONObject>> result = new Result<>();
        try {
            List<JSONObject> marketJson = materialReportService.getKuaiShouMaterialMarket(json);
            result.setResult(marketJson);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }

        return result;
    }


    @PostMapping(value = "/getKuaiShouChainRatio")
    public Result<JSONObject> getKuaiShouChainRatio(@RequestBody JSONObject json) {

        Result<JSONObject> result = new Result<>();
        try {
            JSONObject marketJson = materialReportService.getKuaiShouChainRatio(json);
            result.setResult(marketJson);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }

        return result;

    }


    @PostMapping(value = "/getMaterialAccount")
    public Result<List<UserAllocation>> getKuaiShouChainRatio(@RequestBody String userId) {
        Result<List<UserAllocation>> result = new Result<>();
        try {
            if (Check.isNull(userId)) {
                throw new Exception("用户id为空");
            }
            List<UserAllocation> accountIdList = materialReportService.selectAccountIds(userId);
            result.setResult(accountIdList);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);

        }
        return result;

    }

    @PostMapping(value = "/getMaterialReportByAccount")
    public Result<List<JSONObject>> getMaterialReportByAccount(@RequestBody JSONObject json) {
        Result<List<JSONObject>> result = new Result<>();
        try {
            List<JSONObject> reportList = materialReportService.selectReportByAccountIds(json);
            result.setResult(reportList);
            result.setSuccess(true);
        } catch (Exception e) {
            result.setSuccess(false);
        }
        return result;
    }
    /**
     *  导出数据
     */
    @PostMapping("/materialReport/excel")
    public void getExcelReport(@RequestBody JSONObject requestBody,
                               HttpServletRequest request,
                               HttpServletResponse response) {

        List<JSONObject> excelData = materialReportService.selectReportByAccountIds(requestBody);
        if(excelData.isEmpty()){
            return;
        }
        List<List<Object>> excelList= new ArrayList<>();
        excelData.forEach(data->{
            List<Object> temp= new ArrayList<>();
            temp.add(data.getString("url"));
            temp.add(data.getString("signature"));
            temp.add(data.getString("cost"));
            temp.add(data.getBigDecimal("downloadCompleted"));
            temp.add(data.getLong("photoShow"));
            temp.add(data.getLong("photoClick"));
            temp.add(data.getString("aclick"));
            temp.add(data.getString("bclick"));
            temp.add(data.getBigDecimal("photoClickRatio"));
            temp.add(data.getBigDecimal("actionRatio"));
            temp.add(data.getBigDecimal("impression1kCost"));
            temp.add(data.getBigDecimal("photoClickCost"));
            temp.add(data.getBigDecimal("actionCost"));
            temp.add(data.getLong("conversions"));
            temp.add(data.getBigDecimal("conversionPrice"));
            excelList.add(temp);
        });
        try{
            String[] headers = {"视频","视频md5","花费","安卓下载完成数","封面曝光数","封面点击数","素材曝光数","行为数","封面点击率","行为率","均千次封面曝光花费(元)","平均封面点击单价(元)","平均行为单价(元)","转化数","转化单价(元)"};
            OutputStream os = response.getOutputStream();
            ExportExcelUtils eeu = new ExportExcelUtils();
            XSSFWorkbook workbook = new XSSFWorkbook();
            eeu.exportExcel(workbook, 0, "素材账户报表", headers, excelList);
            HttpUtils.setResponseHeader(response, "素材账户报表.xlsx");
            workbook.write(os);
            os.flush();
            os.close();
        }catch (IOException e){
            log.error(e.getMessage());
        }
    }
}
