package service import ( "XT_New/models" "fmt" "github.com/jinzhu/gorm" ) func CreateAutomaticReduceRecord(config *models.GobalConfig) (err error) { err = writeDb.Model(&models.GobalConfig{}).Create(config).Error return } func CreateDrugAutomaticReduceRecord(config *models.DrugStockConfig) (err error) { err = writeDb.Model(&models.DrugStockConfig{}).Create(config).Error return } func FindAutomaticReduceRecordByOrgId(org_id int64) (err error, config models.GobalConfig) { err = readDb.Model(&models.GobalConfig{}).Where("status = 1 AND org_id = ?", org_id).Find(&config).Error return } func FindDrugStockAutomaticReduceRecordByOrgId(org_id int64) (err error, config models.DrugStockConfig) { err = readDb.Model(&models.DrugStockConfig{}).Where("status = 1 AND org_id = ?", org_id).Find(&config).Error return } func UpdateAutomaticReduceRecord(config *models.GobalConfig) (err error) { err = writeDb.Save(config).Error return } func UpdateDrugStockAutomaticReduceRecord(config *models.DrugStockConfig) (err error) { err = writeDb.Save(config).Error return } func CreatePrintTemplateRecord(template *models.GobalTemplate) (err error) { err = writeDb.Model(&models.GobalTemplate{}).Create(template).Error return } func FindPrintTemplateByOrgId(org_id int64) (err error, template models.GobalTemplate) { err = readDb.Model(&models.GobalTemplate{}).Where("status = 1 AND org_id = ?", org_id).Find(&template).Error return } func UpdatePrintTemplate(template *models.GobalTemplate) (err error) { err = writeDb.Save(template).Error return } func CreateConfigData(config *models.DataUploadConfig) (err error) { err = writeDb.Create(config).Error return } func SaveConfigData(config *models.DataUploadConfig) (err error) { err = writeDb.Save(config).Error return } func GetConfigData(org_id int64, config_type int64) (config models.DataUploadConfig, err error) { err = readDb.Model(&models.DataUploadConfig{}).Where("status = 1 AND org_id = ? AND config_type = ?", org_id, config_type).First(&config).Error return } func GetDockingStatus(config_type int64, province int64, city int64) (config models.DockingStatus, err error) { err = readDb.Model(&models.DockingStatus{}).Where("status = 1 AND docking_type = ? AND province_id = ? AND city_id = ?", config_type, province, city).First(&config).Error return } func GetConfigDataById(id int64) (config models.DataUploadConfig, err error) { err = readDb.Model(&models.DataUploadConfig{}).Where("id = ?", id).First(&config).Error return } func FindDoctorAdviceRecordByOrgId(org_id int64) (err error, config models.DoctorAdviceConfig) { err = readDb.Model(&models.DoctorAdviceConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error return } func CreateDoctorAdviceRecord(config *models.DoctorAdviceConfig) (err error) { err = writeDb.Model(&models.DoctorAdviceConfig{}).Create(config).Error return } func UpdateDoctorAdviceRecord(config *models.DoctorAdviceConfig) (err error) { err = writeDb.Save(config).Error return } func UpdateFiledConfig(org_id int64) (err error) { err = writeDb.Model(&models.FiledConfig{}).Where("org_id = ? AND sys_module = 0", org_id).Updates(map[string]interface{}{"is_show": 1}).Error return } func FindAllHideFiledConfig(template_id int64) (err error, config []*models.FiledConfig) { err = readDb.Model(&models.FiledConfig{}).Where("sys_module = ? AND is_show = 2", template_id).Find(&config).Error return } func UpdateShowFieldConfig(org_id int64, fileds []string) (err error) { err = writeDb.Model(&models.FiledConfig{}).Where("org_id = ? AND filed_name in (?) AND sys_module = 0 ", org_id, fileds).Updates(map[string]interface{}{"is_show": 2}).Error return } func FindAllAdviceParentTemplate(org_id int64) (template []*models.DoctorAdviceParentTemplate) { readDb.Model(&models.DoctorAdviceParentTemplate{}).Preload("DoctorAdviceTemplate", func(db *gorm.DB) *gorm.DB { return db.Preload("SubDoctorAdviceTemplate", "status = 1 AND org_id = ?", org_id).Where("status = 1 AND parent_id = 0 AND org_id = ?", org_id) }).Where("status = 1 AND org_id = ? ", org_id).Find(&template) return } func CreateDoctorParentTemplate(template *models.DoctorAdviceParentTemplate) (err error) { err = writeDb.Create(&template).Error return } func FindAllAdviceTemplates(org_id int64, parent_template_id int64) (template []*models.DoctorAdviceTemplate, err error) { err = readDb.Model(&models.DoctorAdviceTemplate{}).Where("status = 1 AND org_id = ? AND template_id = ?", org_id, parent_template_id).Find(&template).Error return } func CreateDoctorTemplate(template *models.DoctorAdviceTemplate) (err error) { err = writeDb.Create(&template).Error return } func CreateSystemDialysisSolution(solution *models.SystemPrescription) (err error) { err = writeDb.Create(solution).Error return } func UpdateSystemDialysisSolution(solution *models.SystemPrescription) (err error) { err = writeDb.Save(solution).Error return } func FindSystemDialysisSolution(orgID int64, id int64) (solution models.SystemPrescription, err error) { err = readDb.Model(&models.SystemPrescription{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error return } func FindAllSystemPrescription(orgID int64) (solution []*models.SystemPrescription, err error) { err = readDb.Model(&models.SystemPrescription{}).Where("status=1 and user_org_id=?", orgID).Find(&solution).Error return } func FindSystemDialysisPrescriptionByMode(orgID int64, id int64) (solution models.SystemPrescription, err error) { err = readDb.Model(&models.SystemPrescription{}).Where("mode_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error return } func CreateAdviceInitConfig(adviceInit *models.AdviceInit) (err error) { err = writeDb.Create(&adviceInit).Error return } func FindAdviceInitConfig(org_id int64) (adviceInit models.AdviceInit, err error) { err = readDb.Model(&models.AdviceInit{}).Where("user_org_id = ? AND status = 1 ", org_id).First(&adviceInit).Error return } func GetPrint(ids []int64, orgid int64) (doctor []*models.DoctorAdvice, err error) { if len(ids) == 1 { err = XTReadDB().Model(&doctor).Where("groupno = ? and user_org_id = ?", ids[0], orgid).Find(&doctor).Error } else { err = XTReadDB().Model(&doctor).Where("groupno IN(?) and user_org_id = ?", ids, orgid).Find(&doctor).Error } return doctor, err } func GetExportLogByType(org_id int64, log_type int64) (log []*models.ExportLog, err error) { err = readDb.Model(&models.ExportLog{}).Where("user_org_id = ? AND status = 1 AND log_type = ?", org_id, log_type). Preload("ExportErrLog", "status = 1").Order("export_time desc").Find(&log).Error return } func FindXTHisRecordByOrgId(org_id int64) (err error, config models.XtHisConfig) { err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error return } func UpdateXTHisRecord(config *models.XtHisConfig) (err error) { err = writeDb.Save(config).Error return } func CreateXTHisRecord(config *models.XtHisConfig) (err error) { err = writeDb.Model(&models.XtHisConfig{}).Create(config).Error return } func GetExportHisOrderList(user_org_id int64, start_time int64, end_time int64) (order []*models.HisOrder, err error) { db := readDb.Model(&models.HisOrder{}) if start_time != 0 { db = db.Where("his_order.settle_accounts_date>=?", start_time) } if end_time != 0 { db = db.Where("his_order.settle_accounts_date<=?", end_time) } db = db.Where("his_order.status = 1 AND his_order.user_org_id = ?", user_org_id) db = db.Preload("HisOrderInfo", "status = 1 AND user_org_id = ?", user_org_id). Preload("Patients", "status = 1 AND user_org_id = ?", user_org_id). Preload("HisPatient", "status = 1 AND user_org_id = ?", user_org_id). Preload("HisPrescriptionInfo", func(db *gorm.DB) *gorm.DB { return db.Where("status = 1 AND user_org_id = ?", user_org_id).Preload("XtHisDepartment", "status = 1") }) err = db.Order("ctime desc").Find(&order).Error return } func GetDrugInOrderDetail(startime int64, endtime int64, orgid int64, orderType int64, manufacturerId int64, keyword string, page int64, limit int64) (drugInfo []*models.BloodDrugWarehouseInfo, total int64, err error) { likeKey := "%" + keyword + "%" offset := (page - 1) * limit db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1") dbOne := XTReadDB().Table("xt_base_drug as t").Where("t.status =1") dbTwo := XTReadDB().Table("xt_drug_warehouse as s").Where("s.status = 1") fmt.Print(dbOne, dbTwo) if startime > 0 { db = db.Where("x.ctime >=?", startime) } if endtime > 0 { db = db.Where("x.ctime <=?", endtime) } if orgid > 0 { db = db.Where("x.org_id =?", orgid) } if orderType > 0 { db = db.Where("x.type = ?", orderType) } if len(keyword) > 0 { db = db.Where("x.warehousing_order like ? or t.drug_spec like ? or s.creater like ?", likeKey, likeKey, likeKey) } if manufacturerId > 0 { err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.total_price,x.dealer,t.manufacturer,x.remark,x.ctime,x.org_id,x.is_return,x.warehousing_order,x.type,x.retail_price,x.retail_total_price,t.drug_type,t.drug_name,t.drug_spec,t.min_unit,s.creater").Joins("left join xt_base_drug as t on t.id = x.drug_id and t.org_id = ? and t.status =1", orgid).Where("t.manufacturer = ?", manufacturerId).Joins("left join xt_drug_warehouse as s on s.id = x.warehousing_id and s.org_id = ? and s.status =1", orgid).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&drugInfo).Error } else { err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.total_price,x.dealer,t.manufacturer,x.remark,x.ctime,x.org_id,x.is_return,x.warehousing_order,x.type,x.retail_price,x.retail_total_price,t.drug_type,t.drug_name,t.drug_spec,t.min_unit,s.creater").Joins("left join xt_base_drug as t on t.id = x.drug_id and t.org_id = ? and t.status =1", orgid).Joins("left join xt_drug_warehouse as s on s.id = x.warehousing_id and s.org_id = ? and s.status =1", orgid).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&drugInfo).Error } return drugInfo, total, err } func GetDrugReturnOrder(startime int64, endtime int64, orgid int64, orderType int64, manufacturerId int64, keyword string, limit int64, page int64) (returninfo []*models.BloodDrugSalesReturnInfo, total int64, err error) { likeKey := "%" + keyword + "%" offset := (page - 1) * limit db := XTReadDB().Table("xt_drug_sales_return_info as x").Where("x.status =1") dbOne := XTReadDB().Table("xt_base_drug as s").Where("s.status =1") dbTwo := XTReadDB().Table("xt_drug_sales_return as r").Where("r.status = 1") fmt.Print(dbOne, dbTwo) if startime > 0 { db = db.Where("x.ctime >= ?", startime) } if endtime > 0 { db = db.Where("x.ctime <=?", endtime) } if orgid > 0 { db = db.Where("x.org_id = ?", orgid) } if orderType > 0 { db = db.Where("x.type = ?", orderType) } if len(keyword) > 0 { db = db.Where("x.order_number like ? or s.drug_spec like ? or r.creater like ?", likeKey, likeKey, likeKey) } if manufacturerId > 0 { err = db.Select("x.id,x.drug_id,x.sales_return_id,x.count,x.price,x.total,x.product_date,x.expiry_date,x.ctime,x.org_id,x.order_number,x.type,x.dealer,x.manufacturer,x.retail_price,x.retail_total_price,x.number,s.drug_name,s.drug_spec,s.drug_type,s.min_unit,r.creater").Joins("left join xt_base_drug as s on s.id = x.drug_id and s.org_id = ? and s.status = 1", orgid).Joins("left join xt_drug_sales_return as r on r.id = x.sales_return_id and r.org_id = ? and r.status =1", orgid).Where("r.manufacturer = ?", manufacturerId).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&returninfo).Error } else { err = db.Select("x.id,x.drug_id,x.sales_return_id,x.count,x.price,x.total,x.product_date,x.expiry_date,x.ctime,x.org_id,x.order_number,x.type,x.dealer,x.manufacturer,x.retail_price,x.retail_total_price,x.number,s.drug_name,s.drug_spec,s.drug_type,s.min_unit,r.creater").Joins("left join xt_base_drug as s on s.id = x.drug_id and s.org_id = ? and s.status = 1", orgid).Joins("left join xt_drug_sales_return as r on r.id = x.sales_return_id and r.org_id = ? and r.status =1", orgid).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&returninfo).Error } return returninfo, total, err } func GetDrugOutOrder(startime int64, endtime int64, orgid int64, orderType int64, manufacturerId int64, keyword string, page int64, limit int64) (outinfo []*models.BloodDrugWarehouseOutInfo, total int64, err error) { likeKey := "%" + keyword + "%" offset := (page - 1) * limit db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1") dbOne := XTReadDB().Table("xt_base_drug as b").Where("b.status =1") dbTwo := XTReadDB().Table("xt_drug_warehouse_out as t").Where("t.status = 1") fmt.Print(dbOne, dbTwo) if startime > 0 { db = db.Where("x.ctime >=?", startime) } if endtime > 0 { db = db.Where("x.ctime <=?", endtime) } if orgid > 0 { db = db.Where("x.org_id = ?", orgid) } if orderType > 0 { db = db.Where("x.type = ?", orderType) } if len(keyword) > 0 { db = db.Where("x.warehouse_out_order_number like ? or b.drug_spec like ? or t.creater like ?", likeKey, likeKey, likeKey) } if manufacturerId > 0 { err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.is_cancel,x.warehouse_out_order_number,x.type,x.dealer,x.manufacturer,x.is_sys,x.sys_record_time,x.retail_price,x.retail_total_price,b.drug_name,b.drug_spec,b.drug_type,b.min_unit,t.creater").Joins("left join xt_base_drug as b on b.id = x.drug_id and b.org_id = ? and b.status =1", orgid).Joins("left join xt_drug_warehouse_out as t on t.id = x.warehouse_out_id and t.org_id = ? and t.status =1", orgid).Where("t.manufacturer = ?", manufacturerId).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&outinfo).Error } else { err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.is_cancel,x.warehouse_out_order_number,x.type,x.dealer,x.manufacturer,x.is_sys,x.sys_record_time,x.retail_price,x.retail_total_price,b.drug_name,b.drug_spec,b.drug_type,b.min_unit,t.creater").Joins("left join xt_base_drug as b on b.id = x.drug_id and b.org_id = ? and b.status =1", orgid).Joins("left join xt_drug_warehouse_out as t on t.id = x.warehouse_out_id and t.org_id = ? and t.status =1", orgid).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&outinfo).Error } return outinfo, total, err } func GetDrugCancelOrder(startime int64, endtime int64, orgid int64, orderType int64, manufacturerId int64, keyword string, page int64, limit int64) (cancel []*models.BloodDrugCancelStockInfo, total int64, err error) { likeKey := "%" + keyword + "%" offset := (page - 1) * limit db := XTReadDB().Table("xt_drug_cancel_stock_info as x").Where("x.status =1") dbOne := XTReadDB().Table("xt_base_drug as s").Where("s.status =1") dbTwo := XTReadDB().Table("xt_drug_cancel_stock as t").Where("t.status = 1") fmt.Print(dbOne, dbTwo) if startime > 0 { db = db.Where("x.ctime >= ?", startime) } if endtime > 0 { db = db.Where("x.ctime<=?", endtime) } if orgid > 0 { db = db.Where("x.org_id = ?", orgid) } if orderType > 0 { db = db.Where("x.type = ? ", orderType) } if len(keyword) > 0 { db = db.Where("x.order_number like ? or s.drug_spec like ? or t.creater like ?", likeKey, likeKey, likeKey) } if manufacturerId > 0 { err = db.Select("x.id,x.drug_id,x.cancel_stock_id,x.count,x.price,x.total,x.product_date,x.expiry_date,x.ctime,x.org_id,x.order_number,x.type,x.dealer,x.manufacturer,x.retail_price,x.retail_total_price,x.number,s.drug_name,s.drug_type,s.drug_spec,s.min_unit,t.creater").Joins("left join xt_base_drug as s on s.id = x.drug_id and s.org_id = ? and s.status = 1", orgid).Joins("left join xt_drug_cancel_stock as t on t.id = x.cancel_stock_id and t.org_id = ? and t.status =1", orgid).Where("t.manufacturer = ?", manufacturerId).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&cancel).Error } else { err = db.Select("x.id,x.drug_id,x.cancel_stock_id,x.count,x.price,x.total,x.product_date,x.expiry_date,x.ctime,x.org_id,x.order_number,x.type,x.dealer,x.manufacturer,x.retail_price,x.retail_total_price,x.number,s.drug_name,s.drug_type,s.drug_spec,s.min_unit,t.creater").Joins("left join xt_base_drug as s on s.id = x.drug_id and s.org_id = ? and s.status = 1", orgid).Joins("left join xt_drug_cancel_stock as t on t.id = x.cancel_stock_id and t.org_id = ? and t.status =1", orgid).Order("x.ctime desc").Count(&total).Offset(offset).Limit(limit).Scan(&cancel).Error } return cancel, total, err }