package com.huatek.busi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.entity.XTransformerEvaluateData;
import com.huatek.busi.mapper.XTransformerEvaluateDataMapper;
import com.huatek.busi.service.ITransformerEvaluateDataService;
import com.huatek.busi.vo.TransformerEvaluateDataVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.sql.dao.impl.BaseDao;
import com.huatek.torch.util.tools.UserUtil;
import com.huatek.torch.util.uuid.Uuid;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

@Component
@Service(interfaceClass = ITransformerEvaluateDataService.class)
@Transactional(rollbackFor = Exception.class)
public class ITransformerEvaluateDataServiceImpl extends BaseDao<XTransformerEvaluateData, String> implements ITransformerEvaluateDataService {

    @Resource
    private XTransformerEvaluateDataMapper xTransformerEvaluateDataMapper;

    @Override
    public TransformerEvaluateDataVo getLastByMainTreeId(String mainTreeId){
        List<XTransformerEvaluateData> list = xTransformerEvaluateDataMapper.selectByMainTreeId(mainTreeId);
        TransformerEvaluateDataVo vo = null;
        if (list != null && !list.isEmpty()) {
            vo = new TransformerEvaluateDataVo();
            BeanUtils.copyPropertiesWithIgnoreNull(list.get(0), vo);
        }
        return vo;
    }

    @Override
    public boolean save(String token, TransformerEvaluateDataVo vo) {
        XTransformerEvaluateData data = new XTransformerEvaluateData();
        BeanUtils.copyPropertiesWithIgnoreNull(vo, data);
        LocalUser user = UserUtil.getLocalUser(token);
        if (null != user) {
            data.setCreateTime(LocalDateTime.now());
            data.setCreateUser(user.getUid());
        }
        data.setCreateTime(LocalDateTime.now());
        data.setId(Uuid.getUUID());
        data.setMainTreeId(vo.getMainTreeId());
        Integer count = this.addLocal(data);
        return count > 0 ? true : false;
    }

    @Override
    public int delete(String mainTreeId){
        return xTransformerEvaluateDataMapper.delete(mainTreeId);
    }
}
