stock_service.go 201KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  11. err := writeDb.Create(&dealer).Error
  12. return err, dealer
  13. }
  14. func ModifyDealer(dealer *models.Dealer) error {
  15. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  16. "dealer_name": dealer.DealerName,
  17. "contact": dealer.Contact,
  18. "contact_phone": dealer.ContactPhone,
  19. "platform_number": dealer.PlatformNumber,
  20. "email": dealer.Email,
  21. "contact_address": dealer.ContactAddress,
  22. "modifier": dealer.Modifier,
  23. "mtime": time.Now().Unix(),
  24. "remark": dealer.Remark,
  25. "wubi": dealer.WuBi,
  26. "pinyin": dealer.PinYin,
  27. }).Error
  28. return err
  29. }
  30. func FindAllDealerTotal(orgId int64) (total int64) {
  31. db := readDb.Model(&models.Dealer{})
  32. db = db.Where("org_id = ? ", orgId).Count(&total)
  33. return
  34. }
  35. func FindAllDealerList(orgId int64, page int64, limit int64) (list []*models.Dealer, total int64, err error) {
  36. offset := (page - 1) * limit
  37. db := readDb.Model(&models.Dealer{})
  38. db = db.Where("org_id = ? AND status = 1", orgId)
  39. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  40. return
  41. }
  42. func DeleteDealerById(id int64) error {
  43. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  44. return err
  45. }
  46. func FindDealerById(id int64) (*models.Dealer, error) {
  47. dealer := &models.Dealer{}
  48. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  49. return dealer, err
  50. }
  51. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  52. err := writeDb.Create(&manufacturer).Error
  53. return err, manufacturer
  54. }
  55. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  56. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  57. "manufacturer_name": manufacturer.ManufacturerName,
  58. "contact": manufacturer.Contact,
  59. "contact_phone": manufacturer.ContactPhone,
  60. "platform_number": manufacturer.PlatformNumber,
  61. "email": manufacturer.Email,
  62. "contact_address": manufacturer.ContactAddress,
  63. "modifier": manufacturer.Modifier,
  64. "mtime": time.Now().Unix(),
  65. "remark": manufacturer.Remark,
  66. "wubi": manufacturer.WuBi,
  67. "pinyin": manufacturer.PinYin,
  68. }).Error
  69. return err
  70. }
  71. func FindAllManufacturerList(orgId int64, page int64, limit int64) (list []*models.Manufacturer, total int64, err error) {
  72. offset := (page - 1) * limit
  73. db := readDb.Model(&models.Manufacturer{})
  74. db = db.Where("org_id = ? AND status = 1", orgId)
  75. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  76. return
  77. }
  78. func DeleteManufacturerById(id int64) error {
  79. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  80. return err
  81. }
  82. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  83. dealer := &models.Manufacturer{}
  84. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  85. return dealer, err
  86. }
  87. func FindManufacturerTotal(orgId int64) (total int64) {
  88. db := readDb.Model(&models.Manufacturer{})
  89. db = db.Where("org_id = ?", orgId).Count(&total)
  90. return
  91. }
  92. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  93. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  94. return dealers, err
  95. }
  96. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  97. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  98. return manufacturer, err
  99. }
  100. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  101. db := readDb.Model(&models.GoodsType{})
  102. db = db.Where("org_id = ?", orgId).Count(&total)
  103. return
  104. }
  105. func GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  106. goodsType := models.GoodsType{}
  107. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  108. if err == gorm.ErrRecordNotFound {
  109. return nil, err
  110. }
  111. if err != nil {
  112. return nil, err
  113. }
  114. return &goodsType, nil
  115. }
  116. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  117. goodsType := models.GoodsType{}
  118. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  119. if err == gorm.ErrRecordNotFound {
  120. return nil, err
  121. }
  122. if err != nil {
  123. return nil, err
  124. }
  125. return &goodsType, nil
  126. }
  127. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  128. err := writeDb.Create(&goodType).Error
  129. return err, goodType
  130. }
  131. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  132. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  133. "modifier": goodType.Modifier,
  134. "mtime": time.Now().Unix(),
  135. "remark": goodType.Remark,
  136. "type_name": goodType.TypeName,
  137. "out_stock": goodType.OutStock,
  138. "stock_attribute": goodType.StockAttribute,
  139. }).Error
  140. return err, goodType
  141. }
  142. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  143. offset := (page - 1) * limit
  144. db := readDb.Model(&models.GoodsType{})
  145. db = db.Where("org_id=? AND status = 1", orgId)
  146. if len(keyword) > 0 {
  147. likeKey := "%" + keyword + "%"
  148. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  149. }
  150. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  151. return
  152. }
  153. func DeleteGoodTypeById(id int64, operateId int64) error {
  154. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  155. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  156. return err
  157. }
  158. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  159. goodType := &models.GoodsType{}
  160. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  161. return goodType, err
  162. }
  163. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  164. db := readDb.Model(&models.GoodInfo{})
  165. db = db.Where("org_id = ?", orgId).Count(&total)
  166. return
  167. }
  168. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  169. err := writeDb.Create(&goodInfo).Error
  170. return err, goodInfo
  171. }
  172. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  173. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  174. "good_type_id": goodInfo.GoodTypeId,
  175. "modifier": goodInfo.Modifier,
  176. "mtime": time.Now().Unix(),
  177. "remark": goodInfo.Remark,
  178. "specification_name": goodInfo.SpecificationName,
  179. "good_unit": goodInfo.GoodUnit,
  180. "buy_price": goodInfo.BuyPrice,
  181. "sell_price": goodInfo.SellPrice,
  182. "manufacturer": goodInfo.Manufacturer,
  183. "dealer": goodInfo.Dealer,
  184. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  185. "stock_warn_count": goodInfo.StockWarnCount,
  186. "is_reuse": goodInfo.IsReuse,
  187. "good_name": goodInfo.GoodName,
  188. "pinyin": goodInfo.Pinyin,
  189. "wubi": goodInfo.Wubi,
  190. "good_kind": goodInfo.GoodKind,
  191. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  192. "retail_price": goodInfo.PackingPrice,
  193. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  194. "is_special_diseases": goodInfo.IsSpecialDiseases,
  195. "is_record": goodInfo.IsRecord,
  196. "statistics_category": goodInfo.StatisticsCategory,
  197. "good_status": goodInfo.GoodStatus,
  198. "default_count": goodInfo.DefaultCount,
  199. "sign": goodInfo.Sign,
  200. "is_default": goodInfo.IsDefault,
  201. "is_charge_use": goodInfo.IsChargeUse,
  202. "is_charge_predict": goodInfo.IsChargePredict,
  203. "is_statistics_work": goodInfo.IsStatisticsWork,
  204. "sort": goodInfo.Sort,
  205. "is_doctor_use": goodInfo.IsDoctorUse,
  206. "agent": goodInfo.Agent,
  207. "good_number": goodInfo.GoodNumber,
  208. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  209. "special_medical": goodInfo.SpecialMedical,
  210. "production_type": goodInfo.ProductionType,
  211. "min_number": goodInfo.MinNumber,
  212. "packing_unit": goodInfo.PackingUnit,
  213. "packing_price": goodInfo.PackingPrice,
  214. "default_count_unit": goodInfo.DefaultCountUnit,
  215. "min_unit": goodInfo.MinUnit,
  216. }).Error
  217. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  218. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  219. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  220. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  221. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  222. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  223. return err, goodInfo
  224. }
  225. func FindGoodInfoList(orgId int64, page int64, limit int64, keyword string, is_use int64, is_charge int64, good_kind int64, is_mark int64, manufacturer int64) (list []*models.GoodInfo, total int64, err error) {
  226. offset := (page - 1) * limit
  227. db := readDb.Model(&models.GoodInfo{})
  228. db = db.Where("org_id = ? AND status = 1", orgId)
  229. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  230. if len(keyword) > 0 {
  231. likeKey := "%" + keyword + "%"
  232. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  233. } else {
  234. if is_use > 0 {
  235. if is_use == 1 {
  236. db = db.Where("find_in_set('停用',good_status) = 0")
  237. } else {
  238. db = db.Where("find_in_set('停用',good_status) > 0")
  239. }
  240. }
  241. if is_charge > 0 {
  242. if is_charge == 1 {
  243. db = db.Where("find_in_set('收费',good_status) > 0")
  244. } else {
  245. db = db.Where("find_in_set('收费',good_status) = 0")
  246. }
  247. }
  248. if good_kind > 0 {
  249. db = db.Where(" good_kind = ?", good_kind)
  250. }
  251. if is_mark > 0 {
  252. db = db.Where("is_mark = ?", is_mark)
  253. }
  254. if manufacturer > 0 {
  255. db = db.Where("manufacturer = ?", manufacturer)
  256. }
  257. }
  258. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  259. return
  260. }
  261. func DeleteGoodInfoById(id int64, operateId int64) error {
  262. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  263. return err
  264. }
  265. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  266. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  267. return list, err
  268. }
  269. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  270. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  271. return list, err
  272. }
  273. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  274. goodInfo := &models.GoodInfo{}
  275. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  276. return goodInfo, err
  277. }
  278. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  279. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  280. return goodType, err
  281. }
  282. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  283. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  284. return goodType, err
  285. }
  286. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  287. err = readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  288. return goodInfo, err
  289. }
  290. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  291. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  292. return total, err
  293. }
  294. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  295. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  296. return total, err
  297. }
  298. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  299. err := writeDb.Create(&warehouse).Error
  300. return err
  301. }
  302. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  303. err := writeDb.Create(&warehouse).Error
  304. return err
  305. }
  306. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  307. err := writeDb.Create(&salesReturn).Error
  308. return err
  309. }
  310. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  311. err := writeDb.Create(&salesReturn).Error
  312. return err
  313. }
  314. func UpDateGoodReturnStatus(ids []string) {
  315. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  316. }
  317. func UpDateCanCelStockStatus(ids []string) {
  318. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  319. }
  320. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  321. if len(warehousingInfo) > 0 {
  322. utx := writeDb.Begin()
  323. if len(warehousingInfo) > 0 {
  324. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number) VALUES "
  325. insertParams := make([]string, 0)
  326. insertData := make([]interface{}, 0)
  327. for _, info := range warehousingInfo {
  328. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  329. insertData = append(insertData, info.WarehousingId)
  330. insertData = append(insertData, info.GoodId)
  331. insertData = append(insertData, info.GoodTypeId)
  332. insertData = append(insertData, info.Number)
  333. insertData = append(insertData, info.ProductDate)
  334. insertData = append(insertData, info.ExpiryDate)
  335. insertData = append(insertData, info.WarehousingCount)
  336. insertData = append(insertData, info.Price)
  337. insertData = append(insertData, info.TotalPrice)
  338. insertData = append(insertData, info.Dealer)
  339. insertData = append(insertData, info.Manufacturer)
  340. insertData = append(insertData, info.Remark)
  341. insertData = append(insertData, info.Ctime)
  342. insertData = append(insertData, info.Mtime)
  343. insertData = append(insertData, info.Status)
  344. insertData = append(insertData, info.OrgId)
  345. insertData = append(insertData, info.WarehousingOrder)
  346. insertData = append(insertData, info.Type)
  347. insertData = append(insertData, info.WarehousingCount)
  348. insertData = append(insertData, info.LicenseNumber)
  349. }
  350. thisSQL += strings.Join(insertParams, ", ")
  351. err = utx.Exec(thisSQL, insertData...).Error
  352. if err != nil {
  353. utx.Rollback()
  354. return
  355. }
  356. }
  357. utx.Commit()
  358. }
  359. return
  360. }
  361. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  362. if len(warehousingInfo) > 0 {
  363. utx := writeDb.Begin()
  364. if len(warehousingInfo) > 0 {
  365. thisSQL := "INSERT INTO xt_drug_warehouse_info (warehousing_id, drug_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,retail_price,retail_total_price,stock_max_number,max_unit,min_unit,batch_number) VALUES "
  366. insertParams := make([]string, 0)
  367. insertData := make([]interface{}, 0)
  368. for _, info := range warehousingInfo {
  369. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  370. insertData = append(insertData, info.WarehousingId)
  371. insertData = append(insertData, info.DrugId)
  372. insertData = append(insertData, info.Number)
  373. insertData = append(insertData, info.ProductDate)
  374. insertData = append(insertData, info.ExpiryDate)
  375. insertData = append(insertData, info.WarehousingCount)
  376. insertData = append(insertData, info.Price)
  377. insertData = append(insertData, info.TotalPrice)
  378. insertData = append(insertData, info.Dealer)
  379. insertData = append(insertData, info.Manufacturer)
  380. insertData = append(insertData, info.Remark)
  381. insertData = append(insertData, info.Ctime)
  382. insertData = append(insertData, info.Mtime)
  383. insertData = append(insertData, info.Status)
  384. insertData = append(insertData, info.OrgId)
  385. insertData = append(insertData, info.WarehousingOrder)
  386. insertData = append(insertData, info.Type)
  387. insertData = append(insertData, info.RetailPrice)
  388. insertData = append(insertData, info.RetailTotalPrice)
  389. insertData = append(insertData, info.StockMaxNumber)
  390. insertData = append(insertData, info.MaxUnit)
  391. insertData = append(insertData, info.MinUnit)
  392. insertData = append(insertData, info.BatchNumber)
  393. }
  394. thisSQL += strings.Join(insertParams, ", ")
  395. err = utx.Exec(thisSQL, insertData...).Error
  396. if err != nil {
  397. utx.Rollback()
  398. return
  399. }
  400. }
  401. utx.Commit()
  402. }
  403. return
  404. }
  405. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  406. var total float64
  407. var min_number int64
  408. for _, item := range warehousingInfo {
  409. //查询刚入库耗材的库存
  410. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  411. //更新药品库的总量
  412. info, _ := FindeDrugInfo(item.DrugId)
  413. min_number = count.StockMaxNumber * info.MinNumber
  414. str := strconv.FormatInt(min_number, 10)
  415. minFloat, _ := strconv.ParseFloat(str, 64)
  416. total = info.Total + minFloat
  417. lib := models.BaseDrugLib{
  418. Total: total,
  419. }
  420. UpdateBaseDrug(&lib, item.ID)
  421. }
  422. return err
  423. }
  424. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  425. info := models.XtDrugWarehouseInfo{}
  426. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  427. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  428. return info, err
  429. }
  430. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  431. lib := models.BaseDrugLib{}
  432. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  433. return lib, err
  434. }
  435. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  436. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  437. return err
  438. }
  439. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  440. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  441. return err
  442. }
  443. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  444. info := models.DrugWarehouseInfo{}
  445. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  446. return info, err
  447. }
  448. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  449. if len(drugflow) > 0 {
  450. utx := writeDb.Begin()
  451. if len(drugflow) > 0 {
  452. thisSQL := "INSERT INTO xt_drug_flow (warehousing_id, drug_id, number,batch_number,count,user_org_id,patient_id,system_time,consumable_type,is_sys,warehousing_order,warehouse_out_id,warehouse_out_order_number,is_edit,cancel_stock_id,cancel_order_number,manufacturer,dealer,creator,update_creator,status,ctime,mtime,price,warehousing_detail_id,warehouse_out_detail_id,cancel_out_detail_id,expire_date,product_date,max_unit,min_unit) VALUES "
  453. insertParams := make([]string, 0)
  454. insertData := make([]interface{}, 0)
  455. for _, info := range drugflow {
  456. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  457. insertData = append(insertData, info.WarehousingId)
  458. insertData = append(insertData, info.DrugId)
  459. insertData = append(insertData, info.Number)
  460. insertData = append(insertData, info.BatchNumber)
  461. insertData = append(insertData, info.Count)
  462. insertData = append(insertData, info.UserOrgId)
  463. insertData = append(insertData, info.PatientId)
  464. insertData = append(insertData, info.SystemTime)
  465. insertData = append(insertData, info.ConsumableType)
  466. insertData = append(insertData, info.IsSys)
  467. insertData = append(insertData, info.WarehousingOrder)
  468. insertData = append(insertData, info.WarehouseOutId)
  469. insertData = append(insertData, info.WarehouseOutOrderNumber)
  470. insertData = append(insertData, info.IsEdit)
  471. insertData = append(insertData, info.CancelStockId)
  472. insertData = append(insertData, info.CancelOrderNumber)
  473. insertData = append(insertData, info.Manufacturer)
  474. insertData = append(insertData, info.Dealer)
  475. insertData = append(insertData, info.Creator)
  476. insertData = append(insertData, info.UpdateCreator)
  477. insertData = append(insertData, info.Status)
  478. insertData = append(insertData, info.Ctime)
  479. insertData = append(insertData, info.Mtime)
  480. insertData = append(insertData, info.Price)
  481. insertData = append(insertData, info.WarehousingDetailId)
  482. insertData = append(insertData, info.WarehouseOutDetailId)
  483. insertData = append(insertData, info.CancelOutDetailId)
  484. insertData = append(insertData, info.ExpireDate)
  485. insertData = append(insertData, info.ProductDate)
  486. insertData = append(insertData, info.MaxUnit)
  487. insertData = append(insertData, info.MinUnit)
  488. }
  489. thisSQL += strings.Join(insertParams, ", ")
  490. err = utx.Exec(thisSQL, insertData...).Error
  491. if err != nil {
  492. utx.Rollback()
  493. return
  494. }
  495. }
  496. utx.Commit()
  497. }
  498. return
  499. }
  500. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  501. info := models.XtDrugWarehouseInfo{}
  502. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  503. return info, err
  504. }
  505. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  506. warehouseInfo := models.XtDrugWarehouseInfo{}
  507. err := XTReadDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  508. return err
  509. }
  510. func CreateDrugFlowOne(flow models.DrugFlow) error {
  511. err := XTWriteDB().Create(&flow).Error
  512. return err
  513. }
  514. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  515. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  516. return info, err
  517. }
  518. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64) (info models.WarehousingInfo, err error) {
  519. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0", good_id, good_type_id).Order("ctime").First(&info).Error
  520. return info, err
  521. }
  522. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64) (info models.VmWarehousingInfo, err error) {
  523. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  524. if good_id > 0 {
  525. db = db.Where("x.good_id = ?", good_id)
  526. }
  527. if good_type_id > 0 {
  528. db = db.Where("x.good_type_id = ?", good_type_id)
  529. }
  530. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  531. return info, err
  532. }
  533. //药品先进先出,找出最先入库的批次
  534. func FindLastDrugWarehousingInfoByID(drug_id int64) (info models.XtDrugWarehouseInfo, err error) {
  535. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) ", drug_id).Order("ctime").First(&info).Error
  536. return info, err
  537. }
  538. // 出库时先进先出,找出最先入库的批次
  539. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  540. err = readDb.Model(&models.WarehousingInfo{}).Where("drup_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) ", drug_id).Order("ctime").First(&info).Error
  541. return info, err
  542. }
  543. //
  544. //// 出库时先进先出,找出最先入库的批次
  545. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  546. // err = readDb.Model(&models.WarehousingInfo{}).Where("drup_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) ", drug_id).Order("ctime").First(&info).Error
  547. // return info, err
  548. //}
  549. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  550. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  551. return info, err
  552. }
  553. func FindAllWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64) (list []*models.Warehousing, total int64, err error) {
  554. db := readDb.Model(&models.Warehousing{})
  555. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  556. if len(keywords) > 0 {
  557. likeKey := "%" + keywords + "%"
  558. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  559. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse.warehousing_order LIKE ? or xt_warehouse.id in(?)", likeKey, likeKey, ids).Group("xt_warehouse.id")
  560. }
  561. if startTime > 0 {
  562. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  563. }
  564. if endTime > 0 {
  565. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  566. }
  567. db = db.Count(&total)
  568. offset := (page - 1) * limit
  569. err = db.Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  570. return
  571. }
  572. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  573. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  574. if orgid > 0 {
  575. db = db.Where("org_id = ?", orgid)
  576. }
  577. if len(keyword) > 0 {
  578. likeKey := "%" + keyword + "%"
  579. db = db.Where("good_name like ?", likeKey)
  580. }
  581. err = db.Find(&goodInfo).Error
  582. return goodInfo, err
  583. }
  584. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  585. db := XTReadDB().Model(&info).Where("status =1")
  586. if len(good_id) > 0 {
  587. db = db.Where("good_id in (?)", good_id)
  588. }
  589. if startime > 0 {
  590. db = db.Where("ctime >=?", startime)
  591. }
  592. if endtime > 0 {
  593. db = db.Where("ctime<=?", endtime)
  594. }
  595. if orgid > 0 {
  596. db = db.Where("org_id = ?", orgid)
  597. }
  598. err = db.Find(&info).Error
  599. return info, err
  600. }
  601. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64) (info []*models.WarehouseOutInfo, err error) {
  602. db := XTReadDB().Model(&info).Where("status =1")
  603. if len(good_id) > 0 {
  604. db = db.Where("good_id in (?)", good_id)
  605. }
  606. if startime > 0 {
  607. db = db.Where("ctime >=?", startime)
  608. }
  609. if endtime > 0 {
  610. db = db.Where("ctime<=?", endtime)
  611. }
  612. if orgid > 0 {
  613. db = db.Where("org_id = ?", orgid)
  614. }
  615. err = db.Find(&info).Error
  616. return info, err
  617. }
  618. type VMDrugWarehouse struct {
  619. ID int64 `gorm:"column:id" json:"id" form:"id"`
  620. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  621. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  622. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  623. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  624. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  625. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  626. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  627. Status int64 `gorm:"column:status" json:"status" form:"status"`
  628. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  629. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  630. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  631. Type int64 `gorm:"column:type" json:"type" form:"type"`
  632. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  633. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  634. }
  635. func (VMDrugWarehouse) TableName() string {
  636. return "xt_drug_warehouse"
  637. }
  638. type VMDrugWarehouseInfo struct {
  639. ID int64 `gorm:"column:id" json:"id" form:"id"`
  640. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  641. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  642. Number string `gorm:"column:number" json:"number" form:"number"`
  643. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  644. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  645. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  646. Price float64 `gorm:"column:price" json:"price" form:"price"`
  647. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  648. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  649. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  650. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  651. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  652. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  653. Status int64 `gorm:"column:status" json:"status" form:"status"`
  654. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  655. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  656. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  657. Type int64 `gorm:"column:type" json:"type" form:"type"`
  658. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  659. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  660. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  661. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  662. }
  663. func (VMDrugWarehouseInfo) TableName() string {
  664. return "xt_drug_warehouse_info"
  665. }
  666. type Drug struct {
  667. ID int64 `gorm:"column:id" json:"id" form:"id"`
  668. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  669. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  670. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  671. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  672. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  673. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  674. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  675. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  676. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  677. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  678. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  679. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  680. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  681. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  682. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  683. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  684. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  685. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  686. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  687. Number string `gorm:"column:number" json:"number" form:"number"`
  688. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  689. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  690. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  691. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  692. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  693. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  694. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  695. Code string `gorm:"column:code" json:"code" form:"code"`
  696. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  697. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  698. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  699. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  700. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  701. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  702. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  703. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  704. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  705. Label int64 `gorm:"column:label" json:"label" form:"label"`
  706. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  707. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  708. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  709. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  710. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  711. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  712. Status int64 `gorm:"column:status" json:"status" form:"status"`
  713. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  714. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  715. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  716. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  717. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  718. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  719. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  720. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  721. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  722. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  723. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  724. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  725. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  726. Total float64 `gorm:"column:total" json:"total" form:"total"`
  727. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  728. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  729. Dose float64 `gorm:"column:dose" json:"dose" form:"dose"`
  730. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  731. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  732. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  733. }
  734. func (Drug) TableName() string {
  735. return "xt_base_drug"
  736. }
  737. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  738. db := readDb.Model(&VMDrugWarehouse{})
  739. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  740. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  741. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  742. if len(keywords) > 0 {
  743. likeKey := "%" + keywords + "%"
  744. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  745. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  746. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_drug_warehouse.warehousing_order LIKE ?", likeKey, likeKey, likeKey).Group("xt_drug_warehouse.id")
  747. }
  748. if startTime > 0 {
  749. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  750. }
  751. if endTime > 0 {
  752. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  753. }
  754. db = db.Count(&total)
  755. offset := (page - 1) * limit
  756. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  757. return
  758. }
  759. func FindAllDrugWarehousingListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64) (list []*VMDrugWarehouse, total int64, err error) {
  760. db := readDb.Model(&VMDrugWarehouse{})
  761. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  762. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  763. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  764. if len(keywords) > 0 {
  765. likeKey := "%" + keywords + "%"
  766. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  767. db = db.Where(" sgj_user_admin_role.user_name LIKE ? OR xt_drug_warehouse.warehousing_order LIKE ? OR xt_drug_warehouse.id in(?)", likeKey, likeKey, ids).Group("xt_drug_warehouse.id")
  768. }
  769. if startTime > 0 {
  770. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  771. }
  772. if endTime > 0 {
  773. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  774. }
  775. db = db.Count(&total)
  776. offset := (page - 1) * limit
  777. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  778. return
  779. }
  780. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  781. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  782. return list, err
  783. }
  784. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  785. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  786. return list, err
  787. }
  788. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  789. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ? AND find_in_set('停用',drug_status) = 0", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  790. return list, err
  791. }
  792. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  793. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  794. return list, err
  795. }
  796. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  797. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  798. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  799. fmt.Println(table)
  800. if len(id) > 0 {
  801. db = db.Where("x.warehousing_id in(?)", id)
  802. }
  803. if org_id > 0 {
  804. db = db.Where("x.org_id = ?", org_id)
  805. }
  806. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.stock_max_number,x.stock_min_number,x.price,x.remark,x.batch_number,x.batch_number,t.manufacturer,t.dealer,t.drug_name,t.last_price,t.dose_unit,t.dose,t.min_number,t.min_unit,t.max_unit,t.drug_type").Joins("left join xt_base_drug as t on t.id = x.drug_id").Scan(&list).Error
  807. return list, err
  808. }
  809. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  810. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  811. return warehousing, err
  812. }
  813. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  814. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  815. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  816. return warehousing, err
  817. }
  818. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  819. err = readDb.Model(&VMDrugWarehouse{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  820. return warehousing, err
  821. }
  822. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  823. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  824. if len(id) > 0 {
  825. db = db.Where("x.id in(?)", id)
  826. }
  827. if org_id > 0 {
  828. db = db.Where("x.org_id = ?", org_id)
  829. }
  830. err = db.Select("x.id,x.warehousing_order,x.operation_time,x.org_id,x.creater,x.ctime,x.modifier,x.warehousing_time").Find(&warehousing).Error
  831. return warehousing, err
  832. }
  833. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  834. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  835. // return warehousing, err
  836. //
  837. //}
  838. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  839. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  840. return
  841. }
  842. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  843. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  844. return
  845. }
  846. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  847. if len(salesReturnInfo) > 0 {
  848. utx := writeDb.Begin()
  849. if len(salesReturnInfo) > 0 {
  850. thisSQL := "INSERT INTO xt_sales_return_info (good_id, sales_return_id, good_type_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer) VALUES "
  851. insertParams := make([]string, 0)
  852. insertData := make([]interface{}, 0)
  853. for _, info := range salesReturnInfo {
  854. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  855. insertData = append(insertData, info.GoodId)
  856. insertData = append(insertData, info.SalesReturnId)
  857. insertData = append(insertData, info.GoodTypeId)
  858. insertData = append(insertData, info.Count)
  859. insertData = append(insertData, info.Price)
  860. insertData = append(insertData, info.Total)
  861. insertData = append(insertData, info.Ctime)
  862. insertData = append(insertData, info.Status)
  863. insertData = append(insertData, info.OrgId)
  864. insertData = append(insertData, info.OrderNumber)
  865. insertData = append(insertData, info.Type)
  866. insertData = append(insertData, info.Dealer)
  867. insertData = append(insertData, info.Manufacturer)
  868. }
  869. thisSQL += strings.Join(insertParams, ", ")
  870. err = utx.Exec(thisSQL, insertData...).Error
  871. if err != nil {
  872. utx.Rollback()
  873. return
  874. }
  875. }
  876. utx.Commit()
  877. }
  878. return
  879. }
  880. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  881. if len(salesReturnInfo) > 0 {
  882. utx := writeDb.Begin()
  883. if len(salesReturnInfo) > 0 {
  884. thisSQL := "INSERT INTO xt_drug_sales_return_info (drug_id,sales_return_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer,retail_price,retail_total_price) VALUES "
  885. insertParams := make([]string, 0)
  886. insertData := make([]interface{}, 0)
  887. for _, info := range salesReturnInfo {
  888. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  889. insertData = append(insertData, info.DrugId)
  890. insertData = append(insertData, info.SalesReturnId)
  891. insertData = append(insertData, info.Count)
  892. insertData = append(insertData, info.Price)
  893. insertData = append(insertData, info.Total)
  894. insertData = append(insertData, info.Ctime)
  895. insertData = append(insertData, info.Status)
  896. insertData = append(insertData, info.OrgId)
  897. insertData = append(insertData, info.OrderNumber)
  898. insertData = append(insertData, info.Type)
  899. insertData = append(insertData, info.Dealer)
  900. insertData = append(insertData, info.Manufacturer)
  901. insertData = append(insertData, info.RetailPrice)
  902. insertData = append(insertData, info.RetailTotalPrice)
  903. }
  904. thisSQL += strings.Join(insertParams, ", ")
  905. err = utx.Exec(thisSQL, insertData...).Error
  906. if err != nil {
  907. utx.Rollback()
  908. return
  909. }
  910. }
  911. utx.Commit()
  912. }
  913. return
  914. }
  915. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  916. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  917. return total, err
  918. }
  919. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  920. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  921. return total, err
  922. }
  923. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  924. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  925. return total, err
  926. }
  927. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  928. db := readDb.Model(&models.SalesReturn{})
  929. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  930. if len(keywords) > 0 {
  931. likeKey := "%" + keywords + "%"
  932. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  933. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  934. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_sales_return.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_sales_return.id")
  935. }
  936. if startTime > 0 {
  937. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  938. }
  939. if endTime > 0 {
  940. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  941. }
  942. db = db.Count(&total)
  943. offset := (page - 1) * limit
  944. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  945. return
  946. }
  947. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  948. db := readDb.Model(&models.DrugSalesReturn{})
  949. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  950. if len(keywords) > 0 {
  951. likeKey := "%" + keywords + "%"
  952. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  953. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  954. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_drug_sales_return.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_drug_sales_return.id")
  955. }
  956. if startTime > 0 {
  957. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  958. }
  959. if endTime > 0 {
  960. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  961. }
  962. db = db.Count(&total)
  963. offset := (page - 1) * limit
  964. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  965. return
  966. }
  967. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  968. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  969. return list, err
  970. }
  971. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  972. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("GoodInfo", "status = 1").Find(&list).Error
  973. return list, err
  974. }
  975. type DrugCancelStockInfo struct {
  976. ID int64 `gorm:"column:id" json:"id" form:"id"`
  977. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  978. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  979. Count int64 `gorm:"column:count" json:"count" form:"count"`
  980. Price float64 `gorm:"column:price" json:"price" form:"price"`
  981. Total float64 `gorm:"column:total" json:"total" form:"total"`
  982. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  983. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  984. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  985. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  986. Status int64 `gorm:"column:status" json:"status" form:"status"`
  987. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  988. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  989. Type int64 `gorm:"column:type" json:"type" form:"type"`
  990. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  991. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  992. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  993. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  994. Number string `gorm:"column:number" json:"number" form:"number"`
  995. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  996. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  997. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  998. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  999. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1000. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1001. }
  1002. func (DrugCancelStockInfo) TableName() string {
  1003. return "xt_drug_cancel_stock_info"
  1004. }
  1005. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1006. err = readDb.Model(&DrugCancelStockInfo{}).Preload("Drug", "status = 1 AND org_id = ? ", org_id).Where("cancel_stock_id = ? AND status = 1", id).Find(&list).Error
  1007. return list, err
  1008. }
  1009. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1010. db := readDb.Model(&models.WarehousingInfo{})
  1011. db = db.Where("org_id = ? AND status = 1", orgId)
  1012. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1013. if startTime > 0 {
  1014. db = db.Where("ctime >=?", startTime)
  1015. }
  1016. if endTime > 0 {
  1017. db = db.Where("ctime<= ?", endTime)
  1018. }
  1019. db = db.Count(&total)
  1020. offset := (page - 1) * limit
  1021. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1022. return list, total, err
  1023. }
  1024. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1025. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1026. return total, err
  1027. }
  1028. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1029. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1030. return total, err
  1031. }
  1032. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut) error {
  1033. err := writeDb.Create(&warehouseOut).Error
  1034. return err
  1035. }
  1036. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1037. err := writeDb.Create(&warehouseOut).Error
  1038. return err
  1039. }
  1040. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1041. if len(warehouseOutInfo) > 0 {
  1042. utx := writeDb.Begin()
  1043. if len(warehouseOutInfo) > 0 {
  1044. thisSQL := "INSERT INTO xt_warehouse_out_info (warehouse_out_id, good_id, good_type_id, product_date,expiry_date,count,price,total_price,remark,ctime,status,org_id,warehouse_out_order_number,type,dealer,manufacturer,number,license_number) VALUES "
  1045. insertParams := make([]string, 0)
  1046. insertData := make([]interface{}, 0)
  1047. for _, info := range warehouseOutInfo {
  1048. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1049. insertData = append(insertData, info.WarehouseOutId)
  1050. insertData = append(insertData, info.GoodId)
  1051. insertData = append(insertData, info.GoodTypeId)
  1052. insertData = append(insertData, info.ProductDate)
  1053. insertData = append(insertData, info.ExpiryDate)
  1054. insertData = append(insertData, info.Count)
  1055. insertData = append(insertData, info.Price)
  1056. insertData = append(insertData, info.TotalPrice)
  1057. insertData = append(insertData, info.Remark)
  1058. insertData = append(insertData, info.Ctime)
  1059. insertData = append(insertData, info.Status)
  1060. insertData = append(insertData, info.OrgId)
  1061. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1062. insertData = append(insertData, info.Type)
  1063. insertData = append(insertData, info.Dealer)
  1064. insertData = append(insertData, info.Manufacturer)
  1065. insertData = append(insertData, info.Number)
  1066. insertData = append(insertData, info.LicenseNumber)
  1067. }
  1068. thisSQL += strings.Join(insertParams, ", ")
  1069. err = utx.Exec(thisSQL, insertData...).Error
  1070. if err != nil {
  1071. utx.Rollback()
  1072. return
  1073. }
  1074. }
  1075. utx.Commit()
  1076. }
  1077. return
  1078. }
  1079. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1080. if len(warehouseOutInfo) > 0 {
  1081. utx := writeDb.Begin()
  1082. if len(warehouseOutInfo) > 0 {
  1083. thisSQL := "INSERT INTO xt_drug_warehouse_out_info (warehouse_out_id, drug_id, product_date,expiry_date,count,price,total_price,remark,ctime,status,org_id,warehouse_out_order_number,type,dealer,manufacturer,retail_price,retail_total_price,number,batch_number,count_unit) VALUES "
  1084. insertParams := make([]string, 0)
  1085. insertData := make([]interface{}, 0)
  1086. for _, info := range warehouseOutInfo {
  1087. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1088. insertData = append(insertData, info.WarehouseOutId)
  1089. insertData = append(insertData, info.DrugId)
  1090. insertData = append(insertData, info.ProductDate)
  1091. insertData = append(insertData, info.ExpiryDate)
  1092. insertData = append(insertData, info.Count)
  1093. insertData = append(insertData, info.Price)
  1094. insertData = append(insertData, info.TotalPrice)
  1095. insertData = append(insertData, info.Remark)
  1096. insertData = append(insertData, info.Ctime)
  1097. insertData = append(insertData, info.Status)
  1098. insertData = append(insertData, info.OrgId)
  1099. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1100. insertData = append(insertData, info.Type)
  1101. insertData = append(insertData, info.Dealer)
  1102. insertData = append(insertData, info.Manufacturer)
  1103. insertData = append(insertData, info.RetailPrice)
  1104. insertData = append(insertData, info.RetailTotalPrice)
  1105. insertData = append(insertData, info.Number)
  1106. insertData = append(insertData, info.BatchNumber)
  1107. insertData = append(insertData, info.CountUnit)
  1108. }
  1109. thisSQL += strings.Join(insertParams, ", ")
  1110. err = utx.Exec(thisSQL, insertData...).Error
  1111. if err != nil {
  1112. utx.Rollback()
  1113. return
  1114. }
  1115. }
  1116. utx.Commit()
  1117. }
  1118. return
  1119. }
  1120. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1121. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1122. return info, err
  1123. }
  1124. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1125. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1126. return info, err
  1127. }
  1128. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1129. db := readDb.Model(&models.WarehouseOut{})
  1130. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1131. if len(keywords) > 0 {
  1132. likeKey := "%" + keywords + "%"
  1133. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1134. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1135. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out.warehouse_out_order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_out.id")
  1136. }
  1137. if startTime > 0 {
  1138. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1139. }
  1140. if endTime > 0 {
  1141. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1142. }
  1143. db = db.Count(&total)
  1144. offset := (page - 1) * limit
  1145. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1146. return list, total, err
  1147. }
  1148. func FindAllWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64) (list []*models.WarehouseOut, total int64, err error) {
  1149. db := readDb.Model(&models.WarehouseOut{})
  1150. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1151. if len(keywords) > 0 {
  1152. likeKey := "%" + keywords + "%"
  1153. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1154. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out.warehouse_out_order_number LIKE ? or xt_warehouse_out.id in(?)", likeKey, likeKey, ids).Group("xt_warehouse_out.id")
  1155. }
  1156. if startTime > 0 {
  1157. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1158. }
  1159. if endTime > 0 {
  1160. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1161. }
  1162. db = db.Count(&total)
  1163. offset := (page - 1) * limit
  1164. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1165. return list, total, err
  1166. }
  1167. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1168. db := readDb.Model(&models.DrugWarehouseOut{})
  1169. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1170. if len(keywords) > 0 {
  1171. likeKey := "%" + keywords + "%"
  1172. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1173. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1174. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_drug_warehouse_out.warehouse_out_order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_drug_warehouse_out.id")
  1175. }
  1176. if startTime > 0 {
  1177. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1178. }
  1179. if endTime > 0 {
  1180. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1181. }
  1182. db = db.Count(&total)
  1183. offset := (page - 1) * limit
  1184. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1185. return list, total, err
  1186. }
  1187. func FindAllDrugWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64) (list []*models.DrugWarehouseOut, total int64, err error) {
  1188. db := readDb.Model(&models.DrugWarehouseOut{})
  1189. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1190. if len(keywords) > 0 {
  1191. likeKey := "%" + keywords + "%"
  1192. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1193. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_drug_warehouse_out.warehouse_out_order_number LIKE ? or xt_drug_warehouse_out.id in(?) ", likeKey, likeKey, ids).Group("xt_drug_warehouse_out.id")
  1194. }
  1195. if startTime > 0 {
  1196. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1197. }
  1198. if endTime > 0 {
  1199. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1200. }
  1201. db = db.Count(&total)
  1202. offset := (page - 1) * limit
  1203. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1204. return list, total, err
  1205. }
  1206. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1207. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? AND status = 1 AND count <> 0 AND good_id <> 0", id).Preload("GoodInfo", "status = 1").Order("good_type_id desc").Find(&list).Error
  1208. return list, err
  1209. }
  1210. type DrugWarehouseOutInfo struct {
  1211. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1212. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1213. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1214. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1215. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1216. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1217. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1218. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1219. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1220. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1221. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1222. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1223. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1224. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1225. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1226. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1227. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1228. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1229. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1230. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1231. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1232. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1233. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1234. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1235. Number string `gorm:"column:number" json:"number" form:"number"`
  1236. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1237. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1238. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1239. }
  1240. func (DrugWarehouseOutInfo) TableName() string {
  1241. return "xt_drug_warehouse_out_info"
  1242. }
  1243. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1244. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1245. return list, err
  1246. }
  1247. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1248. info := &models.WarehousingInfo{}
  1249. type WarehouseInfoCountAmount struct {
  1250. Total int64
  1251. }
  1252. var result WarehouseInfoCountAmount
  1253. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1254. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1255. return result.Total, info
  1256. }
  1257. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1258. type SalesReturnCountAmount struct {
  1259. Total int64
  1260. }
  1261. var result SalesReturnCountAmount
  1262. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1263. return result.Total, err
  1264. }
  1265. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1266. type WarehouseOutInfoCountAmount struct {
  1267. Total int64
  1268. }
  1269. var result WarehouseOutInfoCountAmount
  1270. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1271. return result.Total, err
  1272. }
  1273. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1274. type CancleStockoCountAmount struct {
  1275. Total int64
  1276. }
  1277. var result CancleStockoCountAmount
  1278. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1279. return result.Total, err
  1280. }
  1281. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1282. info := models.WarehousingInfo{}
  1283. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  1284. return info, err
  1285. }
  1286. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  1287. err := writeDb.Create(&cancelStock).Error
  1288. return err
  1289. }
  1290. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  1291. err := writeDb.Create(&cancelStock).Error
  1292. return err
  1293. }
  1294. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  1295. if len(cancelStockInfo) > 0 {
  1296. utx := writeDb.Begin()
  1297. if len(cancelStockInfo) > 0 {
  1298. thisSQL := "INSERT INTO xt_cancel_stock_info (good_id, cancel_stock_id, good_type_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer,number,register_account,remark,product_date,expiry_date,warehouse_info_id) VALUES "
  1299. insertParams := make([]string, 0)
  1300. insertData := make([]interface{}, 0)
  1301. for _, info := range cancelStockInfo {
  1302. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1303. insertData = append(insertData, info.GoodId)
  1304. insertData = append(insertData, info.CancelStockId)
  1305. insertData = append(insertData, info.GoodTypeId)
  1306. insertData = append(insertData, info.Count)
  1307. insertData = append(insertData, info.Price)
  1308. insertData = append(insertData, info.Total)
  1309. insertData = append(insertData, info.Ctime)
  1310. insertData = append(insertData, info.Status)
  1311. insertData = append(insertData, info.OrgId)
  1312. insertData = append(insertData, info.OrderNumber)
  1313. insertData = append(insertData, info.Type)
  1314. insertData = append(insertData, info.Dealer)
  1315. insertData = append(insertData, info.Manufacturer)
  1316. insertData = append(insertData, info.Number)
  1317. insertData = append(insertData, info.RegisterAccount)
  1318. insertData = append(insertData, info.Remark)
  1319. insertData = append(insertData, info.ProductDate)
  1320. insertData = append(insertData, info.ExpiryDate)
  1321. insertData = append(insertData, info.WarehouseInfoId)
  1322. }
  1323. thisSQL += strings.Join(insertParams, ", ")
  1324. err = utx.Exec(thisSQL, insertData...).Error
  1325. if err != nil {
  1326. utx.Rollback()
  1327. return
  1328. }
  1329. }
  1330. utx.Commit()
  1331. }
  1332. return
  1333. }
  1334. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  1335. if len(cancelStockInfo) > 0 {
  1336. utx := writeDb.Begin()
  1337. if len(cancelStockInfo) > 0 {
  1338. thisSQL := "INSERT INTO xt_drug_cancel_stock_info (drug_id,cancel_stock_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer,retail_price,retail_total_price,register_account,batch_number,remark,max_unit,product_date,expiry_date,batch_number_id) VALUES "
  1339. insertParams := make([]string, 0)
  1340. insertData := make([]interface{}, 0)
  1341. for _, info := range cancelStockInfo {
  1342. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1343. insertData = append(insertData, info.DrugId)
  1344. insertData = append(insertData, info.CancelStockId)
  1345. insertData = append(insertData, info.Count)
  1346. insertData = append(insertData, info.Price)
  1347. insertData = append(insertData, info.Total)
  1348. insertData = append(insertData, info.Ctime)
  1349. insertData = append(insertData, info.Status)
  1350. insertData = append(insertData, info.OrgId)
  1351. insertData = append(insertData, info.OrderNumber)
  1352. insertData = append(insertData, info.Type)
  1353. insertData = append(insertData, info.Dealer)
  1354. insertData = append(insertData, info.Manufacturer)
  1355. insertData = append(insertData, info.RetailPrice)
  1356. insertData = append(insertData, info.RetailTotalPrice)
  1357. insertData = append(insertData, info.RegisterAccount)
  1358. insertData = append(insertData, info.BatchNumber)
  1359. insertData = append(insertData, info.Remark)
  1360. insertData = append(insertData, info.MaxUnit)
  1361. insertData = append(insertData, info.ProductDate)
  1362. insertData = append(insertData, info.ExpiryDate)
  1363. insertData = append(insertData, info.BatchNumberId)
  1364. }
  1365. thisSQL += strings.Join(insertParams, ", ")
  1366. err = utx.Exec(thisSQL, insertData...).Error
  1367. if err != nil {
  1368. utx.Rollback()
  1369. return
  1370. }
  1371. }
  1372. utx.Commit()
  1373. }
  1374. return
  1375. }
  1376. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  1377. db := readDb.Model(&models.CancelStock{})
  1378. db = db.Where("org_id = ? AND status = 1", orgId)
  1379. err = db.Order("ctime desc").Find(&list).Error
  1380. return
  1381. }
  1382. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  1383. db := readDb.Model(&models.WarehousingInfo{})
  1384. db = db.Where("org_id = ? AND status = 1", orgId)
  1385. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  1386. if startTime > 0 {
  1387. db = db.Where("ctime >=?", startTime)
  1388. }
  1389. if endTime > 0 {
  1390. db = db.Where("ctime<= ?", endTime)
  1391. }
  1392. db = db.Count(&total)
  1393. offset := (page - 1) * limit
  1394. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1395. return
  1396. }
  1397. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  1398. db := readDb.Model(&models.StockInfo{})
  1399. if startime > 0 {
  1400. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1401. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  1402. })
  1403. }
  1404. if startime == 0 || endtime == 0 {
  1405. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1406. return db.Where("org_id = ? AND status = 1", orgId)
  1407. })
  1408. }
  1409. if endtime > 0 {
  1410. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1411. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  1412. })
  1413. }
  1414. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  1415. db = db.Group("xt_good_information.id")
  1416. if startime == 0 || endtime == 0 {
  1417. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1418. return db.Where("org_id = ? AND status = 1", orgId)
  1419. })
  1420. }
  1421. if startime > 0 {
  1422. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1423. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  1424. })
  1425. }
  1426. if endtime > 0 {
  1427. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1428. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  1429. })
  1430. }
  1431. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  1432. if startime == 0 || endtime == 0 {
  1433. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  1434. }
  1435. if startime > 0 {
  1436. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  1437. }
  1438. if endtime > 0 {
  1439. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  1440. }
  1441. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  1442. if len(keyword) > 0 {
  1443. likeKey := "%" + keyword + "%"
  1444. db = db.Joins("join xt_goods_type on xt_goods_type.id = xt_good_information.good_type_id AND xt_goods_type.org_id = ? AND xt_goods_type.status = 1", orgId)
  1445. db = db.Where("xt_good_information.good_code LIKE ? OR xt_good_information.specification_name LIKE ? OR xt_good_information.good_name LIKE ? OR xt_goods_type.type_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_good_information.id")
  1446. }
  1447. if type_name > 0 {
  1448. db = db.Joins("join xt_goods_type on xt_goods_type.id = xt_good_information.good_type_id AND xt_goods_type.org_id = ? AND xt_goods_type.status = 1 and xt_goods_type.id = ?", orgId, type_name)
  1449. }
  1450. db = db.Count(&total)
  1451. offset := (page - 1) * limit
  1452. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1453. return
  1454. }
  1455. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  1456. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  1457. return
  1458. }
  1459. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  1460. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  1461. return
  1462. }
  1463. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  1464. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", warehouse.WarehousingOrder).Update(map[string]interface{}{"operation_time": warehouse.OperationTime, "mtime": warehouse.Mtime, "modifier": warehouse.Modifier}).Error
  1465. return
  1466. }
  1467. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  1468. err = readDb.Model(&models.WarehouseOut{}).Where("warehouse_out_order_number = ? AND status = 1", warehouseOut.WarehouseOutOrderNumber).Update(map[string]interface{}{"operation_time": warehouseOut.OperationTime, "mtime": warehouseOut.Mtime, "modifier": warehouseOut.Modifier}).Error
  1469. return
  1470. }
  1471. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  1472. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  1473. "good_id": warehouseInfo.GoodId,
  1474. "number": warehouseInfo.Number,
  1475. "product_date": warehouseInfo.ProductDate,
  1476. "expiry_date": warehouseInfo.ExpiryDate,
  1477. "warehousing_count": warehouseInfo.WarehousingCount,
  1478. "price": warehouseInfo.Price,
  1479. "total_price": warehouseInfo.TotalPrice,
  1480. "mtime": warehouseInfo.Mtime,
  1481. "remark": warehouseInfo.Remark,
  1482. }).Error
  1483. return
  1484. }
  1485. func UpDateWarehouseStatus(id int64) (err error) {
  1486. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1487. return
  1488. }
  1489. func UpDateDrugWarehouseStatus(id int64) (err error) {
  1490. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1491. return
  1492. }
  1493. func DeleteWarehouse(ids []string) (err error) {
  1494. ut := writeDb.Begin()
  1495. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1496. if err != nil {
  1497. ut.Rollback()
  1498. return
  1499. }
  1500. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1501. if err != nil {
  1502. ut.Rollback()
  1503. return
  1504. }
  1505. ut.Commit()
  1506. return
  1507. }
  1508. func DeleteDrugWarehouse(ids []string) (err error) {
  1509. ut := writeDb.Begin()
  1510. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1511. if err != nil {
  1512. ut.Rollback()
  1513. return
  1514. }
  1515. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1516. if err != nil {
  1517. ut.Rollback()
  1518. return
  1519. }
  1520. ut.Commit()
  1521. return
  1522. }
  1523. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  1524. warehouse := &models.Warehousing{}
  1525. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  1526. return warehouse, err
  1527. }
  1528. func FindWareHouseOutById(id int64) (*models.WarehouseOut, error) {
  1529. warehouseOut := &models.WarehouseOut{}
  1530. err := readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  1531. return warehouseOut, err
  1532. }
  1533. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  1534. warehouseOut := &models.DrugWarehouseOut{}
  1535. err := readDb.Model(&models.DrugWarehouseOut{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ? ", org_id).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  1536. return warehouseOut, err
  1537. }
  1538. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  1539. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  1540. return
  1541. }
  1542. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  1543. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  1544. return
  1545. }
  1546. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  1547. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1548. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1549. return
  1550. }
  1551. func DeleteStockInRecordByReocordId(id int64) (err error) {
  1552. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1553. return
  1554. }
  1555. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  1556. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  1557. if manufacturer_id > 0 {
  1558. db = db.Where("manufacturer = ?", manufacturer_id)
  1559. }
  1560. if dealer_id > 0 {
  1561. db = db.Where("dealer = ?", dealer_id)
  1562. }
  1563. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  1564. return goodInfo, err
  1565. }
  1566. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  1567. db := readDb.Model(&models.Warehousing{})
  1568. likekey := "%" + keywords + "%"
  1569. db = db.Where("org_id = ? AND status = 1 ", orgId)
  1570. db = db.Where("warehousing_order LIKE ? ", likekey)
  1571. db = db.Count(&total)
  1572. offset := (page - 1) * limit
  1573. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1574. return
  1575. }
  1576. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  1577. info := models.WarehousingInfo{}
  1578. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  1579. return info, err
  1580. }
  1581. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  1582. info := models.WarehousingInfo{}
  1583. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  1584. return info, err
  1585. }
  1586. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  1587. err = writeDb.Save(&info).Error
  1588. return err
  1589. }
  1590. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  1591. err = writeDb.Save(&info).Error
  1592. return err
  1593. }
  1594. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  1595. info := models.DrugWarehouseInfo{}
  1596. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  1597. return info, err
  1598. }
  1599. func GetDrugWarehouseOrder(id int64) (models.DrugWarehouseInfo, error) {
  1600. info := models.DrugWarehouseInfo{}
  1601. err = XTReadDB().Model(&info).Where("id = ?", id).Find(&info).Error
  1602. return info, err
  1603. }
  1604. func EditWarehousing(warehouse models.Warehousing) {
  1605. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", warehouse.WarehousingOrder).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehousing_time": warehouse.WarehousingTime, "modifier": warehouse.Modifier, "dealer": warehouse.Dealer, "manufacturer": warehouse.Manufacturer}).Error
  1606. }
  1607. func EditDrugWarehousing(warehouse models.DrugWarehouse) {
  1608. err = readDb.Model(&models.DrugWarehouse{}).Where("warehousing_order = ? AND status = 1", warehouse.WarehousingOrder).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehousing_time": warehouse.WarehousingTime, "modifier": warehouse.Modifier, "dealer": warehouse.Dealer, "manufacturer": warehouse.Manufacturer}).Error
  1609. }
  1610. func FindAllWarehouse(org_id int64, types int64) (list []*models.Warehousing, err error) {
  1611. err = readDb.Model(&models.Warehousing{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ? ", org_id).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  1612. return list, err
  1613. }
  1614. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  1615. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  1616. return db.Where("status = 1 AND org_id = ?", org_id).Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id)
  1617. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  1618. return list, err
  1619. }
  1620. func DeleteSalesReturn(ids []string) (err error) {
  1621. ut := writeDb.Begin()
  1622. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1623. if err != nil {
  1624. ut.Rollback()
  1625. return
  1626. }
  1627. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1628. if err != nil {
  1629. ut.Rollback()
  1630. return
  1631. }
  1632. ut.Commit()
  1633. return
  1634. }
  1635. func DeleteDrugSalesReturn(ids []string) (err error) {
  1636. ut := writeDb.Begin()
  1637. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1638. if err != nil {
  1639. ut.Rollback()
  1640. return
  1641. }
  1642. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1643. if err != nil {
  1644. ut.Rollback()
  1645. return
  1646. }
  1647. ut.Commit()
  1648. return
  1649. }
  1650. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1651. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1652. return
  1653. }
  1654. type DrugSalesReturnInfo struct {
  1655. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1656. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1657. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  1658. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1659. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1660. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1661. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1662. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1663. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1664. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1665. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1666. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1667. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1668. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1669. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1670. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1671. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1672. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1673. Number string `gorm:"column:number" json:"number" form:"number"`
  1674. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1675. }
  1676. func (DrugSalesReturnInfo) TableName() string {
  1677. return "xt_drug_sales_return_info"
  1678. }
  1679. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  1680. err = readDb.Model(&DrugSalesReturnInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("sales_return_id = ? AND status = 1 AND org_id = ?", id, org_id).Find(&list).Error
  1681. return
  1682. }
  1683. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  1684. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1685. return
  1686. }
  1687. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  1688. err = readDb.Model(&models.DrugSalesReturn{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1689. return
  1690. }
  1691. func UpDateSaleReturnStatus(id int64) (err error) {
  1692. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1693. return
  1694. }
  1695. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  1696. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1697. return
  1698. }
  1699. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  1700. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1701. return salesReturn, err
  1702. }
  1703. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  1704. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1705. return salesReturn, err
  1706. }
  1707. func EditSaleReturn(sales models.SalesReturn) {
  1708. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer}).Error
  1709. }
  1710. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  1711. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer}).Error
  1712. }
  1713. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  1714. err = writeDb.Save(&info).Error
  1715. return err
  1716. }
  1717. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  1718. err = writeDb.Save(&info).Error
  1719. return err
  1720. }
  1721. func DeleteWarehouseOut(ids []string) (err error) {
  1722. ut := writeDb.Begin()
  1723. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1724. if err != nil {
  1725. ut.Rollback()
  1726. return
  1727. }
  1728. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1729. if err != nil {
  1730. ut.Rollback()
  1731. return
  1732. }
  1733. ut.Commit()
  1734. return
  1735. }
  1736. func DeleteDrugWarehouseOut(ids []string) (err error) {
  1737. ut := writeDb.Begin()
  1738. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1739. if err != nil {
  1740. ut.Rollback()
  1741. return
  1742. }
  1743. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1744. if err != nil {
  1745. ut.Rollback()
  1746. return
  1747. }
  1748. ut.Commit()
  1749. return
  1750. }
  1751. func UpDateWarehouseOutStatus(id int64) (err error) {
  1752. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1753. return
  1754. }
  1755. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  1756. ut := writeDb.Begin()
  1757. err = ut.Save(&info).Error
  1758. if err != nil {
  1759. ut.Rollback()
  1760. return err
  1761. }
  1762. ut.Commit()
  1763. return err
  1764. }
  1765. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  1766. ut := writeDb.Begin()
  1767. err = ut.Save(&info).Error
  1768. if err != nil {
  1769. ut.Rollback()
  1770. return err
  1771. }
  1772. ut.Commit()
  1773. return err
  1774. }
  1775. //func UpDateWarehouseInfoByStockDelete(id int64,count int64) (err error) {
  1776. // err = DB.Model(&models.WarehousingInfo{}).Where("id = ?",info.ID).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  1777. // return err
  1778. //}
  1779. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  1780. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1781. return
  1782. }
  1783. func EditWarehouseOut(warehouseOut models.WarehouseOut) {
  1784. err = readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", warehouseOut.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehouse_out_time": warehouseOut.WarehouseOutTime, "dealer": warehouseOut.Dealer, "manufacturer": warehouseOut.Manufacturer}).Error
  1785. }
  1786. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  1787. err = readDb.Model(&models.DrugWarehouseOut{}).Where("id = ? AND status = 1", warehouseOut.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehouse_out_time": warehouseOut.WarehouseOutTime, "dealer": warehouseOut.Dealer, "manufacturer": warehouseOut.Manufacturer}).Error
  1788. }
  1789. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo) (err error) {
  1790. err = writeDb.Save(&info).Error
  1791. return err
  1792. }
  1793. func UpDateDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) (err error) {
  1794. err = writeDb.Save(&info).Error
  1795. return err
  1796. }
  1797. func FindAllCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.CancelStock, total int64, err error) {
  1798. db := readDb.Model(&models.CancelStock{})
  1799. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  1800. if len(keywords) > 0 {
  1801. likeKey := "%" + keywords + "%"
  1802. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  1803. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  1804. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_cancel_stock.id")
  1805. }
  1806. if startTime > 0 {
  1807. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  1808. }
  1809. if endTime > 0 {
  1810. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  1811. }
  1812. db = db.Count(&total)
  1813. offset := (page - 1) * limit
  1814. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  1815. return
  1816. }
  1817. func FindAllDrugCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugCancelStock, total int64, err error) {
  1818. db := readDb.Model(&models.DrugCancelStock{})
  1819. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  1820. if len(keywords) > 0 {
  1821. likeKey := "%" + keywords + "%"
  1822. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  1823. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  1824. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_drug_cancel_stock.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_drug_cancel_stock.id")
  1825. }
  1826. if startTime > 0 {
  1827. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  1828. }
  1829. if endTime > 0 {
  1830. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  1831. }
  1832. db = db.Count(&total)
  1833. offset := (page - 1) * limit
  1834. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  1835. return
  1836. }
  1837. func DeleteCancelStock(ids []string) (err error) {
  1838. ut := writeDb.Begin()
  1839. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1840. if err != nil {
  1841. ut.Rollback()
  1842. return
  1843. }
  1844. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1845. if err != nil {
  1846. ut.Rollback()
  1847. return
  1848. }
  1849. ut.Commit()
  1850. return
  1851. }
  1852. func DeleteDrugCancelStock(ids []string) (err error) {
  1853. ut := writeDb.Begin()
  1854. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1855. if err != nil {
  1856. ut.Rollback()
  1857. return
  1858. }
  1859. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1860. if err != nil {
  1861. ut.Rollback()
  1862. return
  1863. }
  1864. ut.Commit()
  1865. return
  1866. }
  1867. func UpDateCancleStockStatus(id int64) (err error) {
  1868. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1869. return
  1870. }
  1871. func UpDateDrugCancleStockStatus(id int64) (err error) {
  1872. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1873. return
  1874. }
  1875. func EditCancelStock(sales models.CancelStock) {
  1876. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer}).Error
  1877. }
  1878. func EditDrugCancelStock(sales models.DrugCancelStock) {
  1879. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer}).Error
  1880. }
  1881. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  1882. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  1883. return cancelStock, err
  1884. }
  1885. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  1886. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  1887. return cancelStock, err
  1888. }
  1889. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  1890. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  1891. return cancelStock, err
  1892. }
  1893. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  1894. err = writeDb.Save(&info).Error
  1895. return err
  1896. }
  1897. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  1898. err = writeDb.Save(&info).Error
  1899. return err
  1900. }
  1901. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  1902. err = readDb.Model(&models.WarehouseOut{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ? ", org_id).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  1903. return list, err
  1904. }
  1905. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  1906. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  1907. return db.Where("status = 1 AND org_id = ?", org_id).Preload("GoodsType", "status = 1 AND org_id = ? ", org_id).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id)
  1908. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  1909. return list, err
  1910. }
  1911. type StockDetailModel struct {
  1912. GoodId int64 `gorm:"good_id" json:"good_id"`
  1913. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  1914. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  1915. Price float64 `gorm:"price" json:"price"`
  1916. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  1917. Type int `gorm:"type" json:"type"`
  1918. Creater int64 `gorm:"creater" json:"creater"`
  1919. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  1920. Types int64 `gorm:"types" json:"types"`
  1921. }
  1922. func FindStockInDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.WarehousingInfo, total int64, err error) {
  1923. db := readDb.Model(&models.WarehousingInfo{})
  1924. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  1925. if len(keywords) > 0 {
  1926. likeKey := "%" + keywords + "%"
  1927. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  1928. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  1929. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  1930. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  1931. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse.manufacturer")
  1932. //db = db.Where(" sgj_user_admin_role.user_name LIKE ?", likeKey).Group("xt_warehouse_info.id")
  1933. db = db.Where(" sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_info.warehousing_order LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_info.id")
  1934. } else {
  1935. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  1936. }
  1937. if manufacturer > 0 {
  1938. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  1939. }
  1940. if order_type > 0 {
  1941. db = db.Where("xt_warehouse_info.type = ?", order_type)
  1942. }
  1943. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1944. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  1945. db = db.Count(&total)
  1946. offset := (page - 1) * limit
  1947. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.ctime desc").Find(&list).Error
  1948. return
  1949. }
  1950. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (err error, totalPrice float64) {
  1951. db := readDb.Model(&models.WarehousingInfo{})
  1952. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  1953. if len(keywords) > 0 {
  1954. likeKey := "%" + keywords + "%"
  1955. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  1956. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  1957. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  1958. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  1959. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_info.warehousing_order LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_info.id")
  1960. } else {
  1961. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  1962. }
  1963. if manufacturer > 0 {
  1964. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  1965. }
  1966. if order_type > 0 {
  1967. db = db.Where("xt_warehouse_info.type = ?", order_type)
  1968. }
  1969. type PriceAmount struct {
  1970. Total float64
  1971. }
  1972. var result []PriceAmount
  1973. err = db.Select("total_price as total").Scan(&result).Error
  1974. var total float64
  1975. for _, value := range result {
  1976. total = total + value.Total
  1977. }
  1978. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  1979. return err, total
  1980. }
  1981. func FloatRound(f float64, n int) float64 {
  1982. format := "%." + strconv.Itoa(n) + "f"
  1983. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  1984. return res
  1985. }
  1986. func FindStockOutDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  1987. db := readDb.Model(&models.WarehouseOutInfo{})
  1988. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 ", orgId)
  1989. if len(keywords) > 0 {
  1990. likeKey := "%" + keywords + "%"
  1991. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  1992. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1993. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  1994. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out_info.warehouse_out_order_number LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_out_info.id")
  1995. } else {
  1996. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  1997. }
  1998. if manufacturer > 0 {
  1999. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2000. }
  2001. if order_type > 0 {
  2002. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2003. }
  2004. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2005. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2006. db = db.Count(&total)
  2007. offset := (page - 1) * limit
  2008. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.ctime desc").Find(&list).Error
  2009. return
  2010. }
  2011. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (err error, totalPrice float64) {
  2012. db := readDb.Model(&models.WarehouseOutInfo{})
  2013. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0", orgId)
  2014. if len(keywords) > 0 {
  2015. likeKey := "%" + keywords + "%"
  2016. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  2017. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2018. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2019. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out_info.warehouse_out_order_number LIKE ?", likeKey, likeKey).Group("xt_warehouse_out_info.id")
  2020. } else {
  2021. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  2022. }
  2023. if manufacturer > 0 {
  2024. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2025. }
  2026. if order_type > 0 {
  2027. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2028. }
  2029. type PriceAmount struct {
  2030. Total float64
  2031. }
  2032. var result []PriceAmount
  2033. err = db.Select("(count * price) as total").Scan(&result).Error
  2034. var total float64
  2035. for _, value := range result {
  2036. total = total + value.Total
  2037. }
  2038. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2039. fmt.Println(total)
  2040. return err, total
  2041. }
  2042. func FindSalesReturnDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.SalesReturnInfo, total int64, err error) {
  2043. db := readDb.Model(&models.SalesReturnInfo{})
  2044. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  2045. if len(keywords) > 0 {
  2046. likeKey := "%" + keywords + "%"
  2047. db = db.Joins("join xt_sales_return on xt_sales_return.id = xt_sales_return_info.sales_return_id AND xt_sales_return.return_time >=? AND xt_sales_return.return_time<= ? AND xt_sales_return.status = 1 AND xt_sales_return.org_id = ?", startTime, endTime, orgId)
  2048. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  2049. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  2050. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_sales_return_info.order_number LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_sales_return_info.id")
  2051. } else {
  2052. db = db.Joins("join xt_sales_return on xt_sales_return.id = xt_sales_return_info.sales_return_id AND xt_sales_return.return_time >=? AND xt_sales_return.return_time<= ? AND xt_sales_return.status = 1 AND xt_sales_return.org_id = ?", startTime, endTime, orgId)
  2053. }
  2054. if manufacturer > 0 {
  2055. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  2056. }
  2057. if order_type > 0 {
  2058. db = db.Where("xt_sales_return_info.type = ?", order_type)
  2059. }
  2060. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  2061. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2062. offset := (page - 1) * limit
  2063. db = db.Count(&total)
  2064. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  2065. return
  2066. }
  2067. func FindCancelDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.CancelStockInfo, total int64, err error) {
  2068. db := readDb.Model(&models.CancelStockInfo{})
  2069. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2070. if len(keywords) > 0 {
  2071. likeKey := "%" + keywords + "%"
  2072. db = db.Joins("join xt_cancel_stock on xt_cancel_stock.id = xt_cancel_stock_info.cancel_stock_id AND xt_cancel_stock.return_time >=? AND xt_cancel_stock.return_time<= ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.org_id = ?", startTime, endTime, orgId)
  2073. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2074. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2075. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock_info.order_number LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_cancel_stock_info.id")
  2076. } else {
  2077. db = db.Joins("join xt_cancel_stock on xt_cancel_stock.id = xt_cancel_stock_info.cancel_stock_id AND xt_cancel_stock.return_time >=? AND xt_cancel_stock.return_time<= ? ", startTime, endTime)
  2078. }
  2079. if manufacturer > 0 {
  2080. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2081. }
  2082. if order_type > 0 {
  2083. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2084. }
  2085. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2086. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2087. db = db.Count(&total)
  2088. offset := (page - 1) * limit
  2089. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  2090. return
  2091. }
  2092. func FindStockInByDealerId(id int64) (total int64, err error) {
  2093. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  2094. return
  2095. }
  2096. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  2097. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  2098. return
  2099. }
  2100. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  2101. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  2102. return
  2103. }
  2104. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  2105. readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 AND specification_name = ? AND good_type_id = ? ", org_id, name, good_type_id).Count(&total)
  2106. return
  2107. }
  2108. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  2109. readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 AND specification_name = ? AND good_type_id = ? and good_name = ?", org_id, name, good_type_id, good_name).Count(&total)
  2110. return
  2111. }
  2112. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  2113. readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 AND specification_name = ? AND good_type_id = ? and id <> ? and good_name = ? ", org_id, name, good_type_id, id, goodname).Count(&total)
  2114. return
  2115. }
  2116. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  2117. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  2118. return goodType, err
  2119. }
  2120. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  2121. err = readDb.Model(&models.WarehousingInfo{}).Preload("GoodInfo", " org_id = ? AND good_type_id = ? AND status = 1 AND specification_name <> '' ", org_id, good_type_id).Where(" org_id = ? AND good_type_id = ? AND status = 1 ", org_id, good_type_id).Find(&info).Error
  2122. return info, err
  2123. }
  2124. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  2125. err = readDb.Model(&models.WarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ?", is_sys, org_id, record_time).First(&out).Error
  2126. return
  2127. }
  2128. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2129. err = readDb.Model(&models.DrugWarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ?", is_sys, org_id, record_time).First(&out).Error
  2130. return
  2131. }
  2132. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  2133. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  2134. return
  2135. }
  2136. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  2137. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  2138. return
  2139. }
  2140. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2141. info := models.WarehouseOutInfo{}
  2142. err := XTReadDB().Model(&info).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and patient_id = ? and org_id = ? and status = 1", goodid, goodtypeid, recordtime, patientid, orgid).Find(&info).Error
  2143. if err == gorm.ErrRecordNotFound {
  2144. return nil, err
  2145. }
  2146. if err != nil {
  2147. return nil, err
  2148. }
  2149. return &info, nil
  2150. }
  2151. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2152. info := models.WarehouseOutInfo{}
  2153. err := XTReadDB().Model(&info).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and org_id = ? and status = 1", goodid, goodtypeid, recordtime, orgid).Find(&info).Error
  2154. if err == gorm.ErrRecordNotFound {
  2155. return nil, err
  2156. }
  2157. if err != nil {
  2158. return nil, err
  2159. }
  2160. return &info, nil
  2161. }
  2162. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2163. err := XTWriteDB().Model(&prepare).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and patient_id = ? and org_id = ? and status =1", goodid, goodtypeid, recordtime, patientid, orgid).Updates(map[string]interface{}{"count": prepare.Count}).Error
  2164. return err
  2165. }
  2166. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2167. err := XTWriteDB().Model(&prepare).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and org_id = ? and status =1", goodid, goodtypeid, recordtime, orgid).Updates(map[string]interface{}{"count": prepare.Count}).Error
  2168. return err
  2169. }
  2170. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo) error {
  2171. err := writeDb.Create(&info).Error
  2172. return err
  2173. }
  2174. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  2175. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  2176. return err
  2177. }
  2178. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  2179. ut := writeDb.Begin()
  2180. err := ut.Create(&info).Error
  2181. if err != nil {
  2182. ut.Rollback()
  2183. return err
  2184. }
  2185. ut.Commit()
  2186. return err
  2187. }
  2188. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  2189. ut := writeDb.Begin()
  2190. err := ut.Create(&detail).Error
  2191. if err != nil {
  2192. ut.Rollback()
  2193. return err
  2194. }
  2195. ut.Commit()
  2196. return err
  2197. }
  2198. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  2199. err := writeDb.Create(&detail).Error
  2200. return err
  2201. }
  2202. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  2203. err = readDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND (warehouse_out_id = ? or warehouse_out_order_number = ?)", org_id, good_type_id, good_id, out_id, number).First(&out).Error
  2204. return
  2205. }
  2206. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  2207. err = readDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND sys_record_time = ? AND patient_id = ?", org_id, good_type_id, good_id, record_date, patient_id).Order("ctime desc").Find(&out).Error
  2208. return
  2209. }
  2210. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  2211. err = readDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND sys_record_time = ?", org_id, good_type_id, good_id, record_date).Order("ctime desc").Find(&out).Error
  2212. return
  2213. }
  2214. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  2215. info := models.WarehouseOutInfo{}
  2216. err = readDb.Model(&info).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND sys_record_time = ? and patient_id = ?", org_id, good_type_id, good_id, record_date, patient_id).Order("ctime desc").Find(&info).Error
  2217. return info, err
  2218. }
  2219. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  2220. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ? AND is_sys = 1 AND (warehouse_out_id = ? or warehouse_out_order_number = ?)", org_id, drug_id, out_id, number).First(&out).Error
  2221. return
  2222. }
  2223. func UpdateStockOutInfoCount(org_id int64, id int64) {
  2224. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  2225. }
  2226. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  2227. db := readDb.Model(&models.AutomaticReduceDetail{})
  2228. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  2229. db = db.Preload("GoodsType", "status = 1")
  2230. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  2231. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  2232. db = db.Count(&total)
  2233. err = db.Find(&user).Error
  2234. return
  2235. }
  2236. type DrugAutomaticReduceDetail struct {
  2237. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2238. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  2239. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  2240. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  2241. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2242. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2243. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2244. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  2245. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2246. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2247. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2248. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  2249. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2250. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  2251. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  2252. }
  2253. func (DrugAutomaticReduceDetail) TableName() string {
  2254. return "xt_drug_automatic_reduce_detail"
  2255. }
  2256. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  2257. db := readDb.Model(&DrugAutomaticReduceDetail{})
  2258. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  2259. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  2260. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  2261. db = db.Count(&total)
  2262. err = db.Find(&user).Error
  2263. return
  2264. }
  2265. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  2266. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  2267. }
  2268. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  2269. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  2270. }
  2271. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  2272. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("org_id = ?AND status = 1 AND good_id = ? AND good_type_id = ? AND patient_id = ? AND record_time = ? AND count <> 0", org_id, good_id, good_type_id, patient_id, record_time).Count(&count).Error
  2273. return
  2274. }
  2275. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  2276. err = readDb.Model(&models.DrugAutomaticReduceDetail{}).Where("org_id = ? AND status = 1 AND drug_id = ? AND patient_id = ? AND record_time = ?", org_id, drug_id, patient_id, record_time).Count(&count).Error
  2277. return
  2278. }
  2279. type VMWarehousingInfo struct {
  2280. ID int64 `gorm:"column:id" json:"id"`
  2281. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  2282. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  2283. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  2284. Status int64 `gorm:"column:status" json:"status"`
  2285. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2286. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  2287. }
  2288. func (VMWarehousingInfo) TableName() string {
  2289. return "xt_warehouse_info"
  2290. }
  2291. type GoodInfo struct {
  2292. ID int64 `gorm:"column:id" json:"id"`
  2293. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  2294. Status int64 `gorm:"column:status" json:"status"`
  2295. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2296. }
  2297. func (GoodInfo) TableName() string {
  2298. return "xt_good_information"
  2299. }
  2300. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  2301. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  2302. return
  2303. }
  2304. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  2305. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND record_date = ? AND status = 1 AND count <> 0 ", patient_id, orgId, record_time).Find(&goodUser).Error
  2306. return
  2307. }
  2308. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  2309. likeKey := "%" + keyword + "%"
  2310. db := XTReadDB().Model(&goods)
  2311. if len(keyword) > 0 {
  2312. db = db.Where("type_name like ?", likeKey)
  2313. }
  2314. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  2315. return goods, err
  2316. }
  2317. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  2318. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3)", orgid).
  2319. Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&goodstype).Error
  2320. return goodstype, err
  2321. }
  2322. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  2323. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  2324. if orgid > 0 {
  2325. db = db.Where("x.user_org_id = ?", orgid)
  2326. }
  2327. if recordtime > 0 {
  2328. db = db.Where("x.record_date = ?", recordtime)
  2329. }
  2330. if patientId > 0 {
  2331. db = db.Where("x.patient_id = ?", patientId)
  2332. }
  2333. err = db.Select("x.id,x.user_org_id,x.patient_id,x.record_date,x.good_id,x.good_type_id,x.count,x.commdity_code,t.specification_name").Joins("left join xt_good_information as t on t.id = x.good_id and t.org_id = ? and t.status = 1", orgid).Scan(&prepare).Error
  2334. return prepare, err
  2335. }
  2336. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  2337. detail := models.XtAutomaticReduceDetail{}
  2338. err := XTReadDB().Model(&detail).Where("org_id = ? and record_time = ? and good_id = ? and good_type_id = ? and status =1", orgid, recordtime, goodid, goodtypeid).Find(&detail).Error
  2339. if err == gorm.ErrRecordNotFound {
  2340. return nil, err
  2341. }
  2342. if err != nil {
  2343. return nil, err
  2344. }
  2345. return &detail, nil
  2346. }
  2347. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  2348. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and x.record_time = ? and `status` = 1) as b GROUP BY good_id", orgid, wareouttime).Scan(&autoMatic).Error
  2349. return autoMatic, err
  2350. }
  2351. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  2352. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and x.record_time >= ? and x.record_time<=? and `status` = 1) as b GROUP BY good_id", orgid, startime, endtime).Scan(&autoMatic).Error
  2353. return autoMatic, err
  2354. }
  2355. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  2356. var manufacturer models.Manufacturer
  2357. var err error
  2358. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  2359. if err == gorm.ErrRecordNotFound {
  2360. return nil, err
  2361. }
  2362. if err != nil {
  2363. return nil, err
  2364. }
  2365. return &manufacturer, nil
  2366. }
  2367. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  2368. err := XTWriteDB().Create(manufacturer).Error
  2369. return err
  2370. }
  2371. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  2372. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  2373. return err
  2374. }
  2375. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  2376. var good models.GoodInfo
  2377. var err error
  2378. err = XTReadDB().Model(&good).Where("good_name = ? and good_type_id = ? and specification_name = ? and org_id = ? and status = 1", goodName, goodType, specificationName, orgid).Find(&good).Error
  2379. if err == gorm.ErrRecordNotFound {
  2380. return nil, err
  2381. }
  2382. if err != nil {
  2383. return nil, err
  2384. }
  2385. return &good, nil
  2386. }
  2387. func CreateGoodsInfomation(info *models.GoodInfo) error {
  2388. err := XTWriteDB().Create(&info).Error
  2389. return err
  2390. }
  2391. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  2392. err := XTWriteDB().Model(&info).Where("good_name = ? and good_type_id = ? and specification_name = ? and org_id = ? and status = 1", goodName, goodType, specificationName, orgid).Updates(map[string]interface{}{"good_name": info.GoodName, "good_type_id": info.GoodTypeId, "medical_insurance_level": info.MedicalInsuranceLevel, "good_kind": info.GoodKind, "specification_name": info.SpecificationName, "manufacturer": info.Manufacturer, "good_unit": info.GoodUnit, "retail_price": info.RetailPrice, "stock_warn_count": info.StockWarnCount, "dealer": info.Dealer, "pinyin": info.Pinyin, "wubi": info.Wubi, "buy_price": info.BuyPrice, "medical_insurance_number": info.MedicalInsuranceNumber, "is_special_diseases": info.IsSpecialDiseases, "is_record": info.IsRecord, "statistics_category": info.StatisticsCategory, "good_status": info.GoodStatus, "social_security_directory_code": info.SocialSecurityDirectoryCode, "production_type": info.ProductionType, "special_medical": info.SpecialMedical, "remark": info.Remark}).Error
  2393. return err
  2394. }
  2395. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  2396. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  2397. err = db.Select("x.good_id,Sum(x.count) as count,x.remark,x.price,x.org_id").Where("x.ctime >=? and x.ctime<=? and x.org_id = ?", startime, endtime, orgid).Group("x.good_id").Scan(&houseOutInfo).Error
  2398. return houseOutInfo, err
  2399. }
  2400. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  2401. key := "%" + keyword + "%"
  2402. if len(keyword) == 0 {
  2403. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&goodInfo).Error
  2404. } else {
  2405. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ?", orgid, key).Find(&goodInfo).Error
  2406. }
  2407. return goodInfo, err
  2408. }
  2409. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  2410. var drug models.BaseDrugLib
  2411. var err error
  2412. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  2413. if err == gorm.ErrRecordNotFound {
  2414. return nil, err
  2415. }
  2416. if err != nil {
  2417. return nil, err
  2418. }
  2419. return &drug, nil
  2420. }
  2421. func IsExistDrugByNameOne(drugname string, dosage float64, maxunit string, unival int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  2422. var drug models.BaseDrugLib
  2423. var err error
  2424. err = XTReadDB().Model(&drug).Where("drug_name = ? and dosage = ? and max_unit = ? and unval = ? and min_unit=? and packing_unit = ? and status =1 and org_id = ?", drugname, dosage, maxunit, unival, minunit, packtunit, orgid).Find(&drug).Error
  2425. if err == gorm.ErrRecordNotFound {
  2426. return nil, err
  2427. }
  2428. if err != nil {
  2429. return nil, err
  2430. }
  2431. return &drug, nil
  2432. }
  2433. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  2434. err := XTWriteDB().Create(&info).Error
  2435. return err
  2436. }
  2437. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  2438. err := XTWriteDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and org_id = ? and status = 1", drugName, drugSpec, orgid).Updates(map[string]interface{}{"drug_name": drug.DrugName, "drug_alias": drug.DrugAlias, "drug_spec": drug.DrugSpec, "drug_type": drug.DrugType, "drug_stock_limit": drug.DrugStockLimit, "drug_origin_place": drug.DrugOriginPlace, "drug_dosage_form": drug.DrugDosageForm, "medical_insurance_level": drug.MedicalInsuranceLevel, "max_unit": drug.MaxUnit, "min_unit": drug.MinUnit, "unit_matrixing": drug.UnitMatrixing, "retail_price": drug.RetailPrice, "last_price": drug.LastPrice, "drug_classify": drug.DrugClassify, "manufacturer": drug.Manufacturer, "dealer": drug.Dealer, "pinyin": drug.Pinyin, "wubi": drug.Wubi, "drug_alias_pinyin": drug.DrugAliasPinyin, "drug_alias_wubi": drug.DrugAliasWubi, "drug_category": drug.DrugCategory, "drug_control": drug.DrugControl, "number": drug.Number, "hosp_appr_flag": drug.HospApprFlag, "medical_insurance_number": drug.MedicalInsuranceNumber, "pharmacology_category": drug.PharmacologyCategory, "statistics_category": drug.StatisticsCategory, "code": drug.Code, "is_special_diseases": drug.IsSpecialDiseases, "is_record": drug.IsRecord, "prescription_mark": drug.PrescriptionMark, "social_security_directory_code": drug.SocialSecurityDirectoryCode, "record_date": drug.RecordDate, "drug_remark": drug.DrugRemark, "drug_status": drug.DrugStatus, "limit_remark": drug.LimitRemark, "drug_dose": drug.DrugDose, "drug_dose_unit": drug.DrugDoseUnit, "lmt_used_flag": drug.LmtUsedFlag, "delivery_way": drug.DeliveryWay, "execution_frequency": drug.ExecutionFrequency, "drug_day": drug.DrugDay}).Error
  2439. return err
  2440. }
  2441. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  2442. var projoect models.HisProject
  2443. var err error
  2444. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  2445. if err == gorm.ErrRecordNotFound {
  2446. return nil, err
  2447. }
  2448. if err != nil {
  2449. return nil, err
  2450. }
  2451. return &projoect, nil
  2452. }
  2453. func CreateProjectInfomation(info *models.HisProject) error {
  2454. err := XTWriteDB().Create(&info).Error
  2455. return err
  2456. }
  2457. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  2458. err := XTWriteDB().Model(&project).Where("project_name = ? and user_org_id = ? and status = 1", projectName, orgid).Updates(map[string]interface{}{"project_name": project.ProjectName, "price": project.Price, "unit": project.Unit, "cost_classify": project.CostClassify, "executive_section": project.ExecutiveSection, "medical_coverage": project.MedicalCoverage, "pinyin": project.Pinyin, "wubi": project.Wubi, "statistical_classification": project.StatisticalClassification, "disease_directory": project.DiseaseDirectory, "is_record": project.IsRecord, "medical_code": project.MedicalCode, "tube_color": project.TubeColor, "single_dose": project.SingleDose, "delivery_way": project.DeliveryWay, "execution_frequency": project.ExecutionFrequency, "number_days": project.NumberDays, "total": project.Total, "medical_status": project.MedicalStatus, "category": project.Category, "specail_project": project.SpecailProject, "social_security_directory_code": project.SocialSecurityDirectoryCode, "record_date": project.RecordDate, "remark": project.Remark}).Error
  2459. return err
  2460. }
  2461. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  2462. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  2463. return warehouseinfo, err
  2464. }
  2465. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) {
  2466. err = readDb.Model(&models.DrugWarehouse{}).Where("warehousing_order = ? AND status = 1 and id = ?", warehouse.WarehousingOrder, id).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehousing_time": warehouse.WarehousingTime, "modifier": warehouse.Modifier, "dealer": warehouse.Dealer, "manufacturer": warehouse.Manufacturer}).Error
  2467. }
  2468. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  2469. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  2470. return list, err
  2471. }
  2472. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  2473. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  2474. return list, err
  2475. }
  2476. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  2477. var dealer models.Dealer
  2478. var err error
  2479. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  2480. if err == gorm.ErrRecordNotFound {
  2481. return nil, err
  2482. }
  2483. if err != nil {
  2484. return nil, err
  2485. }
  2486. return &dealer, nil
  2487. }
  2488. func CreateDealer(dealer *models.Dealer) error {
  2489. err := XTWriteDB().Create(&dealer).Error
  2490. return err
  2491. }
  2492. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  2493. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  2494. return err
  2495. }
  2496. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  2497. dataconfig := models.DictDataconfig{}
  2498. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  2499. return dataconfig, err
  2500. }
  2501. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  2502. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  2503. return dataconfig, err
  2504. }
  2505. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  2506. var dataconfig models.DictDataconfig
  2507. var err error
  2508. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  2509. if err == gorm.ErrRecordNotFound {
  2510. return nil, err
  2511. }
  2512. if err != nil {
  2513. return nil, err
  2514. }
  2515. return &dataconfig, nil
  2516. }
  2517. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  2518. dataconfig := models.DictDataconfig{}
  2519. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  2520. return dataconfig, err
  2521. }
  2522. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  2523. err := XTWriteDB().Create(&dataconfig).Error
  2524. return err
  2525. }
  2526. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  2527. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  2528. return err
  2529. }
  2530. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  2531. var goodType models.GoodsType
  2532. var err error
  2533. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  2534. if err == gorm.ErrRecordNotFound {
  2535. return nil, err
  2536. }
  2537. if err != nil {
  2538. return nil, err
  2539. }
  2540. return &goodType, nil
  2541. }
  2542. func CreatedGoodType(goodsType *models.GoodsType) error {
  2543. err := XTWriteDB().Create(&goodsType).Error
  2544. return err
  2545. }
  2546. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  2547. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  2548. return goodsType, err
  2549. }
  2550. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  2551. var department models.XtHisDepartment
  2552. var err error
  2553. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  2554. if err == gorm.ErrRecordNotFound {
  2555. return nil, err
  2556. }
  2557. if err != nil {
  2558. return nil, err
  2559. }
  2560. return &department, nil
  2561. }
  2562. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  2563. dataconfig := models.Dataconfig{}
  2564. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  2565. return dataconfig, err
  2566. }
  2567. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  2568. var dataconfig models.Dataconfig
  2569. var err error
  2570. err = XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and name = ? and status = 1", parentid, orgid, name).Find(&dataconfig).Error
  2571. if err == gorm.ErrRecordNotFound {
  2572. return nil, err
  2573. }
  2574. if err != nil {
  2575. return nil, err
  2576. }
  2577. return &dataconfig, nil
  2578. }
  2579. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  2580. dataconfig := models.Dataconfig{}
  2581. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  2582. return dataconfig, err
  2583. }
  2584. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  2585. err := XTWriteDB().Create(&dataconfig).Error
  2586. return err
  2587. }
  2588. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  2589. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  2590. return dataconfig, err
  2591. }
  2592. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  2593. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  2594. return list, err
  2595. }
  2596. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  2597. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 ", orgid)
  2598. if len(keywords) > 0 {
  2599. likekey := "%" + keywords + "%"
  2600. err = db.Where("(drug_name LIKE ?)", likekey).Find(&drug).Error
  2601. } else {
  2602. err = db.Find(&drug).Error
  2603. }
  2604. return
  2605. }
  2606. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  2607. db := XTReadDB().Model(&good).Where("org_id =? and status = 1", orgid)
  2608. if len(keywords) > 0 {
  2609. likekey := "%" + keywords + "%"
  2610. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2611. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  2612. }).Find(&good).Error
  2613. } else {
  2614. err = db.Find(&good).Error
  2615. }
  2616. return
  2617. }
  2618. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  2619. db := writeDb.Begin()
  2620. err := db.Create(&detail).Error
  2621. if err != nil {
  2622. db.Rollback()
  2623. return err
  2624. }
  2625. db.Commit()
  2626. return err
  2627. }
  2628. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  2629. var autoreduece models.BloodAutomaticReduceDetail
  2630. var err error
  2631. err = XTReadDB().Model(&autoreduece).Where("org_id = ? and patient_id = ? and status = 1 and record_time = ? and good_id = ? and good_type_id = ? and count<> 0", orgid, patient_id, recordtime, goodid, goodtypeid).Find(&autoreduece).Error
  2632. if err == gorm.ErrRecordNotFound {
  2633. return nil, err
  2634. }
  2635. if err != nil {
  2636. return nil, err
  2637. }
  2638. return &autoreduece, nil
  2639. }
  2640. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  2641. detail := models.BloodAutomaticReduceDetail{}
  2642. err := XTWriteDB().Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? and good_type_id = ?", orgid, patient_id, recordtime, goodid, goodtypeid).Updates(map[string]interface{}{"status": 0}).Error
  2643. return err
  2644. }
  2645. func UpDateWarehouseInfoByStockDelete(id int64, count int64) (err error) {
  2646. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  2647. return err
  2648. }
  2649. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  2650. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  2651. return err
  2652. }
  2653. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  2654. var autoreduece models.BloodAutomaticReduceDetail
  2655. var err error
  2656. err = XTReadDB().Model(&autoreduece).Where("good_id = ? and good_type_id = ? and patient_id = ? and org_id = ? and status = 1 and record_time = ?", goodid, goodtypeid, patientid, orgid, recorddate).Find(&autoreduece).Error
  2657. if err == gorm.ErrRecordNotFound {
  2658. return nil, err
  2659. }
  2660. if err != nil {
  2661. return nil, err
  2662. }
  2663. return &autoreduece, nil
  2664. }
  2665. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  2666. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  2667. return
  2668. }
  2669. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  2670. offset := (page - 1) * limit
  2671. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  2672. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  2673. fmt.Println(table)
  2674. if startime > 0 {
  2675. db = db.Where("x.ctime>=?", startime)
  2676. }
  2677. if endtime > 0 {
  2678. db = db.Where("x.ctime<=?", endtime)
  2679. }
  2680. likeKey := "%" + keyword + "%"
  2681. if len(likeKey) > 0 {
  2682. db = db.Where("t.good_name like ?", likeKey)
  2683. }
  2684. if orgid > 0 {
  2685. db = db.Where("x.org_id = ?", orgid)
  2686. }
  2687. if good_type > 0 {
  2688. db = db.Where("t.good_type_id = ?", good_type)
  2689. }
  2690. err = db.Select("x.id,x.warehousing_id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehousing_unit,x.stock_count,x.price,x.total_price,x.dealer,x.remark,x.ctime,x.is_return,x.warehousing_order,x.type,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.manufacturer").Joins("left join xt_good_information as t on t.id = x.good_id").Group("x.good_id").Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Scan(&info).Error
  2691. return info, total, err
  2692. }
  2693. func GetAllGoodInfoStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.GoodInfo, total int64, err error) {
  2694. offset := (page - 1) * limit
  2695. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  2696. likeKey := "%" + keyword + "%"
  2697. if len(likeKey) > 0 {
  2698. db = db.Where("x.good_name like ?", likeKey)
  2699. }
  2700. if orgid > 0 {
  2701. db = db.Where("x.org_id = ?", orgid)
  2702. }
  2703. if good_type > 0 {
  2704. db = db.Where("x.good_type_id = ?", good_type)
  2705. }
  2706. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", "status= 1 and org_id =? and ctime>=? and ctime<=?", orgid, startime, endtime).Preload("CancelStockInfo", "status= 1 and org_id =? and ctime>=? and ctime<=?", orgid, startime, endtime).Find(&info).Error
  2707. return info, total, err
  2708. }
  2709. func GetStockListById(good_id int64, orgid int64, limit int64, page int64, startime int64, endtime int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  2710. offset := (page - 1) * limit
  2711. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  2712. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  2713. fmt.Println(table)
  2714. if startime > 0 {
  2715. db = db.Where("x.ctime>=?", startime)
  2716. }
  2717. if endtime > 0 {
  2718. db = db.Where("x.ctime<=?", endtime)
  2719. }
  2720. if orgid > 0 {
  2721. db = db.Where("x.org_id = ?", orgid)
  2722. }
  2723. if good_id > 0 {
  2724. db = db.Where("x.good_id = ?", good_id)
  2725. }
  2726. err = db.Select("x.id,x.warehousing_id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehousing_unit,x.stock_count,x.price,x.total_price,x.dealer,t.manufacturer,x.remark,x.ctime,x.is_return,x.warehousing_order,x.type,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.min_unit,t.buy_price").Joins("left join xt_good_information as t on t.id = x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&info).Error
  2727. return info, total, err
  2728. }
  2729. func GetStockOutList(good_id int64, orgid int64, limit int64, page int64, startime int64, endtime int64, is_sys int64) (info []*models.WarehouseOutInfo, total int64, err error) {
  2730. offset := (page - 1) * limit
  2731. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  2732. if startime > 0 {
  2733. db = db.Where("x.ctime>=?", startime)
  2734. }
  2735. if endtime > 0 {
  2736. db = db.Where("x.ctime<=?", endtime)
  2737. }
  2738. if orgid > 0 {
  2739. db = db.Where("x.org_id = ?", orgid)
  2740. }
  2741. if good_id > 0 {
  2742. db = db.Where("x.good_id = ?", good_id)
  2743. }
  2744. if is_sys == 2 {
  2745. db = db.Where("x.is_sys = 1")
  2746. }
  2747. if is_sys == 3 {
  2748. db = db.Where("x.is_sys = 0")
  2749. }
  2750. err = db.Select("x.id,x.warehouse_out_id,x.warehouse_info_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.is_sys,x.number,x.dealer,x.manufacturer,x.consumable_type").Offset(offset).Count(&total).Scan(&info).Error
  2751. return info, total, err
  2752. }
  2753. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  2754. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  2755. if startime > 0 {
  2756. db = db.Where("x.ctime >=?", startime)
  2757. }
  2758. if endtime > 0 {
  2759. db = db.Where("x.ctime<=?", endtime)
  2760. }
  2761. if orgid > 0 {
  2762. db = db.Where("x.org_id = ?", orgid)
  2763. }
  2764. err = db.Select("sum(x.warehousing_count) as count,sum(x.stock_count) as stock_count,x.good_id").Group("x.good_id").Scan(&info).Error
  2765. return info, err
  2766. }
  2767. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  2768. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  2769. if startime > 0 {
  2770. db = db.Where("x.ctime >=?", startime)
  2771. }
  2772. if endtime > 0 {
  2773. db = db.Where("x.ctime<=?", endtime)
  2774. }
  2775. if orgid > 0 {
  2776. db = db.Where("x.org_id = ?", orgid)
  2777. }
  2778. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  2779. return info, err
  2780. }
  2781. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  2782. if startime > 0 {
  2783. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and x.record_time >=? and `status` = 1) as b GROUP BY good_id", orgid, startime).Scan(&autoMatic).Error
  2784. } else {
  2785. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and `status` = 1) as b GROUP BY good_id", orgid).Scan(&autoMatic).Error
  2786. }
  2787. if endtime > 0 {
  2788. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and x.record_time <=? and `status` = 1) as b GROUP BY good_id", orgid, endtime).Scan(&autoMatic).Error
  2789. } else {
  2790. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and `status` = 1) as b GROUP BY good_id", orgid).Scan(&autoMatic).Error
  2791. }
  2792. return autoMatic, err
  2793. }
  2794. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  2795. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  2796. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  2797. fmt.Println(table)
  2798. if id > 0 {
  2799. db = db.Where("x.warehouse_out_id = ?", id)
  2800. }
  2801. if orgid > 0 {
  2802. db = db.Where("x.org_id = ?", orgid)
  2803. }
  2804. err = db.Select("x.id,x.warehouse_out_id,x.warehouse_info_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,t.manufacturer,x.is_sys,x.sys_record_time,x.number,x.remark,x.license_number,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.good_id").Order("x.ctime desc").Scan(&out).Error
  2805. return out, err
  2806. }
  2807. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  2808. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  2809. if len(id) > 0 {
  2810. db = db.Where("x.id in(?)", id)
  2811. }
  2812. if orgid > 0 {
  2813. db = db.Where("x.org_id= ?", orgid)
  2814. }
  2815. err = db.Select("x.id,x.warehouse_out_order_number,x.operation_time,x.creater,x.org_id,x.modifier,x.remark,x.warehouse_out_time,x.dealer,x.manufacturer,x.type").Find(&out).Error
  2816. return out, err
  2817. }
  2818. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  2819. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  2820. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  2821. fmt.Println(table)
  2822. if len(id) > 0 {
  2823. db = db.Where("x.warehouse_out_id in (?)", id)
  2824. }
  2825. if orgid > 0 {
  2826. db = db.Where("x.org_id = ?", orgid)
  2827. }
  2828. err = db.Select("x.id,x.warehouse_out_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,t.manufacturer,x.is_sys,x.sys_record_time,x.number,x.remark,x.license_number,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.warehouse_out_id,x.good_id").Order("x.ctime desc").Scan(&out).Error
  2829. return out, err
  2830. }
  2831. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  2832. db := XTReadDB().Model(&info)
  2833. db = db.Preload("GoodInfo", "status = 1")
  2834. err = db.Raw("select * from(SELECT * FROM xt_warehouse_out_info WHERE warehouse_out_id IN(?) AND org_id = ? and status = 1 ORDER BY id DESC LIMIT 1000) a group by good_id", ids, orgId).Find(&info).Error
  2835. return info, err
  2836. }
  2837. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  2838. db := readDb.Model(&models.AutomaticReduceDetail{})
  2839. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  2840. db = db.Preload("GoodsType", "status = 1")
  2841. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  2842. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  2843. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? ", org_id, good_id, record_time)
  2844. db = db.Count(&total)
  2845. err = db.Find(&user).Error
  2846. return
  2847. }
  2848. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  2849. info := models.GoodInfo{}
  2850. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  2851. return info, err
  2852. }
  2853. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  2854. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  2855. return err
  2856. }
  2857. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  2858. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  2859. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  2860. fmt.Println(table)
  2861. if orgid > 0 {
  2862. db = db.Where("x.org_id = ?", orgid)
  2863. }
  2864. if len(orderid) > 0 {
  2865. db = db.Where("x.warehousing_id in(?)", orderid)
  2866. }
  2867. if startime > 0 {
  2868. db = db.Where("x.ctime>=?", startime)
  2869. }
  2870. if endtime > 0 {
  2871. db = db.Where("x.ctime<=?", endtime)
  2872. }
  2873. err = db.Select("x.id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.dealer,x.manufacturer,x.remark,x.license_number,t.good_name,t.packing_unit,t.specification_name").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&info).Error
  2874. return info, err
  2875. }
  2876. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  2877. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  2878. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  2879. fmt.Println(table)
  2880. if startime > 0 {
  2881. db = db.Where("x.ctime>=?", startime)
  2882. }
  2883. if endtime > 0 {
  2884. db = db.Where("x.ctime<=?", endtime)
  2885. }
  2886. if orgid > 0 {
  2887. db = db.Where("x.org_id = ?", orgid)
  2888. }
  2889. err = db.Select("x.drug_id,x.count,x.count_unit,t.max_unit,t.min_unit,t.min_number").Joins("left join xt_base_drug as t on t.id = x.drug_id").Scan(&info).Error
  2890. return info, err
  2891. }
  2892. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  2893. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  2894. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  2895. fmt.Println(table)
  2896. if orgid > 0 {
  2897. db = db.Where("x.org_id = ?", orgid)
  2898. }
  2899. if len(ids) > 0 {
  2900. db = db.Where("x.warehouse_out_id in (?)", ids)
  2901. }
  2902. if startime > 0 {
  2903. db = db.Where("x.ctime>=?", startime)
  2904. }
  2905. if endtime > 0 {
  2906. db = db.Where("x.ctime<=?", endtime)
  2907. }
  2908. err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.number,x.license_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,t.manufacturer,t.dealer").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&out).Error
  2909. return out, err
  2910. }
  2911. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  2912. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  2913. if len(ids) > 0 {
  2914. db = db.Where("x.id in(?)", ids)
  2915. }
  2916. if orgid > 0 {
  2917. db = db.Where("x.org_id = ?", orgid)
  2918. }
  2919. err = db.Select("x.id,x.warehouse_out_order_number,x.operation_time,x.creater,x.org_id,x.modifier,x.remark,x.warehouse_out_time,x.is_sys").Scan(&info).Error
  2920. return info, err
  2921. }
  2922. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  2923. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  2924. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  2925. fmt.Println(table)
  2926. if len(ids) > 0 {
  2927. db = db.Where("x.warehouse_out_id in(?)", ids)
  2928. }
  2929. if orgid > 0 {
  2930. db = db.Where("x.org_id = ?", orgid)
  2931. }
  2932. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.warehousing_out_target,x.count,x.count_unit,x.price,x.product_date,x.expiry_date,x.number,x.batch_number,t.drug_type,t.max_unit,t.min_unit,t.min_number,t.dose,t.dose_unit,t.last_price,t.drug_name").Joins("left join xt_base_drug as t on t.id =x.drug_id").Scan(&info).Error
  2933. return info, err
  2934. }
  2935. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  2936. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  2937. likeKey := "%" + keyword + "%"
  2938. if len(keyword) > 0 {
  2939. db = db.Where("x.drug_name like ?", likeKey)
  2940. }
  2941. if orgid > 0 {
  2942. db = db.Where("x.org_id = ?", orgid)
  2943. }
  2944. err = db.Find(&basedrug).Error
  2945. return basedrug, err
  2946. }
  2947. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  2948. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  2949. if len(ids) > 0 {
  2950. db = db.Where("x.drug_id in(?)", ids)
  2951. }
  2952. if orgid > 0 {
  2953. db = db.Where("x.org_id = ?", orgid)
  2954. }
  2955. if startime > 0 {
  2956. db = db.Where("x.ctime>=?", startime)
  2957. }
  2958. if endtime > 0 {
  2959. db = db.Where("x.ctime<=?", endtime)
  2960. }
  2961. err = db.Find(&info).Error
  2962. return info, err
  2963. }
  2964. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseInfo, err error) {
  2965. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  2966. if len(ids) > 0 {
  2967. db = db.Where("x.drug_id in(?)", ids)
  2968. }
  2969. if orgid > 0 {
  2970. db = db.Where("x.org_id = ?", orgid)
  2971. }
  2972. if startime > 0 {
  2973. db = db.Where("x.ctime>=?", startime)
  2974. }
  2975. if endtime > 0 {
  2976. db = db.Where("x.ctime<=?", endtime)
  2977. }
  2978. err = db.Find(&info).Error
  2979. return info, err
  2980. }
  2981. func GetDrugTotalCount(drugid int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  2982. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  2983. if drugid > 0 {
  2984. db = db.Where("x.drug_id = ?", drugid)
  2985. }
  2986. if orgid > 0 {
  2987. db = db.Where("x.org_id = ?", orgid)
  2988. }
  2989. err = db.Select("x.drug_id,sum(x.stock_max_number) as count,sum(x.stock_min_number) as stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  2990. return info, err
  2991. }
  2992. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  2993. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  2994. if id > 0 {
  2995. db = db.Where("x.id = ?", id)
  2996. }
  2997. if orgid > 0 {
  2998. db = db.Where("x.org_id = ?", orgid)
  2999. }
  3000. err = db.Select("x.drug_id,sum(x.stock_max_number) as count,sum(x.stock_min_number) as stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  3001. return info, err
  3002. }
  3003. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  3004. info := models.VmDrugWarehouseInfo{}
  3005. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3006. if id == 0 {
  3007. db = db.Where("id = ?", id)
  3008. }
  3009. if drugid > 0 {
  3010. db = db.Where("x.drug_id = ?", drugid)
  3011. }
  3012. if orgid > 0 {
  3013. db = db.Where("x.org_id = ?", orgid)
  3014. }
  3015. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  3016. return info, err
  3017. }
  3018. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  3019. lib := models.BaseDrugLib{}
  3020. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  3021. return lib, err
  3022. }
  3023. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  3024. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3025. if goodid > 0 {
  3026. db = db.Where("x.good_id = ?", goodid)
  3027. }
  3028. if orgid > 0 {
  3029. db = db.Where("x.org_id = ?", orgid)
  3030. }
  3031. err = db.Select("x.id,x.warehousing_id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.total_price,x.dealer,x.manufacturer,x.remark,x.is_return,x.warehousing_order,x.warehousing_unit,x.stock_count,x.license_number,x.org_id").Find(&info).Error
  3032. return info, err
  3033. }
  3034. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  3035. err := XTWriteDB().Model(models.WarehousingInfo{}).Where("good_id = ? and org_id = ? and status = 1 and warehousing_id = ?", goodid, orgid, warehousingId).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3036. return err
  3037. }
  3038. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  3039. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  3040. return info, err
  3041. }
  3042. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  3043. //遍历循环入库数据
  3044. for i, item := range list {
  3045. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  3046. if (item.WarehousingCount - item.StockCount) >= count {
  3047. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  3048. info := models.WarehousingInfo{
  3049. GoodId: item.GoodId,
  3050. OrgId: item.OrgId,
  3051. WarehousingId: item.WarehousingId,
  3052. StockCount: item.StockCount + count,
  3053. }
  3054. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  3055. fmt.Println("2323233232323232323232323232323", err2)
  3056. return err
  3057. }
  3058. if (item.WarehousingCount - item.StockCount) < count {
  3059. //如果该批次小于退库数量,则先出库该相差数量
  3060. var countOne = item.WarehousingCount - item.StockCount
  3061. infoOne := models.WarehousingInfo{
  3062. GoodId: item.GoodId,
  3063. OrgId: item.OrgId,
  3064. WarehousingId: item.WarehousingId,
  3065. StockCount: countOne + item.StockCount,
  3066. }
  3067. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  3068. if err == nil {
  3069. //剩余的出库到第二个批次
  3070. var countTwo = count - countOne
  3071. infos := append(list, list[i+1])
  3072. var idArray []*models.WarehousingInfo
  3073. for _, it := range infos {
  3074. if it.WarehousingId != item.WarehousingId {
  3075. idArray = append(idArray, it)
  3076. }
  3077. }
  3078. //查询更新后的库存
  3079. UpdateCancelOut(idArray, countTwo)
  3080. return err
  3081. } else {
  3082. return err
  3083. }
  3084. }
  3085. }
  3086. return err
  3087. }
  3088. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  3089. if len(idstr) > 0 {
  3090. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  3091. } else {
  3092. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  3093. }
  3094. return info, err
  3095. }
  3096. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  3097. if len(idstr) > 0 {
  3098. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  3099. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3100. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  3101. }).Find(&info).Error
  3102. }
  3103. return info, err
  3104. }
  3105. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  3106. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  3107. if startime > 0 {
  3108. db = db.Where("x.ctime >=?", startime)
  3109. }
  3110. if endtime > 0 {
  3111. db = db.Where("x.ctime <= ?", endtime)
  3112. }
  3113. if orgid > 0 {
  3114. db = db.Where("x.org_id = ?", orgid)
  3115. }
  3116. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  3117. return info, err
  3118. }
  3119. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3120. info := models.WarehouseOutInfo{}
  3121. var err error
  3122. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  3123. if err == gorm.ErrRecordNotFound {
  3124. return nil, err
  3125. }
  3126. if err != nil {
  3127. return nil, err
  3128. }
  3129. return &info, nil
  3130. }
  3131. func GetWarehoureOrderInfoCount(goodid int64, orgid int64) (info models.WarehouseOutInfo, err error) {
  3132. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3133. if goodid > 0 {
  3134. db = db.Where("x.good_id = ?", goodid)
  3135. }
  3136. if orgid > 0 {
  3137. db = db.Where("x.org_id = ?", orgid)
  3138. }
  3139. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  3140. return info, err
  3141. }
  3142. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  3143. stock := models.CancelStock{}
  3144. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  3145. return stock, err
  3146. }
  3147. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  3148. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Group("number").Find(&info).Error
  3149. return info, err
  3150. }
  3151. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  3152. err = XTReadDB().Model(&info).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  3153. return info, err
  3154. }
  3155. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  3156. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  3157. return stock, err
  3158. }
  3159. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  3160. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1")
  3161. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  3162. fmt.Println(table)
  3163. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  3164. fmt.Println(tableone)
  3165. if id > 0 {
  3166. db = db.Where("x.warehouse_out_id = ?", id)
  3167. }
  3168. if goodid > 0 {
  3169. db = db.Where("x.good_id = ?", goodid)
  3170. }
  3171. err = db.Select("x.id,x.warehousing_id,x.good_id,x.number,x.count,x.patient_id,x.system_time,x.is_sys,x.warehousing_order,x.warehouse_out_id,x.warehouse_out_order_number,x.price,x.product_date,x.expire_date,x.ctime,o.packing_unit,t.name").Joins("left join xt_patients as t on t.id = x.patient_id").Joins("left join xt_good_information as o on o.id = x.good_id").Scan(&stock).Error
  3172. //err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&stock).Error
  3173. return stock, err
  3174. }
  3175. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  3176. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  3177. return info, err
  3178. }
  3179. func ExportDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseInfo, err error) {
  3180. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1")
  3181. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  3182. fmt.Println("table", table)
  3183. if len(warehouse_out_id) > 0 {
  3184. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  3185. }
  3186. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.price,x.dealer,x.manufacturer,x.warehousing_order,x.batch_number,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  3187. return out, err
  3188. }
  3189. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  3190. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3191. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  3192. fmt.Println("table", table)
  3193. if len(warehouse_out_id) > 0 {
  3194. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  3195. }
  3196. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.count,x.count_unit,x.price,x.product_date,x.expiry_date,x.warehouse_out_order_number,x.dealer,x.manufacturer,x.is_sys,x.sys_record_time,x.warehouse_info_id,x.number,x.batch_number,x.patient_id,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type,b.min_price").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  3197. return out, err
  3198. }
  3199. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid int64) (models.WarehouseOutInfo, error) {
  3200. info := models.WarehouseOutInfo{}
  3201. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  3202. return info, err
  3203. }
  3204. func GetTotalCountByGoodId(goodid int64) (models.WarehousingInfo, error) {
  3205. info := models.WarehousingInfo{}
  3206. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3207. if goodid > 0 {
  3208. db = db.Where("x.good_id = ?", goodid)
  3209. }
  3210. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  3211. return info, err
  3212. }
  3213. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  3214. info := models.WarehousingInfo{}
  3215. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  3216. return info, err
  3217. }
  3218. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  3219. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3220. return err
  3221. }
  3222. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64) (info models.WarehousingInfo, err error) {
  3223. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and id<>?", good_id, id).Order("ctime").First(&info).Error
  3224. return info, err
  3225. }
  3226. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  3227. if len(warehousingInfo) > 0 {
  3228. utx := writeDb.Begin()
  3229. if len(warehousingInfo) > 0 {
  3230. thisSQL := "INSERT INTO xt_stock_flow (warehousing_id, good_id, number, license_number, count,user_org_id,patient_id,system_time,consumable_type,is_sys,warehousing_order,warehouse_out_id,warehouse_out_order_number,is_edit,cancel_stock_id,cancel_order_number,manufacturer,dealer,creator,update_creator,status,ctime,mtime,price,warehousing_detail_id,warehouse_out_detail_id,cancel_out_detail_id,expire_date,product_date) VALUES "
  3231. insertParams := make([]string, 0)
  3232. insertData := make([]interface{}, 0)
  3233. for _, info := range warehousingInfo {
  3234. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  3235. insertData = append(insertData, info.WarehousingId)
  3236. insertData = append(insertData, info.GoodId)
  3237. insertData = append(insertData, info.Number)
  3238. insertData = append(insertData, info.LicenseNumber)
  3239. insertData = append(insertData, info.Count)
  3240. insertData = append(insertData, info.UserOrgId)
  3241. insertData = append(insertData, info.PatientId)
  3242. insertData = append(insertData, info.SystemTime)
  3243. insertData = append(insertData, info.ConsumableType)
  3244. insertData = append(insertData, info.IsSys)
  3245. insertData = append(insertData, info.WarehousingOrder)
  3246. insertData = append(insertData, info.WarehouseOutId)
  3247. insertData = append(insertData, info.WarehouseOutOrderNumber)
  3248. insertData = append(insertData, info.IsEdit)
  3249. insertData = append(insertData, info.CancelStockId)
  3250. insertData = append(insertData, info.CancelOrderNumber)
  3251. insertData = append(insertData, info.Manufacturer)
  3252. insertData = append(insertData, info.Dealer)
  3253. insertData = append(insertData, info.Creator)
  3254. insertData = append(insertData, info.UpdateCreator)
  3255. insertData = append(insertData, info.Status)
  3256. insertData = append(insertData, info.Ctime)
  3257. insertData = append(insertData, info.Mtime)
  3258. insertData = append(insertData, info.Price)
  3259. insertData = append(insertData, info.WarehousingDetailId)
  3260. insertData = append(insertData, info.WarehouseOutDetailId)
  3261. insertData = append(insertData, info.CancelOutDetailId)
  3262. insertData = append(insertData, info.ExpireDate)
  3263. insertData = append(insertData, info.ProductDate)
  3264. }
  3265. thisSQL += strings.Join(insertParams, ", ")
  3266. err = utx.Exec(thisSQL, insertData...).Error
  3267. if err != nil {
  3268. utx.Rollback()
  3269. return
  3270. }
  3271. }
  3272. utx.Commit()
  3273. }
  3274. return
  3275. }
  3276. func CreateStockFlowOne(flow models.VmStockFlow) error {
  3277. err := XTWriteDB().Create(&flow).Error
  3278. return err
  3279. }
  3280. func UpdatedStockFlow(flow models.VmStockFlow) error {
  3281. err := XTWriteDB().Save(&flow).Error
  3282. return err
  3283. }
  3284. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  3285. stock := models.VmStockFlow{}
  3286. var err error
  3287. err = XTReadDB().Model(&stock).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", warehousing_id, patient_id, record_time, good_id).Find(&stock).Error
  3288. if err == gorm.ErrRecordNotFound {
  3289. return nil, err
  3290. }
  3291. if err != nil {
  3292. return nil, err
  3293. }
  3294. return &stock, nil
  3295. }
  3296. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  3297. info := models.WarehouseOutInfo{}
  3298. var err error
  3299. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  3300. if err == gorm.ErrRecordNotFound {
  3301. return nil, err
  3302. }
  3303. if err != nil {
  3304. return nil, err
  3305. }
  3306. return &info, nil
  3307. }
  3308. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64) (*models.WarehouseOutInfo, error) {
  3309. info := models.WarehouseOutInfo{}
  3310. var err error
  3311. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ?", good_id, record_time, patient_id).Find(&info).Error
  3312. if err == gorm.ErrRecordNotFound {
  3313. return nil, err
  3314. }
  3315. if err != nil {
  3316. return nil, err
  3317. }
  3318. return &info, nil
  3319. }
  3320. func GetStockFlowList(limit int64, page int64, consumable_type int64, orgId int64, startTime int64, endTime int64, good_id int64) (flow []*models.VmStockFlow, total int64, err error) {
  3321. offset := (page - 1) * limit
  3322. db := XTReadDB().Model(&flow)
  3323. if consumable_type > 0 {
  3324. //入库
  3325. if consumable_type == 1 {
  3326. db = db.Where("consumable_type = ?", consumable_type)
  3327. }
  3328. //2 手动出库
  3329. if consumable_type == 2 {
  3330. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  3331. }
  3332. //3.自动出库
  3333. if consumable_type == 3 {
  3334. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  3335. }
  3336. //4.手动退库
  3337. if consumable_type == 4 {
  3338. db = db.Where(" consumable_type = ?", consumable_type)
  3339. }
  3340. }
  3341. if orgId > 0 {
  3342. db = db.Where("user_org_id = ?", orgId)
  3343. }
  3344. if startTime > 0 {
  3345. db = db.Where("ctime >= ?", startTime)
  3346. }
  3347. if endTime > 0 {
  3348. db = db.Where("ctime<=?", endTime)
  3349. }
  3350. if good_id > 0 {
  3351. db = db.Where("good_id = ?", good_id)
  3352. }
  3353. err = db.Count(&total).Offset(offset).Limit(limit).Find(&flow).Error
  3354. return flow, total, err
  3355. }
  3356. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  3357. detail := models.AutomaticReduceDetail{}
  3358. err := XTReadDB().Model(&detail).Where("record_time = ? and patient_id = ? and good_id = ? and good_type_id = ? and status = 1", recordtime, patientid, goodid, goodtypeid).Last(&detail).Error
  3359. return detail, err
  3360. }
  3361. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  3362. detail := models.WarehouseOutInfo{}
  3363. err := XTReadDB().Model(&detail).Where("sys_record_time = ? and patient_id = ? and good_id = ? and good_type_id = ? and status = 1", recordtime, patientid, goodid, goodtypeid).Last(&detail).Error
  3364. return detail, err
  3365. }
  3366. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64) error {
  3367. outInfo := models.WarehouseOutInfo{}
  3368. err := XTWriteDB().Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", good_id, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber}).Error
  3369. return err
  3370. }
  3371. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  3372. info := models.WarehouseOutInfo{}
  3373. err = XTReadDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and good_type_id = ? and status = 1", orgid, patient_id, record_time, goodid, good_type_id).Last(&info).Error
  3374. return info, err
  3375. }
  3376. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  3377. info := models.WarehousingInfo{}
  3378. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  3379. return info, err
  3380. }
  3381. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  3382. info := models.WarehousingInfo{}
  3383. db := XTReadDB().Model(&info)
  3384. if good_id > 0 {
  3385. db = db.Where("good_id = ? and status = 1", good_id)
  3386. }
  3387. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  3388. return info, err
  3389. }
  3390. func GetDrugFlow(drugid int64, orgid int64, limit int64, page int64, startTime int64, endTime int64, stock_type int64) (drug_flow []*models.DrugFlow, total int64, err error) {
  3391. offset := (page - 1) * limit
  3392. db := XTReadDB().Model(drug_flow)
  3393. if drugid > 0 {
  3394. db = db.Where("drug_id = ?", drugid)
  3395. }
  3396. if orgid > 0 {
  3397. db = db.Where("user_org_id = ?", orgid)
  3398. }
  3399. if startTime > 0 {
  3400. db = db.Where("ctime >=? ", startTime)
  3401. }
  3402. if endTime > 0 {
  3403. db = db.Where("ctime<=?", endTime)
  3404. }
  3405. if stock_type > 0 {
  3406. //手动入库
  3407. if stock_type == 1 {
  3408. db = db.Where("consumable_type = 1")
  3409. }
  3410. //手动出库
  3411. if stock_type == 2 {
  3412. db = db.Where("consumable_type = 2 and is_sys = 0")
  3413. }
  3414. //自动出库
  3415. if stock_type == 3 {
  3416. db = db.Where("consumable_type = 3 and is_sys = 1")
  3417. }
  3418. if stock_type == 4 {
  3419. db = db.Where("consumable_type = 4")
  3420. }
  3421. }
  3422. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&drug_flow).Error
  3423. return drug_flow, total, err
  3424. }
  3425. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  3426. outInfo := models.DrugWarehouseOutInfo{}
  3427. var err error
  3428. err = XTReadDB().Model(&outInfo).Where("drug_id = ? and patient_id = ? and status = 1 and sys_record_time = ?", drug_id, patient_id, recordtime).Find(&outInfo).Error
  3429. if err == gorm.ErrRecordNotFound {
  3430. return nil, err
  3431. }
  3432. if err != nil {
  3433. return nil, err
  3434. }
  3435. return &outInfo, nil
  3436. }
  3437. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  3438. outInfo := models.DrugWarehouseOutInfo{}
  3439. err := XTWriteDB().Model(&outInfo).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ?", drug_id, patient_id, record_date, orgid).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "mtime": info.Mtime, "ctime": info.Ctime, "org_id": info.OrgId, "warehouse_out_order_number": info.WarehouseOutOrderNumber, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "is_sys": info.IsSys, "sys_record_time": info.SysRecordTime, "warehouse_info_id": info.WarehouseInfoId, "number": info.Number, "batch_number": info.BatchNumber, "patient_id": info.PatientId}).Error
  3440. return err
  3441. }
  3442. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  3443. db := XTReadDB().Model(&cancle).Where("status = 1")
  3444. if startime > 0 {
  3445. db = db.Where("ctime >=?", startime)
  3446. }
  3447. if endtime > 0 {
  3448. db = db.Where("ctime <=?", endtime)
  3449. }
  3450. if len(ids) > 0 {
  3451. db = db.Where("cancel_stock_id in(?)", ids)
  3452. }
  3453. if orgid > 0 {
  3454. db = db.Where("org_id = ?", orgid)
  3455. }
  3456. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  3457. err = db.Find(&cancle).Error
  3458. return cancle, err
  3459. }
  3460. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  3461. db := XTReadDB().Model(&info)
  3462. if id > 0 {
  3463. db = db.Where("cancel_stock_id = ?", id)
  3464. }
  3465. db = db.Preload("BaseDrugLib")
  3466. err = db.Find(&info).Error
  3467. return info, err
  3468. }
  3469. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  3470. info := models.XtDrugWarehouseInfo{}
  3471. db := XTReadDB().Model(&info).Where("status = 1")
  3472. if id > 0 {
  3473. db = db.Where("id = ?", id)
  3474. }
  3475. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  3476. return info, err
  3477. }
  3478. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  3479. info := models.XtDrugWarehouseInfo{}
  3480. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  3481. return info, err
  3482. }
  3483. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  3484. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  3485. //return err
  3486. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  3487. return err
  3488. }
  3489. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  3490. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  3491. //return err
  3492. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  3493. return err
  3494. }
  3495. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  3496. info := models.DrugCancelStockInfo{}
  3497. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  3498. return info, err
  3499. }
  3500. func UpdatedDrugInfo(id int64, count int64) (err error) {
  3501. err = writeDb.Model(&models.DrugWarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  3502. return err
  3503. }
  3504. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  3505. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  3506. return err
  3507. }
  3508. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64) error {
  3509. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  3510. return err
  3511. }
  3512. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64) error {
  3513. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  3514. return err
  3515. }
  3516. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  3517. info := models.WarehousingInfo{}
  3518. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  3519. return info, err
  3520. }
  3521. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  3522. info := models.WarehousingInfo{}
  3523. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  3524. return info, err
  3525. }
  3526. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  3527. info := models.WarehousingInfo{}
  3528. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  3529. return info, err
  3530. }
  3531. func CreateWarehouseOut(out models.WarehouseOut) error {
  3532. err := XTWriteDB().Create(&out).Error
  3533. return err
  3534. }
  3535. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  3536. err := XTReadDB().Create(&info).Error
  3537. return err
  3538. }
  3539. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  3540. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3541. return err
  3542. }
  3543. func UpdateWareInfoById(id int64, count int64) error {
  3544. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3545. return err
  3546. }
  3547. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  3548. info := models.CancelStockInfo{}
  3549. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  3550. return info, err
  3551. }
  3552. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  3553. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  3554. return err
  3555. }
  3556. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  3557. drugFlow := models.DrugFlow{}
  3558. err := XTWriteDB().Model(&drugFlow).Where("drug_id = ? and warehousing_id= ? and status = 1", drugid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  3559. return err
  3560. }
  3561. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  3562. stock := models.DrugCancelStock{}
  3563. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  3564. return stock, err
  3565. }
  3566. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  3567. db := XTReadDB().Model(&info)
  3568. if warehouse_info_id > 0 {
  3569. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  3570. }
  3571. if drug_id > 0 {
  3572. db = db.Where("drug_id = ? and status =1", drug_id)
  3573. }
  3574. err = db.Find(&info).Error
  3575. return info, err
  3576. }
  3577. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  3578. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  3579. return info, err
  3580. }
  3581. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  3582. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  3583. return config, err
  3584. }
  3585. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  3586. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  3587. return config, err
  3588. }
  3589. func GetSearchGoodWarehouseList(keyword string, orgid int64) (info []*models.BloodWarehouseInfo, err error) {
  3590. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3591. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  3592. fmt.Println(table)
  3593. likeKey := "%" + keyword + "%"
  3594. if orgid > 0 {
  3595. db = db.Where("x.org_id = ?", orgid)
  3596. }
  3597. err = db.Group("x.good_id").Select("x.id,x.warehousing_id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.total_price,t.dealer,x.remark,x.warehousing_order,x.warehousing_unit,x.stock_count,x.license_number,x.warehousing_unit,t.good_name,t.specification_name,t.manufacturer,t.packing_price,t.id as good_id,t.packing_unit,t.buy_price").Joins("left join xt_good_information as t on t.id = x.good_id").Where("t.good_name like ? and t.org_id = ?", likeKey, orgid).Order("x.ctime desc").Scan(&info).Error
  3598. return info, err
  3599. }
  3600. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  3601. err := XTWriteDB().Create(&adjustPrice).Error
  3602. return err
  3603. }
  3604. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  3605. likeKey := "%" + keyword + "%"
  3606. offset := (page - 1) * limit
  3607. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  3608. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3609. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  3610. fmt.Println(table, tab)
  3611. if startime > 0 {
  3612. db = db.Where("x.start_time>=?", startime)
  3613. }
  3614. if endtime > 0 {
  3615. db = db.Where("x.start_time<=?", endtime)
  3616. }
  3617. if len(keyword) > 0 {
  3618. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  3619. }
  3620. if orgid > 0 {
  3621. db = db.Where("x.user_org_id = ?", orgid)
  3622. }
  3623. err = db.Group("x.id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,t.packing_unit").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&list).Error
  3624. return list, total, err
  3625. }
  3626. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  3627. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  3628. return err
  3629. }
  3630. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  3631. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  3632. return err
  3633. }
  3634. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  3635. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  3636. return info, err
  3637. }
  3638. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  3639. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  3640. return adjust, err
  3641. }
  3642. func CreateReportPrice(price *models.XtStockReportPrice) error {
  3643. err := XTWriteDB().Create(&price).Error
  3644. return err
  3645. }
  3646. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  3647. likeKey := "%" + keyword + "%"
  3648. offset := (page - 1) * limit
  3649. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  3650. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3651. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  3652. fmt.Println(table, tab)
  3653. if startime > 0 {
  3654. db = db.Where("x.ctime>=?", startime)
  3655. }
  3656. if endtime > 0 {
  3657. db = db.Where("x.ctime<=?", endtime)
  3658. }
  3659. if len(keyword) > 0 {
  3660. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  3661. }
  3662. if orgid > 0 {
  3663. db = db.Where("x.user_org_id = ?", orgid)
  3664. }
  3665. err = db.Group("x.id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,t.packing_unit,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  3666. return adjust, total, err
  3667. }
  3668. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  3669. err := XTWriteDB().Model(&reportPrice).Where("id in(?) and status = 1", ids).Updates(map[string]interface{}{"checker": reportPrice.Checker, "checker_time": reportPrice.CheckerTime, "checker_status": reportPrice.CheckerStatus}).Error
  3670. return err
  3671. }
  3672. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  3673. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  3674. return report, err
  3675. }
  3676. func CreateInentory(inventory models.XtStockInventory) error {
  3677. err = XTWriteDB().Create(&inventory).Error
  3678. return err
  3679. }
  3680. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  3681. likeKey := "%" + keyword + "%"
  3682. offset := (page - 1) * limit
  3683. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  3684. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3685. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  3686. fmt.Println(table, tab)
  3687. if startime > 0 {
  3688. db = db.Where("x.start_time>=?", startime)
  3689. }
  3690. if endtime > 0 {
  3691. db = db.Where("x.end_time<=?", endtime)
  3692. }
  3693. if len(keyword) > 0 {
  3694. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  3695. }
  3696. if orgid > 0 {
  3697. db = db.Where("x.user_org_id = ?", orgid)
  3698. }
  3699. if inventory_status > 0 {
  3700. db = db.Where("checker_status = ?", inventory_status)
  3701. }
  3702. err = db.Group("x.id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,t.packing_unit,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  3703. return adjust, total, err
  3704. }
  3705. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  3706. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  3707. return err
  3708. }
  3709. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  3710. info := models.XtStockAdjustPrice{}
  3711. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  3712. return info, err
  3713. }
  3714. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  3715. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  3716. return list, err
  3717. }
  3718. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  3719. info := models.WarehousingInfo{}
  3720. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  3721. return info, err
  3722. }
  3723. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  3724. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"new_price": info.NewPrice, "good_name": info.GoodName, "packing_price": info.PackingPrice, "good_id": info.GoodId, "remark": info.Remark, "manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  3725. return err
  3726. }
  3727. func DeleteStockPrice(id int64) error {
  3728. info := models.XtStockAdjustPrice{}
  3729. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  3730. return err
  3731. }
  3732. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  3733. report := models.XtStockReportPrice{}
  3734. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  3735. return report, err
  3736. }
  3737. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  3738. err := XTWriteDB().Model(&info).Where("id = ?", id).Updates(map[string]interface{}{"count": info.Count, "specification_name": info.SpecificationName, "good_name": info.GoodName, "number": info.Number, "warehousing_unit": info.WarehousingUnit, "buy_price": info.BuyPrice, "packing_price": info.PackingPrice, "good_origin_place": info.GoodOriginPlace, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "total": info.Total, "remark": info.Remark, "warehousing_info_id": info.WarehousingInfoId, "good_id": info.GoodId}).Error
  3739. return err
  3740. }
  3741. func DeleteDamage(id int64) error {
  3742. adjust := models.XtStockReportPrice{}
  3743. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  3744. return err
  3745. }
  3746. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  3747. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  3748. return adjust, err
  3749. }
  3750. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  3751. inventory := models.XtStockInventory{}
  3752. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  3753. return inventory, err
  3754. }
  3755. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  3756. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"good_id": inventory.GoodId, "good_name": inventory.GoodName, "specification_name": inventory.SpecificationName, "number": inventory.Number, "warehousing_unit": inventory.WarehousingUnit, "buy_price": inventory.BuyPrice, "packing_price": inventory.PackingPrice, "total": inventory.Total, "good_origin_place": inventory.GoodOriginPlace, "license_number": inventory.LicenseNumber, "dealer": inventory.Dealer, "manufacturer": inventory.Dealer, "count": inventory.Count, "remark": inventory.Remark}).Error
  3757. return err
  3758. }
  3759. func DeleteInventory(id int64) error {
  3760. inventory := models.XtStockInventory{}
  3761. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  3762. return err
  3763. }
  3764. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64) (list []*models.VmStockInventory, total int64, err error) {
  3765. likeKey := "%" + keyword + "%"
  3766. offset := (page - 1) * limit
  3767. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  3768. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3769. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  3770. fmt.Println(table, tab)
  3771. if len(keyword) > 0 {
  3772. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  3773. }
  3774. if orgid > 0 {
  3775. db = db.Where("x.user_org_id = ?", orgid)
  3776. }
  3777. err = db.Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,x.expire_date,x.product_date,t.packing_unit").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&list).Error
  3778. return list, total, err
  3779. }
  3780. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  3781. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  3782. return list, err
  3783. }
  3784. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  3785. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  3786. return list, err
  3787. }
  3788. func DeleteDrugPrice(id int64) error {
  3789. info := models.XtDrugAdjustPrice{}
  3790. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  3791. return err
  3792. }
  3793. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  3794. info := models.WarehousingInfo{}
  3795. db := XTReadDB().Model(&info).Where("status = 1")
  3796. if goodid > 0 {
  3797. db = db.Where("good_id = ?", goodid)
  3798. }
  3799. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  3800. return info, err
  3801. }
  3802. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  3803. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3804. return err
  3805. }
  3806. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  3807. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  3808. return err
  3809. }
  3810. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  3811. err := XTWriteDB().Create(&record).Error
  3812. return err
  3813. }
  3814. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  3815. info := models.WarehousingInfo{}
  3816. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  3817. return info, err
  3818. }