self_drug_service.go 109KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "time"
  7. )
  8. func GetCurrentPatient(orgid int64) (patients []*models.DrugPatients, err error) {
  9. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&patients).Error
  10. return patients, err
  11. }
  12. func GetAllDrugName(orgid int64) (baseDrug []*models.XtBaseDrug, err error) {
  13. err = XTReadDB().Group("drug_name").Where("org_id = ? and status = 1 and find_in_set('停用',drug_status) = 0", orgid).Find(&baseDrug).Error
  14. return baseDrug, err
  15. }
  16. func GetDrugName(drugname string, orgid int64) (*models.XtDrugName, error) {
  17. drugName := models.XtDrugName{}
  18. err := XTReadDB().Model(&drugName).Where("drug_name = ? and user_org_id = ? and status =1", drugname, orgid).Find(&drugName).Error
  19. if err == gorm.ErrRecordNotFound {
  20. return nil, err
  21. }
  22. if err != nil {
  23. return nil, err
  24. }
  25. return &drugName, nil
  26. }
  27. func SaveDrugName(drugname *models.XtDrugName) error {
  28. err := XTWriteDB().Create(&drugname).Error
  29. return err
  30. }
  31. func GetDrugNameList(orgid int64) (druname []*models.XtDrugName, err error) {
  32. err = XTReadDB().Model(&druname).Where("user_org_id = ? and status = 1", orgid).Find(&druname).Error
  33. return druname, err
  34. }
  35. func GetRulleName(orgid int64, name string) (baseDrug []*models.XtBaseDrug, err error) {
  36. err = XTReadDB().Model(&baseDrug).Where("org_id = ? and drug_name = ? and status = 1", orgid, name).Find(&baseDrug).Error
  37. return baseDrug, err
  38. }
  39. func GetDrugId(id int64) (models.XtSelfMedical, error) {
  40. medical := models.XtSelfMedical{}
  41. err := XTReadDB().Model(&medical).Where("id = ? and status = 1", id).Find(&medical).Error
  42. return medical, err
  43. }
  44. func GetRulleList(orgid int64, id int64) (rulleName []*models.XtStandName, err error) {
  45. err = XTReadDB().Model(&rulleName).Where("user_org_id = ? and drug_id = ? and status = 1", orgid, id).Find(&rulleName).Error
  46. return rulleName, err
  47. }
  48. func GetUnitByBaseId(id int64) (models.XtBaseDrug, error) {
  49. drug := models.XtBaseDrug{}
  50. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  51. return drug, err
  52. }
  53. func IsExistStandName(drugname string, drugspec string, orgid int64) (*models.XtStandName, error) {
  54. standName := models.XtStandName{}
  55. err := XTReadDB().Model(&standName).Where("drug_name = ? and drug_spec = ? and user_org_id = ? and status = 1", drugname, drugspec, orgid).Find(&standName).Error
  56. if err == gorm.ErrRecordNotFound {
  57. return nil, err
  58. }
  59. if err != nil {
  60. return nil, err
  61. }
  62. return &standName, nil
  63. }
  64. func SaveRulleName(rullName *models.XtStandName) error {
  65. err := XTWriteDB().Model(&rullName).Create(&rullName).Error
  66. return err
  67. }
  68. func GetRullerNameDetail(id int64) (models.XtStandName, error) {
  69. rullerName := models.XtStandName{}
  70. err := XTReadDB().Model(&rullerName).Where("id= ? and status = 1", id).Find(&rullerName).Error
  71. return rullerName, err
  72. }
  73. func GetIsExit(drugname string, drugspec string, id int64) (*models.XtStandName, error) {
  74. standName := models.XtStandName{}
  75. err := XTReadDB().Model(&standName).Where("drug_name = ? and drug_spec = ? and id <> ? and status = 1", drugname, drugspec, id).Find(&standName).Error
  76. if err == gorm.ErrRecordNotFound {
  77. return nil, err
  78. }
  79. if err != nil {
  80. return nil, err
  81. }
  82. return &standName, nil
  83. }
  84. func UpdatedRullerName(id int64, re *models.XtStandName) error {
  85. err := XTWriteDB().Model(&re).Where("id=?", id).Update(map[string]interface{}{"drug_spec": re.DrugSpec, "min_unit": re.MinUnit, "price": re.Price, "drug_stock_limit": re.DrugStockLimit, "drug_name": re.DrugName, "drug_name_id": re.DrugNameId, "drug_id": re.DrugId}).Error
  86. return err
  87. }
  88. //func GetAllDrugNameList(orgid int64) (rullername []*models.XtStandName, err error) {
  89. //
  90. // err = XTReadDB().Group("drug_name").Model(&rullername).Where("user_org_id = ? and status = 1", orgid).Find(&rullername).Error
  91. // return rullername, err
  92. //}
  93. func GetAllDrugNameList(orgid int64) (drugName []*models.XtDrugName, err error) {
  94. err = XTReadDB().Model(&drugName).Where("user_org_id = ? and status = 1", orgid).Find(&drugName).Error
  95. return drugName, err
  96. }
  97. func GetRullerListByDrugName(drugname string, orgid int64) (rullername []*models.XtStandName, err error) {
  98. err = XTReadDB().Model(&rullername).Where("drug_id = ? and user_org_id = ? and status = 1", drugname, orgid).Find(&rullername).Error
  99. return rullername, err
  100. }
  101. func GetBaseDrugMedical(id int64) (models.XtBaseDrug, error) {
  102. drug := models.XtBaseDrug{}
  103. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  104. return drug, err
  105. }
  106. func ModifyDrugWarehouseByInfoId(id int64, stock_max_number int64) error {
  107. info := models.DrugWarehouseInfo{}
  108. err := XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  109. return err
  110. }
  111. func ModifyDrugWarehouseByStockMin(id int64, stock_min_number int64) error {
  112. info := models.DrugWarehouseInfo{}
  113. err := XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  114. return err
  115. }
  116. func GetSelfMedicalByDrugName(drugname string, drug_spec string, patientid int64) (*models.XtSelfMedical, error) {
  117. medical := models.XtSelfMedical{}
  118. err := XTReadDB().Model(&medical).Where("drug_name = ? and drug_spec = ? and patient_id = ? and status = 1", drugname, drug_spec, patientid).Find(&medical).Error
  119. if err == gorm.ErrRecordNotFound {
  120. return nil, err
  121. }
  122. if err != nil {
  123. return nil, err
  124. }
  125. return &medical, err
  126. }
  127. func CreateSelfMedical(medical *models.XtSelfMedical) error {
  128. err = XTWriteDB().Model(&medical).Create(&medical).Error
  129. return err
  130. }
  131. func GetCurrentOrgAllStaff(orgid int64, app int64) (role []*models.SgjUserAdminRole, err error) {
  132. err = UserReadDB().Model(&role).Where("org_id = ? and app_id = ? and status = 1", orgid, app).Find(&role).Error
  133. return role, err
  134. }
  135. func CreateStock(stock *models.XtSelfStock) error {
  136. err := XTWriteDB().Model(&stock).Create(&stock).Error
  137. return err
  138. }
  139. func CreateOutStock(stock *models.XtSelfOutStock) error {
  140. err := XTWriteDB().Model(&stock).Create(&stock).Error
  141. return err
  142. }
  143. func GetStandDrugByDrugName(drugname string, orgid int64) (*models.XtStandName, error) {
  144. standname := models.XtStandName{}
  145. err := XTReadDB().Model(&standname).Where("drug_name = ? and user_org_id = ? and status = 1", drugname, orgid).Find(&standname).Error
  146. if err == gorm.ErrRecordNotFound {
  147. return nil, err
  148. }
  149. if err != nil {
  150. return nil, err
  151. }
  152. return &standname, nil
  153. }
  154. func DeleteDrugName(id int64) (models.XtDrugName, error) {
  155. standName := models.XtDrugName{}
  156. err := XTWriteDB().Model(&standName).Where("id = ?", id).Update(map[string]interface{}{"status": 0}).Error
  157. return standName, err
  158. }
  159. func GetDrugSetByUserOrgId(orgid int64) (*models.XtDrugSet, error) {
  160. set := models.XtDrugSet{}
  161. err := XTReadDB().Model(&set).Where("user_org_id = ? and status = 1", orgid).Find(&set).Error
  162. if err == gorm.ErrRecordNotFound {
  163. return nil, err
  164. }
  165. if err != nil {
  166. return nil, err
  167. }
  168. return &set, nil
  169. }
  170. func SaveRadio(drugSet *models.XtDrugSet) error {
  171. err := XTWriteDB().Model(&drugSet).Create(&drugSet).Error
  172. return err
  173. }
  174. func UpdateDrugSet(set *models.XtDrugSet, orgid int64) error {
  175. err := XTWriteDB().Model(&set).Where("user_org_id = ?", orgid).Update(map[string]interface{}{"drug_start": set.DrugStart}).Error
  176. return err
  177. }
  178. func GetMedicalsByName(orgid int64, drugname string) (*models.XtSelfMedical, error) {
  179. medical := models.XtSelfMedical{}
  180. err := XTReadDB().Model(&medical).Where("user_org_id = ? and drug_name = ? and status = 1", orgid, drugname).Find(&medical).Error
  181. if err == gorm.ErrRecordNotFound {
  182. return nil, err
  183. }
  184. if err != nil {
  185. return nil, err
  186. }
  187. return &medical, nil
  188. }
  189. func DeleteDrugStand(id int64) error {
  190. standName := models.XtStandName{}
  191. err := XTWriteDB().Model(&standName).Where("id =?", id).Update(map[string]interface{}{"status": 0}).Error
  192. return err
  193. }
  194. //func GetStockList(patientid int64, startime int64, keyword string) (stock []*models.SelfStocks, err error) {
  195. // db := XTReadDB().Table("xt_self_stock as x").Where("x.status = 1")
  196. // likeKey := "%" + keyword + "%"
  197. // table := XTReadDB().Table("xt_patients as s")
  198. // fmt.Println(table)
  199. // if patientid > 0 {
  200. // db = db.Where("x.patient_id = ?", patientid)
  201. // }
  202. // if startime > 0 {
  203. // db = db.Where("x.storck_time <= ?", startime)
  204. // }
  205. // err = db.Group("x.drug_name,x.drug_spec").Select("x.id,x.drug_name,x.drug_name_id,x.drug_spec,x.remarks,x.store_number,x.admin_user_id,x.storck_time,x.user_org_id,x.stock_in_number,x.patient_id,x.min_unit,sum(x.store_number) as total").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&stock).Error
  206. // return stock, err
  207. //}
  208. func GetStockList(patientid int64, startime int64, keyword string, orgid int64) (stock []*models.XtSelfMedicals, err error) {
  209. likeKey := "%" + keyword + "%"
  210. db := XTReadDB().Table("xt_self_medical as x").Where("x.status = 1")
  211. if patientid > 0 {
  212. db = db.Where("x.patient_id = ?", patientid)
  213. }
  214. if orgid > 0 {
  215. db = db.Where("x.user_org_id = ?", orgid)
  216. }
  217. err = db.Group("x.drug_name,x.drug_spec").Select("x.id,x.drug_name,x.drug_name_id,x.drug_spec,x.min_unit,sum(t.store_number) as total,t.remarks,x.patient_id,t.stock_in_number,t.storck_time,t.admin_user_id,t.medic_id").Joins("left join xt_self_stock as t on t.medic_id = x.id").Where("t.storck_time<=?", startime).Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&stock).Error
  218. return stock, err
  219. }
  220. func GetOutStockList(patientid int64, startime int64, keyword string, orgid int64) (outStock []*models.SelfOutStock, err error) {
  221. likeKey := "%" + keyword + "%"
  222. db := XTReadDB().Table("xt_self_medical as x").Where("x.status = 1")
  223. if patientid > 0 {
  224. db = db.Where("x.patient_id = ?", patientid)
  225. }
  226. if orgid > 0 {
  227. db = db.Where("x.user_org_id = ?", orgid)
  228. }
  229. err = db.Group("x.drug_name,x.drug_spec").Select("x.id,x.drug_name,x.drug_name_id,x.drug_spec,x.min_unit,sum(t.outstore_number) as count,t.remarks,x.patient_id,t.stock_out_number,t.storck_time,t.admin_user_id,t.medic_id").Joins("left join xt_self_out_stock as t on t.medic_id = x.id").Where("t.storck_time<=?", startime).Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&outStock).Error
  230. return outStock, err
  231. }
  232. func GetTotalOutStockList(patientid int64, startime int64, keyword string, orgid int64) (outStock []*models.SelfOutStock, err error) {
  233. likeKey := "%" + keyword + "%"
  234. db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  235. if patientid > 0 {
  236. db = db.Where("x.patient_id = ?", patientid)
  237. }
  238. if orgid > 0 {
  239. db = db.Where("x.user_org_id = ?", orgid)
  240. }
  241. if startime > 0 {
  242. db = db.Where("x.storck_time<=?", startime)
  243. }
  244. err = db.Select("x.id,x.drug_name,x.drug_name_id,x.drug_spec,x.remarks,x.outstore_number,x.admin_user_id,x.storck_time,x.user_org_id,x.patient_id,x.exit_mode,x.medic_id").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Find(&outStock).Error
  245. return outStock, err
  246. }
  247. //func GetOutStockList(patientid int64, startime int64, keyword string) (outStock []*models.XtSelfOutStocks, err error) {
  248. // db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  249. // likeKey := "%" + keyword + "%"
  250. // table := XTReadDB().Table("xt_patients as s")
  251. // fmt.Println(table)
  252. // if patientid > 0 {
  253. // db = db.Where("x.patient_id = ?", patientid)
  254. // }
  255. // if startime > 0 {
  256. // db = db.Where("x.storck_time <= ?", startime)
  257. // }
  258. // err = db.Group("x.drug_name,x.drug_spec").Select("x.drug_name,x.drug_spec,x.patient_id,x.stock_out_number,sum(x.outstore_number) as count").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&outStock).Error
  259. // return outStock, err
  260. //}
  261. func GetMedicalList(patientid int64, orgid int64, keyword string) (medical []*models.XtSelfMedicals, err error) {
  262. likeKey := "%" + keyword + "%"
  263. db := XTReadDB().Table("xt_self_medical as x").Where("x.status = 1")
  264. if patientid > 0 {
  265. db = db.Where("x.patient_id = ?", patientid)
  266. }
  267. if orgid > 0 {
  268. db = db.Where("x.user_org_id = ?", orgid)
  269. }
  270. err = db.Select("x.drug_name,x.id,x.drug_spec,x.patient_id,x.min_unit").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Group("x.drug_name,x.drug_spec").Scan(&medical).Error
  271. //err = XTReadDB().Model(&medical).Where("patient_id = ? and user_org_id = ? and status = 1", patientid, orgid).Group("drug_name,drug_spec").Find(&medical).Error
  272. return medical, err
  273. }
  274. func GetStockOutDetail(durgname string, patientid int64) (stock []*models.XtSelfOutStock, err error) {
  275. err = XTReadDB().Model(&stock).Where("drug_name = ? and patient_id = ? and status = 1", durgname, patientid).Find(&stock).Error
  276. return stock, err
  277. }
  278. func DeleteDrugStockNumber(drugname string, patientid int64) (models.XtSelfStock, error) {
  279. stock := models.XtSelfStock{}
  280. err := XTWriteDB().Model(&stock).Where("drug_name = ? and patient_id = ?", drugname, patientid).Update(map[string]interface{}{"status": 0}).Error
  281. return stock, err
  282. }
  283. func GetStockDetail(drugname string, drugspec string, startime int64, endtime int64, patientid int64, orgid int64) (stock []*models.XtSelfStock, err error) {
  284. err = XTReadDB().Model(&stock).Where("drug_name = ? and drug_spec = ? and patient_id =? and user_org_id = ? and status = 1 and storck_time>=? and storck_time<=?", drugname, drugspec, patientid, orgid, startime, endtime).Order("storck_time asc").Find(&stock).Error
  285. return stock, err
  286. }
  287. func GetOutStockDetail(drugname string, drugspec string, startime int64, endtime int64, patientid int64, orgid int64) (outStock []*models.XtSelfOutStock, err error) {
  288. err = XTReadDB().Model(&outStock).Where("drug_name = ? and drug_spec=? and patient_id = ? and user_org_id = ? and status = 1 and storck_time>=? and storck_time<=?", drugname, drugspec, patientid, orgid, startime, endtime).Order("storck_time asc").Find(&outStock).Error
  289. return outStock, err
  290. }
  291. func GetAllPatientStockList(drug_name string, drug_spec string, startime int64, endtime int64, keyword string, orgid int64) (stocks []*models.SelfStocks, err error) {
  292. db := XTReadDB().Table("xt_self_stock as x").Where("x.status = 1")
  293. likeKey := "%" + keyword + "%"
  294. table := XTReadDB().Table("xt_patients as s")
  295. fmt.Println(table)
  296. if len(drug_name) > 0 {
  297. db = db.Where("x.drug_name = ?", drug_name)
  298. }
  299. if len(drug_spec) > 0 {
  300. db = db.Where("x.drug_spec = ?", drug_spec)
  301. }
  302. //if startime > 0 {
  303. // db = db.Where("x.storck_time >= ?", startime)
  304. //}
  305. //if endtime > 0 {
  306. // db = db.Where("x.storck_time <= ?", endtime)
  307. //}
  308. if orgid > 0 {
  309. db = db.Where("x.user_org_id = ?", orgid)
  310. }
  311. err = db.Group("x.patient_id,x.drug_name,x.drug_spec").Select("t.drug_name,t.drug_spec,x.patient_id,x.store_number,sum(x.store_number) as total,s.name,s.dialysis_no,x.min_unit,x.drug_name_id").Joins("left join xt_patients as s on s.id = x.patient_id").Joins("left join xt_self_medical as t on t.id = x.medic_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&stocks).Error
  312. return stocks, err
  313. }
  314. func GetAllPatientOutStockList(drug_name string, drug_spec string, startime int64, endtime int64, keyword string, orgid int64) (outStock []*models.XtSelfOutStocks, err error) {
  315. db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  316. likeKey := "%" + keyword + "%"
  317. table := XTReadDB().Table("xt_patients as s")
  318. fmt.Println(table)
  319. if len(drug_name) > 0 {
  320. db = db.Where("x.drug_name = ?", drug_name)
  321. }
  322. if len(drug_spec) > 0 {
  323. db = db.Where("x.drug_spec=?", drug_spec)
  324. }
  325. if startime > 0 {
  326. db = db.Where("x.storck_time >= ?", startime)
  327. }
  328. if endtime > 0 {
  329. db = db.Where("x.storck_time<=?", endtime)
  330. }
  331. if orgid > 0 {
  332. db = db.Where("x.user_org_id = ?", orgid)
  333. }
  334. err = db.Group("x.patient_id,x.drug_name,x.drug_spec").Select("x.drug_name,x.drug_spec,x.patient_id,x.stock_out_number,sum(x.outstore_number) as count,s.name,s.dialysis_no,x.drug_name_id").Joins("left join xt_patients as s on s.id = x.patient_id").Where("s.name like ? or s.dialysis_no like ?", likeKey, likeKey).Scan(&outStock).Error
  335. return outStock, err
  336. }
  337. func GetDrugDataByPatientId(patientid int64) (selfMedical []*models.XtSelfMedical, err error) {
  338. err = XTReadDB().Group("drug_name,drug_spec").Model(&selfMedical).Where("patient_id = ? and status = 1", patientid).Find(&selfMedical).Error
  339. return selfMedical, err
  340. }
  341. func GetDruckStockConfig(orgid int64) (models.XtDrugStockConfig, error) {
  342. drugConfig := models.XtDrugStockConfig{}
  343. err := XTReadDB().Model(&drugConfig).Where("org_id = ? and status = 1", orgid).Find(&drugConfig).Error
  344. return drugConfig, err
  345. }
  346. func GetSetSelfMedical(orgid int64) (models.XtDrugSet, error) {
  347. medical := models.XtDrugSet{}
  348. err := XTReadDB().Model(&medical).Where("user_org_id = ? and status = 1", orgid).Find(&medical).Error
  349. return medical, err
  350. }
  351. func GetSelfMedicalList(patientid int64) (selfmedical []*models.XtSelfMedical, err error) {
  352. err = XTReadDB().Group("drug_name").Model(&selfmedical).Where("patient_id = ? and status = 1", patientid).Find(&selfmedical).Error
  353. return selfmedical, err
  354. }
  355. func GetDrugDescByDrugName(drugname string, patient_id int64, orgid int64) (selfMedical []*models.XtSelfMedical, err error) {
  356. err = XTReadDB().Model(&selfMedical).Where("drug_name = ? and patient_id = ? and user_org_id = ? and status = 1", drugname, patient_id, orgid).Find(&selfMedical).Error
  357. return selfMedical, err
  358. }
  359. func GetBaseMedcal(drugname string, orgid int64) (baseDrug []*models.XtBaseDrug, err error) {
  360. err = XTReadDB().Model(&baseDrug).Where("drug_name = ? and org_id = ? and status = 1", drugname, orgid).Find(&baseDrug).Error
  361. return baseDrug, err
  362. }
  363. func GetTotalBaseMedicalCount(drug_id int64, orgid int64) (drugInfo []*models.DrugWarehousingInfo, err error) {
  364. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  365. err = db.Select("x.drug_id,sum(x.warehousing_count) as count").Where("drug_id = ? and org_id = ?", drug_id, orgid).Scan(&drugInfo).Error
  366. return drugInfo, err
  367. }
  368. func GetTotalBaseMedicalCountOut(drug_id int64, orgid int64) (drugOut []*models.DrugWarehousingInfo, err error) {
  369. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  370. err = db.Select("x.drug_id,sum(x.count) as count").Where("drug_id = ? and org_id = ?", drug_id, orgid).Scan(&drugOut).Error
  371. return drugOut, err
  372. }
  373. func GetDrugWareseOut(startime int64, orgid int64) (*models.XtDrugWarehouse, error) {
  374. warehouse := models.XtDrugWarehouse{}
  375. err := XTReadDB().Model(&warehouse).Where("warehousing_time = ? and org_id = ? and status = 1", startime, orgid).Find(&warehouse).Error
  376. if err == gorm.ErrRecordNotFound {
  377. return nil, err
  378. }
  379. if err != nil {
  380. return nil, err
  381. }
  382. return &warehouse, nil
  383. }
  384. func GetManufacturer(id int64) (models.XtBaseDrug, error) {
  385. drug := models.XtBaseDrug{}
  386. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", id).Find(&drug).Error
  387. return drug, err
  388. }
  389. func CreateDrugWarehoue(warehouse *models.XtDrugWarehouse) error {
  390. err := XTWriteDB().Model(&warehouse).Create(&warehouse).Error
  391. return err
  392. }
  393. func GetTodayWareHousringOrder(startime int64, orgid int64) (models.XtDrugWarehouseOut, error) {
  394. warehouse := models.XtDrugWarehouseOut{}
  395. err := XTReadDB().Model(&warehouse).Where("warehouse_out_time = ? and org_id = ? and status = 1", startime, orgid).Find(&warehouse).Error
  396. return warehouse, err
  397. }
  398. func CreateDrugWareHouseOut(out *models.XtDrugWarehouseOut) error {
  399. err := XTWriteDB().Model(&out).Create(&out).Error
  400. return err
  401. }
  402. func CreateWareHouseOutInfo(info *models.XtDrugWarehouseOutInfo) error {
  403. err := XTWriteDB().Model(&info).Create(&info).Error
  404. return err
  405. }
  406. func CreateAotoMaticReduceDetail(detail *models.XtDrugAutomaticReduceDetail) error {
  407. err := XTWriteDB().Model(&detail).Create(&detail).Error
  408. return err
  409. }
  410. func GetDrugStockConfig(orgid int64) (models.XtDrugStockConfig, error) {
  411. config := models.XtDrugStockConfig{}
  412. err := XTReadDB().Model(&config).Where("org_id =? and status = 1", orgid).Find(&config).Error
  413. return config, err
  414. }
  415. func GetDrugSet(orgid int64) (models.XtDrugSet, error) {
  416. drugSet := models.XtDrugSet{}
  417. err := XTReadDB().Model(&drugSet).Where("user_org_id =? and status = 1", orgid).Find(&drugSet).Error
  418. return drugSet, err
  419. }
  420. type Drugs struct {
  421. ID int64 `gorm:"column:id" json:"id" form:"id"`
  422. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  423. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  424. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  425. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  426. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  427. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  428. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  429. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  430. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  431. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  432. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  433. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  434. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  435. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  436. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  437. Status int64 `gorm:"column:status" json:"status" form:"status"`
  438. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  439. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  440. Drugs []*Drugs `gorm:"ForeignKey:DrugName;AssociationForeignKey:DrugName" json:"drug_specs"`
  441. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  442. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  443. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  444. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  445. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  446. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  447. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  448. DrugDoseUnit int64 `grom:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  449. }
  450. func (Drugs) TableName() string {
  451. return "xt_base_drug"
  452. }
  453. func GetAllBaseDrugLibList(org_id int64) (drugs []*Drugs, err error) {
  454. db := readDb.Model(&models.BaseDrugLib{}).Preload("Drugs", "status = 1 AND org_id = ? AND find_in_set('停用',drug_status) = 0", org_id).Where("org_id = ? AND status = 1 AND find_in_set('停用',drug_status) = 0", org_id).Group("drug_name")
  455. err = db.Find(&drugs).Error
  456. return
  457. }
  458. type PrivateDrug struct {
  459. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  460. ID int64 `gorm:"column:id" json:"id" form:"id"`
  461. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  462. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  463. DrugNameId int64 `gorm:"column:drug_name_id" json:"drug_name_id" form:"drug_name_id"`
  464. Status int64 `gorm:"column:status" json:"status" form:"status"`
  465. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  466. CreatedTime int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
  467. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time" form:"updated_time"`
  468. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  469. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  470. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  471. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  472. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  473. PrivateDrug []*PrivateDrug `gorm:"ForeignKey:DrugName;AssociationForeignKey:DrugName" json:"drug_specs"`
  474. }
  475. func (PrivateDrug) TableName() string {
  476. return "xt_self_medical"
  477. }
  478. func GetPrivateDrugList(patient_id int64, org_id int64) (pds []*PrivateDrug, err error) {
  479. err = XTReadDB().Model(&PrivateDrug{}).Preload("PrivateDrug", "status = 1 AND user_org_id = ? AND patient_id = ? ", org_id, patient_id).Where("patient_id = ? and status = 1 AND user_org_id = ?", patient_id, org_id).Group("drug_name_id").Find(&pds).Error
  480. return
  481. }
  482. func GetAllMedicalList(orgid int64) (list []*models.XtSelfMedical, err error) {
  483. err = XTReadDB().Model(&list).Where("user_org_id = ? and status = 1", orgid).Group("drug_name").Find(&list).Error
  484. return list, err
  485. }
  486. func DeleteDrugNameById(id int64) (models.XtSelfMedical, error) {
  487. medical := models.XtSelfMedical{}
  488. err := XTWriteDB().Model(&medical).Where("id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  489. return medical, err
  490. }
  491. func GetDrugNameIsExist(drugid int64, orgid int64) (*models.XtDrugName, error) {
  492. drugName := models.XtDrugName{}
  493. err := XTReadDB().Model(&drugName).Where("drug_id = ? and user_org_id = ? and status = 1", drugid, orgid).Find(&drugName).Error
  494. if err == gorm.ErrRecordNotFound {
  495. return nil, err
  496. }
  497. if err != nil {
  498. return nil, err
  499. }
  500. return &drugName, nil
  501. }
  502. func UpdatedDrugName(id int64, orgid int64, drugName *models.XtDrugName) error {
  503. err := XTWriteDB().Model(&drugName).Where("drug_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": drugName.DrugName}).Error
  504. return err
  505. }
  506. func GetStandNameIsExist(id int64, orgid int64) (*models.XtStandName, error) {
  507. standName := models.XtStandName{}
  508. err := XTReadDB().Model(&standName).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&standName).Error
  509. if err == gorm.ErrRecordNotFound {
  510. return nil, err
  511. }
  512. if err != nil {
  513. return nil, err
  514. }
  515. return &standName, nil
  516. }
  517. func UpdateStandName(id int64, orgid int64, standName *models.XtStandName) error {
  518. err := XTWriteDB().Model(&standName).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": standName.DrugName, "drug_spec": standName.DrugSpec, "drug_stock_limit": standName.DrugStockLimit, "min_unit": standName.MinUnit}).Error
  519. return err
  520. }
  521. func GetSelfMedicalIsExist(id int64, orgid int64) (*models.XtSelfMedical, error) {
  522. medical := models.XtSelfMedical{}
  523. err := XTReadDB().Model(&medical).Where("drug_name_id = ? and user_org_id = ? and status =1", id, orgid).Find(&medical).Error
  524. if err == gorm.ErrRecordNotFound {
  525. return nil, err
  526. }
  527. if err != nil {
  528. return nil, err
  529. }
  530. return &medical, nil
  531. }
  532. func UpdateSelfMedical(id int64, orgid int64, medical *models.XtSelfMedical) error {
  533. err := XTWriteDB().Model(&medical).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": medical.DrugName, "drug_spec": medical.DrugSpec, "execution_frequency": medical.ExecutionFrequency, "prescribing_number": medical.PrescribingNumber, "delivery_way": medical.DeliveryWay, "single_dose": medical.SingleDose, "min_unit": medical.MinUnit}).Error
  534. return err
  535. }
  536. func GetDrugNameId(id int64, orgid int64) (models.XtSelfMedical, error) {
  537. medical := models.XtSelfMedical{}
  538. err := XTReadDB().Model(&medical).Where("drug_name_id = ? and status = 1 and user_org_id = ?", id, orgid).Find(&medical).Error
  539. return medical, err
  540. }
  541. func GetSelfStosckIsExist(id int64, orgid int64) (*models.XtSelfStock, error) {
  542. medical := models.XtSelfStock{}
  543. err := XTReadDB().Model(&medical).Where("drug_name_id = ? and user_org_id = ? and status =1", id, orgid).Find(&medical).Error
  544. if err == gorm.ErrRecordNotFound {
  545. return nil, err
  546. }
  547. if err != nil {
  548. return nil, err
  549. }
  550. return &medical, nil
  551. }
  552. func UpdatedSelfStock(stock *models.XtSelfStock, id int64, orgid int64) error {
  553. err := XTWriteDB().Model(&stock).Where("drug_name_id=? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": stock.DrugName, "min_unit": stock.MinUnit, "drug_spec": stock.DrugSpec}).Error
  554. return err
  555. }
  556. func GetOutSelfOutStockIsExist(id int64, orgid int64) (*models.XtSelfOutStock, error) {
  557. outStock := models.XtSelfOutStock{}
  558. err := XTReadDB().Model(&outStock).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&outStock).Error
  559. if err == gorm.ErrRecordNotFound {
  560. return nil, err
  561. }
  562. if err != nil {
  563. return nil, err
  564. }
  565. return &outStock, nil
  566. }
  567. func UpdatedOutSelfStock(stock *models.XtSelfOutStock, id int64, orgid int64) error {
  568. err := XTWriteDB().Model(&stock).Where("drug_name_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_name": stock.DrugName, "drug_spec": stock.DrugSpec}).Error
  569. return err
  570. }
  571. func GetDoctorAdviceIsExist(id int64, orgid int64) (*models.DoctorAdvice, error) {
  572. advice := models.DoctorAdvice{}
  573. err := XTReadDB().Model(&advice).Where("drug_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&advice).Error
  574. if err == gorm.ErrRecordNotFound {
  575. return nil, err
  576. }
  577. if err != nil {
  578. return nil, err
  579. }
  580. return &advice, nil
  581. }
  582. func UpdateNoExcuteDoctorAdvice(id int64, orgid int64, advice *models.DoctorAdvice) error {
  583. err := XTWriteDB().Model(&advice).Where("drug_id = ? and user_org_id = ? and status = 1 and execution_state = 2", id, orgid).Updates(map[string]interface{}{"advice_name": advice.AdviceName, "advice_desc": advice.AdviceDesc, "single_dose": advice.SingleDose, "single_dose_unit": advice.SingleDoseUnit, "delivery_way": advice.DeliveryWay, "execution_frequency": advice.ExecutionFrequency}).Error
  584. return err
  585. }
  586. func GetDoctorAdviceTemplateIsExist(id int64, orgid int64) (*models.DoctorAdviceTemplate, error) {
  587. template := models.DoctorAdviceTemplate{}
  588. err := XTReadDB().Model(&template).Where("drug_id = ? and org_id = ? and status = 1", id, orgid).Find(&template).Error
  589. if err == gorm.ErrRecordNotFound {
  590. return nil, err
  591. }
  592. if err != nil {
  593. return nil, err
  594. }
  595. return &template, nil
  596. }
  597. func UpdatedDoctorAdviceTemplate(id int64, orgid int64, template *models.DoctorAdviceTemplate) error {
  598. err := XTWriteDB().Model(&template).Where("drug_id = ? and org_id = ? and status = 1", id, orgid).Updates(map[string]interface{}{"advice_name": template.AdviceName, "advice_desc": template.AdviceDesc, "single_dose": template.SingleDose, "single_dose_unit": template.SingleDoseUnit, "delivery_way": template.DeliveryWay, "execution_frequency": template.ExecutionFrequency}).Error
  599. return err
  600. }
  601. func ToSeachPatient(keyword string, orgid int64) (patients []*models.Patients, err error) {
  602. likeKey := "%" + keyword + "%"
  603. err = XTReadDB().Model(&patients).Where("name like ? and user_org_id = ? and status = 1", likeKey, orgid).Find(&patients).Error
  604. return patients, err
  605. }
  606. func GetMedicalDrugNameList(orgid int64) (drugName []*models.DrugName, err error) {
  607. db := XTReadDB().Model(&drugName).Where("user_org_id = ? and status = 1", orgid)
  608. err = db.Preload("XtStandName", "status =1 and user_org_id = ?", orgid).Find(&drugName).Error
  609. return drugName, err
  610. }
  611. func GetSelfOutStock(orgid int64, advicedate int64, patientid int64, adivicename string, advicedesc string) (*models.XtSelfOutStock, error) {
  612. stock := models.XtSelfOutStock{}
  613. err = XTReadDB().Model(&stock).Where("user_org_id = ? and storck_time = ? and patient_id = ? and drug_name = ? and drug_spec = ? and status = 1", orgid, advicedate, patientid, adivicename, advicedesc).Find(&stock).Error
  614. if err == gorm.ErrRecordNotFound {
  615. return nil, err
  616. }
  617. if err != nil {
  618. return nil, err
  619. }
  620. return &stock, err
  621. }
  622. func UpdatedSelfOutSatock(id int64, stock *models.XtSelfOutStock) error {
  623. err := XTWriteDB().Model(&stock).Where("id = ?", id).Updates(map[string]interface{}{"drug_name": stock.DrugName, "drug_name_id": stock.DrugNameId, "drug_spec": stock.DrugSpec, "outstore_number": stock.OutstoreNumber, "admin_user_id": stock.AdminUserId, "storck_time": stock.StorckTime, "patient_id": stock.PatientId, "stock_out_number": stock.StockOutNumber, "exit_mode": stock.ExitMode, "medic_id": stock.MedicId, "updated_time": time.Now().Unix()}).Error
  624. return err
  625. }
  626. func GetSelfStockQuery(typename int64, keywords string, startime int64, endtime int64, orgid int64, limit int64, page int64) (selfoutstock []*models.XtSelfOutStockOne, total int64, err error) {
  627. offset := (page - 1) * limit
  628. db := XTReadDB().Table("xt_self_stock as x").Where("x.status = 1")
  629. likeKey := "%" + keywords + "%"
  630. if len(keywords) > 0 {
  631. db = db.Where("x.drug_name like ? ", likeKey)
  632. }
  633. if startime > 0 {
  634. db = db.Where("x.storck_time >=?", startime)
  635. }
  636. if endtime > 0 {
  637. db = db.Where("x.storck_time<=?", endtime)
  638. }
  639. if orgid > 0 {
  640. db = db.Where("x.user_org_id = ?", orgid)
  641. }
  642. if typename > 0 {
  643. err = db.Select("x.drug_name,x.drug_name_id,x.drug_spec,Sum(store_number) as count,b.drug_type").Joins("left join xt_base_drug as b on b.id = x.drug_name_id and b.org_id = ? and b.status =1 ", orgid).Where("b.drug_type = ?", typename).Group("x.drug_name_id").Count(&total).Offset(offset).Limit(limit).Scan(&selfoutstock).Error
  644. } else {
  645. err = db.Select("x.drug_name,x.drug_name_id,x.drug_spec,Sum(store_number) as count,b.drug_type").Joins("left join xt_base_drug as b on b.id = x.drug_name_id and b.org_id = ? and b.status =1", orgid).Group("x.drug_name_id").Count(&total).Offset(offset).Limit(limit).Scan(&selfoutstock).Error
  646. }
  647. return selfoutstock, total, err
  648. }
  649. func GetSelfOutStockQuery(startTime int64, endTime int64, orgid int64) (selfoutstock []*models.XtSelfOutStockOne, err error) {
  650. db := XTReadDB().Table("xt_self_out_stock as x").Where("x.status = 1")
  651. if startTime > 0 {
  652. db = db.Where("x.storck_time >?", startTime)
  653. }
  654. if endTime > 0 {
  655. db = db.Where("x.storck_time<=?", endTime)
  656. }
  657. if orgid > 0 {
  658. db = db.Where("x.user_org_id = ?", orgid)
  659. }
  660. err = db.Select("x.drug_name_id,Sum(x.outstore_number) as count").Group("x.drug_name").Scan(&selfoutstock).Error
  661. return selfoutstock, err
  662. }
  663. func GetLastDrugWarehouseOutInfo(drugid int64, warehouseOutId int64, orgid int64) (models.XtDrugWarehouseOutInfo, error) {
  664. info := models.XtDrugWarehouseOutInfo{}
  665. err = XTReadDB().Model(&info).Where("drug_id = ? and warehouse_out_id = ? and org_id = ? and status = 1", drugid, warehouseOutId, orgid).Last(&info).Error
  666. return info, err
  667. }
  668. func PostSearchDrugWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodDrugWarehouseInfo, err error) {
  669. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 and (x.stock_max_number>0 or x.stock_min_number>0)")
  670. table := XTReadDB().Table("xt_base_drug as t").Where("t.status")
  671. fmt.Println(table)
  672. likeKey := "%" + keyword + "%"
  673. if orgid > 0 {
  674. db = db.Where("x.org_id = ?", orgid)
  675. }
  676. if storehouse_id > 0 {
  677. db = db.Where("x.storehouse_id = ?", storehouse_id)
  678. }
  679. err = db.Group("x.drug_id").Select("x.id,x.warehousing_id,x.drug_id,t.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.total_price,x.dealer,x.remark,x.warehousing_order,x.warehouseing_unit,x.stock_max_number,x.stock_min_number,x.batch_number,x.storehouse_id,t.drug_origin_place,t.drug_name,t.min_unit,x.manufacturer,t.retail_price,t.max_unit,t.min_number,t.dose,t.dose_unit,t.last_price,t.drug_origin_place").Joins("left join xt_base_drug as t on t.id = x.drug_id").Where("t.drug_name like ? and t.org_id = ?", likeKey, orgid).Order("x.ctime desc").Scan(&info).Error
  680. return info, err
  681. }
  682. func CreateDrugPrice(price models.XtDrugAdjustPrice) error {
  683. err := XTWriteDB().Create(&price).Error
  684. return err
  685. }
  686. func GetDrugPriceList(startime int64, endtime int64, orgId int64, keyword string, limit int64, page int64) (adjust []*models.VmDrugAdjustPrice, total int64, err error) {
  687. likeKey := "%" + keyword + "%"
  688. offset := (page - 1) * limit
  689. db := XTReadDB().Table("xt_drug_adjust_price as x").Where("x.status = 1")
  690. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  691. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  692. fmt.Println(table, tab)
  693. if len(keyword) > 0 {
  694. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  695. }
  696. if orgId > 0 {
  697. db = db.Where("x.user_org_id = ?", orgId)
  698. }
  699. if startime > 0 {
  700. db = db.Where("x.ctime>=?", startime)
  701. }
  702. if endtime > 0 {
  703. db = db.Where("x.ctime<=?", endtime)
  704. }
  705. err = db.Group("x.id").Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.number").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  706. return adjust, total, err
  707. }
  708. func UpdateDrugAdjuestPrice(ids []string, info models.XtDrugAdjustPrice) error {
  709. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  710. return err
  711. }
  712. func GetDrugAdjuestPrice(ids []string) (info []*models.XtDrugAdjustPrice, err error) {
  713. err = XTReadDB().Model(&info).Where("id in(?) and status = 1", ids).Find(&info).Error
  714. return info, err
  715. }
  716. func GetDrugDamage(ids []string) (info []*models.XtDrugDamage, err error) {
  717. err = XTReadDB().Model(&info).Where("id in(?) and status = 1", ids).Find(&info).Error
  718. return info, err
  719. }
  720. func CreateDrugDamage(damage models.XtDrugDamage) error {
  721. err := XTWriteDB().Create(&damage).Error
  722. return err
  723. }
  724. func GetDrugDamageList(startime int64, endtime int64, orgId int64, keyword string, limit int64, page int64, storehouse_id int64) (adjust []*models.VmDrugInventory, total int64, err error) {
  725. likeKey := "%" + keyword + "%"
  726. offset := (page - 1) * limit
  727. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1 and type = 4")
  728. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  729. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  730. fmt.Println(table, tab)
  731. //if len(keyword) > 0 {
  732. // db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  733. //}
  734. if len(keyword) > 0 {
  735. db = db.Where("x.drug_name like ?", likeKey)
  736. }
  737. if orgId > 0 {
  738. db = db.Where("x.user_org_id = ?", orgId)
  739. }
  740. if startime > 0 {
  741. db = db.Where("x.ctime >=?", startime)
  742. }
  743. if endtime > 0 {
  744. db = db.Where("x.ctime <=?", endtime)
  745. }
  746. if storehouse_id > 0 {
  747. db = db.Where("x.storehouse_id = ?", storehouse_id)
  748. }
  749. err = db.Group("x.drug_id").Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.number,x.drug_origin_place,x.warehouse_info_id,x.batch_number,x.stock_max_number,x.stock_min_number,x.storehouse_id,t.min_number,t.max_unit,t.min_unit,t.min_price").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  750. return adjust, total, err
  751. }
  752. func GetDrugDamageByOrgId(orgid int64) (list []*models.XtDrugInventory, err error) {
  753. err = XTReadDB().Model(&list).Where("user_org_id = ? and type = 4 and status = 1", orgid).Find(&list).Error
  754. return list, err
  755. }
  756. func UpdateDrugDamage(ids []string, damage models.XtDrugDamage) error {
  757. err := XTWriteDB().Model(&damage).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": damage.Checker, "checker_status": damage.CheckerStatus, "checker_time": damage.CheckerTime}).Error
  758. return err
  759. }
  760. func GetDrugModifyPrice(id int64) (models.XtDrugAdjustPrice, error) {
  761. adjuest := models.XtDrugAdjustPrice{}
  762. err := XTReadDB().Model(&adjuest).Where("id=? and status = 1", id).Find(&adjuest).Error
  763. return adjuest, err
  764. }
  765. func ModifyDrugPrice(id int64, adjust models.XtDrugAdjustPrice) error {
  766. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"drug_name": adjust.DrugName, "retail_price": adjust.RetailPrice, "new_price": adjust.NewPrice, "count": adjust.Count, "remark": adjust.Remark, "specification_name": adjust.SpecificationName, "warehousing_unit": adjust.WarehousingUnit, "manufacturer": adjust.Manufacturer, "number": adjust.Number, "drug_id": adjust.DrugId}).Error
  767. return err
  768. }
  769. func GetModifyPricePrint(ids []string) (info []*models.XtDrugAdjustPrice, err error) {
  770. err = XTReadDB().Model(&info).Where("id in(?) and status = 1", ids).Find(&info).Error
  771. return info, err
  772. }
  773. func GetDrugDamageDetail(id int64) (models.XtDrugDamage, error) {
  774. damage := models.XtDrugDamage{}
  775. err := XTReadDB().Model(&damage).Where("id=? and status = 1", id).Find(&damage).Error
  776. return damage, err
  777. }
  778. func ModifyDrugDamage(id int64, damage models.XtDrugDamage) error {
  779. err := XTWriteDB().Model(&damage).Where("id=? and status = 1", id).Updates(map[string]interface{}{"drug_name": damage.DrugName, "warehousing_unit": damage.WarehousingUnit, "count": damage.Count, "last_price": damage.LastPrice, "retail_price": damage.RetailPrice, "new_price": damage.NewPrice, "manufacturer": damage.Manufacturer, "remark": damage.Remark, "number": damage.Number, "drug_id": damage.DrugId, "stock_max_number": damage.StockMaxNumber, "stock_min_number": damage.StockMinNumber, "warehousing_info_id": damage.WarehousingInfoId, "drug_origin_place": damage.DrugOriginPlace}).Error
  780. return err
  781. }
  782. func DeleteDrugDamage(id int64) error {
  783. damage := models.XtDrugDamage{}
  784. err := XTWriteDB().Model(&damage).Where("id=? and status = 1").Updates(map[string]interface{}{"status": 0}).Error
  785. return err
  786. }
  787. func GetDrugDamagePrint(ids []string) (damage []*models.XtDrugDamage, err error) {
  788. err = XTReadDB().Model(&damage).Where("id in(?) and status = 1", ids).Find(&damage).Error
  789. return damage, err
  790. }
  791. func GetWarehoseInfoById(id int64, storehouse_id int64, orgid int64) (info []*models.XtDrugWarehouseInfo, err error) {
  792. if orgid == 10191 {
  793. err = XTReadDB().Model(&info).Where("drug_id=? and status = 1 and storehouse_id = ? and is_check = 1", id, storehouse_id).Preload("XtBaseDrug", "status= 1").Find(&info).Error
  794. return info, err
  795. } else {
  796. err = XTReadDB().Model(&info).Where("drug_id=? and status = 1 and storehouse_id = ? and is_check = 1 and (stock_max_number>0 or stock_min_number>0)", id, storehouse_id).Preload("XtBaseDrug", "status= 1").Find(&info).Error
  797. return info, err
  798. }
  799. }
  800. func CreateDrugInventory(inventory models.XtDrugInventory) error {
  801. err := XTWriteDB().Create(&inventory).Error
  802. return err
  803. }
  804. func GetDrugInventoryList(keyword string, page int64, limit int64, orgid int64, startime int64, endtime int64) (list []*models.VmDrugInventory, total int64, err error) {
  805. fmt.Println("startdate", limit, page)
  806. likeKey := "%" + keyword + "%"
  807. offset := (page - 1) * limit
  808. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1")
  809. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  810. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  811. fmt.Println(table, tab)
  812. if len(keyword) > 0 {
  813. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  814. }
  815. if orgid > 0 {
  816. db = db.Where("x.user_org_id = ?", orgid)
  817. }
  818. if startime > 0 {
  819. db = db.Where("x.start_time >=?", startime)
  820. }
  821. if endtime > 0 {
  822. db = db.Where("x.start_time<=?", endtime)
  823. }
  824. err = db.Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.number,x.batch_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.min_count,x.min_unit,t.dose,t.dose_unit,t.max_unit,t.min_unit").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Order("x.id desc").Offset(offset).Limit(limit).Order("x.ctime desc").Scan(&list).Error
  825. return list, total, err
  826. }
  827. func UpdateDrugInventory(ids []string, inventory models.XtDrugInventory) error {
  828. err := XTWriteDB().Model(&inventory).Where("id in(?) and status = 1", ids).Updates(map[string]interface{}{"checker": inventory.Checker, "checker_status": inventory.CheckerStatus, "checker_time": inventory.CheckerTime}).Error
  829. return err
  830. }
  831. func GetDrugInventoryListByIds(ids []string) (inventory []*models.XtDrugInventory, err error) {
  832. err = XTReadDB().Model(&inventory).Where("id in(?) and status = 1", ids).Find(&inventory).Error
  833. return inventory, err
  834. }
  835. func GetDrugDetailByDrugId(drug_id int64) (models.XtBaseDrug, error) {
  836. drug := models.XtBaseDrug{}
  837. err := XTReadDB().Model(&drug).Where("id = ? and status = 1", drug_id).Find(&drug).Error
  838. return drug, err
  839. }
  840. func GetDrugWareInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  841. info := models.XtDrugWarehouseInfo{}
  842. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  843. return info, err
  844. }
  845. func GetDrugWareInfoByIdTwo(id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  846. err = XTReadDB().Model(&info).Where("(id = ? or warehousing_info_id = ?) and status = 1", id, id).Find(&info).Error
  847. return info, err
  848. }
  849. func GetLastDrugWarehouse(orgid int64) (models.DrugWarehouse, error) {
  850. info := models.DrugWarehouse{}
  851. err := XTReadDB().Model(&info).Where("org_id=? and status = 1", orgid).Last(&info).Error
  852. return info, err
  853. }
  854. func GetLastDrugWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  855. info := models.DrugWarehouseOut{}
  856. err := XTReadDB().Model(&info).Where("org_id=? and status = 1", orgid).Last(&info).Error
  857. return info, err
  858. }
  859. func GetLastDrugWarehouseInfSix(drugid int64) (models.XtDrugWarehouseInfo, error) {
  860. info := models.XtDrugWarehouseInfo{}
  861. err := XTReadDB().Model(&info).Where("drug_id=? and status = 1", drugid).Last(&info).Error
  862. return info, err
  863. }
  864. func GetFirstDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  865. info := models.XtDrugWarehouseInfo{}
  866. err := XTReadDB().Model(&info).Where("id=? and status = 1", drugid).First(&info).Error
  867. return info, err
  868. }
  869. func GetDrugInventoryDetail(id int64) (models.XtDrugInventory, error) {
  870. inventory := models.XtDrugInventory{}
  871. err := XTReadDB().Model(&inventory).Where("id = ? and status = 1", id).Find(&inventory).Error
  872. return inventory, err
  873. }
  874. func ModifyDrugInventory(id int64, info models.XtDrugInventory) error {
  875. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"drug_name": info.DrugName, "specification_name": info.SpecificationName, "warehousing_unit": info.WarehousingUnit, "count": info.Count, "last_price": info.LastPrice, "manufacturer": info.Manufacturer, "remark": info.Remark, "drug_id": info.DrugId, "warehousing_order": info.WarehousingOrder, "number": info.Number, "total": info.Number, "drug_origin_place": info.DrugOriginPlace, "stock_max_number": info.StockMaxNumber, "stock_min_number": info.StockMinNumber, "warehouse_info_id": info.WarehouseInfoId, "min_count": info.MinCount, "min_unit": info.MinUnit}).Error
  876. return err
  877. }
  878. func DeleteDrugInventory(id int64) error {
  879. inventory := models.XtDrugInventory{}
  880. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  881. return err
  882. }
  883. func GetDrugInventoryPrintList(ids []string) (info []*models.XtDrugInventory, err error) {
  884. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&info).Error
  885. return info, err
  886. }
  887. func GetDrugInventoryDetailList(keyword string, page int64, limit int64, orgid int64, storehouse_id int64) (list []*models.VmDrugInventory, total int64, err error) {
  888. likeKey := "%" + keyword + "%"
  889. offset := (page - 1) * limit
  890. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1")
  891. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  892. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  893. fmt.Println(table, tab)
  894. if len(keyword) > 0 {
  895. db = db.Where("x.warehousing_order = ? or r.user_name like ? or t.drug_name like ?", keyword, likeKey, likeKey)
  896. }
  897. if orgid > 0 {
  898. db = db.Where("x.user_org_id = ?", orgid)
  899. }
  900. if storehouse_id > 0 {
  901. db = db.Where("x.storehouse_id = ?", storehouse_id)
  902. }
  903. err = db.Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.number,x.batch_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.drug_origin_place,x.expiry_date,x.product_date,x.min_count,x.storehouse_id,x.ctime,x.warehouse_info_id,t.dose,t.dose_unit,t.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.last_stock_max_number,x.last_stock_min_number,x.inventory_type,t.min_number,t.retail_price").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Offset(offset).Limit(limit).Order("x.id desc").Scan(&list).Error
  904. return list, total, err
  905. }
  906. func GetDrugInventoryDetailMapList(keyword string, page int64, limit int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmDrugInventory, total int64, err error) {
  907. likeKey := "%" + keyword + "%"
  908. offset := (page - 1) * limit
  909. db := XTReadDB().Table("xt_drug_inventory as x").Where("x.status = 1")
  910. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  911. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  912. fmt.Println(table, tab)
  913. if len(keyword) > 0 {
  914. db = db.Where("x.warehousing_order = ? or r.user_name like ? or t.drug_name like ?", keyword, likeKey, likeKey)
  915. }
  916. if orgid > 0 {
  917. db = db.Where("x.user_org_id = ?", orgid)
  918. }
  919. if storehouse_id > 0 {
  920. db = db.Where("x.storehouse_id = ?", storehouse_id)
  921. }
  922. if len(ids) > 0 {
  923. db = db.Where("x.id in(?)", ids)
  924. }
  925. err = db.Select("x.id,x.drug_name,x.specification_name,x.warehousing_unit,x.count,x.last_price,x.retail_price,x.manufacturer,x.dealer,x.remark,x.drug_id,x.warehousing_order,x.number,x.batch_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.drug_origin_place,x.expiry_date,x.product_date,x.min_count,x.storehouse_id,x.warehouse_info_id,t.dose,t.dose_unit,t.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.last_stock_max_number,x.last_stock_min_number,x.inventory_type,t.min_number,t.retail_price").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_base_drug as t on t.id =x.drug_id").Count(&total).Offset(offset).Limit(limit).Scan(&list).Error
  926. return list, total, err
  927. }
  928. func GetDrugInventoryDetailListTwo(keyword string, page int64, limit int64, orgid int64, storehouse_id int64) (list []*models.XtDrugInventory, total int64, err error) {
  929. db := XTReadDB().Model(&list).Where("status = 1")
  930. if orgid > 0 {
  931. db = db.Where("user_org_id = ?", orgid)
  932. }
  933. if storehouse_id > 0 {
  934. db = db.Where("storehouse_id = ?", storehouse_id)
  935. }
  936. offset := (page - 1) * limit
  937. err = db.Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Preload("DrugWarehouseInfo", "org_id = ? and status = 1", orgid).Count(&total).Offset(offset).Limit(limit).Find(&list).Error
  938. return list, total, err
  939. }
  940. func UpdateDrugProofInventory(id int64, inventory *models.XtDrugInventory) error {
  941. err := XTWriteDB().Model(&inventory).Where("id=? and status = 1", id).Updates(map[string]interface{}{"warehouse_info_id": inventory.WarehouseInfoId, "proof_count": inventory.ProofCount, "warehousing_unit": inventory.WarehousingUnit, "remark": inventory.Remark}).Error
  942. return err
  943. }
  944. func UpdateDrugWarehouseInfoById(id int64, inventory *models.XtDrugWarehouseInfo) error {
  945. err := XTWriteDB().Model(&inventory).Where("id = ?", id).Updates(map[string]interface{}{"stock_max_number": inventory.StockMaxNumber}).Error
  946. return err
  947. }
  948. func GetDrugWarehouseInfoTotal(id int64) (list []*models.XtDrugWarehouseInfo, err error) {
  949. err = XTReadDB().Where("id = ? or warehousing_info_id = ? and status = 1", id, id).Preload("XtBaseDrug", "status = 1").Find(&list).Error
  950. return list, err
  951. }
  952. func CreateDrugWarehouseInfo(info models.DrugWarehouseInfo) error {
  953. err := XTWriteDB().Create(&info).Error
  954. return err
  955. }
  956. func CreateDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo) error {
  957. err := XTWriteDB().Create(&info).Error
  958. return err
  959. }
  960. func GetDamageByDrugId(drugid int64, warhousingorder string, drugtype int64) (drug []*models.VmXtDrugInventory, err error) {
  961. db := XTReadDB().Table("xt_drug_inventory").Where("status = 1")
  962. if drugid > 0 {
  963. db = db.Where("drug_id = ?", drugid)
  964. }
  965. if len(warhousingorder) > 0 {
  966. db = db.Where("warehousing_order = ?", warhousingorder)
  967. }
  968. if drugtype > 0 {
  969. db = db.Where("type = ?", drugtype)
  970. }
  971. if drugtype == 0 {
  972. db = db.Where("type = 4")
  973. }
  974. err = db.Preload("XtBaseDrug", "status = 1").Find(&drug).Error
  975. return drug, err
  976. }
  977. func GetInventoryModeList(orgid int64) (info []*models.PatientDrugWarehouseInfo, err error) {
  978. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  979. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  980. fmt.Println(table)
  981. if orgid > 0 {
  982. db = db.Where("x.org_id = ?", orgid)
  983. }
  984. err = db.Select("x.drug_id,x.number,x.product_date,x.expiry_date,x.stock_max_number,x.stock_min_number,x.batch_number,t.drug_name,t.max_unit,t.min_unit,t.min_number,t.dose,t.dose_unit,x.manufacturer,x.dealer").Joins("left join xt_base_drug as t on t.id = x.drug_id").Group("x.drug_id,x.batch_number").Scan(&info).Error
  985. return info, err
  986. }
  987. func GetManufacturerListByKeyword(orgid int64, keyword string) (list []*models.BloodManufacturer, err error) {
  988. likeKey := "%" + keyword + "%"
  989. db := XTReadDB().Model(&list).Where("status = 1")
  990. if orgid > 0 {
  991. db = db.Where("org_id = ?", orgid)
  992. }
  993. if len(keyword) > 0 {
  994. db = db.Where("manufacturer_name like ?", likeKey)
  995. }
  996. err = db.Find(&list).Error
  997. return list, err
  998. }
  999. func GetGoodNameByword(orgid int64, keyword string) (list []*models.GoodInfo, err error) {
  1000. likeKey := "%" + keyword + "%"
  1001. db := XTReadDB().Model(&list).Where("status = 1")
  1002. if orgid > 0 {
  1003. db = db.Where("org_id = ?", orgid)
  1004. }
  1005. if len(keyword) > 0 {
  1006. db = db.Where("good_name like ?", likeKey)
  1007. }
  1008. err = db.Find(&list).Error
  1009. return list, err
  1010. }
  1011. func GetDrugNameByword(orgid int64, keyword string) (list []*models.XtBaseDrug, err error) {
  1012. likeKey := "%" + keyword + "%"
  1013. db := XTReadDB().Model(&list).Where("status = 1")
  1014. if orgid > 0 {
  1015. db = db.Where("org_id = ?", orgid)
  1016. }
  1017. if len(keyword) > 0 {
  1018. db = db.Where("drug_name like ?", likeKey)
  1019. }
  1020. err = db.Find(&list).Error
  1021. return list, err
  1022. }
  1023. func GetGoodWarehouseInfoByOrgId(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  1024. db := XTReadDB().Model(&info).Where("is_check =1 and status =1")
  1025. if orgid > 0 {
  1026. db = db.Where("org_id = ?", orgid)
  1027. }
  1028. if storehouse_id > 0 {
  1029. db = db.Where("storehouse_id = ?", storehouse_id)
  1030. }
  1031. err = db.Group("good_id").Find(&info).Error
  1032. return info, err
  1033. }
  1034. func GetGoodWarehouseInfoByOrgIdTwo(orgid int64) (info []*models.GoodQueryNewWarehousingInfo, err error) {
  1035. db := XTReadDB().Model(&info).Where("is_check =1 and status =1")
  1036. if orgid > 0 {
  1037. db = db.Where("org_id = ?", orgid)
  1038. }
  1039. err = db.Group("good_id").Find(&info).Error
  1040. return info, err
  1041. }
  1042. func GetGoodNewQuery(orgId int64, storehouse_id int64, good_type int64, keyword string, page int64, limit int64, ids []int64, goodIds []int64, startime int64, endtime int64) (good []*models.NewGoodInformation, total int64, err error) {
  1043. offset := (page - 1) * limit
  1044. likeKey := "%" + keyword + "%"
  1045. db := XTReadDB().Table("xt_good_information as x").Where("x.status = 1 and x.total_count > 0 and x.org_id =?", orgId)
  1046. if good_type == 2 {
  1047. db = db.Where(" x.total_count<=x.stock_warn_count")
  1048. }
  1049. if len(goodIds) > 0 {
  1050. db = db.Where("id in(?)", goodIds)
  1051. }
  1052. if len(keyword) > 0 {
  1053. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  1054. }
  1055. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Find(&good).Error
  1056. return good, total, err
  1057. }
  1058. func FindGoodWarehouseInfoGroup(good_id int64, orgid int64, storhouse_id int64, startime int64, endtime int64) (info []*models.WarehousingInfo, err error) {
  1059. db := XTReadDB().Model(&info).Where("status = 1 and is_check = 1")
  1060. if good_id > 0 {
  1061. db = db.Where("good_id = ?", good_id)
  1062. }
  1063. if orgid > 0 {
  1064. db = db.Where("org_id = ?", orgid)
  1065. }
  1066. if storhouse_id > 0 {
  1067. db = db.Where("storehouse_id = ?", storhouse_id)
  1068. }
  1069. if startime > 0 {
  1070. db = db.Where("ctime >=?", startime)
  1071. }
  1072. if endtime > 0 {
  1073. db = db.Where("ctime<=?", endtime)
  1074. }
  1075. db.Group("storehouse_id").Find(&info)
  1076. return info, err
  1077. }
  1078. func FindGoodWarehouseInfo(good_id int64, orgid int64, storhouse_id int64, startime int64, endtime int64) (info []*models.WarehousingInfo, err error) {
  1079. db := XTReadDB().Model(&info).Where("status = 1 and is_check = 1")
  1080. if good_id > 0 {
  1081. db = db.Where("good_id = ?", good_id)
  1082. }
  1083. if orgid > 0 {
  1084. db = db.Where("org_id = ?", orgid)
  1085. }
  1086. if storhouse_id > 0 {
  1087. db = db.Where("storehouse_id = ?", storhouse_id)
  1088. }
  1089. if startime > 0 {
  1090. db = db.Where("ctime >=?", startime)
  1091. }
  1092. if endtime > 0 {
  1093. db = db.Where("ctime<=?", endtime)
  1094. }
  1095. db.Find(&info)
  1096. return info, err
  1097. }
  1098. func FindGoodWarehouseOutInfo(good_id int64, orgid int64, storhouse_id int64, startime int64, endtime int64) (out []*models.WarehouseOutInfo, err error) {
  1099. db := XTReadDB().Model(&out).Where("status = 1 and is_check = 1")
  1100. if good_id > 0 {
  1101. db = db.Where("good_id = ?", good_id)
  1102. }
  1103. if orgid > 0 {
  1104. db = db.Where("org_id = ?", orgid)
  1105. }
  1106. if storhouse_id > 0 {
  1107. db = db.Where("storehouse_id = ?", storhouse_id)
  1108. }
  1109. if startime > 0 {
  1110. db = db.Where("ctime>=?", startime)
  1111. }
  1112. if endtime > 0 {
  1113. db = db.Where("ctime<=?", endtime)
  1114. }
  1115. err = db.Find(&out).Error
  1116. return out, err
  1117. }
  1118. func FindCancelStockInfo(good_id int64, orgid int64, storhouse_id int64, startime int64, endtime int64) (info []*models.CancelStockInfo, err error) {
  1119. db := XTReadDB().Model(&info).Where("status = 1 and is_check = 1")
  1120. if good_id > 0 {
  1121. db = db.Where("good_id = ?", good_id)
  1122. }
  1123. if orgid > 0 {
  1124. db = db.Where("org_id = ?", orgid)
  1125. }
  1126. if storhouse_id > 0 {
  1127. db = db.Where("storehouse_id = ?", storhouse_id)
  1128. }
  1129. if startime > 0 {
  1130. db = db.Where("ctime>=?", startime)
  1131. }
  1132. if endtime > 0 {
  1133. db = db.Where("ctime<=?", endtime)
  1134. }
  1135. err = db.Find(&info).Error
  1136. return info, err
  1137. }
  1138. func FindGoodStockOutFlow(good_id int64, user_org_id int64, storhouse_id int64, startime int64, endtime int64) (info []*models.VmStockFlow, err error) {
  1139. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type = 3)")
  1140. if good_id > 0 {
  1141. db = db.Where("good_id = ?", good_id)
  1142. }
  1143. if user_org_id > 0 {
  1144. db = db.Where("user_org_id = ?", user_org_id)
  1145. }
  1146. if storhouse_id > 0 {
  1147. db = db.Where("storehouse_id = ?", storhouse_id)
  1148. }
  1149. if startime > 0 {
  1150. db = db.Where("ctime >= ?", storhouse_id)
  1151. }
  1152. if endtime > 0 {
  1153. db = db.Where("ctime <= ?", storhouse_id)
  1154. }
  1155. err = db.Find(&info).Error
  1156. return info, err
  1157. }
  1158. func FindGoodCancelFlow(good_id int64, user_org_id int64, storhouse_id int64, startime int64, endtime int64) (info []*models.VmStockFlow, err error) {
  1159. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 4 or consumable_type = 7)")
  1160. if good_id > 0 {
  1161. db = db.Where("good_id = ?", good_id)
  1162. }
  1163. if user_org_id > 0 {
  1164. db = db.Where("user_org_id = ?", user_org_id)
  1165. }
  1166. if storhouse_id > 0 {
  1167. db = db.Where("storehouse_id = ?", storhouse_id)
  1168. }
  1169. if startime > 0 {
  1170. db = db.Where("ctime >= ?", storhouse_id)
  1171. }
  1172. if endtime > 0 {
  1173. db = db.Where("ctime <= ?", storhouse_id)
  1174. }
  1175. err = db.Find(&info).Error
  1176. return info, err
  1177. }
  1178. func GetGoodWarehouseOutInfoByIdList(good_id int64, limit int64, page int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.WarehouseOutInfo, total int64, err error) {
  1179. db := XTReadDB().Model(&info).Where("status = 1")
  1180. offset := (page - 1) * limit
  1181. if good_id > 0 {
  1182. db = db.Where("good_id = ?", good_id)
  1183. }
  1184. if orgid > 0 {
  1185. db = db.Where("org_id =?", orgid)
  1186. }
  1187. if startime > 0 {
  1188. db = db.Where("ctime >=?", startime)
  1189. }
  1190. if endtime > 0 {
  1191. db = db.Where("ctime<=?", endtime)
  1192. }
  1193. if storehouse_id > 0 {
  1194. db = db.Where("storehouse_id = ?", storehouse_id)
  1195. }
  1196. err = db.Count(&total).Offset(offset).Limit(limit).Preload("VmStockFlow", "status = 1 and user_org_id = ?", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Order("id desc").Find(&info).Error
  1197. return info, total, err
  1198. }
  1199. func GetStockFlushByGoodId(good_id int64, org_id int64, startime int64, endtime int64) (flushinfo []*models.XtWarehouseFlushInfo, err error) {
  1200. db := XTReadDB().Model(&models.XtWarehouseFlushInfo{}).Where("status = 1 and type = 1")
  1201. if good_id > 0 {
  1202. db = db.Where("good_id = ?", good_id)
  1203. }
  1204. if org_id > 0 {
  1205. db = db.Where("user_org_id = ?", org_id)
  1206. }
  1207. if startime > 0 {
  1208. db = db.Where("ctime >=?", startime)
  1209. }
  1210. if endtime > 0 {
  1211. db = db.Where("ctime<=?", endtime)
  1212. }
  1213. err = db.Find(&flushinfo).Error
  1214. return flushinfo, err
  1215. }
  1216. func GetGoodWarehouseOutInfoByIdListTwenty(good_id int64, limit int64, page int64, orgid int64, startime int64, endtime int64) (info []*models.WarehouseOutInfo, err error) {
  1217. db := XTReadDB().Model(&info).Where("status = 1")
  1218. if good_id > 0 {
  1219. db = db.Where("good_id = ?", good_id)
  1220. }
  1221. if orgid > 0 {
  1222. db = db.Where("org_id =?", orgid)
  1223. }
  1224. if startime > 0 {
  1225. db = db.Where("ctime >=?", startime)
  1226. }
  1227. if endtime > 0 {
  1228. db = db.Where("ctime<=?", endtime)
  1229. }
  1230. err = db.Find(&info).Error
  1231. return info, err
  1232. }
  1233. func GetGoodWarehouseInfoByList(goodid int64, orgid int64, startime int64, endtime int64) (info []*models.WarehousingInfo, err error) {
  1234. db := XTReadDB().Model(&info).Where("status= 1 and is_check = 1")
  1235. if goodid > 0 {
  1236. db = db.Where("good_id = ?", goodid)
  1237. }
  1238. if orgid > 0 {
  1239. db = db.Where("org_id = ?", orgid)
  1240. }
  1241. err = db.Find(&info).Error
  1242. return info, err
  1243. }
  1244. func GetGoodCancelStockInfoByList(goodid int64, orgid int64, startime int64, endtime int64) (info []*models.CancelStockInfo, err error) {
  1245. db := XTReadDB().Model(&info).Where("status = 1 and is_check = 1")
  1246. if goodid > 0 {
  1247. db = db.Where("good_id = ?", goodid)
  1248. }
  1249. if orgid > 0 {
  1250. db = db.Where("org_id = ?", orgid)
  1251. }
  1252. err = db.Find(&info).Error
  1253. return info, err
  1254. }
  1255. func GetGoodPatientList(orgid int64, keyword string) (patient []*models.XtPatients, err error) {
  1256. likeKey := "%" + keyword + "%"
  1257. if len(keyword) > 0 {
  1258. err = XTReadDB().Where("user_org_id = ? and status = 1 and name like ?", orgid, likeKey).Find(&patient).Error
  1259. } else {
  1260. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&patient).Error
  1261. }
  1262. return patient, err
  1263. }
  1264. func GetWarehouseOutDetailByPatientIdOne(orgid int64, patient_id int64, startime int64, endtime int64, limit int64, page int64) (info []*models.WarehouseOutInfo, total int64, err error) {
  1265. db := XTReadDB().Model(&info).Where("status = 1 and (is_sys = 1 or is_sys = 5)")
  1266. offset := (page - 1) * limit
  1267. if orgid > 0 {
  1268. db = db.Where("org_id = ?", orgid)
  1269. }
  1270. if patient_id > 0 {
  1271. db = db.Where("patient_id = ?", patient_id)
  1272. }
  1273. if startime > 0 {
  1274. db = db.Where("ctime>=?", startime)
  1275. }
  1276. if endtime > 0 {
  1277. db = db.Where("ctime<=?", endtime)
  1278. }
  1279. err = db.Group("good_id").Preload("GoodInfo", "org_id = ? and status = 1", orgid).Count(&total).Offset(offset).Limit(limit).Find(&info).Error
  1280. return info, total, err
  1281. }
  1282. func GetWarehouseOutDetailByPatientId(orgid int64, patient_id int64, startime int64, endtime int64) (info []*models.WarehouseOutInfo, err error) {
  1283. db := XTReadDB().Model(&info).Where("status = 1 and (is_sys = 1 or is_sys =5)")
  1284. if orgid > 0 {
  1285. db = db.Where("org_id = ?", orgid)
  1286. }
  1287. if patient_id > 0 {
  1288. db = db.Where("patient_id = ?", patient_id)
  1289. }
  1290. if startime > 0 {
  1291. db = db.Where("ctime>=?", startime)
  1292. }
  1293. if endtime > 0 {
  1294. db = db.Where("ctime<=?", endtime)
  1295. }
  1296. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1297. return info, err
  1298. }
  1299. func GetGoodExpiryDateQuery(storehouse_id int64, expiry_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64, startime int64, endtime int64) (info []*models.WarehousingInfo, total int64, err error) {
  1300. offset := (page - 1) * limit
  1301. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 and x.stock_count >0")
  1302. if storehouse_id > 0 {
  1303. db = db.Where("x.storehouse_id = ?", storehouse_id)
  1304. }
  1305. //已过期
  1306. if expiry_type == 1 {
  1307. db = db.Where("x.expiry_date < ?", startime)
  1308. }
  1309. if expiry_type > 1 {
  1310. db = db.Where("x.expiry_date > ? and x.expiry_date<=?", startime, endtime)
  1311. }
  1312. if orgid > 0 {
  1313. db = db.Where("x.org_id = ?", orgid)
  1314. }
  1315. if len(keyword) > 0 {
  1316. db = db.Where("x.manufacturer in(?) or x.good_id in(?)", ids, goodIds)
  1317. }
  1318. err = db.Count(&total).Offset(offset).Limit(limit).Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1319. return info, total, err
  1320. }
  1321. func GetGoodPurchaseStockQuery(good_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64) (goodinfo []*models.GoodInfoTwenty, total int64, err error) {
  1322. db := XTReadDB().Model(&goodinfo).Where("status = 1 and total_count > 0")
  1323. offset := (page - 1) * limit
  1324. likeKey := "%" + keyword + "%"
  1325. if good_type > 0 {
  1326. db = db.Where("good_type_id = ?", good_type)
  1327. }
  1328. if orgid > 0 {
  1329. db = db.Where("org_id = ?", orgid)
  1330. }
  1331. if len(ids) > 0 {
  1332. db = db.Where("id in(?)", goodIds)
  1333. }
  1334. if len(keyword) > 0 {
  1335. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  1336. }
  1337. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&goodinfo).Error
  1338. return goodinfo, total, err
  1339. }
  1340. func GetDrugWarehouseOutDetailByPatientIdOne(patient_id int64, orgid int64, startime int64, endtime int64, page int64, limit int64) (info []*models.DrugWarehouseOutInfo, total int64, err error) {
  1341. db := XTReadDB().Model(&info).Where("status = 1")
  1342. offset := (page - 1) * limit
  1343. if patient_id > 0 {
  1344. db = db.Where("patient_id = ?", patient_id)
  1345. }
  1346. if orgid > 0 {
  1347. db = db.Where("org_id = ?", orgid)
  1348. }
  1349. if startime > 0 {
  1350. db = db.Where("sys_record_time >= ?", startime)
  1351. }
  1352. if endtime > 0 {
  1353. db = db.Where("sys_record_time <= ?", endtime)
  1354. }
  1355. err = db.Group("drug_id").Count(&total).Offset(offset).Limit(limit).Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Find(&info).Error
  1356. return
  1357. }
  1358. func GetDrugWarehouseOutDetailByPatientId(patient_id int64, orgid int64, startime int64, endtime int64) (info []*models.DrugWarehouseOutInfo, err error) {
  1359. db := XTReadDB().Model(&info).Where("status = 1")
  1360. if patient_id > 0 {
  1361. db = db.Where("patient_id = ?", patient_id)
  1362. }
  1363. if orgid > 0 {
  1364. db = db.Where("org_id = ?", orgid)
  1365. }
  1366. if startime > 0 {
  1367. db = db.Where("sys_record_time >= ?", startime)
  1368. }
  1369. if endtime > 0 {
  1370. db = db.Where("sys_record_time <= ?", endtime)
  1371. }
  1372. err = db.Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Find(&info).Error
  1373. return
  1374. }
  1375. func GetDrugExpiryDateQuery(storehouse_id int64, expiry_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseInfo, total int64, err error) {
  1376. offset := (page - 1) * limit
  1377. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 and (x.stock_max_number>0 or x.stock_min_number >0)")
  1378. if storehouse_id > 0 {
  1379. db = db.Where("x.storehouse_id = ?", storehouse_id)
  1380. }
  1381. //已过期
  1382. if expiry_type == 1 {
  1383. db = db.Where("x.expiry_date < ?", startime)
  1384. }
  1385. if expiry_type > 1 {
  1386. db = db.Where("x.expiry_date > ? and x.expiry_date<=?", startime, endtime)
  1387. }
  1388. if orgid > 0 {
  1389. db = db.Where("x.org_id = ?", orgid)
  1390. }
  1391. if len(keyword) > 0 {
  1392. db = db.Where("x.manufacturer in(?) or x.drug_id in(?)", ids, goodIds)
  1393. }
  1394. err = db.Count(&total).Offset(offset).Limit(limit).Preload("XtBaseDrug", "org_id = ? and status = 1", orgid).Find(&info).Error
  1395. return info, total, err
  1396. }
  1397. func GetDrugPurchaseStockQuery(good_type int64, keyword string, page int64, limit int64, orgid int64, startime int64, endtime int64, ids []int64, infos []int64) (druginfo []*models.VmBaseDrugTwenty, total int64, err error) {
  1398. db := XTReadDB().Model(&druginfo).Where("status = 1")
  1399. offset := (page - 1) * limit
  1400. likeKey := "%" + keyword + "%"
  1401. if good_type > 0 {
  1402. db = db.Where("drug_type = ?", good_type)
  1403. }
  1404. if orgid > 0 {
  1405. db = db.Where("org_id = ?", orgid)
  1406. }
  1407. if len(keyword) > 0 {
  1408. db = db.Where("drug_name like ? or manufacturer in(?)", likeKey, ids)
  1409. }
  1410. if len(infos) > 0 {
  1411. db = db.Where("id in(?)", infos)
  1412. }
  1413. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&druginfo).Error
  1414. return druginfo, total, err
  1415. }
  1416. func GetDrugWarehouseInfoByOrgId(orgid int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  1417. db := XTReadDB().Model(&info).Where("is_check = 1 and status = 1")
  1418. if orgid > 0 {
  1419. db = db.Where("org_id = ?", orgid)
  1420. }
  1421. if storehouse_id > 0 {
  1422. db = db.Where("storehouse_id = ?", storehouse_id)
  1423. }
  1424. err = db.Group("drug_id").Find(&info).Error
  1425. return info, err
  1426. }
  1427. func GetDrugWarehouseInfoByOrgIdTwo(orgid int64) (info []*models.XtDrugWarehouseInfo, err error) {
  1428. db := XTReadDB().Model(&info).Where("is_check = 1 and status = 1")
  1429. if orgid > 0 {
  1430. db = db.Where("org_id = ?", orgid)
  1431. }
  1432. err = db.Group("drug_id").Find(&info).Error
  1433. return info, err
  1434. }
  1435. func GetDrugNewQuery(orgId int64, storehouse_id int64, good_type int64, keyword string, page int64, limit int64, ids []int64, goodIds []int64, startime int64, endtime int64) (drug []*models.VmBaseDrug, total int64, err error) {
  1436. offset := (page - 1) * limit
  1437. likeKey := "%" + keyword + "%"
  1438. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  1439. if good_type == 2 {
  1440. db = db.Where("x.total_count<=x.drug_stock_limit")
  1441. }
  1442. if storehouse_id > 0 {
  1443. db = db.Preload("DrugWarehouseInfo", "org_id = ? and status = 1 and storehouse_id = ? and is_check = 1", orgId, storehouse_id)
  1444. } else {
  1445. db = db.Preload("DrugWarehouseInfo", "org_id = ? and status = 1 and is_check = 1", orgId)
  1446. }
  1447. if len(goodIds) > 0 {
  1448. db = db.Where("id in(?)", goodIds)
  1449. }
  1450. if len(keyword) > 0 {
  1451. db = db.Where("drug_name like ? or manufacturer in(?)", likeKey, ids)
  1452. }
  1453. if orgId > 0 {
  1454. db = db.Where("x.org_id = ?", orgId)
  1455. }
  1456. err = db.Count(&total).Offset(offset).Limit(limit).Preload("DrugWarehouse", func(db *gorm.DB) *gorm.DB {
  1457. if startime > 0 {
  1458. db = db.Where("ctime>=?", startime)
  1459. }
  1460. if endtime > 0 {
  1461. db = db.Where("ctime<=?", endtime)
  1462. }
  1463. if storehouse_id > 0 {
  1464. db = db.Where("storehouse_id = ?", storehouse_id)
  1465. }
  1466. return db.Where("status = 1").Group("drug_id,storehouse_id")
  1467. }).Preload("DrugCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  1468. if startime > 0 {
  1469. db = db.Where("ctime>=?", startime)
  1470. }
  1471. if endtime > 0 {
  1472. db = db.Where("ctime<=?", endtime)
  1473. }
  1474. return db.Where("status = 1")
  1475. }).Preload("DrugWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1476. if startime > 0 {
  1477. db = db.Where("ctime>=?", startime)
  1478. }
  1479. if endtime > 0 {
  1480. db = db.Where("ctime<=?", endtime)
  1481. }
  1482. return db.Where("status = 1")
  1483. }).Preload("DrugFlowWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  1484. if startime > 0 {
  1485. db = db.Where("ctime>=?", startime)
  1486. }
  1487. if endtime > 0 {
  1488. db = db.Where("ctime<=?", endtime)
  1489. }
  1490. if storehouse_id > 0 {
  1491. db = db.Where("storehouse_id = ?", storehouse_id)
  1492. }
  1493. return db.Where("status = 1 and (consumable_type = 2 or consumable_type = 3) and user_org_id = ?", orgId)
  1494. }).Preload("DrugActFlowWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  1495. if startime > 0 {
  1496. db = db.Where("ctime>=?", startime)
  1497. }
  1498. if endtime > 0 {
  1499. db = db.Where("ctime<=?", endtime)
  1500. }
  1501. if storehouse_id > 0 {
  1502. db = db.Where("storehouse_id = ?", storehouse_id)
  1503. }
  1504. return db.Where("status = 1 and (consumable_type = 2 or consumable_type = 3) and user_org_id = ?", orgId)
  1505. }).Order("x.ctime desc").Find(&drug).Error
  1506. return drug, total, err
  1507. }
  1508. func GetDrugStockCountList(orgId int64, storehouse_id int64, good_type int64, keyword string, page int64, limit int64, ids []int64, goodIds []int64, startime int64, endtime int64) (drug []*models.NewBaseDrugOne, total int64, err error) {
  1509. offset := (page - 1) * limit
  1510. likeKey := "%" + keyword + "%"
  1511. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1 AND find_in_set('停用',x.drug_status) = 0")
  1512. if good_type == 2 {
  1513. db = db.Where("x.total_count<=x.drug_stock_limit")
  1514. }
  1515. if good_type == 3 {
  1516. db = db.Where("x.sum_count = 0")
  1517. }
  1518. if good_type == 4 {
  1519. db = db.Where("x.sum_count > 0")
  1520. }
  1521. if storehouse_id > 0 {
  1522. db = db.Preload("DrugWarehouseInfo", "org_id = ? and status = 1 and storehouse_id = ? and is_check = 1", orgId, storehouse_id)
  1523. } else {
  1524. db = db.Preload("DrugWarehouseInfo", "org_id = ? and status = 1 and is_check = 1", orgId)
  1525. }
  1526. if len(keyword) > 0 {
  1527. db = db.Where("drug_name like ? or manufacturer in(?)", likeKey, ids)
  1528. }
  1529. if orgId > 0 {
  1530. db = db.Where("x.org_id = ?", orgId)
  1531. }
  1532. if orgId == 10489 {
  1533. err = db.Preload("XtDrugStockCount", func(db *gorm.DB) *gorm.DB {
  1534. if storehouse_id > 0 {
  1535. db = db.Where("storehouse_id = ?", storehouse_id)
  1536. }
  1537. return db.Where("status = 1 and user_org_id = ?", orgId)
  1538. }).Preload("DrugStockCount", func(db *gorm.DB) *gorm.DB {
  1539. if storehouse_id > 0 {
  1540. db = db.Where("storehouse_id = ?", storehouse_id)
  1541. }
  1542. return db.Where("status = 1 and user_org_id = ?", orgId).Group("drug_id,storehouse_id")
  1543. }).Count(&total).Offset(offset).Limit(limit).Order("x.sum_count desc").Find(&drug).Error
  1544. } else {
  1545. err = db.Preload("XtDrugStockCount", func(db *gorm.DB) *gorm.DB {
  1546. if storehouse_id > 0 {
  1547. db = db.Where("storehouse_id = ?", storehouse_id)
  1548. }
  1549. return db.Where("status = 1 and user_org_id = ?", orgId)
  1550. }).Preload("DrugStockCount", func(db *gorm.DB) *gorm.DB {
  1551. if storehouse_id > 0 {
  1552. db = db.Where("storehouse_id = ?", storehouse_id)
  1553. }
  1554. return db.Where("status = 1 and user_org_id = ?", orgId).Group("drug_id,storehouse_id")
  1555. }).Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Find(&drug).Error
  1556. }
  1557. return drug, total, err
  1558. }
  1559. func GetDrugWarehouseOutInfoByIdList(good_id int64, limit int64, page int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.DrugWarehouseOutInfo, total int64, err error) {
  1560. db := XTReadDB().Model(&info).Where("status = 1")
  1561. offset := (page - 1) * limit
  1562. if good_id > 0 {
  1563. db = db.Where("drug_id = ?", good_id)
  1564. }
  1565. if orgid > 0 {
  1566. db = db.Where("org_id =?", orgid)
  1567. }
  1568. if startime > 0 {
  1569. db = db.Where("ctime >=?", startime)
  1570. }
  1571. if endtime > 0 {
  1572. db = db.Where("ctime<=?", endtime)
  1573. }
  1574. if storehouse_id > 0 {
  1575. db = db.Where("storehouse_id = ?", storehouse_id)
  1576. }
  1577. err = db.Count(&total).Offset(offset).Limit(limit).Preload("BaseDrugLib", "status=1 and org_id = ?", orgid).Order("id desc").Find(&info).Error
  1578. return info, total, err
  1579. }
  1580. func GetAllGoodListSeven(orgid int64) (good []*models.GoodInfoSeven, err error) {
  1581. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  1582. return good, err
  1583. }
  1584. func GetAllGoodListEight(orgid int64) (good []*models.GoodInfoSeven, err error) {
  1585. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&good).Error
  1586. return good, err
  1587. }
  1588. func GetGoodInformationWarehouseInfo(good_id int64, orgid int64) (info []*models.WarehousingInfoTen, err error) {
  1589. err = XTReadDB().Where("good_id = ? and org_id = ? and status = 1 and is_check = 1 and stock_count > 0", good_id, orgid).Find(&info).Error
  1590. return info, err
  1591. }
  1592. func UpdateGoodInformation(id int64, orgid int64, total_count int64) error {
  1593. err := XTWriteDB().Model(&models.GoodInfoTen{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"total_count": total_count}).Error
  1594. return err
  1595. }
  1596. func UpdateGoodListCount(user_org_id int64, over_count int64, good_id int64, sotrehouse_id int64) error {
  1597. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("status=1 and user_org_id =? and good_id =? and storehouse_id=?", user_org_id, good_id, sotrehouse_id).Update(map[string]interface{}{"stock_out_count": over_count}).Error
  1598. return err
  1599. }
  1600. func UpdateGoodListCountOne(user_org_id int64, over_count int64, good_id int64, storehouse_id int64) error {
  1601. err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("status=1 and user_org_id =? and good_id = ? and storehouse_id = ?", user_org_id, good_id, storehouse_id).Update(map[string]interface{}{"stock_act_out_count": over_count}).Error
  1602. return err
  1603. }
  1604. func GetBaseDrugName(orgid int64) (drug []*models.BaseDrugLibTwenty, err error) {
  1605. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&drug).Error
  1606. return drug, err
  1607. }
  1608. func GetDrugWarehouseInfoName(drugid int64, orgid int64) (info []*models.DrugWarehouseInfoTen, err error) {
  1609. err = XTReadDB().Where("drug_id = ? and org_id = ? and is_check = 1 and (stock_max_number >0 or stock_min_number > 0) and status = 1", drugid, orgid).Find(&info).Error
  1610. return info, err
  1611. }
  1612. func UpdateDrugById(id int64, orgid int64, limit_count int64, total_count int64) error {
  1613. err := XTWriteDB().Model(&models.BaseDrugLibTen{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"drug_stock_limit_count": limit_count, "total_count": total_count}).Error
  1614. return err
  1615. }
  1616. func GetDrugFlowFlush(drugid int64, org_id int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseFlushInfo, err error) {
  1617. db := XTReadDB().Model(&info).Where("status = 1 and type = 1")
  1618. if drugid > 0 {
  1619. db = db.Where("drug_id = ?", drugid)
  1620. }
  1621. if org_id > 0 {
  1622. db = db.Where("user_org_id = ?", org_id)
  1623. }
  1624. if startime > 0 {
  1625. db = db.Where("ctime >=?", startime)
  1626. }
  1627. if endtime > 0 {
  1628. db = db.Where("ctime <=?", endtime)
  1629. }
  1630. err = db.Find(&info).Error
  1631. return info, err
  1632. }
  1633. func GetDrugFlowFlushOne(drugid int64, org_id int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseFlushInfo, err error) {
  1634. db := XTReadDB().Model(&info).Where("status = 0 and type = 1")
  1635. if drugid > 0 {
  1636. db = db.Where("drug_id = ?", drugid)
  1637. }
  1638. if org_id > 0 {
  1639. db = db.Where("user_org_id = ?", org_id)
  1640. }
  1641. if startime > 0 {
  1642. db = db.Where("ctime >=?", startime)
  1643. }
  1644. if endtime > 0 {
  1645. db = db.Where("ctime <=?", endtime)
  1646. }
  1647. err = db.Find(&info).Error
  1648. return info, err
  1649. }
  1650. func GetStartGoodWarehouseInfoList(good_id int64, orgid int64, startime int64) (info []*models.StartWarehousingInfo, err error) {
  1651. db := XTReadDB().Model(&info).Where("status = 1 and is_check =1")
  1652. if good_id > 0 {
  1653. db = db.Where("good_id = ?", good_id)
  1654. }
  1655. if orgid > 0 {
  1656. db = db.Where("org_id = ?", orgid)
  1657. }
  1658. if startime > 0 {
  1659. db = db.Where("ctime <?", startime)
  1660. }
  1661. err = db.Find(&info).Error
  1662. return info, err
  1663. }
  1664. func GetEndGoodWarehouseInfoList(good_id int64, orgid int64, endtime int64) (info []*models.StartWarehousingInfo, err error) {
  1665. db := XTReadDB().Model(&info).Where("status = 1 and is_check =1")
  1666. if good_id > 0 {
  1667. db = db.Where("good_id = ?", good_id)
  1668. }
  1669. if orgid > 0 {
  1670. db = db.Where("org_id = ?", orgid)
  1671. }
  1672. if endtime > 0 {
  1673. db = db.Where("ctime <=?", endtime)
  1674. }
  1675. err = db.Find(&info).Error
  1676. return info, err
  1677. }
  1678. func GetAddGoodWarehouseInfoList(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.StartWarehousingInfo, err error) {
  1679. db := XTReadDB().Model(&info).Where("status = 1 and is_check =1")
  1680. if good_id > 0 {
  1681. db = db.Where("good_id = ?", good_id)
  1682. }
  1683. if orgid > 0 {
  1684. db = db.Where("org_id = ?", orgid)
  1685. }
  1686. if startime > 0 {
  1687. db = db.Where("ctime >=?", startime)
  1688. }
  1689. if endtime > 0 {
  1690. db = db.Where("ctime <=?", endtime)
  1691. }
  1692. if storehouse_id > 0 {
  1693. db = db.Where("storehouse_id = ?", storehouse_id)
  1694. }
  1695. err = db.Find(&info).Error
  1696. return info, err
  1697. }
  1698. func GetAddGoodWarehouseInfoListTwenty(good_id int64, orgid int64, startime int64, endtime int64) (info []*models.StartWarehousingInfo, err error) {
  1699. db := XTReadDB().Model(&info).Where("status = 1 and is_check =1")
  1700. if good_id > 0 {
  1701. db = db.Where("good_id = ?", good_id)
  1702. }
  1703. if orgid > 0 {
  1704. db = db.Where("org_id = ?", orgid)
  1705. }
  1706. if startime > 0 {
  1707. db = db.Where("ctime >=?", startime)
  1708. }
  1709. if endtime > 0 {
  1710. db = db.Where("ctime <=?", endtime)
  1711. }
  1712. err = db.Find(&info).Error
  1713. return info, err
  1714. }
  1715. func GetAddStartFlow(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.VmStockFlowTwenty, err error) {
  1716. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type =1 or consumable_type = 13)")
  1717. if good_id > 0 {
  1718. db = db.Where("good_id = ?", good_id)
  1719. }
  1720. if orgid > 0 {
  1721. db = db.Where("user_org_id = ?", orgid)
  1722. }
  1723. if startime > 0 {
  1724. db = db.Where("ctime >=?", startime)
  1725. }
  1726. if endtime > 0 {
  1727. db = db.Where("ctime <=?", endtime)
  1728. }
  1729. if storehouse_id > 0 {
  1730. db = db.Where("storehouse_id =?", storehouse_id)
  1731. }
  1732. err = db.Find(&info).Error
  1733. return info, err
  1734. }
  1735. func GetAddStartFlowTwenty(good_id int64, orgid int64, startime int64, endtime int64) (info []*models.VmStockFlowTwenty, err error) {
  1736. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type =1)")
  1737. if good_id > 0 {
  1738. db = db.Where("good_id = ?", good_id)
  1739. }
  1740. if orgid > 0 {
  1741. db = db.Where("user_org_id = ?", orgid)
  1742. }
  1743. if startime > 0 {
  1744. db = db.Where("ctime >=?", startime)
  1745. }
  1746. if endtime > 0 {
  1747. db = db.Where("ctime <=?", endtime)
  1748. }
  1749. err = db.Find(&info).Error
  1750. return info, err
  1751. }
  1752. func GetReduceWarehouseInfoList(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1753. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type= 15)")
  1754. if good_id > 0 {
  1755. db = db.Where("good_id = ?", good_id)
  1756. }
  1757. if orgid > 0 {
  1758. db = db.Where("user_org_id = ?", orgid)
  1759. }
  1760. if startime > 0 {
  1761. db = db.Where("ctime >=?", startime)
  1762. }
  1763. if endtime > 0 {
  1764. db = db.Where("ctime <=?", endtime)
  1765. }
  1766. if storehouse_id > 0 {
  1767. db = db.Where("storehouse_id = ?", storehouse_id)
  1768. }
  1769. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1770. return outinfo, err
  1771. }
  1772. func GetReduceWarehouseInfoListTwenty(good_id int64, orgid int64, startime int64, endtime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1773. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type= 15)")
  1774. if good_id > 0 {
  1775. db = db.Where("good_id = ?", good_id)
  1776. }
  1777. if orgid > 0 {
  1778. db = db.Where("user_org_id = ?", orgid)
  1779. }
  1780. if startime > 0 {
  1781. db = db.Where("ctime >=?", startime)
  1782. }
  1783. if endtime > 0 {
  1784. db = db.Where("ctime <=?", endtime)
  1785. }
  1786. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1787. return outinfo, err
  1788. }
  1789. func GetStartEndCancelFLowInfo(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1790. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 7)")
  1791. if good_id > 0 {
  1792. db = db.Where("good_id = ?", good_id)
  1793. }
  1794. if orgid > 0 {
  1795. db = db.Where("user_org_id = ?", orgid)
  1796. }
  1797. if startime > 0 {
  1798. db = db.Where("ctime >=?", startime)
  1799. }
  1800. if endtime > 0 {
  1801. db = db.Where("ctime <=?", endtime)
  1802. }
  1803. if storehouse_id > 0 {
  1804. db = db.Where("storehouse_id =?", storehouse_id)
  1805. }
  1806. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1807. return outinfo, err
  1808. }
  1809. func GetStartEndCancelFLowInfoTwenty(good_id int64, orgid int64, startime int64, endtime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1810. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 7)")
  1811. if good_id > 0 {
  1812. db = db.Where("good_id = ?", good_id)
  1813. }
  1814. if orgid > 0 {
  1815. db = db.Where("user_org_id = ?", orgid)
  1816. }
  1817. if startime > 0 {
  1818. db = db.Where("ctime >=?", startime)
  1819. }
  1820. if endtime > 0 {
  1821. db = db.Where("ctime <=?", endtime)
  1822. }
  1823. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1824. return outinfo, err
  1825. }
  1826. func FindeStartFlowInfo(good_id int64, orgid int64, startime int64) (info []*models.VmStockFlowTwenty, err error) {
  1827. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type = 1")
  1828. if good_id > 0 {
  1829. db = db.Where("good_id = ?", good_id)
  1830. }
  1831. if orgid > 0 {
  1832. db = db.Where("user_org_id = ?", orgid)
  1833. }
  1834. if startime > 0 {
  1835. db = db.Where("ctime <?", startime)
  1836. }
  1837. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1838. return info, err
  1839. }
  1840. func FindeStartFlowOutInfo(good_id int64, orgid int64, startime int64) (info []*models.VmStockFlowTwenty, err error) {
  1841. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type = 3)")
  1842. if good_id > 0 {
  1843. db = db.Where("good_id = ?", good_id)
  1844. }
  1845. if orgid > 0 {
  1846. db = db.Where("user_org_id = ?", orgid)
  1847. }
  1848. if startime > 0 {
  1849. db = db.Where("ctime <?", startime)
  1850. }
  1851. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1852. return info, err
  1853. }
  1854. func FindeStartCancelInfo(good_id int64, orgid int64, startime int64) (info []*models.VmStockFlowTwenty, err error) {
  1855. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 4 or consumable_type = 7)")
  1856. if good_id > 0 {
  1857. db = db.Where("good_id = ?", good_id)
  1858. }
  1859. if orgid > 0 {
  1860. db = db.Where("user_org_id = ?", orgid)
  1861. }
  1862. if startime > 0 {
  1863. db = db.Where("ctime <?", startime)
  1864. }
  1865. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1866. return info, err
  1867. }
  1868. func FindEndStartFlowInfo(good_id int64, orgid int64, endtime int64) (info []*models.VmStockFlowTwenty, err error) {
  1869. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type = 1")
  1870. if good_id > 0 {
  1871. db = db.Where("good_id = ?", good_id)
  1872. }
  1873. if orgid > 0 {
  1874. db = db.Where("user_org_id = ?", orgid)
  1875. }
  1876. if endtime > 0 {
  1877. db = db.Where("ctime <=?", endtime)
  1878. }
  1879. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1880. return info, err
  1881. }
  1882. func FindeEndFLowOutInfo(good_id int64, orgid int64, endtime int64) (info []*models.VmStockFlowTwenty, err error) {
  1883. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type = 3)")
  1884. if good_id > 0 {
  1885. db = db.Where("good_id = ?", good_id)
  1886. }
  1887. if orgid > 0 {
  1888. db = db.Where("user_org_id = ?", orgid)
  1889. }
  1890. if endtime > 0 {
  1891. db = db.Where("ctime <?", endtime)
  1892. }
  1893. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1894. return info, err
  1895. }
  1896. func FindEndCancelInfo(good_id int64, orgid int64, endtime int64) (info []*models.VmStockFlowTwenty, err error) {
  1897. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 4 or consumable_type = 7)")
  1898. if good_id > 0 {
  1899. db = db.Where("good_id = ?", good_id)
  1900. }
  1901. if orgid > 0 {
  1902. db = db.Where("user_org_id = ?", orgid)
  1903. }
  1904. if endtime > 0 {
  1905. db = db.Where("ctime <?", endtime)
  1906. }
  1907. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  1908. return info, err
  1909. }
  1910. func FindeWarehouseOutInfo(good_id int64, orgid int64, startime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1911. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15)")
  1912. if good_id > 0 {
  1913. db = db.Where("good_id = ?", good_id)
  1914. }
  1915. if orgid > 0 {
  1916. db = db.Where("user_org_id = ?", orgid)
  1917. }
  1918. if startime > 0 {
  1919. db = db.Where("ctime <?", startime)
  1920. }
  1921. if storehouse_id > 0 {
  1922. db = db.Where("storehouse_id= ?", storehouse_id)
  1923. }
  1924. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1925. return outinfo, err
  1926. }
  1927. func FindeWarehouseOutInfoTwenty(good_id int64, orgid int64, startime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1928. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15)")
  1929. if good_id > 0 {
  1930. db = db.Where("good_id = ?", good_id)
  1931. }
  1932. if orgid > 0 {
  1933. db = db.Where("user_org_id = ?", orgid)
  1934. }
  1935. if startime > 0 {
  1936. db = db.Where("ctime <?", startime)
  1937. }
  1938. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1939. return outinfo, err
  1940. }
  1941. func FindeStartStockInventoryProfit(good_id int64, orgid int64, startime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1942. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 10)")
  1943. if good_id > 0 {
  1944. db = db.Where("good_id = ?", good_id)
  1945. }
  1946. if orgid > 0 {
  1947. db = db.Where("user_org_id = ?", orgid)
  1948. }
  1949. if startime > 0 {
  1950. db = db.Where("ctime <?", startime)
  1951. }
  1952. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1953. return outinfo, err
  1954. }
  1955. func FindeEndStokInventoryProfit(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1956. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 10)")
  1957. if good_id > 0 {
  1958. db = db.Where("good_id = ?", good_id)
  1959. }
  1960. if orgid > 0 {
  1961. db = db.Where("user_org_id = ?", orgid)
  1962. }
  1963. if startime > 0 {
  1964. db = db.Where("ctime >=?", startime)
  1965. }
  1966. if endtime < 0 {
  1967. db = db.Where("ctime <=?", endtime)
  1968. }
  1969. if storehouse_id > 0 {
  1970. db = db.Where("storehouse_id = ?", storehouse_id)
  1971. }
  1972. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1973. return outinfo, err
  1974. }
  1975. func FindeEndStokInventoryProfitTwenty(good_id int64, orgid int64, startime int64, endtime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1976. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 10)")
  1977. if good_id > 0 {
  1978. db = db.Where("good_id = ?", good_id)
  1979. }
  1980. if orgid > 0 {
  1981. db = db.Where("user_org_id = ?", orgid)
  1982. }
  1983. if startime > 0 {
  1984. db = db.Where("ctime >=?", startime)
  1985. }
  1986. if endtime < 0 {
  1987. db = db.Where("ctime <=?", endtime)
  1988. }
  1989. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  1990. return outinfo, err
  1991. }
  1992. func FindeEndStockInventoryLosses(good_id int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  1993. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 11)")
  1994. if good_id > 0 {
  1995. db = db.Where("good_id = ?", good_id)
  1996. }
  1997. if orgid > 0 {
  1998. db = db.Where("user_org_id = ?", orgid)
  1999. }
  2000. if startime > 0 {
  2001. db = db.Where("ctime >=?", startime)
  2002. }
  2003. if endtime < 0 {
  2004. db = db.Where("ctime <=?", endtime)
  2005. }
  2006. if storehouse_id > 0 {
  2007. db = db.Where("storehouse_id = ?", storehouse_id)
  2008. }
  2009. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  2010. return outinfo, err
  2011. }
  2012. func FindeEndStockInventoryLossesTwenty(good_id int64, orgid int64, startime int64, endtime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  2013. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 11)")
  2014. if good_id > 0 {
  2015. db = db.Where("good_id = ?", good_id)
  2016. }
  2017. if orgid > 0 {
  2018. db = db.Where("user_org_id = ?", orgid)
  2019. }
  2020. if startime > 0 {
  2021. db = db.Where("ctime >=?", startime)
  2022. }
  2023. if endtime < 0 {
  2024. db = db.Where("ctime <=?", endtime)
  2025. }
  2026. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  2027. return outinfo, err
  2028. }
  2029. func FindStartStockInventoryLosses(good_id int64, orgid int64, startime int64) (outinfo []*models.VmStockFlowTwenty, err error) {
  2030. db := XTReadDB().Model(&outinfo).Where("status = 1 and (consumable_type = 11)")
  2031. if good_id > 0 {
  2032. db = db.Where("good_id = ?", good_id)
  2033. }
  2034. if orgid > 0 {
  2035. db = db.Where("user_org_id = ?", orgid)
  2036. }
  2037. if startime > 0 {
  2038. db = db.Where("ctime <?", startime)
  2039. }
  2040. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&outinfo).Error
  2041. return outinfo, err
  2042. }
  2043. func GetDrugWarehouseInfoStart(drug_id int64, orgid int64, startTime int64) (info []*models.DrugFlowTwenty, err error) {
  2044. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type=1 ")
  2045. if drug_id > 0 {
  2046. db = db.Where("drug_id = ?", drug_id)
  2047. }
  2048. if orgid > 0 {
  2049. db = db.Where("user_org_id = ?", orgid)
  2050. }
  2051. if startTime > 0 {
  2052. db = db.Where("ctime<?", startTime)
  2053. }
  2054. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2055. return info, err
  2056. }
  2057. func DrugWarehouseInfoEnd(drug_id int64, orgid int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2058. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type = 1")
  2059. if drug_id > 0 {
  2060. db = db.Where("drug_id = ?", drug_id)
  2061. }
  2062. if orgid > 0 {
  2063. db = db.Where("user_org_id = ?", orgid)
  2064. }
  2065. if endTime > 0 {
  2066. db = db.Where("ctime<=?", endTime)
  2067. }
  2068. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2069. return info, err
  2070. }
  2071. func DrugWarehouseOutInfoEnd(drug_id int64, orgid int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2072. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type= 3 or consumable_type= 15)")
  2073. if drug_id > 0 {
  2074. db = db.Where("drug_id = ?", drug_id)
  2075. }
  2076. if orgid > 0 {
  2077. db = db.Where("user_org_id = ?", orgid)
  2078. }
  2079. if endTime > 0 {
  2080. db = db.Where("ctime<=?", endTime)
  2081. }
  2082. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2083. return info, err
  2084. }
  2085. func GetEndDrugFlowCancelInfo(drug_id int64, orgid int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2086. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 4 or consumable_type= 7)")
  2087. if drug_id > 0 {
  2088. db = db.Where("drug_id = ?", drug_id)
  2089. }
  2090. if orgid > 0 {
  2091. db = db.Where("user_org_id = ?", orgid)
  2092. }
  2093. if endTime > 0 {
  2094. db = db.Where("ctime<=?", endTime)
  2095. }
  2096. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2097. return info, err
  2098. }
  2099. func GetDrugWarehouseStartEnd(drug_id int64, orgid int64, startTime int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2100. db := XTReadDB().Model(&info).Where("status = 1 and consumable_type = 1")
  2101. if drug_id > 0 {
  2102. db = db.Where("drug_id = ?", drug_id)
  2103. }
  2104. if orgid > 0 {
  2105. db = db.Where("user_org_id = ?", orgid)
  2106. }
  2107. if startTime > 0 {
  2108. db = db.Where("ctime>=?", startTime)
  2109. }
  2110. if endTime > 0 {
  2111. db = db.Where("ctime<=?", endTime)
  2112. }
  2113. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", orgid).Find(&info).Error
  2114. return info, err
  2115. }
  2116. func FindeDrugWarehouseOutInfo(drug_id int64, orgid int64, startTime int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2117. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type= 3 or consumable_type= 15 )")
  2118. if drug_id > 0 {
  2119. db = db.Where("drug_id = ?", drug_id)
  2120. }
  2121. if orgid > 0 {
  2122. db = db.Where("user_org_id = ?", orgid)
  2123. }
  2124. if startTime > 0 {
  2125. db = db.Where("ctime>=?", startTime)
  2126. }
  2127. if endTime > 0 {
  2128. db = db.Where("ctime<=?", endTime)
  2129. }
  2130. err = db.Preload("DrugWarehouseInfoOne", "status= 1 and org_id = ?", orgid).Find(&info).Error
  2131. return info, err
  2132. }
  2133. func FindStartEndDrugWarehouseOutInfo(drug_id int64, orgid int64, startTime int64, endTime int64) (info []*models.DrugFlowTwenty, err error) {
  2134. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type= 7)")
  2135. if drug_id > 0 {
  2136. db = db.Where("drug_id = ?", drug_id)
  2137. }
  2138. if orgid > 0 {
  2139. db = db.Where("user_org_id = ?", orgid)
  2140. }
  2141. if startTime > 0 {
  2142. db = db.Where("ctime>=?", startTime)
  2143. }
  2144. if endTime > 0 {
  2145. db = db.Where("ctime<=?", endTime)
  2146. }
  2147. err = db.Preload("DrugWarehouseInfoOne", "status= 1 and org_id = ?", orgid).Find(&info).Error
  2148. return info, err
  2149. }
  2150. func FindeDrugWarehouseOutStart(drug_id int64, orgid int64, startTime int64) (info []*models.DrugFlow, err error) {
  2151. db := XTReadDB().Model(&info).Where("status = 1 and (consumable_type = 2 or consumable_type= 3)")
  2152. if drug_id > 0 {
  2153. db = db.Where("drug_id = ?", drug_id)
  2154. }
  2155. if orgid > 0 {
  2156. db = db.Where("user_org_id = ?", orgid)
  2157. }
  2158. if startTime > 0 {
  2159. db = db.Where("ctime <?", startTime)
  2160. }
  2161. err = db.Preload("DrugWarehouseInfoOne", "status= 1 and org_id = ?", orgid).Find(&info).Error
  2162. return info, err
  2163. }
  2164. func GetHisPrescriptionProjectByOrgId(orgid int64) (project []*models.HisPrescriptionProject, err error) {
  2165. err = XTReadDB().Where("user_org_id = ? and status = 0 and type = 3", orgid).Find(&project).Error
  2166. return project, err
  2167. }
  2168. func GetPatientStockFlow(patient_id int64, record_date int64, good_id int64) (models.VmStockFlow, error) {
  2169. flow := models.VmStockFlow{}
  2170. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ?", patient_id, record_date, good_id).Find(&flow).Error
  2171. return flow, err
  2172. }
  2173. func ModifyPatientStockFlow(id int64, flow models.VmStockFlow) error {
  2174. err = XTReadDB().Model(&models.VmStockFlow{}).Where("id = ?", id).Updates(map[string]interface{}{"count": flow.Count, "status": 1, "is_read": 1}).Error
  2175. return err
  2176. }
  2177. func GetDrugFlowSart(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlow, err error) {
  2178. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 1")
  2179. if drug_id > 0 {
  2180. db = db.Where("drug_id = ?", drug_id)
  2181. }
  2182. if user_org_id > 0 {
  2183. db = db.Where("user_org_id = ?", user_org_id)
  2184. }
  2185. if ctime > 0 {
  2186. db = db.Where("ctime <?", ctime)
  2187. }
  2188. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2189. return flow, err
  2190. }
  2191. func GetDrugFlowOut(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlowTwenty, err error) {
  2192. db := XTReadDB().Model(&flow).Where("status = 1 and (consumable_type = 2 or consumable_type= 3 or consumable_type= 15)")
  2193. if drug_id > 0 {
  2194. db = db.Where("drug_id = ?", drug_id)
  2195. }
  2196. if user_org_id > 0 {
  2197. db = db.Where("user_org_id = ?", user_org_id)
  2198. }
  2199. if ctime > 0 {
  2200. db = db.Where("ctime <?", ctime)
  2201. }
  2202. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2203. return flow, err
  2204. }
  2205. func GetStartDrugFlowCancelInfo(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlowTwenty, err error) {
  2206. db := XTReadDB().Model(&flow).Where("status = 1 and (consumable_type = 4 or consumable_type = 7)")
  2207. if drug_id > 0 {
  2208. db = db.Where("drug_id = ?", drug_id)
  2209. }
  2210. if user_org_id > 0 {
  2211. db = db.Where("user_org_id = ?", user_org_id)
  2212. }
  2213. if ctime > 0 {
  2214. db = db.Where("ctime <?", ctime)
  2215. }
  2216. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2217. return flow, err
  2218. }
  2219. func GetDrugFlowStartProfit(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlowTwenty, err error) {
  2220. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 10")
  2221. if drug_id > 0 {
  2222. db = db.Where("drug_id = ?", drug_id)
  2223. }
  2224. if user_org_id > 0 {
  2225. db = db.Where("user_org_id = ?", user_org_id)
  2226. }
  2227. if ctime > 0 {
  2228. db = db.Where("ctime <?", ctime)
  2229. }
  2230. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2231. return flow, err
  2232. }
  2233. func GetDrugFlowStartLosses(drug_id int64, user_org_id int64, ctime int64) (flow []*models.DrugFlowTwenty, err error) {
  2234. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 11 ")
  2235. if drug_id > 0 {
  2236. db = db.Where("drug_id = ?", drug_id)
  2237. }
  2238. if user_org_id > 0 {
  2239. db = db.Where("user_org_id = ?", user_org_id)
  2240. }
  2241. if ctime > 0 {
  2242. db = db.Where("ctime <?", ctime)
  2243. }
  2244. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2245. return flow, err
  2246. }
  2247. func GetDrugFlowEndProfit(drug_id int64, user_org_id int64, endtime int64) (flow []*models.DrugFlowTwenty, err error) {
  2248. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 10")
  2249. if drug_id > 0 {
  2250. db = db.Where("drug_id = ?", drug_id)
  2251. }
  2252. if user_org_id > 0 {
  2253. db = db.Where("user_org_id = ?", user_org_id)
  2254. }
  2255. if endtime > 0 {
  2256. db = db.Where("ctime <=?", endtime)
  2257. }
  2258. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2259. return flow, err
  2260. }
  2261. func GetDrugFlowEndLosses(drug_id int64, user_org_id int64, endtime int64) (flow []*models.DrugFlowTwenty, err error) {
  2262. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 11")
  2263. if drug_id > 0 {
  2264. db = db.Where("drug_id = ?", drug_id)
  2265. }
  2266. if user_org_id > 0 {
  2267. db = db.Where("user_org_id = ?", user_org_id)
  2268. }
  2269. if endtime > 0 {
  2270. db = db.Where("ctime <=?", endtime)
  2271. }
  2272. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2273. return flow, err
  2274. }
  2275. func GetDrugFlowStartEndProfit(drug_id int64, user_org_id int64, startime int64, endtime int64) (flow []*models.DrugFlowTwenty, err error) {
  2276. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 10 ")
  2277. if drug_id > 0 {
  2278. db = db.Where("drug_id = ?", drug_id)
  2279. }
  2280. if user_org_id > 0 {
  2281. db = db.Where("user_org_id = ?", user_org_id)
  2282. }
  2283. if startime > 0 {
  2284. db = db.Where("ctime >=?", startime)
  2285. }
  2286. if endtime > 0 {
  2287. db = db.Where("ctime<=?", endtime)
  2288. }
  2289. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2290. return flow, err
  2291. }
  2292. func GetDrugFlowStartEndLosses(drug_id int64, user_org_id int64, startime int64, endtime int64) (flow []*models.DrugFlowTwenty, err error) {
  2293. db := XTReadDB().Model(&flow).Where("status = 1 and consumable_type = 11 ")
  2294. if drug_id > 0 {
  2295. db = db.Where("drug_id = ?", drug_id)
  2296. }
  2297. if user_org_id > 0 {
  2298. db = db.Where("user_org_id = ?", user_org_id)
  2299. }
  2300. if startime > 0 {
  2301. db = db.Where("ctime >=?", startime)
  2302. }
  2303. if endtime > 0 {
  2304. db = db.Where("ctime<=?", endtime)
  2305. }
  2306. err = db.Preload("DrugWarehouseInfoOne", "org_id =? and status = 1", user_org_id).Find(&flow).Error
  2307. return flow, err
  2308. }
  2309. func GetDrugWarehouseInfoStorehouseList(orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  2310. err = XTReadDB().Where("org_id = ? and is_check= 1 and status = 1", orgid).Group("drug_id,storehouse_id").Find(&info).Error
  2311. return info, err
  2312. }
  2313. func GetDrugStockCout(drug_id int64, storehouse_id int64, user_org_id int64) (models.XtDrugStockCount, error) {
  2314. drugCount := models.XtDrugStockCount{}
  2315. err := XTReadDB().Where("drug_id = ? and storehouse_id = ? and user_org_id = ? and is_check =1 and status = 1", drug_id, storehouse_id, user_org_id).Find(&drugCount).Error
  2316. return drugCount, err
  2317. }
  2318. func GetDrugWarehouseInfoByStorehouseId(storehouse_id int64, drug_id int64, org_id int64) (info []*models.DrugWarehouseInfo, err error) {
  2319. err = XTReadDB().Where("drug_id = ? and storehouse_id = ? and org_id = ? and status =1 and is_check = 1", drug_id, storehouse_id, org_id).Find(&info).Error
  2320. return info, err
  2321. }
  2322. func GetDrugFlowStockOutCount(storehouse_id int64, drug_id int64, org_id int64) (flow []*models.DrugFlow, err error) {
  2323. err = XTReadDB().Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1 and (consumable_type = 2 or consumable_type =3)", storehouse_id, drug_id, org_id).Find(&flow).Error
  2324. return flow, err
  2325. }
  2326. func GetDrugFlowStockCancelCount(storehouse_id int64, drug_id int64, org_id int64) (flow []*models.DrugFlow, err error) {
  2327. err = XTReadDB().Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1 and consumable_type = 7", storehouse_id, drug_id, org_id).Find(&flow).Error
  2328. return flow, err
  2329. }
  2330. func CreateDrugStockCount(stockCount models.XtDrugStockCount) error {
  2331. err := XTWriteDB().Create(&stockCount).Error
  2332. return err
  2333. }
  2334. func GetGoodNewPurchaseStockQuery(good_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64) (goodinfo []*models.GoodInfoTwenty, total int64, err error) {
  2335. db := XTReadDB().Model(&goodinfo).Where("status = 1 and total_count > 0")
  2336. offset := (page - 1) * limit
  2337. likeKey := "%" + keyword + "%"
  2338. if good_type > 0 {
  2339. db = db.Where("good_type_id = ?", good_type)
  2340. }
  2341. if orgid > 0 {
  2342. db = db.Where("org_id = ?", orgid)
  2343. }
  2344. if len(ids) > 0 {
  2345. db = db.Where("id in(?)", goodIds)
  2346. }
  2347. if len(keyword) > 0 {
  2348. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  2349. }
  2350. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&goodinfo).Error
  2351. return goodinfo, total, err
  2352. }
  2353. func GetGoodNewPurchaseStockQueryOne(good_type int64, keyword string, page int64, limit int64, orgid int64, ids []int64, goodIds []int64) (goodinfo []*models.GoodInfoTwenty, total int64, err error) {
  2354. db := XTReadDB().Model(&goodinfo).Where("status = 1")
  2355. offset := (page - 1) * limit
  2356. likeKey := "%" + keyword + "%"
  2357. if good_type > 0 {
  2358. db = db.Where("good_type_id = ?", good_type)
  2359. }
  2360. if orgid > 0 {
  2361. db = db.Where("org_id = ?", orgid)
  2362. }
  2363. if len(ids) > 0 {
  2364. db = db.Where("id in(?)", goodIds)
  2365. }
  2366. if len(keyword) > 0 {
  2367. db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids)
  2368. }
  2369. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&goodinfo).Error
  2370. return goodinfo, total, err
  2371. }
  2372. func GetStartLastFLow(goodid int64, orgid int64, startime int64, storehouse_id int64) (models.VmStockFlowTwenty, error) {
  2373. flwo := models.VmStockFlowTwenty{}
  2374. err := XTReadDB().Where("good_id = ? and user_org_id = ? and status = 1 and ctime < ? and storehouse_id = ?", goodid, orgid, startime, storehouse_id).Last(&flwo).Error
  2375. return flwo, err
  2376. }
  2377. func GetStartLastFLowTwenty(goodid int64, orgid int64, startime int64) (models.VmStockFlowTwenty, error) {
  2378. flwo := models.VmStockFlowTwenty{}
  2379. err := XTReadDB().Where("good_id = ? and user_org_id = ? and status = 1 and ctime < ?", goodid, orgid, startime).Last(&flwo).Error
  2380. return flwo, err
  2381. }
  2382. func GetEndLastFlow(goodid int64, orgid int64, endtime int64, storehouse_id int64) (models.VmStockFlowTwenty, error) {
  2383. flwo := models.VmStockFlowTwenty{}
  2384. err := XTReadDB().Where("good_id = ? and user_org_id = ? and status = 1 and ctime <= ? and storehouse_id= ?", goodid, orgid, endtime, storehouse_id).Last(&flwo).Error
  2385. return flwo, err
  2386. }
  2387. func GetEndLastFlowTwenty(goodid int64, orgid int64, endtime int64) (models.VmStockFlowTwenty, error) {
  2388. flwo := models.VmStockFlowTwenty{}
  2389. err := XTReadDB().Where("good_id = ? and user_org_id = ? and status = 1 and ctime <= ?", goodid, orgid, endtime).Last(&flwo).Error
  2390. return flwo, err
  2391. }
  2392. func GetDrugNewPurchaseStockQuery(good_type int64, keyword string, page int64, limit int64, orgid int64, startime int64, endtime int64, ids []int64, infos []int64) (druginfo []*models.VmBaseDrugTwenty, total int64, err error) {
  2393. db := XTReadDB().Model(&druginfo).Where("status = 1")
  2394. offset := (page - 1) * limit
  2395. likeKey := "%" + keyword + "%"
  2396. if good_type > 0 {
  2397. db = db.Where("drug_type = ?", good_type)
  2398. }
  2399. if orgid > 0 {
  2400. db = db.Where("org_id = ?", orgid)
  2401. }
  2402. if len(keyword) > 0 {
  2403. db = db.Where("drug_name like ? or manufacturer in(?)", likeKey, ids)
  2404. }
  2405. if len(infos) > 0 {
  2406. db = db.Where("id in(?)", infos)
  2407. }
  2408. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&druginfo).Error
  2409. return druginfo, total, err
  2410. }
  2411. func GetDrugStartFlow(drug_id int64, user_org_id int64, startime int64) (models.DrugFlowTwenty, error) {
  2412. flow := models.DrugFlowTwenty{}
  2413. err := XTReadDB().Where("drug_id = ? and user_org_id = ? and ctime<? and status = 1", drug_id, user_org_id, startime).Last(&flow).Error
  2414. return flow, err
  2415. }
  2416. func GetDrugEndFlow(drug_id int64, user_org_id int64, endtime int64) (models.DrugFlowTwenty, error) {
  2417. flow := models.DrugFlowTwenty{}
  2418. err := XTReadDB().Where("drug_id = ? and user_org_id = ? and ctime<=? and status = 1", drug_id, user_org_id, endtime).Last(&flow).Error
  2419. return flow, err
  2420. }
  2421. func GetAllStockCount(orgid int64) (stock []*models.XtGoodStockCount, err error) {
  2422. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&stock).Error
  2423. return stock, err
  2424. }
  2425. func GetLastStockFlow(good_id int64) (models.VmStockFlow, error) {
  2426. flow := models.VmStockFlow{}
  2427. err := XTReadDB().Where("good_id = ? and status =1 and ctime<=1667231999", good_id).Last(&flow).Error
  2428. return flow, err
  2429. }
  2430. func ModifyStockFlowById(id int64, over_count int64) error {
  2431. err := XTWriteDB().Model(&models.VmStockFlow{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"over_count": over_count}).Error
  2432. return err
  2433. }
  2434. func GetAllDrugCount(user_org_id int64) (drug []*models.XtDrugStockCount, err error) {
  2435. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&drug).Error
  2436. return drug, err
  2437. }
  2438. func GetLastDrugFlow(drug_id int64) (models.DrugFlow, error) {
  2439. flow := models.DrugFlow{}
  2440. err := XTReadDB().Where("drug_id = ? and status =1 and ctime<=1667231999", drug_id).Last(&flow).Error
  2441. return flow, err
  2442. }
  2443. func ModifyDrugFlowById(id int64, over_count int64) error {
  2444. err := XTWriteDB().Model(&models.DrugFlow{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"over_count": over_count}).Error
  2445. return err
  2446. }
  2447. func GetDrugInventoryWarehouseInfoList(org_id int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  2448. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id = ? and is_check = 1 and org_id = ? and (stock_max_number >0 or stock_min_number >0)", storehouse_id, org_id).Preload("XtBaseDrug", "status= 1").Order("drug_id").Find(&info).Error
  2449. return info, err
  2450. }
  2451. func GetGoodStockCountList(user_org_id int64, good_id int64) (stock []*models.XtGoodStockCount, err error) {
  2452. err = XTReadDB().Where("user_org_id =? and status=1 and good_id = ?", user_org_id, good_id).Find(&stock).Error
  2453. return stock, err
  2454. }
  2455. func GetDrugCountMapList(drug_id int64, user_org_id int64) (drug []*models.XtDrugStockCount, err error) {
  2456. err = XTReadDB().Where("drug_id = ? and user_org_id =? and status=1", drug_id, user_org_id).Find(&drug).Error
  2457. return drug, err
  2458. }
  2459. func UpdateDrugStockInCount(drugId int64, storehouseId int64, over_count int64, user_org_id int64) error {
  2460. err := XTWriteDB().Model(&models.XtDrugStockCount{}).Where("drug_id =? and storehouse_id =? and status=1 and user_org_id =?", drugId, storehouseId, user_org_id).Update(map[string]interface{}{"sum_out_count": over_count}).Error
  2461. return err
  2462. }
  2463. func UpdateDrugStockInCountOne(drugId int64, storehouseId int64, over_count int64, user_org_id int64) error {
  2464. err := XTWriteDB().Model(&models.XtDrugStockCount{}).Where("drug_id =? and storehouse_id =? and status=1 and user_org_id =?", drugId, storehouseId, user_org_id).Update(map[string]interface{}{"sum_act_out_count": over_count}).Error
  2465. return err
  2466. }