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

import java.time.LocalDateTime;
import java.util.*;

import com.huatek.busi.dao.BusiParamTransformerCalculateDao;
import com.huatek.busi.dao.BusiParamTransformerVoluntarilyDao;
import com.huatek.busi.dao.BusiParamTransformerVoluntarilyVoDao;
import com.huatek.busi.entity.BusiParamTransformerCalculate;
import com.huatek.busi.entity.BusiParamTransformerVoluntarily;
import com.huatek.busi.vo.BusiParamTransformerVoluntarilyVo;
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.BusiParamTransformerDao;
import com.huatek.busi.entity.BusiParamTemplet;
import com.huatek.busi.entity.BusiParamTransformer;
import com.huatek.busi.mapper.BusiParamTempletMapper;
import com.huatek.busi.mapper.BusiParamTransformerMapper;
import com.huatek.busi.service.param.BusiParamTransformerService;
import com.huatek.busi.vo.BusiParamTransformerVo;
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;

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

    @Autowired
    private BusiParamTransformerMapper busiParamTransformerMapper;
    @Autowired
    private BusiParamTempletMapper busiParamTempletMapper;

    @Autowired
    private BusiParamTransformerDao busiParamTransformerDao;
    @Autowired
    private BusiParamTransformerCalculateDao busiParamTransformerCalculateDao;

    @Autowired
    private BusiParamTransformerVoluntarilyDao busiParamTransformerVoluntarilyDao;

    @Autowired
    private BusiParamTransformerVoluntarilyVoDao busiParamTransformerVoluntarilyVoDao;

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

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

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

//    private void setChildrenCalculate(List<BusiParamTransformerVo> paramList) {
//        if (CollectionUtils.isEmpty(paramList)) {
//            return;
//        }
//        for (BusiParamTransformerVo param : paramList) {
//            List<BusiParamTransformerVo> children = busiParamTransformerMapper.findSubParamsCalculate(param.getSiteId(),
//                    param.getMainEquipmentId(), param.getName());
//            param.setChildren(children);
//            setChildrenCalculate(children);
//        }
//    }

    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 =busiParamTransformerMapper.findFirstLevelNumber(siteId,mainEquipmentId);
            Integer number =numberPojo.getNumber();
            List<BusiParamTransformerVo> children = busiParamTransformerMapper.findSubParams(param.getSiteId(),
                param.getMainEquipmentId(), param.getName(),number);
            param.setChildren(children);
            setChildren(children);
        }
    }

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


    @Override
    public int updateCalculateBusiParam(String token, List<BusiParamTransformer> busiParams) {
        if (CollectionUtils.isEmpty(busiParams)) {
            return 0;
        }
        int count = 0;
        Integer oldNumber = null;
        List<BusiParamTransformerVo> list = busiParamTransformerMapper.findParamsManual(busiParams.get(0).getSiteId(), busiParams.get(0).getMainEquipmentId());
        if (!CollectionUtils.isEmpty(list)){
            oldNumber = list.get(0).getNumber();
        }
        for (BusiParamTransformer busiParamTransformer : busiParams) {
            if (busiParamTransformer.getNumber()!=null){
                Integer number = busiParamTransformer.getNumber();
                if (null!=oldNumber && oldNumber.equals(number)){
                    busiParamTransformer.setNumber(number+1);
                }else {
                    busiParamTransformer.setNumber(oldNumber+1);
                }

            }
            LocalDateTime localDateTime= LocalDateTime.now();
            busiParamTransformer.setId(Uuid.getUUID());
            busiParamTransformer.setCreateTime(localDateTime);
            count += busiParamTransformerDao.add(busiParamTransformer);
        }
        return count;
    }


    @Override
	public List<BusiParamTransformerVo> findParams(String siteId, String mainEquipmentId) {
		return
	            busiParamTransformerMapper.findParams(siteId, mainEquipmentId);
	}
    @Override
    public List<BusiParamTransformerVo> findParamsManual(String siteId, String mainEquipmentId) {
        return busiParamTransformerMapper.findParamsManual(siteId, mainEquipmentId);
    }
    @Override
    public int saveOrUpdateBusiParamTransformer(String token, List<BusiParamTransformerVoluntarilyVo> busiParams) {

        int count = 0;
        boolean flag = false;
        for (BusiParamTransformerVoluntarilyVo busiParamTransformer : busiParams) {
            String id =busiParamTransformer.getId();
            String siteId = busiParamTransformer.getSiteId();
            String mainEquipmentId = busiParamTransformer.getMainEquipmentId();
            Integer number = busiParamTransformer.getNumber();
            List<BusiParamTransformerVoluntarily> paramVo = busiParamTransformerMapper.findTransformerParma(id,siteId,mainEquipmentId);
            paramVo.removeAll(Collections.singleton(null));
            if(paramVo!=null) {
                for (BusiParamTransformerVoluntarily busiParamTransformerVoluntarily : paramVo) {
                    Integer numberVo = busiParamTransformerVoluntarily.getNumber();
                    String idVo = busiParamTransformerVoluntarily.getId();
                    if (!number.equals(numberVo) || id.equals(idVo)) {
                        busiParamTransformerVoluntarilyVoDao.updateLocal(busiParamTransformer);
                        flag = true;
                        break;
                    }
                }
            }
            if (!flag){
                busiParamTransformerVoluntarilyVoDao.add(busiParamTransformer);
            }

        }

        return count;
    }

    @Override
    public List<BusiParamTransformerVoluntarily> findVoluntarilyParam(String siteId, String mainEquipmentId) {
        List<BusiParamTransformerVoluntarily> resultList =busiParamTransformerMapper.findVoluntarilyParam(siteId,mainEquipmentId);
        setChildrenVo(resultList);
        return resultList;
    }

    private void setChildrenVo(List<BusiParamTransformerVoluntarily> paramList) {
        if (CollectionUtils.isEmpty(paramList)) {
            return;
        }
        for (BusiParamTransformerVoluntarily param : paramList) {
            List<BusiParamTransformerVoluntarily> children = busiParamTransformerMapper.findSubParamsVo(param.getSiteId(),
                    param.getMainEquipmentId(), param.getName());
            param.setChildren(children);
            setChildrenVo(children);
        }
    }


}
