self_drug_service.go 119KB

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