package com.huatek.busi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.entity.RBlqData;
import com.huatek.busi.entity.RMasterData;
import com.huatek.busi.mapper.RMasterDataMapper;
import com.huatek.busi.service.IRBlqDataService;
import com.huatek.busi.service.IRMasterDataService;
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;

@Slf4j
@Component
@Service(interfaceClass = IRBlqDataService.class)
@Transactional(rollbackFor = Exception.class)
public class RBlqDataServiceImpl extends BaseDao<RBlqData, String> implements IRBlqDataService {

    @Resource
    private RMasterDataMapper rMasterDataMapper;

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