package com.steerinfo.metermonitor.metermonitorweight.service.impl;

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.metermonitor.dto.MeterMonitorWeightDto;
import com.steerinfo.metermonitor.metermonitorweight.mapper.MeterMonitorWeightMapper;
import com.steerinfo.metermonitor.metermonitorweight.model.MeterMonitorWeight;
import com.steerinfo.metermonitor.metermonitorweight.service.IMeterMonitorWeightService;
import com.steerinfo.meterwork.dto.TimesDto;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkcaractualfirst.mapper.MeterWorkCarActualFirstMapper;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * MeterMonitorWeight服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2020-09-22 07:19
 * 类描述
 * 修订历史：
 * 日期：2020-09-22
 * 作者：generator
 * 参考：
 * 描述：MeterMonitorWeight服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "meterMonitorWeightService")
public class MeterMonitorWeightServiceImpl extends BaseServiceImpl<MeterMonitorWeight, String> implements IMeterMonitorWeightService {

    @Autowired
    private MeterMonitorWeightMapper meterMonitorWeightMapper;

    @Resource
    private MeterWorkCarActualFirstMapper meterWorkCarActualFirstMapper;

    @Override
    protected IBaseMapper<MeterMonitorWeight, String> getMapper() {
        return meterMonitorWeightMapper;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public List<MeterMonitorWeightDto> listByTime(String actualFirstNo)
    {
        try{
            MeterWorkCarActualFirst mwcaf=meterWorkCarActualFirstMapper.selectByPrimaryKey(actualFirstNo);
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("actualFirstNo", actualFirstNo);
            obj.put("baseSpotNo", mwcaf.getBaseSpotNo());
            List<MeterMonitorWeight> list = meterMonitorWeightMapper.selectTime(obj);
            List<MeterMonitorWeightDto> mmwds = new ArrayList<>();
            if (list.size() > 0){
                HashMap<String, Object> map = new HashMap<>();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String start = sdf.format(list.get(0).getCollectStartTime());
                String end = sdf.format(list.get(0).getCollectEndTime());
                map.put("collectStartTime",start);
                map.put("collectEndTime",end);
                map.put("baseSpotNo", mwcaf.getBaseSpotNo());
                mmwds = meterMonitorWeightMapper.selectByTime(map);
                String weight = "";
                for(int i = 0;i < mmwds.size();i++) {
                    if (StringUtils.isNotEmpty(mmwds.get(i).getWeight())){
                        weight = mmwds.get(i).getWeight();
                        break;
                    }
                }
                for(int i = 0;i < mmwds.size();i++){
                    if (i == 0){
                        mmwds.get(i).setWeight(weight);
                    }
                    if (StringUtils.isEmpty(mmwds.get(i).getWeight())){
                        mmwds.get(i).setWeight(mmwds.get(i-1).getWeight());
                    }
                }
            }

//            for (MeterMonitorWeight mmw : list) {
//                String startTime = GeneratedNumberUtil.sdfall.format(mmw.getCollectStartTime());
//                if(StringUtils.isEmpty(map.get(startTime))){
//                    mmwds.add(getObjDto(startTime,mmw.getCollectWeight()));
//                }
//                String endTime = GeneratedNumberUtil.sdfall.format(mmw.getCollectEndTime());
//                if(StringUtils.isEmpty(map.get(endTime))){
//                    mmwds.add(getObjDto(endTime,mmw.getCollectWeight()));
//                    map.put(endTime,endTime);
//                }
//            }
            Collections.reverse(mmwds);
            return  mmwds;
        }catch (MarkerMetException e){
            throw  e;
        }
    }


    @Override
    @Transactional
    public List<MeterMonitorWeightDto> chartByTime(HashMap parmas){
        HashMap<String, Object> map = new HashMap<>();
        map.put("collectStartTime",parmas.get("collectStartTime").toString());
        map.put("collectEndTime",parmas.get("collectEndTime").toString());
        map.put("baseSpotNo",parmas.get("baseSpotNo").toString());
        List<MeterMonitorWeightDto> mmwds = meterMonitorWeightMapper.selectByTime(parmas);
        String weight = "";
        for(int i = 0;i < mmwds.size();i++) {
            if (StringUtils.isNotEmpty(mmwds.get(i).getWeight())){
                weight = mmwds.get(i).getWeight();
                break;
            }
            mmwds.get(i).setWeight("0");
        }
        for(int i = 0;i < mmwds.size();i++){
            if (StringUtils.isEmpty(mmwds.get(i).getWeight())){
                mmwds.get(i).setWeight(mmwds.get(i-1).getWeight());
            }
        }
        return mmwds;
    }


//    private MeterMonitorWeightDto getObjDto(String time, String weight) throws ParseException {
//        MeterMonitorWeightDto mmwd = new MeterMonitorWeightDto();
//        mmwd.setTimeSpot(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time));
//        mmwd.setWeight(weight);
//        return mmwd;
//    }
    @Override
    @Transactional(rollbackFor=Exception.class)
    public List<TimesDto> listFast(String spotTypeNo, String collectStartTime, String collectEndTime)
    {
        try{
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("spotTypeNo", spotTypeNo);
            obj.put("collectStartTime", collectStartTime);
            obj.put("collectEndTime", collectEndTime);
            List<TimesDto> list = meterMonitorWeightMapper.listFast(obj);
            return list;
        }catch (MarkerMetException e){
            throw  e;
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public List<TimesDto> listSlowly(String spotTypeNo, String collectStartTime, String collectEndTime)
    {
        try{
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("spotTypeNo", spotTypeNo);
            obj.put("collectStartTime", collectStartTime);
            obj.put("collectEndTime", collectEndTime);
            List<TimesDto> list = meterMonitorWeightMapper.listSlowly(obj);
            return list;
        }catch (MarkerMetException e){
            throw  e;
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public List<TimesDto> listAvg(String spotTypeNo, String collectStartTime, String collectEndTime)
    {
        try{
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("spotTypeNo", spotTypeNo);
            obj.put("collectStartTime", collectStartTime);
            obj.put("collectEndTime", collectEndTime);
            List<TimesDto> list = meterMonitorWeightMapper.listAvg(obj);
            return list;
        }catch (MarkerMetException e){
            throw  e;
        }
    }


    @Override
    @Transactional
    public String doAddWf(MeterMonitorWeight model){
        long lDate1 = model.getCollectStartTime().getTime()/1000;
        long lDate2 = model.getCollectEndTime().getTime()/1000;
        long times = Math.round(lDate2-lDate1);
        model.setCollectTime(times);
        meterMonitorWeightMapper.insertSelective(model);
        return model.getWeightNo();
    }

    @Override
    @Transactional
    public String doUpdateWf(MeterMonitorWeight model){
        if (StringUtils.isEmpty(model.getWeightNo())){
            throw new MarkerMetException(500, "编号为空");
        }
        if (model.getCollectEndTime() == null){
            throw new MarkerMetException(500, "结束时间为空");
        }
        MeterMonitorWeight meterMonitorWeight = meterMonitorWeightMapper.selectByPrimaryKey(model.getWeightNo());
        if(meterMonitorWeight != null){
            meterMonitorWeight.setCollectEndTime(model.getCollectEndTime());
            long lDate1 = meterMonitorWeight.getCollectStartTime().getTime()/1000;
            long lDate2 = model.getCollectEndTime().getTime()/1000;
            long times = Math.round(lDate2-lDate1);
            model.setCollectTime(times);
            meterMonitorWeightMapper.updateByPrimaryKeySelective(meterMonitorWeight);
        }
        return model.getWeightNo();
    }


}
