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

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.BusiParamArresterDao;
import com.huatek.busi.dao.BusiParamGisDao;
import com.huatek.busi.entity.BusiParamArrester;
import com.huatek.busi.entity.BusiParamGis;
import com.huatek.busi.entity.BusiParamTemplet;
import com.huatek.busi.mapper.BusiParamArresterMapper;
import com.huatek.busi.mapper.BusiParamGisMapper;
import com.huatek.busi.mapper.BusiParamTempletMapper;
import com.huatek.busi.service.param.BusiParamArresterService;
import com.huatek.busi.service.param.BusiParamGisService;
import com.huatek.busi.vo.BusiParamArresterVo;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

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

/**
 * 避雷器的权重系数维护表 ServiceImpl
 *
 * @author vent
 * @date 3 31, 2023 2:57:26 PM
 */
@Slf4j
@Component
@Service(interfaceClass = BusiParamArresterService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiParamArresterServiceImpl implements BusiParamArresterService {

    @Autowired
    private BusiParamArresterMapper busiParamArresterMapper;
    @Autowired
    private BusiParamTempletMapper busiParamTempletMapper;

    @Autowired
    private BusiParamArresterDao busiParamArresterDao;

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

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

    }

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

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

    @Override
    public int updateBusiParamArrester(String token, List<BusiParamArrester> busiParams) {
        if (CollectionUtils.isEmpty(busiParams)) {
            return 0;
        }
        int count = 0;
        for (BusiParamArrester busiParamArrester : busiParams) {

            LocalDateTime localDateTime= LocalDateTime.now();
            busiParamArrester.setCreateTime(localDateTime);
            count += busiParamArresterDao.updateLocal(busiParamArrester);
        }
        return count;
    }

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

	@Override
	public List<BusiParamArresterVo> findBusiParamGisWeights(String siteId, String mainEquipmentId,Integer number) {
		List<BusiParamArresterVo> resultList = busiParamArresterMapper.findBusiParamArresterWeights(siteId, mainEquipmentId,number);
		setChildren(resultList);
		return resultList;
	}

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