package com.ruixuan.salesLeads.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.core.controller.BaseController;
import com.ruixuan.common.core.domain.AjaxResult;
import com.ruixuan.common.core.domain.ResultResponse;
import com.ruixuan.common.utils.DateUtils;
import com.ruixuan.common.utils.StringUtils;
import com.ruixuan.common.utils.http.HttpUtil;
import com.ruixuan.common.utils.poi.ExcelUtil;
import com.ruixuan.salesLeads.listener.ImportExcelListener;
import com.ruixuan.salesLeads.mapper.TestMapper;
import com.ruixuan.salesLeads.pojo.SaleClueInfoPojo;
import com.ruixuan.salesLeads.pojo.TestPojo;
import com.ruixuan.salesLeads.service.ITestService;
import de.siegmar.fastcsv.reader.CsvContainer;
import de.siegmar.fastcsv.reader.CsvReader;
import de.siegmar.fastcsv.reader.CsvRow;
import io.swagger.annotations.*;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ***************************************************
 *
 * @Auther: zianY
 * @Descipion: demo测试
 * @CreateDate: 2022-05-19
 * ****************************************************
 */


@Api(tags = "测试",description = "demoController")
@RestController
@RequestMapping("/demoController")
public class DemoController extends BaseController {


    @Autowired
    private ITestService testServiceImpl;




    @ApiOperation("获取用户列表")
    @GetMapping("/demoList")
    public ResultResponse<List<TestPojo>> userList()
    {

        //startPage();
        return testServiceImpl.selectDemoList();
    }




    @ApiOperation(value = "导入",notes = "swagger不支持上传,请移步使用postman上传仅支持form-data。")
    @PostMapping(value = "/importExcel")
    public ResultResponse importExcel(@ModelAttribute
                                      //@ApiParam("wenjianwenjian") @RequestParam("file") MultipartFile file,
                                      //@ApiParam("aaa") @RequestParam("aa") String aa,
                                       TestPojo testPojo) throws Exception{



         //读取Excel
//        EasyExcel.read(testPojo.getFile().getInputStream(),null,
//                    new ImportExcelListener()).sheet().headRowNumber(1).doRead();


        // 从监听中获取结果集
        List<Map<String,String>> dataList = ImportExcelListener.importDataList;
        Map<Integer, String> headList = ImportExcelListener.importHeadList;

        logger.info("--结果shuju---{}",dataList);
        logger.info("--结果表头---{}",headList);



        //System.out.println("--转化--"+SalesleadsInfoPojo.SalesleadsInfoPojoInit(dataList));

        ImportExcelListener.importDataList.clear();
        ImportExcelListener.importHeadList.clear();




//        // 得到Class类对象
//        Class<?> clazz = Class.forName("com.ruixuan.salesLeads.pojo.SalesleadsInfoPojo");
//
//        // 获取类的所有属性
//        Field[] fields = clazz.getDeclaredFields();
//
//
//        Map<String,String> map = new HashMap<>();
//
//
//        for (int i = 0; i < headList.size(); i++) {
//
//
//            for (Field field : fields) {
//                ApiModelProperty myField2 = field.getAnnotation(ApiModelProperty.class);
//
//                System.out.println(myField2.value());
//                System.out.println(field.getName());
//
//                if (headList.containsKey(myField2.value())){
//                    System.out.println("包含属性---"+field.getName());
//                }
//
//            }
//
//        }

        //startPage();
        return ResultResponse.successMsg("cheng.",null);
    }

    @ApiOperation(value = "导入",notes = "swagger不支持上传,请移步使用postman上传仅支持form-data。")
    @PostMapping(value = "/importCsv")
    public ResultResponse importExcel(
                                      @ApiParam("wenjianwenjian") @RequestParam("file") MultipartFile file

                                       ) throws Exception{
        //logger.info("jieguo====>>>{}",lisMap);

        return ResultResponse.successMsg("cheng.",null);
    }


    @Autowired
    private TestMapper testMapper;

    @ApiOperation(value = "导入",notes = "swagger不支持上传,请移步使用postman上传仅支持form-data。")
    @PostMapping(value = "/importUserInit")
    public ResultResponse importUserInit(@ApiParam("file") @RequestParam("file") MultipartFile file) throws Exception{
        //logger.info("jieguo====>>>{}",lisMap);


        ExcelUtil<TestPojo> util = new ExcelUtil<TestPojo>(TestPojo.class);
        List<TestPojo> userList = util.importExcel(file.getInputStream(),1);

        for (TestPojo testPojo : userList) {
            JSONObject deptJson = testMapper.getDeptNameByName(testPojo.getDeptName());
            testPojo.setDeptId(deptJson.getString("dept_id"));
            testPojo.setUserName(testPojo.getEmail().substring(0,testPojo.getEmail().indexOf("@")));
        }

        testMapper.replaceUserInfoBatchInit(userList);
        return ResultResponse.successMsg("cheng.",null);
    }



    @ApiOperation(value = "查询报表测试")
    @GetMapping("getLiveReportData")
    public void getLiveReportData(Long accountId,String start_time,String end_time,Integer group_type) throws Exception {
        try {
            String url = "https://ad.e.kuaishou.com/rest/openapi/gw/esp/v1/report/list";
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            headers.put("Access-Token", "d3731e2e43ab3c45f15a1800478cae02");
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("advertiser_id", accountId);
            JSONObject params = new JSONObject();
            params.put("start_time", DateUtils.timeToStamp(start_time));
            params.put("end_time",DateUtils.timeToStamp(end_time));
            params.put("group_type",group_type);//汇总方式	1-分日 2-分时仅时间跨度为一天的时候支持分时。仅最近 7 天支持分时查询
            params.put("view_type",1);//查询类型	1-账户 2-计划 3-组 4-创意
            param.put("param",params);
            System.out.println(param);
            String result = HttpUtil.httpPostRequest(url, param, headers);
            JSONObject resultJson = JSONObject.parseObject(result);
            System.out.println(resultJson);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
