package service import ( "XT_New/models" "XT_New/utils" "github.com/astaxie/beego/config" "github.com/jinzhu/gorm" "strconv" "strings" "time" ) func GetTodayGood(stime, etime, orgid, is_medicine int64, keyword string) (finlly []models.ListOfDrugs, err error) { if is_medicine == 0 { keyword = "%" + keyword + "%" err = XTReadDB().Raw("select pp.project_id as id, good.good_name as name, good.specification_name as specifications,good.sum_count as stock from his_prescription_project pp join xt_good_information good on pp.project_id = good.id and good.org_id = ? where pp.type =3 and pp.user_org_id = ? and pp.record_date >= ? and pp.record_date <= ? and is_medicine = ? and good.good_name like ? Group by pp.project_id", orgid, orgid, stime, etime, is_medicine, keyword).Scan(&finlly).Error } else { keyword = "%" + keyword + "%" err = XTReadDB().Raw("select pp.project_id as id, good.good_name as name, good.specification_name as specifications,good.sum_count as stock from his_prescription_project pp join xt_good_information good on pp.project_id = good.id and good.org_id = ? where pp.type =3 and pp.user_org_id = ? and pp.record_date >= ? and pp.record_date <= ? and is_medicine = ? and good.good_name like ? Group by pp.project_id", orgid, orgid, stime, etime, is_medicine, keyword).Scan(&finlly).Error } return } func FindGoodMedicationList(orgid, drug_id, stime, etime, is_medicine int64) (pp []*models.PatientInformation, err error) { /////////////// InitDrugidIsNil(orgid, stime, etime) var tmp []*models.HisPrescriptionProject err = XTReadDB().Model(&models.HisPrescriptionProject{}).Where( "status = 1 and record_date >= ? and record_date <= ? and user_org_id = ? and project_id = ? and is_medicine = ?", stime, etime, orgid, drug_id, is_medicine).Find(&tmp).Error if err != nil { return pp, err } for _, v := range tmp { num, _ := strconv.ParseInt(v.Count, 10, 64) pp = append(pp, &models.PatientInformation{ Id: "h" + config.ToString(v.ID), Name: FindUserName(v.PatientId), PatientId: v.PatientId, SingleDosage: config.ToString(v.SingleDose) + v.Unit, Usage: v.DeliveryWay, Frequency: v.ExecutionFrequency, Days: config.ToString(v.Day) + "天", Total: config.ToString(v.Count) + v.Unit, DataSources: "his处方", Quantity: num, Unit: v.Unit, DrugCode: v.DrugCode, ID: v.ID, }) } return } func GoodDeparture(ids string, creater, orgid int64) (err error) { //开事务 tx := XTWriteDB().Begin() defer func() { if err != nil { utils.ErrorLog("事务失败,原因为: %v", err) tx.Rollback() } else { tx.Commit() } }() //处理下字符串 t_ids := "" if ids[len(ids)-1] == 44 { t_ids = ids[:len(ids)-1] } else { t_ids = ids } time_now := time.Now().Unix() tmp_id := strings.Split(t_ids, ",") patient_id := make(map[int64]int64) for _, v := range tmp_id { front := v[:1] after := v[1:] if front == "h" { var advice_info []*models.HisPrescriptionProject err = tx.Model(&models.HisPrescriptionProject{}).Where("id = ? and status = 1", after).Find(&advice_info).Error if err != nil { return } for _, v := range advice_info { if _, ok := patient_id[v.PatientId]; !ok { patient_id[v.PatientId] = v.PatientId } } //修改状态 errs := XTWriteDB().Model(&models.HisPrescriptionProject{}).Where("id = ?", after).Updates(map[string]interface{}{ "is_medicine": 1, "people": creater, "dispensing_time": time_now, }).Error if errs != nil { return errs } err1 := ChangeGoodHisPrescriptionid(after) if err1 != nil { return err1 } } } for _, v := range patient_id { //生成明细记录 tmp_ph := models.Pharmary{ UserOrgId: orgid, PatientId: v, Ctime: time.Now().Unix(), Mtime: time.Now().Unix(), Status: 1, RecordDate: time_now, } err = tx.Create(&tmp_ph).Error if err != nil { return err } } return } // 改变处方状态 func ChangeGoodHisPrescriptionid(id string) (err error) { var advice models.HisPrescriptionProject err = XTReadDB().Model(&models.HisPrescriptionProject{}).Where("id = ? and status = 1", id).Find(&advice).Error if err != nil { if err == gorm.ErrRecordNotFound { return nil } return } err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", advice.PrescriptionId).Updates(map[string]interface{}{ "is_medicine": 1, "mtime": time.Now().Unix(), }).Error if err != nil { return } return }