package com.emr.service.emrReasonAnnoModel;

import com.emr.dao.emrReasonAnnoModel.Emr_Reason_Anno_ModelMapper;
import com.emr.entity.Power_User;
import com.emr.entity.emrReasonAnnoModel.Emr_Reason_Anno_Model;
import com.emr.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/6/27 14:20
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/6/27 14:20
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional
public class AnnotationTemplateServiceImpl implements AnnotationTemplateService{
    @Autowired
    private Emr_Reason_Anno_ModelMapper annoModelMapper;
    @Override
    public List<Emr_Reason_Anno_Model> selectAllAnnoTypeByCreater(Integer isEffective, HttpServletRequest request) {
        //查询分类集合
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String creater = user.getUserName();
        //如果登录者角色id=0，全查，如若不是，按用户id查
        Integer roleId = user.getRoleId();
        if(null != roleId && roleId == 0){
            //系统管理员全查
            return annoModelMapper.selectAllAnnoTypeByCreater(null,isEffective);
        }else if(null != roleId && roleId == -100){
            //医院管理员查对应医院用户id集合.
            String creaters = getCreatersByHospitalId(request);
            return annoModelMapper.selectAllAnnoTypeByCreater(creaters,isEffective);
        }else{
            creater = "'" + creater + "'";
            return annoModelMapper.selectAllAnnoTypeByCreater(creater,isEffective);
        }
    }

    @Override
    public List<Emr_Reason_Anno_Model> selectNotContentAllAnnoTypeByCreater(HttpServletRequest request) {
        //查询分类集合
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String creater = user.getUserName();
        //如果登录者角色id=0，全查，如若不是，按用户id查
        Integer roleId = user.getRoleId();
        if(null != roleId && roleId == 0){
            //系统管理员全查
            return annoModelMapper.selectNotContentAllAnnoTypeByCreater(null);
        }else if(null != roleId && roleId == -100){
            //医院管理员查对应医院用户id集合.
            String creaters = getCreatersByHospitalId(request);
            return annoModelMapper.selectNotContentAllAnnoTypeByCreater(creaters);
        }else{
            creater = "'" + creater + "'";
            return annoModelMapper.selectNotContentAllAnnoTypeByCreater(creater);
        }
    }

    @Override
    public List<Emr_Reason_Anno_Model> selectReasonOrAnnoTemplateList(String typeFlag,HttpServletRequest request) {
        //查询分类集合
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String creater = user.getUserName();
        //如果登录者角色id=0，全查，如若不是，按用户id查
        Integer roleId = user.getRoleId();
        if(null != roleId && roleId == 0){
            //系统管理员全查
            return annoModelMapper.selectReasonOrAnnoTemplateList(typeFlag,null);
        }else if(null != roleId && roleId == -100){
            //医院管理员查对应医院用户id集合.
            String creaters = getCreatersByHospitalId(request);
            return annoModelMapper.selectReasonOrAnnoTemplateList(typeFlag,creaters);
        }else{
            creater = "'" + creater + "'";
            return annoModelMapper.selectReasonOrAnnoTemplateList(typeFlag,creater);
        }
    }

    @Override
    public List<Emr_Reason_Anno_Model> selectAnnoTemplateListByCreater(String typeFlag,
                                                                       Emr_Reason_Anno_Model reasonAnnoModel,HttpServletRequest request) {
        //查询分类集合
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String creater = user.getUserName();
        //如果登录者角色id=0，全查，如若不是，按用户id查
        Integer roleId = user.getRoleId();
        if(null != roleId && roleId == 0){
            //系统管理员全查
            return annoModelMapper.selectAnnoTemplateListByCreater(typeFlag,null,reasonAnnoModel);
        }else if(null != roleId && roleId == -100){
            //医院管理员查对应医院用户id集合.
            String creaters = getCreatersByHospitalId(request);
            return annoModelMapper.selectAnnoTemplateListByCreater(typeFlag,creaters,reasonAnnoModel);
        }else{
            creater = "'" + creater + "'";
            return annoModelMapper.selectAnnoTemplateListByCreater(typeFlag,creater,reasonAnnoModel);
        }
    }

    @Override
    public void addAnno(Emr_Reason_Anno_Model reasonAnnoModel,HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String userName = user.getUserName();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        reasonAnnoModel.setCreater(userName);
        reasonAnnoModel.setCreateTime(fmt.format(new Date()));
        reasonAnnoModel.setUpdater(userName);
        reasonAnnoModel.setUpdateTime(fmt.format(new Date()));
        annoModelMapper.insert(reasonAnnoModel);
    }

    @Override
    public void updateAnno(Emr_Reason_Anno_Model reasonAnnoModel,HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String userName = user.getUserName();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        reasonAnnoModel.setUpdater(userName);
        reasonAnnoModel.setUpdateTime(fmt.format(new Date()));
        annoModelMapper.updateByPrimaryKeySelective(reasonAnnoModel);
    }

    @Override
    public void deleteByPrimaryKey(Integer id) {
        annoModelMapper.deleteByPrimaryKey(id);
        //删除时，子集的父id置空
        annoModelMapper.updateByDelParentId(id);
    }

    //封装根据医院系统管理员获取用户id逗号隔开的字符串
    private String getCreatersByHospitalId(HttpServletRequest request){
        //医院管理员查对应医院用户id集合.
        StringBuilder creaters = new StringBuilder();
        List<User> userList = (List<User>)request.getSession().getAttribute("USER_LIST");
        if(null != userList && !userList.isEmpty()){
            for(int i = 0;i < userList.size();i++){
                if(userList.get(i).getRoleId() == 0){
                    userList.remove(userList.get(i));
                    break;
                }
            }
            for(int i = 0;i < userList.size();i++){
                if(i != userList.size() - 1){
                    creaters.append("'").append(userList.get(i).getUserName()).append("',");
                }else{
                    creaters.append("'").append(userList.get(i).getUserName()).append("'");
                }
            }
        }
        return creaters.toString();
    }
}
