package com.huatek.busi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.entity.RGenData;
import com.huatek.busi.entity.RMasterData;
import com.huatek.busi.mapper.RMasterDataMapper;
import com.huatek.busi.service.IRGenDataService;
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 = IRGenDataService.class)
@Transactional(rollbackFor = Exception.class)
public class RGenDataServiceImpl extends BaseDao<RGenData, String> implements IRGenDataService {

    @Resource
    private RMasterDataMapper rMasterDataMapper;

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