package com.zcxk.meterreadingsystemv2.service; import com.zcxk.meterreadingsystemv2.common.JacksonUtil; import com.zcxk.meterreadingsystemv2.dao.DeviceMapper; import com.zcxk.meterreadingsystemv2.dao.TRemoteCustomerMapper; import com.zcxk.meterreadingsystemv2.dao.YcblhbMapper; import com.zcxk.meterreadingsystemv2.dbs.DynamicDataSourceContextHolder; import com.zcxk.meterreadingsystemv2.dto.DeviceCustomerInfo; import com.zcxk.meterreadingsystemv2.entity.TRemoteCustomer; import com.zcxk.meterreadingsystemv2.entity.Ycblhb; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.Date; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; @Slf4j @Service public class MssqlService { @Resource private DeviceMapper deviceMapper; @Resource private TRemoteCustomerMapper tRemoteCustomerMapper; @Resource YcblhbMapper ycblhbMapper; public void addCustomer() { String originName = "jl"; AtomicInteger count = new AtomicInteger(0); DynamicDataSourceContextHolder.setDataSourceKey("smart-city"); log.info("=====service当前连接的数据库是:" + DynamicDataSourceContextHolder.getDataSourceKey()); List list = deviceMapper.getDeviceCustomerInfoByCustomerId(19); DynamicDataSourceContextHolder.setDataSourceKey(originName); log.info("=====service当前连接的数据库是:" + DynamicDataSourceContextHolder.getDataSourceKey()); if (list != null && list.size() > 0) { log.info("=======================list size="+list.size()); for (DeviceCustomerInfo deviceCustomerInfo : list) { TRemoteCustomer tRemoteCustomerOld = tRemoteCustomerMapper.findByMeteraddr(deviceCustomerInfo.getWaterMeterNo()); if (tRemoteCustomerOld != null) { log.info(deviceCustomerInfo.getDeviceNo()+"已经存在"); continue; } TRemoteCustomer tRemoteCustomer = new TRemoteCustomer(); tRemoteCustomer.setFactoryid(5); tRemoteCustomer.setMeteraddr(deviceCustomerInfo.getWaterMeterNo()); tRemoteCustomer.setUsername(deviceCustomerInfo.getUserName()); tRemoteCustomer.setLinkman(deviceCustomerInfo.getUserName()); tRemoteCustomer.setPhone(deviceCustomerInfo.getUserPhone()); tRemoteCustomer.setPaperno(deviceCustomerInfo.getIdCard()); tRemoteCustomer.setAddress(deviceCustomerInfo.getLocation()); //tRemoteCustomer.setCaliber(); 口径 if (deviceCustomerInfo.getInstallTime() != null) { tRemoteCustomer.setInstalldate(deviceCustomerInfo.getInstallTime()); }else { tRemoteCustomer.setInstalldate(new Date()); } tRemoteCustomer.setIfctrlvalve(0);//是否阀控表 tRemoteCustomer.setImported(0); //log.info(JSON.toJSONString(tRemoteCustomer)); //tRemoteCustomer.setOldmeterid(); tRemoteCustomer.setUsercode(deviceCustomerInfo.getWaterFileNo());//客户编码 //tRemoteCustomer.setExtenddata1(); //tRemoteCustomer.setExtenddata2(); //tRemoteCustomer.setExtenddata3(); //tRemoteCustomer.setOldmeteraddr(); //tRemoteCustomer.setValveflag(); //tRemoteCustomer.setId(); //System.out.println(tRemoteCustomer); tRemoteCustomerMapper.insertSelective(tRemoteCustomer); count.incrementAndGet(); } log.info("=======================end list size="+list.size()+"-count="+count.get()); } } public void addHuichuanCustomer() { String originName = "huichuan"; String commOrgLevel = "10001002"; String commOrgIdenty = "10001002"; AtomicInteger count = new AtomicInteger(0); DynamicDataSourceContextHolder.setDataSourceKey("smart-city"); log.info("=====service当前连接的数据库是:" + DynamicDataSourceContextHolder.getDataSourceKey()); List list = deviceMapper.getDeviceCustomerInfoByCustomerId(38); DynamicDataSourceContextHolder.setDataSourceKey(originName); //log.info("=====service当前连接的数据库是:" + DynamicDataSourceContextHolder.getDataSourceKey()); if (list != null && list.size() > 0) { log.info("=======================list size="+list.size()); for (DeviceCustomerInfo info : list) { Ycblhb ycblhb = new Ycblhb(); ycblhb.setYhdz(info.getBuildingName() + info.getLocation()); ycblhb.setSbdz(info.getBuildingName() + info.getLocation()); ycblhb.setBc("HC0112YCB"); ycblhb.setBsm(info.getWaterMeterNo()); ycblhb.setZdsj("0101");//汇川0101 ycblhb.setYys("020101"); ycblhb.setBw(info.getBuildingName() + info.getLocation()); ycblhb.setBxh("2"); //ycblhb.setBqds(acceptListData.getReadData()); ycblhb.setBqds(BigDecimal.ZERO); ycblhb.setNhrq(info.getInstallTime()); ycblhb.setLdh(info.getBuildingName()); ycblhb.setDyh(info.getCommunityName()); ycblhb.setMph(info.getLocation()); ycblhb.setBcj("31"); ycblhb.setCommOrgLevel(commOrgLevel); ycblhb.setCommOrgIdenty(commOrgIdenty); log.info(JacksonUtil.obj2String(ycblhb)); ycblhbMapper.insertSelective(ycblhb); count.incrementAndGet(); } log.info("=======================end list size="+list.size()+"-count="+count.get()); } } }