package com.emr.service.recordType;

import com.emr.dao.recordType.Emr_TypeMapper;
import com.emr.dao.recordType.Emr_Type_RelatedMapper;
import com.emr.entity.Power_User;
import com.emr.entity.recordType.Emr_Type;
import com.emr.entity.recordType.Emr_Type_Related;
import com.emr.service.CommomService;
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/5 11:13
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/6/5 11:13
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional
public class EmrTypeServiceImpl implements EmrTypeService {
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private Emr_TypeMapper emrTypeMapper;
    @Autowired
    private Emr_Type_RelatedMapper emrTypeRelatedMapper;
    @Override
    public List<Emr_Type> selectAllByCreater(Integer isEffective, HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String userName = user.getUserName();
        Integer roleId = user.getRoleId();
        //系统管理员全查
        if(null != roleId && roleId == 0){
            return emrTypeMapper.selectAllByCreater(null,isEffective);
        }else if(null != roleId && roleId == -100){
            StringBuilder creaters = CommomService.creaters(request);
            return emrTypeMapper.selectAllByCreater(creaters.toString(),isEffective);
        }else{
            //按创建者id查
            userName = "'" + userName + "'";
            return emrTypeMapper.selectAllByCreater(userName,isEffective);
        }
    }

    @Override
    public List<Emr_Type> selectAllByCreater2(Integer isEffective, HttpServletRequest request) {
        String userName = "admin";
        Integer roleId = 0;
        //系统管理员全查
        if(null != roleId && roleId == 0){
            return emrTypeMapper.selectAllByCreater(null,isEffective);
        }else if(null != roleId && roleId == -100){
            StringBuilder creaters = CommomService.creaters(request);
            return emrTypeMapper.selectAllByCreater(creaters.toString(),isEffective);
        }else{
            //按创建者id查
            userName = "'" + userName + "'";
            return emrTypeMapper.selectAllByCreater(userName,isEffective);
        }
    }


    @Override
    public Emr_Type checkTypeNameByCreater(String typeName,HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String creater = user.getUserName();
        return emrTypeMapper.checkTypeNameByCreater(creater,typeName);
    }

    @Override
    public Emr_Type checkTypeFlagByCreater(String typeFlag,HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String creater = user.getUserName();
        return emrTypeMapper.checkTypeFlagByCreater(creater,typeFlag);
    }

    @Override
    public void addEmrType(Emr_Type emrType,HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String userName = user.getUserName();
        if(emrType.getSelectFlag() == 1){
            Emr_Type type = emrTypeMapper.selectFlagExist(userName);
            //先把该用户有设置的默认模板selectFlag= 1置成2
            if(null != type){
                type.setSelectFlag(0);
                emrTypeMapper.updateByPrimaryKeySelective(type);
            }else{
                emrType.setSelectFlag(1);
            }
        }
        emrType.setCreater(userName);
        emrType.setUpdater(userName);
        emrType.setCreateTime(fmt.format(new Date()));
        emrType.setUpdateTime(fmt.format(new Date()));
        emrTypeMapper.insert(emrType);
    }

    @Override
    public void updateEmrType(Emr_Type emrType,Integer selectFlagId,HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String userName = user.getUserName();
        //如果不是该默认模板且修改为默认，则取消原来默认
        Integer selectFlag = emrType.getSelectFlag();
        if(selectFlag != null && selectFlag == 1 && null != selectFlagId && !emrType.getId().equals(selectFlagId)){
            Emr_Type type = emrTypeMapper.selectByPrimaryKey(selectFlagId);
            type.setSelectFlag(0);
            emrTypeMapper.updateByPrimaryKeySelective(type);
        }
        emrType.setUpdater(userName);
        emrType.setUpdateTime(fmt.format(new Date()));
        emrTypeMapper.updateByPrimaryKeySelective(emrType);
    }

    @Override
    public void deleteEmrType(Integer id) {
        emrTypeMapper.deleteByPrimaryKey(id);
        //顺带类别归属分段一并删除
        emrTypeRelatedMapper.deleteByTypeId(id);
    }

    @Override
    public Emr_Type selectByPrimaryKey(Integer id) {
        Emr_Type emr_type = emrTypeMapper.selectByPrimaryKey(id);
        return emr_type;
    }

    @Override
    public Emr_Type selectFlagExist(HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        //roleId为0系统管理员，creater为null，否则传入creater
        Integer roleId = user.getRoleId();
        String creater = user.getUserName();
        if(null != roleId && roleId == 0){
            return emrTypeMapper.selectFlagExist(null);
        }else{
            return emrTypeMapper.selectFlagExist(creater);
        }
    }
}
