package com.huatek.busi.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.*;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.common.util.GenerateUtil;
import com.huatek.busi.entity.BusiOfflineCableBrand;
import com.huatek.busi.mapper.BusiOfflineCableBrandMapper;
import com.huatek.busi.service.*;
import com.huatek.busi.vo.BusiOfflineCableBrandVo;
import com.huatek.torch.frame.tools.*;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

@Slf4j
@Component
@Service(interfaceClass = IBusiOfflineCableBrandService.class)
@Transactional(rollbackFor = Exception.class)
public class BusiOfflineCableBrandServiceImpl extends BaseDao<BusiOfflineCableBrand, String> implements IBusiOfflineCableBrandService {

    @Resource
    private BusiOfflineCableBrandMapper busiOfflineCableBrandMapper;

    @Reference(check = false)
    BusiSiteTreeService busiSiteTreeService;

    @Override
    public BusiOfflineCableBrandVo getBusiOfflineCableBrandInfo(String siteId, String mainEquipmentId) {
        return busiOfflineCableBrandMapper.getBusiOfflineCableBrandInfo(siteId, mainEquipmentId);
    }

    @Override
    public int saveOrUpdateBrand(String siteId, String id, String name){
        BusiOfflineCableBrand busiOfflineCableBrand = new BusiOfflineCableBrand();
        BusiOfflineCableBrandVo busiOfflineCableBrandVo = busiOfflineCableBrandMapper.getBusiOfflineCableBrandInfo(siteId, id);
        if(busiOfflineCableBrandVo == null){
            busiOfflineCableBrandVo = new BusiOfflineCableBrandVo();
            busiOfflineCableBrandVo.setId(Uuid.getUUID());
            busiOfflineCableBrandVo.setSiteId(siteId);
            busiOfflineCableBrandVo.setMainEquipmentId(id);
            busiOfflineCableBrandVo.setCableName(name);
            busiOfflineCableBrandVo.setEquipmentCode(GenerateUtil.generateCode(17));
            busiOfflineCableBrandVo.setStatus(SiteConstants.Status.NORMAL.getValue());
            busiOfflineCableBrandVo.setCreateTime(LocalDateTime.now());
            BeanUtils.copyProperties(busiOfflineCableBrandVo, busiOfflineCableBrand);
            return this.add(busiOfflineCableBrand);
        }
        busiOfflineCableBrandVo.setCableName(name);
        busiOfflineCableBrandVo.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(busiOfflineCableBrandVo, busiOfflineCableBrand);
        return this.updateLocal(busiOfflineCableBrand);
    }

    @Override
    public Map<String, Object> saveBusiOfflineCableBrand(String token, BusiOfflineCableBrandVo busiOfflineCableBrandVo) {

        if(StrUtil.isEmpty(token)){
            return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "BUSINESS.TOKEN", "");
        }

        // 根据token获取当前用户
        LocalUser user = UserUtil.getLocalUser(token);

        String msg;
        int count = 0, code;
        String id = busiOfflineCableBrandVo.getId();
        BusiOfflineCableBrand busiOfflineCableBrand = new BusiOfflineCableBrand();
        if(StrUtil.isEmpty(id)){
            busiOfflineCableBrandVo.setId(Uuid.getUUID());
            if(user != null){
                busiOfflineCableBrandVo.setCreateUser(user.getUid());
                busiOfflineCableBrandVo.setCreateTime(LocalDateTime.now());
            }
            busiOfflineCableBrandVo.setStatus(SiteConstants.Status.NORMAL.getValue());
            BeanUtils.copyProperties(busiOfflineCableBrandVo, busiOfflineCableBrand);
            count = this.add(busiOfflineCableBrand);
            if(count > 0){
                msg = "BUSINESS.INSERT.SUCCESS";
                code = ConstantUtil.REQUEST_SUCCESS;
            }else {
                msg = "BUSINESS.INSERT.FAIL";
                code = ConstantUtil.REQUEST_FAIL;
            }
        }else {
            busiOfflineCableBrand = this.get(id);
            if(busiOfflineCableBrand != null){
                busiOfflineCableBrandVo.setId(busiOfflineCableBrand.getId());
                if(user != null){
                    busiOfflineCableBrandVo.setUpdateUser(user.getUid());
                    busiOfflineCableBrandVo.setUpdateTime(LocalDateTime.now());
                }
                BeanUtils.copyProperties(busiOfflineCableBrandVo, busiOfflineCableBrand);
                count = this.updateLocal(busiOfflineCableBrand);
                if(count > 0){
                    msg = "BUSINESS.UPDATE.SUCCESS";
                    code = ConstantUtil.REQUEST_SUCCESS;
                }else {
                    msg = "BUSINESS.UPDATE.FAIL";
                    code = ConstantUtil.REQUEST_FAIL;
                }
            }else {
                msg = "BUSINESS.UPDATE.FAIL";
                code = ConstantUtil.REQUEST_FAIL;
            }
        }

        // 主设备编号
        String mainEquipmentId = busiOfflineCableBrand.getMainEquipmentId();
        // 产品名称
        String cableName = busiOfflineCableBrand.getCableName();

        // 根据设备编号修改设备名称
        busiSiteTreeService.updateNodeFromEquipment(mainEquipmentId, cableName);
        return ResultUtil.put(code, msg, count);
    }
}
