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

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

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.huatek.busi.common.SiteConstants.Status;
import com.huatek.busi.common.util.GenerateUtil;
import com.huatek.busi.dao.BusiOfflineArresterBrandDao;
import com.huatek.busi.dto.BusiOfflineArresterBrandDto;
import com.huatek.busi.entity.BusiOfflineArresterBrand;
import com.huatek.busi.mapper.BusiOfflineArresterBrandMapper;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.arrester.BusiOfflineArresterBrandService;
import com.huatek.busi.vo.BusiOfflineArresterBrandVo;
import com.huatek.frame.base.util.BeanUtils;

/**
 * @ClassName: ArresterBrandServiceImpl
 * @FullClassPath: com.huatek.busi.service.impl.arrester.ArresterBrandServiceImpl
 * @Description: 避雷器-测温点服务实现
 * @author: edward_luo
 * @date: Jul 04, 2019 16:01:11 AM
 * @version: 1.0
 */
@Component
@Service(interfaceClass = BusiOfflineArresterBrandService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOfflineArresterBrandServiceImpl implements BusiOfflineArresterBrandService {

    @Autowired
    private BusiOfflineArresterBrandMapper busiOfflineArresterBrandMapper;

    @Autowired
    private BusiOfflineArresterBrandDao busiOfflineArresterBrandDao;

    @Autowired
    private BusiSiteTreeService busiSiteTreeService;

    @Override
    public BusiOfflineArresterBrandVo getBusiOfflineArresterBrandInfo(String siteId, String mainEquipmentId) {
        return busiOfflineArresterBrandMapper.getBusiOfflineArresterBrandInfo(siteId, mainEquipmentId);
    }

    @Override
    public int saveBusiOfflineArresterBrand(String token, BusiOfflineArresterBrandDto busiOfflineArresterBrandDto) {
        BusiOfflineArresterBrand busiOfflineArresterBrand = new BusiOfflineArresterBrand();
        BeanUtils.copyPropertiesWithIgnoreNull(busiOfflineArresterBrandDto, busiOfflineArresterBrand);
        // TODO 避雷器标牌保存校验
        // ...

        // 更新树节点上的主设备名称
        busiSiteTreeService.updateNodeFromEquipment(busiOfflineArresterBrandDto.getMainEquipmentId(),
            busiOfflineArresterBrandDto.getArresterName());
        return busiOfflineArresterBrandDao.updateLocal(busiOfflineArresterBrand);
    }

    @Override
    public List<BusiOfflineArresterBrand> findAll() {
        return busiOfflineArresterBrandMapper.findAll();
    }

    @Override
    public int saveOrUpdateBrand(String siteId, String mainEquipmentId, String name) {
        BusiOfflineArresterBrand busiOfflineArresterBrand =
            busiOfflineArresterBrandMapper.getBusiOfflineArresterBrand(siteId, mainEquipmentId);
        if(busiOfflineArresterBrand == null) {
            busiOfflineArresterBrand = new BusiOfflineArresterBrand();
            busiOfflineArresterBrand.setSiteId(siteId);
            busiOfflineArresterBrand.setMainEquipmentId(mainEquipmentId);
            busiOfflineArresterBrand.setStatus(Status.NORMAL.getValue());
            busiOfflineArresterBrand.setEquipmentCode(GenerateUtil.generateCode(17));
            busiOfflineArresterBrand.setCreateTime(LocalDateTime.now());
        }
        busiOfflineArresterBrand.setArresterName(name);
        return busiOfflineArresterBrandDao.saveOrUpdate(busiOfflineArresterBrand);
    }

}
