package com.chinaitop.depot.intelligent.grainsituation.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chinaitop.depot.intelligent.basicdata.mapper.TCcinfoMapper;
import com.chinaitop.depot.intelligent.basicdata.model.TCcinfo;
import com.chinaitop.depot.intelligent.basicdata.model.TCcinfoExample;
import com.chinaitop.depot.intelligent.fumigation.model.TFumigationPlan;
import com.chinaitop.depot.intelligent.grainsituation.mapper.TCcdataMapper;
import com.chinaitop.depot.intelligent.grainsituation.mapper.TCcvalueMapper;
import com.chinaitop.depot.intelligent.grainsituation.model.TCcdata;
import com.chinaitop.depot.intelligent.grainsituation.model.TCcdataExample;
import com.chinaitop.depot.intelligent.grainsituation.model.TCcvalue;
import com.chinaitop.depot.intelligent.grainsituation.model.TCcvalueExample;
import com.chinaitop.depot.intelligent.grainsituation.service.InsectPestDetectionService;
import com.chinaitop.depot.intelligent.utils.DateUtils;
import com.chinaitop.depot.intelligent.utils.ParameterUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.unissoft.model.ResponseEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class InsectPestDetectionServiceImpl implements InsectPestDetectionService {

    @Resource
    private TCcdataMapper tCcdataMapper;

    @Resource
    private TCcinfoMapper tCcinfoMapper;

    @Resource
    private TCcvalueMapper tCcvalueMapper;

    /**
     * 列表
     *
     * @param iBeginTdh
     * @param iEndTdh
     * @return
     */
    @Override
    public List<Map<String, Object>> getList(String vCfCode, Integer iBeginTdh, Integer iEndTdh,String orgId, String startTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("orgId", orgId);
        if (StringUtils.isNotBlank(vCfCode)) {
            map.put("vCfCode", vCfCode);
        }
        if (null != iBeginTdh && null != iEndTdh) {
            map.put("iBeginTdh", iBeginTdh);
            map.put("iEndTdh", iEndTdh);
        }
        if(null != iBeginTdh && null == iEndTdh){
            map.put("iBeginTdh", iBeginTdh);
            map.put("iEndTdh", iBeginTdh);
        }
        if(null == iBeginTdh && null != iEndTdh){
            map.put("iBeginTdh", iEndTdh);
            map.put("iEndTdh", iEndTdh);
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            map.put("startTime", startTime);
            map.put("endTime", endTime);
        }
        return tCcdataMapper.getList(map);
    }

    @Override
    public ResponseEntity getByCfCode(String cfCode, String searchStartDate, String searchEndDate,Integer orgId) {
        HashMap<String, String> map = Maps.newHashMap();
        if (StringUtils.isNotBlank(cfCode)) {
            map.put("vCfCode", cfCode);
        }
        if(ParameterUtil.isnotnull(orgId)){
            map.put("orgId", orgId+"");
        }
        if (StringUtils.isNotBlank(searchStartDate) && StringUtils.isNotBlank(searchEndDate)) {
            map.put("searchStartDate", searchStartDate + DateUtils.startTime);
            map.put("searchEndDate", searchEndDate + DateUtils.endTime);
        }
        List<Map<String, Object>> byCfCode = tCcvalueMapper.getByCfCode(map);

        if (byCfCode.size() > 0) {
            String[] legend = {"最大值(只)", "最小值(只)", "平均值(只)"};
            ArrayList<String> xAxis = Lists.newArrayList();
            ArrayList<Integer> maxs = Lists.newArrayList();
            ArrayList<Integer> mins = Lists.newArrayList();
            ArrayList<Double> avgs = Lists.newArrayList();
            JSONArray series = new JSONArray();
            for (Map<String, Object> maps : byCfCode) {
                int max = Integer.parseInt(maps.get("max").toString());
                int min = Integer.parseInt(maps.get("min").toString());
                double avg = Double.parseDouble(maps.get("avg").toString());
                String string = maps.get("vUpdateTime").toString();
                maxs.add(max);
                mins.add(min);
                avgs.add(avg);
                xAxis.add(string);
            }
            for (String leg : legend) {
                JSONObject json = new JSONObject();
                json.put("name", leg);
                json.put("type", "line");
                json.put("stack", "bars");
//                json.put("data", new JSONArray());
                series.add(json);
            }
            JSONObject jsonx = series.getJSONObject(0);
            jsonx.put("data", maxs);
            JSONObject jsonn = series.getJSONObject(1);
            jsonn.put("data", mins);
            JSONObject jsong = series.getJSONObject(2);
            jsong.put("data", avgs);

            JSONObject result = new JSONObject();
            result.put("xAxis", xAxis);
            result.put("legend", legend);
            result.put("series", series);
            System.out.println(result);
            return ResponseEntity.ok(result);
        }
        return null;
    }

    @Override
    public List<TCcinfo> getByCode(List<String> code) {
        TCcinfoExample example = new TCcinfoExample();
        TCcinfoExample.Criteria criteria = example.createCriteria();
        if (code != null) {
            criteria.andVdevcodeIn(code);
        }
        List<TCcinfo> tCcinfos = tCcinfoMapper.selectByExample(example);
        return tCcinfos;
    }

    @Override
    public Map<String, Map<String, Object>> getByCfCodes(Map<String, Object> mapDate) {
        List<Map<String, Object>> byCfCodes = tCcvalueMapper.getByCfCodes(mapDate);
        if (byCfCodes.size() > 0) {
            HashMap<String, Map<String, Object>> map = Maps.newHashMap();
            for (Map<String, Object> objectMap : byCfCodes) {
                map.put(objectMap.get("vCfCode").toString(), objectMap);
            }
            return map;
        }
        return null;
    }

    @Transactional
    @Override
    public void insertBatch(List<TCcdata> tCcdata) {
        tCcdataMapper.insertBatch(tCcdata);
    }

    @Override
    public List<TCcdata> getByCodeAndTimes(List<String> cDcsCodes, List<Date> cTimes, List<Integer> tdhs) {
        TCcdataExample example = new TCcdataExample();
        TCcdataExample.Criteria criteria = example.createCriteria();
        if ((cDcsCodes != null && cDcsCodes.size() > 0) && (cTimes != null && cTimes.size() > 0) && (tdhs != null && tdhs.size() > 0)) {
            criteria.andVCcCodeIn(cDcsCodes);
            criteria.andVUpdateTimeIn(cTimes);
            criteria.andITdhIn(tdhs);
            List<TCcdata> tCcdata = tCcdataMapper.selectByExample(example);
            return tCcdata;
        }
        return null;
    }

    @Override
    public TCcvalue getOneByCfCode(String orgId, String vCfCode) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(vCfCode) && StringUtils.isNotBlank(orgId)) {
            map.put("orgId",orgId);
            map.put("vCfCode", vCfCode);
            return tCcvalueMapper.getOneByCfCode(map);
        }
        return null;
    }

	@Override
	public List<Map<String, Object>> getJcd(Map<String, Object> map) {
		return tCcvalueMapper.getJcd(map);
	}

    @Override
    public void add(TCcdata tCcdata,TCcvalue tCcvalue) {
        tCcvalueMapper.insert(tCcvalue);
        tCcdataMapper.insert(tCcdata);
    }

    @Override
    public void update(TCcdata tCcdata,TCcvalue tCcvalue) {
        TCcdataExample tCcdataExample = new TCcdataExample();
        TCcdataExample.Criteria criteria = tCcdataExample.createCriteria();
        criteria.andIdEqualTo(tCcdata.getId());
        tCcdataMapper.updateByExampleSelective(tCcdata,tCcdataExample);

        TCcvalueExample tCcvalueExample = new TCcvalueExample();
        TCcvalueExample.Criteria criteria1 = tCcvalueExample.createCriteria();
        criteria1.andIdEqualTo(tCcvalue.getId());
        tCcvalueMapper.updateByExampleSelective(tCcvalue,tCcvalueExample);
    }

    @Override
    public List<TCcdata> getInsectPestDeatil(TCcdataExample example) {
        return tCcdataMapper.selectByExample(example);
    }
}
