package com.nantian.cloud.dbcapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.dbcapp.entity.CompanyContacter;
import com.nantian.cloud.dbcapp.entity.EnterpriseState;
import com.nantian.cloud.dbcapp.mapper.energyinfo.CompanyContacterMapper;
import com.nantian.cloud.dbcapp.mapper.record.EnterpriseStateMapper;
import com.nantian.cloud.dbcapp.service.CompanyContacterService;
import com.nantian.cloud.dbcapp.service.EnterpriseStateService;
import com.nantian.cloud.dbcapp.unit.MD5Util;
import com.nantian.cloud.dbcapp.unit.SnowflakeIDWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service("CompanyContacterService")
public class CompanyContacterServiceImpl extends ServiceImpl<CompanyContacterMapper, CompanyContacter> implements CompanyContacterService {

    @Autowired
    SnowflakeIDWorker snowflakeIDWorker;

    @Override
    public List<CompanyContacter> getData(String epc) {
        return baseMapper.getData(epc);
    }

    @Override
    public Object saveCompanyContacter(List<CompanyContacter> list) {
        List<CompanyContacter> insertList = new ArrayList<>();
        List<CompanyContacter> updateList = new ArrayList<>();
        list.forEach(r->{
            if (r.getId()==null){
                r.setId(snowflakeIDWorker.getID());
                r.setDigest(MD5Util.getDigest(r.getEnterpriseCode(), r.getContacterName(),
                        r.getPhone(), r.getDept()));
                insertList.add(r);
            }else {
                updateList.add(r);
            }
        });
        if (insertList.size()>0){
            baseMapper.saveCompanyContacter(insertList);

        }
        if (updateList.size()>0){
            baseMapper.updateCompanyContacter(updateList);
        }
        return "";
    }
}

