package com.ywt.outpatient.service.rpc;

import com.ywt.biz.common.util.Checker;
import com.ywt.biz.common.util.DateUtil;
import com.ywt.outpatient.core.PagedList;
import com.ywt.outpatient.core.SqlHelper;
import com.ywt.outpatient.domain.entity.center.MedicalRecords;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MedicalRecordService {

    @Autowired
    private SqlHelper sqlHelper;

    public PagedList<MedicalRecords> getOutpatientRecordList(int pageIndex, int pageSize, String hisPatientId) {
        final String fields = "*";
        final String orderBy = "create_time desc";
        final String whereSql = "his_patient_id = ? and type = 2 and deleted = 0";
        final Object[] params = new Object[]{hisPatientId};

        return sqlHelper.getPagedList(pageIndex, pageSize, fields, whereSql, orderBy, MedicalRecords.class, params);
    }

    /**
     * 根据 hisPatientId & hospitalId 获取病历
     */
    public PagedList<MedicalRecords> getOutpatientRecordListNew(int pageIndex, int pageSize, String hisPatientId, int hospitalId) {
        final String fields = "*";
        final String orderBy = "create_time desc";
        String whereSql = "his_patient_id = ? and type = 2 and deleted = 0 and hospital_id = ? ";
        Object[] params = new Object[]{hisPatientId, hospitalId};
        // 南方医科大学南方医院白云分院v2.1.7（查询优化）版本需要做近180天数据的过滤
        try {
            Date date = DateUtil.getChangeMonthDate(new Date(), 1, -180);
            if (!Checker.isNone(date)) {
                whereSql += " and adm_time >= ? ";
                params = new Object[]{hisPatientId, hospitalId, date};
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sqlHelper.getPagedList(pageIndex, pageSize, fields, whereSql, orderBy, MedicalRecords.class, params);
    }

}
