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

import java.time.LocalDateTime;
import java.util.List;

import com.huatek.busi.entity.BusiParamTransformer;
import com.huatek.busi.vo.BusiParamTransformerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.BusiParamGisDao;
import com.huatek.busi.entity.BusiParamGis;
import com.huatek.busi.entity.BusiParamTemplet;
import com.huatek.busi.mapper.BusiParamGisMapper;
import com.huatek.busi.mapper.BusiParamTempletMapper;
import com.huatek.busi.service.param.BusiParamGisService;
import com.huatek.busi.vo.BusiParamGisVo;
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;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

/**
 * gis的权重系数维护表 ServiceImpl
 *
 * @author anker_cheng
 * @date Jul 12, 2019 2:57:28 PM
 */
@Slf4j
@Component
@Service(interfaceClass = BusiParamGisService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiParamGisServiceImpl implements BusiParamGisService {

    @Autowired
    private BusiParamGisMapper busiParamGisMapper;
    @Autowired
    private BusiParamTempletMapper busiParamTempletMapper;

    @Autowired
    private BusiParamGisDao busiParamGisDao;

    @Override
    public void initBusiParams(String token, String siteId, String mainEquipmentId) {
        List<BusiParamTemplet> busiParamTemplets = busiParamTempletMapper.findBusiParams("gis");
        if (CollectionUtils.isEmpty(busiParamTemplets)) {
            log.debug("busi param templets is empty! paramType: gis");
            return;
        }
        for (BusiParamTemplet templet : busiParamTemplets) {
            BusiParamGis busiParamGis = new BusiParamGis();
            BeanUtils.copyProperties(templet, busiParamGis, "id", "createUser", "createTime");
            busiParamGis.setId(Uuid.getUUID());
            busiParamGis.setSiteId(siteId);
            busiParamGis.setMainEquipmentId(mainEquipmentId);
            busiParamGis.setIsAttend("1");
            busiParamGis.setNumber(0);
            LocalUser user = UserUtil.getLocalUser(token);
            busiParamGis.setCalculatedValue(templet.getCalculatedValue());
            if (user != null) {
                busiParamGis.setCreateUser(user.getUid());
            }
            busiParamGis.setCreateTime(LocalDateTime.now());
            busiParamGisDao.add(busiParamGis);
        }
    }

    @Override
    public List<BusiParamTransformerVo> findBusiParamGis(String siteId, String mainEquipmentId) {
        //查询最近一次保存的参数
        BusiParamTransformerVo numberPojo =busiParamGisMapper.findFirstLevelNumber(siteId,mainEquipmentId);
        if(!ObjectUtils.isEmpty(numberPojo)){
            Integer number = numberPojo.getNumber();
            List<BusiParamTransformerVo> resultList = busiParamGisMapper.findFirstLevelParams(siteId, mainEquipmentId,number);
            setChildren(resultList);
            return resultList;
        }
       return null;

    }

    @Override
    public List<BusiParamTransformerVo> findNotValueBusiParamGis(String siteId, String mainEquipmentId) {
        //查询最近一次保存的参数
        BusiParamTransformerVo numberPojo =busiParamGisMapper.findFirstLevelNumber(siteId,mainEquipmentId);
        Integer number = numberPojo.getNumber();
        List<BusiParamTransformerVo> resultList =busiParamGisMapper.findNotValueFirstLevelParams(siteId, mainEquipmentId,number);
        setChildren(resultList);
        return resultList;
    }

    private void setChildren(List<BusiParamTransformerVo> paramList) {
        if (CollectionUtils.isEmpty(paramList)) {
            return;
        }
        for (BusiParamTransformerVo param : paramList) {
            String siteId = param.getSiteId();
            String mainEquipmentId = param.getMainEquipmentId();
            BusiParamTransformerVo numberPojo =busiParamGisMapper.findFirstLevelNumber(siteId,mainEquipmentId);
            Integer number = numberPojo.getNumber();
            List<BusiParamTransformerVo> children = busiParamGisMapper.findSubParams(param.getSiteId(), param.getMainEquipmentId(), param.getName(),number);
            param.setChildren(children);
            setChildren(children);
        }
    }

    @Override
    public int updateBusiParamGis(String token, List<BusiParamGis> busiParams) {
        if (CollectionUtils.isEmpty(busiParams)) {
            return 0;
        }
        int count = 0;
        for (BusiParamGis busiParamGis : busiParams) {
//            if (busiParamGis.getNumber()!=null){
//                Integer number = busiParamGis.getNumber();
//                busiParamGis.setNumber(number+1);
//            }
            LocalDateTime localDateTime= LocalDateTime.now();
//            busiParamGis.setId(Uuid.getUUID());
            busiParamGis.setCreateTime(localDateTime);
            count += busiParamGisDao.updateLocal(busiParamGis);
        }
        return count;
    }

    @Override
    public int updateCalculateBusiParam(String token, List<BusiParamGis> busiParams) {
        if (CollectionUtils.isEmpty(busiParams)) {
            return 0;
        }
        int count = 0;
        for (BusiParamGis busiParamGis : busiParams) {
            if (busiParamGis.getNumber()!=null){
                Integer number = busiParamGis.getNumber();
                busiParamGis.setNumber(number+1);
            }
            LocalDateTime localDateTime= LocalDateTime.now();
            busiParamGis.setId(Uuid.getUUID());
            busiParamGis.setCreateTime(localDateTime);
            count += busiParamGisDao.add(busiParamGis);
        }
        return count;
    }

	@Override
	public List<BusiParamTransformerVo> findBusiParamGisWeights(String siteId, String mainEquipmentId) {
		List<BusiParamTransformerVo> resultList = busiParamGisMapper.findBusiParamGisWeights(siteId, mainEquipmentId);
		setChildren(resultList);
		return resultList;
	}

    @Override
    public List<BusiParamTransformerVo> findBusiParamGisManual(String siteId, String mainEquipmentId) {
        List<BusiParamTransformerVo> resultList = busiParamGisMapper.findBusiParamGisManual(siteId, mainEquipmentId);
        setChildren(resultList);
        return resultList;
    }
}
