package com.huatek.busi.service.impl;

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

    @Resource
    private RMasterDataMapper rMasterDataMapper;

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