package com.steerinfo.ems.dzmaterialsf.service.impl;

import com.steerinfo.ems.Utils.DateUtils;
import com.steerinfo.ems.dzmaterialsf.mapper.DzMaterialSfMapper;
import com.steerinfo.ems.dzmaterialsf.model.DzMaterialSf;
import com.steerinfo.ems.dzmaterialsf.service.DzMaterialSFDBServer;
import com.steerinfo.ems.dzmaterialsf.service.IDzMaterialSfService;
import com.steerinfo.framework.mapper.ICBaseMapper;
import com.steerinfo.framework.service.impl.CBaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * DzMaterialSf服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2021-10-16 08:57
 * 类描述
 * 修订历史：
 * 日期：2021-10-16
 * 作者：generator
 * 参考：
 * 描述：DzMaterialSf服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "dzMaterialSfService")
public class DzMaterialSfServiceImpl extends CBaseServiceImpl<DzMaterialSf, String> implements IDzMaterialSfService {

    private static final Logger logger = LoggerFactory.getLogger(DzMaterialSfServiceImpl.class);

    @Autowired
    private DzMaterialSfMapper dzMaterialSfMapper;

    @Autowired
    private DzMaterialSFDBServer dzMaterialSFDBServer;

    @Override
    protected ICBaseMapper<DzMaterialSf, String> getMapper() {
        return dzMaterialSfMapper;
    }

    @Override
    public int dataGeneration() {
        int addSize = 0;
        int updateSize = 0;
        try {
            List<DzMaterialSf> dzMaterialSfList = dzMaterialSFDBServer.getDzMaterialSf();
            //List<DzMaterialSf> models;
            DzMaterialSf model;
            HashMap<String, Object> hashMap = new HashMap<>();
            for (DzMaterialSf dzMaterialSf : dzMaterialSfList){
                if(dzMaterialSf.getCinvccode() == null || ("").equals(dzMaterialSf.getCinvccode())) {
                    dzMaterialSf.setCinvccode("0");
                }
                dzMaterialSf.setReadtime(DateUtils.dateStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
                dzMaterialSf.setUpdateman("系统");
                dzMaterialSf.setUpdatetime(DateUtils.dateStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
                if(dzMaterialSfMapper.updateByPrimaryKeySelective(dzMaterialSf)==0){
                    dzMaterialSf.setCreateman("系统");
                    dzMaterialSf.setCreatetime(DateUtils.dateStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
                    addSize+= dzMaterialSfMapper.insertSelective(dzMaterialSf);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            logger.info("本次总共新增大宗物料收发数据条数：" + addSize);
            logger.info("本次总共更新大宗物料收发数据条数：" + updateSize);
        }
        return addSize+updateSize;
    }
}
