package com.rongwei.bscommon.sys.service.impl;


import com.rongwei.bsentity.domin.SrmEightDHistoryDo;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.SrmEightDHistorylistMapper;
import com.rongwei.bscommon.sys.service.SrmEightDHistorylistService;
import com.rongwei.rwcommon.base.R;
import dto.SrmEighrDHistoryDto;
import lombok.extern.log4j.Log4j2;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 8D计划修改历史表 服务实现类
 * </p>
 *
 * @author trj
 * @since 2023-03-13
 */
@Service("SrmEightDHistorylistServiceImpl")
@Log4j2
public class SrmEightDHistorylistServiceImpl extends ServiceImpl<SrmEightDHistorylistMapper, SrmEightDHistoryDo> implements SrmEightDHistorylistService {

    @Override
    public R saveHistory(String data) {
        log.info("接收的历史数据:{}", data);
        if (Strings.isNotEmpty(data)) {
            List<SrmEighrDHistoryDto> srmEighrDHistoryDtos = JSON.parseArray(data, SrmEighrDHistoryDto.class);
            List<SrmEightDHistoryDo> eightDHistoryEntities = new ArrayList<>();
            if (ArrayUtil.isNotEmpty(srmEighrDHistoryDtos)) {
                //当数据不为空时，拷贝给entity 进行持久化
                eightDHistoryEntities = BeanUtil.copyToList(srmEighrDHistoryDtos, SrmEightDHistoryDo.class);
                log.debug("拷贝集合实体：{}", eightDHistoryEntities);
                if (ArrayUtil.isNotEmpty(eightDHistoryEntities)) {
                    boolean flag = this.saveOrUpdateBatch(eightDHistoryEntities);
                    if (flag) {
                        return R.saveOk();
                    }
                }

            }
        }


        return R.saveError();

    }
}
