package com.steerinfo.meterwork.meterworkoldandnew.controller;

import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.meterwork.meterworkcaractual.mapper.MeterWorkCarActualMapper;
import com.steerinfo.meterwork.meterworkoldandnew.service.impl.MeterWorkOldAndNewService;
import com.steerinfo.util.StringUtils;
import io.swagger.annotations.ApiOperation;
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.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/${api.version}/meterworkoldandnews")
public class MeterWorkOldAndNew extends BaseRESTfulController {
    @Resource
    MeterWorkCarActualMapper meterWorkCarActualMapper;

    @Resource
    MeterWorkOldAndNewService meterWorkOldAndNewService;

    /*
     * 汽车衡新老系统查询接口
     */
    @ApiOperation(value="查询结净老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/doQueryOldAndNew")
    public RESTfulResult doQueryOldAndNew(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dateStart = sdf.parse(params.get("startTime").toString());
            Date dateEnd = sdf. parse(params.get("endTime").toString());
            long diff = (dateEnd.getTime() - dateStart.getTime())/(24*60*60*1000);
            //不可以查询查过两天的新老系统内联合数据
            if(diff > 2) {
                rm.setFailed();
                rm.setMessage("不可以查询超过两天的数据");
                rm.setData(null);
                rm.setCode("500");
                return rm;
            }
            List<HashMap> oldAndNewList = meterWorkCarActualMapper.selectOldAndNew(params);
            PageList pageInfo = new PageList(oldAndNewList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    @ApiOperation(value="查询一次老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/doQueryFirstOldAndNew")
    public RESTfulResult doQueryFirstOldAndNew(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dateStart = sdf.parse(params.get("startTime").toString());
            Date dateEnd = sdf. parse(params.get("endTime").toString());
            long diff = (dateEnd.getTime() - dateStart.getTime())/(24*60*60*1000);
            //不可以查询查过两天的新老系统内联合数据
            if(diff > 2) {
                rm.setFailed();
                rm.setMessage("不可以查询超过两天的数据");
                rm.setData(null);
                rm.setCode("500");
                return rm;
            }
            List<HashMap> oldAndNewList = meterWorkCarActualMapper.doQueryFirstOldAndNew(params);
            PageList pageInfo = new PageList(oldAndNewList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }


    @ApiOperation(value="统计结净老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/countOldAndNew")
    public RESTfulResult countOldAndNew(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//            Date dateStart = sdf.parse(params.get("startTime").toString());
//            Date dateEnd = sdf. parse(params.get("endTime").toString());
//            long diff = (dateEnd.getTime() - dateStart.getTime())/(24*60*60*1000);
//            //不可以查询查过两天的新老系统内联合数据
//            if(diff > 2) {
//                rm.setFailed();
//                rm.setMessage("不可以查询超过两天的数据");
//                rm.setData(null);
//                rm.setCode("500");
//                return rm;
//            }
            List<HashMap> oldAndNewList = meterWorkCarActualMapper.countOldAndNew(params);
            PageList pageInfo = new PageList(oldAndNewList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    @ApiOperation(value="查询结净老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/doQueryOldAndNew2")
    public RESTfulResult doQueryOldAndNew2(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            //PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));

            // 新系统结净数据
            List<HashMap> newList = meterWorkOldAndNewService.getNew(params);

            // 老系统结净数据
            List<HashMap> oldList = meterWorkOldAndNewService.getOld(params);

            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());

            PageList pageInfo = this.helpPage(params, totalList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    @ApiOperation(value="查询一次老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/doQueryFirstOldAndNew2")
    public RESTfulResult doQueryFirstOldAndNew2(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            //PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));

            // 新系统一次数据
            List<HashMap> newList = meterWorkOldAndNewService.getNewFirst(params);

            // 老系统一次数据
            List<HashMap> oldList = meterWorkOldAndNewService.getOldFirst(params);

            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());
            PageList pageInfo = this.helpPage(params, totalList);

//            if(totalList.size() >= 1){
//                double weightSum = totalList.stream().flatMap(item -> Double.parseDouble(item.get("METER_WEIGHT").toString())).sum();
//            }
//
////            for(HashMap param: totalList){
////                weightSum += Long.parseLong(param.get("METER_WEIGHT").toString());
////            }
//
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("pageInfo", pageInfo);
//            map.put("weightSum", weightSum);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }


    @ApiOperation(value="统计结净老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/countOldAndNew2")
    public RESTfulResult countOldAndNew2(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            //PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//            Date dateStart = sdf.parse(params.get("startTime").toString());
//            Date dateEnd = sdf. parse(params.get("endTime").toString());
//            long diff = (dateEnd.getTime() - dateStart.getTime())/(24*60*60*1000);
//            //不可以查询查过两天的新老系统内联合数据
//            if(diff > 2) {
//                rm.setFailed();
//                rm.setMessage("不可以查询超过两天的数据");
//                rm.setData(null);
//                rm.setCode("500");
//                return rm;
//            }

            // 新系统结净数据
            List<HashMap> newList = meterWorkOldAndNewService.countNew(params);

            // 老系统结净数据
            List<HashMap> oldList = meterWorkOldAndNewService.countOld(params);

            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());

            //List<HashMap> oldAndNewList = meterWorkCarActualMapper.countOldAndNew(params);
            PageList pageInfo = this.helpPage(params, totalList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    /*
     * 轨道衡新老系统查询接口
     */
    @ApiOperation(value="查询轨道衡结净老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/railway/doQueryOldAndNew2")
    public RESTfulResult doQueryRailwayOldAndNew2(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            //PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));

            // 新系统结净数据
            List<HashMap> newList = meterWorkOldAndNewService.getRailwayNew(params);

            // 老系统结净数据
            List<HashMap> oldList = meterWorkOldAndNewService.getRailwayOld(params);

            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());

            PageList pageInfo = this.helpPage(params, totalList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    /*
     * 轨道衡新老计量系统联合查询接口
     */
    @ApiOperation(value="查询轨道衡结净老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/railway/doQueryFirstOldAndNew2")
    public RESTfulResult doQueryRailwayFirstOldAndNew2(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {

            // 新系统结净数据
            List<HashMap> newList = meterWorkOldAndNewService.getRailwayFirstNew(params);

            // 老系统结净数据
            List<HashMap> oldList = meterWorkOldAndNewService.getRailwayFirstOld(params);

            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());


            //PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), 200);

            PageList pageInfo = this.helpPage(params, totalList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    @ApiOperation(value="统计结净老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/railway/countOldAndNew2")
    public RESTfulResult countRailwayOldAndNew2(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            //PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));

            // 新系统结净数据
            List<HashMap> newList = meterWorkOldAndNewService.countRailwayNew(params);

            // 老系统结净数据
            List<HashMap> oldList = meterWorkOldAndNewService.countRailwayOld(params);

            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());

            //List<HashMap> oldAndNewList = meterWorkCarActualMapper.countOldAndNew(params);
            PageList pageInfo = this.helpPage(params, totalList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    /*
     * 动态轨道衡新老系统查询接口
     */
    @ApiOperation(value="查询轨道衡结净老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/railwaydynamic/doQueryOldAndNew2")
    public RESTfulResult doQueryRailwayDynamicOldAndNew2(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            //PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));

            // 新系统结净数据
            List<HashMap> newList = meterWorkOldAndNewService.getRailwayDynamicNew(params);

            // 老系统结净数据
            List<HashMap> oldList = meterWorkOldAndNewService.getRailwayDynamicOld(params);

            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());

            PageList pageInfo = this.helpPage(params, totalList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    /*
     * 动态轨道衡新老计量系统联合查询接口
     */
    @ApiOperation(value="查询轨道衡结净老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/railwaydynamic/doQueryFirstOldAndNew2")
    public RESTfulResult doQueryRailwayDynamicFirstOldAndNew2(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {

            // 新系统结净数据
            List<HashMap> newList = meterWorkOldAndNewService.getRailwayDynamicFirstNew(params);

            // 老系统结净数据
            List<HashMap> oldList = meterWorkOldAndNewService.getRailwayDynamicFirstOld(params);

            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());


            //PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), 200);

            PageList pageInfo = this.helpPage(params, totalList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    @ApiOperation(value="统计结净老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/railwaydynamic/countOldAndNew2")
    public RESTfulResult countRailwayDynamicOldAndNew2(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            //PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));

            // 新系统结净数据
            List<HashMap> newList = meterWorkOldAndNewService.countRailwayDynamicNew(params);

            // 老系统结净数据
            List<HashMap> oldList = meterWorkOldAndNewService.countRailwayDynamicOld(params);

            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());

            //List<HashMap> oldAndNewList = meterWorkCarActualMapper.countOldAndNew(params);
            PageList pageInfo = this.helpPage(params, totalList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    private PageList helpPage(HashMap params, List<HashMap> totalList){
        Integer pageNo = Integer.parseInt(params.get("pageNum").toString());
        Integer pageSize = Integer.parseInt(params.get("pageSize").toString());
        Integer nextPage;
        Integer prePage;
        Integer startRow;
        double pages;

        //split page
        List<HashMap> pageList = totalList.stream().skip((pageNo-1)*pageSize).limit(pageSize).
                collect(Collectors.toList());


        if(totalList.size() > pageNo * pageSize){
            nextPage = pageNo + 1;
        }
        else {
            nextPage = pageNo;
        }

        if(pageNo != 1){
            prePage = pageNo - 1;
        }
        else {
            prePage = 1;
        }

        pages = Math.ceil((double) totalList.size() / (double)pageSize);

        startRow = (pageNo - 1) * pageSize + 1;

        PageList pageInfo = new PageList(pageList);
        pageInfo.setPageNum(pageNo);
        pageInfo.setPageSize(pageSize);
        pageInfo.setNextPage(nextPage);
        pageInfo.setPrePage(prePage);
        pageInfo.setTotal(totalList.size());
        pageInfo.setPages((int)pages);
        pageInfo.setStartRow(startRow);

        return pageInfo;
    }


    /*
     * 轨道衡新老计量系统联合查询接口
     */
    @ApiOperation(value="查询铁水衡吊钩秤结净老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/railwayHook/doQueryFirstOldAndNew2")
    public RESTfulResult doQueryRailwayHookFirstOldAndNew2(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            // 新系统结净数据
            List<HashMap> newList = meterWorkOldAndNewService.getRailwayHookFirstNew(params);
            // 老系统结净数据
            List<HashMap> oldList = meterWorkOldAndNewService.getRailwayHookFirstOld(params);
            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());
            PageList pageInfo = this.helpPage(params, totalList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    /*
     * 轨道衡新老系统查询接口
     */
    @ApiOperation(value="查询轨道衡结净老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/railwayHook/doQueryOldAndNew2")
    public RESTfulResult doQueryRailwayHookOldAndNew2(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            // 新系统结净数据
            List<HashMap> newList = meterWorkOldAndNewService.getRailwayHookNew(params);
            // 老系统结净数据
            List<HashMap> oldList = meterWorkOldAndNewService.getRailwayHookOld(params);
            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());
            PageList pageInfo = this.helpPage(params, totalList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    @ApiOperation(value="统计结净老系统和新系统", notes="返回List<HashMap>对象")
    @PostMapping(value = "/railwayHook/countOldAndNew2")
    public RESTfulResult countRailwayHookOldAndNew2(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            // 新系统结净数据
            List<HashMap> newList = meterWorkOldAndNewService.countRailwayHookNew(params);
            // 老系统结净数据
            List<HashMap> oldList = meterWorkOldAndNewService.countRailwayHookOld(params);
            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());
            PageList pageInfo = this.helpPage(params, totalList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    @ApiOperation(value="查询所有秤点结净老系统和新系统（包含汽车衡、轨道衡、吊钩秤、铁水衡、皮带秤）", notes="返回List<HashMap>对象")
    @PostMapping(value = "/doQueryAllOldAndNew")
    public RESTfulResult doQueryAllOldAndNew(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            //PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
            // 新系统结净数据
            List<HashMap> newList = meterWorkOldAndNewService.getAllNew(params);
            // 老系统结净数据
            List<HashMap> oldList = meterWorkOldAndNewService.getAllOld(params);
            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());
            PageList pageInfo = this.helpPage(params, totalList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    @ApiOperation(value="统计所有秤点结净老系统和新系统（包含汽车衡、轨道衡、吊钩秤、铁水衡、皮带秤）", notes="返回List<HashMap>对象")
    @PostMapping(value = "/countAllOldAndNew")
    public RESTfulResult countAllOldAndNew(@RequestBody HashMap params) {
        RESTfulResult rm = success();
        try {
            //PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
            // 新系统结净数据
            List<HashMap> newList = meterWorkOldAndNewService.getCountAllNew(params);
            // 老系统结净数据
            List<HashMap> oldList = meterWorkOldAndNewService.getCountAllOld(params);
            // 新老系统数据联合查询
            List<HashMap> totalList = Stream.of(newList, oldList)
                    .flatMap(x -> x.stream())
                    .collect(Collectors.toList());
            PageList pageInfo = this.helpPage(params, totalList);
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }
}
