/**
 * 
 */
package com.huatek.busi.service.impl.transformer;

import java.time.LocalDateTime;
import java.util.ArrayList;
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.TransformerEvaluateDataDao;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.entity.TransformerEvaluateData;
import com.huatek.busi.mapper.TransformerEvaluateDataMapper;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.transformer.TransformerEvaluateDataService;
import com.huatek.busi.vo.TransformerEvaluateDataVo;
import com.huatek.busi.vo.TransformerEvaluateDataVo2;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;

/**
 * @ClassName: TransformerEvaluateDataServiceImpl
 * @FullClassPath: com.huatek.busi.service.impl.transformer.
 *                 TransformerEvaluateDataServiceImpl
 * @Description: 变压器评测数据服务.
 * @author: Arno
 * @date: Sep 2, 2019 4:58:47 PM
 * @version: 1.0
 */

@Component
@Service(interfaceClass = TransformerEvaluateDataService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerEvaluateDataServiceImpl implements TransformerEvaluateDataService {

	@Autowired
	private TransformerEvaluateDataDao transformerEvaluateDataDao;

	@Autowired
	private TransformerEvaluateDataMapper transformerEvaluateDataMapper;
	
	@Autowired
	private BusiSiteTreeService busiSiteTreeService;

	@Override
	public boolean save(String token, TransformerEvaluateDataVo vo) {
		Integer count = 0;
		TransformerEvaluateData data = new TransformerEvaluateData();
		BeanUtils.copyPropertiesWithIgnoreNull(vo, data);
		LocalUser user = UserUtil.getLocalUser(token);
		if (null != user) {
			data.setCreateName(user.getUserName());
			data.setCreateUser(user.getUid());
		}
		data.setCreateTime(LocalDateTime.now());
		data.setId(Uuid.getUUID());
		data.setMainTreeId(vo.getMainTreeId());
		data.setStatus("1");
		count = transformerEvaluateDataDao.add(data);
		boolean flag = count > 0 ? true : false;
		return flag;
	}

	@Override
	public boolean update(String token, TransformerEvaluateDataVo vo) {
		Integer count = 0;
		TransformerEvaluateData data = this.findByPK(vo.getId());
		LocalUser user = UserUtil.getLocalUser(token);
		BeanUtils.copyPropertiesWithIgnoreNull(vo, data);
		if (null != user) {
			data.setModifiedName(user.getUserName());
			data.setModifiedUser(user.getUid());
		}
		data.setModifiedTime(LocalDateTime.now());
		count = transformerEvaluateDataDao.updateLocal(data);
		boolean flag = count > 0 ? true : false;
		return flag;
	}

	@Override
	public TransformerEvaluateData findByPK(String PK) {
		return transformerEvaluateDataDao.get(PK);
	}

	@Override
	public List<TransformerEvaluateData> findByMainTreeId(String mainTreeId) {
		return transformerEvaluateDataMapper.selectByMainTreeId(mainTreeId);
	}

	@Override
	public TransformerEvaluateDataVo info(String id) {
		TransformerEvaluateDataVo vo = new TransformerEvaluateDataVo();
		TransformerEvaluateData find = this.findByPK(id);
		if (null != find) {
			BeanUtils.copyPropertiesWithIgnoreNull(find, vo);
		}
		return vo;
	}

	@Override
	public TransformerEvaluateDataVo getLastByMainTreeId(String mainTreeId) {
		List<TransformerEvaluateData> list = this.findByMainTreeId(mainTreeId);
		TransformerEvaluateDataVo vo = null;
		if (list != null && !list.isEmpty()) {
			vo = new TransformerEvaluateDataVo();
			BeanUtils.copyPropertiesWithIgnoreNull(list.get(0), vo);
		}
		return vo;
	}

	@Override
	public int delete(String token, List<String> ids) {
		String userName  = null ;
		String uid  = null ;
		LocalDateTime now  = null ;
		LocalUser user = UserUtil.getLocalUser(token);
		if (null != user) {
			 userName = user.getUserName();
			 uid = user.getUid();
			 now = LocalDateTime.now();
		}
		for(String id: ids){
			TransformerEvaluateData find = this.findByPK(id);
			find.setStatus("2");
			find.setModifiedName(userName);
			find.setModifiedTime(now);
			find.setModifiedUser(uid);
			transformerEvaluateDataDao.updateLocal(find);
		}
		return 0;
	}
	
	@Override
	public PageInfo<TransformerEvaluateDataVo2> page(String mainTreeId, LocalDateTime start, LocalDateTime end , Integer page, Integer limit) {
		return PageHelper.startPage(page == null ? 1 :page , limit == null? 10 : limit)
        .doSelectPageInfo(() -> transformerEvaluateDataMapper.page(mainTreeId,start,end));
	}
	
	@Override
    public Map<String, Object> exportTransformerEvaluateData(String mainTreeId, LocalDateTime start, LocalDateTime end ) {
        Map<String, Object> returnMap = new HashMap<>();
        if (StringUtils.isNotEmpty(mainTreeId)) {
            BusiSiteTree busiSiteTree = busiSiteTreeService.findByPK(mainTreeId);
            List<TransformerEvaluateDataVo2> transformerEvaluateDataList = transformerEvaluateDataMapper.page(mainTreeId,start,end).getResult();
            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 TransformerEvaluateDataVo2());
            }
            returnMap.put("data", transformerEvaluateDataList);
        } else {
            throw new RuntimeException("主设备编码不正确");
        }
        return returnMap;
    }
}
