package com.ruoyi.web.controller.nanjing;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.xdevapi.JsonArray;
import com.ruoyi.common.core.controller.BaseController;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.nanjing.domain.ReportData;
import com.ruoyi.nanjing.domain.TBdProductinfo;
import com.ruoyi.nanjing.service.ITBdProducttypeService;
import com.ruoyi.web.controller.common.DemoData;
import org.apache.catalina.User;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.xssf.usermodel.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/nanjing/reportData")
public class ReportDataController extends BaseController {

    private String prefix = "nanjing/Report";
    @Autowired
    private ITBdProducttypeService producttypeService;

    @RequiresPermissions("nanjing:report:view")
    @GetMapping()
    public String reportData(ModelMap map) {
        return prefix + "/Report";
//        return "redirect:"+"http://127.0.0.1:18080/RDP-SERVER/rdppage/main/d354a3beb6dae7938155b3f9a962e479";
    }


    @PostMapping("/report")
    @ResponseBody
    public TableDataInfo reportData(String begin, String end, String barcode) {
        Map map = new HashMap<String, Object>();
        map.put("begin", begin);
        map.put("end", end);
        map.put("barcode", barcode);
        List<Map<String,Object>> reportDataList = producttypeService.selectReport(map);


        return getDataTable(reportDataList);
    }


//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(String begin, String end, String barcode){
//        Map map = new HashMap<String, Object>();
//        map.put("begin", begin);
//        map.put("end", end);
//        map.put("barcode", barcode);
//        AjaxResult ajaxResult=new AjaxResult();
//        List<ReportData> reportDataList = producttypeService.selectReport(map);
//        List<String> paraName = new ArrayList<>();
//        List<String> barcodes = new ArrayList<>();
//        List<String> stations = new ArrayList<>();
//        //取出参数名
//        reportDataList.forEach(x->barcodes.add(x.getBarcode()));
//        reportDataList.forEach(x->paraName.add(x.getParameaning()));
//        reportDataList.forEach(x->stations.add(x.getStationName()));
//        //去重
//        //条码List
//        List<String> barcodeList = barcodes.stream().distinct().collect(Collectors.toList());
//        //参数List
//        List<String> paraNameList = paraName.stream().distinct().collect(Collectors.toList());
//        //工位名集合
//        List<String> stationList = stations.stream().distinct().collect(Collectors.toList());
//
//        //定义结果集合
//        List<Map<String,Object>> list = new ArrayList<>();
//        //定义结果集map
//        Map<String,Object> map1 = new HashMap<>();
//
//        int number=0;
//        for(String item:barcodeList){
//            number=number+1;
//            map1.put("序号",String.valueOf(number));
//            map1.put("产品条码",item);
//            for (String station:stationList) {
//                for(String param:paraNameList){
//                    for (ReportData rpd:reportDataList) {
//                        map1.put("时间",rpd.getBeginTime());
//                        if (rpd.getBarcode().equals(item)&&rpd.getStationName().equals(station)&&rpd.getParameaning().equals(param)){
//                            if(rpd.getValue()==null){
//                                rpd.setValue("-");
//                            }
//                            String paramName = rpd.getStationName()+"-"+rpd.getParameaning();
////                            map1.put(station,param);
//                            map1.put(paramName,rpd.getValue());
//                            break;
//                        }
//                    }
//                }
//            }
//            //排序的作用
//            TreeMap<String,Object> treeMap = new TreeMap<>(map1);
//            list.add(treeMap);
//
//        }
//        List<String> arrayList=new ArrayList();
//        if (list!=null) {
//            Set<String> k = list.get(0).keySet();
//            Iterator<String> it = k.iterator();
//            while (it.hasNext()) {
//                String key = it.next();
//
//                //有了键，就可以通过map集合的get方法获取对应的值
////                if (key=="barcode"){
////                    key="产品条码";
////                }if (key=="beginTime"){
////                    key="时间";
////                }
//                arrayList.add(key);
//
//            }
//        }
////        ExcelUtil.createWorkBook(list,list1);
//        XSSFWorkbook wb = new XSSFWorkbook();
//        XSSFSheet sheet = wb.createSheet("First sheet");
//        sheet.setDefaultRowHeight((short) (2 * 256));
//        XSSFFont font = wb.createFont();
//        font.setFontName("宋体");
//        font.setFontHeightInPoints((short) 16);
//        XSSFRow row = sheet.createRow(0);
//        String targetFile=null;
//       for (int s=0;s<arrayList.size();s++){
//           row.createCell(s);
//           XSSFCell cell = row.createCell(s);
//           cell.setCellValue(arrayList.get(s));
//           sheet.setColumnWidth(s, 4000);
//       }
//        XSSFRow rows ;
//        XSSFCell cells;
//       for (int i=0;i<list.size();i++){
//           rows = sheet.createRow(i + 1);
//           for (int j = 0; j < arrayList.size(); j++) {
//               cells = rows.createCell(j);
//               String value = (String) list.get(i).get(arrayList.get(j));
//               cells.setCellValue(value);
//           }
//       }
//       try {
//
//           targetFile="条码信息"+ new SimpleDateFormat("yyyyMMdd").format(new Date()) + ".xlsx";
////           File file = new File();
//           FileOutputStream fileOutputStream = new FileOutputStream(ExcelUtil.getAbsoluteFilew(targetFile));
//           wb.write(fileOutputStream);
//           wb.close();
//           fileOutputStream.close();
//       }catch (IOException e){
//           e.printStackTrace();
//       }
//
//        return ajaxResult.success(targetFile);
//    }

}
