package com.emr.service.emrCollect;

import com.emr.dao.emrCollect.Emr_CollectMapper;
import com.emr.entity.Power_User;
import com.emr.entity.emrCollect.Emr_Collect;
import com.emr.service.CommomService;
import org.apache.commons.lang3.StringUtils;
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/10 17:23
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/6/10 17:23
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional
public class EmrCollectServiceImpl implements EmrCollectService {
    @Autowired
    Emr_CollectMapper emrCollectMapper;
    @Override
    public List<Emr_Collect> selectAllCollectTypeByCreater(Integer collectTypeFlag, HttpServletRequest request) {
        //查询收藏分类集合
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        Integer userId = user.getUserId();
        //如果登录者角色id=0，全查，如若不是，按用户id查
        Integer roleId = user.getRoleId();
        if(null != roleId && roleId == 0){
            //系统管理员全查
            return emrCollectMapper.selectAllCollectTypeByCreater(null,collectTypeFlag);
        }else if(null != roleId && roleId == -100){
            //医院管理员查对应医院用户id集合
            String userIds = CommomService.userIds(request);
            return emrCollectMapper.selectAllCollectTypeByCreater(userIds, collectTypeFlag);
        }else{
            String userIds = "'" +userId+ "'";
            return emrCollectMapper.selectAllCollectTypeByCreater(userIds,collectTypeFlag);
        }
    }

    @Override
    public List<Emr_Collect> selectAllCollectByCreater(Emr_Collect collect,String startTime,String endTime,HttpServletRequest request) {
        //查询收藏分类集合
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String userName = user.getUserId().toString();
        //如果登录者角色id=0，全查，如若不是，按用户id查
        Integer roleId = user.getRoleId();
        if(null != roleId && roleId == 0){
            //系统管理员全查
            return emrCollectMapper.selectAllCollectByCreater(null,collect,startTime,endTime);
        }else if(null != roleId && roleId == -100){
            //医院管理员查对应医院用户id集合
            String userIds = CommomService.userIds(request);
            return emrCollectMapper.selectAllCollectByCreater(userIds,collect,startTime,endTime);
        }else{
            userName = "'" + userName + "'";
            return emrCollectMapper.selectAllCollectByCreater(userName,collect,startTime,endTime);
        }
    }

    @Override
    public void addCollect(Emr_Collect emrCollect,HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        Integer userId = user.getUserId();
        String userName = user.getUserName();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       /* if(StringUtils.isNoneBlank(emrCollect.getCollectTime())){
            emrCollect.setCollectTime(emrCollect.getCollectTime() + " 00:00:00");
        }*/
        emrCollect.setUserId(userId);
        emrCollect.setCreater(userName);
        emrCollect.setCreateTime(fmt.format(new Date()));
        emrCollect.setUpdater(userName);
        emrCollect.setUpdateTime(fmt.format(new Date()));
        emrCollectMapper.insert(emrCollect);
    }

    @Override
    public void updateCollect(Emr_Collect emrCollect,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");
        emrCollect.setUpdater(userName);
        emrCollect.setUpdateTime(fmt.format(new Date()));
        emrCollectMapper.updateByPrimaryKeySelective(emrCollect);
    }

    @Override
    public Emr_Collect selectCollectByUserIdAndPatientId(Integer userId, String patientId) {
        return emrCollectMapper.selectCollectByUserIdAndPatientId(userId, patientId);
    }

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