package com.kidgrow.usercenter.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kidgrow.common.constant.SecurityConstants;
import com.kidgrow.common.model.*;
import com.kidgrow.redis.util.RedisConstant;
import com.kidgrow.redis.util.RedisUtils;
import com.kidgrow.usercenter.mapper.SysRoleMapper;
import com.kidgrow.usercenter.mapper.SysRoleOrganizationMapper;
import com.kidgrow.usercenter.mapper.SysUserRoleMapper;
import com.kidgrow.usercenter.model.SysRoleOrganization;
import com.kidgrow.usercenter.model.SysRoleUser;
import com.kidgrow.usercenter.service.ISysRoleOrganizationService;
import com.kidgrow.usercenter.service.ISysUserOrgService;
import com.kidgrow.usercenter.service.ISysUserService;
import com.kidgrow.usercenter.vo.SysDoctorDto;
import com.kidgrow.usercenter.vo.SysDoctorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kidgrow.common.service.impl.SuperServiceImpl;
import java.util.*;
import org.apache.commons.collections4.MapUtils;
import lombok.extern.slf4j.Slf4j;
import com.kidgrow.usercenter.model.SysDoctor;
import com.kidgrow.usercenter.mapper.SysDoctorMapper;
import com.kidgrow.usercenter.service.ISysDoctorService;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
/**
* 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020
*
* @version 1.0
* @Description:
* @Project: 用户中心
* @CreateDate: Created in 2020-04-02 14:02:50
* @Author: liuke
*/
@Slf4j
@Service
public class SysDoctorServiceImpl extends SuperServiceImpl implements ISysDoctorService {
/**
* 列表
*
* @param params
* @return
*/
@Autowired
private ISysUserService iSysUserService;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private ISysUserOrgService sysUserOrgService;
@Autowired
private RedisUtils redisUtils;
@Autowired
private SysRoleOrganizationServiceImpl SysRoleOrganizationServiceImpl;
@Autowired
private SysRoleMapper sysRoleMapper;
@Autowired
private SysUserRoleMapper sysUserRoleMapper;
private final String HOSPITAL_ADMIN="hospital_admin";
@Override
public PageResult findList(Map params, SysUser user) {
Page page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
//添加权限
SysRoleOrganizationServiceImpl.getRoleOrg(params, user);
List list = baseMapper.findList(page, params);
List listvo = new ArrayList<>();
list.forEach(e -> {
SysDoctorVo vo = new SysDoctorVo();
BeanCopier beanCopier = BeanCopier.create(SysDoctor.class, SysDoctorVo.class, false);
beanCopier.copy(e, vo, null);
//查询用户的登录账号;
if (e.getUserId() != null) {
SysUser sysUser = iSysUserService.getById(e.getUserId());
if (sysUser != null) {
vo.setUsername(sysUser.getUsername());
}
}
listvo.add(vo);
});
return PageResult.builder().data(listvo).code(0).count(page.getTotal()).build();
}
/**
* 根据SysDoctor对象当做查询条件进行查询
*
* @param sysDoctor
* @return SysDoctor
*/
@Override
public SysDoctor findByObject(SysDoctor sysDoctor) {
return baseMapper.findByObject(sysDoctor);
}
@Override
public boolean delete(Long id) {
//查询user表
SysDoctor sysDoctor = baseMapper.selectById(id);
if (sysDoctor != null && sysDoctor.getUserId() != null) {
boolean b = iSysUserService.delUser(sysDoctor.getUserId());
}
sysDoctor.setIsDel(true);
int i = baseMapper.deleteById(id);
return true;
}
@Override
public ResultBody enable(Map params) {
Long aLong = MapUtils.getLong(params,"id");
SysDoctor sysDoctor = baseMapper.selectById(aLong);
Boolean enabled = MapUtils.getBoolean(params, "enabled");
if (sysDoctor != null && sysDoctor.getUserId() != null) {
SysUser byId = iSysUserService.getById(sysDoctor.getUserId());
if(byId!=null){
byId.setEnabled(enabled);
iSysUserService.updateById(byId);
}
}
if(sysDoctor!=null){
sysDoctor.setEnabled(enabled);
baseMapper.updateById(sysDoctor);
}else {
return ResultBody.failed("禁用失败");
}
return ResultBody.ok(0,"禁用成功");
}
@Override
@Transactional
public boolean saveOrUpdateSer(SysDoctorDto sysDoctor, HttpServletRequest request,SysUser user) {
String id = request.getHeader(SecurityConstants.USER_ID_HEADER);
if(null==sysDoctor){
return false;
}else {
if (sysDoctor.getId()==null) {
//保存
SysUser sysUser=new SysUser();
sysUser.setUsername(sysDoctor.getUsername());
sysUser.setPassword(passwordEncoder.encode(sysDoctor.getPassword()));
sysUser.setNickname(sysDoctor.getHospitalName());
sysUser.setHeadImgUrl(sysDoctor.getDoctorLogo());
sysUser.setMobile(sysDoctor.getDoctorTel());
sysUser.setType("doctor");
sysUser.setTenantId("hospital");
sysUser.setCreateTime(new Date());
SysUser byId = iSysUserService.getById(id);
if(byId!=null){
sysUser.setCreateUserId(byId.getId());
sysUser.setCreateUserName(byId.getUsername());
}
iSysUserService.save(sysUser);
//保存 角色信息
Map map= new HashMap<>();
map.put("code",HOSPITAL_ADMIN);
map.put("enabled",1);
map.put("is_del",0);
List sysRoles = sysRoleMapper.selectByMap(map);
if(sysRoles.size()>0){
//保存一个角色
SysRole sysRole = sysRoles.get(0);
SysRoleUser sysRoleUser=new SysRoleUser();
sysRoleUser.setRoleId(sysRole.getId());
sysRoleUser.setUserId(sysUser.getId());
int insert = sysUserRoleMapper.insert(sysRoleUser);
}
//保存doctor的数据
SysDoctor sysDoc=new SysDoctor();
BeanCopier beanCopier = BeanCopier.create(SysDoctorDto.class, SysDoctor.class, false);
beanCopier.copy(sysDoctor,sysDoc,null);
sysDoc.setUserId(sysUser.getId());
if(user.getOrganizations()!=null){
List organizations = user.getOrganizations();
sysDoc.setCreateUserOrgCode(organizations.get(organizations.size()-1).getOrgCode());
}
baseMapper.insert(sysDoc);
//保存将sys_user_org 保存部门
SysUserOrg sysUserOrg=new SysUserOrg();
sysUserOrg.setUserId(sysUser.getId());
sysUserOrg.setOrgId(sysDoctor.getDepartmentId());
//保存公司
sysUserOrgService.saveOrUpdate(sysUserOrg);
SysUserOrg sysGongsi=new SysUserOrg();
sysGongsi.setUserId(sysUser.getId());
sysGongsi.setOrgId(sysDoctor.getHospitalId());
sysUserOrgService.saveOrUpdate(sysGongsi);
//保存到Redis
boolean hset = redisUtils.hset(RedisConstant.USER_ORGANIZATION, sysUserOrg.getUserId().toString(), sysUserOrg);
}else {
//更新
SysDoctor getOne = baseMapper.selectById(sysDoctor.getId());
Map columnMap=new HashMap<>();
columnMap.put("user_id",sysDoctor.getUserId());
boolean b = sysUserOrgService.removeByMap(columnMap);
//保存将sys_user_org
SysUserOrg sysUserOrg=new SysUserOrg();
sysUserOrg.setUserId(getOne.getUserId());
sysUserOrg.setEnabled(false);
sysUserOrg.setOrgId(sysDoctor.getDepartmentId());
sysUserOrg.setCreateTime(new Date());
sysUserOrgService.saveOrUpdate(sysUserOrg);
SysUserOrg sysGongsi=new SysUserOrg();
sysGongsi.setUserId(getOne.getId());
sysGongsi.setOrgId(sysDoctor.getHospitalId());
sysUserOrgService.saveOrUpdate(sysGongsi);
baseMapper.updateById(sysDoctor);
if(redisUtils.hHasKey(RedisConstant.USER_ORGANIZATION, getOne.getUserId().toString())){
redisUtils.hdel(RedisConstant.USER_ORGANIZATION, getOne.getUserId().toString());
}
redisUtils.hset(RedisConstant.USER_ORGANIZATION, getOne.getUserId().toString(), sysUserOrg);
}
}
return true;
}
@Override
public List findByMap(Map map) {
map.put("enabled",1);
map.put("is_del",0);
return baseMapper.selectByMap(map);
}
}