package com.huatek.busi.service.impl;

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

    @Resource
    private RMasterDataMapper rMasterDataMapper;

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