package org.jeecg.modules.performance.controller;


import cn.com.ctop.common.module.utils.ExportExcelUtils;
import cn.com.ctop.common.module.utils.HttpUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.performance.entity.PerformanceQuarter;
import org.jeecg.modules.performance.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * Created by JQ.bi on 2020.6.9
 */
@Slf4j
@RestController
@RequestMapping("/ctop/performance/show")
public class PerformanceShowCtrl {

    @Autowired
    IPerformanceInfoService performanceService;
    @Autowired
    IPerformanceQuarterService performanceQuarterService;
    @Autowired
    IOperateDetailService operateDetailService;
    @Autowired
    IDesignDetailService designDetailService;
    @Autowired
    ISalePerformanceDetailService salePerformanceDetailService;

    @GetMapping("/getPerformanceInfo")
    public Result<Map<String, Object>> getPerformanceInfo() {
        Result<Map<String, Object>> result = new Result<>();
        result.setResult(performanceService.getPerformanceInfo());
        result.setSuccess(true);
        return result;
    }

    @PostMapping("/{pageNo}/{pageSize}/{order}")
    public Result<PageInfo<PerformanceQuarter>> showPerformance(@PathVariable int pageNo,
                                                                @PathVariable int pageSize,
                                                                @PathVariable String order,
                                                                @RequestBody JSONObject requestBody) {
        Result<PageInfo<PerformanceQuarter>> result = new Result<>();
        PageInfo<PerformanceQuarter> pageInfo = performanceQuarterService.getPerformanceByConditionsPage(pageNo, pageSize, order, requestBody);
        result.setSuccess(true);
        result.setResult(pageInfo);
        return result;
    }

    /**
     * 导出数据
     *
     * @param requestBody
     * @param response
     */
    @PostMapping("/excel")
    public void getExcelReport(@RequestBody JSONObject requestBody,
                               HttpServletResponse response) {
        List<PerformanceQuarter> excelData = performanceQuarterService.getPerformanceByConditions(requestBody);
        if (excelData.isEmpty()) {
            return;
        }
        String[] titles = {"姓名", "公司", "职位", "媒体类型", "年份", "季度", "消耗", "提成比例", "绩效", "管理岗绩效"};

        List<List<Object>> excelList = new ArrayList<>();
        excelData.forEach(data -> {
            List<Object> temp = new ArrayList<>();
            temp.add(data.getUserName());
            temp.add(data.getCompanyName());
            temp.add(data.getRoleName());
            temp.add(data.getMediaType() == 1 ? "头条" : "快手");
            temp.add(data.getYear());
            temp.add(data.getQuarter());
            temp.add(data.getCost());
            temp.add(data.getPoint());
            temp.add(data.getPerformance());
            temp.add(data.getAdditionalFees());
            excelList.add(temp);
        });
        try {
            OutputStream os = response.getOutputStream();
            ExportExcelUtils eeu = new ExportExcelUtils();
            XSSFWorkbook workbook = new XSSFWorkbook();
            eeu.exportExcel(workbook, 0, "绩效", titles, excelList);
            HttpUtils.setResponseHeader(response, "绩效.xlsx");
            workbook.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }

    @PostMapping("/operateDetail")
    public Result<List<JSONObject>> getOperateDetail(@RequestBody JSONObject requestBody) {
        Result<List<JSONObject>> result = new Result<>();
        result.setResult(operateDetailService.getOperateGroupAccountByUser(requestBody));
        result.setSuccess(true);
        return result;
    }

    @PostMapping("/designDetail/{pageNo}/{pageSize}")
    public Result<PageInfo<JSONObject>> getOperateDetail(@PathVariable int pageNo,
                                                     @PathVariable int pageSize,
                                                     @RequestBody JSONObject requestBody) {
        Result<PageInfo<JSONObject>> result = new Result<>();
        result.setResult(designDetailService.getDesignGroupMaterialByUser(requestBody,pageNo,pageSize));
        result.setSuccess(true);
        return result;
    }
    @PostMapping("/saleDetail")
    public Result<List<JSONObject>> getSaleDetail(@RequestBody JSONObject requestBody) {
        Result<List<JSONObject>> result = new Result<>();
        result.setResult(salePerformanceDetailService.getSaleGroupAdByUser(requestBody));
        result.setSuccess(true);
        return result;
    }
}
