package com.yihu.jw.patient.service;

import com.yihu.jw.doctor.dao.BaseDoctorDao;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.entity.base.patient.BasePatientDO;
import com.yihu.jw.entity.base.patient.PatientMedicareCardDO;
import com.yihu.jw.entity.patient.BasePatientGroupDO;
import com.yihu.jw.entity.patient.BasePatientGroupDictDO;
import com.yihu.jw.patient.dao.BasePatientDao;
import com.yihu.jw.patient.dao.BasePatientGroupDao;
import com.yihu.jw.patient.dao.BasePatientGroupDictDao;
import com.yihu.jw.patient.dao.BasePatientMedicareCardDao;
import com.yihu.jw.patient.util.ConstantUtils;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.utils.hibernate.HibenateUtils;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Service;

import java.util.*;
@Service
public class BasePatientGroupDictService <T, R extends CrudRepository> extends BaseJpaService<BasePatientGroupDictDO, BasePatientGroupDictDao> {
    @Autowired
    private BasePatientGroupDao basePatientGroupDao;
    @Autowired
    private BasePatientGroupDictDao basePatientGroupDictDao;
    @Autowired
    private BaseDoctorDao basedoctorDao;
    @Autowired
    private BasePatientDao basePatientDao;
    @Autowired
    private HibenateUtils hibenateUtils;
    //创建/修改分组
    public BasePatientGroupDictDO createPatientGroupDict(String dictId,String doctor,String groupCode,String groupName){
        BaseDoctorDO baseDoctorDO = basedoctorDao.findById(doctor);
        BasePatientGroupDictDO basePatientGroupDictDO=null;
        if (StringUtils.isNotBlank(dictId)){
            basePatientGroupDictDO = basePatientGroupDictDao.findOne(dictId);
        }else {
            basePatientGroupDictDO = new BasePatientGroupDictDO();
        }
        if (baseDoctorDO!=null){
            basePatientGroupDictDO.setCreateUser(doctor);
            basePatientGroupDictDO.setCreateUserName(baseDoctorDO.getName());
        }
        basePatientGroupDictDO.setDel("1");
        basePatientGroupDictDO.setCreateTime(new Date());
        basePatientGroupDictDO.setGroupCode(groupCode);
        basePatientGroupDictDO.setGroupName(groupName);
       return basePatientGroupDictDao.save(basePatientGroupDictDO);
    }
    //删除分组
    public BasePatientGroupDictDO delPatientGroupDict(String dictId,String del){
        BasePatientGroupDictDO  basePatientGroupDictDO = basePatientGroupDictDao.findOne(dictId);
        basePatientGroupDictDO.setDel(del);
        return basePatientGroupDictDao.save(basePatientGroupDictDO);
    }
    public MixEnvelop findByDoctor(String doctor,String groupName,Integer page,Integer pageSize){
        Page<BasePatientGroupDictDO> list= basePatientGroupDictDao.findByCreateUserAndDel(doctor,"1",new PageRequest(page-1,pageSize));
        int listCount= basePatientGroupDictDao.findByDoctorCount(doctor);
        MixEnvelop mixEnvelop = new MixEnvelop();
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setCurrPage(page+1);
        mixEnvelop.setDetailModelList(list.getContent());
        mixEnvelop.setTotalCount(listCount);
        return mixEnvelop;
    }


