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

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.util.GenerateUtil;
import com.huatek.busi.dao.BusiOfflineGeneratorBrandDao;
import com.huatek.busi.dao.BusiOfflineGeneratorDao;
import com.huatek.busi.dao.BusiOfflineGisBrandDao;
import com.huatek.busi.dto.BusiOfflineGeneratorBrandDto;
import com.huatek.busi.dto.BusiOffllineGisBrandDto;
import com.huatek.busi.entity.BusiOfflineCableBrand;
import com.huatek.busi.entity.BusiOfflineGeneratorBrand;
import com.huatek.busi.entity.BusiOfflineGisBrand;
import com.huatek.busi.mapper.BusiOfflineGeneratorBrandMapper;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.generatro.BusiOfflineGeneratorBrandService;
import com.huatek.busi.vo.BusiOfflineCableBrandVo;
import com.huatek.busi.vo.BusiOfflineGeneratorBrandVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.frame.base.util.Constants;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.sql.dao.impl.BaseDao;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;
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.Map;

/**
 * 发电机标牌信息表 ServiceImpl
 *
 * @author mitchell_ma
 */
@Component
@Service(interfaceClass = BusiOfflineGeneratorBrandService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOfflineGeneratorBrandServiceImpl extends BaseDao<BusiOfflineGeneratorBrand, String> implements BusiOfflineGeneratorBrandService {

    @Autowired
    BusiOfflineGeneratorBrandMapper busiOfflineGeneratorBrandMapper;
    @Autowired
    private BusiOfflineGeneratorDao busiOfflineGeneratorDao;
    @Autowired
    private BusiOfflineGeneratorBrandDao busiOfflineGeneratorBrandDao;
    @Autowired
    private BusiSiteTreeService busiSiteTreeService;
    @Override
    public BusiOfflineGeneratorBrandVo findBusiOfflineGeneratorBrand(String siteId, String mainEquipmentId) {
        return busiOfflineGeneratorBrandMapper.findBusiOfflineGeneratorBrandVo(siteId, mainEquipmentId);
    }

    @Override
    public int updateBusiOfflineGeneratorBrand(String token, BusiOfflineGeneratorBrandDto busiOfflineGeneratorBrandDto) {
        BusiOfflineGeneratorBrand busiOfflineGeneratorBrand = new BusiOfflineGeneratorBrand();
        BeanUtils.copyPropertiesWithIgnoreNull(busiOfflineGeneratorBrandDto, busiOfflineGeneratorBrand);
        // TODO generator标牌保存数据校验
        // ...

        // 更新树节点上的主设备名称
        busiSiteTreeService.updateNodeFromEquipment(busiOfflineGeneratorBrandDto.getMainEquipmentId(),
                busiOfflineGeneratorBrandDto.getGeneratorName());
        return busiOfflineGeneratorDao.updateLocal(busiOfflineGeneratorBrand);
    }

    @Override
    public int saveOrUpdateBrand(String siteId, String id, String name){
        BusiOfflineGeneratorBrand busiOfflineGeneratorBrand = new BusiOfflineGeneratorBrand();
        BusiOfflineGeneratorBrandVo busiOfflineGeneratorBrandVo = busiOfflineGeneratorBrandMapper.findBusiOfflineGeneratorBrandVo(siteId, id);
        if(busiOfflineGeneratorBrandVo == null){
            busiOfflineGeneratorBrandVo = new BusiOfflineGeneratorBrandVo();
            busiOfflineGeneratorBrandVo.setId(Uuid.getUUID());
            busiOfflineGeneratorBrandVo.setSiteId(siteId);
            busiOfflineGeneratorBrandVo.setMainEquipmentId(id);
            busiOfflineGeneratorBrandVo.setGeneratorName(name);
            busiOfflineGeneratorBrandVo.setEquipmentCode(GenerateUtil.generateCode(17));
            busiOfflineGeneratorBrandVo.setStatus(SiteConstants.Status.NORMAL.getValue());
            busiOfflineGeneratorBrandVo.setCreateTime(LocalDateTime.now());
            org.springframework.beans.BeanUtils.copyProperties(busiOfflineGeneratorBrandVo, busiOfflineGeneratorBrand);
            return this.add(busiOfflineGeneratorBrand);
        }
        busiOfflineGeneratorBrandVo.setGeneratorName(name);
        busiOfflineGeneratorBrandVo.setUpdateTime(LocalDateTime.now());
        org.springframework.beans.BeanUtils.copyProperties(busiOfflineGeneratorBrandVo, busiOfflineGeneratorBrand);
        return this.updateLocal(busiOfflineGeneratorBrand);
    }

    @Override
    public Map<String, Object> saveBusiOfflineGeneratorBrand(String token,
                                                             BusiOfflineGeneratorBrandDto busiOfflineGeneratorBrandDto) {
        try {
            BusiOfflineGeneratorBrand busiOffllineGeneratorBrand = new BusiOfflineGeneratorBrand();
            BeanUtils.copyPropertiesWithIgnoreNull(busiOfflineGeneratorBrandDto, busiOffllineGeneratorBrand);
            // TODO GIS标牌保存数据校验
            // ...

            // 更新树节点上的主设备名称
            busiSiteTreeService.updateNodeFromEquipment(busiOfflineGeneratorBrandDto.getMainEquipmentId(),
                    busiOfflineGeneratorBrandDto.getGeneratorName());
            busiOfflineGeneratorBrandDao.updateLocal(busiOffllineGeneratorBrand);
        }catch (Exception e){
            return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "", "");
        }

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

    }
}
