package com.huatek.busi.service.impl.gis;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.dao.BusiOutlineGisExperimentDataDao;
import com.huatek.busi.dto.BusiOutlineGisExperimentDataDto;
import com.huatek.busi.entity.BusiOutlineGisExperimentData;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.mapper.BusiOutlineGisExperimentDataMapper;
import com.huatek.busi.service.BusiOutlineGisExperimentDataService;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.vo.BusiOutlineGisExperimentDataVo;
import com.huatek.busi.vo.TransformerEvaluateDataVo2;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.util.uuid.Uuid;

/**
 * 离线数据--GIS开关试验数据 ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 4, 2019 2:15:39 PM
 */
@Component
@Service(interfaceClass = BusiOutlineGisExperimentDataService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOutlineGisExperimentDataServiceImpl implements BusiOutlineGisExperimentDataService {

	@Autowired
	private BusiOutlineGisExperimentDataMapper busiOutlineGisExperimentDataMapper;
	@Autowired
	private BusiOutlineGisExperimentDataDao busiOutlineGisExperimentDataDao;
	
	
	@Autowired
	private BusiSiteTreeService busiSiteTreeService;

    @Override
    public PageInfo<BusiOutlineGisExperimentDataVo>
        findBusiOutlineGisExperimentDataPage(String mainTreeId, LocalDateTime start, LocalDateTime end , Integer page, Integer limit) {
//    	return PageHelper
//            .startPage(busiOutlineGisExperimentDataDto.getPage(), busiOutlineGisExperimentDataDto.getLimit())
//            .doSelectPageInfo(() -> busiOutlineGisExperimentDataMapper
//                .selectBusiOutlineGisExperimentDataPage(busiOutlineGisExperimentDataDto));
    	
    	return PageHelper.startPage(page == null ? 1 :page , limit == null? 10 : limit)
    	        .doSelectPageInfo(() -> busiOutlineGisExperimentDataMapper.selectBusiOutlineGisExperimentDataPage(mainTreeId,start,end));
    }
    


	@Override
	public int addBusiOutlineGisExperimentData(String token,
			BusiOutlineGisExperimentDataDto busiOutlineGisExperimentDataDto) {
		BusiOutlineGisExperimentData busiOutlineGisExperimentData = new BusiOutlineGisExperimentData();
		BeanUtils.copyPropertiesWithIgnoreNull(busiOutlineGisExperimentDataDto, busiOutlineGisExperimentData);
		busiOutlineGisExperimentData.setId(Uuid.getUUID());
		busiOutlineGisExperimentData.setCreateTime(LocalDateTime.now());
		busiOutlineGisExperimentData.setExperimentDateTime(LocalDateTime.now());
		int count = busiOutlineGisExperimentDataDao.add(busiOutlineGisExperimentData);
		return count;
	}

	@Override
	public BusiOutlineGisExperimentData findBusiOutlineGisExperimentData(String id) {
		return busiOutlineGisExperimentDataDao.get(id);
	}

	@Override
	public int updateBusiOutlineGisExperimentData(String token,
			BusiOutlineGisExperimentDataDto busiOutlineGisExperimentDataDto) {
		BusiOutlineGisExperimentData busiOutlineGisExperimentData = new BusiOutlineGisExperimentData();
		BeanUtils.copyPropertiesWithIgnoreNull(busiOutlineGisExperimentDataDto, busiOutlineGisExperimentData);
		int count = busiOutlineGisExperimentDataDao.updateLocal(busiOutlineGisExperimentData);
		return count;
	}

	@Override
	public void deleteBusiOutlineGisExperimentData(String[] ids) {
		List<String> idList = Arrays.asList(ids);
		busiOutlineGisExperimentDataMapper.batchDel(idList);
	}

	@Override
	public BusiOutlineGisExperimentData findBusiOutlineDataByEquip(String equipmentId, String siteId) {
		return busiOutlineGisExperimentDataMapper.findBusiOutlineDataByEquip(equipmentId, siteId);
	}

	@Override
	public Map<String, Object> exportGisExperimentData(String mainTreeId, LocalDateTime startTime, LocalDateTime endTime) {
		// TODO Auto-generated method stub
		 Map<String, Object> returnMap = new HashMap<>();
	        if (StringUtils.isNotEmpty(mainTreeId)) {
	            BusiSiteTree busiSiteTree = busiSiteTreeService.findByPK(mainTreeId);
	            List<BusiOutlineGisExperimentDataVo> transformerEvaluateDataList = busiOutlineGisExperimentDataMapper.exportGisExperimentData(mainTreeId,startTime,endTime);
	            returnMap.put("fileName", String.format("%s%s", busiSiteTree.getName(), "评测数据"));
	            returnMap.put("title", String.format("%s%s", busiSiteTree.getName(), "评测数据"));
	            if (transformerEvaluateDataList.isEmpty()) {
	            	transformerEvaluateDataList = new ArrayList<>();
	            	transformerEvaluateDataList.add(new BusiOutlineGisExperimentDataVo());
	            }
	            returnMap.put("data", transformerEvaluateDataList);
	        } else {
	            throw new RuntimeException("主设备编码不正确");
	        }
	        return returnMap;
	}
}