Explorar el Código

清洗wlyy_device数据代码

zd_123 hace 7 años
padre
commit
6fdc35d78b

+ 122 - 50
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/web/data/DataHandingService.java

@ -10,18 +10,21 @@ import com.yihu.wlyy.entity.device.PatientDeviceLog;
import com.yihu.wlyy.entity.doctor.profile.Doctor;
import com.yihu.wlyy.entity.manage.User;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.entity.patient.SignFamily;
import com.yihu.wlyy.health.repository.DeviceDetailDao;
import com.yihu.wlyy.repository.doctor.DoctorDao;
import com.yihu.wlyy.repository.manage.UserDao;
import com.yihu.wlyy.repository.patient.PatientDao;
import com.yihu.wlyy.repository.patient.PatientDeviceDao;
import com.yihu.wlyy.repository.patient.PatientDeviceLogDao;
import com.yihu.wlyy.repository.patient.SignFamilyDao;
import com.yihu.wlyy.service.app.device.DeviceDetailService;
import com.yihu.wlyy.service.app.device.PatientDeviceService;
import com.yihu.wlyy.service.app.health.bank.CreditLogService;
import com.yihu.wlyy.util.*;
import net.sf.json.JSONArray;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringEscapeUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -67,6 +70,8 @@ public class DataHandingService {
    private CreditLogService creditLogService;
    @Autowired
    private DeviceDetailDao deviceDetailDao;
    @Autowired
    private SignFamilyDao signFamilyDao;
    @Transactional
    public String producePatientAndDoctorPassword() {
@ -298,28 +303,33 @@ public class DataHandingService {
    public boolean updateDeviceDefalut(){
        boolean flag = true;
        try{
            String deviceSql = "SELECT wd.*,dd.category_code,dd.is_multi_user FROM device.wlyy_devices wd LEFT JOIN wlyy.dm_device dd ON wd.device_model = dd.model";
            //String deviceSql = "SELECT wd.*,dd.category_code,dd.is_multi_user FROM device.wlyy_devices wd LEFT JOIN wlyy.dm_device dd ON wd.device_model = dd.model";
            String deviceSql ="SELECT wd.*,dd.category_code,dd.is_multi_user FROM device.wlyy_devices wd LEFT JOIN wlyy.dm_device dd ON wd.device_model = dd.model where dd.category_code IN(1,2) AND (wd.binding_count IS NULL OR wd.binding_count='' OR wd.binding_count='null')";
            List<Map<String,Object>> deviceDetailList = jdbcTemplate.queryForList(deviceSql);
            //把wlyy_devices库里bindCount先补充默认值
            List<String> deviceSqlList = new ArrayList<>();
            for (Map<String,Object> deviceMap : deviceDetailList){
                String bindingCount="";
                if (StringUtils.isEmpty(String.valueOf(deviceMap.get("binding_count"))) || StringUtils.pathEquals("null",String.valueOf(deviceMap.get("binding_count")))){
                    if (org.apache.commons.lang3.StringUtils.isNotEmpty(String.valueOf(deviceMap.get("is_multi_user"))) && StringUtils.pathEquals("0",String.valueOf(deviceMap.get("is_multi_user")))){
               /* if (StringUtils.isEmpty(String.valueOf(deviceMap.get("binding_count"))) || StringUtils.pathEquals("null",String.valueOf(deviceMap.get("binding_count")))){
                    }*/
                if (org.apache.commons.lang3.StringUtils.isNotEmpty(String.valueOf(deviceMap.get("is_multi_user"))) && StringUtils.pathEquals("0",String.valueOf(deviceMap.get("is_multi_user")))){
                    bindingCount="{\"1\":\"0\"}";
                }else if (org.apache.commons.lang3.StringUtils.isNotEmpty(String.valueOf(deviceMap.get("is_multi_user"))) && StringUtils.pathEquals("1",String.valueOf(deviceMap.get("is_multi_user")))){
                    bindingCount="{\"1\":\"0\",\"2\":\"0\"}";
                }else if (StringUtils.isEmpty(String.valueOf(deviceMap.get("is_multi_user"))) || StringUtils.pathEquals("null",String.valueOf(deviceMap.get("is_multi_user")))){
                    if (String.valueOf(deviceMap.get("device_name")).contains("血糖")){
                        bindingCount="{\"1\":\"0\"}";
                    }else if (org.apache.commons.lang3.StringUtils.isNotEmpty(String.valueOf(deviceMap.get("is_multi_user"))) && StringUtils.pathEquals("1",String.valueOf(deviceMap.get("is_multi_user")))){
                    }
                    if (String.valueOf(deviceMap.get("device_name")).contains("血压")){
                        bindingCount="{\"1\":\"0\",\"2\":\"0\"}";
                    }
                    if (StringUtils.isEmpty(String.valueOf(deviceMap.get("device_name")))){
                        bindingCount="{\"1\":\"0\",\"2\":\"0\"}";
                    }else if (StringUtils.isEmpty(String.valueOf(deviceMap.get("is_multi_user"))) || StringUtils.pathEquals("null",String.valueOf(deviceMap.get("is_multi_user")))){
                        if (String.valueOf(deviceMap.get("device_name")).contains("血糖")){
                            bindingCount="{\"1\":\"0\"}";
                        }
                        if (String.valueOf(deviceMap.get("device_name")).contains("血压")){
                            bindingCount="{\"1\":\"0\",\"2\":\"0\"}";
                        }
                    }
                    //String updateDeviceSql = ;
                    deviceSqlList.add("UPDATE device.wlyy_devices SET binding_count='"+bindingCount +"' where device_code='"+String.valueOf(deviceMap.get("device_code"))+"'");
                }
                //String updateDeviceSql = ;
                deviceSqlList.add("UPDATE device.wlyy_devices SET binding_count='"+bindingCount +"' where device_code='"+String.valueOf(deviceMap.get("device_code"))+"'");
            }
            if (deviceSqlList.size()>0){
                String deviceStr[] = new String[deviceSqlList.size()];
@ -329,24 +339,28 @@ public class DataHandingService {
            //更新grant_org_code字段
            List<String> deviceNoCodeSqlList = new ArrayList<>();
            String orgCodeSql = "SELECT * FROM device.wlyy_devices WHERE grant_org_code IS NULL OR grant_org_code =''";
            String orgCodeSql = "SELECT * FROM device.wlyy_devices WHERE grant_org_code IS NULL OR grant_org_code ='' OR grant_org_code='null'";
            List<DeviceDetail> noOrgList = jdbcTemplate.query(orgCodeSql,new BeanPropertyRowMapper<>(DeviceDetail.class));
            if (noOrgList!=null && noOrgList.size()>0){
                for (DeviceDetail deviceDetail:noOrgList){
                    String orgCode = "";
                    if (deviceDetail.getOrgName().contains("嵩屿")){
                        orgCode = "3502050100";
                    }
                    if (deviceDetail.getOrgName().contains("石塘")){
                        orgCode = "3502050200";
                    }
                    if (deviceDetail.getOrgName().contains("东孚")){
                        orgCode = "3502050300";
                    }
                    if (deviceDetail.getOrgName().contains("新阳")){
                        orgCode = "3502050400";
                    if (!StringUtils.isEmpty(deviceDetail.getOrgName())){
                        if (deviceDetail.getOrgName().contains("嵩屿")){
                            orgCode = "3502050100";
                        }
                        if (deviceDetail.getOrgName().contains("石塘")){
                            orgCode = "3502050200";
                        }
                        if (deviceDetail.getOrgName().contains("东孚")){
                            orgCode = "3502050300";
                        }
                        if (deviceDetail.getOrgName().contains("新阳")){
                            orgCode = "3502050400";
                        }
                        if (org.apache.commons.lang3.StringUtils.isNotBlank(orgCode)){
                            deviceNoCodeSqlList.add("UPDATE device.wlyy_devices SET grant_org_code='"+orgCode+"' where id='"+deviceDetail.getId()+"'");
                        }
                    }
                    deviceNoCodeSqlList.add("UPDATE device.wlyy_devices SET grant_org_code='"+orgCode+"' where id='"+deviceDetail.getId()+"'");
                }
                if (deviceNoCodeSqlList.size()>0){
                    String deviceNoCodeStr[] = new String[deviceNoCodeSqlList.size()];
@ -364,7 +378,7 @@ public class DataHandingService {
    @Transactional(rollbackFor = Exception.class)
    public void updateDevice()throws Exception{
        //String patientDeviceSql = "SELECT pd.*,sf.admin_team_code,sf.hospital FROM wlyy_patient_device pd LEFT JOIN wlyy_sign_family sf ON pd.`user` = sf.patient where sf.`status`>0 GROUP BY pd.id";
        String patientDeviceSql = "SELECT pd.id,pd.device_id AS deviceId,pd.category_code categoryCode,pd.device_name deviceName,pd.device_sn deviceSn,pd.`user`,pd.user_type userType,pd.czrq,sf.admin_team_code,sf.hospital,sf.hospital_name orgName FROM wlyy_patient_device pd LEFT JOIN wlyy_sign_family sf ON pd.`user` = sf.patient where sf.`status`>0 GROUP BY pd.id";
        String patientDeviceSql = "SELECT pd.id,pd.device_id AS deviceId,pd.category_code categoryCode,pd.device_name deviceName,pd.device_sn deviceSn,pd.`user`,pd.user_type userType,pd.czrq FROM wlyy_patient_device pd where pd.category_code in(1,2) GROUP BY pd.id";
        List<Map<String,Object>> patientDeviceList = jdbcTemplate.queryForList(patientDeviceSql);
        String deviceSql = "SELECT * FROM device.wlyy_devices";
        List<DeviceDetail> deviceDetailList = jdbcTemplate.query(deviceSql,new BeanPropertyRowMapper<>(DeviceDetail.class));
@ -405,36 +419,64 @@ public class DataHandingService {
                if (deviceDetail.getDeviceCode().equals(deviceSn)){
                    i++;
                }
            }
            //判断wlyy_device里的bindCount是否正确 正确无需更新
            DeviceDetail deviceDetail = deviceDetailDao.findBySn(deviceSn);
            int count = 0;
            if (deviceDetail!=null && org.apache.commons.lang3.StringUtils.isNotBlank(deviceDetail.getBindingCount())){
                JSONObject jsonObject = new JSONObject(deviceDetail.getBindingCount());
                Iterator iterator = jsonObject.keys();
                while(iterator.hasNext()) {
                    count += jsonObject.getInt((String) iterator.next());
                }
            }
            PatientDevice patientDevice = MapListUtils.convertMap2Bean(patientDeviceMap,PatientDevice.class);
            if (i==1){
                List<String> userTypeList = map.get(patientDevice.getDeviceSn());
                String bindCount = "";
                int size = userTypeList.size();
                if (size==1){
                    if (Integer.valueOf(patientDevice.getUserType())>0 && Integer.valueOf(patientDevice.getUserType())==1){
                        bindCount = "{\"1\":\"1\",\"2\":\"0\"}";
                    }else if (Integer.valueOf(patientDevice.getUserType())>0 && Integer.valueOf(patientDevice.getUserType())==2){
                        bindCount = "{\"1\":\"0\",\"2\":\"1\"}";
                    }else if (Integer.valueOf(patientDevice.getUserType())<0){
                        bindCount = "{\"1\":\"1\"}";
                    }
                }else if (size>1){
                    if (Integer.valueOf(patientDevice.getUserType())>0){
                        bindCount = "{\"1\":\"1\",\"2\":\"1\"}";
                    }else if (Integer.valueOf(patientDevice.getUserType())<0){
                        bindCount = "{\"1\":\"2\"}";
            if (count!=i){
                if (i==1){
                    List<String> userTypeList = map.get(patientDevice.getDeviceSn());
                    String bindCount = "";
                    int size = userTypeList.size();
                    if (size==1){
                        if (Integer.valueOf(patientDevice.getUserType())>0 && Integer.valueOf(patientDevice.getUserType())==1){
                            bindCount = "{\"1\":\"1\",\"2\":\"0\"}";
                        }else if (Integer.valueOf(patientDevice.getUserType())>0 && Integer.valueOf(patientDevice.getUserType())==2){
                            bindCount = "{\"1\":\"0\",\"2\":\"1\"}";
                        }else if (Integer.valueOf(patientDevice.getUserType())<0){
                            bindCount = "{\"1\":\"1\"}";
                        }
                    }else if (size>1){
                        if (Integer.valueOf(patientDevice.getUserType())>0){
                            bindCount = "{\"1\":\"1\",\"2\":\"1\"}";
                        }else if (Integer.valueOf(patientDevice.getUserType())<0){
                            bindCount = "{\"1\":\"2\"}";
                        }
                    }
                   /* String sql = "UPDATE device.wlyy_devices SET is_grant=1,grant_admin_team='"+String.valueOf(patientDeviceMap.get
                            ("admin_team_code"))+"',grant_org_code='"+String.valueOf(patientDeviceMap.get("hospital"))+"',is_binding="+size+",binding_count='"+bindCount
                            +"',grant_time='"+DateUtil.dateToStr(patientDevice.getCzrq(),"yyyy-MM-dd HH:mm:ss")+"',org_name='"+String.valueOf(patientDeviceMap.get("orgName"))+"' where device_code='"+patientDevice.getDeviceSn()+"'";
                    sqlList.add(sql);*/
                    String sql = "UPDATE device.wlyy_devices SET is_grant=1,is_binding="+size+",binding_count='"+bindCount
                            +"',grant_time='"+DateUtil.dateToStr(patientDevice.getCzrq(),"yyyy-MM-dd HH:mm:ss")+"' where device_code='"+patientDevice.getDeviceSn()+"'";
                    sqlList.add(sql);
                }
                String sql = "UPDATE device.wlyy_devices SET is_grant=1,grant_admin_team='"+String.valueOf(patientDeviceMap.get
                        ("admin_team_code"))+"',grant_org_code='"+String.valueOf(patientDeviceMap.get("hospital"))+"',is_binding="+size+",binding_count='"+bindCount
                        +"',grant_time='"+DateUtil.dateToStr(patientDevice.getCzrq(),"yyyy-MM-dd HH:mm:ss")+"',org_name='"+String.valueOf(patientDeviceMap.get("orgName"))+"' where device_code='"+patientDevice.getDeviceSn()+"'";
                sqlList.add(sql);
            }
            //如果wlyy_devices表里没有对应的数据 则增加一条。
            if (i==0){
                String bindCount = "";
                String user= String.valueOf(patientDeviceMap.get("user"));
                SignFamily signFamily = signFamilyDao.findByPatient(user);
                if (signFamily==null && signFamilyDao.findLastJySignByPatient(user,2)!=null){
                    signFamily = signFamilyDao.findLastJySignByPatient(user,2).get(0);
                }
                String adminTeam="";
                String orgName ="";
                String orCode ="";
                if (signFamily!=null){
                    adminTeam=String.valueOf(signFamily.getAdminTeamId());
                    orgName = signFamily.getHospitalName();
                    orCode = signFamily.getHospital();
                }
                if (Integer.valueOf(patientDevice.getUserType())>0 && Integer.valueOf(patientDevice.getUserType())==1){
                    bindCount = "{\"1\":\"1\",\"2\":\"0\"}";
                }else if (Integer.valueOf(patientDevice.getUserType())>0 && Integer.valueOf(patientDevice.getUserType())==2){
@ -442,12 +484,42 @@ public class DataHandingService {
                }else if (Integer.valueOf(patientDevice.getUserType())<0){
                    bindCount = "{\"1\":\"1\"}";
                }
                deviceDetailService.insertDevice(patientDevice,String.valueOf(patientDeviceMap.get("admin_team_code")),String.valueOf(patientDeviceMap.get("hospital_name")),String.valueOf(patientDeviceMap.get("hospital")),bindCount,1);
                deviceDetailService.insertDevice(patientDevice,adminTeam,orgName,orCode,bindCount,1);
            }
        }
        String[] sqlStr = new String[sqlList.size()];
        sqlStr = sqlList.toArray(sqlStr);
        jdbcTemplate.batchUpdate(sqlStr);
        //更新wlyy_device里的org_name grant_org_code grant_admin_team
        List<String> uSqlList = new ArrayList<>();
        //String noBindSql ="SELECT * FROM device.`wlyy_devices` WHERE (grant_admin_team IS NULL OR grant_admin_team ='' OR grant_admin_team='null' OR grant_org_code IS NULL OR grant_org_code='' OR grant_org_code='null' OR grant_org_code='' OR org_name IS NULL OR org_name= '' OR org_name='null')  AND is_binding>0";
        String noBindSql ="SELECT * FROM device.`wlyy_devices` WHERE (grant_admin_team IS NULL OR grant_admin_team ='' OR grant_admin_team='null' OR grant_org_code IS NULL OR grant_org_code='' OR grant_org_code='null' OR grant_org_code='')  AND is_binding>0";
        List<DeviceDetail> noBindDeviceList = jdbcTemplate.query(noBindSql,new BeanPropertyRowMapper<>(DeviceDetail.class));
        for (DeviceDetail deviceDetail: noBindDeviceList){
            for (Map<String,Object> map1:patientDeviceList){
                String deviceSn = String.valueOf(map1.get("deviceSn"));
                if (deviceDetail.getDeviceCode().equals(deviceSn)){
                    SignFamily signFamily = signFamilyDao.findByPatient(String.valueOf(map1.get("user")));
                    if (signFamily==null){
                        List<SignFamily> signFamilyList = signFamilyDao.findLastJySignByPatient(String.valueOf(map1.get("user")),2);
                        if (signFamilyList!=null && signFamilyList.size()>0){
                            signFamily = signFamilyDao.findLastJySignByPatient(String.valueOf(map1.get("user")),2).get(0);
                        }
                    }
                    if (signFamily!=null){
                        String sql = "UPDATE device.wlyy_devices SET is_grant=1,grant_admin_team='"+signFamily.getAdminTeamId()+"',grant_org_code='"+signFamily.getHospital()+"',is_binding=1 ,org_name='"+signFamily.getHospitalName()+"' where device_code='"+deviceSn+"'";
                        uSqlList.add(sql);
                    }
                }
            }
        }
        String[] uSqlStr = new String[uSqlList.size()];
        uSqlStr = uSqlList.toArray(uSqlStr);
        jdbcTemplate.batchUpdate(uSqlStr);
    }
    public void addPointOldDeviceBind()throws Exception{

+ 2 - 2
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/web/data/DataHandlingController.java

@ -302,9 +302,9 @@ public class DataHandlingController extends BaseController {
    public String updateDevice(){
        try {
            boolean flag = dataHandingService.updateDeviceDefalut();
            /*if (flag){
            if (flag){
                dataHandingService.updateDevice();
            }*/
            }
            return write(200,"清洗数据成功!");
        }catch (Exception e){
            e.printStackTrace();