    /**
     * 创建分页对象
     * @param page
     * @param size
     * @param sorts
     * @return
     */
    public Pageable creatPage(Integer page, Integer size, String sorts){
        PageRequest pageRequest = null;
        if(null == page || page <= 0){
            page = defaultPage;
        }
        if(null == size || size <= 0){
            size = defaultSize;
        }
        if(!org.apache.commons.lang3.StringUtils.isEmpty(sorts)){
            pageRequest = new PageRequest(page,size,createSort(sorts));
        }else{
            pageRequest = new PageRequest(page,size);
        }

        return pageRequest;
    }
    //将患者移动到指定分组中
    public void movePatientToGroup(String groupId,String patient,String doctor) throws Exception{
        BasePatientGroupDO basePatientGroupDO= basePatientGroupDao.findOneGroupByDoctorAndPatientAndGroupId(doctor,patient,groupId);
        if (basePatientGroupDO==null){
            basePatientGroupDO = new BasePatientGroupDO();
            basePatientGroupDO.setCreateTime(new Date());
            BasePatientDO basePatientDO = basePatientDao.findById(patient);
            if (basePatientDO!=null){
                basePatientGroupDO.setPatient(patient);
                basePatientGroupDO.setPatientName(basePatientDO.getName());
            }
            BaseDoctorDO baseDoctorDO = basedoctorDao.findById(doctor);
            if (baseDoctorDO!=null){
                basePatientGroupDO.setDoctor(doctor);
                basePatientGroupDO.setDoctorName(baseDoctorDO.getName());
            }
            basePatientGroupDO.setGroupId(groupId);
            basePatientGroupDao.save(basePatientGroupDO);
        }else {
            throw  new Exception("该患者已存在该分组中");
        }
    }
    //将患者从指定分组中删除
    public void delPatientGroup(String id) throws Exception{
        BasePatientGroupDO basePatientGroupDO= basePatientGroupDao.findOne(id);
        if (basePatientGroupDO!=null){
            basePatientGroupDao.delete(id);
        }else {
            throw  new Exception("该记录已删除");
        }
    }
    //查询患者分组
    public MixEnvelop findPatientGroup(String patientName,String doctor,String dictId,Integer page,Integer pageSize){
        String sql = "select t.id as \"id\"," +
                "  t.group_id as \"groupId\"," +
                "  t.patient as \"patient\"," +
                "  t.patient_name as \"patientName\"," +
                "  t.doctor as \"doctor\"," +
                "  b.idcard as \"idcard\"," +
                "  t.create_time as \"createTime\"" +
                " from base_patient_group t left join base_patient b " +
                " on t.patient = b.id where b.del = '1'";
        String sqlCount ="select count(1) as \"total\"" +
                " from base_patient_group t left join base_patient b " +
                " on t.patient = b.id where b.del = '1'";
        if (StringUtils.isNoneBlank(patientName)){
            sql +=" and b.name like '%"+patientName+"%'";
            sqlCount +=" and b.name like '%"+patientName+"%'";
        }
        if (StringUtils.isNoneBlank(doctor)){
            sql +=" and t.doctor ='"+doctor+"'";
            sqlCount +=" and t.doctor ='"+doctor+"'";
        }
        if (StringUtils.isNoneBlank(dictId)){
            sql +=" and t.group_id ='"+dictId+"'";
            sqlCount +=" and t.group_id ='"+dictId+"'";
        }
        List<Map<String,Object>> list = hibenateUtils.createSQLQuery(sql ,page,pageSize);
        List<Map<String,Object>> rstotal = hibenateUtils.createSQLQuery(sqlCount);
        Long count = 0L;
        if (rstotal != null && rstotal.size() > 0) {
            count = Long.parseLong(rstotal.get(0).get("total").toString());
        }
        MixEnvelop mixEnvelop = new MixEnvelop();
        mixEnvelop.setTotalCount(count.intValue());
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setCurrPage(page);
        mixEnvelop.setDetailModelList(list);
        return mixEnvelop;
    }
    //查询分组患者数量
    public List<Map<String,Object>> findPatientGroupNum(String doctor,String dictIds){
        List<Map<String,Object>> list = new ArrayList<>();
        if (StringUtils.isNoneBlank(dictIds)){
            String[] dictIdGroup = dictIds.split(",");
            for (int i=0;i<dictIdGroup.length;i++){
                int count = basePatientGroupDao.getCountByDoctor(doctor,dictIdGroup[i]);
                Map map = new HashMap();
                map.put(dictIdGroup[i],count);
                list.add(map);
            }
        }
        return list;
    }
}
