123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- package service
-
- import (
- "XT_New/models"
- "fmt"
- "github.com/jinzhu/gorm"
- "time"
- )
-
- func GetInspectionMajor(orgid int64) (inspection []*models.XtInspectionReference, err error) {
-
- err = XTReadDB().Model(&inspection).Where("org_id = ? and status = 1", orgid).Group("project_name").Order("id asc").Find(&inspection).Error
- return inspection, err
- }
-
- func GetInspectionMinor(projectid int64) (inspection []*models.XtInspectionReference, err error) {
-
- err = XTReadDB().Model(&inspection).Where("project_id = ? and status = 1", projectid).Find(&inspection).Error
- return inspection, err
- }
-
- func GetInspectionRange(id int64) (models.XtInspectionReference, error) {
- reference := models.XtInspectionReference{}
- err := XTReadDB().Model(&reference).Where("id=? and status = 1", id).Find(&reference).Error
- return reference, err
- }
-
- func GetConfigurationById(major int64, moni int64, orgid int64) (*models.XtQualityControlStandard, error) {
- standard := models.XtQualityControlStandard{}
- err := XTReadDB().Model(&standard).Where("inspection_major = ? and inspection_minor = ? and user_org_id = ? and status = 1", major, moni, orgid).Find(&standard).Error
- if err == gorm.ErrRecordNotFound {
- return nil, err
- }
- if err != nil {
- return nil, err
- }
- return &standard, nil
- }
-
- func GetConfigurationByIdTwo(major int64, moni int64, orgid int64) (standard models.XtQualityControlStandard, err error) {
-
- err = XTReadDB().Model(&standard).Where("inspection_major = ? and inspection_minor = ? and user_org_id = ? and status = 1", major, moni, orgid).First(&standard).Error
- return standard, err
- }
-
- func SaveInspection(standard *models.XtQualityControlStandard) error {
-
- err := XTWriteDB().Create(&standard).Error
- return err
- }
-
- func GetConfigurationlist(orgid int64, limit int64, page int64) (standard []*models.QualityControlStandard, total int64, err error) {
-
- db := XTReadDB().Table("xt_quality_control_standard as x").Where("x.status =1")
- if orgid > 0 {
- db = db.Where("x.user_org_id = ?", orgid)
- }
- table := XTReadDB().Table("xt_inspection_reference as s")
- fmt.Println(table)
- offset := (page - 1) * limit
- err = db.Order("x.sort asc,x.created_time desc").Group("x.id").Select("x.id,x.inspection_major,x.inspection_minor,x.min_range,x.large_range,x.sort,x.user_org_id,s.unit,s.project_name,s.item_name").Count(&total).
- Joins("left join xt_inspection_reference as s on s.id = x.inspection_minor").Offset(offset).Limit(limit).Scan(&standard).Error
- return standard, total, err
- }
-
- func GetConfigurationDetail(id int64) (models.XtQualityControlStandard, error) {
- standard := models.XtQualityControlStandard{}
- err := XTReadDB().Model(&standard).Where("id=? and status = 1", id).Find(&standard).Error
- return standard, err
- }
-
- func GetAllInspectionMinor(orgid int64) (standard []*models.XtInspectionReference, err error) {
-
- err = XTReadDB().Model(&standard).Where("org_id = ? and status = 1", orgid).Find(&standard).Error
- return standard, err
- }
-
- func UpdarteConfiguration(st *models.XtQualityControlStandard, id int64) error {
-
- err := XTWriteDB().Model(&st).Where("id = ?", id).Updates(map[string]interface{}{"inspection_major": st.InspectionMajor, "inspection_minor": st.InspectionMinor, "min_range": st.MinRange, "large_range": st.LargeRange, "sort": st.Sort, "updated_time": time.Now().Unix()}).Error
- return err
- }
-
- func DeleteConfiguration(id int64) (err error) {
-
- err = XTWriteDB().Model(models.XtQualityControlStandard{}).Where("id=?", id).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
- return err
- }
-
- func GetAllInspectionData(orgid int64) (*models.XtInspectionReference, error) {
- diseases := models.XtInspectionReference{}
- err := XTReadDB().Model(&diseases).Where("org_id = ? and status = 1", orgid).Find(&diseases).Error
- if err == gorm.ErrRecordNotFound {
- return nil, err
- }
- if err != nil {
- return nil, err
- }
- return &diseases, nil
- }
-
- func GetAllInspectiondatatwo(orgid int64) (reference []*models.XtInspectionReference, err error) {
-
- err = XTReadDB().Model(&reference).Where("org_id = ? and status = 1", orgid).Group("project_name").Find(&reference).Error
- return reference, err
- }
-
- func GetInspectionMajorById(marjor int64, orgid int64) (*models.XtCheckConfiguration, error) {
- configuration := models.XtCheckConfiguration{}
- err := XTReadDB().Model(&configuration).Where("inspection_major = ? and user_org_id = ? and status =1", marjor, orgid).Find(&configuration).Error
- if err == gorm.ErrRecordNotFound {
- return nil, err
- }
- if err != nil {
- return nil, err
- }
- return &configuration, nil
- }
-
- func GetInspectionMajorByIdTwo(marjor int64, orgid int64) (configuration models.XtCheckConfiguration, err error) {
- err = XTReadDB().Model(&configuration).Where("inspection_major = ? and user_org_id = ? and status =1", marjor, orgid).First(&configuration).Error
- return configuration, err
- }
-
- func CreateCheckConfiguration(configuration *models.XtCheckConfiguration) error {
- err := XTWriteDB().Create(&configuration).Error
- return err
- }
-
- func GetAllCheckList(orgid int64, page int64, limit int64) (check []*models.CheckConfiguration, total int64, err error) {
-
- db := XTReadDB().Table("xt_check_configuration as x").Where("x.status =1")
- table := XTReadDB().Table("xt_inspection_reference as r")
- fmt.Println(table)
- if orgid > 0 {
- db = db.Where("x.user_org_id = ?", orgid)
- }
- offset := (page - 1) * limit
- err = db.Group("x.id").Order("x.sort asc,x.created_time desc").Select("x.id,x.inspection_major,x.inspection_frequency,x.sort,x.user_org_id,r.project_name").Count(&total).
- Joins("left join xt_inspection_reference as r on r.project_id = x.inspection_major").Offset(offset).Limit(limit).Scan(&check).Error
- return check, total, err
- }
-
- func GetCheckDetail(id int64) (models.XtCheckConfiguration, error) {
-
- configuration := models.XtCheckConfiguration{}
- err := XTReadDB().Model(&configuration).Where("id=? and status =1", id).Find(&configuration).Error
- return configuration, err
- }
-
- func UpdateCheck(con *models.XtCheckConfiguration, id int64) error {
-
- err := XTWriteDB().Model(&con).Where("id=?", id).Updates(map[string]interface{}{"inspection_major": con.InspectionMajor, "inspection_frequency": con.InspectionFrequency, "sort": con.Sort, "updated_time": time.Now().Unix()}).Error
- return err
- }
-
- func DeleteCheck(id int64) error {
-
- err := XTWriteDB().Model(models.XtCheckConfiguration{}).Where("id=?", id).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
- return err
- }
-
- func GetDialysiTotal(startime int64, endtime int64, orgid int64) (prescription []*models.DialysisPrescription, total int64, err error) {
- err = XTReadDB().Model(&prescription).Where("record_date >= ? and record_date<=? and user_org_id = ?", startime, endtime, orgid).Count(&total).Find(&prescription).Error
- return prescription, total, err
- }
-
- func GetDialysisCountMode(starttime int64, endtime int64, orgid int64) (counts []*models.PatientPrescriptionCountStruct, err error) {
- err = readDb.Table("xt_dialysis_prescription as p").Where("p.record_date>= ? and p.record_date<=? and p.user_org_id=?", starttime, endtime, orgid).Select("p.mode_id,count(p.mode_id) as count").Group("p.mode_id").Scan(&counts).Error
- return counts, err
- }
-
- func GetTotalRollOut(starttime int64, endtime int64, orgid int64) (counts []*models.PatientLapseto, total int64, err error) {
-
- db := readDb.Table("xt_patient_lapseto as x").Where("x.status = 1")
- table := readDb.Table("xt_dialysis_order as s")
- fmt.Println(table)
- err = db.Select("x.patient_id,x.lapseto_type").Joins("left join xt_dialysis_order as s on s.patient_id = x.patient_id").Where("x.lapseto_time >=? and x.lapseto_time <= ? and x.lapseto_type = 2 and s.user_org_id = ?", starttime, endtime, orgid).Group("x.patient_id").Count(&total).Scan(&counts).Error
- return counts, total, err
- }
-
- func GetTotalRollOutPatients(orgid int64) (patients []*models.XtPatients, total int64, err error) {
-
- err = XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1 and lapseto = 2", orgid).Count(&total).Find(&patients).Error
- return patients, total, err
- }
-
- func GetPatientTotalCount(orgID int64) (total int64) {
- readDb.Model(&models.XtPatients{}).Where("user_org_id=? and status=1", orgID).Count(&total)
- return
- }
-
- func GetPatientTotalCountTwo(orgid int64, starttime int64, endtime int64) (dialysisorder []*models.XtDialysisOrder, total int64, err error) {
-
- err = XTReadDB().Model(&dialysisorder).Group("patient_id").Where("user_org_id = ? and dialysis_date >= ? and dialysis_date <=? and status =1", orgid, starttime, endtime).Count(&total).Find(&dialysisorder).Error
- return dialysisorder, total, err
- }
-
- func GetManPatientTotalCount(orgid int64, starttime int64, endtime int64) (dialysisorder []*models.SgjDialysisOrder, total int64, err error) {
-
- db := XTReadDB().Table("xt_dialysis_order as s").Where("s.status = 1")
- table := XTReadDB().Table("xt_patients as x")
- fmt.Println("table", table)
- err = db.Select("s.dialysis_date,s.prescription_id").Joins("left join xt_patients as x on x.id = s.patient_id").Where("s.dialysis_date>=? and s.dialysis_date<=? and s.user_org_id = ? and s.status = 1 and x.gender = 1", starttime, endtime, orgid).Group("s.patient_id").Count(&total).Scan(&dialysisorder).Error
- return dialysisorder, total, err
-
- }
-
- func GetManPatientTotal(orgid int64) (patients []*models.XtPatients, total int64, err error) {
-
- err = XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1 and gender = 1", orgid).Count(&total).Find(&patients).Error
- return patients, total, err
- }
-
- //func GetPatientInfectiousCount(orgid int64, starttime int64, endtime int64) (counts []*models.PatientContagionsCountStruct, err error) {
- // err = readDb.Table("xt_patients_infectious_diseases as x").Joins("join xt_dialysis_order as o on o.patient_id = x.patient_id").Joins("join xt_patients as s on s.id = x.patient_id").Where("o.user_org_id = ? and o.status =1 and o.dialysis_date>=? and o.dialysis_date <=? and x.status =1 and s.is_infectious = 2", orgid, starttime, endtime).Select("x.disease_id,count(x.disease_id) as count").Group("x.disease_id").Scan(&counts).Error
- // return counts, err
- //}
-
- func GetPatientInfectiousCount(orgid int64) (counts []*models.PatientContagionsCountStruct, err error) {
-
- err = readDb.Table("xt_patients_infectious_diseases as x").Joins("join xt_patients as s on s.id = x.patient_id").Where("s.user_org_id = ? and s.status = 1 and s.is_infectious = 2", orgid).Select("x.disease_id,count(x.disease_id) as count").Group("x.disease_id").Scan(&counts).Error
- return counts, err
- }
-
- func GetPatientOtherInfectious(orgid int64) (patients []*models.XtPatients, total int64, err error) {
-
- err = XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1 and is_infectious = 1", orgid).Count(&total).Find(&patients).Error
- return patients, total, err
- }
-
- func GetTotalAgeCount(orgid int64) (counts []*models.PatientAgeCountStruct, err error) {
-
- readDb.Raw(`SELECT nnd AS 'age',COUNT(*) AS 'count' FROM(
- SELECT
- CASE
- WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=20 THEN '20'
- WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>20 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=40 THEN '40'
- WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>40 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=60 THEN '60'
- WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>60 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=80 THEN '80'
- WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>80 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=150 THEN '150'
- ELSE '-1'
- END
- AS nnd FROM xt_patients as s where s.user_org_id=? and s.status=1
- )a GROUP BY nnd`, orgid).Scan(&counts)
- return
-
- }
-
- func GetTotalAgeCountTwo(orgid int64, starttime int64, endtime int64) (counts []*models.PatientAgeCountStruct, err error) {
- readDb.Raw(`SELECT nnd AS 'age',COUNT(*) AS 'count' FROM(
- SELECT
- CASE
- when (substring(now(),1,4)-substring(id_card_no,7,4))-(substring(id_card_no,11,4)-date_format(now(),'%m%d')>0) and (substring(now(),1,4)-substring(id_card_no,7,4))-(substring(id_card_no,11,4)-date_format(now(),'%m%d')<20) THEN '20'
- END
- AS nnd FROM xt_patients as s left join xt_dialysis_order as o on o.patient_id = s.id where s.user_org_id=? and s.status=1 and o.dialysis_date>= ? and o.dialysis_date<=?
- )a GROUP BY nnd`, orgid, starttime, endtime).Scan(&counts)
- return
- }
-
- func GetTotalDialysisAgeCount(orgid int64) (patients []*models.XtPatients, err error) {
-
- err = XTReadDB().Where("user_org_id = ? and status =1", orgid).Find(&patients).Error
- return patients, err
- }
-
- func GetDialysisAgeData(orgID int64) (counts []*models.DialysisAgePieDataStruct, err error) {
- loc, _ := time.LoadLocation("Local")
- nowTime := time.Now()
- nowDay := nowTime.Format("2006-01-02")
- dayTime, err := time.ParseInLocation("2006-01-02 15:04:05", nowDay+" 00:00:00", loc)
- fmt.Println("datTime", dayTime)
- if err != nil {
- return
- }
- oneYearPoint := dayTime.AddDate(-12, 0, 0).Unix()
-
- fiveYearPoint := dayTime.AddDate(-36, 0, 0).Unix()
-
- tenYearPoint := dayTime.AddDate(-60, 0, 0).Unix()
-
- twentyYearPoint := dayTime.AddDate(-61, 0, 0).Unix()
-
- db := readDb.Table("xt_patients").Where("status=1")
- countSQL := "SELECT nnd AS 'age',COUNT(*) AS 'count' FROM(" +
- "SELECT " +
- "CASE " +
- " WHEN first_dialysis_date<>0 AND first_dialysis_date>? THEN '1'" +
- " WHEN first_dialysis_date<>0 AND first_dialysis_date>? && first_dialysis_date<=? THEN '2'" +
- " WHEN first_dialysis_date<>0 AND first_dialysis_date>? && first_dialysis_date<=? THEN '3'" +
- " WHEN first_dialysis_date<>0 AND first_dialysis_date>? && first_dialysis_date<=? THEN '4'" +
- " WHEN first_dialysis_date<>0 AND first_dialysis_date<=? THEN '5'" +
- " ELSE '0'" +
- "END AS nnd FROM xt_patients WHERE status=1"
- countParams := make([]interface{}, 0)
- countParams = append(countParams, oneYearPoint)
- countParams = append(countParams, fiveYearPoint)
- countParams = append(countParams, oneYearPoint)
- countParams = append(countParams, tenYearPoint)
- countParams = append(countParams, fiveYearPoint)
- countParams = append(countParams, twentyYearPoint)
- countParams = append(countParams, tenYearPoint)
- countParams = append(countParams, twentyYearPoint)
-
- if orgID > 0 {
- db = db.Where("user_org_id=?", orgID)
- countSQL += " AND user_org_id=?"
- countParams = append(countParams, orgID)
- }
-
- countSQL += ")a GROUP BY nnd"
- err = readDb.Raw(countSQL, countParams...).Scan(&counts).Error
- return
-
- }
-
- func GetCurentOrgPatients(orgid int64) (patients []*models.XtPatients, err error) {
-
- err = XTReadDB().Where("user_org_id = ? and status =1", orgid).Order("created_time desc").Find(&patients).Error
- return patients, err
- }
-
- func GetDialysisList(startime int64, endtime int64, page int64, limit int64, orgid int64) (prescription []*models.BloodDialysisPrescription, total int64, err error) {
- fmt.Println(startime, endtime)
- db := XTReadDB().Table("xt_dialysis_prescription as p").Where("p.status =1 ")
- table := XTReadDB().Table("xt_patients as s")
- fmt.Println("table", table)
- if orgid > 0 {
- db = db.Where("p.user_org_id = ?", orgid)
- }
- if startime > 0 {
- db = db.Where("p.record_date >= ?", startime)
- }
- if endtime > 0 {
- db = db.Where("p.record_date <=?", endtime)
- }
- offset := (page - 1) * limit
- err = db.Group("p.patient_id,p.mode_id").Select("p.mode_id,p.patient_id,s.name,s.id_card_no,s.dialysis_no").Joins("left join xt_patients as s on s.id = p.patient_id").Count(&total).Offset(offset).Limit(limit).Scan(&prescription).Error
- return prescription, total, err
- }
-
- func GetLastSort(orgid int64) (models.XtQualityControlStandard, error) {
- standard := models.XtQualityControlStandard{}
- err := XTReadDB().Model(&standard).Where("user_org_id = ? and status =1", orgid).Last(&standard).Error
- return standard, err
- }
-
- func GetLastCheckList(orgid int64) (models.XtCheckConfiguration, error) {
- configuration := models.XtCheckConfiguration{}
- err := XTReadDB().Model(&configuration).Where("user_org_id = ? and status =1", orgid).Last(&configuration).Error
- return configuration, err
- }
|