package com.huatek.busi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.entity.RMasterData;
import com.huatek.busi.mapper.RMasterDataMapper;
import com.huatek.busi.service.IRMasterDataService;
import com.huatek.busi.vo.DeviceSubclassOrTestItemListVo;
import com.huatek.busi.vo.LabRMasterDataVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.frame.base.util.Constants;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.sql.dao.impl.BaseDao;
import com.huatek.torch.util.uuid.Uuid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

@Slf4j
@Component
@Service(interfaceClass = IRMasterDataService.class)
@Transactional(rollbackFor = Exception.class)
public class RMasterDataServiceImpl extends BaseDao<RMasterData, String> implements IRMasterDataService {

    @Resource
    private RMasterDataMapper rMasterDataMapper;

    @Override
    public LabRMasterDataVo saveOrUpdateRMasterData(Map<String, Object> map, LocalUser user) {
        LabRMasterDataVo labRMasterDataVo = new LabRMasterDataVo();
        RMasterData rMasterData = rMasterDataMapper.findRMasterByWhere(map);
        if(rMasterData == null){
            rMasterData = new RMasterData();
            rMasterData.setId(Uuid.getUUID());
            rMasterData.setSiteId(map.get("siteId").toString());
            rMasterData.setMainEquId(map.get("mvaId").toString());
            rMasterData.setMainEquName(map.get("mvaName").toString());
            rMasterData.setRefId(map.get("refId").toString());
            rMasterData.setEquLittleType(map.get("dCode").toString());
            rMasterData.setEquLittleId(map.get("equId").toString());
            rMasterData.setSheetName(map.get("sheetName").toString());
            rMasterData.setEquipmentType(map.get("eqType").toString());
            rMasterData.setDeleted(Constants.DEFAULT_NO);
            if(user != null){
                rMasterData.setCreateUser(user.getUid());
                rMasterData.setCreateTime(LocalDateTime.now());
            }
            this.add(rMasterData);
        }
        BeanUtils.copyProperties(rMasterData, labRMasterDataVo);
        return labRMasterDataVo;
    }

    @Override
    public List<RMasterData> findRMasterDataByEquType(String equType){
        if(Objects.equals(equType, SiteConstants.ExternalTypeEnum.GIS.getValue()) || Objects.equals(equType, SiteConstants.ExternalTypeEnum.BYQ.getValue())) {
            return rMasterDataMapper.findRMasterDataByEquType(equType);
        } else if(Objects.equals(equType, SiteConstants.ExternalTypeEnum.CW.getValue())){
            return rMasterDataMapper.findRKggDataByEquType(equType);
        } else if(Objects.equals(equType, SiteConstants.ExternalTypeEnum.BLQ.getValue())){
            return rMasterDataMapper.findRBlqDataByEquType(equType);
        } else if(Objects.equals(equType, SiteConstants.ExternalTypeEnum.GEN.getValue())){
            return rMasterDataMapper.findRGenDataByEquType(equType);
        } else if(Objects.equals(equType, SiteConstants.ExternalTypeEnum.DL.getValue())){
            return rMasterDataMapper.findRDlDataByEquType(equType);
        }
        return null;
    }

    @Override
    public int updateByRefIdsAndEquType(List<String> refIds, String equType){
        if(Objects.equals(equType, SiteConstants.ExternalTypeEnum.GIS.getValue()) || Objects.equals(equType, SiteConstants.ExternalTypeEnum.BYQ.getValue())) {
            return rMasterDataMapper.updateByRefIdsAndEquType(refIds, equType);
        } else if(Objects.equals(equType, SiteConstants.ExternalTypeEnum.CW.getValue())){
            return rMasterDataMapper.updateKggByRefIdsAndEquType(refIds, equType);
        } else if(Objects.equals(equType, SiteConstants.ExternalTypeEnum.BLQ.getValue())){
            return rMasterDataMapper.updateBlqByRefIdsAndEquType(refIds, equType);
        } else if(Objects.equals(equType, SiteConstants.ExternalTypeEnum.GEN.getValue())){
            return rMasterDataMapper.updateGenByRefIdsAndEquType(refIds, equType);
        } else if(Objects.equals(equType, SiteConstants.ExternalTypeEnum.DL.getValue())){
            return rMasterDataMapper.updateDlByRefIdsAndEquType(refIds, equType);
        }
        return 0;
    }

    @Override
    public int updateByIdAndEquType(String eqType, String refId){
        if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.GIS.getValue()) || Objects.equals(eqType, SiteConstants.ExternalTypeEnum.BYQ.getValue())) {
            return rMasterDataMapper.updateByIdAndEquType(eqType, refId);
        } else if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.CW.getValue())){
            return rMasterDataMapper.updateKggByIdAndEquType(eqType, refId);
        } else if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.BLQ.getValue())){
            return rMasterDataMapper.updateBlqByIdAndEquType(eqType, refId);
        } else if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.GEN.getValue())){
            return rMasterDataMapper.updateGenByIdAndEquType(eqType, refId);
        } else if(Objects.equals(eqType, SiteConstants.ExternalTypeEnum.DL.getValue())){
            return rMasterDataMapper.updateDlByIdAndEquType(eqType, refId);
        }
        return 0;
    }

    @Override
    public List<DeviceSubclassOrTestItemListVo> deviceSubclassList(String equType) {
        return rMasterDataMapper.deviceSubclassList(equType);
    }
}
