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

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.BusiOfflineSwitchgearBrandDao;
import com.huatek.busi.dto.BusiOfflineSwitchgearBrandDto;
import com.huatek.busi.entity.BusiOfflineSwitchgearBrand;
import com.huatek.busi.mapper.BusiOfflineSwitchgearBrandMapper;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.temperature.BusiOfflineSwitchgearBrandService;
import com.huatek.busi.vo.BusiOfflineSwitchgearBrandVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 开关柜标牌信息表 ServiceImpl
 *
 * @author walker_wang
 * @date Jul 4, 2019 1:50:15 PM
 */
@Component
@Service(interfaceClass = BusiOfflineSwitchgearBrandService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOfflineSwitchgearBrandServiceImpl implements BusiOfflineSwitchgearBrandService {

    @Autowired
    private BusiOfflineSwitchgearBrandMapper busiOffllineSwitchgearBrandMapper;

    @Autowired
    private BusiOfflineSwitchgearBrandDao busiOfflineSwitchgearBrandDao;

    @Autowired
    private BusiSiteTreeService busiSiteTreeService;

    @Override
    public Map<String, Object> updateBusiOfflineSwitchgearBrand(String token, BusiOfflineSwitchgearBrandDto busiOfflineSwitchgearBrandDto) {
        try {
            BusiOfflineSwitchgearBrand busiOfflineSwitchgearBrand = new BusiOfflineSwitchgearBrand();
            BeanUtils.copyPropertiesWithIgnoreNull(busiOfflineSwitchgearBrandDto, busiOfflineSwitchgearBrand);
            // TODO 开关柜标牌保存数据校验
            // ...

            // 更新树节点上的主设备名称
            busiSiteTreeService.updateNodeFromEquipment(busiOfflineSwitchgearBrandDto.getMainEquipmentId(), busiOfflineSwitchgearBrandDto.getSwitchgearName());
            busiOfflineSwitchgearBrand.setStatus("1");
            busiOfflineSwitchgearBrandDao.updateLocal(busiOfflineSwitchgearBrand);
        }catch (Exception e){
            return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "", "");
        }

        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
    }

    @Override
    public BusiOfflineSwitchgearBrandVo findBusiOfflineSwitchgearBrand(String siteId, String mainEquipmentId) {
        return busiOffllineSwitchgearBrandMapper.findBusiOfflineSwitchgearBrandVo(siteId, mainEquipmentId);
    }

    @Override
    public BusiOfflineSwitchgearBrandVo findBusiOfflineSwitchgearBrand(String siteId, String mainEquipmentId, String equTypeId){
        return busiOffllineSwitchgearBrandMapper.findBusiOfflineSwitchgearBrandVo1(siteId, mainEquipmentId, equTypeId);
    }

    @Override
    public List<BusiOfflineSwitchgearBrand> findAll() {
        return busiOffllineSwitchgearBrandMapper.findAll();
    }

    @Override
    public int saveOrUpdateBrand(String siteId, String mainEquipmentId, String name) {
        BusiOfflineSwitchgearBrand busiOfflineSwitchgearBrand =
            busiOffllineSwitchgearBrandMapper.findBusiOfflineSwitchgearBrand(siteId, mainEquipmentId);
        if (busiOfflineSwitchgearBrand == null) {
            busiOfflineSwitchgearBrand = new BusiOfflineSwitchgearBrand();
            busiOfflineSwitchgearBrand.setSiteId(siteId);
            busiOfflineSwitchgearBrand.setMainEquipmentId(mainEquipmentId);
            busiOfflineSwitchgearBrand.setEquipmentCode(GenerateUtil.generateCode(17));
            busiOfflineSwitchgearBrand.setCreateTime(LocalDateTime.now());
            busiOfflineSwitchgearBrand.setStatus(Status.NORMAL.getValue());
        }
        busiOfflineSwitchgearBrand.setSwitchgearName(name);
        return busiOfflineSwitchgearBrandDao.saveOrUpdate(busiOfflineSwitchgearBrand);
    }

    @Override
    public int insert(BusiOfflineSwitchgearBrand busiOfflineSwitchgearBrand){
        return busiOfflineSwitchgearBrandDao.insert(busiOfflineSwitchgearBrand);
    }

    @Override
    public int update(BusiOfflineSwitchgearBrand busiOfflineSwitchgearBrand){
        return busiOfflineSwitchgearBrandDao.updateLocal(busiOfflineSwitchgearBrand);
    }

}
