package com.kidgrow.oprationcenter.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kidgrow.advisory.model.AdvisoryDoctorSummary;
import com.kidgrow.advisory.model.AdvisoryManager;
import com.kidgrow.advisory.model.AdvisorySummary;
import com.kidgrow.advisory.model.feign.AdvisoryService;
import com.kidgrow.common.model.PageResult;
import com.kidgrow.common.model.ResultBody;
import com.kidgrow.common.service.impl.SuperServiceImpl;
import com.kidgrow.common.utils.StringUtils;
import com.kidgrow.oprationcenter.mapper.AdvisoryDoctorInfoMapper;
import com.kidgrow.oprationcenter.model.AdvisoryDoctorInfo;
import com.kidgrow.oprationcenter.model.AppointmentTime;
import com.kidgrow.oprationcenter.model.HospitalScreening;
import com.kidgrow.oprationcenter.service.IAdvisoryDoctorInfoService;
import com.kidgrow.oprationcenter.service.IAppointmentTimeService;
import com.kidgrow.redis.util.RedisUtils;
import com.kidgrow.user.feign.DoctorAnswerUserRelationService;
import com.kidgrow.user.feign.UserInfoService;
import com.kidgrow.user.model.UserInfo;
import com.kidgrow.usercenter.feign.SysDoctorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.text.DecimalFormat;
import java.util.*;
/**
* 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020
*
* @Description:
* @Project:
* @CreateDate: Created in 2021/3/1 14:34
* @Author: dougang
*/
@Slf4j
@Service
public class AdvisoryDoctorInfoServiceImpl extends SuperServiceImpl implements IAdvisoryDoctorInfoService {
@Autowired
private SysDoctorService doctorService;
@Autowired
private IAppointmentTimeService appointmentTimeService;
@Autowired
private AdvisoryService advisoryService;
@Autowired
private UserInfoService userInfoService;
@Autowired
private RedisUtils redisUtils;
@Autowired
private DoctorAnswerUserRelationService doctorAnswerUserRelationService;
/**
* 医生列表
*
* @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();
}
/**
* 通过ID删除
*
* @param id
* @return
*/
@Override
public int removeById(Long id) {
ResultBody resultBody = doctorAnswerUserRelationService.deletByDoctorId(String.valueOf(id));
log.info("删除医生关系返回值=>" + resultBody);
appointmentTimeService.delByAdvisoryId(id);
baseMapper.removeById(id);
return 1;
}
/**
* 根据条件查询
*
* @param params
* @return
*/
@Override
public List findByObject(Map params) {
return baseMapper.findByObject(params);
}
/**
* 更新医生信息
*
* @param map
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public ResultBody updateDoctorInfo(Map map) {
//医生ID
String id = MapUtils.getString(map, "doctorId");
//价格
double price = MapUtils.getDouble(map, "price") * 100;
//最大人数
int maxPeople = MapUtils.getIntValue(map, "maxPeople");
//预约时间
List appointmments = JSONArray.parseArray(JSONArray.toJSONString(map.get("appointments")), AppointmentTime.class);
AdvisoryDoctorInfo advisoryDoctorInfo = baseMapper.selectById(Long.parseLong(id));
if (advisoryDoctorInfo == null) {
return ResultBody.failed().msg("查询医生信息失败");
}
advisoryDoctorInfo.setPrice(price);
advisoryDoctorInfo.setMaxPeople(maxPeople);
int b = baseMapper.updateById(advisoryDoctorInfo);
if (b == 0) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return ResultBody.failed().msg("操作失败");
}
if (appointmments != null && appointmments.size() > 0) {
appointmentTimeService.delByAdvisoryId(Long.parseLong(id));
for (AppointmentTime time : appointmments) {
time.setAdvisoryId(Long.parseLong(id));
appointmentTimeService.saveOrUpdate(time);
}
}
return ResultBody.ok().msg("操作成功");
}
/**
* 查询咨询医生详情
*
* @param map
* @return
*/
@Override
public ResultBody getDoctorById(Map map) {
//医生ID
String id = MapUtils.getString(map, "doctorId");
//是否需要查询预约时间
int apppointmentFlag = MapUtils.getIntValue(map, "apppointmentFlag");
AdvisoryDoctorInfo byId = baseMapper.selectById(Long.parseLong(id));
if (byId == null) {
return ResultBody.failed().msg("未找到医生信息");
}
ResultBody byId1 = doctorService.findById(byId.getDoctorId());
if (byId1.getCode() != 0 || byId1.getData() == null) {
return ResultBody.failed().msg("查询医生信息失败");
}
JSONObject doctor = JSONObject.parseObject(JSONObject.toJSONString(byId1.getData()));
JSONObject json = new JSONObject();
json.put("doctorName", doctor.getString("doctorName"));
json.put("departmentName", doctor.getString("departmentName"));
json.put("hospitalName", doctor.getString("hospitalName"));
json.put("head", doctor.getString("doctorLogo"));
json.put("phone", doctor.getString("doctorTel"));
json.put("otherLink", doctor.getString("doctorOtherLink"));
json.put("about", doctor.getString("doctorAbout"));
json.put("price", byId.getPrice() / 100);
json.put("specialty", byId.getSpecialty());
json.put("service", byId.getService());
json.put("maxPeople", byId.getMaxPeople());
json.put("rank", doctor.getString("doctorRank"));
if (apppointmentFlag == 1) {
List byAdvisoryId = appointmentTimeService.findByAdvisoryId(Long.parseLong(id));
if (byAdvisoryId.size() > 0) {
JSONArray array = new JSONArray();
JSONObject obj = null;
for (AppointmentTime app : byAdvisoryId) {
boolean flag = true;
for (int i = 0; i < array.size(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
if (jsonObject.getString("name").equals(app.getName())) {
if (app.getAfternoon() == 1) {
jsonObject.put("afternoon", 1);
} else if (app.getEvening() == 1) {
jsonObject.put("evening", 1);
} else if (app.getMorning() == 1) {
jsonObject.put("morning", 1);
}
flag = false;
array.add(jsonObject);
break;
}
}
if (flag) {
obj = new JSONObject();
obj.put("name", app.getName());
obj.put("afternoon", app.getAfternoon());
obj.put("evening", app.getEvening());
obj.put("morning", app.getMorning());
array.add(obj);
}
}
json.put("appointments", array);
}
}
return ResultBody.ok().data(json);
}
/**
* 获取筛查咨询医生列表
*
* @param hospitalId
* @return
*/
@Override
public ResultBody getSreeningAdvisoryDoctorList(Long hospitalId) {
Map map = new HashMap<>(16);
map.put("hospitalId", hospitalId);
JSONArray array = new JSONArray();
JSONObject json = null;
List byObject = baseMapper.findByObject(map);
if (byObject.size() > 0) {
for (AdvisoryDoctorInfo doc : byObject) {
json = new JSONObject();
json.put("doctorName", doc.getDoctorName());
json.put("departmentName", doc.getDeptName());
json.put("hospitalName", doc.getHospitalName());
json.put("head", doc.getHead());
json.put("about", doc.getIntroduction());
json.put("price", doc.getPrice() / 100);
json.put("specialty", doc.getSpecialty());
json.put("service", doc.getService());
json.put("id", String.valueOf(doc.getId()));
ResultBody byId1 = doctorService.findById(doc.getDoctorId());
if (byId1.getCode() == 0 || byId1.getData() != null) {
JSONObject doctor = JSONObject.parseObject(JSONObject.toJSONString(byId1.getData()));
json.put("rank", doctor.getString("doctorRank"));
}
array.add(json);
}
}
return ResultBody.ok().data(array).msg("操作成功");
}
/**
* 获取咨询医生列表
*
* @param map
* @return
*/
@Override
public ResultBody getAdvisoryDoctorList(Map map) {
double lat = MapUtils.getDouble(map, "lat");
double lon = MapUtils.getDouble(map, "lon");
String excludeHospitalId = MapUtils.getString(map, "excludeHospitalId");
List