1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521 |
- package service
-
- import (
- "encoding/json"
- "fmt"
- "math"
- "strconv"
- "strings"
- "sync"
- "time"
-
- "XT_New/models"
- "XT_New/utils"
- "github.com/astaxie/beego/config"
- "github.com/astaxie/beego/context"
- "github.com/jinzhu/gorm"
- "golang.org/x/sync/errgroup"
- )
-
- func SaveErrs(org_id int64, input *context.BeegoInput, err error) {
- dataBody := make(map[string]interface{}, 0)
- json.Unmarshal(input.RequestBody, &dataBody)
- route := input.Context.Request.RequestURI
- parameter := MapToJson(dataBody)
- tmp := models.XtErrs{
- org_id,
- route,
- parameter,
- err.Error(),
- }
- XTWriteDB().Create(&tmp)
- }
-
- func SavePharmacy() (err error) {
- var advice_info []*models.HisDoctorAdviceInfo
- err = XTReadDB().Raw("select * from xt_doctor_advice where id > 21780 and id < 21790").Scan(&advice_info).Error
- if err != nil {
- return
- }
- return nil
- }
-
- // 查询药房中某一天的患者人数,is_medicine:0未发,1已发(改)
- func GetTodayPharmacy(stime, etime, orgid, is_medicine int64) (num int, err error) {
- //orgid = 9675
- InitDrugidIsNil(orgid, stime, etime)
- var tmp []*models.TmpTTT
- if is_medicine == 0 {
- err = XTReadDB().Raw("select distinct patient_id from his_doctor_advice_info where "+
- "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = 0 and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
- "union "+
- "select distinct patient_id from xt_doctor_advice where "+
- "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = 0 and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) ",
- stime, etime, orgid, orgid, stime, etime, orgid, orgid).Scan(&tmp).Error
- if err != nil {
- return
- }
- } else {
- err = XTReadDB().Raw("select distinct patient_id from his_doctor_advice_info where "+
- "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = 1 and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
- "union "+
- "select distinct patient_id from xt_doctor_advice where "+
- "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = 1 and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) ",
- stime, etime, orgid, orgid, stime, etime, orgid, orgid).Scan(&tmp).Error
- if err != nil {
- return
- }
- }
-
- return len(tmp), err
-
- }
-
- func GetTodayAdviceCount(stime, etime, orgid, is_medicine int64) (num int, err error) {
-
- //orgid = 9675
- InitDrugidIsNil(orgid, stime, etime)
- var tmp []*models.TmpTTT
- if is_medicine == 0 {
- err = XTReadDB().Raw("select * from his_doctor_advice_info where status= 1 and advice_date>=? and advice_date<=? and user_org_id=? and is_medicine=0 group by patient_id",
- stime, etime, orgid).Scan(&tmp).Error
- if err != nil {
- return
- }
- } else {
- err = XTReadDB().Raw("select * from his_doctor_advice_info where status= 1 and advice_date>=? and advice_date<=? and user_org_id=? and is_medicine=1 group by patient_id",
- stime, etime, orgid).Scan(&tmp).Error
- if err != nil {
- return
- }
- }
-
- return len(tmp), err
- }
-
- func GetPatientKeyWord(keywword string, org_id int64) (patient []*models.XtPatients, err error) {
-
- db := XTReadDB().Model(&patient).Where("status=1")
-
- likeKey := "%" + keywword + "%"
- if len(keywword) > 0 {
- db = db.Where("name like ? or first_letter like ?", likeKey, likeKey)
- }
- if org_id > 0 {
- db = db.Where("user_org_id = ?", org_id)
- }
-
- err = db.Find(&patient).Error
-
- return patient, err
- }
-
- func GetSchedulePatientId(start_time int64, end_time int64, org_id int64, class_type int64, partion_type int64, patientsId []int64) (schedule []*models.XtSchedules, err error) {
-
- db := XTReadDB().Model(&schedule).Where("status = 1")
-
- if start_time > 0 {
- db = db.Where("schedule_date >=?", start_time)
- }
-
- if end_time > 0 {
- db = db.Where("schedule_date <=?", end_time)
- }
-
- if org_id > 0 {
- db = db.Where("user_org_id = ?", org_id)
- }
- if class_type > 0 {
- db = db.Where("schedule_type =?", class_type)
- }
- if partion_type > 0 {
- db = db.Where("partition_id = ?", partion_type)
- }
-
- if len(patientsId) == 0 {
- db = db.Where("patient_id in(?)", patientsId)
- }
-
- if len(patientsId) > 0 {
- db = db.Where("patient_id in(?)", patientsId)
- }
-
- err = db.Find(&schedule).Error
-
- return schedule, err
- }
-
- func GetTodayAdviceCountOne(stime int64, etime int64, orgid int64, is_medicine int64, ids []int64) (advice []*models.HisDoctorAdviceInfo, err error) {
-
- db := XTReadDB().Model(&advice).Where("status=1 and is_medicine = ?", is_medicine)
- if stime > 0 {
- db = db.Where("advice_date>=?", stime)
- }
- if etime > 0 {
- db = db.Where("advice_date<=?", stime)
- }
- if orgid > 0 {
- db = db.Where("user_org_id = ?", orgid)
- }
-
- if len(ids) == 0 {
- db = db.Where("patient_id not in(?)", ids)
- }
- if len(ids) > 0 {
- db = db.Where("patient_id in(?)", ids)
- }
-
- err = db.Group("patient_id").Find(&advice).Error
- return advice, err
- }
-
- func GetPatientByAdviceId(patient_id int64) (models.TmpPatientOne, error) {
-
- patient := models.TmpPatientOne{}
- err = XTReadDB().Where("id = ?", patient_id).Find(&patient).Error
- return patient, err
- }
-
- // (改)
- func GetTodayDrug(stime, etime, orgid, is_medicine int64, keyword string) (list []*models.TmpPatient, err error) {
- InitDrugidIsNil(orgid, stime, etime)
- //病人
- var tmp []*models.TmpTTT
- //tmp := make([]string,0)
- if is_medicine == 0 {
- if keyword != "" {
- keyword = "%" + keyword + "%"
- err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
- "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
- "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
- "union "+
- "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
- "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
- "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1))a order by dispensing_time desc ",
- stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid, stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid).Scan(&tmp).Error
- } else {
- err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
- "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
- "union "+
- "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
- "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and is_medicine = ? and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1))a order by dispensing_time desc",
- stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
- }
-
- if err != nil {
- return
- }
- } else {
- if keyword != "" {
- keyword = "%" + keyword + "%"
- err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
- "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
- "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
- "union "+
- "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
- "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
- "patient_id in (select id from xt_patients where user_org_id = ? and name like ? or dialysis_no like ?) and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and status = 1))a order by dispensing_time desc ",
- stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid, stime, etime, orgid, is_medicine, orgid, keyword, keyword, orgid).Scan(&tmp).Error
- } else {
- err = XTReadDB().Raw("select a.* from(select distinct patient_id,dispensing_time from his_doctor_advice_info where "+
- "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
- "union "+
- "select distinct patient_id,dispensing_time from xt_doctor_advice where "+
- "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and status = 1))a order by dispensing_time desc",
- stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
- }
-
- if err != nil {
- return
- }
- }
-
- tmp_s := make([]int64, 0)
- s_map := make(map[int64]int64)
- for _, v := range tmp {
- if _, ok := s_map[v.PatientID]; !ok {
- s_map[v.PatientID] = v.PatientID
- tmp_s = append(tmp_s, v.PatientID)
- }
- }
- list, err = GetManyUsers(tmp_s)
- return
-
- }
-
- // 查询患者(改)
- func GetManyUsers(tmp []int64) (list []*models.TmpPatient, err error) {
-
- for i := 0; i < len(tmp); i++ {
- var tt models.Patients
- err = XTReadDB().Model(&models.Patients{}).Where("id = ?", tmp[i]).Find(&tt).Error
-
- tmp_list := &models.TmpPatient{
- PatientID: tt.ID,
- Name: tt.Name,
- DialysisNo: tt.DialysisNo,
- }
- list = append(list, tmp_list)
- }
- return
- }
-
- // 查询患者当天时间段中的药,is_medicine:0未发,1已发(改)
- func GetPatientMedication(orgid, patient_id, stime, etime, is_medicine int64) (pp []*models.PharmacyContent, err error) {
- InitDrugidIsNil(orgid, stime, etime)
- var tmp []*models.HisDoctorAdviceInfoL
- if orgid == 10375 {
- err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
- "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and patient_id = ? and drug_id in (select id from xt_base_drug where org_id = ?)", stime, etime, orgid, patient_id, orgid).Find(&tmp).Error
- if err != nil {
- return pp, err
- }
-
- for _, v := range tmp {
- pp = append(pp, &models.PharmacyContent{
- ID: v.ID,
- Name: v.AdviceName,
- SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
- Usage: v.DeliveryWay,
- Frequency: v.ExecutionFrequency,
- Days: config.ToString(v.Day) + "天",
- Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
- Doctor: GetAdminUserName(v.AdviceDoctor, orgid), //开立医生
- DataSources: "his处方",
- Remarks: v.Remark, //备注
- DrugId: v.DrugId,
- Price: v.Price,
- DrugCode: v.DrugCode,
- })
- }
-
- var tmp_advice []*models.XtDoctorAdviceL
- if is_medicine == 0 {
- err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
- "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and patient_id = ? and is_medicine = ? and drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1)", stime, etime, orgid, patient_id, is_medicine, orgid).Find(&tmp_advice).Error
- if err != nil {
- return pp, err
- }
- } else {
- err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
- "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and patient_id = ? and is_medicine = ? and drug_id in (select id from xt_base_drug where org_id = ? and status = 1)", stime, etime, orgid, patient_id, is_medicine, orgid).Find(&tmp_advice).Error
- if err != nil {
- return pp, err
- }
- }
-
- for _, v := range tmp_advice {
- pp = append(pp, &models.PharmacyContent{
- ID: v.ID,
- Name: v.AdviceName,
- SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
- Usage: v.DeliveryWay,
- Frequency: v.ExecutionFrequency,
- Days: "-",
- Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
- Doctor: GetAdminUserName(v.AdviceDoctor, orgid),
- DataSources: "临时医嘱",
- Remarks: v.Remark,
- DrugId: v.DrugId,
- DrugCode: v.DrugCode,
- })
- }
- return
- }
-
- if orgid != 10375 {
- if is_medicine == 0 {
- err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
- "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and patient_id = ? and is_medicine = ? and drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1)", stime, etime, orgid, patient_id, is_medicine, orgid).Find(&tmp).Error
- if err != nil {
- return pp, err
- }
- } else {
- err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
- "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and patient_id = ? and is_medicine = ? and drug_id in (select id from xt_base_drug where org_id = ? and status = 1)", stime, etime, orgid, patient_id, is_medicine, orgid).Find(&tmp).Error
- if err != nil {
- return pp, err
- }
- }
-
- for _, v := range tmp {
- pp = append(pp, &models.PharmacyContent{
- ID: v.ID,
- Name: v.AdviceName,
- SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
- Usage: v.DeliveryWay,
- Frequency: v.ExecutionFrequency,
- Days: config.ToString(v.Day) + "天",
- Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
- Doctor: GetAdminUserName(v.AdviceDoctor, orgid), //开立医生
- DataSources: "his处方",
- Remarks: v.Remark, //备注
- DrugId: v.DrugId,
- ExecutionState: v.ExecutionState,
- ExecutionStaff: v.ExecutionStaff,
- ExecutionTime: v.ExecutionTime,
- Price: v.Price,
- DrugCode: v.DrugCode,
- })
- }
-
- var tmp_advice []*models.XtDoctorAdviceL
- if is_medicine == 0 {
- err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
- "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and patient_id = ? and is_medicine = ? and drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1)", stime, etime, orgid, patient_id, is_medicine, orgid).Find(&tmp_advice).Error
- if err != nil {
- return pp, err
- }
- } else {
- err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
- "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and patient_id = ? and is_medicine = ? and drug_id in (select id from xt_base_drug where org_id = ? and status = 1)", stime, etime, orgid, patient_id, is_medicine, orgid).Find(&tmp_advice).Error
- if err != nil {
- return pp, err
- }
- }
-
- for _, v := range tmp_advice {
- pp = append(pp, &models.PharmacyContent{
- ID: v.ID,
- Name: v.AdviceName,
- SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
- Usage: v.DeliveryWay,
- Frequency: v.ExecutionFrequency,
- Days: "-",
- Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
- Doctor: GetAdminUserName(v.AdviceDoctor, orgid),
- DataSources: "临时医嘱",
- Remarks: v.Remark,
- DrugId: v.DrugId,
- ExecutionState: v.ExecutionState,
- ExecutionStaff: v.ExecutionStaff,
- ExecutionTime: v.ExecutionTime,
- DrugCode: v.DrugCode,
- //Price: v.Price,
-
- })
- }
- return
- }
- return
- }
-
- // 获取创建者姓名(改)
- func GetAdminUserName(doctor, orgid int64) string {
- var tmp models.VmUserAdminRole
- XTReadDB().Model(&models.VmUserAdminRole{}).Where("admin_user_id = ? and org_id = ? and status = 1", doctor, orgid).Find(&tmp)
- return tmp.UserName
- }
-
- // 查询该机构的药房配置,true: 通过药房发药,false:不通过药房发药。如果没有该机构的信息则生成一条数据
- func GetOrgIdPharmacyConfig(orgid int64) bool {
- var total int
- var tmp_pharmacy models.PharmacyConfig
- XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp_pharmacy).Count(&total)
- if total > 0 {
- //有记录
- if tmp_pharmacy.IsOpen == 1 {
- return true
- } else {
- return false
- }
- } else {
- //没有记录生成一条
- tmp := models.PharmacyConfig{
- UserOrgId: orgid,
- IsOpen: 2,
- Status: 1,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- }
- XTWriteDB().Create(&tmp)
- return false
- }
- }
-
- // 修改该机构的药房配置
- func ModifyPharmacyConfig(orgid, isopen int64) (err error) {
- if isopen == 1 || isopen == 2 {
- err = XTWriteDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{
- "mtime": time.Now().Unix(),
- "is_open": isopen,
- }).Error
- } else {
- err = fmt.Errorf("类型解析错误")
- }
- return
- }
-
- // 查询该药品是否通过药房发药.0否;1是
- func DrugAllocation(drug_id int64) (is_pharmacy int64, err error) {
- tmp := models.BaseDrugLib{}
- err = XTReadDB().Model(&models.BaseDrugLib{}).Where("id = ?", drug_id).Find(&tmp).Error
- return tmp.IsPharmacy, err
- }
-
- // 根据id查询该药是否发药
- func MedicineState(id int64) (is_medicine models.HisDoctorAdviceInfoL, err error) {
- tmp := models.HisDoctorAdviceInfoL{}
- err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ?", id).Find(&tmp).Error
- return tmp, err
- }
-
- // 发药明细列表(
- func DispensingDetailsList(stime, etime, orgid, page, limit int64, keyword string) (dislist []*models.DispensingList, total int64, err error) {
- var fenye []*models.Pharmary //分页用的
- offset := (page - 1) * limit
- dbone := XTReadDB().Model(&models.Pharmary{}).Where("status = 1 and user_org_id = ? and record_date >= ? and record_date <= ?", orgid, stime, etime)
- if keyword != "" {
- var tmp_id []*models.TmpID
- keyword = "%" + keyword + "%"
- XTReadDB().Raw("select id from xt_patients where name like ? and user_org_id = ?", keyword, orgid).Scan(&tmp_id)
- tmp_ids := make([]int64, 0)
- for _, v := range tmp_id {
- tmp_ids = append(tmp_ids, v.Id)
- }
- dbone = dbone.Where("patient_id in (?)", tmp_ids)
- }
-
- //查询出分页用的数据
- err = dbone.Count(&total).Offset(offset).Order("mtime desc").Limit(limit).Find(&fenye).Error
- if err != nil {
- return
- }
- for _, v := range fenye {
- tmp_doctorid, tmp_doctorname, errs := GetDoctorIds(v.PatientId, v.RecordDate, orgid)
- if errs != nil {
- err = errs
- return
- }
- pat, _ := GetPatientName(v.PatientId)
- dislist = append(dislist, &models.DispensingList{
- PatientID: v.PatientId,
- Name: pat.Name,
- DoctorId: tmp_doctorid,
- DoctorName: tmp_doctorname,
- RecordTime: v.RecordDate,
- RecordDate: fmt.Sprintf(time.Unix(v.RecordDate, 0).Format("2006-01-02 15:04:05")),
- })
- }
- return
- }
-
- // 处方详情//////////////////////
- func PrescriptionDetails(patient_id, record_date, orgid int64) (pre []*models.PrescripDetails, err error) {
- var tmp []*models.HisDoctorAdviceInfoL
- err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
- "status = 1 and patient_id = ? and user_org_id = ? and dispensing_time = ? and is_medicine = 1 and drug_id in (select id from xt_base_drug where org_id = ? and status = 1)", patient_id, orgid, record_date, orgid).Find(&tmp).Error
- if err != nil {
- return
- }
- for _, v := range tmp {
- pre = append(pre, &models.PrescripDetails{
- Drugname: v.AdviceName,
- SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
- Usage: v.DeliveryWay,
- Frequency: v.ExecutionFrequency,
- Days: config.ToString(v.Day) + "天",
- Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
- UnitPrice: config.ToString(v.Price) + "元",
- Remarks: v.Remark,
- })
- }
-
- var tmp_advice []*models.XtDoctorAdviceL
- err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
- "status = 1 and (advice_type = 2 or advice_type = 3) and patient_id = ? and user_org_id = ? and dispensing_time = ? and is_medicine = 1 and drug_id in (select id from xt_base_drug where org_id = ? and status = 1)", patient_id, orgid, record_date, orgid).Find(&tmp_advice).Error
- if err != nil {
- return
- }
- for _, v := range tmp_advice {
- pre = append(pre, &models.PrescripDetails{
- Drugname: v.AdviceName,
- SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
- Usage: v.DeliveryWay,
- Frequency: v.ExecutionFrequency,
- Days: "-",
- Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
- UnitPrice: "-",
- Remarks: v.Remark,
- })
- }
- return
- }
-
- // 查询病人的his id
- func GetHisID(prescription_id, orgid int64) (id int64) {
- var tmp models.HisPrintPrescription
- XTReadDB().Model(&models.HisPrintPrescription{}).Where("id = ?", prescription_id).Find(&tmp)
- var fin models.HisPrescriptionInfoTwo
- XTReadDB().Model(&models.HisPrescriptionInfoTwo{}).Where("prescription_number = ? and user_org_id = ?", tmp.PrescriptionNumber, orgid).Find(&fin)
- return fin.ID
- }
-
- // 根据药品id获取药品信息
- func GetDrugNameTX(id int64, tx *gorm.DB) (tmp models.SpBaseDrug, err error) {
- err = tx.Model(&models.SpBaseDrug{}).Where("id = ? and status = 1", id).Find(&tmp).Error
- return
- }
- func GetXtManufacturer(id int64, tx *gorm.DB) (name string, err error) {
- var tmp models.Manufacturer
- err = tx.Model(&models.Manufacturer{}).Where("id = ? and status = 1", id).Find(&tmp).Error
- name = tmp.ManufacturerName
- return
- }
-
- // 查询药品(使用keyword)
- func GetManyDrugs(orgid int64, keyword string) (map[int64]models.PharmacyBaseDrug, error) {
- var tmp []*models.PharmacyBaseDrug
- tt := make(map[int64]models.PharmacyBaseDrug)
- err := XTReadDB().Model(&models.PharmacyBaseDrug{}).Where(" org_id = ? ", orgid).Where(" drug_name like ? ", "%"+keyword+"%").Find(&tmp).Error
- for _, v := range tmp {
- tt[v.ID] = *v
- }
- return tt, err
- }
-
- // 查询(
- func GetTodayMedicine(stime, etime, orgid, is_medicine int64, keyword string) (finlly []*models.ListOfDrugs, err error) {
- InitDrugidIsNil(orgid, stime, etime)
-
- fmt.Println("startime==========================", stime)
- var tmp []*models.TmpLLL
- if is_medicine == 0 {
- if keyword != "" {
- keyword = "%" + keyword + "%"
- err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
- "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1) "+
- "union "+
- "select distinct drug_id from xt_doctor_advice where "+
- "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1)",
- stime, etime, orgid, is_medicine, orgid, keyword, stime, etime, orgid, is_medicine, orgid, keyword).Scan(&tmp).Error
- } else {
- err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
- "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) "+
- "union "+
- "select distinct drug_id from xt_doctor_advice where "+
- "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) ",
- stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
- }
- if err != nil {
- return
- }
- } else {
- if keyword != "" {
- keyword = "%" + keyword + "%"
- err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
- "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1) "+
- "union "+
- "select distinct drug_id from xt_doctor_advice where "+
- "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and drug_name like ? and is_pharmacy = 1)",
- stime, etime, orgid, is_medicine, orgid, keyword, stime, etime, orgid, is_medicine, orgid, keyword).Scan(&tmp).Error
- } else {
- err = XTReadDB().Raw("select distinct drug_id from his_doctor_advice_info where "+
- "status = 1 and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
- "union "+
- "select distinct drug_id from xt_doctor_advice where "+
- "status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and user_org_id = ? and is_medicine = ? and "+
- "drug_id in (select id from xt_base_drug where org_id = ? and status = 1) ",
- stime, etime, orgid, is_medicine, orgid, stime, etime, orgid, is_medicine, orgid).Scan(&tmp).Error
- }
- if err != nil {
- return
- }
- }
-
- for _, v := range tmp {
- name, specifications := FindDrugSpecifications(v.DrugId)
- number, errs := GetInventoryQuantity(orgid, v.DrugId)
- if errs != nil {
- err = errs
- return
- }
- finlly = append(finlly, &models.ListOfDrugs{
- ID: v.DrugId,
- Name: name,
- Specifications: specifications,
- Stock: number,
- })
- }
- return
- }
-
- // 根据药品id获取药品规格(
- func FindDrugSpecifications(id int64) (name, specifications string) {
- var tmp models.PharmacyBaseDrug
- XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ?", id).Find(&tmp)
- name = tmp.DrugName
- specifications = config.ToString(tmp.Dose) + tmp.DoseUnit + "*" + config.ToString(tmp.MinNumber) + tmp.MinUnit + "/" + config.ToString(tmp.MaxUnit)
- return
- }
-
- // 根据药品id获取当前药品出库仓库的库存数量
- func GetInventoryQuantity(orgid, drugid int64) (number string, err error) {
- //获取药品拆零数量
- var phar models.PharmacyBaseDrug
- XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ?", drugid).Find(&phar)
- min_number := phar.MinNumber
- if min_number == 0 {
- err = fmt.Errorf("药品拆零数量不能为零!")
- return
- }
- storeconfig, _ := FindStorehouseConfig(orgid)
- var tmp []models.SpDrugWarehouseInfo
- XTReadDB().Model(&models.SpDrugWarehouseInfo{}).Where("org_id = ? and drug_id = ? and storehouse_id = ? and status = 1", orgid, drugid, storeconfig.DrugStorehouseOut).Find(&tmp)
- max, min := int64(0), int64(0)
- for _, v := range tmp {
- max += v.StockMaxNumber
- min += v.StockMinNumber
- }
- max += min / min_number
- min = min % min_number
- if max != 0 {
- number = config.ToString(max) + phar.MaxUnit
- }
- if min != 0 {
- number = number + config.ToString(min) + phar.MinUnit
- }
- if number == "" {
- number = "0"
- }
- //number = config.ToString(max)+phar.MaxUnit+config.ToString(min)+phar.MinUnit
- return
- }
-
- // 获取该药品的病人信息(
- func FindMedicationList(orgid, drug_id, stime, etime, is_medicine int64) (pp []*models.PatientInformation, err error) { ///////////////
- InitDrugidIsNil(orgid, stime, etime)
- var tmp []*models.HisDoctorAdviceInfoL
- err = XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where(
- "status = 1 and advice_date >= ? and advice_date <= ? and user_org_id = ? and drug_id = ? and is_medicine = ?", stime, etime, orgid, drug_id, is_medicine).Find(&tmp).Error
- if err != nil {
- return pp, err
- }
- for _, v := range tmp {
- pp = append(pp, &models.PatientInformation{
- Id: "h" + config.ToString(v.ID),
- Name: FindUserName(v.PatientId),
- PatientId: v.PatientId,
- SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
- Usage: v.DeliveryWay,
- Frequency: v.ExecutionFrequency,
- Days: config.ToString(v.Day) + "天",
- Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
- DataSources: "his处方",
- People: GetUserAdminName(v.People, orgid), //领药人!!!!!!!!!!!!!!!!!!!!!!
- Quantity: int64(v.PrescribingNumber),
- Unit: v.PrescribingNumberUnit,
- DrugCode: v.DrugCode,
- ID: v.ID,
- })
- }
-
- var tmp_advice []*models.XtDoctorAdviceL
- err = XTReadDB().Model(&models.XtDoctorAdviceL{}).Where(
- "status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and user_org_id = ? and drug_id = ? and is_medicine = ?", stime, etime, orgid, drug_id, is_medicine).Find(&tmp_advice).Error
- if err != nil {
- return pp, err
- }
- for _, v := range tmp_advice {
- pp = append(pp, &models.PatientInformation{
- Id: "x" + config.ToString(v.ID),
- Name: FindUserName(v.PatientId),
- SingleDosage: config.ToString(v.SingleDose) + v.SingleDoseUnit,
- Usage: v.DeliveryWay,
- Frequency: v.ExecutionFrequency,
- Days: "",
- Total: config.ToString(v.PrescribingNumber) + v.PrescribingNumberUnit,
- DataSources: "临时医嘱",
- People: GetUserAdminName(v.People, orgid), //领药人
- Quantity: int64(v.PrescribingNumber),
- Unit: v.PrescribingNumberUnit,
- DrugCode: v.DrugCode,
- ID: v.ID,
- })
- }
- return
- }
-
- // 患者发药按钮点击(
- func DispensingMedicine(orgid, patient_id, stime, etime, creater int64) (err error) {
- //开事务
- tx := XTWriteDB().Begin()
- defer func() {
- if err != nil {
- utils.ErrorLog("事务失败,原因为: %v", err)
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- time_now := time.Now().Unix()
- var hids []*models.TmpID
- var xids []*models.TmpID
- err = tx.Raw("select id from his_doctor_advice_info where status = 1 and advice_date >= ? and advice_date <= ? "+
- "and user_org_id = ? and patient_id = ? and is_medicine = 0 and drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1) ", stime, etime, orgid, patient_id, orgid).Scan(&hids).Error
- if err != nil {
- return
- }
- hid := make([]int64, 0)
- for _, v := range hids {
- hid = append(hid, v.Id)
- }
- var advice_info []*models.HisDoctorAdviceInfo
- err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", hid).Find(&advice_info).Error
- if err != nil {
- return
- }
-
- if orgid != 10480 {
- for _, v := range advice_info {
-
- tmp_bool := IsPharmacyConfig(orgid)
- if tmp_bool {
- if PettyCash(v.DrugId) {
- continue
- }
- if orgid != 10217 && orgid != 10188 && orgid != 9671 && orgid != 10164 && orgid != 10387 && orgid != 10375 && orgid != 10480 {
- ////判断药品库存是否充足
- kou := FenDrugInventory(v, orgid)
- if !kou {
- err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
- return
- }
- }
-
- //扣减库存
- err = FenStock(orgid, creater, v)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
- }
- }
- }
-
- //修改状态
- errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id in (?)", hid).Updates(map[string]interface{}{
- "is_medicine": 1,
- "people": 0,
- "dispensing_time": time_now,
- }).Error
- if errs != nil {
- return errs
- }
- err1 := ChangeHisPrescription(hid)
- if err1 != nil {
- return err1
- }
-
- //针对大丰响水
- if orgid != 10217 && orgid != 10188 && orgid != 9671 && orgid != 10164 && orgid != 10387 && orgid != 10375 && orgid != 10480 {
- err = tx.Raw("select id from xt_doctor_advice where status = 1 and (advice_type = 2 or advice_type = 3) and advice_date >= ? and advice_date <= ? and "+
- "user_org_id = ? and patient_id = ? and is_medicine = 0 and drug_id in (select id from xt_base_drug where org_id = ? and is_pharmacy = 1)", stime, etime, orgid, patient_id, orgid).Scan(&xids).Error
- if err != nil {
- return
- }
- xid := make([]int64, 0)
- for _, v := range xids {
- xid = append(xid, v.Id)
- }
- var advice []*models.HisDoctorAdviceInfo
- err = tx.Raw("select * from xt_doctor_advice where id in (?) and status = 1", xid).Scan(&advice).Error
- if err != nil {
- return
- }
-
- for _, v := range advice {
- tmp_bool := IsPharmacyConfig(orgid)
- if tmp_bool {
- if PettyCash(v.DrugId) {
- continue
- }
- kou := FenDrugInventory(v, orgid)
- if !kou {
- err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
- return
- }
- //扣减库存
- err = FenStock(orgid, creater, v)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
- }
- }
- //修改状态
- errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id in (?)", xid).Updates(map[string]interface{}{
- "is_medicine": 1,
- "people": 0,
- "dispensing_time": time_now,
- "updated_time": time.Now().Unix(),
- }).Error
- if errs1 != nil {
- return errs1
- }
- }
-
- //生成明细记录
- tmp_ph := models.Pharmary{
- UserOrgId: orgid,
- PatientId: patient_id,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- Status: 1,
- RecordDate: time_now,
- }
- err = tx.Create(&tmp_ph).Error
-
- return
- }
-
- func DispensingMedicineOne(orgid, patient_id, stime, etime, creater int64) (err error) {
-
- //开事务
- tx := XTWriteDB().Begin()
- defer func() {
- if err != nil {
- utils.ErrorLog("事务失败,原因为: %v", err)
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
-
- //查询当前患者未发药的列表
- adviceList, err := GetHisPatientNoMedical(orgid, patient_id, stime, etime)
- //发药
- if len(adviceList) > 0 {
- for _, item := range adviceList {
-
- if orgid != 10480 {
- pharmacyConfig := IsPharmacyConfig(orgid)
- if pharmacyConfig == true {
- //扣减库存
- err = HisFaDrugsDelivery(orgid, creater, item)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
- }
- }
-
- //修改发药状态
- err = UpdateIsMedicalHisAdvice(item.ID)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
- err = UpdateHisPrescription(item.PrescriptionId)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
-
- //查询默认仓库
- storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
- //查询默认仓库剩余多少库存
- var sum_count int64
- stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, item.DrugId)
- for _, its := range stockInfo {
- baseDrug, _ := GetBaseDrugMedical(its.DrugId)
- if its.MaxUnit == baseDrug.MaxUnit {
- its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
- }
- sum_count += its.StockMaxNumber + its.StockMinNumber
- }
- UpdateBaseDrugSumTwo(item.DrugId, sum_count, orgid)
- //扣减库存
- UpdateDrugStockCount(item.DrugId, item.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
-
- over, _ := FindOverCount(item.DrugId, item.UserOrgId, storeHouseConfig.DrugStorehouseOut)
- UpdateActOut(over.ID, over.SumInCount, over.FlushCount, over.SumCancelCount)
- }
- }
-
- advice, err := GetDocAdviceList(orgid, patient_id, stime, etime)
- //响水不走这里
- if orgid != 10188 && orgid != 10480 && orgid != 10217 {
- if len(advice) > 0 {
- for _, item := range advice {
- err = DrugsDelivery(item.UserOrgId, item.ExecutionStaff, item)
- //查询默认仓库
- storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
- //查询默认仓库剩余多少库存
- var sum_count int64
- stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, item.DrugId)
- for _, its := range stockInfo {
- baseDrug, _ := GetBaseDrugMedical(its.DrugId)
- if its.MaxUnit == baseDrug.MaxUnit {
- its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
- }
- sum_count += its.StockMaxNumber + its.StockMinNumber
- }
- UpdateBaseDrugSumTwo(item.DrugId, sum_count, orgid)
- //扣减库存
- UpdateDrugStockCount(item.DrugId, item.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
-
- over, _ := FindOverCount(item.DrugId, item.UserOrgId, storeHouseConfig.DrugStorehouseOut)
- UpdateActOut(over.ID, over.SumInCount, over.FlushCount, over.SumCancelCount)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
- err = UpdateIsAdvice(item.ID)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
- }
- }
- }
-
- //生成明细记录
- tmp_ph := models.Pharmary{
- UserOrgId: orgid,
- PatientId: patient_id,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- Status: 1,
- RecordDate: time.Now().Unix(),
- }
- err = tx.Create(&tmp_ph).Error
-
- return
- }
-
- func GetHisPatientNoMedical(user_org_id int64, patient_id int64, startime int64, end_time int64) (advice []*models.HisDoctorAdviceInfo, err error) {
-
- err = XTReadDB().Where("user_org_id =? and patient_id = ? and advice_date>=? and advice_date<=? and is_medicine =0 and status=1", user_org_id, patient_id, startime, end_time).Find(&advice).Error
- return advice, err
- }
-
- func GetHisPatientNoMedicalOne(user_org_id int64, patient_id int64, drug_id int64, advice_date int64) (advice []*models.HisDoctorAdviceInfo, err error) {
-
- err = XTReadDB().Where("user_org_id =? and patient_id = ? and drug_id =? and advice_date=? and is_medicine =0 and status=1", user_org_id, patient_id, drug_id, advice_date).Find(&advice).Error
- return advice, err
- }
-
- func UpdateIsMedicalHisAdvice(id int64) (err error) {
- adviceInfo := models.HisDoctorAdviceInfo{}
- err = XTWriteDB().Model(&adviceInfo).Where("id =? and status =1", id).Update(map[string]interface{}{"is_medicine": 1, "dispensing_time": time.Now().Unix()}).Error
- return err
- }
- func UpdateHisPrescription(id int64) (err error) {
-
- prescriptionInfo := models.HisPrescription{}
- err = XTWriteDB().Model(&prescriptionInfo).Where("id = ? and status=1", id).Update(map[string]interface{}{"is_medicine": 1, "mtime": time.Now().Unix()}).Error
- return err
- }
-
- func GetDocAdviceList(user_org_id int64, patient_id int64, startime int64, end_time int64) (advice []*models.DoctorAdvice, err error) {
-
- err = XTReadDB().Where("user_org_id =? and patient_id = ? and advice_date>=? and advice_date<=? and is_medicine =0 and status=1", user_org_id, patient_id, startime, end_time).Find(&advice).Error
- return advice, err
-
- }
-
- func UpdateIsAdvice(id int64) (err error) {
-
- err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_medicine": 1, "dispensing_time": time.Now().Unix()}).Error
- return err
- }
-
- // 患者退药按钮点击
- func DrugWithdrawal(orgid, patient_id, stime, etime, creater int64) (err error) {
- //开事务
- tx := XTWriteDB().Begin()
- defer func() {
- if err != nil {
- utils.ErrorLog("事务失败,原因为: %v", err)
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- map_time := make(map[int64]int64)
- var hids []*models.TmpID
- var xids []*models.TmpID
- //var hid,xid []int64//查询已发药的药品
- err = tx.Raw("select id,dispensing_time from his_doctor_advice_info where status = 1 and advice_date >= ? and advice_date <= ? "+
- "and user_org_id = ? and patient_id = ? and is_medicine = 1 and drug_id in (select id from xt_base_drug where org_id = ? and status = 1)", stime, etime, orgid, patient_id, orgid).Scan(&hids).Error
- if err != nil {
- return
- }
- hid := make([]int64, 0)
- for _, v := range hids {
- hid = append(hid, v.Id)
- map_time[v.DispensingTime] = v.DispensingTime
- }
- //修改状态
- errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id in (?)", hid).Updates(map[string]interface{}{
- "is_medicine": 0,
- "people": creater,
- "dispensing_time": 0,
- }).Error
- if errs != nil {
- return errs
- }
- err1 := ChangeHisPrescriptionT(hid)
- if err1 != nil {
- return err1
- }
- var advice_info []*models.HisDoctorAdviceInfo
- err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", hid).Find(&advice_info).Error
- if err != nil {
- return
- }
-
- //针对阜阳经沃
- if orgid != 10480 {
- for _, v := range advice_info {
-
- //扣减库存
- if !IsPharmacyDelivery(v.DrugId, orgid) {
- continue
- }
- err = DrugAutoAddCancelInfo(v, creater)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
- //drug, _ := FindBaseDrugLibRecordSeven(orgid, v.DrugId)
- //查询默认仓库
- storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
- //查询默认仓库剩余多少库存
- var sum_count int64
- stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
- for _, its := range stockInfo {
- baseDrug, _ := GetBaseDrugMedical(its.DrugId)
- if its.MaxUnit == baseDrug.MaxUnit {
- its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
- }
- sum_count += its.StockMaxNumber + its.StockMinNumber
- }
-
- UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
- //扣减库存
- UpdateDrugStockCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
-
- //退药
- UpdateHisPrescriptionInfo(v.PatientId, v.AdviceDate, v.UserOrgId)
-
- //退药
- UpdateHisPrescriptionInfoOne(v.PatientId, v.AdviceDate, v.UserOrgId)
-
- }
- }
-
- err = tx.Raw("select id,dispensing_time from xt_doctor_advice where status = 1 and (advice_type = 2 or advice_type = 3) and created_time >= ? and created_time <= ? and "+
- "user_org_id = ? and patient_id = ? and is_medicine = 1 and drug_id in (select id from xt_base_drug where org_id = ? and status = 1)", stime, etime, orgid, patient_id, orgid).Scan(&xids).Error
- if err != nil {
- return
- }
- xid := make([]int64, 0)
- for _, v := range xids {
- xid = append(xid, v.Id)
- map_time[v.DispensingTime] = v.DispensingTime
- }
- //修改状态
- errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id in (?)", xid).Updates(map[string]interface{}{
- "is_medicine": 0,
- "people": creater,
- "dispensing_time": 0,
- "updated_time": time.Now().Unix(),
- }).Error
- if errs1 != nil {
- return errs1
- }
- var advice []*models.HisDoctorAdviceInfo
- err = tx.Raw("select * from xt_doctor_advice where id in (?) and status = 1", xid).Scan(&advice).Error
- if err != nil {
- return
- }
- if orgid != 10480 {
- for _, v := range advice {
- //扣减库存
- if !IsPharmacyDelivery(v.DrugId, orgid) {
- continue
- }
- err = DrugAutoAddCancelInfo(v, creater)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
- //查询默认仓库
- storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
- //查询默认仓库剩余多少库存
- var sum_count int64
- stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
- for _, its := range stockInfo {
- baseDrug, _ := GetBaseDrugMedical(its.DrugId)
- if its.MaxUnit == baseDrug.MaxUnit {
- its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
- }
- sum_count += its.StockMaxNumber + its.StockMinNumber
- }
- UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
- //扣减库存
- UpdateDrugStockCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
-
- }
- }
-
- //删除掉发药明细
- for k, _ := range map_time {
- err = tx.Model(&models.Pharmary{}).Where("user_org_id = ? and record_date = ? and patient_id = ? ", orgid, k, patient_id).Updates(map[string]interface{}{
- "status": 0,
- "mtime": time.Now().Unix(),
- }).Error
- if err != nil {
- return
- }
- }
- return
- }
-
- // 获取患者名称
- func FindUserName(patient_id int64) (name string) {
- var tmp models.GetHisName
- XTReadDB().Model(&models.GetHisName{}).Where("id = ?", patient_id).Find(&tmp)
- name = tmp.Name
- return
- }
-
- // 获取药品名称
- func FindDrugsName(drug_id int64) (name string) {
- var tmp models.Drug
- XTReadDB().Model(&models.Drug{}).Where("id = ?", drug_id).Find(&tmp)
- name = tmp.DrugName
- return
- }
-
- // 药品发药按钮点击(
- func MedicineDeparture(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, ",")
- var parameter string //测试
- patient_id := make(map[int64]int64)
- for _, v := range tmp_id {
- front := v[:1]
- after := v[1:]
- if front == "h" {
- var advice_info []*models.HisDoctorAdviceInfo
- err = tx.Model(&models.HisDoctorAdviceInfo{}).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
- }
- tmp_bool := IsPharmacyConfig(orgid)
- if tmp_bool {
- if PettyCash(v.DrugId) {
- continue
- }
- kou := FenDrugInventory(v, orgid)
- if !kou {
- err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
- tmp := models.XtErrs{
- OrgId: orgid,
- Route: "creater:" + config.ToString(creater),
- Parameter: parameter,
- Text_err: err.Error(),
- }
- XTWriteDB().Create(&tmp)
- return
- } else {
- tmps, _ := json.Marshal(advice_info) //
- parameter = parameter + string(tmps) //
- }
- //扣减库存
- err = FenStock(orgid, creater, v)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
- }
- }
-
- //修改状态
- errs := XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ?", after).Updates(map[string]interface{}{
- "is_medicine": 1,
- "people": creater,
- "dispensing_time": time_now,
- }).Error
- if errs != nil {
- return errs
- }
- err1 := ChangeHisPrescriptionid(after)
- if err1 != nil {
- return err1
- }
- } else {
- var advice []*models.HisDoctorAdviceInfo
- err = tx.Raw("select * from xt_doctor_advice where id = ? and status = 1 and (advice_type = 2 or advice_type = 3)", after).Scan(&advice).Error
- if err != nil {
- return
- }
- for _, v := range advice {
- if _, ok := patient_id[v.PatientId]; !ok {
- patient_id[v.PatientId] = v.PatientId
- }
- tmp_bool := IsPharmacyConfig(orgid)
- if tmp_bool {
- codeConfig, _ := GetDrugCodeConfig(orgid)
- if codeConfig.IsOpen == 1 {
- if v.DrugCode == "" || len(v.DrugCode) == 0 {
- err = fmt.Errorf(FindDrugsName(v.DrugId) + "药品追溯码不能为空!")
- return
- }
- }
- if PettyCash(v.DrugId) {
- continue
- }
- kou := FenDrugInventory(v, orgid)
- if !kou {
- err = fmt.Errorf(FindDrugsName(v.DrugId) + "库存不足")
- tmp := models.XtErrs{
- OrgId: orgid,
- Route: "creater:" + config.ToString(creater),
- Parameter: parameter,
- Text_err: err.Error(),
- }
- XTWriteDB().Create(&tmp)
- return
- } else {
- tmps, _ := json.Marshal(advice) //
- parameter = parameter + string(tmps) //
- }
-
- //扣减库存
- err = FenStock(orgid, creater, v)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
- }
- }
- //修改状态
- errs1 := XTWriteDB().Model(&models.XtDoctorAdviceL{}).Where("id = ?", after).Updates(map[string]interface{}{
- "is_medicine": 1,
- "people": creater,
- "dispensing_time": time_now,
- "updated_time": time.Now().Unix(),
- }).Error
- if errs1 != nil {
- return errs1
- }
- }
- }
-
- 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 GetUserAdminName(id, orgID int64) string {
- var tmp models.XTSgjUserAdminRole
- XTReadDB().Model(&models.XTSgjUserAdminRole{}).Where("admin_user_id = ? and org_id = ?", id, orgID).Find(&tmp)
- return tmp.UserName
- }
-
- // 根据id查询是否已发药,true已发药,false未发药gai
- func GiveTheMedicine(id int64) bool {
- var xue models.PharmacyDoctorAdvice
- XTReadDB().Model(&models.PharmacyDoctorAdvice{}).Where("id = ?", id).Find(&xue)
- if xue.IsMedicine == 1 {
- return true
- }
- return false
- }
-
- // 查询改组中的药品是否包含已发药的,true已发药,false未发药
- func GiveGroupMedicine(orgid, groupNo int64) bool {
- var total int
- XTReadDB().Model(&models.DoctorAdvice{}).Where("user_org_id = ? and groupno = ? and status = 1 and is_medicine = 1", orgid, groupNo).Count(&total)
- if total > 0 {
- return true
- }
- return false
- }
-
- // 查询处方中是否包含已发药的
- func GiveChuMedicine(id int64) bool {
- var total int
- XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", id).Count(&total)
- if total > 0 {
- return true
- }
- return false
- }
-
- // 根据处方id查找该处方中是否存在已发药的药品
- func IsChuIssuedDrugs(prescription_id int64) bool {
- var total int
- XTReadDB().Model(&models.HisDoctorAdviceInfoL{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", prescription_id).Count(&total)
- if total > 0 {
- return true
- }
- return false
- }
-
- // 判断该药品是否通过药房管理出库,true是,false否
- // id 药品id,org_id 机构id
- func IsPharmacyDelivery(id, org_id int64) bool {
- //判断药品是否通过药房管理
- var total01 int
- XTReadDB().Model(&models.PharmacyBaseDrug{}).Where("id = ? and is_pharmacy = 1", id).Count(&total01)
- //判断该机构是否通过药房管理出库
- var total02 int
- XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1", org_id).Count(&total02)
- if total01 > 0 && total02 > 0 {
- return true
- }
- return false
- }
-
- // 判断机构是否通过药房管理出库
- func IsPharmacyConfig(orgid int64) (bo bool) {
- var total int
- XTReadDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and is_open = 1 and status = 1", orgid).Count(&total)
- if total > 0 {
- return true
- }
- return false
- }
-
- // 根据患者id和发药时间,获取医生的id和姓名
- func GetDoctorIds(id, recordtime, orgid int64) (doctor_id int64, doctor_name string, err error) {
- var tmp []*models.TmpAdviceDoctor
- //var tmp []int64
- err = XTReadDB().Raw("select distinct advice_doctor from his_doctor_advice_info where "+
- "status = 1 and dispensing_time = ? and user_org_id = ? and is_medicine = 1 and patient_id = ? and drug_id in (select id from xt_base_drug where org_id = ? and status = 1) "+
- "union "+
- "select distinct advice_doctor from xt_doctor_advice where "+
- "status = 1 and (advice_type = 2 or advice_type = 3) and dispensing_time = ? and user_org_id = ? and is_medicine = 1 and patient_id = ? and drug_id in (select id from xt_base_drug where org_id = ? and status = 1) ",
- recordtime, orgid, id, orgid, recordtime, orgid, id, orgid).Scan(&tmp).Error
- if err != nil || len(tmp) == 0 {
- return
- }
- doctor_id = tmp[0].AdviceDoctor
- doctor_name = GetUserAdminName(doctor_id, orgid)
- return
- }
-
- func MapToJson(param map[string]interface{}) string {
- dataType, _ := json.Marshal(param)
- dataString := string(dataType)
- return dataString
- }
-
- // 封装扣减库存
- func FenStock(orgid, creater int64, v *models.HisDoctorAdviceInfo) (err error) {
-
- err = HisDrugsDelivery(orgid, creater, v)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
-
- //查询默认仓库
- storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
- //查询默认仓库剩余多少库存
- var sum_count int64
- stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, v.DrugId)
- for _, its := range stockInfo {
- baseDrug, _ := GetBaseDrugMedical(its.DrugId)
- if its.MaxUnit == baseDrug.MaxUnit {
- its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
- }
- sum_count += its.StockMaxNumber + its.StockMinNumber
- }
- UpdateBaseDrugSumTwo(v.DrugId, sum_count, orgid)
- //扣减库存
- UpdateDrugStockCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
-
- over, _ := FindOverCount(v.DrugId, v.UserOrgId, storeHouseConfig.DrugStorehouseOut)
- UpdateActOut(over.ID, over.SumInCount, over.FlushCount, over.SumCancelCount)
- return
- }
-
- // 封装查询药品库存是否足够
- func FenDrugInventory(item *models.HisDoctorAdviceInfo, orgid int64) bool {
- var total int64
- var prescribing_number_total int64
- houseConfig, _ := GetAllStoreHouseConfig(orgid)
- ////查询该药品是否有库存
- list, _ := GetDrugTotalCountTwenty(item.DrugId, item.UserOrgId, houseConfig.DrugStorehouseOut)
- ////查询改药品信息
- medical, _ := GetBaseDrugMedical(item.DrugId)
- ////判断单位是否相等
- if medical.MaxUnit == item.PrescribingNumberUnit {
- prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
- count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
- //转化为最小单位
- total = list.Count*medical.MinNumber + list.StockMinNumber
- prescribing_number_total = count * medical.MinNumber
- }
- if medical.MinUnit == item.PrescribingNumberUnit {
- prescribingNumber_temp := strconv.FormatFloat(math.Abs(item.PrescribingNumber), 'f', 0, 64)
- count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
- total = list.Count*medical.MinNumber + list.StockMinNumber
- prescribing_number_total = count
- }
-
- if prescribing_number_total <= total {
- //可以扣减
- return true
- } else {
- //不可扣减
- return false
- }
-
- }
- func GetAdviceIds1(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
- var advice []*models.DoctorAdvice
- err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
- if err != nil {
- return
- }
- for _, v := range advice {
- if !IsPharmacyDelivery(v.DrugId, orgid) {
- tmp = append(tmp, config.ToString(v.ID))
- } else {
- err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
- "execution_staff": execution_staff,
- "execution_time": thetime.Unix(),
- "updated_time": time.Now().Unix(),
- }).Error
- if err != nil {
- return
- }
- }
- }
- return
- }
- func GetAdviceIds2(ids []string, orgid, execution_staff int64, thetime time.Time) (tmp []string, err error) {
- var advice []*models.HisDoctorAdviceInfoL
- err = readDb.Model(&models.HisDoctorAdviceInfoL{}).Where("id IN (?) AND status = 1", ids).Find(&advice).Error
- if err != nil {
- return
- }
- for _, v := range advice {
- if !IsPharmacyDelivery(v.DrugId, orgid) {
- tmp = append(tmp, config.ToString(v.ID))
- } else {
- err = XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? ", v.ID).Updates(map[string]interface{}{
- "execution_staff": execution_staff,
- "execution_time": thetime.Unix(),
- "updated_time": time.Now().Unix(),
- }).Error
- if err != nil {
- return
- }
- }
- }
- return
- }
- func GetAdviceId1(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
- var advice models.DoctorAdvice
- err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND status = 1", id).Find(&advice).Error
- if err != nil {
- return false, err
- }
- if IsPharmacyDelivery(advice.DrugId, orgid) {
- err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? ", id).Updates(map[string]interface{}{
- "execution_staff": execution_staff,
- "execution_time": thetime.Unix(),
- "updated_time": time.Now().Unix(),
- }).Error
- if err != nil {
- return false, err
- }
- return true, err
- }
- return false, err
- }
- func GetAdviceId2(id, orgid, execution_staff int64, thetime time.Time) (bo bool, err error) {
- var advice models.HisDoctorAdviceInfoL
- err = readDb.Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? AND status = 1", id).Find(&advice).Error
- if err != nil {
- return false, err
- }
- if IsPharmacyDelivery(advice.DrugId, orgid) {
- err = XTWriteDB().Model(&models.HisDoctorAdviceInfoL{}).Where("id = ? ", id).Updates(map[string]interface{}{
- "execution_staff": execution_staff,
- "execution_time": thetime.Unix(),
- "updated_time": time.Now().Unix(),
- }).Error
- if err != nil {
- return false, err
- }
- return true, err
- }
- return false, err
- }
-
- // 判断药品是否零用
- func PettyCash(id int64) bool {
- drug := models.XtBaseDrug{}
- XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug)
- if drug.IsUse == 1 {
- return true
- }
- return false
- }
-
- // 初始化
- func InitDrugidIsNil(orgid, stime, etime int64) {
- var advice []*models.DoctorAdvice
- XTReadDB().Model(&models.DoctorAdvice{}).Where("drug_id = 0 and user_org_id = ? and status = 1 and created_time >= ? and created_time <= ?", orgid, stime, etime).Find(&advice)
- for _, v := range advice {
- XTWriteDB().Exec("update xt_doctor_advice set drug_id = (select id from xt_base_drug where drug_name = ? and org_id = ? and status = 1 limit 1) where id = ?", v.AdviceName, v.UserOrgId, v.ID)
- }
- }
-
- // 改变处方状态(发药
- func ChangeHisPrescription(tmp []int64) (err error) {
- var advice_info []*models.HisDoctorAdviceInfo
- err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil
- }
- return
- }
-
- tmp_id := make(map[int64]int64)
- for _, v := range advice_info {
- tmp_id[v.PrescriptionId] = v.PrescriptionId
- }
- for k, _ := range tmp_id {
- err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
- "is_medicine": 1,
- "mtime": time.Now().Unix(),
- }).Error
- if err != nil {
- return
- }
- }
- return
- }
-
- // 改变处方状态(退药
- func ChangeHisPrescriptionT(tmp []int64) (err error) {
- var advice_info []*models.HisDoctorAdviceInfo
- err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("id in (?) and status = 1", tmp).Find(&advice_info).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil
- }
- return
- }
-
- tmp_id := make(map[int64]int64)
- for _, v := range advice_info {
- tmp_id[v.PrescriptionId] = v.PrescriptionId
- }
- for k, _ := range tmp_id {
- var total int
- //查询退药的
- XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("prescription_id = ? and status = 1 and is_medicine = 1", k).Count(&total)
- if total == 0 {
- err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", k).Updates(map[string]interface{}{
- "is_medicine": 0,
- "mtime": time.Now().Unix(),
- }).Error
- if err != nil {
- return
- }
- }
- }
- return
- }
-
- // 改变处方状态
- func ChangeHisPrescriptionid(id string) (err error) {
- var advice models.HisDoctorAdviceInfo
- err = XTReadDB().Model(&models.HisDoctorAdviceInfo{}).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
- }
-
- // 获取药品规格
- func ReplacementDrugs(orgid int64, special bool) (list []*models.ReplacementDrugs, err error) {
- var g errgroup.Group
- var lib []*models.BaseDrugLib
- var lock sync.Mutex
-
- if special {
- err = readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? and drug_status not like '%停用%' AND status = 1 and is_special_diseases = 1", orgid).Find(&lib).Error
-
- } else {
- err = readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? and drug_status not like '%停用%' AND status = 1 ", orgid).Find(&lib).Error
-
- }
- if err != nil {
- return
- }
- for _, v := range lib {
- v := v
- g.Go(func() error {
- var tmp models.Manufacturer
- err := readDb.Model(&models.Manufacturer{}).Where("id = ?", v.Manufacturer).Find(&tmp).Error
- if err != nil {
- return err
- }
-
- lock.Lock()
-
- if orgid == 10206 {
-
- list = append(list, &models.ReplacementDrugs{
- Id: v.ID,
- Name: v.DrugName + " " + v.DrugSpec + " " + v.Number,
- })
- lock.Unlock()
-
- } else {
- list = append(list, &models.ReplacementDrugs{
- Id: v.ID,
- Name: v.DrugName + " " + v.Dose + v.DoseUnit + "*" + config.ToString(v.MinNumber) + v.MinUnit + "/" + v.MaxUnit + " " + tmp.ManufacturerName,
- })
- lock.Unlock()
- }
- return err
- })
- }
- if errs := g.Wait(); errs != nil {
- err = errs
- return
- }
- return
- }
-
- // 获取药品规格,不考虑特殊病
- func ReplacementDrugsT(orgid int64) (list []*models.ReplacementDrugs, err error) {
- var lib []*models.BaseDrugLib
- err = readDb.Model(&models.BaseDrugLib{}).Where("org_id = ? AND status = 1 and drug_status not like '%停用%' ", orgid).Find(&lib).Error
- if err != nil {
- return
- }
- for _, v := range lib {
-
- if orgid == 10206 {
- list = append(list, &models.ReplacementDrugs{
- Id: v.ID,
- Name: v.DrugName + " " + v.DrugSpec + " " + v.Number,
- })
-
- } else {
- var tmp models.Manufacturer
- err = readDb.Model(&models.Manufacturer{}).Where("id = ?", v.Manufacturer).Find(&tmp).Error
- if err != nil {
- return
- }
- list = append(list, &models.ReplacementDrugs{
- Id: v.ID,
- Name: v.DrugName + " " + v.Dose + v.DoseUnit + "*" + config.ToString(v.MinNumber) + v.MinUnit + "/" + v.PrescribingNumberUnit + " " + tmp.ManufacturerName,
- })
- }
- }
- return
- }
-
- // 根据药品id获取药品名字
- func IdToDrugName(id int64) (name string) {
- var lib models.BaseDrugLib
- readDb.Model(&models.BaseDrugLib{}).Where("id = ?", id).Find(&lib)
- return lib.DrugName
- }
-
- // 获取项目和耗材和套餐
- func ProjectConsumables3(orgid int64) (list []*models.DropDownList, err error) {
-
- var project []*models.XtHisProject
- project, err = GetHisProject(orgid)
- if err != nil {
- return
- }
- for _, v := range project {
- list = append(list, &models.DropDownList{
- Id: "p" + config.ToString(v.ID),
- Name: v.ProjectName,
- })
- }
-
- var good []*models.GoodInfo
- err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and status = 1", orgid).Find(&good).Error
- if err != nil {
- return
- }
- for _, v := range good {
- list = append(list, &models.DropDownList{
- Id: "i" + config.ToString(v.ID),
- Name: v.GoodName,
- })
- }
-
- var team []*models.XtHisProjectTeam
- team, err = GetAllProjectTeam(orgid)
- if err != nil {
- return
- }
- for _, v := range team {
- list = append(list, &models.DropDownList{
- Id: "h" + config.ToString(v.ID),
- Name: v.ProjectTeam,
- })
- }
-
- return
- }
-
- // 获取项目和耗材
- func ProjectConsumables2(orgid int64, special bool) (list []*models.DropDownList, err error) {
-
- var project []*models.XtHisProject
- if special {
- err = XTReadDB().Model(&project).Where("user_org_id = ? and medical_status != 1 and status = 1 and disease_directory = 1", orgid).Find(&project).Error
- } else {
- err = XTReadDB().Model(&project).Where("user_org_id = ? and medical_status != 1 and status = 1", orgid).Find(&project).Error
- }
- if err != nil {
- return
- }
- for _, v := range project {
- list = append(list, &models.DropDownList{
- Id: "p" + config.ToString(v.ID),
- Name: v.ProjectName,
- })
- }
-
- var good []*models.GoodInfo
- if special {
- err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and good_status not like '%停用%' and status = 1 and is_special_diseases = 1", orgid).Find(&good).Error
- } else {
- err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and good_status not like '%停用%' and status = 1", orgid).Find(&good).Error
- }
- if err != nil {
- return
- }
- for _, v := range good {
- list = append(list, &models.DropDownList{
- Id: "i" + config.ToString(v.ID),
- Name: v.GoodName + " " + v.SpecificationName + " " + v.GoodNumber,
- })
- }
-
- return
- }
-
- // 获取项目和耗材不考虑特殊病
- func ProjectConsumables2T(orgid int64) (list []*models.DropDownList, err error) {
-
- var project []*models.XtHisProject
- err = XTReadDB().Model(&project).Where("user_org_id = ? and status = 1 and medical_status != 1", orgid).Find(&project).Error
- if err != nil {
- return
- }
- for _, v := range project {
- list = append(list, &models.DropDownList{
- Id: "p" + config.ToString(v.ID),
- Name: v.ProjectName,
- })
- }
-
- var good []*models.GoodInfo
- err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? and status = 1 and good_status != 1", orgid).Find(&good).Error
- if err != nil {
- return
- }
- for _, v := range good {
- list = append(list, &models.DropDownList{
- Id: "i" + config.ToString(v.ID),
- Name: v.GoodName + " " + v.SpecificationName + " " + v.GoodNumber,
- })
- }
-
- return
- }
-
- // 查询药品
- func FindPatientDrug(orgid, patient_id, drugid, mode int64) bool {
- var total int
- XTReadDB().Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1 and "+
- "prescription_id in (select id from his_prescription_info_template where status = 1 and user_org_id = ? and type = 1 and p_template_id in ("+
- "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
- if total > 0 {
- return true
- }
- return false
- }
-
- // 查询项目
- func FindPatientXiang(orgid, patient_id, drugid, mode int64) bool {
- var total int
- XTReadDB().Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and status = 1 and type = 2 and "+
- "prescription_id in (select id from his_prescription_info_template where status = 1 and user_org_id = ? and type = 2 and p_template_id in ("+
- "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
- if total > 0 {
- return true
- }
- return false
- }
-
- // 查询耗材
- func FindPatientXiang2(orgid, patient_id, drugid, mode int64) bool {
- var total int
- XTReadDB().Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and status = 1 and type = 3 and "+
- "prescription_id in (select id from his_prescription_info_template where status = 1 and user_org_id = ? and type = 2 and p_template_id in ("+
- "select id from his_prescription_template where status= 1 and user_org_id = ? and mode = ?))", orgid, patient_id, drugid, orgid, orgid, mode).Count(&total)
- if total > 0 {
- return true
- }
- return false
- }
-
- // types处方类型,1.药品 2.项目,model透析模式
- func GetHisInfoTempalteId(model, orgid, types int64) (tp []int64) {
- type tmpid struct {
- Id int64
- }
- var tmp []*tmpid
- XTReadDB().Raw("select id from his_prescription_info_template where status = 1 and user_org_id = ? and type = ? and p_template_id in ("+
- "select id from his_prescription_template where status = 1 and user_org_id = ? and mode = ?)", orgid, types, orgid, model).Scan(&tmp)
- for _, v := range tmp {
- tp = append(tp, v.Id)
- }
- return
- }
-
- // 替换药品处方模板
- func ReplaceDrugPrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, s models.HisPrescriptionAdviceTemplate, tx *gorm.DB) (err error) {
- err = tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and "+
- "status = 1 and prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
- "drug_id": s.DrugId,
- "advice_name": s.AdviceName,
- "single_dose": s.SingleDose,
- "single_dose_unit": s.SingleDoseUnit,
- "delivery_way": s.DeliveryWay,
- "execution_frequency": s.ExecutionFrequency,
- "day": s.Day,
- "prescribing_number": s.PrescribingNumber,
- "prescribing_number_unit": s.PrescribingNumberUnit,
- "price": s.Price,
- "remark": s.Remark,
- "updated_time": time.Now().Unix(),
- }).Error
- return err
- }
-
- // 替换项目模板
- func ReplaceProjectPrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, s models.HisPrescriptionProjectTemplate, tx *gorm.DB) (err error) {
- err = tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and "+
- "status = 1 and prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
- "project_id": s.ProjectId,
- "type": s.Type,
- "frequency_type": s.FrequencyType,
- "day_count": s.DayCount,
- "week_day": s.WeekDay,
- "price": s.Price,
- "count": s.Count,
- "single_dose": s.SingleDose,
- "delivery_way": s.DeliveryWay,
- "execution_frequency": s.ExecutionFrequency,
- "remark": s.Remark,
- "day": s.Day,
- "unit": s.Unit,
- "mtime": time.Now().Unix(),
- }).Error
- return err
- }
-
- // 删除项目模板
- func DeleteProjectTemplate(orgid, patient_id, drugid int64, ids []int64, tx *gorm.DB) (err error) {
- err = tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and patient_id = ? and project_id = ? and "+
- "prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
- "status": 0,
- "mtime": time.Now().Unix(),
- }).Error
- return err
- }
-
- // 删除处方模板
- func DeletePrescriptionTemplate(orgid, patient_id, drugid int64, ids []int64, tx *gorm.DB) (err error) {
- err = tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and "+
- "prescription_id in (?)", orgid, patient_id, drugid, ids).Updates(map[string]interface{}{
- "status": 0,
- "updated_time": time.Now().Unix(),
- }).Error
- return err
- }
-
- // 根据透析模式和患者id获取该患者的处方内容
- func PTemplateInformation(orgid, mode_id, patient_id int64) (tmp interface{}, err error) {
- //获取处方id
- var tt []*models.HisPrescriptionTemplate
- err = XTReadDB().Raw("select id from his_prescription_template where status = 1 and user_org_id = ? and mode = ? and patient_id = ?", orgid, mode_id, patient_id).Scan(&tt).Error
-
- if len(tt) > 1 {
- err = fmt.Errorf("数据有误")
- return
- }
- x := make(map[int64]string, 0) //项目
- h := make(map[int64]string, 0) //耗材
- for _, v := range tt {
- var prescriptions []*models.HisPrescriptionInfoTemplateL
- prescriptions, err = GetHisPrescriptionTemplateL(v.ID, orgid)
- if err != nil {
- return
- }
- for i := 0; i < len(prescriptions); i++ {
- for j := 0; j < len(prescriptions[i].HisPrescriptionProjectTemplate); j++ {
- if prescriptions[i].HisPrescriptionProjectTemplate[j].Type == 2 {
- //项目
- if v, ok := x[prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification]; ok {
- prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate = v
- } else {
- prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate, err = TranslateZu(prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification, orgid, "统计分类")
- if err != nil {
- return
- }
- x[prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.StatisticalClassification] = prescriptions[i].HisPrescriptionProjectTemplate[j].XtHisProject.Translate
- }
-
- }
- if prescriptions[i].HisPrescriptionProjectTemplate[j].Type == 3 {
- //耗材
- if v, ok := h[prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.GoodKind]; ok {
- prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate = v
- } else {
- prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate = "耗材"
- h[prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.GoodKind] = prescriptions[i].HisPrescriptionProjectTemplate[j].GoodInfo.Translate
- }
- }
- }
- }
- tmp = prescriptions
- }
- return
- }
- func DeleteOne(types, id int64) (err error) {
- if types == 1 {
- //药品
- err = XTWriteDB().Model(&models.HisPrescriptionAdviceTemplate{}).Where("id = ?", id).Updates(map[string]interface{}{
- "status": 0,
- "updated_time": time.Now().Unix(),
- }).Error
- if err != nil {
- return
- }
- } else if types == 2 {
- //项目
- err = XTWriteDB().Model(&models.HisPrescriptionProjectTemplateL{}).Where("id = ?", id).Updates(map[string]interface{}{
- "status": 0,
- "mtime": time.Now().Unix(),
- }).Error
- if err != nil {
- return
- }
-
- } else {
- err = fmt.Errorf("参数错误")
- return
- }
- return
-
- }
-
- // 收尾工作
- func Scavenger(orgid int64) (err error) {
- //开事务
- tx := XTWriteDB().Begin()
- defer func() {
- if err != nil {
- utils.ErrorLog("事务失败,原因为: %v", err)
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- //查询该机构的所有模板
- var tmp1 []*models.HisPrescriptionTemplate
- var tmp2 []*models.HisPrescriptionInfoTemplate
- deltmp1 := make([]int64, 0) //清除的数据
- deltmp2 := make([]int64, 0) //清除的数据
- err = tx.Model(&models.HisPrescriptionTemplate{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp1).Error
- if err != nil {
- return
- }
- for _, v := range tmp1 {
- var total1 int
- tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("user_org_id = ? and p_template_id = ? and status = 1", orgid, v.ID).Count(&total1)
- if total1 == 0 {
- deltmp1 = append(deltmp1, v.ID)
- }
- }
- err = tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("user_org_id = ? and status = 1", orgid).Find(&tmp2).Error
- if err != nil {
- return
- }
- for _, v := range tmp2 {
- if v.Type == 1 {
- var total2 int
- tx.Model(&models.HisPrescriptionAdviceTemplate{}).Where("user_org_id = ? and prescription_id = ? and status = 1", orgid, v.ID).Count(&total2)
- if total2 == 0 {
- deltmp2 = append(deltmp2, v.ID)
- }
- }
- if v.Type == 2 {
- var total3 int
- tx.Model(&models.HisPrescriptionProjectTemplate{}).Where("user_org_id = ? and prescription_id = ? and status = 1", orgid, v.ID).Count(&total3)
- if total3 == 0 {
- deltmp2 = append(deltmp2, v.ID)
- }
- }
- }
-
- //清除1
- err = tx.Model(&models.HisPrescriptionTemplate{}).Where("id in (?)", deltmp1).Updates(map[string]interface{}{
- "status": 0,
- "mtime": time.Now().Unix(),
- }).Error
- if err != nil {
- return
- }
- //清除1
- err = tx.Model(&models.HisPrescriptionInfoTemplate{}).Where("id in (?)", deltmp2).Updates(map[string]interface{}{
- "status": 0,
- "mtime": time.Now().Unix(),
- }).Error
- if err != nil {
- return
- }
-
- return
- }
-
- // 分区和收费(患者发药
- func PartitionAndLayout(stime, etime, orgid, shift, partition int64, flist []*models.TmpPatient) (tmp []*models.TmpPatient, err error) {
- //获取排班
- var sch []*models.XtScheduleTwo
- ma := make(map[int64]int64)
- s := "user_org_id = ? and status = 1 and schedule_date >= ? and schedule_date <= ?"
- if shift != 0 {
- s = s + " and schedule_type = " + config.ToString(shift)
- }
- if partition != 0 {
- s = s + " and partition_id = " + config.ToString(partition)
- }
- if shift == 0 && partition == 0 {
- return flist, nil
- }
- err = XTReadDB().Model(&models.XtScheduleTwo{}).Where(s, orgid, stime, etime).Find(&sch).Error
-
- if err != nil {
- return
- }
- for _, v := range sch {
- ma[v.PatientId] = v.PatientId
- }
- for _, v := range flist {
- if _, ok := ma[v.PatientID]; ok {
- tmp = append(tmp, v)
- }
- }
- return
- }
-
- // 分区和收费(药品发药
- func PartitionAndLayoutDrug(deliveryway string, stime, etime, orgid, shift, partition int64, flist []*models.PatientInformation) (tmp []*models.PatientInformation, err error) {
- //获取排班
- var sch []*models.XtScheduleTwo
- ma := make(map[int64]int64)
- s := "user_org_id = ? and status = 1 and schedule_date >= ? and schedule_date <= ?"
- if shift != 0 {
- s = s + " and schedule_type = " + config.ToString(shift)
- }
- if partition != 0 {
- s = s + " and partition_id = " + config.ToString(partition)
- }
- if shift == 0 && partition == 0 {
- for _, v := range flist {
- ma[v.PatientId] = v.PatientId
- }
- for _, v := range flist {
- if _, ok := ma[v.PatientId]; ok && (v.Usage == deliveryway || deliveryway == "全部") {
- tmp = append(tmp, v)
- }
- }
- return tmp, nil
- }
- err = XTWriteDB().Model(&models.XtScheduleTwo{}).Where(s, orgid, stime, etime).Find(&sch).Error
- if err != nil {
- return
- }
- for _, v := range sch {
- ma[v.PatientId] = v.PatientId
- }
-
- for _, v := range flist {
- if _, ok := ma[v.PatientId]; ok && (v.Usage == deliveryway || deliveryway == "全部") {
- tmp = append(tmp, v)
- }
- }
- return
- }
-
- // 给药途径
- func Administration(deliveryway string, orgid int64, flist []*models.ListOfDrugs) (tmp []*models.ListOfDrugs, err error) {
- //查询药品
- var pp []*models.PharmacyBaseDrug
- s := "org_id = ? and status = 1"
- if deliveryway == "全部" {
- tmp = flist
- return
- } else {
- s = s + " and delivery_way = '" + deliveryway + "'"
- }
- err = XTReadDB().Model(&models.PharmacyBaseDrug{}).Where(s, orgid).Find(&pp).Error
- if err != nil {
- return
- }
- ma := make(map[int64]int64)
- for _, v := range pp {
- ma[v.ID] = v.ID
- }
- for _, v := range flist {
- if _, ok := ma[v.ID]; ok {
- tmp = append(tmp, v)
- }
- }
- return
- }
-
- // 计算总量
- func CalculateTheTotalAmount(tmp []*models.PatientInformation, drug_id int64) (total string, err error) {
- //获取药品信息
- var t models.SpBaseDrug
- err = XTReadDB().Model(&models.SpBaseDrug{}).Where("id = ?", drug_id).Find(&t).Error
- if err != nil {
- return
- }
- terr, maxunit, minunit := int64(0), int64(0), int64(0) //terr异常的单位数量,maxunit包装单位数量,minunit拆零单位数量
- minmunber := t.MinNumber //拆零数量
- terrUnit := ""
- for _, v := range tmp {
-
- switch v.Unit {
- case t.MaxUnit:
- maxunit = maxunit + v.Quantity
- case t.MinUnit:
- minunit = minunit + v.Quantity
- default:
- terr = terr + v.Quantity
- terrUnit = v.Unit
- }
- }
- if minunit >= minmunber {
- if minmunber == 0 {
- err = fmt.Errorf("拆零数量不能为零")
- return
- }
- maxunit = maxunit + minunit/minmunber
- minunit = minunit % minmunber
-
- }
- if terr > 0 {
- total = total + config.ToString(terr) + terrUnit
- }
- if maxunit > 0 {
- total = total + config.ToString(maxunit) + t.MaxUnit
- }
- if minunit > 0 {
- total = total + config.ToString(minunit) + t.MinUnit
- }
-
- return
- }
- func GetAllValidDeviceZones02(orgID int64) ([]*models.DeviceZone, error) {
- var zones []*models.DeviceZone
- err := readDb.Model(&models.DeviceZone{}).Where("org_id = ? and status = 1", orgID).Find(&zones).Error
- if err != nil {
- return nil, err
- }
- return zones, nil
- }
-
- func FindeHisAdviceDocAdvice(orgid int64, patient_id int64, stime int64, etime int64) (advice []*models.HisDoctorAdviceInfo, err error) {
-
- db := XTReadDB().Model(&advice).Where("status= 1 and is_medicine = 0")
-
- if orgid > 0 {
- db = db.Where("user_org_id = ?", orgid)
- }
- if patient_id > 0 {
- db = db.Where("patient_id = ?", patient_id)
- }
- if stime > 0 {
- db = db.Where("advice_date >=?", stime)
- }
- if etime > 0 {
- db = db.Where("advice_date<=?", etime)
- }
- err = db.Find(&advice).Error
-
- return advice, err
- }
-
- func FindeHisAdviceDocAdviceOne(orgid int64, patient_id int64, drug_id int64, advice_date int64) (advice []*models.HisDoctorAdviceInfo, err error) {
-
- db := XTReadDB().Model(&advice).Where("status= 1 and is_medicine = 0")
-
- if orgid > 0 {
- db = db.Where("user_org_id = ?", orgid)
- }
- if patient_id > 0 {
- db = db.Where("patient_id = ?", patient_id)
- }
- if drug_id > 0 {
- db = db.Where("drug_id =?", drug_id)
- }
- if advice_date > 0 {
- db = db.Where("advice_date = ?", advice_date)
- }
- err = db.Find(&advice).Error
-
- return advice, err
- }
-
- func GetFlow(drug_id int64, system_time int64, org_id int64, patient_id int64) (flow []*models.DrugFlow, err error) {
-
- err = XTWriteDB().Where("drug_id = ? and system_time =? and user_org_id = ? and status = 1 and patient_id =?", drug_id, system_time, org_id, patient_id).Find(&flow).Error
- return flow, err
- }
-
- func UpdateAdviceFlow(id int64) error {
- err := XTWriteDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and status = 1 and is_medicine =0", id).Update(map[string]interface{}{"is_medicine": 1}).Error
- return err
- }
-
- func UpdateHisPrescriptionInfo(patient_id int64, advice_date int64, user_org_id int64) error {
- err = XTWriteDB().Model(&models.HisDoctorAdvice{}).Where("patient_id = ? and advice_date = ? and user_org_id= ? and status=1", patient_id, advice_date, user_org_id).Updates(map[string]interface{}{"is_medicine": 0}).Error
-
- err = XTWriteDB().Model(&models.HisPrescription{}).Where("patient_id = ? and record_date = ? and user_org_id = ? and status=1", patient_id, advice_date, user_org_id).Updates(map[string]interface{}{"is_medicine": 0}).Error
- return err
- }
-
- func UpdateHisPrescriptionInfoOne(patient_id int64, advice_date int64, user_org_id int64) error {
-
- err = XTWriteDB().Model(&models.HisPrescription{}).Where("patient_id = ? and record_date = ? and user_org_id = ? and status=1", patient_id, advice_date, user_org_id).Updates(map[string]interface{}{"is_medicine": 0}).Error
- return err
- }
-
- func GetHisPatientAdviceList(user_org_id int64, patient_id int64, start_time int64, end_time int64) (list []*models.HisDoctorAdvice, err error) {
-
- err = XTReadDB().Model(&models.HisDoctorAdvice{}).Where("patient_id=? and advice_date=? and advice_date>=? and advice_date<=? and status=1 and user_org_id = ? and is_medicine =1", patient_id, start_time, end_time, user_org_id).Find(&list).Error
- return list, err
- }
-
- func GetGoodInventoryList(keyword string, org_id int64) (good []*models.GoodInfoEight, err error) {
-
- keyword = "%" + keyword + "%"
-
- db := XTReadDB().Where("status=1")
-
- if len(keyword) > 0 {
- db = db.Where("good_name like ? or specification_name like ?", keyword, keyword)
- }
- if org_id > 0 {
- db = db.Where("org_id = ?", org_id)
- }
-
- err = db.Order("id desc").Find(&good).Error
-
- return good, err
- }
-
- func GetGoodInventoryByGoodId(id int64) (info []*models.XtWarehouseInfoEight, err error) {
-
- err = XTReadDB().Where("good_id = ? and status=1 and is_check=1", id).Order("id desc").Find(&info).Error
-
- return info, err
- }
-
- func GetInventoryWarehouseInfo(id int64) (models.XtWarehouseInfoEight, error) {
-
- infoEight := models.XtWarehouseInfoEight{}
- err := XTReadDB().Where("id = ? and status=1", id).Find(&id).Error
-
- return infoEight, err
- }
-
- func UpdateWarehouseInfoById(last_stock_count int64, id int64) (models.WarehousingInfo, error) {
-
- info := models.WarehousingInfo{}
-
- err := XTWriteDB().Model(&info).Where("id = ? and status =1", id).Updates(map[string]interface{}{"stock_count": last_stock_count}).Error
- return info, err
- }
-
- func ChangeHisDrugCode(id int64, drug_code string, user_org_id int64) (models.HisDoctorAdviceInfo, error) {
-
- adviceInfo := models.HisDoctorAdviceInfo{}
-
- err := XTWriteDB().Model(&adviceInfo).Where("id = ? and user_org_id = ? and status =1", id, user_org_id).Updates(map[string]interface{}{"drug_code": drug_code}).Error
-
- fmt.Println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee", err)
- return adviceInfo, err
- }
-
- func ChangeAdivceDrugCode(id int64, drug_code string, user_org_id int64) (models.DoctorAdvice, error) {
-
- adviceInfo := models.DoctorAdvice{}
-
- err := XTWriteDB().Model(&adviceInfo).Where("id = ? and user_org_id = ? and status =1", id, user_org_id).Updates(map[string]interface{}{"drug_code": drug_code}).Error
-
- return adviceInfo, err
- }
-
- func DispensingMedicineTwo(orgid, patient_id, advice_date, drug_id, creater int64) (err error) {
-
- //开事务
- tx := XTWriteDB().Begin()
- defer func() {
- if err != nil {
- utils.ErrorLog("事务失败,原因为: %v", err)
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
-
- //查询当前患者未发药的列表
- adviceList, err := GetHisPatientNoMedicalOne(orgid, patient_id, drug_id, advice_date)
- //发药
- if len(adviceList) > 0 {
- for _, item := range adviceList {
-
- pharmacyConfig := IsPharmacyConfig(orgid)
- if pharmacyConfig == true {
- //扣减库存
- err = HisFaDrugsDelivery(orgid, creater, item)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
- }
-
- //修改发药状态
- err = UpdateIsMedicalHisAdvice(item.ID)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
- err = UpdateHisPrescription(item.PrescriptionId)
- if err != nil {
- err = fmt.Errorf("!:%v", err)
- return
- }
-
- //查询默认仓库
- storeHouseConfig, _ := GetAllStoreHouseConfig(orgid)
- //查询默认仓库剩余多少库存
- var sum_count int64
- stockInfo, _ := GetDrugAllStockInfo(storeHouseConfig.DrugStorehouseOut, orgid, item.DrugId)
- for _, its := range stockInfo {
- baseDrug, _ := GetBaseDrugMedical(its.DrugId)
- if its.MaxUnit == baseDrug.MaxUnit {
- its.StockMaxNumber = its.StockMaxNumber * baseDrug.MinNumber
- }
- sum_count += its.StockMaxNumber + its.StockMinNumber
- }
- UpdateBaseDrugSumTwo(item.DrugId, sum_count, orgid)
- //扣减库存
- UpdateDrugStockCount(item.DrugId, item.UserOrgId, storeHouseConfig.DrugStorehouseOut, sum_count)
-
- over, _ := FindOverCount(item.DrugId, item.UserOrgId, storeHouseConfig.DrugStorehouseOut)
- UpdateActOut(over.ID, over.SumInCount, over.FlushCount, over.SumCancelCount)
- }
- }
-
- //生成明细记录
- tmp_ph := models.Pharmary{
- UserOrgId: orgid,
- PatientId: patient_id,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- Status: 1,
- RecordDate: time.Now().Unix(),
- }
- err = tx.Create(&tmp_ph).Error
-
- return
- }
|