self_drug_service.go 107KB

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