package com.kidgrow.usercenter.service.impl; import cn.hutool.core.date.DateTime; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.kidgrow.common.constant.CommonConstant; import com.kidgrow.common.model.PageResult; import com.kidgrow.common.model.ResultBody; import com.kidgrow.common.model.SysOrganization; import com.kidgrow.common.model.UserType; import com.kidgrow.common.service.impl.SuperServiceImpl; import com.kidgrow.common.utils.DateUtils; import com.kidgrow.oprationcenter.feign.ProductOrderService; import com.kidgrow.oprationcenter.model.ProductOrder; import com.kidgrow.oprationcenter.model.ProductOrderDetail; import com.kidgrow.usercenter.mapper.SysDepartmentMapper; import com.kidgrow.usercenter.model.SysDepartment; import com.kidgrow.usercenter.model.SysHospital; import com.kidgrow.usercenter.service.ISysDepartmentService; import com.kidgrow.usercenter.service.ISysHospitalService; import com.kidgrow.usercenter.service.ISysOrganizationService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.MapUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; /** * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020
* * @version 1.0 * @Description: 科室表
* @Project: 用户中心
* @CreateDate: Created in 2020-04-02 14:02:50
* @Author: liuke */ @Slf4j @Service public class SysDepartmentServiceImpl extends SuperServiceImpl implements ISysDepartmentService { @Autowired private ISysOrganizationService iSysOrganizationService; @Autowired private ISysHospitalService iSysHospitalService; @Autowired private ProductOrderService productOrderService; @Autowired private PasswordEncoder passwordEncoder; @Value("${spring.profiles.active}") private String envName; /** * 列表 * * @param params * @return */ @Override public PageResult findList(Map params) { Page page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit")); List list = baseMapper.findList(page, params); return PageResult.builder().data(list).code(0).count(page.getTotal()).build(); } /** * 根据SysDepartment对象当做查询条件进行查询 * * @param sysDepartment * @return SysDepartment */ @Override public SysDepartment findByObject(SysDepartment sysDepartment) { return baseMapper.findByObject(sysDepartment); } @Override public boolean updatePay(Long departmentId, boolean isPay) { return baseMapper.updatePay(departmentId, isPay); } @Override public ResultBody findAll(Map params) { return ResultBody.ok().data(baseMapper.selectByMap(params)); } @Override public ResultBody findListByHospitalId(Map params) { //查询组织 Long id = MapUtils.getLong(params, "id"); List sysDepartments = new ArrayList<>(); SysHospital byId = iSysHospitalService.getById(id); if (byId != null) { params = new HashMap<>(); params.put("org_parent_id", byId.getOrgId()); List sysOrganizations = iSysOrganizationService.listByMap(params); if (sysOrganizations.size() > 0) { List collect = sysOrganizations.stream().map(e -> e.getId()).collect(Collectors.toList()); QueryWrapper queryWrapper = new QueryWrapper(); queryWrapper.in("org_id", collect); sysDepartments = baseMapper.selectList(queryWrapper); } } return ResultBody.ok().data(sysDepartments); } @Override public String checkDepartmentName(Long hosId, String departmentName) { String departName = baseMapper.checkDepartmentName(hosId, departmentName); return departName; } @Override public ResultBody getHealth(SysDepartment sysDepartment) { SysDepartment department = baseMapper.selectById(sysDepartment.getId()); if (department == null) { return ResultBody.failed("该数据为空"); } else { if (department.getIsHealth()) { Date now = new Date(); Date endDate = DateUtils.addDays(department.getHealthEndTime(), 1); if (now.getTime() < department.getHealthBeginTime().getTime()) { department.setIsHealth(false); return ResultBody.ok().data(department); } else if (department.getHealthBeginTime().getTime() <= now.getTime() && now.getTime() <= endDate.getTime()) { return ResultBody.ok().data(department); } else if (endDate.getTime() < now.getTime()) { department.setIsHealth(false); return ResultBody.ok().data(department); } return ResultBody.ok(); } else { return ResultBody.ok().data(false); } } } /** * 初始化私有云本地数据库 * * @param hospitalId 医院ID * @param departmentId 科室ID * @return java.lang.String 私有云数据库初始化SQL */ public String initPrivateData(Long hospitalId, Long departmentId) { String initSQL = ""; // 初始化数据SQL String organizationSQL = ""; // 组织数据SQL String hospitalSQL = ""; // 医院数据SQL String departmentSQL = ""; // 科室数据SQL String doctorSQL = ""; // 医生数据SQL String userSQL = ""; // 用户数据SQL String userOrgSQL = ""; // 组织用户关系数据SQL String roleUserSQL = ""; // 角色用户关系数据SQL String productOrderSQL = ""; // 合同信息数据SQL String productOrderDetailSQL = ""; // 合同明细(套餐)信息数据SQL String productOrderRecordSQL = ""; // 合同充值记录表SQL // 时间转换格式 SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); String createTime = ""; String updateTime = ""; // 1、根据hospitalId从sys_hospital中查询医院信息 SysHospital hospitalInfo = iSysHospitalService.getById(hospitalId); if (hospitalInfo != null) { createTime = "'" + dateTimeFormat.format(hospitalInfo.getCreateTime()) + "'"; updateTime = (hospitalInfo.getUpdateTime() == null) ? "NULL" : "'" + dateTimeFormat.format(hospitalInfo.getUpdateTime()) + "'"; hospitalSQL = " INSERT INTO user_center_pri.sys_hospital VALUES (" + hospitalInfo.getId() + ", " + "'" + hospitalInfo.getHospitalName() + "', " + hospitalInfo.getOrgId() + ", " + "'" + hospitalInfo.getHospitalShortName() + "', " + "'" + hospitalInfo.getHospitalCode() + "', " + "'" + hospitalInfo.getHospitalTypeId() + "', " + "'" + hospitalInfo.getHospitalTypeName() + "', " + "'" + hospitalInfo.getHospitalTel() + "', " + "'" + hospitalInfo.getHospitalProvince() + "', " + "'" + hospitalInfo.getHospitalCity() + "', " + "'" + hospitalInfo.getHospitalArea() + "', " + "'" + hospitalInfo.getAreaCode() + "', " + "'" + hospitalInfo.getHospitalLink() + "', " + "'" + hospitalInfo.getHospitalAdress() + "', " + "'" + hospitalInfo.getLatitude() + "', " + "'" + hospitalInfo.getLongitude() + "', " + "'" + hospitalInfo.getHospitalQualifiedId() + "', " + "'" + hospitalInfo.getHospitalQualifiedName() + "', " + "'http://123.kidgrow.cloud/OtherImage/2020-08-12/logo.jpg'," + "NULL," + "NULL," + "'" + hospitalInfo.getHospitalAbout() + "', " + "0, 0, " + hospitalInfo.getHospitalState() + ", " + hospitalInfo.getEnabled() + ", " + "'" + hospitalInfo.getTenantId() + "', " + hospitalInfo.getIsDel() + ", " + hospitalInfo.getCreateUserId() + ", " + "'" + hospitalInfo.getCreateUserName() + "', " + hospitalInfo.getUpdateUserId() + ", " + "'" + hospitalInfo.getUpdateUserName() + "', " + createTime + ", " + updateTime + ", " + "'" + hospitalInfo.getCreateUserOrgCode() + "' " + ");\n\r"; } else { log.error("【{}】医院信息读取失败",hospitalId); return "医院信息读取失败,请重新生成SQL!"; } // 2、根据departmentId从sys_department中查询科室信息 SysDepartment department = baseMapper.selectById(departmentId); if (department != null) { createTime = "'" + dateTimeFormat.format(department.getCreateTime()) + "'"; updateTime = (department.getUpdateTime() == null) ? "NULL" : "'" + dateTimeFormat.format(department.getUpdateTime()) + "'"; departmentSQL = " INSERT INTO user_center_pri.sys_department VALUES (" + department.getId() + ", " + "'" + department.getDepartmentName() + "', " + department.getOrgId() + ", " + department.getServerUserId() + ", " + "'" + department.getServerUserName() + "', " + "'" + department.getServerUserTel() + "', " + department.getIsDel() + ", " + department.getEnabled() + ", " + department.getCreateUserId() + ", " + "'" + department.getCreateUserName() + "', " + department.getUpdateUserId() + ", " + "'" + department.getUpdateUserName() + "', " + createTime + ", " + updateTime + ", " + department.getSaleUserId() + ", " + "'" + department.getSaleUserName() + "', " + "'" + department.getSaleUserTel() + "', " + department.getAccountsCount() + ", " + "0,0,NULL,NULL, " + "'" + department.getDepartmentLink() + "', " + "'" + department.getDepartmentTel() + "', " + "0,0,1," + "'" + department.getPrivateServerGuuid() + "' " + ");\n\r"; } else { log.error("【{}】科室信息读取失败",departmentId); return "科室信息读取失败,请重新生成SQL!"; } // 3、根据医院和科室表中的org_id查询sys_organization中查询组织信息 List organizationList = iSysOrganizationService.list( new QueryWrapper() .in("id", hospitalInfo.getOrgId(), department.getOrgId()) .eq("is_del", 0) .eq("enabled", 1) ); if ((organizationList != null) && (organizationList.size() > 0)) { for (int i = 0; i < organizationList.size(); i++) { SysOrganization organization = organizationList.get(i); createTime = "'" + dateTimeFormat.format(organization.getCreateTime()) + "'"; updateTime = (organization.getUpdateTime() == null) ? "NULL" : "'" + dateTimeFormat.format(organization.getUpdateTime()) + "'"; organizationSQL += " INSERT INTO user_center_pri.sys_organization VALUES (" + organization.getId() + ", " + organization.getOrgLevel() + ", " + organization.getOrgAttr() + ", " + organization.getOrgParentId() + ", " + "'" + organization.getOrgName() + "', " + organization.getOrgOrder() + ", " + organization.getIsDel() + ", " + organization.getEnabled() + ", " + organization.getCreateUserId() + ", " + "'" + organization.getCreateUserName() + "', " + organization.getUpdateUserId() + ", " + "'" + organization.getUpdateUserName() + "', " + createTime + ", " + updateTime + ", " + "'" + organization.getCreateUserOrgCode() + "', " + "'" + organization.getOrgCode() + "' " + ");\n\r"; } } // 4、给科室创建管理员账号 // 4.1 sys_user创建账号【userName:admin, password:123456,Mobile:admin,Type:DOCTOR,tenantId:】 Long userId = IdWorker.getId(); userSQL = " INSERT INTO user_center_pri.sys_user VALUES (" + userId.toString() + ", " + "'admin', " + "'" + passwordEncoder.encode("123456") + "', " + "'admin', " + "NULL, " + "'admin', " + "0, 1, " + "'"+ UserType.DOCTOR.name()+"', " + "'" + dateTimeFormat.format(DateTime.now()) + "', " + "NULL, NULL, NULL, 0, " + "'" +CommonConstant.H_TENANT + "', " + CommonConstant.ADMIN_USER_ID.toString() +", " + "'" + CommonConstant.ADMIN_USER_NAME + "', " + "NULL, 0, " + "NULL, "+ // create_user_org_code "1" + ");\n\r"; // 4.2、 sys_user_org创建记录 // 用户与医院的关系数据 userOrgSQL = " INSERT INTO user_center_pri.sys_user_org VALUES (" + IdWorker.getId() + ", " + userId.toString() + ", " + hospitalInfo.getOrgId().toString() + ", " + hospitalId.toString() + ", " + CommonConstant.SYSTEM_ORG_HOS_LEVEL.toString() + ", " + "1, 0, " + CommonConstant.ADMIN_USER_ID.toString() +", " + "'" + CommonConstant.ADMIN_USER_NAME + "', " + "NULL, NULL, " + "'" + dateTimeFormat.format(DateTime.now()) + "', " + "NULL, "+ "NULL " + // create_user_org_code ");\n\r"; // 用户与科室的关系数据 userOrgSQL += " INSERT INTO user_center_pri.sys_user_org VALUES (" + IdWorker.getId() + ", " + userId.toString() + ", " + department.getOrgId().toString() + ", " + departmentId.toString() + ", " + CommonConstant.SYSTEM_ORG_DEP_LEVEL.toString() + ", " + "1, 0, " + CommonConstant.ADMIN_USER_ID.toString() +", " + "'" + CommonConstant.ADMIN_USER_NAME + "', " + "NULL, NULL, " + "'" + dateTimeFormat.format(DateTime.now()) + "', " + "NULL, "+ "NULL " + // create_user_org_code ");\n\r"; // 4.3、 sys_role_user创建记录 roleUserSQL = " INSERT INTO user_center_pri.sys_role_user VALUES ( " + userId.toString() + ", "+ CommonConstant.HOSPITAL_DOCTOR_ID +" );\n\r" + // 普通医生 " INSERT INTO user_center_pri.sys_role_user VALUES ( " + userId.toString() + ", "+ CommonConstant.HOSPITAL_ADMIN_ID +");\n\r" ; // 医院管理员 // 4.4、 sys_doctor创建记录 doctorSQL = " INSERT INTO user_center_pri.sys_doctor VALUES (" + IdWorker.getId() + ", " + userId.toString() + ", " + hospitalInfo.getId().toString() + ", " + "'" + hospitalInfo.getHospitalName() + "', " + department.getId().toString() + ", " + "'" + department.getDepartmentName() + "', " + "'管理员', NULL, NULL, 'admin', " + "NULL, NULL, NULL, NULL, NULL, " + "0, 0, 0, 0, 1, 1, 1, " + CommonConstant.ADMIN_USER_ID.toString() +", " + "'" + CommonConstant.ADMIN_USER_NAME + "', " + "NULL, NULL, " + "'" +dateTimeFormat.format(DateTime.now()) + "', " + "NULL, " + department.getSaleUserId().toString() + ", " + "'" + department.getSaleUserName() + "', " + "1, " + "'" + CommonConstant.ADMIN_USER_ORG_CODE + "'" + ");\n\r"; // 5、根据hospitalId和departmentId从opration_center.product_order中查询合同信息 Map orderMap = new HashMap<>(); orderMap.put("hospital_id", hospitalId); orderMap.put("department_id", departmentId); orderMap.put("enabled", 1); orderMap.put("is_del", 0); orderMap.put("sql_is_downloaded",0); ResultBody orderResult = productOrderService.findProductOrderListByMap(orderMap); if (orderResult.isOk()) { //List productOrderList = (List) orderResult.getData(); List productOrderList = JSON.parseArray(JSON.toJSONString(orderResult.getData()), ProductOrder.class); if ((productOrderList != null) && (productOrderList.size() > 0)) { for (int i = 0; i < productOrderList.size(); i++) { ProductOrder productOrder = productOrderList.get(i); createTime = "'" + dateTimeFormat.format(productOrder.getCreateTime()) + "'"; updateTime = (productOrder.getUpdateTime() == null) ? "NULL" : "'" + dateTimeFormat.format(productOrder.getUpdateTime()) + "'"; productOrderSQL += "INSERT INTO opration_center_pri.product_order VALUES(" + productOrder.getId() + ", " + productOrder.getHospitalId() + ", " + "'" + productOrder.getHospitalName() + "', " + productOrder.getDepartmentId() + ", " + "'" + productOrder.getDepartmentName() + "', " + "'" + productOrder.getContractNo() + "', " + "'" + productOrder.getContractTitle() + "', " + productOrder.getContractNum() + ", " + "'" + dateFormat.format(productOrder.getContractBeginTime()) + "', " + "'" + dateFormat.format(productOrder.getContractEndTime()) + "', " + "'" + dateTimeFormat.format(productOrder.getContractTime()) + "', " + productOrder.getIsDel() + ", " + productOrder.getEnabled() + ", " + "1, " + updateTime + ", " + productOrder.getCreateUserId() + ", " + "'" + productOrder.getCreateUserName() + "', " + productOrder.getUpdateUserId() + ", " + "'" + productOrder.getUpdateUserName() + "', " + createTime + ", " + "NULL, "+ // tenant_id "'" + productOrder.getCreateUserOrgCode() + "' " + ");\n\r"; // 6、根据order_id从opration_center.product_order_detail中查询合同中的套餐信息 orderMap.clear(); orderMap.put("order_id", productOrder.getId()); orderMap.put("enabled", 1); orderMap.put("is_del", 0); ResultBody detailResult = productOrderService.findProductOrderDetailListByMap(orderMap); if (detailResult.isOk()) { //List productOrderDetailList = (List) detailResult.getData(); List productOrderDetailList = JSON.parseArray(JSON.toJSONString(detailResult.getData()), ProductOrderDetail.class); if ((productOrderDetailList != null) && (productOrderDetailList.size() > 0)) { for (int j = 0; j < productOrderDetailList.size(); j++) { ProductOrderDetail productOrderDetail = productOrderDetailList.get(j); createTime = "'" + dateTimeFormat.format(productOrderDetail.getCreateTime()) + "'"; updateTime = (productOrderDetail.getUpdateTime() == null) ? "NULL" : "'" + dateTimeFormat.format(productOrderDetail.getUpdateTime()) + "'"; productOrderDetailSQL += "INSERT INTO opration_center_pri.product_order_detail VALUES( " + productOrderDetail.getId() + ", " + productOrderDetail.getOrderId() + ", " + productOrderDetail.getProId() + ", " + productOrderDetail.getProType() + ", " + productOrderDetail.getTermType() + ", " + "'" + productOrderDetail.getProName() + "', " + productOrderDetail.getIsShare() + ", " + productOrderDetail.getIsDel() + ", " + productOrderDetail.getOrderAilightCount() + ", " + productOrderDetail.getOrderRecordCount() + ", " + productOrderDetail.getAilightCount() + ", " + "'" + productOrderDetail.getAilightCountEncryption() + "', " + productOrderDetail.getRecordCount() + ", " + "'" + dateFormat.format(productOrderDetail.getProBegintime()) + "', " + "'" + productOrderDetail.getProBegintimeEncryption() + "', " + "'" + dateFormat.format(productOrderDetail.getProEndtime()) + "', " + "'" + productOrderDetail.getProEndtimeEncryption() + "', " + productOrderDetail.getEnabled() + ", " + productOrderDetail.getCreateUserId() + ", " + "'" + productOrderDetail.getCreateUserName() + "', " + productOrderDetail.getUpdateUserId() + ", " + updateTime + ", " + "'" + productOrderDetail.getUpdateUserName() + "', " + createTime + ", " + "Null, " + // tenant_id "'" + productOrderDetail.getCreateUserOrgCode() + "' " + ");\n\r"; productOrderRecordSQL += "INSERT INTO opration_center_pri.product_order_record VALUES( " + productOrderDetail.getId() + ", " + productOrderDetail.getOrderId() + ", " + productOrderDetail.getProId() + ", " + productOrderDetail.getProType() + ", " + "'" + productOrderDetail.getProName() + "', " + productOrderDetail.getIsShare() + ", " + productOrderDetail.getIsDel() + ", " + productOrderDetail.getAilightCount() + ", " + productOrderDetail.getRecordCount() + ", " + "'" + dateFormat.format(productOrderDetail.getProBegintime()) + "', " + "'" + dateFormat.format(productOrderDetail.getProEndtime()) + "', " + productOrderDetail.getEnabled() + ", " + productOrderDetail.getCreateUserId() + ", " + "'" + productOrderDetail.getCreateUserName() + "', " + productOrderDetail.getUpdateUserId() + ", " + updateTime + ", " + "'" + productOrderDetail.getUpdateUserName() + "', " + createTime + ", " + "Null, " + // tenant_id "'" + productOrderDetail.getCreateUserOrgCode() + "' " + ");\n\r"; } } else { log.error("此合同【{}】没有合同明细(套餐)",productOrder.getId()); // return "没有已开通的合同明细(套餐),请先充值,再生成SQL文"; } } else { log.error(detailResult.getMsg()); return "没有已开通的合同明细(套餐),请先充值,再生成SQL文"; } // 更新合同表中的是否下载字段,为已下载(true) productOrder.setSqlIsDownloaded(true); ResultBody updateResult = productOrderService.downLoadProductOrderOver(productOrder); if (!updateResult.isOk()) { log.error("合同表下载状态更新失败!"); } } } else { log.error("此科室【hospital_id:{},department_id:{}】没有已开通的合同记录",hospitalId,departmentId); return "没有已开通的合同记录,请先充值,再生成SQL文"; } } else { log.error(orderResult.getMsg()); return "没有已开通的合同记录,请先充值,再生成SQL文"; } if ((productOrderSQL.isEmpty()) || (productOrderDetailSQL.isEmpty())){ log.error("此科室【hospital_id:{},department_id:{}】没有已开通的合同记录",hospitalId,departmentId); return "没有已开通的合同记录,请先充值,再生成SQL文"; } initSQL = organizationSQL + hospitalSQL + departmentSQL + userSQL + userOrgSQL + roleUserSQL + doctorSQL + productOrderSQL + productOrderDetailSQL + productOrderRecordSQL; return initSQL; } }