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

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dao.BusiParamAppendixDao;
import com.huatek.busi.dao.BusiParamArresterDao;
import com.huatek.busi.entity.BusiParamAppendix;
import com.huatek.busi.entity.BusiParamArrester;
import com.huatek.busi.entity.BusiParamTemplet;
import com.huatek.busi.mapper.BusiParamAppendixMapper;
import com.huatek.busi.mapper.BusiParamArresterMapper;
import com.huatek.busi.mapper.BusiParamTempletMapper;
import com.huatek.busi.service.param.BusiParamAppendixService;
import com.huatek.busi.service.param.BusiParamArresterService;
import com.huatek.busi.vo.BusiParamAppendixVo;
import com.huatek.busi.vo.BusiParamArresterVo;
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 = BusiParamAppendixService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiParamAppendixServiceImpl implements BusiParamAppendixService {

    @Autowired
    private BusiParamAppendixMapper busiParamAppendixMapper;
    @Autowired
    private BusiParamTempletMapper busiParamTempletMapper;

    @Autowired
    private BusiParamAppendixDao busiParamAppendixDao;

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

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

    }

    @Override
    public List<BusiParamAppendixVo> findParams(String siteId, String mainEquipmentId) {
        BusiParamAppendixVo numberPojo =busiParamAppendixMapper.findFirstLevelNumber(siteId,mainEquipmentId);
        Integer number = numberPojo.getNumber();
        return busiParamAppendixMapper.findParams(siteId,mainEquipmentId,number);
    }

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

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

    @Override
    public int updateBusiParamAppendix(String token, List<BusiParamAppendix> busiParamAppendixs) {
        if (CollectionUtils.isEmpty(busiParamAppendixs)) {
            return 0;
        }
        int count = 0;
        for (BusiParamAppendix busiParamAppendix : busiParamAppendixs) {

            LocalDateTime localDateTime= LocalDateTime.now();
            busiParamAppendix.setCreateTime(localDateTime);
            count += busiParamAppendixDao.updateLocal(busiParamAppendix);
        }
        return count;
    }

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

	@Override
	public List<BusiParamAppendixVo> findBusiParamAppendixWeights(String siteId, String mainEquipmentId,Integer number,String pname) {
		List<BusiParamAppendixVo> resultList = busiParamAppendixMapper.findBusiParamAppendixWeights(siteId, mainEquipmentId,number,pname);
		setChildren(resultList);
		return resultList;
	}

}
