stock_service.go 412KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "github.com/jinzhu/gorm"
  10. )
  11. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  12. err := writeDb.Create(&dealer).Error
  13. return err, dealer
  14. }
  15. func ModifyDealer(dealer *models.Dealer) error {
  16. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  17. "dealer_name": dealer.DealerName,
  18. "contact": dealer.Contact,
  19. "contact_phone": dealer.ContactPhone,
  20. "platform_number": dealer.PlatformNumber,
  21. "email": dealer.Email,
  22. "contact_address": dealer.ContactAddress,
  23. "modifier": dealer.Modifier,
  24. "mtime": time.Now().Unix(),
  25. "remark": dealer.Remark,
  26. "wubi": dealer.WuBi,
  27. "pinyin": dealer.PinYin,
  28. }).Error
  29. return err
  30. }
  31. func FindAllDealerTotal(orgId int64) (total int64) {
  32. db := readDb.Model(&models.Dealer{})
  33. db = db.Where("org_id = ? ", orgId).Count(&total)
  34. return
  35. }
  36. // 获取经销商的名称
  37. func FindAllDealerName(orgId int64) (tmp []*models.Dealer, err error) {
  38. err = readDb.Model(&models.Dealer{}).Where("org_id = ? and status = 1", orgId).Find(&tmp).Error
  39. return
  40. }
  41. func FindAllDealerList(orgId int64, page int64, limit, key int64) (list []*models.Dealer, total int64, err error) {
  42. offset := (page - 1) * limit
  43. db := readDb.Model(&models.Dealer{})
  44. db = db.Where("org_id = ? AND status = 1", orgId)
  45. if key != 0 {
  46. db = db.Where("id = ? ", key)
  47. offset = 0
  48. limit = 10
  49. }
  50. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  51. return
  52. }
  53. func DeleteDealerById(id int64) error {
  54. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  55. return err
  56. }
  57. func FindDealerById(id int64) (*models.Dealer, error) {
  58. dealer := &models.Dealer{}
  59. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  60. return dealer, err
  61. }
  62. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  63. err := writeDb.Create(&manufacturer).Error
  64. return err, manufacturer
  65. }
  66. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  67. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  68. "manufacturer_name": manufacturer.ManufacturerName,
  69. "contact": manufacturer.Contact,
  70. "contact_phone": manufacturer.ContactPhone,
  71. "platform_number": manufacturer.PlatformNumber,
  72. "email": manufacturer.Email,
  73. "contact_address": manufacturer.ContactAddress,
  74. "modifier": manufacturer.Modifier,
  75. "mtime": time.Now().Unix(),
  76. "remark": manufacturer.Remark,
  77. "wubi": manufacturer.WuBi,
  78. "pinyin": manufacturer.PinYin,
  79. }).Error
  80. return err
  81. }
  82. // 获取生产厂商名称
  83. func FindAllManufacturerName(orgid int64) (tmp []*models.Manufacturer, err error) {
  84. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? and status = 1", orgid).Find(&tmp).Error
  85. return
  86. }
  87. func FindAllManufacturerList(orgId int64, page int64, limit, key int64) (list []*models.Manufacturer, total int64, err error) {
  88. offset := (page - 1) * limit
  89. db := readDb.Model(&models.Manufacturer{})
  90. db = db.Where("org_id = ? AND status = 1", orgId)
  91. if key != 0 {
  92. db = db.Where("id = ?", key)
  93. offset = 0
  94. limit = 10
  95. }
  96. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  97. return
  98. }
  99. func DeleteManufacturerById(id int64) error {
  100. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  101. return err
  102. }
  103. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  104. dealer := &models.Manufacturer{}
  105. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  106. return dealer, err
  107. }
  108. func FindManufacturerTotal(orgId int64) (total int64) {
  109. db := readDb.Model(&models.Manufacturer{})
  110. db = db.Where("org_id = ?", orgId).Count(&total)
  111. return
  112. }
  113. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  114. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  115. return dealers, err
  116. }
  117. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  118. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  119. return manufacturer, err
  120. }
  121. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  122. db := readDb.Model(&models.GoodsType{})
  123. db = db.Where("org_id = ?", orgId).Count(&total)
  124. return
  125. }
  126. func GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  127. goodsType := models.GoodsType{}
  128. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  129. if err == gorm.ErrRecordNotFound {
  130. return nil, err
  131. }
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &goodsType, nil
  136. }
  137. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  138. goodsType := models.GoodsType{}
  139. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  140. if err == gorm.ErrRecordNotFound {
  141. return nil, err
  142. }
  143. if err != nil {
  144. return nil, err
  145. }
  146. return &goodsType, nil
  147. }
  148. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  149. err := writeDb.Create(&goodType).Error
  150. return err, goodType
  151. }
  152. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  153. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  154. "modifier": goodType.Modifier,
  155. "mtime": time.Now().Unix(),
  156. "remark": goodType.Remark,
  157. "type_name": goodType.TypeName,
  158. "out_stock": goodType.OutStock,
  159. "stock_attribute": goodType.StockAttribute,
  160. }).Error
  161. return err, goodType
  162. }
  163. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  164. offset := (page - 1) * limit
  165. db := readDb.Model(&models.GoodsType{})
  166. db = db.Where("org_id=? AND status = 1", orgId)
  167. if len(keyword) > 0 {
  168. likeKey := "%" + keyword + "%"
  169. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  170. }
  171. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  172. return
  173. }
  174. func DeleteGoodTypeById(id int64, operateId int64) error {
  175. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  176. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  177. return err
  178. }
  179. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  180. goodType := &models.GoodsType{}
  181. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  182. return goodType, err
  183. }
  184. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  185. db := readDb.Model(&models.GoodInfo{})
  186. db = db.Where("org_id = ?", orgId).Count(&total)
  187. return
  188. }
  189. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  190. err := writeDb.Create(&goodInfo).Error
  191. return err, goodInfo
  192. }
  193. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  194. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  195. "good_type_id": goodInfo.GoodTypeId,
  196. "modifier": goodInfo.Modifier,
  197. "mtime": time.Now().Unix(),
  198. "remark": goodInfo.Remark,
  199. "specification_name": goodInfo.SpecificationName,
  200. "good_unit": goodInfo.GoodUnit,
  201. "buy_price": goodInfo.BuyPrice,
  202. "sell_price": goodInfo.SellPrice,
  203. "manufacturer": goodInfo.Manufacturer,
  204. "dealer": goodInfo.Dealer,
  205. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  206. "stock_warn_count": goodInfo.StockWarnCount,
  207. "is_reuse": goodInfo.IsReuse,
  208. "good_name": goodInfo.GoodName,
  209. "pinyin": goodInfo.Pinyin,
  210. "wubi": goodInfo.Wubi,
  211. "good_kind": goodInfo.GoodKind,
  212. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  213. "retail_price": goodInfo.PackingPrice,
  214. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  215. "is_special_diseases": goodInfo.IsSpecialDiseases,
  216. "is_record": goodInfo.IsRecord,
  217. "statistics_category": goodInfo.StatisticsCategory,
  218. "good_status": goodInfo.GoodStatus,
  219. "default_count": goodInfo.DefaultCount,
  220. "sign": goodInfo.Sign,
  221. "is_default": goodInfo.IsDefault,
  222. "is_charge_use": goodInfo.IsChargeUse,
  223. "is_charge_predict": goodInfo.IsChargePredict,
  224. "is_statistics_work": goodInfo.IsStatisticsWork,
  225. "sort": goodInfo.Sort,
  226. "is_doctor_use": goodInfo.IsDoctorUse,
  227. "agent": goodInfo.Agent,
  228. "good_number": goodInfo.GoodNumber,
  229. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  230. "special_medical": goodInfo.SpecialMedical,
  231. "production_type": goodInfo.ProductionType,
  232. "min_number": goodInfo.MinNumber,
  233. "packing_unit": goodInfo.PackingUnit,
  234. "packing_price": goodInfo.PackingPrice,
  235. "default_count_unit": goodInfo.DefaultCountUnit,
  236. "min_unit": goodInfo.MinUnit,
  237. "register_number": goodInfo.RegisterNumber,
  238. "provinces_code": goodInfo.ProvincesCode,
  239. "is_user": goodInfo.IsUser,
  240. "number": goodInfo.Number,
  241. "is_warehouse": goodInfo.IsWarehouse,
  242. "bbx01": goodInfo.Bbx01,
  243. "bby01": goodInfo.Bby01,
  244. "zuobiao_id": goodInfo.Bbx01,
  245. "first_letter": goodInfo.FirstLetter,
  246. "is_show": goodInfo.IsShow,
  247. "is_zero_flag": goodInfo.IsZeroFlag,
  248. }).Error
  249. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  250. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  251. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  254. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  255. return err, goodInfo
  256. }
  257. func FindGoodInfoList(orgId int64, page int64, limit int64, keyword string, is_use int64, is_charge int64, good_kind int64, is_mark int64, manufacturer int64, is_type int64) (list []*models.GoodInfo, total int64, err error) {
  258. offset := (page - 1) * limit
  259. db := readDb.Model(&models.GoodInfo{})
  260. db = db.Where("org_id = ? AND status = 1", orgId)
  261. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  262. if len(keyword) > 0 {
  263. likeKey := "%" + keyword + "%"
  264. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  265. if is_use > 0 {
  266. if is_use == 2 {
  267. db = db.Where("find_in_set('停用',good_status) = 0")
  268. } else {
  269. db = db.Where("find_in_set('停用',good_status) > 0")
  270. }
  271. }
  272. if is_charge > 0 {
  273. if is_charge == 1 {
  274. db = db.Where("find_in_set('收费',good_status) > 0")
  275. } else {
  276. db = db.Where("find_in_set('收费',good_status) = 0")
  277. }
  278. }
  279. if good_kind > 0 {
  280. db = db.Where(" good_kind = ?", good_kind)
  281. }
  282. if is_mark > 0 {
  283. db = db.Where("is_mark = ?", is_mark)
  284. }
  285. if manufacturer > 0 {
  286. db = db.Where("manufacturer = ?", manufacturer)
  287. }
  288. if is_type > 0 {
  289. db = db.Where("good_type_id = ?", is_type)
  290. }
  291. } else {
  292. if is_use > 0 {
  293. if is_use == 2 {
  294. db = db.Where("find_in_set('停用',good_status) = 0")
  295. } else {
  296. db = db.Where("find_in_set('停用',good_status) > 0")
  297. }
  298. }
  299. if is_charge > 0 {
  300. if is_charge == 1 {
  301. db = db.Where("find_in_set('收费',good_status) > 0")
  302. } else {
  303. db = db.Where("find_in_set('收费',good_status) = 0")
  304. }
  305. }
  306. if good_kind > 0 {
  307. db = db.Where(" good_kind = ?", good_kind)
  308. }
  309. if is_mark > 0 {
  310. db = db.Where("is_mark = ?", is_mark)
  311. }
  312. if manufacturer > 0 {
  313. db = db.Where("manufacturer = ?", manufacturer)
  314. }
  315. if is_type > 0 {
  316. db = db.Where("good_type_id = ?", is_type)
  317. }
  318. }
  319. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  320. return
  321. }
  322. func DeleteGoodInfoById(id int64, operateId int64) error {
  323. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  324. return err
  325. }
  326. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  327. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  328. return list, err
  329. }
  330. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  331. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  332. return list, err
  333. }
  334. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  335. goodInfo := &models.GoodInfo{}
  336. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  337. return goodInfo, err
  338. }
  339. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  340. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  341. return goodType, err
  342. }
  343. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  344. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  345. return goodType, err
  346. }
  347. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  348. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 and is_warehouse = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  349. return goodInfo, err
  350. }
  351. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  352. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  353. return goodInfo, err
  354. }
  355. func FindAllGoodInfoThree(org_id int64) (goodInfo []*models.XtGoodInformationFourty, err error) {
  356. err = XTReadDB().Model(&models.XtGoodInformationFourty{}).Where("org_id = ? AND status = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  357. return goodInfo, err
  358. }
  359. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  360. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  361. return total, err
  362. }
  363. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  364. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  365. return total, err
  366. }
  367. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  368. err := writeDb.Create(&warehouse).Error
  369. return err
  370. }
  371. func UpdateWarehouse(id int64, storehouse_id int64) error {
  372. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  373. return err
  374. }
  375. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  376. err := writeDb.Create(&warehouse).Error
  377. return err
  378. }
  379. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  380. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  381. return err
  382. }
  383. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  384. err := writeDb.Create(&salesReturn).Error
  385. return err
  386. }
  387. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  388. err := writeDb.Create(&salesReturn).Error
  389. return err
  390. }
  391. func UpDateGoodReturnStatus(ids []string) {
  392. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  393. }
  394. func UpDateCanCelStockStatus(ids []string) {
  395. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  396. }
  397. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  398. if len(warehousingInfo) > 0 {
  399. utx := writeDb.Begin()
  400. if len(warehousingInfo) > 0 {
  401. 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,packing_price,storehouse_id,good_code) VALUES "
  402. insertParams := make([]string, 0)
  403. insertData := make([]interface{}, 0)
  404. for _, info := range warehousingInfo {
  405. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  406. insertData = append(insertData, info.WarehousingId)
  407. insertData = append(insertData, info.GoodId)
  408. insertData = append(insertData, info.GoodTypeId)
  409. insertData = append(insertData, info.Number)
  410. insertData = append(insertData, info.ProductDate)
  411. insertData = append(insertData, info.ExpiryDate)
  412. insertData = append(insertData, info.WarehousingCount)
  413. insertData = append(insertData, info.Price)
  414. insertData = append(insertData, info.TotalPrice)
  415. insertData = append(insertData, info.Dealer)
  416. insertData = append(insertData, info.Manufacturer)
  417. insertData = append(insertData, info.Remark)
  418. insertData = append(insertData, info.Ctime)
  419. insertData = append(insertData, info.Mtime)
  420. insertData = append(insertData, info.Status)
  421. insertData = append(insertData, info.OrgId)
  422. insertData = append(insertData, info.WarehousingOrder)
  423. insertData = append(insertData, info.Type)
  424. insertData = append(insertData, info.WarehousingCount)
  425. insertData = append(insertData, info.LicenseNumber)
  426. insertData = append(insertData, info.PackingPrice)
  427. insertData = append(insertData, info.StorehouseId)
  428. insertData = append(insertData, info.GoodCode)
  429. }
  430. thisSQL += strings.Join(insertParams, ", ")
  431. err = utx.Exec(thisSQL, insertData...).Error
  432. if err != nil {
  433. utx.Rollback()
  434. return
  435. }
  436. }
  437. utx.Commit()
  438. }
  439. return
  440. }
  441. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  442. if len(warehousingInfo) > 0 {
  443. utx := writeDb.Begin()
  444. if len(warehousingInfo) > 0 {
  445. 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,stock_min_number,storehouse_id) VALUES "
  446. insertParams := make([]string, 0)
  447. insertData := make([]interface{}, 0)
  448. for _, info := range warehousingInfo {
  449. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  450. insertData = append(insertData, info.WarehousingId)
  451. insertData = append(insertData, info.DrugId)
  452. insertData = append(insertData, info.Number)
  453. insertData = append(insertData, info.ProductDate)
  454. insertData = append(insertData, info.ExpiryDate)
  455. insertData = append(insertData, info.WarehousingCount)
  456. insertData = append(insertData, info.Price)
  457. insertData = append(insertData, info.TotalPrice)
  458. insertData = append(insertData, info.Dealer)
  459. insertData = append(insertData, info.Manufacturer)
  460. insertData = append(insertData, info.Remark)
  461. insertData = append(insertData, info.Ctime)
  462. insertData = append(insertData, info.Mtime)
  463. insertData = append(insertData, info.Status)
  464. insertData = append(insertData, info.OrgId)
  465. insertData = append(insertData, info.WarehousingOrder)
  466. insertData = append(insertData, info.Type)
  467. insertData = append(insertData, info.RetailPrice)
  468. insertData = append(insertData, info.RetailTotalPrice)
  469. insertData = append(insertData, info.StockMaxNumber)
  470. insertData = append(insertData, info.MaxUnit)
  471. insertData = append(insertData, info.MinUnit)
  472. insertData = append(insertData, info.BatchNumber)
  473. insertData = append(insertData, info.StockMinNumber)
  474. insertData = append(insertData, info.StorehouseId)
  475. }
  476. thisSQL += strings.Join(insertParams, ", ")
  477. err = utx.Exec(thisSQL, insertData...).Error
  478. if err != nil {
  479. utx.Rollback()
  480. return
  481. }
  482. }
  483. utx.Commit()
  484. }
  485. return
  486. }
  487. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  488. if len(warehousingInfo) > 0 {
  489. utx := writeDb.Begin()
  490. if len(warehousingInfo) > 0 {
  491. 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,stock_min_number,supply_warehouse_id,supply_warehouse_detail_info,storehouse_id,is_check) VALUES "
  492. insertParams := make([]string, 0)
  493. insertData := make([]interface{}, 0)
  494. for _, info := range warehousingInfo {
  495. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  496. insertData = append(insertData, info.WarehousingId)
  497. insertData = append(insertData, info.DrugId)
  498. insertData = append(insertData, info.Number)
  499. insertData = append(insertData, info.ProductDate)
  500. insertData = append(insertData, info.ExpiryDate)
  501. insertData = append(insertData, info.WarehousingCount)
  502. insertData = append(insertData, info.Price)
  503. insertData = append(insertData, info.TotalPrice)
  504. insertData = append(insertData, info.Dealer)
  505. insertData = append(insertData, info.Manufacturer)
  506. insertData = append(insertData, info.Remark)
  507. insertData = append(insertData, info.Ctime)
  508. insertData = append(insertData, info.Mtime)
  509. insertData = append(insertData, info.Status)
  510. insertData = append(insertData, info.OrgId)
  511. insertData = append(insertData, info.WarehousingOrder)
  512. insertData = append(insertData, info.Type)
  513. insertData = append(insertData, info.RetailPrice)
  514. insertData = append(insertData, info.RetailTotalPrice)
  515. insertData = append(insertData, info.StockMaxNumber)
  516. insertData = append(insertData, info.MaxUnit)
  517. insertData = append(insertData, info.MinUnit)
  518. insertData = append(insertData, info.BatchNumber)
  519. insertData = append(insertData, info.StockMinNumber)
  520. insertData = append(insertData, info.SupplyWarehouseId)
  521. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  522. insertData = append(insertData, info.StorehouseId)
  523. insertData = append(insertData, info.IsCheck)
  524. }
  525. thisSQL += strings.Join(insertParams, ", ")
  526. err = utx.Exec(thisSQL, insertData...).Error
  527. if err != nil {
  528. utx.Rollback()
  529. return
  530. }
  531. }
  532. utx.Commit()
  533. }
  534. return
  535. }
  536. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  537. var total float64
  538. var min_number int64
  539. for _, item := range warehousingInfo {
  540. //查询刚入库耗材的库存
  541. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  542. //更新药品库的总量
  543. info, _ := FindeDrugInfo(item.DrugId)
  544. min_number = count.StockMaxNumber * info.MinNumber
  545. str := strconv.FormatInt(min_number, 10)
  546. minFloat, _ := strconv.ParseFloat(str, 64)
  547. total = info.Total + minFloat
  548. lib := models.BaseDrugLib{
  549. Total: total,
  550. }
  551. UpdateBaseDrug(&lib, item.ID)
  552. }
  553. return err
  554. }
  555. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  556. info := models.XtDrugWarehouseInfo{}
  557. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  558. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  559. return info, err
  560. }
  561. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  562. lib := models.BaseDrugLib{}
  563. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  564. return lib, err
  565. }
  566. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  567. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  568. return err
  569. }
  570. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  571. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  572. return err
  573. }
  574. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  575. info := models.DrugWarehouseInfo{}
  576. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  577. return info, err
  578. }
  579. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  580. out := models.DrugWarehouseOut{}
  581. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  582. return out, err
  583. }
  584. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  585. if len(drugflow) > 0 {
  586. utx := writeDb.Begin()
  587. if len(drugflow) > 0 {
  588. 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,storehouse_id) VALUES "
  589. insertParams := make([]string, 0)
  590. insertData := make([]interface{}, 0)
  591. for _, info := range drugflow {
  592. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  593. insertData = append(insertData, info.WarehousingId)
  594. insertData = append(insertData, info.DrugId)
  595. insertData = append(insertData, info.Number)
  596. insertData = append(insertData, info.BatchNumber)
  597. insertData = append(insertData, info.Count)
  598. insertData = append(insertData, info.UserOrgId)
  599. insertData = append(insertData, info.PatientId)
  600. insertData = append(insertData, info.SystemTime)
  601. insertData = append(insertData, info.ConsumableType)
  602. insertData = append(insertData, info.IsSys)
  603. insertData = append(insertData, info.WarehousingOrder)
  604. insertData = append(insertData, info.WarehouseOutId)
  605. insertData = append(insertData, info.WarehouseOutOrderNumber)
  606. insertData = append(insertData, info.IsEdit)
  607. insertData = append(insertData, info.CancelStockId)
  608. insertData = append(insertData, info.CancelOrderNumber)
  609. insertData = append(insertData, info.Manufacturer)
  610. insertData = append(insertData, info.Dealer)
  611. insertData = append(insertData, info.Creator)
  612. insertData = append(insertData, info.UpdateCreator)
  613. insertData = append(insertData, info.Status)
  614. insertData = append(insertData, info.Ctime)
  615. insertData = append(insertData, info.Mtime)
  616. insertData = append(insertData, info.Price)
  617. insertData = append(insertData, info.WarehousingDetailId)
  618. insertData = append(insertData, info.WarehouseOutDetailId)
  619. insertData = append(insertData, info.CancelOutDetailId)
  620. insertData = append(insertData, info.ExpireDate)
  621. insertData = append(insertData, info.ProductDate)
  622. insertData = append(insertData, info.MaxUnit)
  623. insertData = append(insertData, info.MinUnit)
  624. insertData = append(insertData, info.StorehouseId)
  625. }
  626. thisSQL += strings.Join(insertParams, ", ")
  627. err = utx.Exec(thisSQL, insertData...).Error
  628. if err != nil {
  629. utx.Rollback()
  630. return
  631. }
  632. }
  633. utx.Commit()
  634. }
  635. return
  636. }
  637. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  638. info := models.DrugFlow{}
  639. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  640. if err == gorm.ErrRecordNotFound {
  641. return nil, err
  642. }
  643. if err != nil {
  644. return nil, err
  645. }
  646. return &info, nil
  647. }
  648. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  649. info := models.XtDrugWarehouseInfo{}
  650. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  651. return info, err
  652. }
  653. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  654. warehouseInfo := models.XtDrugWarehouseInfo{}
  655. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  656. return err
  657. }
  658. func CreateDrugFlowOne(flow models.DrugFlow) error {
  659. ut := writeDb.Begin()
  660. err := ut.Create(&flow).Error
  661. if err != nil {
  662. ut.Rollback()
  663. return err
  664. }
  665. ut.Commit()
  666. return err
  667. }
  668. func CreateSelfDrugFlowTwo(flow models.XtSelfDrugFlow) error {
  669. ut := writeDb.Begin()
  670. err := ut.Create(&flow).Error
  671. if err != nil {
  672. ut.Rollback()
  673. return err
  674. }
  675. ut.Commit()
  676. return err
  677. }
  678. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  679. err := tx.Create(&flow).Error
  680. if err != nil {
  681. tx.Rollback()
  682. return err
  683. }
  684. return err
  685. }
  686. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  687. detail := models.DrugAutomaticReduceDetail{}
  688. err := XTWriteDB().Model(&detail).Where("patient_id = ? and record_time = ? and drug_id = ? and status= 1 and advice_id =? ", patientid, recorddate, drugid, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  689. err = XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("patient_id =? and sys_record_time =? and drug_id = ? and status = 1 and advice_id =?", patientid, recorddate, drugid, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  690. return detail, err
  691. }
  692. func UpdateSelfDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.XtSelfDrugWarehouseOutInfo, error) {
  693. detail := models.XtSelfDrugWarehouseOutInfo{}
  694. err = XTWriteDB().Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("patient_id =? and sys_record_time =? and drug_id = ? and status = 1 and advice_id =?", patientid, recorddate, drugid, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  695. return detail, err
  696. }
  697. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  698. flow := models.DrugFlow{}
  699. err := XTWriteDB().Model(&flow).Where("patient_id =? and warehouse_out_order_number = ? and drug_id = ? and status = 1 and advice_id =?", patientid, warehouse_out_number, drug_id, advice_id).Updates(map[string]interface{}{"is_read": 1}).Error
  700. return flow, err
  701. }
  702. func UpdateDrugFlowTensOne(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.XtSelfDrugFlow, error) {
  703. flow := models.XtSelfDrugFlow{}
  704. err := XTWriteDB().Model(&flow).Where("patient_id =? and warehouse_out_order_number = ? and drug_id = ? and status = 1 and advice_id =?", patientid, warehouse_out_number, drug_id, advice_id).Updates(map[string]interface{}{"is_read": 1}).Error
  705. return flow, err
  706. }
  707. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  708. tx := XTWriteDB().Begin()
  709. err := tx.Create(&flow).Error
  710. if err != nil {
  711. tx.Rollback()
  712. return err
  713. }
  714. tx.Commit()
  715. return err
  716. }
  717. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  718. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  719. return info, err
  720. }
  721. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  722. info := models.WarehousingInfo{}
  723. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  724. return info, err
  725. }
  726. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  727. err := XTWriteDB().Create(&info).Error
  728. return err
  729. }
  730. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  731. if storehouse_id == 0 {
  732. 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
  733. }
  734. if storehouse_id > 0 {
  735. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, good_type_id, storehouse_id).Order("ctime").First(&info).Error
  736. }
  737. return info, err
  738. }
  739. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  740. if storehouse_id == 0 {
  741. err = tx.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
  742. if err != gorm.ErrRecordNotFound {
  743. if err != nil {
  744. tx.Rollback()
  745. return info, err
  746. }
  747. }
  748. }
  749. if storehouse_id > 0 {
  750. err = tx.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, good_type_id, storehouse_id).Order("ctime").First(&info).Error
  751. if err != gorm.ErrRecordNotFound {
  752. if err != nil {
  753. tx.Rollback()
  754. return info, err
  755. }
  756. }
  757. }
  758. return info, err
  759. }
  760. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  761. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  762. if good_id > 0 {
  763. db = db.Where("x.good_id = ?", good_id)
  764. }
  765. if good_type_id > 0 {
  766. db = db.Where("x.good_type_id = ?", good_type_id)
  767. }
  768. if storehouse_id > 0 {
  769. db = db.Where("x.storehouse_id = ?", storehouse_id)
  770. }
  771. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  772. return info, err
  773. }
  774. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  775. info := models.WarehousingInfo{}
  776. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  777. return info, err
  778. }
  779. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  780. info := models.WarehousingInfo{}
  781. var err error
  782. if storehouse_id == 0 {
  783. err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and is_check = 1", goodId).Find(&info).Error
  784. }
  785. if storehouse_id > 0 {
  786. err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and storehouse_id = ? and is_check = 1", goodId, storehouse_id).Find(&info).Error
  787. }
  788. return info, err
  789. }
  790. //药品先进先出,找出最先入库的批次
  791. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  792. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").First(&info).Error
  793. return info, err
  794. }
  795. func FindLastDrugWarehousingInfoByIDTwo(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  796. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").Last(&info).Error
  797. return info, err
  798. }
  799. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  800. err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").First(&info).Error
  801. if err != gorm.ErrRecordNotFound {
  802. if err != nil {
  803. tx.Rollback()
  804. return info, err
  805. }
  806. }
  807. return info, err
  808. }
  809. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  810. if is_source == 1 {
  811. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and supply_warehouse_id = ? and storehouse_id = ?", drug_id, warehouse_out_id, storehouse_id).Order("ctime").First(&info).Error
  812. }
  813. if is_source == 2 || is_source == 0 {
  814. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and storehouse_id = ?", drug_id, storehouse_id).Order("ctime").First(&info).Error
  815. }
  816. return info, err
  817. }
  818. // 出库时先进先出,找出最先入库的批次
  819. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  820. 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
  821. return info, err
  822. }
  823. //
  824. //// 出库时先进先出,找出最先入库的批次
  825. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  826. // 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
  827. // return info, err
  828. //}
  829. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  830. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  831. return info, err
  832. }
  833. func FindAllWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, check_type int64) (list []*models.Warehousing, total int64, err error) {
  834. offset := (page - 1) * limit
  835. db := readDb.Model(&models.Warehousing{})
  836. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  837. if len(keywords) > 0 {
  838. likeKey := "%" + keywords + "%"
  839. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  840. 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")
  841. }
  842. if startTime > 0 {
  843. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  844. }
  845. if endTime > 0 {
  846. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  847. }
  848. if storehouse_id > 0 {
  849. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  850. }
  851. if check_type == 0 {
  852. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  853. }
  854. if check_type > 0 {
  855. db = db.Where("xt_warehouse.is_check = ?", check_type)
  856. }
  857. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  858. return
  859. }
  860. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  861. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  862. if orgid > 0 {
  863. db = db.Where("org_id = ?", orgid)
  864. }
  865. if len(keyword) > 0 {
  866. likeKey := "%" + keyword + "%"
  867. db = db.Where("good_name like ?", likeKey)
  868. }
  869. err = db.Find(&goodInfo).Error
  870. return goodInfo, err
  871. }
  872. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  873. db := XTReadDB().Model(&info).Where("status =1")
  874. if len(good_id) > 0 {
  875. db = db.Where("good_id in (?)", good_id)
  876. }
  877. if startime > 0 {
  878. db = db.Where("ctime >=?", startime)
  879. }
  880. if endtime > 0 {
  881. db = db.Where("ctime<=?", endtime)
  882. }
  883. if orgid > 0 {
  884. db = db.Where("org_id = ?", orgid)
  885. }
  886. if storehouse_id > 0 {
  887. db = db.Where("storehouse_id = ?", storehouse_id)
  888. }
  889. err = db.Find(&info).Error
  890. return info, err
  891. }
  892. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  893. db := XTReadDB().Model(&info).Where("status =1")
  894. if len(good_id) > 0 {
  895. db = db.Where("good_id in (?)", good_id)
  896. }
  897. if startime > 0 {
  898. db = db.Where("ctime >=?", startime)
  899. }
  900. if endtime > 0 {
  901. db = db.Where("ctime<=?", endtime)
  902. }
  903. if orgid > 0 {
  904. db = db.Where("org_id = ?", orgid)
  905. }
  906. if storehouse_id > 0 {
  907. db = db.Where("storehouse_id = ?", storehouse_id)
  908. }
  909. err = db.Find(&info).Error
  910. return info, err
  911. }
  912. type VMDrugWarehouse struct {
  913. ID int64 `gorm:"column:id" json:"id" form:"id"`
  914. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  915. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  916. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  917. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  918. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  919. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  920. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  921. Status int64 `gorm:"column:status" json:"status" form:"status"`
  922. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  923. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  924. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  925. Type int64 `gorm:"column:type" json:"type" form:"type"`
  926. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  927. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  928. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  929. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  930. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  931. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  932. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  933. }
  934. func (VMDrugWarehouse) TableName() string {
  935. return "xt_drug_warehouse"
  936. }
  937. type VMDrugWarehouseInfo struct {
  938. ID int64 `gorm:"column:id" json:"id" form:"id"`
  939. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  940. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  941. Number string `gorm:"column:number" json:"number" form:"number"`
  942. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  943. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  944. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  945. Price float64 `gorm:"column:price" json:"price" form:"price"`
  946. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  947. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  948. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  949. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  950. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  951. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  952. Status int64 `gorm:"column:status" json:"status" form:"status"`
  953. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  954. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  955. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  956. Type int64 `gorm:"column:type" json:"type" form:"type"`
  957. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  958. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  959. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  960. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  961. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  962. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  963. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  964. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  965. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  966. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  967. }
  968. func (VMDrugWarehouseInfo) TableName() string {
  969. return "xt_drug_warehouse_info"
  970. }
  971. type Drug struct {
  972. ID int64 `gorm:"column:id" json:"id" form:"id"`
  973. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  974. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  975. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  976. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  977. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  978. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  979. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  980. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  981. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  982. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  983. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  984. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  985. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  986. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  987. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  988. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  989. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  990. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  991. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  992. Number string `gorm:"column:number" json:"number" form:"number"`
  993. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  994. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  995. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  996. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  997. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  998. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  999. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  1000. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  1001. Code string `gorm:"column:code" json:"code" form:"code"`
  1002. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  1003. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  1004. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  1005. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  1006. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  1007. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  1008. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  1009. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  1010. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  1011. Label int64 `gorm:"column:label" json:"label" form:"label"`
  1012. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  1013. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  1014. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  1015. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  1016. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  1017. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  1018. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1019. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1020. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1021. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1022. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1023. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1024. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  1025. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  1026. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  1027. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  1028. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  1029. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  1030. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  1031. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  1032. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1033. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1034. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1035. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1036. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1037. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1038. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1039. IsZeroFlag int64 `gorm:"column:is_zero_flag" json:"is_zero_flag" form:"is_zero_flag"`
  1040. }
  1041. func (Drug) TableName() string {
  1042. return "xt_base_drug"
  1043. }
  1044. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1045. db := readDb.Model(&VMDrugWarehouse{})
  1046. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1047. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1048. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1049. if len(keywords) > 0 {
  1050. likeKey := "%" + keywords + "%"
  1051. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1052. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1053. 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")
  1054. }
  1055. if startTime > 0 {
  1056. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1057. }
  1058. if endTime > 0 {
  1059. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1060. }
  1061. db = db.Count(&total)
  1062. offset := (page - 1) * limit
  1063. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1064. return
  1065. }
  1066. func FindAllDrugWarehousingListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, check_type int64) (list []*VMDrugWarehouse, total int64, err error) {
  1067. db := readDb.Model(&VMDrugWarehouse{})
  1068. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1069. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1070. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1071. if len(keywords) > 0 {
  1072. likeKey := "%" + keywords + "%"
  1073. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1074. 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")
  1075. }
  1076. if startTime > 0 {
  1077. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1078. }
  1079. if endTime > 0 {
  1080. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1081. }
  1082. if storehouse_id > 0 {
  1083. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1084. }
  1085. if check_type > 0 {
  1086. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1087. }
  1088. db = db.Count(&total)
  1089. offset := (page - 1) * limit
  1090. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1091. return
  1092. }
  1093. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1094. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1095. return list, err
  1096. }
  1097. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1098. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1099. return list, err
  1100. }
  1101. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1102. 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
  1103. return list, err
  1104. }
  1105. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1106. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1107. return list, err
  1108. }
  1109. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1110. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1111. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1112. fmt.Println(table)
  1113. if len(id) > 0 {
  1114. db = db.Where("x.warehousing_id in(?)", id)
  1115. }
  1116. if org_id > 0 {
  1117. db = db.Where("x.org_id = ?", org_id)
  1118. }
  1119. err = db.Select("x.id,x.max_unit as limax_unit,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,x.manufacturer,x.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
  1120. return list, err
  1121. }
  1122. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1123. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1124. return warehousing, err
  1125. }
  1126. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1127. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1128. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1129. return warehousing, err
  1130. }
  1131. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1132. 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
  1133. return warehousing, err
  1134. }
  1135. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1136. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1137. if len(id) > 0 {
  1138. db = db.Where("x.id in(?)", id)
  1139. }
  1140. if org_id > 0 {
  1141. db = db.Where("x.org_id = ?", org_id)
  1142. }
  1143. 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
  1144. return warehousing, err
  1145. }
  1146. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1147. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1148. // return warehousing, err
  1149. //
  1150. //}
  1151. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1152. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1153. return
  1154. }
  1155. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1156. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1157. return
  1158. }
  1159. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1160. if len(salesReturnInfo) > 0 {
  1161. utx := writeDb.Begin()
  1162. if len(salesReturnInfo) > 0 {
  1163. 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 "
  1164. insertParams := make([]string, 0)
  1165. insertData := make([]interface{}, 0)
  1166. for _, info := range salesReturnInfo {
  1167. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1168. insertData = append(insertData, info.GoodId)
  1169. insertData = append(insertData, info.SalesReturnId)
  1170. insertData = append(insertData, info.GoodTypeId)
  1171. insertData = append(insertData, info.Count)
  1172. insertData = append(insertData, info.Price)
  1173. insertData = append(insertData, info.Total)
  1174. insertData = append(insertData, info.Ctime)
  1175. insertData = append(insertData, info.Status)
  1176. insertData = append(insertData, info.OrgId)
  1177. insertData = append(insertData, info.OrderNumber)
  1178. insertData = append(insertData, info.Type)
  1179. insertData = append(insertData, info.Dealer)
  1180. insertData = append(insertData, info.Manufacturer)
  1181. }
  1182. thisSQL += strings.Join(insertParams, ", ")
  1183. err = utx.Exec(thisSQL, insertData...).Error
  1184. if err != nil {
  1185. utx.Rollback()
  1186. return
  1187. }
  1188. }
  1189. utx.Commit()
  1190. }
  1191. return
  1192. }
  1193. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1194. if len(salesReturnInfo) > 0 {
  1195. utx := writeDb.Begin()
  1196. if len(salesReturnInfo) > 0 {
  1197. 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 "
  1198. insertParams := make([]string, 0)
  1199. insertData := make([]interface{}, 0)
  1200. for _, info := range salesReturnInfo {
  1201. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1202. insertData = append(insertData, info.DrugId)
  1203. insertData = append(insertData, info.SalesReturnId)
  1204. insertData = append(insertData, info.Count)
  1205. insertData = append(insertData, info.Price)
  1206. insertData = append(insertData, info.Total)
  1207. insertData = append(insertData, info.Ctime)
  1208. insertData = append(insertData, info.Status)
  1209. insertData = append(insertData, info.OrgId)
  1210. insertData = append(insertData, info.OrderNumber)
  1211. insertData = append(insertData, info.Type)
  1212. insertData = append(insertData, info.Dealer)
  1213. insertData = append(insertData, info.Manufacturer)
  1214. insertData = append(insertData, info.RetailPrice)
  1215. insertData = append(insertData, info.RetailTotalPrice)
  1216. }
  1217. thisSQL += strings.Join(insertParams, ", ")
  1218. err = utx.Exec(thisSQL, insertData...).Error
  1219. if err != nil {
  1220. utx.Rollback()
  1221. return
  1222. }
  1223. }
  1224. utx.Commit()
  1225. }
  1226. return
  1227. }
  1228. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1229. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1230. return total, err
  1231. }
  1232. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1233. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1234. return total, err
  1235. }
  1236. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1237. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1238. if err != gorm.ErrRecordNotFound {
  1239. if err != nil {
  1240. tx.Rollback()
  1241. return total, err
  1242. }
  1243. }
  1244. return total, err
  1245. }
  1246. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1247. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1248. return total, err
  1249. }
  1250. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1251. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1252. if err != gorm.ErrRecordNotFound {
  1253. if err != nil {
  1254. tx.Rollback()
  1255. return total, err
  1256. }
  1257. }
  1258. return total, err
  1259. }
  1260. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1261. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1262. // return total, err
  1263. //}
  1264. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1265. db := readDb.Model(&models.SalesReturn{})
  1266. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1267. if len(keywords) > 0 {
  1268. likeKey := "%" + keywords + "%"
  1269. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1270. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1271. 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")
  1272. }
  1273. if startTime > 0 {
  1274. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1275. }
  1276. if endTime > 0 {
  1277. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1278. }
  1279. db = db.Count(&total)
  1280. offset := (page - 1) * limit
  1281. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1282. return
  1283. }
  1284. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1285. db := readDb.Model(&models.DrugSalesReturn{})
  1286. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1287. if len(keywords) > 0 {
  1288. likeKey := "%" + keywords + "%"
  1289. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1290. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1291. 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")
  1292. }
  1293. if startTime > 0 {
  1294. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1295. }
  1296. if endTime > 0 {
  1297. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1298. }
  1299. db = db.Count(&total)
  1300. offset := (page - 1) * limit
  1301. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1302. return
  1303. }
  1304. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1305. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1306. return list, err
  1307. }
  1308. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1309. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1310. return list, err
  1311. }
  1312. type DrugCancelStockInfo struct {
  1313. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1314. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1315. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1316. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1317. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1318. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1319. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1320. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1321. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1322. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1323. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1324. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1325. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1326. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1327. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1328. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1329. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1330. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1331. Number string `gorm:"column:number" json:"number" form:"number"`
  1332. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1333. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1334. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1335. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1336. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1337. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1338. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1339. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1340. }
  1341. func (DrugCancelStockInfo) TableName() string {
  1342. return "xt_drug_cancel_stock_info"
  1343. }
  1344. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1345. err = readDb.Model(&DrugCancelStockInfo{}).Preload("Drug", "status = 1 AND org_id = ? ", org_id).Preload("DrugWarehouseInfo", "status = 1 AND org_id = ? ", org_id).Where("cancel_stock_id = ? AND status = 1", id).Find(&list).Error
  1346. return list, err
  1347. }
  1348. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1349. db := readDb.Model(&models.WarehousingInfo{})
  1350. db = db.Where("org_id = ? AND status = 1", orgId)
  1351. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1352. if startTime > 0 {
  1353. db = db.Where("ctime >=?", startTime)
  1354. }
  1355. if endTime > 0 {
  1356. db = db.Where("ctime<= ?", endTime)
  1357. }
  1358. db = db.Count(&total)
  1359. offset := (page - 1) * limit
  1360. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1361. return list, total, err
  1362. }
  1363. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1364. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1365. return total, err
  1366. }
  1367. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1368. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1369. return total, err
  1370. }
  1371. func FindAllSelfDrugWarehouseOut(org_id int64, tx *gorm.DB) (total int64, err error) {
  1372. err = tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1373. if err != gorm.ErrRecordNotFound {
  1374. if err != nil {
  1375. tx.Rollback()
  1376. return
  1377. }
  1378. }
  1379. return total, err
  1380. }
  1381. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut) error {
  1382. err := writeDb.Create(&warehouseOut).Error
  1383. return err
  1384. }
  1385. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1386. err := tx.Create(&warehouseOut).Error
  1387. if err != nil {
  1388. tx.Rollback()
  1389. return err
  1390. }
  1391. return err
  1392. }
  1393. func UpdateSingleWarehoseOut(id int64, storehouse_id int64) error {
  1394. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1395. return err
  1396. }
  1397. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1398. err := writeDb.Create(&warehouseOut).Error
  1399. return err
  1400. }
  1401. func GetSigleDrugWarehouseOutByPatientId(warehousing_time int64, patient_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouse, error) {
  1402. selfDrugWarehouse := models.XtSelfDrugWarehouse{}
  1403. err := tx.Where("warehousing_time = ? and patient_id = ? and is_sys =1 and status=1", warehousing_time, patient_id).Find(&selfDrugWarehouse).Error
  1404. if err != gorm.ErrRecordNotFound {
  1405. if err != nil {
  1406. tx.Rollback()
  1407. return selfDrugWarehouse, err
  1408. }
  1409. }
  1410. return selfDrugWarehouse, err
  1411. }
  1412. func AddSelfSigleDrugWarehouseOut(warehouseOut *models.XtSelfDrugWarehouseOut, tx *gorm.DB) error {
  1413. err := tx.Create(&warehouseOut).Error
  1414. if err != nil {
  1415. tx.Rollback()
  1416. return err
  1417. }
  1418. return err
  1419. }
  1420. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1421. err := tx.Create(&warehouseOut).Error
  1422. if err != nil {
  1423. tx.Rollback()
  1424. return err
  1425. }
  1426. return err
  1427. }
  1428. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1429. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1430. return err
  1431. }
  1432. func UpdateSelfSingleDrugWarehouseOut(id int64, patient_id int64, tx *gorm.DB) error {
  1433. err := tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"patient_id": patient_id}).Error
  1434. return err
  1435. }
  1436. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1437. if len(warehouseOutInfo) > 0 {
  1438. utx := writeDb.Begin()
  1439. if len(warehouseOutInfo) > 0 {
  1440. 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 "
  1441. insertParams := make([]string, 0)
  1442. insertData := make([]interface{}, 0)
  1443. for _, info := range warehouseOutInfo {
  1444. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1445. insertData = append(insertData, info.WarehouseOutId)
  1446. insertData = append(insertData, info.GoodId)
  1447. insertData = append(insertData, info.GoodTypeId)
  1448. insertData = append(insertData, info.ProductDate)
  1449. insertData = append(insertData, info.ExpiryDate)
  1450. insertData = append(insertData, info.Count)
  1451. insertData = append(insertData, info.Price)
  1452. insertData = append(insertData, info.TotalPrice)
  1453. insertData = append(insertData, info.Remark)
  1454. insertData = append(insertData, info.Ctime)
  1455. insertData = append(insertData, info.Status)
  1456. insertData = append(insertData, info.OrgId)
  1457. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1458. insertData = append(insertData, info.Type)
  1459. insertData = append(insertData, info.Dealer)
  1460. insertData = append(insertData, info.Manufacturer)
  1461. insertData = append(insertData, info.Number)
  1462. insertData = append(insertData, info.LicenseNumber)
  1463. }
  1464. thisSQL += strings.Join(insertParams, ", ")
  1465. err = utx.Exec(thisSQL, insertData...).Error
  1466. if err != nil {
  1467. utx.Rollback()
  1468. return
  1469. }
  1470. }
  1471. utx.Commit()
  1472. }
  1473. return
  1474. }
  1475. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1476. if len(warehouseOutInfo) > 0 {
  1477. utx := writeDb.Begin()
  1478. if len(warehouseOutInfo) > 0 {
  1479. 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 "
  1480. insertParams := make([]string, 0)
  1481. insertData := make([]interface{}, 0)
  1482. for _, info := range warehouseOutInfo {
  1483. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1484. insertData = append(insertData, info.WarehouseOutId)
  1485. insertData = append(insertData, info.DrugId)
  1486. insertData = append(insertData, info.ProductDate)
  1487. insertData = append(insertData, info.ExpiryDate)
  1488. insertData = append(insertData, info.Count)
  1489. insertData = append(insertData, info.Price)
  1490. insertData = append(insertData, info.TotalPrice)
  1491. insertData = append(insertData, info.Remark)
  1492. insertData = append(insertData, info.Ctime)
  1493. insertData = append(insertData, info.Status)
  1494. insertData = append(insertData, info.OrgId)
  1495. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1496. insertData = append(insertData, info.Type)
  1497. insertData = append(insertData, info.Dealer)
  1498. insertData = append(insertData, info.Manufacturer)
  1499. insertData = append(insertData, info.RetailPrice)
  1500. insertData = append(insertData, info.RetailTotalPrice)
  1501. insertData = append(insertData, info.Number)
  1502. insertData = append(insertData, info.BatchNumber)
  1503. insertData = append(insertData, info.CountUnit)
  1504. }
  1505. thisSQL += strings.Join(insertParams, ", ")
  1506. err = utx.Exec(thisSQL, insertData...).Error
  1507. if err != nil {
  1508. utx.Rollback()
  1509. return
  1510. }
  1511. }
  1512. utx.Commit()
  1513. }
  1514. return
  1515. }
  1516. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1517. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1518. return info, err
  1519. }
  1520. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1521. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1522. return info, err
  1523. }
  1524. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1525. db := readDb.Model(&models.WarehouseOut{})
  1526. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1527. if len(keywords) > 0 {
  1528. likeKey := "%" + keywords + "%"
  1529. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1530. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1531. 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")
  1532. }
  1533. if startTime > 0 {
  1534. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1535. }
  1536. if endTime > 0 {
  1537. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1538. }
  1539. db = db.Count(&total)
  1540. offset := (page - 1) * limit
  1541. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1542. return list, total, err
  1543. }
  1544. func FindAllWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, way_type int64, check_type int64) (list []*models.WarehouseOut, total int64, err error) {
  1545. db := readDb.Model(&models.WarehouseOut{})
  1546. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1547. if len(keywords) > 0 {
  1548. likeKey := "%" + keywords + "%"
  1549. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1550. 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")
  1551. }
  1552. if startTime > 0 {
  1553. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1554. }
  1555. if endTime > 0 {
  1556. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1557. }
  1558. if storehouse_id > 0 {
  1559. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1560. }
  1561. if way_type == 0 {
  1562. db = db.Where("xt_warehouse_out.is_sys = 0 or xt_warehouse_out.is_sys = 1 or xt_warehouse_out.is_sys = 12 or xt_warehouse_out.is_sys = 5")
  1563. }
  1564. if way_type == 1 {
  1565. db = db.Where("xt_warehouse_out.is_sys = 0")
  1566. }
  1567. if way_type == 2 {
  1568. db = db.Where("xt_warehouse_out.is_sys = 1")
  1569. }
  1570. if way_type == 3 {
  1571. db = db.Where("xt_warehouse_out.is_sys = 12")
  1572. }
  1573. if way_type == 4 {
  1574. db = db.Where("xt_warehouse_out.is_sys = 5")
  1575. }
  1576. if check_type > 0 {
  1577. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1578. }
  1579. if check_type == 0 {
  1580. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1581. }
  1582. db = db.Count(&total)
  1583. offset := (page - 1) * limit
  1584. if orgId == 10191 {
  1585. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1586. } else {
  1587. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1588. }
  1589. return list, total, err
  1590. }
  1591. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1592. db := readDb.Model(&models.DrugWarehouseOut{})
  1593. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1594. if len(keywords) > 0 {
  1595. likeKey := "%" + keywords + "%"
  1596. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1597. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1598. 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")
  1599. }
  1600. if startTime > 0 {
  1601. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1602. }
  1603. if endTime > 0 {
  1604. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1605. }
  1606. db = db.Count(&total)
  1607. offset := (page - 1) * limit
  1608. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1609. return list, total, err
  1610. }
  1611. func FindAllDrugWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, way_type int64, check_type int64) (list []*models.DrugWarehouseOut, total int64, err error) {
  1612. db := readDb.Model(&models.DrugWarehouseOut{})
  1613. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1614. if len(keywords) > 0 {
  1615. likeKey := "%" + keywords + "%"
  1616. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1617. 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")
  1618. }
  1619. if startTime > 0 {
  1620. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1621. }
  1622. if endTime > 0 {
  1623. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1624. }
  1625. if storehouse_id > 0 {
  1626. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1627. }
  1628. if check_type > 0 {
  1629. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1630. }
  1631. if way_type == 0 {
  1632. db = db.Where("xt_drug_warehouse_out.is_sys = 0 || xt_drug_warehouse_out.is_sys = 1 || xt_drug_warehouse_out.is_sys = 12 || xt_drug_warehouse_out.is_sys = 5")
  1633. }
  1634. if way_type == 1 {
  1635. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1636. }
  1637. if way_type == 2 {
  1638. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1639. }
  1640. if way_type == 3 {
  1641. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1642. }
  1643. if way_type == 4 {
  1644. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1645. }
  1646. db = db.Count(&total)
  1647. offset := (page - 1) * limit
  1648. if orgId == 10191 {
  1649. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1650. } else {
  1651. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1652. }
  1653. return list, total, err
  1654. }
  1655. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1656. 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
  1657. return list, err
  1658. }
  1659. type DrugWarehouseOutInfo struct {
  1660. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1661. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1662. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1663. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1664. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1665. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1666. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1667. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1668. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1669. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1670. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1671. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1672. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1673. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1674. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1675. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1676. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1677. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1678. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1679. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1680. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1681. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1682. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1683. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1684. Number string `gorm:"column:number" json:"number" form:"number"`
  1685. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1686. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1687. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1688. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1689. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1690. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1691. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1692. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1693. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1694. ClassType string `gorm:"column:class_type" json:"class_type" form:"class_type"`
  1695. }
  1696. func (DrugWarehouseOutInfo) TableName() string {
  1697. return "xt_drug_warehouse_out_info"
  1698. }
  1699. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1700. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1701. return list, err
  1702. }
  1703. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1704. info := &models.WarehousingInfo{}
  1705. type WarehouseInfoCountAmount struct {
  1706. Total int64
  1707. }
  1708. var result WarehouseInfoCountAmount
  1709. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1710. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1711. return result.Total, info
  1712. }
  1713. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1714. type SalesReturnCountAmount struct {
  1715. Total int64
  1716. }
  1717. var result SalesReturnCountAmount
  1718. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1719. return result.Total, err
  1720. }
  1721. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1722. type WarehouseOutInfoCountAmount struct {
  1723. Total int64
  1724. }
  1725. var result WarehouseOutInfoCountAmount
  1726. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1727. return result.Total, err
  1728. }
  1729. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1730. type CancleStockoCountAmount struct {
  1731. Total int64
  1732. }
  1733. var result CancleStockoCountAmount
  1734. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1735. return result.Total, err
  1736. }
  1737. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1738. info := models.WarehousingInfo{}
  1739. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  1740. return info, err
  1741. }
  1742. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  1743. stock := models.CancelStock{}
  1744. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  1745. if err == gorm.ErrRecordNotFound {
  1746. return nil, err
  1747. }
  1748. if err != nil {
  1749. return nil, err
  1750. }
  1751. return &stock, nil
  1752. }
  1753. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  1754. stock := models.CancelStock{}
  1755. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1756. if err == gorm.ErrRecordNotFound {
  1757. return nil, err
  1758. }
  1759. if err != nil {
  1760. return nil, err
  1761. }
  1762. return &stock, nil
  1763. }
  1764. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  1765. stock := models.CancelStock{}
  1766. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1767. if err != gorm.ErrRecordNotFound {
  1768. if err != nil {
  1769. tx.Rollback()
  1770. return &stock, err
  1771. }
  1772. }
  1773. return &stock, err
  1774. }
  1775. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  1776. err := writeDb.Create(&cancelStock).Error
  1777. return err
  1778. }
  1779. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  1780. err := tx.Create(&cancelStock).Error
  1781. if err != nil {
  1782. tx.Rollback()
  1783. return err
  1784. }
  1785. return err
  1786. }
  1787. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  1788. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1789. return err
  1790. }
  1791. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  1792. stock := models.CancelStock{}
  1793. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1794. return stock, err
  1795. }
  1796. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  1797. stock := models.CancelStock{}
  1798. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1799. if err != gorm.ErrRecordNotFound {
  1800. if err != nil {
  1801. tx.Rollback()
  1802. return stock, err
  1803. }
  1804. }
  1805. return stock, err
  1806. }
  1807. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  1808. stock := models.DrugCancelStock{}
  1809. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1810. return stock, err
  1811. }
  1812. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  1813. stock := models.DrugCancelStock{}
  1814. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1815. if err != gorm.ErrRecordNotFound {
  1816. if err != nil {
  1817. tx.Rollback()
  1818. return stock, err
  1819. }
  1820. }
  1821. return stock, err
  1822. }
  1823. func GetManufactureById(id int64) (models.Manufacturer, error) {
  1824. manufacturer := models.Manufacturer{}
  1825. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1826. return manufacturer, err
  1827. }
  1828. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  1829. manufacturer := models.Manufacturer{}
  1830. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1831. if err != gorm.ErrRecordNotFound {
  1832. if err != nil {
  1833. tx.Rollback()
  1834. return manufacturer, err
  1835. }
  1836. }
  1837. return manufacturer, err
  1838. }
  1839. func GetDealerById(id int64) (models.Dealer, error) {
  1840. dealer := models.Dealer{}
  1841. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1842. return dealer, err
  1843. }
  1844. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  1845. dealer := models.Dealer{}
  1846. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1847. if err != gorm.ErrRecordNotFound {
  1848. if err != nil {
  1849. tx.Rollback()
  1850. return dealer, err
  1851. }
  1852. }
  1853. return dealer, err
  1854. }
  1855. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  1856. err := writeDb.Create(&cancelStock).Error
  1857. return err
  1858. }
  1859. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  1860. err := tx.Create(&cancelStock).Error
  1861. if err != nil {
  1862. tx.Rollback()
  1863. return err
  1864. }
  1865. return err
  1866. }
  1867. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  1868. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1869. return err
  1870. }
  1871. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  1872. // err := tx.Create(&cancelStock).Error
  1873. // return err
  1874. //}
  1875. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  1876. if len(cancelStockInfo) > 0 {
  1877. utx := writeDb.Begin()
  1878. if len(cancelStockInfo) > 0 {
  1879. 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,storehouse_id) VALUES "
  1880. insertParams := make([]string, 0)
  1881. insertData := make([]interface{}, 0)
  1882. for _, info := range cancelStockInfo {
  1883. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1884. insertData = append(insertData, info.GoodId)
  1885. insertData = append(insertData, info.CancelStockId)
  1886. insertData = append(insertData, info.GoodTypeId)
  1887. insertData = append(insertData, info.Count)
  1888. insertData = append(insertData, info.Price)
  1889. insertData = append(insertData, info.Total)
  1890. insertData = append(insertData, info.Ctime)
  1891. insertData = append(insertData, info.Status)
  1892. insertData = append(insertData, info.OrgId)
  1893. insertData = append(insertData, info.OrderNumber)
  1894. insertData = append(insertData, info.Type)
  1895. insertData = append(insertData, info.Dealer)
  1896. insertData = append(insertData, info.Manufacturer)
  1897. insertData = append(insertData, info.Number)
  1898. insertData = append(insertData, info.RegisterAccount)
  1899. insertData = append(insertData, info.Remark)
  1900. insertData = append(insertData, info.ProductDate)
  1901. insertData = append(insertData, info.ExpiryDate)
  1902. insertData = append(insertData, info.WarehouseInfoId)
  1903. insertData = append(insertData, info.WarehouseInfoId)
  1904. }
  1905. thisSQL += strings.Join(insertParams, ", ")
  1906. err = utx.Exec(thisSQL, insertData...).Error
  1907. if err != nil {
  1908. utx.Rollback()
  1909. return
  1910. }
  1911. }
  1912. utx.Commit()
  1913. }
  1914. return
  1915. }
  1916. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  1917. err := XTWriteDB().Create(&info).Error
  1918. return err
  1919. }
  1920. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  1921. // err := tx.Create(&info).Error
  1922. // return err
  1923. //}
  1924. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  1925. info := models.DrugCancelStockInfo{}
  1926. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1927. return info, err
  1928. }
  1929. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  1930. // info := models.DrugCancelStockInfo{}
  1931. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1932. // return info, err
  1933. //}
  1934. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  1935. err := XTWriteDB().Create(&info).Error
  1936. return err
  1937. }
  1938. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  1939. err := XTWriteDB().Create(&info).Error
  1940. if err != nil {
  1941. tx.Rollback()
  1942. return err
  1943. }
  1944. return err
  1945. }
  1946. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  1947. info := models.CancelStockInfo{}
  1948. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1949. return info, err
  1950. }
  1951. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  1952. info := models.CancelStockInfo{}
  1953. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1954. if err != gorm.ErrRecordNotFound {
  1955. if err != nil {
  1956. tx.Rollback()
  1957. return info, err
  1958. }
  1959. }
  1960. return info, err
  1961. }
  1962. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  1963. info := models.DrugCancelStockInfo{}
  1964. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  1965. return info, err
  1966. }
  1967. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  1968. if len(cancelStockInfo) > 0 {
  1969. utx := writeDb.Begin()
  1970. if len(cancelStockInfo) > 0 {
  1971. 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 "
  1972. insertParams := make([]string, 0)
  1973. insertData := make([]interface{}, 0)
  1974. for _, info := range cancelStockInfo {
  1975. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1976. insertData = append(insertData, info.DrugId)
  1977. insertData = append(insertData, info.CancelStockId)
  1978. insertData = append(insertData, info.Count)
  1979. insertData = append(insertData, info.Price)
  1980. insertData = append(insertData, info.Total)
  1981. insertData = append(insertData, info.Ctime)
  1982. insertData = append(insertData, info.Status)
  1983. insertData = append(insertData, info.OrgId)
  1984. insertData = append(insertData, info.OrderNumber)
  1985. insertData = append(insertData, info.Type)
  1986. insertData = append(insertData, info.Dealer)
  1987. insertData = append(insertData, info.Manufacturer)
  1988. insertData = append(insertData, info.RetailPrice)
  1989. insertData = append(insertData, info.RetailTotalPrice)
  1990. insertData = append(insertData, info.RegisterAccount)
  1991. insertData = append(insertData, info.BatchNumber)
  1992. insertData = append(insertData, info.Remark)
  1993. insertData = append(insertData, info.MaxUnit)
  1994. insertData = append(insertData, info.ProductDate)
  1995. insertData = append(insertData, info.ExpiryDate)
  1996. insertData = append(insertData, info.BatchNumberId)
  1997. }
  1998. thisSQL += strings.Join(insertParams, ", ")
  1999. err = utx.Exec(thisSQL, insertData...).Error
  2000. if err != nil {
  2001. utx.Rollback()
  2002. return
  2003. }
  2004. }
  2005. utx.Commit()
  2006. }
  2007. return
  2008. }
  2009. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  2010. db := readDb.Model(&models.CancelStock{})
  2011. db = db.Where("org_id = ? AND status = 1", orgId)
  2012. err = db.Order("ctime desc").Find(&list).Error
  2013. return
  2014. }
  2015. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2016. db := readDb.Model(&models.WarehousingInfo{})
  2017. db = db.Where("org_id = ? AND status = 1", orgId)
  2018. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2019. if startTime > 0 {
  2020. db = db.Where("ctime >=?", startTime)
  2021. }
  2022. if endTime > 0 {
  2023. db = db.Where("ctime<= ?", endTime)
  2024. }
  2025. db = db.Count(&total)
  2026. offset := (page - 1) * limit
  2027. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2028. return
  2029. }
  2030. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  2031. db := XTReadDB().Model(&models.StockInfo{})
  2032. if startime > 0 {
  2033. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2034. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  2035. })
  2036. }
  2037. if startime == 0 || endtime == 0 {
  2038. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2039. return db.Where("org_id = ? AND status = 1", orgId)
  2040. })
  2041. }
  2042. if endtime > 0 {
  2043. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2044. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  2045. })
  2046. }
  2047. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  2048. db = db.Group("xt_good_information.id")
  2049. if startime == 0 || endtime == 0 {
  2050. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2051. return db.Where("org_id = ? AND status = 1", orgId)
  2052. })
  2053. }
  2054. if startime > 0 {
  2055. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2056. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  2057. })
  2058. }
  2059. if endtime > 0 {
  2060. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2061. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  2062. })
  2063. }
  2064. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  2065. if startime == 0 || endtime == 0 {
  2066. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  2067. }
  2068. if startime > 0 {
  2069. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  2070. }
  2071. if endtime > 0 {
  2072. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2073. }
  2074. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2075. if len(keyword) > 0 {
  2076. likeKey := "%" + keyword + "%"
  2077. 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)
  2078. 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")
  2079. }
  2080. if type_name > 0 {
  2081. 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)
  2082. }
  2083. db = db.Count(&total)
  2084. offset := (page - 1) * limit
  2085. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2086. return
  2087. }
  2088. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2089. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2090. return
  2091. }
  2092. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2093. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2094. return
  2095. }
  2096. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2097. 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
  2098. return
  2099. }
  2100. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2101. 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
  2102. return
  2103. }
  2104. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2105. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2106. "good_id": warehouseInfo.GoodId,
  2107. "number": warehouseInfo.Number,
  2108. "product_date": warehouseInfo.ProductDate,
  2109. "expiry_date": warehouseInfo.ExpiryDate,
  2110. "warehousing_count": warehouseInfo.WarehousingCount,
  2111. "price": warehouseInfo.Price,
  2112. "total_price": warehouseInfo.TotalPrice,
  2113. "mtime": warehouseInfo.Mtime,
  2114. "remark": warehouseInfo.Remark,
  2115. }).Error
  2116. return
  2117. }
  2118. func UpDateWarehouseStatus(id int64) (err error) {
  2119. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2120. return
  2121. }
  2122. func UpdatedStockFlowById(id int64) (err error) {
  2123. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2124. return
  2125. }
  2126. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2127. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2128. return
  2129. }
  2130. func UpdateDrugFlowById(id int64) (err error) {
  2131. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2132. return
  2133. }
  2134. func DeleteWarehouse(ids []string) (err error) {
  2135. ut := writeDb.Begin()
  2136. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2137. if err != nil {
  2138. ut.Rollback()
  2139. return
  2140. }
  2141. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2142. if err != nil {
  2143. ut.Rollback()
  2144. return
  2145. }
  2146. ut.Commit()
  2147. return
  2148. }
  2149. func DeleteStockFlow(ids []string) (err error) {
  2150. ut := writeDb.Begin()
  2151. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2152. if err != nil {
  2153. ut.Rollback()
  2154. return
  2155. }
  2156. ut.Commit()
  2157. return
  2158. }
  2159. func DeleteDrugWarehouse(ids []string) (err error) {
  2160. ut := writeDb.Begin()
  2161. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2162. if err != nil {
  2163. ut.Rollback()
  2164. return
  2165. }
  2166. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2167. if err != nil {
  2168. ut.Rollback()
  2169. return
  2170. }
  2171. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2172. if err != nil {
  2173. ut.Rollback()
  2174. return
  2175. }
  2176. ut.Commit()
  2177. return
  2178. }
  2179. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2180. warehouse := &models.Warehousing{}
  2181. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2182. return warehouse, err
  2183. }
  2184. func FindWareHouseOutById(id int64) (*models.WarehouseOut, error) {
  2185. warehouseOut := &models.WarehouseOut{}
  2186. err := readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2187. return warehouseOut, err
  2188. }
  2189. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2190. warehouseOut := &models.DrugWarehouseOut{}
  2191. 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
  2192. return warehouseOut, err
  2193. }
  2194. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2195. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2196. return
  2197. }
  2198. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2199. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2200. return
  2201. }
  2202. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2203. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2204. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2205. return
  2206. }
  2207. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2208. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2209. return
  2210. }
  2211. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2212. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2213. if manufacturer_id > 0 {
  2214. db = db.Where("manufacturer = ?", manufacturer_id)
  2215. }
  2216. if dealer_id > 0 {
  2217. db = db.Where("dealer = ?", dealer_id)
  2218. }
  2219. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2220. return goodInfo, err
  2221. }
  2222. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2223. db := readDb.Model(&models.Warehousing{})
  2224. likekey := "%" + keywords + "%"
  2225. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2226. db = db.Where("warehousing_order LIKE ? ", likekey)
  2227. db = db.Count(&total)
  2228. offset := (page - 1) * limit
  2229. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2230. return
  2231. }
  2232. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2233. info := models.WarehousingInfo{}
  2234. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2235. return info, err
  2236. }
  2237. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2238. info := models.WarehousingInfo{}
  2239. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2240. return info, err
  2241. }
  2242. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2243. err = writeDb.Save(&info).Error
  2244. return err
  2245. }
  2246. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2247. err = writeDb.Save(&info).Error
  2248. return err
  2249. }
  2250. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2251. err = writeDb.Model(&info).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "batch_number": info.BatchNumber, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "retail_price": info.RetailPrice, "stock_max_number": info.StockMaxNumber, "stock_min_number": info.StockMinNumber, "storehouse_id": info.StorehouseId}).Error
  2252. return err
  2253. }
  2254. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2255. err = writeDb.Model(&info).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "batch_number": info.BatchNumber, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "retail_price": info.RetailPrice, "storehouse_id": info.StorehouseId}).Error
  2256. return err
  2257. }
  2258. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2259. info := models.DrugWarehouseInfo{}
  2260. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2261. return info, err
  2262. }
  2263. func EditWarehousing(warehouse models.Warehousing) {
  2264. 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, "storehouse_id": warehouse.StorehouseId}).Error
  2265. }
  2266. func EditDrugWarehousing(warehouse models.DrugWarehouse) {
  2267. 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
  2268. }
  2269. func FindAllWarehouse(org_id int64, types int64) (list []*models.Warehousing, err error) {
  2270. 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
  2271. return list, err
  2272. }
  2273. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2274. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2275. 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)
  2276. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2277. return list, err
  2278. }
  2279. func DeleteSalesReturn(ids []string) (err error) {
  2280. ut := writeDb.Begin()
  2281. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2282. if err != nil {
  2283. ut.Rollback()
  2284. return
  2285. }
  2286. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2287. if err != nil {
  2288. ut.Rollback()
  2289. return
  2290. }
  2291. ut.Commit()
  2292. return
  2293. }
  2294. func DeleteDrugSalesReturn(ids []string) (err error) {
  2295. ut := writeDb.Begin()
  2296. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2297. if err != nil {
  2298. ut.Rollback()
  2299. return
  2300. }
  2301. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2302. if err != nil {
  2303. ut.Rollback()
  2304. return
  2305. }
  2306. ut.Commit()
  2307. return
  2308. }
  2309. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2310. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2311. return
  2312. }
  2313. type DrugSalesReturnInfo struct {
  2314. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2315. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2316. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2317. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2318. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2319. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2320. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2321. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2322. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2323. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2324. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2325. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2326. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2327. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2328. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2329. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2330. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2331. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2332. Number string `gorm:"column:number" json:"number" form:"number"`
  2333. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2334. }
  2335. func (DrugSalesReturnInfo) TableName() string {
  2336. return "xt_drug_sales_return_info"
  2337. }
  2338. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2339. 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
  2340. return
  2341. }
  2342. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2343. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2344. return
  2345. }
  2346. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2347. 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
  2348. return
  2349. }
  2350. func UpDateSaleReturnStatus(id int64) (err error) {
  2351. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2352. return
  2353. }
  2354. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2355. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2356. return
  2357. }
  2358. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2359. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2360. return salesReturn, err
  2361. }
  2362. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2363. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2364. return salesReturn, err
  2365. }
  2366. func EditSaleReturn(sales models.SalesReturn) {
  2367. 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
  2368. }
  2369. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2370. 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
  2371. }
  2372. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2373. err = writeDb.Save(&info).Error
  2374. return err
  2375. }
  2376. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2377. err = writeDb.Save(&info).Error
  2378. return err
  2379. }
  2380. func DeleteWarehouseOut(ids []string) (err error) {
  2381. ut := writeDb.Begin()
  2382. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2383. if err != nil {
  2384. ut.Rollback()
  2385. return
  2386. }
  2387. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2388. if err != nil {
  2389. ut.Rollback()
  2390. return
  2391. }
  2392. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2393. if err != nil {
  2394. ut.Rollback()
  2395. return
  2396. }
  2397. ut.Commit()
  2398. return
  2399. }
  2400. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2401. ut := writeDb.Begin()
  2402. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2403. if err != nil {
  2404. ut.Rollback()
  2405. return
  2406. }
  2407. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2408. if err != nil {
  2409. ut.Rollback()
  2410. return
  2411. }
  2412. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2413. if err != nil {
  2414. ut.Rollback()
  2415. return
  2416. }
  2417. ut.Commit()
  2418. return
  2419. }
  2420. func UpDateWarehouseOutStatus(id int64) (err error) {
  2421. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2422. return
  2423. }
  2424. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2425. ut := writeDb.Begin()
  2426. err = ut.Save(&info).Error
  2427. if err != nil {
  2428. ut.Rollback()
  2429. return err
  2430. }
  2431. ut.Commit()
  2432. return err
  2433. }
  2434. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2435. err = tx.Save(&info).Error
  2436. if err != nil {
  2437. tx.Rollback()
  2438. return err
  2439. }
  2440. return err
  2441. }
  2442. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2443. ut := writeDb.Begin()
  2444. err = ut.Save(&info).Error
  2445. if err != nil {
  2446. ut.Rollback()
  2447. return err
  2448. }
  2449. ut.Commit()
  2450. return err
  2451. }
  2452. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2453. err = tx.Save(&info).Error
  2454. if err != nil {
  2455. tx.Rollback()
  2456. return err
  2457. }
  2458. return err
  2459. }
  2460. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2461. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2462. return
  2463. }
  2464. func EditWarehouseOut(warehouseOut models.WarehouseOut) {
  2465. 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, "storehouse_id": warehouseOut.StorehouseId}).Error
  2466. }
  2467. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2468. 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, "storehouse_id": warehouseOut.StorehouseId}).Error
  2469. }
  2470. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo) (err error) {
  2471. err = writeDb.Save(&info).Error
  2472. return err
  2473. }
  2474. func FindAllCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, storehouse_id int64, check_type int64) (list []*models.CancelStock, total int64, err error) {
  2475. db := readDb.Model(&models.CancelStock{})
  2476. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2477. if len(keywords) > 0 {
  2478. likeKey := "%" + keywords + "%"
  2479. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2480. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2481. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2482. }
  2483. if startTime > 0 {
  2484. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2485. }
  2486. if endTime > 0 {
  2487. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2488. }
  2489. if storehouse_id > 0 {
  2490. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2491. }
  2492. if check_type > 0 {
  2493. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2494. }
  2495. db = db.Count(&total)
  2496. offset := (page - 1) * limit
  2497. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2498. return
  2499. }
  2500. func FindAllDrugCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, storehoue_id int64, check_type int64) (list []*models.DrugCancelStock, total int64, err error) {
  2501. db := readDb.Model(&models.DrugCancelStock{})
  2502. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2503. if len(keywords) > 0 {
  2504. likeKey := "%" + keywords + "%"
  2505. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2506. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2507. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_drug_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_drug_cancel_stock.id")
  2508. }
  2509. if startTime > 0 {
  2510. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2511. }
  2512. if endTime > 0 {
  2513. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2514. }
  2515. if storehoue_id > 0 {
  2516. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2517. }
  2518. if check_type > 0 {
  2519. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2520. }
  2521. db = db.Count(&total)
  2522. offset := (page - 1) * limit
  2523. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2524. return
  2525. }
  2526. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2527. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2528. return info, err
  2529. }
  2530. func DeleteCancelStock(ids []string) (err error) {
  2531. ut := writeDb.Begin()
  2532. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2533. if err != nil {
  2534. ut.Rollback()
  2535. return
  2536. }
  2537. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2538. if err != nil {
  2539. ut.Rollback()
  2540. return
  2541. }
  2542. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2543. if err != nil {
  2544. ut.Rollback()
  2545. return
  2546. }
  2547. ut.Commit()
  2548. return
  2549. }
  2550. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2551. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2552. return info, err
  2553. }
  2554. func DeleteDrugCancelStock(ids []string) (err error) {
  2555. ut := writeDb.Begin()
  2556. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2557. if err != nil {
  2558. ut.Rollback()
  2559. return
  2560. }
  2561. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2562. if err != nil {
  2563. ut.Rollback()
  2564. return
  2565. }
  2566. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2567. if err != nil {
  2568. ut.Rollback()
  2569. return
  2570. }
  2571. ut.Commit()
  2572. return
  2573. }
  2574. func UpDateCancleStockStatus(id int64) (err error) {
  2575. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2576. return
  2577. }
  2578. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2579. info := models.CancelStockInfo{}
  2580. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2581. return info, err
  2582. }
  2583. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2584. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2585. return err
  2586. }
  2587. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2588. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("cancel_out_detail_id = ? and status = 1 and good_id = ?", id, good_id).Updates(map[string]interface{}{"status": 0}).Error
  2589. return
  2590. }
  2591. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2592. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2593. return
  2594. }
  2595. func UpDateDrugCancelStockById(id int64) (err error) {
  2596. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2597. return
  2598. }
  2599. func EditCancelStock(sales models.CancelStock) {
  2600. 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, "storehouse_id": sales.StorehouseId}).Error
  2601. }
  2602. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2603. 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, "storehouse_id": sales.StorehouseId}).Error
  2604. }
  2605. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2606. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2607. return cancelStock, err
  2608. }
  2609. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2610. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2611. return cancelStock, err
  2612. }
  2613. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2614. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2615. return cancelStock, err
  2616. }
  2617. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2618. err = writeDb.Save(&info).Error
  2619. return err
  2620. }
  2621. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2622. err = writeDb.Save(&info).Error
  2623. return err
  2624. }
  2625. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2626. 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
  2627. return list, err
  2628. }
  2629. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2630. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2631. 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)
  2632. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2633. return list, err
  2634. }
  2635. type StockDetailModel struct {
  2636. GoodId int64 `gorm:"good_id" json:"good_id"`
  2637. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2638. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2639. Price float64 `gorm:"price" json:"price"`
  2640. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2641. Type int `gorm:"type" json:"type"`
  2642. Creater int64 `gorm:"creater" json:"creater"`
  2643. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2644. Types int64 `gorm:"types" json:"types"`
  2645. }
  2646. func FindStockInDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.WarehousingInfo, total int64, err error) {
  2647. db := readDb.Model(&models.WarehousingInfo{})
  2648. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2649. if len(keywords) > 0 {
  2650. likeKey := "%" + keywords + "%"
  2651. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2652. 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)
  2653. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2654. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2655. db = db.Where(" sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_info.warehousing_order LIKE ? OR xt_good_information.specification_name LIKE ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_warehouse_info.id")
  2656. } else {
  2657. 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)
  2658. }
  2659. if manufacturer > 0 {
  2660. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2661. }
  2662. if storehouse_id > 0 {
  2663. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2664. }
  2665. if order_type > 0 {
  2666. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2667. }
  2668. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2669. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2670. db = db.Count(&total)
  2671. offset := (page - 1) * limit
  2672. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2673. return
  2674. }
  2675. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2676. db := readDb.Model(&models.WarehousingInfo{})
  2677. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2678. if len(keywords) > 0 {
  2679. likeKey := "%" + keywords + "%"
  2680. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2681. 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)
  2682. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2683. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2684. 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")
  2685. } else {
  2686. 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)
  2687. }
  2688. if manufacturer > 0 {
  2689. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2690. }
  2691. if order_type > 0 {
  2692. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2693. }
  2694. if storehouse_id > 0 {
  2695. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2696. }
  2697. type PriceAmount struct {
  2698. Total float64
  2699. }
  2700. var result []PriceAmount
  2701. err = db.Select("total_price as total").Scan(&result).Error
  2702. var total float64
  2703. for _, value := range result {
  2704. total = total + value.Total
  2705. }
  2706. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2707. return err, total
  2708. }
  2709. func FloatRound(f float64, n int) float64 {
  2710. format := "%." + strconv.Itoa(n) + "f"
  2711. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  2712. return res
  2713. }
  2714. func FindStockOutDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2715. if orgId == 9671 {
  2716. db := readDb.Model(&models.WarehouseOutInfo{})
  2717. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.is_check = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 AND xt_warehouse_out_info.sys_record_time>=? AND xt_warehouse_out_info.sys_record_time<=?", orgId, startTime, endTime)
  2718. if len(keywords) > 0 {
  2719. likeKey := "%" + keywords + "%"
  2720. 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)
  2721. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2722. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2723. 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 ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_warehouse_out_info.id")
  2724. } else {
  2725. 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)
  2726. }
  2727. if manufacturer > 0 {
  2728. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2729. }
  2730. if order_type > 0 {
  2731. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2732. }
  2733. if storehouse_id > 0 {
  2734. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2735. }
  2736. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2737. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2738. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2739. db = db.Count(&total)
  2740. offset := (page - 1) * limit
  2741. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2742. return
  2743. } else {
  2744. db := readDb.Model(&models.WarehouseOutInfo{})
  2745. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.is_check = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 ", orgId)
  2746. if len(keywords) > 0 {
  2747. likeKey := "%" + keywords + "%"
  2748. 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)
  2749. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2750. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2751. 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 ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_warehouse_out_info.id")
  2752. } else {
  2753. 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)
  2754. }
  2755. if manufacturer > 0 {
  2756. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2757. }
  2758. if order_type > 0 {
  2759. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2760. }
  2761. if storehouse_id > 0 {
  2762. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2763. }
  2764. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2765. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2766. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2767. db = db.Count(&total)
  2768. offset := (page - 1) * limit
  2769. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2770. return
  2771. }
  2772. }
  2773. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2774. db := readDb.Model(&models.WarehouseOutInfo{})
  2775. 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)
  2776. if len(keywords) > 0 {
  2777. likeKey := "%" + keywords + "%"
  2778. 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)
  2779. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2780. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2781. 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")
  2782. } else {
  2783. 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)
  2784. }
  2785. if manufacturer > 0 {
  2786. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2787. }
  2788. if order_type > 0 {
  2789. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2790. }
  2791. if storehouse_id > 0 {
  2792. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2793. }
  2794. type PriceAmount struct {
  2795. Total float64
  2796. }
  2797. var result []PriceAmount
  2798. err = db.Select("(count * price) as total").Scan(&result).Error
  2799. var total float64
  2800. for _, value := range result {
  2801. total = total + value.Total
  2802. }
  2803. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2804. fmt.Println(total)
  2805. return err, total
  2806. }
  2807. 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) {
  2808. db := readDb.Model(&models.SalesReturnInfo{})
  2809. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  2810. if len(keywords) > 0 {
  2811. likeKey := "%" + keywords + "%"
  2812. 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)
  2813. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  2814. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  2815. 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 ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_sales_return_info.id")
  2816. } else {
  2817. 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)
  2818. }
  2819. if manufacturer > 0 {
  2820. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  2821. }
  2822. if order_type > 0 {
  2823. db = db.Where("xt_sales_return_info.type = ?", order_type)
  2824. }
  2825. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  2826. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2827. offset := (page - 1) * limit
  2828. db = db.Count(&total)
  2829. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  2830. return
  2831. }
  2832. func FindeCancelGroup(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.CancelStockInfo, err error) {
  2833. db := readDb.Model(&models.CancelStockInfo{})
  2834. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2835. if len(keywords) > 0 {
  2836. likeKey := "%" + keywords + "%"
  2837. 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)
  2838. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2839. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2840. 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")
  2841. } else {
  2842. 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)
  2843. }
  2844. if manufacturer > 0 {
  2845. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2846. }
  2847. if order_type > 0 {
  2848. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2849. }
  2850. if storehouse_id > 0 {
  2851. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  2852. }
  2853. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2854. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2855. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  2856. return
  2857. }
  2858. func FindCancelDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.CancelStockInfo, total int64, err error) {
  2859. db := readDb.Model(&models.CancelStockInfo{})
  2860. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2861. if len(keywords) > 0 {
  2862. likeKey := "%" + keywords + "%"
  2863. 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)
  2864. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2865. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2866. 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 ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_cancel_stock_info.id")
  2867. } else {
  2868. 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)
  2869. }
  2870. if manufacturer > 0 {
  2871. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2872. }
  2873. if order_type > 0 {
  2874. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2875. }
  2876. if storehouse_id > 0 {
  2877. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  2878. }
  2879. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2880. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2881. db = db.Count(&total)
  2882. offset := (page - 1) * limit
  2883. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  2884. return
  2885. }
  2886. func FindStockInByDealerId(id int64) (total int64, err error) {
  2887. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  2888. return
  2889. }
  2890. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  2891. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  2892. return
  2893. }
  2894. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  2895. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  2896. return
  2897. }
  2898. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  2899. 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)
  2900. return
  2901. }
  2902. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  2903. 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)
  2904. return
  2905. }
  2906. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  2907. 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)
  2908. return
  2909. }
  2910. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  2911. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  2912. return goodType, err
  2913. }
  2914. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  2915. 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
  2916. return info, err
  2917. }
  2918. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  2919. 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
  2920. return
  2921. }
  2922. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  2923. err = readDb.Model(&models.WarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? AND second_warehouse_id=?", is_sys, org_id, record_time, second_warehouse_id).First(&out).Error
  2924. return
  2925. }
  2926. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2927. 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
  2928. return
  2929. }
  2930. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  2931. err = tx.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
  2932. if err != gorm.ErrRecordNotFound {
  2933. if err != nil {
  2934. tx.Rollback()
  2935. return
  2936. }
  2937. }
  2938. return
  2939. }
  2940. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2941. 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
  2942. return
  2943. }
  2944. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  2945. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  2946. return
  2947. }
  2948. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  2949. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  2950. return
  2951. }
  2952. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2953. info := models.WarehouseOutInfo{}
  2954. 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
  2955. if err == gorm.ErrRecordNotFound {
  2956. return nil, err
  2957. }
  2958. if err != nil {
  2959. return nil, err
  2960. }
  2961. return &info, nil
  2962. }
  2963. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2964. info := models.WarehouseOutInfo{}
  2965. 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
  2966. if err == gorm.ErrRecordNotFound {
  2967. return nil, err
  2968. }
  2969. if err != nil {
  2970. return nil, err
  2971. }
  2972. return &info, nil
  2973. }
  2974. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2975. 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, "over_count": prepare.OverCount}).Error
  2976. return err
  2977. }
  2978. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2979. 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
  2980. return err
  2981. }
  2982. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo) error {
  2983. err := writeDb.Create(&info).Error
  2984. return err
  2985. }
  2986. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  2987. err := tx.Create(&info).Error
  2988. if err != nil {
  2989. tx.Rollback()
  2990. return err
  2991. }
  2992. return err
  2993. }
  2994. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  2995. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  2996. return err
  2997. }
  2998. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  2999. err := writeDb.Create(&info).Error
  3000. return err
  3001. }
  3002. func AddSelfSigleDrugWarehouseOutInfo(info *models.XtSelfDrugWarehouseOutInfo, tx *gorm.DB) error {
  3003. err := tx.Create(&info).Error
  3004. if err != nil {
  3005. tx.Rollback()
  3006. return err
  3007. }
  3008. return err
  3009. }
  3010. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  3011. err := tx.Create(&info).Error
  3012. if err != nil {
  3013. tx.Rollback()
  3014. return err
  3015. }
  3016. return err
  3017. }
  3018. func UpdateSigleDrugWarehouseOutInfoDetail(id int64, count int64, tx *gorm.DB) error {
  3019. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3020. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("over_count", gorm.Expr("over_count - ?", count)).Error
  3021. if err != nil {
  3022. tx.Rollback()
  3023. return err
  3024. }
  3025. return err
  3026. }
  3027. func UpdateSigleDrugWarehouseOutInfoDetailOne(id int64, count int64, tx *gorm.DB) error {
  3028. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"count": count}).Error
  3029. return err
  3030. }
  3031. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  3032. ut := writeDb.Begin()
  3033. err := ut.Create(&detail).Error
  3034. if err != nil {
  3035. ut.Rollback()
  3036. return err
  3037. }
  3038. ut.Commit()
  3039. return err
  3040. }
  3041. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  3042. err := writeDb.Create(&detail).Error
  3043. return err
  3044. }
  3045. func GetDrugAutoMaticReduceDetail(drug_id int64, patient_id int64, record_time int64, org_id int64, adivce_id int64, warehouse_info_id int64, tx *gorm.DB) (models.DrugAutomaticReduceDetail, error) {
  3046. detail := models.DrugAutomaticReduceDetail{}
  3047. err := tx.Where("drug_id = ? and patient_id =? and record_time =? and org_id = ? and advice_id = ? and warehouse_info_id =?", drug_id, patient_id, record_time, org_id, adivce_id, warehouse_info_id).Find(&detail).Error
  3048. if err != gorm.ErrRecordNotFound {
  3049. if err != nil {
  3050. tx.Rollback()
  3051. return detail, err
  3052. }
  3053. }
  3054. return detail, err
  3055. }
  3056. func UpdateNewSingleDrugAutoReduceRecordInfo(id int64, count int64, tx *gorm.DB) error {
  3057. err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3058. if err != nil {
  3059. tx.Rollback()
  3060. return err
  3061. }
  3062. return err
  3063. }
  3064. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  3065. err := tx.Create(&detail).Error
  3066. if err != nil {
  3067. tx.Rollback()
  3068. return err
  3069. }
  3070. return err
  3071. }
  3072. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  3073. 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
  3074. return
  3075. }
  3076. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  3077. 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
  3078. return
  3079. }
  3080. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  3081. 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
  3082. return
  3083. }
  3084. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  3085. info := models.WarehouseOutInfo{}
  3086. 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
  3087. return info, err
  3088. }
  3089. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  3090. 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
  3091. return
  3092. }
  3093. func UpdateStockOutInfoCount(org_id int64, id int64) {
  3094. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  3095. }
  3096. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3097. db := readDb.Model(&models.AutomaticReduceDetail{})
  3098. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3099. db = db.Preload("GoodsType", "status = 1")
  3100. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3101. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  3102. db = db.Count(&total)
  3103. err = db.Find(&user).Error
  3104. return
  3105. }
  3106. type DrugAutomaticReduceDetail struct {
  3107. ID int64 `gorm:"column:id" json:"id" form:"id"`
  3108. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  3109. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  3110. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  3111. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  3112. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3113. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3114. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3115. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3116. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3117. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3118. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3119. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3120. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3121. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3122. }
  3123. func (DrugAutomaticReduceDetail) TableName() string {
  3124. return "xt_drug_automatic_reduce_detail"
  3125. }
  3126. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3127. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3128. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3129. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3130. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3131. db = db.Count(&total)
  3132. err = db.Find(&user).Error
  3133. return
  3134. }
  3135. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3136. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3137. }
  3138. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3139. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3140. }
  3141. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3142. 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
  3143. return
  3144. }
  3145. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3146. 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
  3147. return
  3148. }
  3149. type VMWarehousingInfo struct {
  3150. ID int64 `gorm:"column:id" json:"id"`
  3151. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3152. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3153. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3154. Status int64 `gorm:"column:status" json:"status"`
  3155. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3156. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3157. }
  3158. func (VMWarehousingInfo) TableName() string {
  3159. return "xt_warehouse_info"
  3160. }
  3161. type GoodInfo struct {
  3162. ID int64 `gorm:"column:id" json:"id"`
  3163. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3164. Status int64 `gorm:"column:status" json:"status"`
  3165. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3166. }
  3167. func (GoodInfo) TableName() string {
  3168. return "xt_good_information"
  3169. }
  3170. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3171. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3172. return
  3173. }
  3174. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3175. 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
  3176. return
  3177. }
  3178. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3179. likeKey := "%" + keyword + "%"
  3180. db := XTReadDB().Model(&goods)
  3181. if len(keyword) > 0 {
  3182. db = db.Where("type_name like ?", likeKey)
  3183. }
  3184. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3185. return goods, err
  3186. }
  3187. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3188. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3189. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3190. return goodstype, err
  3191. }
  3192. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3193. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3194. if orgid > 0 {
  3195. db = db.Where("x.user_org_id = ?", orgid)
  3196. }
  3197. if recordtime > 0 {
  3198. db = db.Where("x.record_date = ?", recordtime)
  3199. }
  3200. if patientId > 0 {
  3201. db = db.Where("x.patient_id = ?", patientId)
  3202. }
  3203. 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
  3204. return prepare, err
  3205. }
  3206. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3207. detail := models.XtAutomaticReduceDetail{}
  3208. 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
  3209. if err == gorm.ErrRecordNotFound {
  3210. return nil, err
  3211. }
  3212. if err != nil {
  3213. return nil, err
  3214. }
  3215. return &detail, nil
  3216. }
  3217. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3218. 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
  3219. return autoMatic, err
  3220. }
  3221. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3222. 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
  3223. return autoMatic, err
  3224. }
  3225. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3226. var manufacturer models.Manufacturer
  3227. var err error
  3228. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3229. if err == gorm.ErrRecordNotFound {
  3230. return nil, err
  3231. }
  3232. if err != nil {
  3233. return nil, err
  3234. }
  3235. return &manufacturer, nil
  3236. }
  3237. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3238. err := XTWriteDB().Create(manufacturer).Error
  3239. return err
  3240. }
  3241. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3242. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3243. return err
  3244. }
  3245. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3246. var good models.GoodInfo
  3247. var err error
  3248. 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
  3249. if err == gorm.ErrRecordNotFound {
  3250. return nil, err
  3251. }
  3252. if err != nil {
  3253. return nil, err
  3254. }
  3255. return &good, nil
  3256. }
  3257. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3258. err := XTWriteDB().Create(&info).Error
  3259. return err
  3260. }
  3261. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3262. 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, "number": info.Number, "register_number": info.RegisterNumber}).Error
  3263. return err
  3264. }
  3265. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3266. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3267. 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
  3268. return houseOutInfo, err
  3269. }
  3270. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3271. key := "%" + keyword + "%"
  3272. if len(keyword) == 0 {
  3273. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3274. } else {
  3275. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3276. }
  3277. return goodInfo, err
  3278. }
  3279. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3280. var drug models.BaseDrugLib
  3281. var err error
  3282. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3283. if err == gorm.ErrRecordNotFound {
  3284. return nil, err
  3285. }
  3286. if err != nil {
  3287. return nil, err
  3288. }
  3289. return &drug, nil
  3290. }
  3291. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3292. var drug models.BaseDrugLib
  3293. var err error
  3294. err = XTReadDB().Model(&drug).Where("drug_name = ? and dose = ? and dose_unit = ? and min_number = ? and min_unit=? and max_unit = ? and status =1 and org_id = ?", drugname, dosage, doseunit, minnumber, minunit, packtunit, orgid).Find(&drug).Error
  3295. if err == gorm.ErrRecordNotFound {
  3296. return nil, err
  3297. }
  3298. if err != nil {
  3299. return nil, err
  3300. }
  3301. return &drug, nil
  3302. }
  3303. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3304. err := XTWriteDB().Create(&info).Error
  3305. return err
  3306. }
  3307. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3308. 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, "single_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
  3309. return err
  3310. }
  3311. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3312. var projoect models.HisProject
  3313. var err error
  3314. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3315. if err == gorm.ErrRecordNotFound {
  3316. return nil, err
  3317. }
  3318. if err != nil {
  3319. return nil, err
  3320. }
  3321. return &projoect, nil
  3322. }
  3323. func CreateProjectInfomation(info *models.HisProject) error {
  3324. err := XTWriteDB().Create(&info).Error
  3325. return err
  3326. }
  3327. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3328. 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
  3329. return err
  3330. }
  3331. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3332. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3333. return warehouseinfo, err
  3334. }
  3335. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3336. err := XTWriteDB().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, "storehouse_id": warehouse.StorehouseId}).Error
  3337. return err
  3338. }
  3339. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3340. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3341. return list, err
  3342. }
  3343. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3344. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3345. return list, err
  3346. }
  3347. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3348. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3349. return list, err
  3350. }
  3351. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3352. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3353. // return list, err
  3354. // }
  3355. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3356. var dealer models.Dealer
  3357. var err error
  3358. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3359. if err == gorm.ErrRecordNotFound {
  3360. return nil, err
  3361. }
  3362. if err != nil {
  3363. return nil, err
  3364. }
  3365. return &dealer, nil
  3366. }
  3367. func CreateDealer(dealer *models.Dealer) error {
  3368. err := XTWriteDB().Create(&dealer).Error
  3369. return err
  3370. }
  3371. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3372. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3373. return err
  3374. }
  3375. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3376. dataconfig := models.DictDataconfig{}
  3377. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3378. return dataconfig, err
  3379. }
  3380. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3381. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3382. return dataconfig, err
  3383. }
  3384. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3385. var dataconfig models.DictDataconfig
  3386. var err error
  3387. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3388. if err == gorm.ErrRecordNotFound {
  3389. return nil, err
  3390. }
  3391. if err != nil {
  3392. return nil, err
  3393. }
  3394. return &dataconfig, nil
  3395. }
  3396. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3397. dataconfig := models.DictDataconfig{}
  3398. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3399. return dataconfig, err
  3400. }
  3401. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3402. err := XTWriteDB().Create(&dataconfig).Error
  3403. return err
  3404. }
  3405. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3406. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3407. return err
  3408. }
  3409. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3410. var goodType models.GoodsType
  3411. var err error
  3412. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3413. if err == gorm.ErrRecordNotFound {
  3414. return nil, err
  3415. }
  3416. if err != nil {
  3417. return nil, err
  3418. }
  3419. return &goodType, nil
  3420. }
  3421. func CreatedGoodType(goodsType *models.GoodsType) error {
  3422. err := XTWriteDB().Create(&goodsType).Error
  3423. return err
  3424. }
  3425. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3426. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3427. return goodsType, err
  3428. }
  3429. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3430. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3431. return goodsType, err
  3432. }
  3433. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3434. var department models.XtHisDepartment
  3435. var err error
  3436. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3437. if err == gorm.ErrRecordNotFound {
  3438. return nil, err
  3439. }
  3440. if err != nil {
  3441. return nil, err
  3442. }
  3443. return &department, nil
  3444. }
  3445. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3446. dataconfig := models.Dataconfig{}
  3447. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3448. return dataconfig, err
  3449. }
  3450. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3451. var dataconfig models.Dataconfig
  3452. var err error
  3453. 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
  3454. if err == gorm.ErrRecordNotFound {
  3455. return nil, err
  3456. }
  3457. if err != nil {
  3458. return nil, err
  3459. }
  3460. return &dataconfig, nil
  3461. }
  3462. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3463. dataconfig := models.Dataconfig{}
  3464. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3465. return dataconfig, err
  3466. }
  3467. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3468. err := XTWriteDB().Create(&dataconfig).Error
  3469. return err
  3470. }
  3471. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3472. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3473. return dataconfig, err
  3474. }
  3475. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3476. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3477. return list, err
  3478. }
  3479. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3480. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3481. if len(keywords) > 0 {
  3482. likekey := "%" + keywords + "%"
  3483. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3484. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3485. }).Find(&drug).Error
  3486. } else {
  3487. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3488. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3489. }).Find(&drug).Error
  3490. }
  3491. return
  3492. }
  3493. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3494. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3495. if len(keywords) > 0 {
  3496. likekey := "%" + keywords + "%"
  3497. err = db.Where("(drug_name LIKE ?)", likekey).Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3498. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3499. }).Find(&drug).Error
  3500. } else {
  3501. err = db.Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3502. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3503. }).Find(&drug).Error
  3504. }
  3505. return
  3506. }
  3507. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3508. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3509. if len(keywords) > 0 {
  3510. likekey := "%" + keywords + "%"
  3511. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3512. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3513. }).Find(&good).Error
  3514. } else {
  3515. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3516. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3517. }).Find(&good).Error
  3518. }
  3519. return
  3520. }
  3521. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3522. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3523. if len(keywords) > 0 {
  3524. likekey := "%" + keywords + "%"
  3525. err = db.Where("(good_name Like ?)", likekey).Preload("LaStWarehousingInfo", "org_id=? and status = 1 and stock_count <> 0 and storehouse_id =?", orgid, storehouse_id).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3526. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3527. }).Find(&good).Error
  3528. } else {
  3529. err = db.Preload("LaStWarehousingInfo", "org_id=? and status = 1 and stock_count <> 0 and storehouse_id =?", orgid, storehouse_id).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3530. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3531. }).Find(&good).Error
  3532. }
  3533. return
  3534. }
  3535. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3536. db := writeDb.Begin()
  3537. err := db.Create(&detail).Error
  3538. if err != nil {
  3539. db.Rollback()
  3540. return err
  3541. }
  3542. db.Commit()
  3543. return err
  3544. }
  3545. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3546. var autoreduece models.BloodAutomaticReduceDetail
  3547. var err error
  3548. 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
  3549. if err == gorm.ErrRecordNotFound {
  3550. return nil, err
  3551. }
  3552. if err != nil {
  3553. return nil, err
  3554. }
  3555. return &autoreduece, nil
  3556. }
  3557. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3558. auto := models.BloodAutomaticReduceDetail{}
  3559. err = XTReadDB().Model(&auto).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(&auto).Error
  3560. return auto, err
  3561. }
  3562. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3563. detail := models.BloodAutomaticReduceDetail{}
  3564. 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, "count": 0}).Error
  3565. return err
  3566. }
  3567. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3568. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and consumable_type = 3 and status = 1", patient_id, recrod_date, goodid).Find(&stockflow).Error
  3569. return stockflow, err
  3570. }
  3571. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3572. err = XTWriteDB().Model(models.BloodAutomaticReduceDetail{}).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ?", orgid, patient_id, recordtime, goodid).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3573. err = XTWriteDB().Model(models.WarehouseOutInfo{}).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3574. err = XTWriteDB().Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? and patient_id = ? and record_date = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3575. return err
  3576. }
  3577. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3578. detail := models.BloodAutomaticReduceDetail{}
  3579. err := XTWriteDB().Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? ", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3580. info := models.WarehouseOutInfo{}
  3581. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3582. prepare := models.DialysisBeforePrepare{}
  3583. err = XTWriteDB().Model(&prepare).Where("user_org_id = ? and patient_id = ? and record_date = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3584. flow := models.VmStockFlow{}
  3585. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"is_read": 1}).Error
  3586. return err
  3587. }
  3588. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3589. detail := models.BloodAutomaticReduceDetail{}
  3590. err := XTWriteDB().Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3591. info := models.WarehouseOutInfo{}
  3592. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3593. prepare := models.DialysisBeforePrepare{}
  3594. err = XTWriteDB().Model(&prepare).Where("user_org_id = ? and patient_id = ? and record_date = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3595. flow := models.VmStockFlow{}
  3596. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"is_read": 1}).Error
  3597. return err
  3598. }
  3599. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  3600. detail := models.BloodAutomaticReduceDetail{}
  3601. err := XTWriteDB().Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? ", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3602. info := models.WarehouseOutInfo{}
  3603. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and order_id = ?", orgid, patient_id, recordtime, goodid, order_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3604. prepare := models.DialysisBeforePrepare{}
  3605. err = XTWriteDB().Model(&prepare).Where("user_org_id = ? and patient_id = ? and record_date = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3606. flow := models.VmStockFlow{}
  3607. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and order_id = ?", orgid, patient_id, recordtime, goodid, order_id).Updates(map[string]interface{}{"is_read": 1}).Error
  3608. return err
  3609. }
  3610. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  3611. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3612. //err = writeDb.Model(&models.VmStockFlow{}).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", id, patient_id, record_time, goodid).UpdateColumn("over_count", gorm.Expr("over_count + ?", count)).Error
  3613. return err
  3614. }
  3615. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3616. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  3617. return info, err
  3618. }
  3619. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  3620. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  3621. return list, err
  3622. }
  3623. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  3624. err = writeDb.Model(&models.VmStockFlow{}).Where("warehousing_id = ? and system_time = ? and good_id = ? and patient_id = ?", id, recordtime, good_id, patient_id).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3625. return err
  3626. }
  3627. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  3628. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  3629. return err
  3630. }
  3631. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  3632. var autoreduece models.BloodAutomaticReduceDetail
  3633. var err error
  3634. 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
  3635. if err == gorm.ErrRecordNotFound {
  3636. return nil, err
  3637. }
  3638. if err != nil {
  3639. return nil, err
  3640. }
  3641. return &autoreduece, nil
  3642. }
  3643. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  3644. detail := models.WarehouseOutInfoSeven{}
  3645. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3646. return detail, err
  3647. }
  3648. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  3649. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3650. return detail, err
  3651. }
  3652. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  3653. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  3654. return project, err
  3655. }
  3656. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  3657. project := models.HisPrescriptionProject{}
  3658. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  3659. return project, err
  3660. }
  3661. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  3662. err = XTReadDB().Select("count,warehouse_info_id,good_id,org_id,sys_record_time,patient_id,project_id,warehouse_info_id,warehouse_out_order_number,storehouse_id,price,dealer,manufacturer,good_type_id,product_date,expiry_date,number,license_number").Where(" patient_id = ? and sys_record_time = ? and status = 1 and is_sys = 1 and count>0", patientid, recorddate).Find(&auto).Error
  3663. return auto, err
  3664. }
  3665. func ModefyWarehouseInfo(count int64, id int64) error {
  3666. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3667. return err
  3668. }
  3669. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  3670. err := XTWriteDB().Model(models.AutomaticReduceDetail{}).Where("patient_id = ? and record_time = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3671. err = XTWriteDB().Model(models.DialysisBeforePrepare{}).Where("patient_id = ? and record_date = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3672. err = XTWriteDB().Model(models.VmStockFlow{}).Where("patient_id = ? and system_time = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"is_read": 1}).Error
  3673. //err = XTWriteDB().Model(models.WarehouseOutInfoNight{}).Where("patient_id = ? and sys_record_time = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3674. return err
  3675. }
  3676. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  3677. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3678. return
  3679. }
  3680. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  3681. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3682. return
  3683. }
  3684. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  3685. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3686. return info, err
  3687. }
  3688. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3689. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3690. return info, err
  3691. }
  3692. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3693. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0 and storehouse_id = ?", good_id, storehouse_id).Find(&info).Error
  3694. return info, err
  3695. }
  3696. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3697. offset := (page - 1) * limit
  3698. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  3699. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3700. fmt.Println(table)
  3701. if startime > 0 {
  3702. db = db.Where("x.ctime>=?", startime)
  3703. }
  3704. if endtime > 0 {
  3705. db = db.Where("x.ctime<=?", endtime)
  3706. }
  3707. likeKey := "%" + keyword + "%"
  3708. if len(likeKey) > 0 {
  3709. db = db.Where("t.good_name like ?", likeKey)
  3710. }
  3711. if orgid > 0 {
  3712. db = db.Where("x.org_id = ?", orgid)
  3713. }
  3714. if good_type > 0 {
  3715. db = db.Where("t.good_type_id = ?", good_type)
  3716. }
  3717. 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
  3718. return info, total, err
  3719. }
  3720. func GetAllGoodInfoStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64, good_id int64, storehouse_id int64) (info []*models.GoodInfo, total int64, err error) {
  3721. offset := (page - 1) * limit
  3722. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  3723. likeKey := "%" + keyword + "%"
  3724. if len(likeKey) > 0 {
  3725. db = db.Where("x.good_name like ?", likeKey)
  3726. }
  3727. if orgid > 0 {
  3728. db = db.Where("x.org_id = ?", orgid)
  3729. }
  3730. if good_type > 0 {
  3731. db = db.Where("x.good_type_id = ?", good_type)
  3732. }
  3733. if good_id > 0 {
  3734. db = db.Where("x.id = ?", good_id)
  3735. }
  3736. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3737. if startime > 0 {
  3738. db = db.Where("ctime>=?", startime)
  3739. }
  3740. if endtime > 0 {
  3741. db = db.Where("ctime<=?", endtime)
  3742. }
  3743. if good_id > 0 {
  3744. db = db.Where("good_id = ?", good_id)
  3745. }
  3746. if storehouse_id > 0 {
  3747. db = db.Where("storehouse_id = ?", storehouse_id)
  3748. }
  3749. return db.Where("status = 1 and is_check = 1")
  3750. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3751. if startime > 0 {
  3752. db = db.Where("ctime>=?", startime)
  3753. }
  3754. if endtime > 0 {
  3755. db = db.Where("ctime<=?", endtime)
  3756. }
  3757. if good_id > 0 {
  3758. db = db.Where("good_id = ?", good_id)
  3759. }
  3760. if storehouse_id > 0 {
  3761. db = db.Where("storehouse_id = ?", storehouse_id)
  3762. }
  3763. return db.Where("status = 1").Group("good_id,storehouse_id")
  3764. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3765. if startime > 0 {
  3766. db = db.Where("ctime>=?", startime)
  3767. }
  3768. if endtime > 0 {
  3769. db = db.Where("ctime<=?", endtime)
  3770. }
  3771. return db.Where("status = 1")
  3772. }).Find(&info).Error
  3773. return info, total, err
  3774. }
  3775. func GetStockListById(good_id int64, orgid int64, limit int64, page int64, startime int64, endtime int64, startfirsttime int64, endfirsttime int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3776. offset := (page - 1) * limit
  3777. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  3778. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3779. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  3780. fmt.Println(table, tables)
  3781. if startime > 0 {
  3782. db = db.Where("x.ctime>=?", startime)
  3783. }
  3784. if endtime > 0 {
  3785. db = db.Where("x.ctime<=?", endtime)
  3786. }
  3787. if startfirsttime > 0 {
  3788. db = db.Where("x.expiry_date >=?", startfirsttime)
  3789. }
  3790. if endfirsttime > 0 {
  3791. db = db.Where("x.expiry_date <=?", endfirsttime)
  3792. }
  3793. if orgid > 0 {
  3794. db = db.Where("x.org_id = ?", orgid)
  3795. }
  3796. if good_id > 0 {
  3797. db = db.Where("x.good_id = ?", good_id)
  3798. }
  3799. 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.packing_price,t.manufacturer,x.remark,x.ctime,x.is_return,x.warehousing_order,x.type,x.storehouse_id,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.min_unit,t.buy_price,f.batch_number_count").Joins("left join xt_good_information as t on t.id = x.good_id").Joins("left join xt_stock_flow as f on f.warehousing_detail_id = x.id").Count(&total).Offset(offset).Limit(limit).Group("x.id").Order("x.id desc").Scan(&info).Error
  3800. return info, total, err
  3801. }
  3802. 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) {
  3803. offset := (page - 1) * limit
  3804. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  3805. if startime > 0 {
  3806. db = db.Where("x.ctime>=?", startime)
  3807. }
  3808. if endtime > 0 {
  3809. db = db.Where("x.ctime<=?", endtime)
  3810. }
  3811. if orgid > 0 {
  3812. db = db.Where("x.org_id = ?", orgid)
  3813. }
  3814. if good_id > 0 {
  3815. db = db.Where("x.good_id = ?", good_id)
  3816. }
  3817. if is_sys == 2 {
  3818. db = db.Where("x.is_sys = 1")
  3819. }
  3820. if is_sys == 3 {
  3821. db = db.Where("x.is_sys = 0")
  3822. }
  3823. 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
  3824. return info, total, err
  3825. }
  3826. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3827. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3828. if startime > 0 {
  3829. db = db.Where("x.ctime >=?", startime)
  3830. }
  3831. if endtime > 0 {
  3832. db = db.Where("x.ctime<=?", endtime)
  3833. }
  3834. if orgid > 0 {
  3835. db = db.Where("x.org_id = ?", orgid)
  3836. }
  3837. 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
  3838. return info, err
  3839. }
  3840. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3841. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3842. if startime > 0 {
  3843. db = db.Where("x.ctime >=?", startime)
  3844. }
  3845. if endtime > 0 {
  3846. db = db.Where("x.ctime<=?", endtime)
  3847. }
  3848. if orgid > 0 {
  3849. db = db.Where("x.org_id = ?", orgid)
  3850. }
  3851. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  3852. return info, err
  3853. }
  3854. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3855. if endtime > 0 {
  3856. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count,x.record_time 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
  3857. }
  3858. if endtime == 0 {
  3859. err = XTReadDB().Raw("SELECT good_id,SUM(b.count) as count FROM (SELECT DISTINCT x.patient_id,x.good_id,x.count,x.record_time FROM xt_automatic_reduce_detail as x WHERE x.org_id = ? and `status` = 1) as b GROUP BY good_id", orgid).Scan(&autoMatic).Error
  3860. }
  3861. return autoMatic, err
  3862. }
  3863. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  3864. out := models.WarehouseOut{}
  3865. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  3866. return out, err
  3867. }
  3868. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3869. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3870. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3871. fmt.Println(table)
  3872. if id > 0 {
  3873. db = db.Where("x.warehouse_out_id = ?", id)
  3874. }
  3875. if orgid > 0 {
  3876. db = db.Where("x.org_id = ?", orgid)
  3877. }
  3878. 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,x.patient_id,t.manufacturer,x.is_sys,x.sys_record_time,x.number,x.remark,x.license_number,x.supply_cancel_out_id,x.supply_warehouse_id,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.register_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,x.warehouse_info_id").Order("x.ctime desc").Scan(&out).Error
  3879. return out, err
  3880. }
  3881. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  3882. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  3883. if len(id) > 0 {
  3884. db = db.Where("x.id in(?)", id)
  3885. }
  3886. if orgid > 0 {
  3887. db = db.Where("x.org_id= ?", orgid)
  3888. }
  3889. 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
  3890. return out, err
  3891. }
  3892. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3893. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3894. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3895. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  3896. fmt.Println(table, tableOne)
  3897. if len(id) > 0 {
  3898. db = db.Where("x.warehouse_out_id in (?)", id)
  3899. }
  3900. if orgid > 0 {
  3901. db = db.Where("x.org_id = ?", orgid)
  3902. }
  3903. 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,x.manufacturer,t.social_security_directory_code,x.is_sys,x.sys_record_time,n.number,x.remark,x.license_number,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.warehouse_info_id,x.remark,x.patient_id,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit,t.packing_price,x.register_number,x.class_type").Joins("left join xt_warehouse_info as n on n.id=x.warehouse_info_id").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
  3904. return out, err
  3905. }
  3906. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3907. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3908. if len(id) > 0 {
  3909. db = db.Where("warehouse_out_id in (?)", id)
  3910. }
  3911. if orgid > 0 {
  3912. db = db.Where("user_org_id = ?", orgid)
  3913. }
  3914. err = db.Group("warehousing_id").Find(&list).Error
  3915. return list, err
  3916. }
  3917. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3918. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3919. if len(id) > 0 {
  3920. db = db.Where("warehouse_out_id in (?)", id)
  3921. }
  3922. if orgid > 0 {
  3923. db = db.Where("user_org_id = ?", orgid)
  3924. }
  3925. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3926. return list, err
  3927. }
  3928. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3929. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  3930. if len(id) > 0 {
  3931. db = db.Where("warehouse_out_id in (?)", id)
  3932. }
  3933. if orgid > 0 {
  3934. db = db.Where("user_org_id = ?", orgid)
  3935. }
  3936. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3937. return list, err
  3938. }
  3939. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  3940. db := XTReadDB().Model(&info)
  3941. db = db.Preload("GoodInfo", "status = 1")
  3942. 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
  3943. return info, err
  3944. }
  3945. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3946. db := readDb.Model(&models.AutomaticReduceDetail{})
  3947. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3948. db = db.Preload("GoodsType", "status = 1")
  3949. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3950. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  3951. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  3952. db = db.Count(&total)
  3953. err = db.Find(&user).Error
  3954. return
  3955. }
  3956. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  3957. info := models.GoodInfo{}
  3958. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3959. return info, err
  3960. }
  3961. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  3962. info := models.GoodInformationThirty{}
  3963. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3964. return info, err
  3965. }
  3966. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  3967. info := models.GoodInformationThirty{}
  3968. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  3969. if err != gorm.ErrRecordNotFound {
  3970. if err != nil {
  3971. tx.Rollback()
  3972. return info, err
  3973. }
  3974. }
  3975. return info, err
  3976. }
  3977. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  3978. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  3979. return err
  3980. }
  3981. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  3982. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3983. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3984. fmt.Println(table)
  3985. if orgid > 0 {
  3986. db = db.Where("x.org_id = ?", orgid)
  3987. }
  3988. if len(orderid) > 0 {
  3989. db = db.Where("x.warehousing_id in(?)", orderid)
  3990. }
  3991. if startime > 0 {
  3992. db = db.Where("x.ctime>=?", startime)
  3993. }
  3994. if endtime > 0 {
  3995. db = db.Where("x.ctime<=?", endtime)
  3996. }
  3997. 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
  3998. return info, err
  3999. }
  4000. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  4001. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4002. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4003. fmt.Println(table)
  4004. if orgid > 0 {
  4005. db = db.Where("x.org_id = ?", orgid)
  4006. }
  4007. if len(orderid) > 0 {
  4008. db = db.Where("x.warehousing_id in(?)", orderid)
  4009. }
  4010. if startime > 0 {
  4011. db = db.Where("x.ctime>=?", startime)
  4012. }
  4013. if endtime > 0 {
  4014. db = db.Where("x.ctime<=?", endtime)
  4015. }
  4016. if storehouse_id > 0 {
  4017. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4018. }
  4019. 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,p.warehousing_time").Joins("left join xt_warehouse as p on p.id =x.warehousing_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&info).Error
  4020. return info, err
  4021. }
  4022. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4023. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4024. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  4025. fmt.Println(table)
  4026. if startime > 0 {
  4027. db = db.Where("x.ctime>=?", startime)
  4028. }
  4029. if endtime > 0 {
  4030. db = db.Where("x.ctime<=?", endtime)
  4031. }
  4032. if orgid > 0 {
  4033. db = db.Where("x.org_id = ?", orgid)
  4034. }
  4035. 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
  4036. return info, err
  4037. }
  4038. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  4039. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  4040. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4041. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  4042. fmt.Println(table, tableOne)
  4043. if orgid > 0 {
  4044. db = db.Where("x.org_id = ?", orgid)
  4045. }
  4046. if orgid > 0 {
  4047. db = db.Where("t.org_id = ?", orgid)
  4048. }
  4049. if orgid > 0 {
  4050. db = db.Where("p.org_id = ?", orgid)
  4051. }
  4052. if len(ids) > 0 {
  4053. db = db.Where("x.warehouse_out_id in (?)", ids)
  4054. }
  4055. if startime > 0 {
  4056. db = db.Where("x.ctime>=?", startime)
  4057. }
  4058. if endtime > 0 {
  4059. db = db.Where("x.ctime<=?", endtime)
  4060. }
  4061. 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,x.warehouse_out_order_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,x.manufacturer,x.dealer,p.warehouse_out_time").Joins("left join xt_warehouse_out as p on p.id =x.warehouse_out_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&out).Error
  4062. return out, err
  4063. }
  4064. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  4065. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  4066. if len(ids) > 0 {
  4067. db = db.Where("x.id in(?)", ids)
  4068. }
  4069. if orgid > 0 {
  4070. db = db.Where("x.org_id = ?", orgid)
  4071. }
  4072. 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
  4073. return info, err
  4074. }
  4075. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4076. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  4077. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  4078. fmt.Println(table)
  4079. if len(ids) > 0 {
  4080. db = db.Where("x.warehouse_out_id in(?)", ids)
  4081. }
  4082. if orgid > 0 {
  4083. db = db.Where("x.org_id = ?", orgid)
  4084. }
  4085. 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
  4086. return info, err
  4087. }
  4088. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  4089. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  4090. likeKey := "%" + keyword + "%"
  4091. if len(keyword) > 0 {
  4092. db = db.Where("x.drug_name like ?", likeKey)
  4093. }
  4094. if orgid > 0 {
  4095. db = db.Where("x.org_id = ?", orgid)
  4096. }
  4097. err = db.Find(&basedrug).Error
  4098. return basedrug, err
  4099. }
  4100. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4101. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4102. if len(ids) > 0 {
  4103. db = db.Where("x.drug_id in(?)", ids)
  4104. }
  4105. if orgid > 0 {
  4106. db = db.Where("x.org_id = ?", orgid)
  4107. }
  4108. if startime > 0 {
  4109. db = db.Where("x.ctime>=?", startime)
  4110. }
  4111. if endtime > 0 {
  4112. db = db.Where("x.ctime<=?", endtime)
  4113. }
  4114. if storehouse_id > 0 {
  4115. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4116. }
  4117. err = db.Find(&info).Error
  4118. return info, err
  4119. }
  4120. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4121. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4122. if len(ids) > 0 {
  4123. db = db.Where("x.drug_id in(?)", ids)
  4124. }
  4125. if orgid > 0 {
  4126. db = db.Where("x.org_id = ?", orgid)
  4127. }
  4128. if startime > 0 {
  4129. db = db.Where("x.ctime>=?", startime)
  4130. }
  4131. if endtime > 0 {
  4132. db = db.Where("x.ctime<=?", endtime)
  4133. }
  4134. if storehouse_id > 0 {
  4135. db = db.Where("storehouse_id = ?", storehouse_id)
  4136. }
  4137. err = db.Find(&info).Error
  4138. return info, err
  4139. }
  4140. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4141. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4142. if drugid > 0 {
  4143. db = db.Where("x.drug_id = ?", drugid)
  4144. }
  4145. if orgid > 0 {
  4146. db = db.Where("x.org_id = ?", orgid)
  4147. }
  4148. if storehouse_id > 0 {
  4149. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4150. }
  4151. 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
  4152. return info, err
  4153. }
  4154. func GetDrugTotalCountTwoTwety(drugid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info models.VmDrugWarehouseInfo, err error) {
  4155. db := tx.Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4156. if drugid > 0 {
  4157. db = db.Where("x.drug_id = ?", drugid)
  4158. }
  4159. if orgid > 0 {
  4160. db = db.Where("x.org_id = ?", orgid)
  4161. }
  4162. if storehouse_id > 0 {
  4163. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4164. }
  4165. 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
  4166. if err != gorm.ErrRecordNotFound {
  4167. if err != nil {
  4168. tx.Rollback()
  4169. return info, err
  4170. }
  4171. }
  4172. return info, err
  4173. }
  4174. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4175. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4176. if drugid > 0 {
  4177. db = db.Where("x.drug_id = ?", drugid)
  4178. }
  4179. if orgid > 0 {
  4180. db = db.Where("x.org_id = ?", orgid)
  4181. }
  4182. if storehouse_id > 0 {
  4183. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4184. }
  4185. 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
  4186. return info, err
  4187. }
  4188. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4189. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4190. if id > 0 {
  4191. db = db.Where("x.id = ?", id)
  4192. }
  4193. if orgid > 0 {
  4194. db = db.Where("x.org_id = ?", orgid)
  4195. }
  4196. 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
  4197. return info, err
  4198. }
  4199. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4200. info := models.VmDrugWarehouseInfo{}
  4201. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4202. if id == 0 {
  4203. db = db.Where("id = ?", id)
  4204. }
  4205. if drugid > 0 {
  4206. db = db.Where("x.drug_id = ?", drugid)
  4207. }
  4208. if orgid > 0 {
  4209. db = db.Where("x.org_id = ?", orgid)
  4210. }
  4211. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4212. return info, err
  4213. }
  4214. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4215. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4216. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4217. fmt.Println(table)
  4218. if drugid > 0 {
  4219. db = db.Where("x.drug_id = ?", drugid)
  4220. }
  4221. if storehouse_id > 0 {
  4222. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4223. }
  4224. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,d.min_number").Joins("left join xt_base_drug as d on d.id = x.drug_id").Scan(&list).Error
  4225. return list, err
  4226. }
  4227. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4228. lib := models.BaseDrugLib{}
  4229. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4230. return lib, err
  4231. }
  4232. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4233. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4234. if goodid > 0 {
  4235. db = db.Where("x.good_id = ?", goodid)
  4236. }
  4237. if orgid > 0 {
  4238. db = db.Where("x.org_id = ?", orgid)
  4239. }
  4240. 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
  4241. return info, err
  4242. }
  4243. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4244. 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
  4245. return err
  4246. }
  4247. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4248. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4249. return info, err
  4250. }
  4251. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4252. //遍历循环入库数据
  4253. for i, item := range list {
  4254. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4255. if (item.WarehousingCount - item.StockCount) >= count {
  4256. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4257. info := models.WarehousingInfo{
  4258. GoodId: item.GoodId,
  4259. OrgId: item.OrgId,
  4260. WarehousingId: item.WarehousingId,
  4261. StockCount: item.StockCount + count,
  4262. }
  4263. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4264. fmt.Println("2323233232323232323232323232323", err2)
  4265. return err
  4266. }
  4267. if (item.WarehousingCount - item.StockCount) < count {
  4268. //如果该批次小于退库数量,则先出库该相差数量
  4269. var countOne = item.WarehousingCount - item.StockCount
  4270. infoOne := models.WarehousingInfo{
  4271. GoodId: item.GoodId,
  4272. OrgId: item.OrgId,
  4273. WarehousingId: item.WarehousingId,
  4274. StockCount: countOne + item.StockCount,
  4275. }
  4276. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4277. if err == nil {
  4278. //剩余的出库到第二个批次
  4279. var countTwo = count - countOne
  4280. infos := append(list, list[i+1])
  4281. var idArray []*models.WarehousingInfo
  4282. for _, it := range infos {
  4283. if it.WarehousingId != item.WarehousingId {
  4284. idArray = append(idArray, it)
  4285. }
  4286. }
  4287. //查询更新后的库存
  4288. UpdateCancelOut(idArray, countTwo)
  4289. return err
  4290. } else {
  4291. return err
  4292. }
  4293. }
  4294. }
  4295. return err
  4296. }
  4297. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4298. if len(idstr) > 0 {
  4299. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4300. } else {
  4301. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4302. }
  4303. return info, err
  4304. }
  4305. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4306. if len(idstr) > 0 {
  4307. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4308. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4309. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4310. }).Find(&info).Error
  4311. }
  4312. return info, err
  4313. }
  4314. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4315. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4316. if startime > 0 {
  4317. db = db.Where("x.ctime >=?", startime)
  4318. }
  4319. if endtime > 0 {
  4320. db = db.Where("x.ctime <= ?", endtime)
  4321. }
  4322. if orgid > 0 {
  4323. db = db.Where("x.org_id = ?", orgid)
  4324. }
  4325. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4326. return info, err
  4327. }
  4328. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4329. info := models.WarehouseOutInfo{}
  4330. var err error
  4331. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4332. if err == gorm.ErrRecordNotFound {
  4333. return nil, err
  4334. }
  4335. if err != nil {
  4336. return nil, err
  4337. }
  4338. return &info, nil
  4339. }
  4340. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4341. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4342. if goodid > 0 {
  4343. db = db.Where("x.good_id = ?", goodid)
  4344. }
  4345. if orgid > 0 {
  4346. db = db.Where("x.org_id = ?", orgid)
  4347. }
  4348. if id > 0 {
  4349. db = db.Where("x.warehouse_info_id = ?", id)
  4350. }
  4351. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4352. return info, err
  4353. }
  4354. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4355. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4356. if goodid > 0 {
  4357. db = db.Where("x.good_id = ?", goodid)
  4358. }
  4359. if orgid > 0 {
  4360. db = db.Where("x.user_org_id = ?", orgid)
  4361. }
  4362. if id > 0 {
  4363. db = db.Where("x.warehousing_id = ?", id)
  4364. }
  4365. if storehouse_id > 0 {
  4366. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4367. }
  4368. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4369. return info, err
  4370. }
  4371. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4372. stock := models.CancelStock{}
  4373. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4374. return stock, err
  4375. }
  4376. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4377. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4378. return info, err
  4379. }
  4380. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4381. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4382. return info, err
  4383. }
  4384. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4385. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4386. return stock, err
  4387. }
  4388. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4389. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and (x.consumable_type = 3 or x.consumable_type = 2 or x.consumable_type = 15)")
  4390. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4391. fmt.Println(table)
  4392. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4393. fmt.Println(tableone)
  4394. if id > 0 {
  4395. db = db.Where("x.warehouse_out_id = ?", id)
  4396. }
  4397. if goodid > 0 {
  4398. db = db.Where("x.good_id = ?", goodid)
  4399. }
  4400. 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
  4401. return stock, err
  4402. }
  4403. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4404. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4405. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4406. fmt.Println(table)
  4407. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4408. fmt.Println(tableone)
  4409. if id > 0 {
  4410. db = db.Where("x.warehouse_out_id = ?", id)
  4411. }
  4412. if goodid > 0 {
  4413. db = db.Where("x.good_id = ?", goodid)
  4414. }
  4415. 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
  4416. return stock, err
  4417. }
  4418. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4419. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4420. return info, err
  4421. }
  4422. func ExportDrugList(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseInfo, err error) {
  4423. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1 AND x.org_id=?", org_id)
  4424. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4425. tableOne := XTReadDB().Table("xt_drug_warehouse as p").Where("p.status=1 and p.org_id=?", org_id)
  4426. fmt.Println("table", table)
  4427. fmt.Println("tableOne", tableOne)
  4428. if len(warehouse_out_id) > 0 {
  4429. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4430. }
  4431. 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,p.warehousing_time").Joins("left join xt_drug_warehouse as p on p.id =x.warehousing_id").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  4432. return out, err
  4433. }
  4434. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4435. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4436. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4437. fmt.Println("table", table)
  4438. if len(warehouse_out_id) > 0 {
  4439. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4440. }
  4441. 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
  4442. return out, err
  4443. }
  4444. func GetExportOutOrderDrugListOne(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseOutInfo, err error) {
  4445. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1 and x.user_org_id =?", org_id)
  4446. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4447. fmt.Println("table", table)
  4448. if len(warehouse_out_id) > 0 {
  4449. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4450. }
  4451. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.count,x.max_unit as count_unit,x.price,x.retail_price,x.product_date,x.expire_date,x.warehouse_out_order_number,x.dealer,x.manufacturer,x.is_sys,x.number,x.batch_number,x.patient_id,x.ctime,x.consumable_type,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
  4452. return out, err
  4453. }
  4454. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4455. db := XTReadDB().Model(&info).Where("status = 1")
  4456. if len(warehouse_out_id) > 0 {
  4457. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4458. }
  4459. if orgid > 0 {
  4460. db = db.Where("org_id = ?", orgid)
  4461. }
  4462. err = db.Find(&info).Error
  4463. return info, err
  4464. }
  4465. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4466. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4467. return out, err
  4468. }
  4469. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4470. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4471. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Preload("DrugWarehouseInfo", "status = 1 and org_id = ?", orgid)
  4472. }).Find(&out).Error
  4473. return out, err
  4474. }
  4475. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4476. info := models.WarehouseOutInfo{}
  4477. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4478. return info, err
  4479. }
  4480. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4481. info := models.WarehousingInfo{}
  4482. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4483. if goodid > 0 {
  4484. db = db.Where("x.good_id = ?", goodid)
  4485. }
  4486. if storehouse_id > 0 {
  4487. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4488. }
  4489. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4490. return info, err
  4491. }
  4492. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4493. info := models.WarehousingInfo{}
  4494. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4495. return info, err
  4496. }
  4497. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4498. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4499. return err
  4500. }
  4501. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4502. if storehouse_id == 0 {
  4503. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4504. }
  4505. if storehouse_id > 0 {
  4506. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0 and storehouse_id=?", good_id, storehouse_id).Order("ctime asc").First(&info).Error
  4507. }
  4508. return info, err
  4509. }
  4510. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4511. if len(warehousingInfo) > 0 {
  4512. utx := writeDb.Begin()
  4513. if len(warehousingInfo) > 0 {
  4514. 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 "
  4515. insertParams := make([]string, 0)
  4516. insertData := make([]interface{}, 0)
  4517. for _, info := range warehousingInfo {
  4518. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4519. insertData = append(insertData, info.WarehousingId)
  4520. insertData = append(insertData, info.GoodId)
  4521. insertData = append(insertData, info.Number)
  4522. insertData = append(insertData, info.LicenseNumber)
  4523. insertData = append(insertData, info.Count)
  4524. insertData = append(insertData, info.UserOrgId)
  4525. insertData = append(insertData, info.PatientId)
  4526. insertData = append(insertData, info.SystemTime)
  4527. insertData = append(insertData, info.ConsumableType)
  4528. insertData = append(insertData, info.IsSys)
  4529. insertData = append(insertData, info.WarehousingOrder)
  4530. insertData = append(insertData, info.WarehouseOutId)
  4531. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4532. insertData = append(insertData, info.IsEdit)
  4533. insertData = append(insertData, info.CancelStockId)
  4534. insertData = append(insertData, info.CancelOrderNumber)
  4535. insertData = append(insertData, info.Manufacturer)
  4536. insertData = append(insertData, info.Dealer)
  4537. insertData = append(insertData, info.Creator)
  4538. insertData = append(insertData, info.UpdateCreator)
  4539. insertData = append(insertData, info.Status)
  4540. insertData = append(insertData, info.Ctime)
  4541. insertData = append(insertData, info.Mtime)
  4542. insertData = append(insertData, info.Price)
  4543. insertData = append(insertData, info.WarehousingDetailId)
  4544. insertData = append(insertData, info.WarehouseOutDetailId)
  4545. insertData = append(insertData, info.CancelOutDetailId)
  4546. insertData = append(insertData, info.ExpireDate)
  4547. insertData = append(insertData, info.ProductDate)
  4548. }
  4549. thisSQL += strings.Join(insertParams, ", ")
  4550. err = utx.Exec(thisSQL, insertData...).Error
  4551. if err != nil {
  4552. utx.Rollback()
  4553. return
  4554. }
  4555. }
  4556. utx.Commit()
  4557. }
  4558. return
  4559. }
  4560. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4561. utx := XTWriteDB().Begin()
  4562. err := utx.Create(&flow).Error
  4563. if err != nil {
  4564. utx.Rollback()
  4565. return err
  4566. }
  4567. utx.Commit()
  4568. return err
  4569. }
  4570. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4571. err := tx.Create(&flow).Error
  4572. if err != nil {
  4573. tx.Rollback()
  4574. return err
  4575. }
  4576. return err
  4577. }
  4578. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4579. err := XTWriteDB().Create(&flow).Error
  4580. return err
  4581. }
  4582. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4583. err := XTWriteDB().Save(&flow).Error
  4584. return err
  4585. }
  4586. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4587. err = XTWriteDB().Model(&flow).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", warehousing_id, patient_id, record_time, good_id).Updates(map[string]interface{}{"number": flow.Number, "license_number": flow.LicenseNumber, "count": flow.Count, "expire_date": flow.ExpireDate, "product_date": flow.ProductDate, "price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "over_count": flow.OverCount}).Error
  4588. return err
  4589. }
  4590. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  4591. err = XTWriteDB().Model(&flow).Where("warehouse_out_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", warehouse_out_id, patient_id, record_time, good_id).Updates(map[string]interface{}{"number": flow.Number, "license_number": flow.LicenseNumber, "count": flow.Count, "expire_date": flow.ExpireDate, "product_date": flow.ProductDate, "price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "over_count": flow.OverCount}).Error
  4592. return err
  4593. }
  4594. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  4595. stock := models.VmStockFlow{}
  4596. var err error
  4597. err = XTReadDB().Model(&stock).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and is_read = 0", warehousing_id, patient_id, record_time, good_id).Find(&stock).Error
  4598. if err == gorm.ErrRecordNotFound {
  4599. return nil, err
  4600. }
  4601. if err != nil {
  4602. return nil, err
  4603. }
  4604. return &stock, nil
  4605. }
  4606. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4607. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3", patient_id, record_time, good_id).Find(&flow).Error
  4608. return flow, nil
  4609. }
  4610. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4611. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4612. return flow, nil
  4613. }
  4614. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4615. err = tx.Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4616. if err != gorm.ErrRecordNotFound {
  4617. if err != nil {
  4618. tx.Rollback()
  4619. return flow, nil
  4620. }
  4621. }
  4622. return flow, nil
  4623. }
  4624. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4625. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4626. return flow, nil
  4627. }
  4628. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  4629. err = tx.Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4630. if err != gorm.ErrRecordNotFound {
  4631. if err != nil {
  4632. tx.Rollback()
  4633. return flow, nil
  4634. }
  4635. }
  4636. return flow, nil
  4637. }
  4638. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  4639. err = XTReadDB().Where("patient_id = ? and record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4640. return flow, nil
  4641. }
  4642. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  4643. err = tx.Where("patient_id = ? and record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4644. if err != gorm.ErrRecordNotFound {
  4645. if err != nil {
  4646. tx.Rollback()
  4647. return flow, err
  4648. }
  4649. }
  4650. return flow, err
  4651. }
  4652. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4653. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  4654. return flow, nil
  4655. }
  4656. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4657. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7", patient_id, record_time, good_id).Find(&flow).Error
  4658. return flow, nil
  4659. }
  4660. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4661. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4662. return flow, nil
  4663. }
  4664. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4665. err = tx.Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4666. if err != gorm.ErrRecordNotFound {
  4667. if err != nil {
  4668. tx.Rollback()
  4669. return flow, nil
  4670. }
  4671. }
  4672. return flow, nil
  4673. }
  4674. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4675. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4676. return flow, nil
  4677. }
  4678. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  4679. info := models.WarehouseOutInfo{}
  4680. var err error
  4681. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4682. if err == gorm.ErrRecordNotFound {
  4683. return nil, err
  4684. }
  4685. if err != nil {
  4686. return nil, err
  4687. }
  4688. return &info, nil
  4689. }
  4690. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  4691. info := models.WarehouseOutInfo{}
  4692. var err error
  4693. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and is_sys=1", good_id, record_time, patient_id).Find(&info).Error
  4694. if err == gorm.ErrRecordNotFound {
  4695. return nil, err
  4696. }
  4697. if err != nil {
  4698. return nil, err
  4699. }
  4700. return &info, nil
  4701. }
  4702. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  4703. info := models.WarehouseOutInfo{}
  4704. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and is_sys=1", good_id, record_time, patient_id).Find(&info).Error
  4705. return info, err
  4706. }
  4707. 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) {
  4708. offset := (page - 1) * limit
  4709. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  4710. if consumable_type > 0 {
  4711. //入库
  4712. if consumable_type == 1 {
  4713. db = db.Where("consumable_type = ?", consumable_type)
  4714. }
  4715. //2 手动出库
  4716. if consumable_type == 2 {
  4717. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  4718. }
  4719. //3.自动出库
  4720. if consumable_type == 3 {
  4721. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  4722. }
  4723. //4.手动退库
  4724. if consumable_type == 4 {
  4725. db = db.Where(" consumable_type = ?", consumable_type)
  4726. }
  4727. //自动退库
  4728. if consumable_type == 7 {
  4729. db = db.Where(" consumable_type = ?", consumable_type)
  4730. }
  4731. if consumable_type == 10 {
  4732. db = db.Where(" consumable_type = ?", consumable_type)
  4733. }
  4734. if consumable_type == 11 {
  4735. db = db.Where(" consumable_type = ?", consumable_type)
  4736. }
  4737. if consumable_type == 12 {
  4738. db = db.Where(" consumable_type = ?", consumable_type)
  4739. }
  4740. if consumable_type == 13 {
  4741. db = db.Where(" consumable_type = ?", consumable_type)
  4742. }
  4743. if consumable_type == 15 {
  4744. db = db.Where(" consumable_type = ?", consumable_type)
  4745. }
  4746. }
  4747. if orgId > 0 {
  4748. db = db.Where("user_org_id = ?", orgId)
  4749. }
  4750. if startTime > 0 {
  4751. db = db.Where("ctime >= ?", startTime)
  4752. }
  4753. if endTime > 0 {
  4754. db = db.Where("ctime<=?", endTime)
  4755. }
  4756. if good_id > 0 {
  4757. db = db.Where("good_id = ?", good_id)
  4758. }
  4759. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  4760. return flow, total, err
  4761. }
  4762. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  4763. detail := models.AutomaticReduceDetail{}
  4764. 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
  4765. return detail, err
  4766. }
  4767. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  4768. detail := models.WarehouseOutInfo{}
  4769. 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
  4770. return detail, err
  4771. }
  4772. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  4773. outInfo := models.WarehouseOutInfo{}
  4774. 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, "over_count": info.OverCount}).Error
  4775. return err
  4776. }
  4777. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  4778. outInfo := models.WarehouseOutInfo{}
  4779. err := tx.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, "over_count": info.OverCount}).Error
  4780. if err != nil {
  4781. tx.Rollback()
  4782. return err
  4783. }
  4784. return err
  4785. }
  4786. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  4787. outInfo := models.WarehouseOutInfo{}
  4788. err := XTWriteDB().Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1 and project_id = ? and warehouse_info_id =? ", good_id, patient_id, record_time, project_id, warehouse_info_id).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, "over_count": info.OverCount}).Error
  4789. return err
  4790. }
  4791. func UpdatedNewWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) error {
  4792. outInfo := models.WarehouseOutInfo{}
  4793. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1 and project_id = ? and warehouse_info_id =? ", good_id, patient_id, record_time, project_id, warehouse_info_id).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, "over_count": info.OverCount}).Error
  4794. if err != nil {
  4795. tx.Rollback()
  4796. return err
  4797. }
  4798. return err
  4799. }
  4800. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  4801. info := models.WarehouseOutInfo{}
  4802. 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
  4803. return info, err
  4804. }
  4805. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  4806. info := models.WarehousingInfo{}
  4807. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4808. return info, err
  4809. }
  4810. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  4811. info := models.WarehousingInfo{}
  4812. db := XTReadDB().Model(&info)
  4813. if good_id > 0 {
  4814. db = db.Where("good_id = ? and status = 1", good_id)
  4815. }
  4816. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  4817. return info, err
  4818. }
  4819. 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) {
  4820. offset := (page - 1) * limit
  4821. db := XTReadDB().Model(drug_flow).Where("status = 1")
  4822. if drugid > 0 {
  4823. db = db.Where("drug_id = ?", drugid)
  4824. }
  4825. if orgid > 0 {
  4826. db = db.Where("user_org_id = ?", orgid)
  4827. }
  4828. if startTime > 0 {
  4829. db = db.Where("ctime >=? ", startTime)
  4830. }
  4831. if endTime > 0 {
  4832. db = db.Where("ctime<=?", endTime)
  4833. }
  4834. if stock_type > 0 {
  4835. //手动入库
  4836. if stock_type == 1 {
  4837. db = db.Where("consumable_type = 1")
  4838. }
  4839. //手动出库
  4840. if stock_type == 2 {
  4841. db = db.Where("consumable_type = 2 and is_sys = 0")
  4842. }
  4843. //自动出库
  4844. if stock_type == 3 {
  4845. db = db.Where("consumable_type = 3 and is_sys = 1")
  4846. }
  4847. if stock_type == 4 {
  4848. db = db.Where("consumable_type = 4")
  4849. }
  4850. if stock_type == 10 {
  4851. db = db.Where("consumable_type = 10")
  4852. }
  4853. if stock_type == 11 {
  4854. db = db.Where("consumable_type = 11")
  4855. }
  4856. if stock_type == 12 {
  4857. db = db.Where("consumable_type = 12")
  4858. }
  4859. if stock_type == 13 {
  4860. db = db.Where("consumable_type = 13")
  4861. }
  4862. if stock_type == 15 {
  4863. db = db.Where("consumable_type = 15")
  4864. }
  4865. }
  4866. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Preload("BaseDrugLib", "status = 1").Preload("DrugWarehouseInfo", "status = 1").Preload("XtDrugWarehouseOutInfo", "status = 1").Find(&drug_flow).Error
  4867. return drug_flow, total, err
  4868. }
  4869. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  4870. outInfo := models.DrugWarehouseOutInfo{}
  4871. var err error
  4872. 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
  4873. if err == gorm.ErrRecordNotFound {
  4874. return nil, err
  4875. }
  4876. if err != nil {
  4877. return nil, err
  4878. }
  4879. return &outInfo, nil
  4880. }
  4881. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  4882. 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
  4883. return outInfo, err
  4884. }
  4885. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  4886. outInfo := models.DrugWarehouseOutInfo{}
  4887. 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
  4888. return err
  4889. }
  4890. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  4891. db := XTReadDB().Model(&cancle).Where("status = 1")
  4892. if startime > 0 {
  4893. db = db.Where("ctime >=?", startime)
  4894. }
  4895. if endtime > 0 {
  4896. db = db.Where("ctime <=?", endtime)
  4897. }
  4898. if len(ids) > 0 {
  4899. db = db.Where("cancel_stock_id in(?)", ids)
  4900. }
  4901. if orgid > 0 {
  4902. db = db.Where("org_id = ?", orgid)
  4903. }
  4904. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  4905. err = db.Find(&cancle).Error
  4906. return cancle, err
  4907. }
  4908. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  4909. db := XTReadDB().Model(&info)
  4910. if id > 0 {
  4911. db = db.Where("cancel_stock_id = ? and status = 1", id)
  4912. }
  4913. db = db.Preload("BaseDrugLib")
  4914. err = db.Find(&info).Error
  4915. return info, err
  4916. }
  4917. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  4918. info := models.XtDrugWarehouseInfo{}
  4919. db := XTReadDB().Model(&info).Where("status = 1")
  4920. if id > 0 {
  4921. db = db.Where("id = ?", id)
  4922. }
  4923. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4924. return info, err
  4925. }
  4926. func GetDrugWarehouseInfoOne(id int64) (models.XtDrugWarehouseInfo, error) {
  4927. info := models.XtDrugWarehouseInfo{}
  4928. err := XTReadDB().Where("status = 1 and id=?", id).Find(&info).Error
  4929. return info, err
  4930. }
  4931. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  4932. // info := models.XtDrugWarehouseInfo{}
  4933. // db := tx.Model(&info).Where("status = 1")
  4934. // if id > 0 {
  4935. // db = db.Where("id = ?", id)
  4936. // }
  4937. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4938. // return info, err
  4939. //}
  4940. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4941. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  4942. return info, err
  4943. }
  4944. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  4945. err = XTReadDB().Select("id,stock_max_number,stock_min_number").Where("drug_id = ? and org_id =? and status = 1 and storehouse_id = ? and is_check = 1", drugid, orgid, storehouse_id).Find(&info).Error
  4946. return info, err
  4947. }
  4948. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  4949. info := models.HisDoctorAdviceInfo{}
  4950. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  4951. return info, err
  4952. }
  4953. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  4954. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4955. return info, err
  4956. }
  4957. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  4958. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4959. if err != gorm.ErrRecordNotFound {
  4960. if err != nil {
  4961. tx.Rollback()
  4962. return info, err
  4963. }
  4964. }
  4965. return info, err
  4966. }
  4967. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  4968. adviceInfo := models.HisDoctorAdviceInfo{}
  4969. err := tx.Where("patient_id = ? and advice_date= ? and user_org_id = ? and status=1", patient_id, advice_date, user_org_id).Last(&adviceInfo).Error
  4970. if err != gorm.ErrRecordNotFound {
  4971. if err != nil {
  4972. tx.Rollback()
  4973. return adviceInfo, err
  4974. }
  4975. }
  4976. return adviceInfo, err
  4977. }
  4978. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  4979. info := models.HisDoctorAdviceInfo{}
  4980. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1 and id =?", drugid, patient_id, advice_date, advice_id).First(&info).Error
  4981. return info, err
  4982. }
  4983. func GetHisAdviceListByDrugIdNight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  4984. info := models.HisDoctorAdviceInfo{}
  4985. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1 and id =?", drugid, patient_id, advice_date, advice_id).Find(&info).Error
  4986. return info, err
  4987. }
  4988. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  4989. detail := models.DrugAutomaticReduceDetail{}
  4990. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1 and advice_id = ?", drugid, patient_id, advice_date, advice_id).Last(&detail).Error
  4991. return detail, err
  4992. }
  4993. func GetWarehouseOutListExport(drug_id int64, patient_id int64, record_date int64, advice_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  4994. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and advice_id =? and status =1", drug_id, patient_id, record_date, advice_id).Find(&out).Error
  4995. return out, err
  4996. }
  4997. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  4998. err = XTReadDB().Where("advice_id = ? and drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and is_check = 1", advice_id, drugid, patient_id, advice_date).Find(&out).Error
  4999. return out, err
  5000. }
  5001. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  5002. err = tx.Where("advice_id = ? and drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and is_check = 1", advice_id, drugid, patient_id, advice_date).Find(&out).Error
  5003. if err != gorm.ErrRecordNotFound {
  5004. if err != nil {
  5005. tx.Rollback()
  5006. return out, err
  5007. }
  5008. }
  5009. return out, err
  5010. }
  5011. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  5012. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5013. return err
  5014. }
  5015. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  5016. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5017. return err
  5018. }
  5019. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  5020. err := XTWriteDB().Model(models.DrugAutomaticReduceDetail{}).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  5021. err = XTWriteDB().Model(models.DrugWarehouseOutInfo{}).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1", drugid, patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  5022. err = XTWriteDB().Model(models.DrugFlow{}).Where("drug_id = ? and patient_id = ? and system_time = ? and status = 1", drugid, patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  5023. return err
  5024. }
  5025. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5026. err := XTWriteDB().Model(models.DrugAutomaticReduceDetail{}).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1 and advice_id = ?", drugid, patient_id, record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  5027. err = XTWriteDB().Model(models.DrugFlow{}).Where("drug_id = ? and patient_id = ? and system_time = ? and status = 1 and advice_id = ?", drugid, patient_id, record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  5028. return err
  5029. }
  5030. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5031. err := XTWriteDB().Model(models.DrugAutomaticReduceDetail{}).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  5032. err = XTWriteDB().Model(models.DrugFlow{}).Where("drug_id = ? and patient_id = ? and system_time = ? and status = 1 and advice_id = ?", drugid, patient_id, record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  5033. return err
  5034. }
  5035. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  5036. info := models.XtDrugWarehouseInfo{}
  5037. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  5038. return info, err
  5039. }
  5040. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  5041. info := models.XtDrugWarehouseInfo{}
  5042. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5043. return info, err
  5044. }
  5045. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  5046. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5047. return err
  5048. }
  5049. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5050. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5051. // return err
  5052. //}
  5053. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  5054. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  5055. //return err
  5056. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5057. return err
  5058. }
  5059. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5060. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5061. // return err
  5062. //}
  5063. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  5064. info := models.DrugCancelStockInfo{}
  5065. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5066. return info, err
  5067. }
  5068. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  5069. info := models.DrugCancelStockInfo{}
  5070. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  5071. return info, err
  5072. }
  5073. func UpdatedDrugInfo(id int64, count int64) (err error) {
  5074. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5075. return err
  5076. }
  5077. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  5078. err := XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_stock_id = ? and drug_id = ? and status = 1", cancelstockid, drug_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5079. return err
  5080. }
  5081. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  5082. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5083. return err
  5084. }
  5085. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5086. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1 and id = ?", drugid, orgid, warehouse_info_id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5087. return err
  5088. }
  5089. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  5090. 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
  5091. return err
  5092. }
  5093. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5094. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1 and id = ?", drugid, orgid, warehouse_info_id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5095. return err
  5096. }
  5097. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  5098. 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
  5099. return err
  5100. }
  5101. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  5102. info := models.WarehousingInfo{}
  5103. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5104. return info, err
  5105. }
  5106. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5107. info := models.WarehousingInfo{}
  5108. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  5109. return info, err
  5110. }
  5111. func GetLastWarehouseInfoTwo(goodid int64, storehouse_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  5112. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=? and org_id = ? and stock_count > 0", goodid, storehouse_id, user_org_id).Order("ctime").First(&info).Error
  5113. return info, err
  5114. }
  5115. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5116. info := models.WarehousingInfo{}
  5117. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  5118. return info, err
  5119. }
  5120. func CreateWarehouseOut(out models.WarehouseOut) error {
  5121. err := XTWriteDB().Create(&out).Error
  5122. return err
  5123. }
  5124. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  5125. out := models.WarehouseOut{}
  5126. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  5127. return out, err
  5128. }
  5129. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  5130. err := XTWriteDB().Create(&info).Error
  5131. return err
  5132. }
  5133. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  5134. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5135. return err
  5136. }
  5137. func UpdateWareInfoById(id int64, count int64) error {
  5138. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5139. return err
  5140. }
  5141. func UpdateWareInfoByIdSix(id int64, count int64) error {
  5142. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  5143. return err
  5144. }
  5145. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  5146. info := models.CancelStockInfo{}
  5147. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5148. return info, err
  5149. }
  5150. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  5151. info := models.CancelStockInfo{}
  5152. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5153. return info, err
  5154. }
  5155. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5156. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5157. return info, err
  5158. }
  5159. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5160. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5161. return info, err
  5162. }
  5163. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5164. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5165. return err
  5166. }
  5167. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5168. drugFlow := models.DrugFlow{}
  5169. err := XTWriteDB().Model(&drugFlow).Where("drug_id = ? and warehousing_id= ? and status = 1", drugid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count, "max_unit": flow.MaxUnit, "min_unit": flow.MinUnit, "price": flow.Price, "expire_date": flow.ExpireDate}).Error
  5170. return err
  5171. }
  5172. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5173. drugFlow := models.DrugFlow{}
  5174. err := XTWriteDB().Model(&drugFlow).Where("drug_id = ? and warehousing_detail_id= ? and status = 1", drugid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count, "max_unit": flow.MaxUnit, "min_unit": flow.MinUnit, "price": flow.Price, "expire_date": flow.ExpireDate}).Error
  5175. return err
  5176. }
  5177. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5178. stock := models.DrugCancelStock{}
  5179. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5180. return stock, err
  5181. }
  5182. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5183. // stock := models.DrugCancelStock{}
  5184. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5185. // return stock, err
  5186. //}
  5187. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5188. db := XTReadDB().Model(&info)
  5189. if warehouse_info_id > 0 {
  5190. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5191. }
  5192. if drug_id > 0 {
  5193. db = db.Where("drug_id = ? and status =1", drug_id)
  5194. }
  5195. err = db.Find(&info).Error
  5196. return info, err
  5197. }
  5198. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5199. // db := tx.Model(&info)
  5200. // if warehouse_info_id > 0 {
  5201. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5202. // }
  5203. // if drug_id > 0 {
  5204. // db = db.Where("drug_id = ? and status =1", drug_id)
  5205. // }
  5206. // err = db.Find(&info).Error
  5207. // return info, err
  5208. //}
  5209. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5210. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5211. return info, err
  5212. }
  5213. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5214. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5215. // return info, err
  5216. //}
  5217. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5218. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5219. return config, err
  5220. }
  5221. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5222. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5223. return config, err
  5224. }
  5225. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5226. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5227. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5228. fmt.Println(table)
  5229. likeKey := "%" + keyword + "%"
  5230. if orgid > 0 {
  5231. db = db.Where("x.org_id = ?", orgid)
  5232. }
  5233. if storehouse_id > 0 {
  5234. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5235. }
  5236. 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,x.storehouse_id,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
  5237. return info, err
  5238. }
  5239. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5240. err := XTWriteDB().Create(&adjustPrice).Error
  5241. return err
  5242. }
  5243. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5244. likeKey := "%" + keyword + "%"
  5245. offset := (page - 1) * limit
  5246. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5247. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5248. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5249. fmt.Println(table, tab)
  5250. if startime > 0 {
  5251. db = db.Where("x.start_time>=?", startime)
  5252. }
  5253. if endtime > 0 {
  5254. db = db.Where("x.start_time<=?", endtime)
  5255. }
  5256. if len(keyword) > 0 {
  5257. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5258. }
  5259. if orgid > 0 {
  5260. db = db.Where("x.user_org_id = ?", orgid)
  5261. }
  5262. 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
  5263. return list, total, err
  5264. }
  5265. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5266. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5267. return err
  5268. }
  5269. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5270. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5271. return err
  5272. }
  5273. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5274. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5275. return info, err
  5276. }
  5277. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5278. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5279. return adjust, err
  5280. }
  5281. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5282. err := XTWriteDB().Create(&price).Error
  5283. return err
  5284. }
  5285. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5286. likeKey := "%" + keyword + "%"
  5287. offset := (page - 1) * limit
  5288. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5289. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5290. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5291. fmt.Println(table, tab)
  5292. if startime > 0 {
  5293. db = db.Where("x.ctime>=?", startime)
  5294. }
  5295. if endtime > 0 {
  5296. db = db.Where("x.ctime<=?", endtime)
  5297. }
  5298. if len(keyword) > 0 {
  5299. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5300. }
  5301. if orgid > 0 {
  5302. db = db.Where("x.user_org_id = ?", orgid)
  5303. }
  5304. 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
  5305. return adjust, total, err
  5306. }
  5307. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5308. likeKey := "%" + keyword + "%"
  5309. offset := (page - 1) * limit
  5310. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5311. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5312. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5313. fmt.Println(table, tab)
  5314. if len(keyword) > 0 {
  5315. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5316. }
  5317. if orgid > 0 {
  5318. db = db.Where("x.user_org_id = ?", orgid)
  5319. }
  5320. if startime > 0 {
  5321. db = db.Where("x.ctime >=?", startime)
  5322. }
  5323. if endtime > 0 {
  5324. db = db.Where("x.ctime <=?", endtime)
  5325. }
  5326. if storehouse_id > 0 {
  5327. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5328. }
  5329. err = db.Group("x.good_id").Select("x.ctime,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.storehouse_id,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
  5330. return adjust, total, err
  5331. }
  5332. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5333. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5334. if inventory_type == 0 {
  5335. db = db.Where("x.inventory_type = 4")
  5336. }
  5337. if inventory_type > 0 {
  5338. db = db.Where("x.inventory_type = ?", inventory_type)
  5339. }
  5340. if len(keyword) > 0 {
  5341. likeKey := "%" + keyword + "%"
  5342. db = db.Where("x.warehousing_order like ?", likeKey)
  5343. }
  5344. if orgid > 0 {
  5345. db = db.Where("x.user_org_id = ?", orgid)
  5346. }
  5347. err = db.Group("x.good_id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,sum(x.count) as 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").Scan(&adjust).Error
  5348. return adjust, err
  5349. }
  5350. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5351. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5352. if orgid > 0 {
  5353. db = db.Where("x.user_org_id = ?", orgid)
  5354. }
  5355. err = db.Group("x.good_id").Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,sum(x.count) as 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").Scan(&adjust).Error
  5356. return adjust, err
  5357. }
  5358. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5359. 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
  5360. return err
  5361. }
  5362. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5363. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5364. return report, err
  5365. }
  5366. func CreateInentory(inventory models.XtStockInventory) error {
  5367. err = XTWriteDB().Create(&inventory).Error
  5368. return err
  5369. }
  5370. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5371. likeKey := "%" + keyword + "%"
  5372. offset := (page - 1) * limit
  5373. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5374. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5375. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5376. fmt.Println(table, tab)
  5377. if startime > 0 {
  5378. db = db.Where("x.ctime>=?", startime)
  5379. }
  5380. if endtime > 0 {
  5381. db = db.Where("x.ctime<=?", endtime)
  5382. }
  5383. if len(keyword) > 0 {
  5384. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5385. }
  5386. if orgid > 0 {
  5387. db = db.Where("x.user_org_id = ?", orgid)
  5388. }
  5389. if inventory_status > 0 {
  5390. db = db.Where("checker_status = ?", inventory_status)
  5391. }
  5392. 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
  5393. return adjust, total, err
  5394. }
  5395. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5396. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5397. return err
  5398. }
  5399. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5400. info := models.XtStockAdjustPrice{}
  5401. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5402. return info, err
  5403. }
  5404. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5405. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5406. return list, err
  5407. }
  5408. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5409. info := models.WarehousingInfo{}
  5410. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5411. return info, err
  5412. }
  5413. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5414. info := models.WarehousingInfo{}
  5415. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5416. //return info, err
  5417. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5418. if id > 0 {
  5419. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5420. }
  5421. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5422. return info, err
  5423. }
  5424. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5425. info := models.WarehousingInfo{}
  5426. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5427. if id > 0 {
  5428. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5429. }
  5430. if storehouse_id > 0 {
  5431. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5432. }
  5433. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5434. return info, err
  5435. }
  5436. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5437. 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
  5438. return err
  5439. }
  5440. func DeleteStockPrice(id int64) error {
  5441. info := models.XtStockAdjustPrice{}
  5442. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5443. return err
  5444. }
  5445. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5446. report := models.XtStockReportPrice{}
  5447. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5448. return report, err
  5449. }
  5450. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5451. 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
  5452. return err
  5453. }
  5454. func DeleteDamage(id int64) error {
  5455. adjust := models.XtStockReportPrice{}
  5456. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5457. return err
  5458. }
  5459. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5460. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5461. return adjust, err
  5462. }
  5463. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5464. inventory := models.XtStockInventory{}
  5465. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5466. return inventory, err
  5467. }
  5468. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5469. 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
  5470. return err
  5471. }
  5472. func DeleteInventory(id int64) error {
  5473. inventory := models.XtStockInventory{}
  5474. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5475. return err
  5476. }
  5477. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5478. likeKey := "%" + keyword + "%"
  5479. offset := (page - 1) * limit
  5480. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5481. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5482. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5483. fmt.Println(table, tab)
  5484. if len(keyword) > 0 {
  5485. db = db.Where("x.good_name like ?", likeKey)
  5486. }
  5487. if orgid > 0 {
  5488. db = db.Where("x.user_org_id = ?", orgid)
  5489. }
  5490. if storehouse_id > 0 {
  5491. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5492. }
  5493. 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,x.last_stock_count,x.type,x.inventory_type,x.stock_count,x.storehouse_id,x.ctime").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).Order("x.id desc").Scan(&list).Error
  5494. return list, total, err
  5495. }
  5496. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5497. likeKey := "%" + keyword + "%"
  5498. offset := (page - 1) * limit
  5499. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5500. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5501. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5502. fmt.Println(table, tab)
  5503. if len(keyword) > 0 {
  5504. db = db.Where("x.good_name like ?", likeKey)
  5505. }
  5506. if orgid > 0 {
  5507. db = db.Where("x.user_org_id = ?", orgid)
  5508. }
  5509. if storehouse_id > 0 {
  5510. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5511. }
  5512. if len(ids) > 0 {
  5513. db = db.Where("x.id in(?)", ids)
  5514. }
  5515. 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,x.last_stock_count,x.type,x.inventory_type,x.stock_count,x.storehouse_id").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
  5516. return list, total, err
  5517. }
  5518. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5519. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5520. return list, err
  5521. }
  5522. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5523. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5524. return list, err
  5525. }
  5526. func DeleteDrugPrice(id int64) error {
  5527. info := models.XtDrugAdjustPrice{}
  5528. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5529. return err
  5530. }
  5531. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5532. info := models.WarehousingInfo{}
  5533. db := XTReadDB().Model(&info).Where("status = 1")
  5534. if goodid > 0 {
  5535. db = db.Where("good_id = ?", goodid)
  5536. }
  5537. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5538. return info, err
  5539. }
  5540. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5541. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5542. return err
  5543. }
  5544. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5545. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5546. return err
  5547. }
  5548. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5549. err := XTWriteDB().Create(&record).Error
  5550. return err
  5551. }
  5552. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5553. info := models.WarehousingInfo{}
  5554. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5555. //return info, err
  5556. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5557. if id > 0 {
  5558. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5559. }
  5560. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5561. return info, err
  5562. }
  5563. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5564. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5565. return info, err
  5566. }
  5567. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  5568. info := models.DrugWarehouseInfo{}
  5569. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5570. return info, err
  5571. }
  5572. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  5573. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber, "stock_min_number": info.StockMinNumber, "max_unit": info.MaxUnit, "min_unit": info.MinUnit}).Error
  5574. return err
  5575. }
  5576. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  5577. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_max_number": info.StockMaxNumber, "stock_min_number": info.StockMinNumber, "max_unit": info.MaxUnit, "min_unit": info.MinUnit}).Error
  5578. return err
  5579. }
  5580. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  5581. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=? and is_check= 1 and stock_count>0", id, storehouse_id).Preload("GoodInfo", "status = 1").Find(&info).Error
  5582. return info, err
  5583. }
  5584. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  5585. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5586. return err
  5587. }
  5588. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  5589. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  5590. return err
  5591. }
  5592. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  5593. db := XTReadDB().Model(&list).Where(" status = 1")
  5594. if good_id > 0 {
  5595. db = db.Where("good_id = ?", good_id)
  5596. }
  5597. if len(keyword) > 0 {
  5598. likeKey := "%" + keyword + "%"
  5599. db = db.Where("warehousing_order like ?", likeKey)
  5600. }
  5601. if inventory_type > 0 {
  5602. db = db.Where("inventory_type = ?", inventory_type)
  5603. }
  5604. if inventory_type == 0 {
  5605. db = db.Where("inventory_type = 4")
  5606. }
  5607. err = db.Find(&list).Error
  5608. return list, err
  5609. }
  5610. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  5611. flow := models.DrugFlow{}
  5612. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  5613. if err == gorm.ErrRecordNotFound {
  5614. return nil, err
  5615. }
  5616. if err != nil {
  5617. return nil, err
  5618. }
  5619. return &flow, nil
  5620. }
  5621. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  5622. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  5623. return info, err
  5624. }
  5625. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  5626. info := models.XtDrugWarehouseInfo{}
  5627. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  5628. return info, err
  5629. }
  5630. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5631. info := models.WarehousingInfo{}
  5632. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  5633. return info, err
  5634. }
  5635. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  5636. flow := models.VmStockFlow{}
  5637. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  5638. if err == gorm.ErrRecordNotFound {
  5639. return nil, err
  5640. }
  5641. if err != nil {
  5642. return nil, err
  5643. }
  5644. return &flow, nil
  5645. }
  5646. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  5647. info := models.GoodInfo{}
  5648. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  5649. return info, err
  5650. }
  5651. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  5652. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  5653. return err
  5654. }
  5655. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  5656. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  5657. return err
  5658. }
  5659. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  5660. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount, "price": info.Price, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "license_number": info.LicenseNumber, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "remark": info.Remark, "packing_price": info.PackingPrice, "storehouse_id": info.StorehouseId, "register_number": info.RegisterNumber, "good_id": info.GoodId, "good_code": info.GoodCode}).Error
  5661. return err
  5662. }
  5663. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5664. err := XTWriteDB().Model(&flow).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, good_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5665. return err
  5666. }
  5667. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5668. err := XTWriteDB().Model(&flow).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, good_id).Updates(map[string]interface{}{"count": flow.Count, "number": flow.Number, "product_date": flow.ProductDate, "expire_date": flow.ExpireDate, "Price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "license_number": flow.LicenseNumber, "storehouse_id": flow.StorehouseId, "register_number": flow.RegisterNumber}).Error
  5669. return err
  5670. }
  5671. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  5672. err := XTWriteDB().Model(&flow).Where("warehouse_out_detail_id = ? and status = 1", wareoutdetailid).Updates(map[string]interface{}{"count": flow.Count, "number": flow.Number, "product_date": flow.ProductDate, "expire_date": flow.ExpireDate, "Price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "license_number": flow.LicenseNumber, "price": flow.Price, "storehouse_id": flow.StorehouseId, "admin_user_id": flow.AdminUserId, "stock_count": flow.StockCount, "register_number": flow.RegisterNumber}).Error
  5673. return err
  5674. }
  5675. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  5676. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  5677. return err
  5678. }
  5679. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  5680. if orgid == 10653 {
  5681. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0 and x.stock_count>0")
  5682. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5683. fmt.Println(table)
  5684. if orgid > 0 {
  5685. db = db.Where("x.org_id = ?", orgid)
  5686. }
  5687. err = db.Select("x.id,x.good_id,x.number,x.number,x.product_date,x.expiry_date,x.warehousing_unit,x.dealer,x.manufacturer,t.good_name,t.specification_name,t.packing_unit,t.remark").Joins("left join xt_good_information as t on t.id = x.good_id").Group("x.good_id,x.number").Scan(&info).Error
  5688. return info, err
  5689. } else {
  5690. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0")
  5691. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5692. fmt.Println(table)
  5693. if orgid > 0 {
  5694. db = db.Where("x.org_id = ?", orgid)
  5695. }
  5696. err = db.Select("x.id,x.good_id,x.number,x.number,x.product_date,x.expiry_date,x.warehousing_unit,x.dealer,x.manufacturer,t.good_name,t.specification_name,t.packing_unit,t.remark").Joins("left join xt_good_information as t on t.id = x.good_id").Group("x.good_id,x.number").Scan(&info).Error
  5697. return info, err
  5698. }
  5699. }
  5700. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  5701. warehouseInfo := models.XtDrugWarehouseInfo{}
  5702. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "batch_number": info.BatchNumber, "max_unit": info.MaxUnit, "storehouse_id": info.StorehouseId}).Error
  5703. return err
  5704. }
  5705. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  5706. err := XTWriteDB().Create(&info).Error
  5707. return err
  5708. }
  5709. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  5710. err := tx.Create(&info).Error
  5711. if err != nil {
  5712. tx.Rollback()
  5713. return err
  5714. }
  5715. return err
  5716. }
  5717. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  5718. flow := models.VmStockFlow{}
  5719. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  5720. if err == gorm.ErrRecordNotFound {
  5721. return nil, err
  5722. }
  5723. if err != nil {
  5724. return nil, err
  5725. }
  5726. return &flow, nil
  5727. }
  5728. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  5729. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5730. return err
  5731. }
  5732. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  5733. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count, "manufacturer": flow.Count, "dealer": flow.Dealer, "number": flow.Number, "license_number": flow.LicenseNumber, "price": flow.Price, "product_date": flow.ProductDate, "expire_date": flow.ExpireDate, "storehouse_id": flow.StorehouseId}).Error
  5734. return err
  5735. }
  5736. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  5737. flow := models.DrugWarehouseOutInfo{}
  5738. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  5739. return flow, err
  5740. }
  5741. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  5742. info := models.WarehouseOutInfo{}
  5743. err := XTReadDB().Model(&info).Where("good_id = ? and patient_id = ? and sys_record_time = ? and status = 1", goodid, patient_id, record_date).First(&info).Error
  5744. return info, err
  5745. }
  5746. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  5747. err := XTWriteDB().Model(&info).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1", drugid, patientid, sys_record_time).Updates(map[string]interface{}{"count": info.Count, "count_unit": info.CountUnit}).Error
  5748. return err
  5749. }
  5750. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  5751. err := XTWriteDB().Model(&info).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and warehouse_info_id = ?", drugid, patientid, sys_record_time, warehouse_info_id).Updates(map[string]interface{}{"count": info.Count, "count_unit": info.CountUnit}).Error
  5752. return err
  5753. }
  5754. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  5755. info := models.DrugWarehouseOutInfo{}
  5756. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and warehouse_info_id = ? and status = 1", drugid, patientid, recorddate, warehosueinfoid).Find(&info).Error
  5757. if err == gorm.ErrRecordNotFound {
  5758. return nil, err
  5759. }
  5760. if err != nil {
  5761. return nil, err
  5762. }
  5763. return &info, nil
  5764. }
  5765. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  5766. drugFlow := models.DrugFlow{}
  5767. err := XTReadDB().Where("warehouse_out_order_number = ? and drug_id =? and patient_id = ? and status = 1", warehouse_out_order_number, drugid, patientid).Find(&drugFlow).Error
  5768. if err == gorm.ErrRecordNotFound {
  5769. return nil, err
  5770. }
  5771. if err != nil {
  5772. return nil, err
  5773. }
  5774. return &drugFlow, nil
  5775. }
  5776. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  5777. err := XTWriteDB().Model(&flow).Where("warehouse_out_order_number = ? and drug_id = ? and patient_id = ? and status = 1", warehouse_out_order_number, drugid, patient_id).Updates(map[string]interface{}{"count": flow.Count, "max_unit": flow.MaxUnit}).Error
  5778. return err
  5779. }
  5780. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  5781. err := XTWriteDB().Model(&flow).Where("warehouse_out_order_number = ? and drug_id = ? and patient_id = ? and status = 1 and warehousing_detail_id = ?", warehouse_out_order_number, drugid, patient_id, warehousing_detail_id).Updates(map[string]interface{}{"count": flow.Count, "max_unit": flow.MaxUnit}).Error
  5782. return err
  5783. }
  5784. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  5785. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5786. return detail, err
  5787. }
  5788. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  5789. detail := models.DrugAutomaticReduceDetail{}
  5790. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5791. if err == gorm.ErrRecordNotFound {
  5792. return nil, err
  5793. }
  5794. if err != nil {
  5795. return nil, err
  5796. }
  5797. return &detail, nil
  5798. }
  5799. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  5800. err := XTWriteDB().Model(&auto).Where("patient_id = ? and record_time = ? and drug_id = ? and status = 1", patientid, recordate, drugid).Updates(map[string]interface{}{"count": auto.Count, "count_unit": auto.CountUnit}).Error
  5801. return err
  5802. }
  5803. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  5804. err := XTWriteDB().Model(&auto).Where("patient_id = ? and record_time = ? and drug_id = ? and status = 1 and warehouse_info_id = ?", patientid, recordate, drugid, warehouse_info_id).Updates(map[string]interface{}{"count": auto.Count, "count_unit": auto.CountUnit}).Error
  5805. return err
  5806. }
  5807. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  5808. info := models.DrugWarehouseInfo{}
  5809. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5810. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5811. return info, err
  5812. }
  5813. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  5814. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5815. return err
  5816. }
  5817. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  5818. info := models.DrugWarehouseInfo{}
  5819. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5820. return err
  5821. }
  5822. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  5823. info := models.DrugCancelStockInfo{}
  5824. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5825. return info, err
  5826. }
  5827. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  5828. info := models.DrugWarehouseInfo{}
  5829. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  5830. return err
  5831. }
  5832. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  5833. info := models.DrugWarehouseInfo{}
  5834. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  5835. return err
  5836. }
  5837. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  5838. err = XTReadDB().Where("drug_id = ? and patient_id = ? and advice_date = ? and execution_state = 1 and status = 1 and user_org_id = ?", drugid, patient_id, advicedate, orgid).Find(&advice).Error
  5839. return advice, err
  5840. }
  5841. func GetAllHisDoctorAdviceByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (advice []*models.HisDoctorAdviceInfo, err error) {
  5842. err = tx.Where("drug_id = ? and patient_id = ? and advice_date = ? and execution_state = 1 and status = 1 and user_org_id = ?", drugid, patient_id, advicedate, orgid).Find(&advice).Error
  5843. if err != gorm.ErrRecordNotFound {
  5844. if err != nil {
  5845. tx.Rollback()
  5846. return advice, err
  5847. }
  5848. }
  5849. return advice, err
  5850. }
  5851. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5852. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", drugid, patient_id, advicedate, orgid).Find(&info).Error
  5853. return info, err
  5854. }
  5855. func GetAllDrugFlowByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5856. err = tx.Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", drugid, patient_id, advicedate, orgid).Find(&info).Error
  5857. if err != gorm.ErrRecordNotFound {
  5858. if err != nil {
  5859. tx.Rollback()
  5860. return info, err
  5861. }
  5862. }
  5863. return info, err
  5864. }
  5865. // 耗材加库存
  5866. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5867. info := models.GoodInfo{}
  5868. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  5869. return info, err
  5870. }
  5871. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5872. info := models.GoodInfo{}
  5873. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  5874. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_count)).Error
  5875. return info, err
  5876. }
  5877. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5878. info := models.GoodInfo{}
  5879. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5880. return info, err
  5881. }
  5882. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5883. info := models.GoodInfo{}
  5884. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5885. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", sum_count)).Error
  5886. return info, err
  5887. }
  5888. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  5889. info := models.GoodInfo{}
  5890. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"batch_retai_price": retail_price}).Error
  5891. return info, err
  5892. }
  5893. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5894. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5895. return info, err
  5896. }
  5897. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5898. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5899. return info, err
  5900. }
  5901. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  5902. info := models.WarehousingInfo{}
  5903. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  5904. return info, err
  5905. }
  5906. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  5907. info := models.WarehousingInfo{}
  5908. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  5909. return info, err
  5910. }
  5911. // 药品加库存
  5912. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5913. info := models.BaseDrugLib{}
  5914. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  5915. return info, err
  5916. }
  5917. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5918. info := models.BaseDrugLib{}
  5919. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  5920. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_count)).Error
  5921. return info, err
  5922. }
  5923. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5924. info := models.BaseDrugLib{}
  5925. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5926. return info, err
  5927. }
  5928. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5929. info := models.BaseDrugLib{}
  5930. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5931. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", sum_count)).Error
  5932. return info, err
  5933. }
  5934. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  5935. info := models.BaseDrugLib{}
  5936. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  5937. err = XTWriteDB().Model(&info).Where("id=? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"batch_retai_price": batch_retai_price}).Error
  5938. return info, err
  5939. }
  5940. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5941. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5942. return info, err
  5943. }
  5944. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5945. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5946. return info, err
  5947. }
  5948. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  5949. info := models.WarehouseOutInfo{}
  5950. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5951. return info, err
  5952. }
  5953. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  5954. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  5955. return out, err
  5956. }
  5957. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  5958. info := models.WarehousingInfo{}
  5959. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5960. return info, err
  5961. }
  5962. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  5963. err = XTReadDB().Where("status = 1").Find(&template).Error
  5964. return template, err
  5965. }
  5966. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  5967. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  5968. return good, err
  5969. }
  5970. func GetMobileGoodList(org_id int64) (list []*models.GoodInformationThirty, err error) {
  5971. err = XTReadDB().Where("org_id = ? and status =1", org_id).Find(&list).Error
  5972. return list, err
  5973. }
  5974. func GetAllMobileProjectList(orgid int64) (project []*models.HisProject, err error) {
  5975. err = XTReadDB().Where("user_org_id =? and status =1", orgid).Find(&project).Error
  5976. return project, err
  5977. }
  5978. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5979. info := models.GoodInfo{}
  5980. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5981. return info, err
  5982. }
  5983. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  5984. db := XTReadDB().Where("status = 1")
  5985. if good_id > 0 {
  5986. db = db.Where("good_id = ?", good_id)
  5987. }
  5988. err = db.Find(&info).Error
  5989. return info, err
  5990. }
  5991. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  5992. db := XTReadDB().Model(&info).Where("status = 1")
  5993. if drug_id > 0 {
  5994. db = db.Where("drug_id = ?", drug_id)
  5995. }
  5996. err = db.Find(&info).Error
  5997. return info, err
  5998. }
  5999. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6000. lib := models.BaseDrugLib{}
  6001. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6002. return lib, err
  6003. }
  6004. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  6005. info := models.Warehousing{}
  6006. err := XTReadDB().Where("is_sys= ? and second_warehouse_id=? and status = 1 and org_id = ? and warehousing_time = ?", is_sys, sencond_wahouse_id, orgid, record_date).Find(&info).Error
  6007. if err == gorm.ErrRecordNotFound {
  6008. return nil, err
  6009. }
  6010. if err != nil {
  6011. return nil, err
  6012. }
  6013. return &info, nil
  6014. }
  6015. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  6016. info := models.Warehousing{}
  6017. err := XTReadDB().Where("is_sys = ? and second_warehouse_id=? and status = 1 and org_id = ? and warehousing_time = ?", is_sys, sencond_wahouse_id, orgid, record_date).Last(&info).Error
  6018. return info, err
  6019. }
  6020. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  6021. out := models.DrugWarehouseOut{}
  6022. err := XTReadDB().Where("org_id = ? and is_sys = ? and second_warehouse_id = ? and warehouse_out_time = ? and status = 1", orgid, is_sys, sencond_wahouse_id, record_date).Find(&out).Error
  6023. if err == gorm.ErrRecordNotFound {
  6024. return nil, err
  6025. }
  6026. if err != nil {
  6027. return nil, err
  6028. }
  6029. return &out, nil
  6030. }
  6031. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  6032. info := models.DrugWarehouseOut{}
  6033. err := XTReadDB().Where("is_sys = ? and second_warehouse_id = ? and status = 1 and org_id =? and warehouse_out_time = ?", is_sys, sencond_wahouse_id, orgid, record_date).Last(&info).Error
  6034. return info, err
  6035. }
  6036. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  6037. info := models.DrugWarehouse{}
  6038. err := XTReadDB().Where("is_sys = ? and second_warehouse_id = ? and status = 1 and org_id = ? and warehousing_time = ?", is_sys, second_warehouse_id, orgid, record_date).Find(&info).Error
  6039. if err == gorm.ErrRecordNotFound {
  6040. return nil, err
  6041. }
  6042. if err != nil {
  6043. return nil, err
  6044. }
  6045. return &info, nil
  6046. }
  6047. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  6048. err := XTWriteDB().Create(&warehouse).Error
  6049. return err
  6050. }
  6051. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  6052. info := models.DrugWarehouse{}
  6053. err := XTReadDB().Where("is_sys = ? and second_warehouse_id = ? and status = 1 and org_id = ? and warehousing_time = ?", is_sys, second_warehouse_id, orgid, record_date).Last(&info).Error
  6054. return info, err
  6055. }
  6056. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6057. info := models.GoodInfo{}
  6058. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", sum_count)).Error
  6059. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", waresing_count)).Error
  6060. return info, err
  6061. }
  6062. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6063. info := models.GoodInfo{}
  6064. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6065. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", waresing_count)).Error
  6066. return info, err
  6067. }
  6068. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6069. tx := XTWriteDB().Begin()
  6070. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count, "sum_in_count": sum_in_count}).Error
  6071. if err != nil {
  6072. tx.Rollback()
  6073. return err
  6074. }
  6075. tx.Commit()
  6076. return err
  6077. }
  6078. func UpdateMedicalSumCountOne(drug_id int64, sum_count int64, orgid int64) error {
  6079. tx := XTWriteDB().Begin()
  6080. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6081. if err != nil {
  6082. tx.Rollback()
  6083. return err
  6084. }
  6085. tx.Commit()
  6086. return err
  6087. }
  6088. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  6089. // err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count, "sum_in_count": sum_in_count}).Error
  6090. // return err
  6091. //}
  6092. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6093. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count, "sum_in_count": sum_in_count}).Error
  6094. return err
  6095. }
  6096. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6097. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6098. table := XTReadDB().Table("xt_base_drug as x d")
  6099. fmt.Println(table)
  6100. if startime > 0 {
  6101. db = db.Where("x.advice_date >= ?", startime)
  6102. }
  6103. if endtime > 0 {
  6104. db = db.Where("x.advice_date <= ?", endtime)
  6105. }
  6106. if orgid > 0 {
  6107. db = db.Where("x.user_org_id = ?", orgid)
  6108. }
  6109. if len(keyword) > 0 {
  6110. likeKey := "%" + keyword + "%"
  6111. db = db.Select("x.patient_id,x.user_org_id,x.id,x.advice_date,x.advice_date,x.prescribing_number_unit,x.drug_id,x.prescribing_number").Joins("left join sgj_xt.xt_base_drug as d on d.id = x.drug_id").Where("d.drug_name like ?", likeKey)
  6112. }
  6113. offset := (page - 1) * limit
  6114. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  6115. return adviceinfo, total, err
  6116. }
  6117. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6118. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6119. table := XTReadDB().Table("xt_base_drug as x d")
  6120. fmt.Println(table)
  6121. if startime > 0 {
  6122. db = db.Where("x.advice_date >= ?", startime)
  6123. }
  6124. if endtime > 0 {
  6125. db = db.Where("x.advice_date <= ?", endtime)
  6126. }
  6127. if orgid > 0 {
  6128. db = db.Where("x.user_org_id = ?", orgid)
  6129. }
  6130. if len(keyword) > 0 {
  6131. likeKey := "%" + keyword + "%"
  6132. db = db.Select("x.patient_id,x.user_org_id,x.id,x.advice_date,x.advice_date,x.prescribing_number_unit,x.drug_id,x.prescribing_number").Joins("left join sgj_xt.xt_base_drug as d on d.id = x.drug_id").Where("d.drug_name like ?", likeKey)
  6133. }
  6134. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  6135. return adviceinfo, total, err
  6136. }
  6137. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  6138. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  6139. return drug, err
  6140. }
  6141. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6142. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6143. table := XTReadDB().Table("xt_good_information as d")
  6144. fmt.Println(table)
  6145. if startime > 0 {
  6146. db = db.Where("x.record_date >= ?", startime)
  6147. }
  6148. if endtime > 0 {
  6149. db = db.Where("x.record_date <= ?", endtime)
  6150. }
  6151. if orgid > 0 {
  6152. db = db.Where("x.user_org_id = ?", orgid)
  6153. }
  6154. if len(keyword) > 0 {
  6155. likeKey := "%" + keyword + "%"
  6156. db = db.Select("x.id,x.project_id,x.user_org_id,x.patient_id,x.record_date,x.count").Joins("left join xt_good_information as d on d.id = x.project_id").Where("d.good_name like ?", likeKey).Group("d.id")
  6157. }
  6158. offset := (page - 1) * limit
  6159. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  6160. return project, total, err
  6161. }
  6162. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6163. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6164. table := XTReadDB().Table("xt_good_information as d")
  6165. fmt.Println(table)
  6166. if startime > 0 {
  6167. db = db.Where("x.record_date >= ?", startime)
  6168. }
  6169. if endtime > 0 {
  6170. db = db.Where("x.record_date <= ?", endtime)
  6171. }
  6172. if orgid > 0 {
  6173. db = db.Where("x.user_org_id = ?", orgid)
  6174. }
  6175. if len(keyword) > 0 {
  6176. likeKey := "%" + keyword + "%"
  6177. db = db.Select("x.id,x.project_id,x.user_org_id,x.patient_id,x.record_date,x.count").Joins("left join xt_good_information as d on d.id = x.project_id").Where("d.good_name like ? ", likeKey).Group("d.id")
  6178. }
  6179. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  6180. return project, total, err
  6181. }
  6182. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  6183. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  6184. return good, err
  6185. }
  6186. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  6187. setting := models.XtStockSetting{}
  6188. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6189. if err == gorm.ErrRecordNotFound {
  6190. return nil, err
  6191. }
  6192. if err != nil {
  6193. return nil, err
  6194. }
  6195. return &setting, nil
  6196. }
  6197. func CreateStockSetting(setting models.XtStockSetting) error {
  6198. err := XTWriteDB().Create(&setting).Error
  6199. return err
  6200. }
  6201. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6202. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6203. return err
  6204. }
  6205. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6206. setting := models.XtStockSetting{}
  6207. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6208. return setting, err
  6209. }
  6210. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6211. detail := models.BloodAutomaticReduceDetail{}
  6212. err := XTReadDB().Where("good_id = ? and record_time = ? and org_id = ? and status = 1 and patient_id = ?", good_id, record_date, orgid, patient_id).Find(&detail).Error
  6213. return detail, err
  6214. }
  6215. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6216. detail := models.WarehouseOutInfoNight{}
  6217. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and org_id = ? and status = 1 and patient_id = ?", good_id, record_date, orgid, patient_id).Find(&detail).Error
  6218. return detail, err
  6219. }
  6220. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6221. setting := models.XtAdviceSetting{}
  6222. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6223. if err == gorm.ErrRecordNotFound {
  6224. return nil, err
  6225. }
  6226. if err != nil {
  6227. return nil, err
  6228. }
  6229. return &setting, nil
  6230. }
  6231. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6232. err := XTWriteDB().Create(&setting).Error
  6233. return err
  6234. }
  6235. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6236. err := XTWriteDB().Model(&setting).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_advice_open": setting.IsAdviceOpen, "mtime": time.Now().Unix()}).Error
  6237. return err
  6238. }
  6239. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6240. setting := models.XtAdviceSetting{}
  6241. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6242. return setting, err
  6243. }
  6244. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6245. config := models.XtPrescriptionConfig{}
  6246. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6247. if err == gorm.ErrRecordNotFound {
  6248. return nil, err
  6249. }
  6250. if err != nil {
  6251. return nil, err
  6252. }
  6253. return &config, nil
  6254. }
  6255. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6256. err := XTWriteDB().Create(&prescriptionConfig).Error
  6257. return err
  6258. }
  6259. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6260. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": config.IsOpen, "mtime": time.Now().Unix()}).Error
  6261. return err
  6262. }
  6263. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6264. config := models.XtPrescriptionConfig{}
  6265. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6266. return config, err
  6267. }
  6268. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6269. var deliver_number int64 = 0
  6270. var stock_number int64 = 0
  6271. deliver_number = maxNumber
  6272. // 根据先进先出原则,查询最先入库的批次,进行出库
  6273. // 如果没有对应的库存,则报错
  6274. //开启事物
  6275. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6276. if err != nil {
  6277. return err
  6278. }
  6279. // 将该批次的剩余库存数量转换为拆零数量
  6280. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6281. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6282. if stock_number >= deliver_number {
  6283. var maxNumber int64 = 0
  6284. var minNumber int64 = 0
  6285. var stock_max_number int64 = 0
  6286. stock_max_number = warehouse.StockMaxNumber
  6287. maxNumber = deliver_number / drup.MinNumber
  6288. minNumber = deliver_number % drup.MinNumber
  6289. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6290. minNumber = maxNumber
  6291. }
  6292. if drup.MaxUnit != drup.MinUnit {
  6293. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6294. return errors.New("库存数量不足")
  6295. }
  6296. }
  6297. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6298. if warehouse.StockMaxNumber < 0 {
  6299. warehouse.StockMaxNumber = 0
  6300. }
  6301. if warehouse.StockMinNumber < 0 {
  6302. warehouse.StockMinNumber = 0
  6303. }
  6304. warehouse.Mtime = time.Now().Unix()
  6305. if warehouse.StockMinNumber < minNumber {
  6306. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6307. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6308. } else {
  6309. if minNumber > 0 {
  6310. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6311. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6312. } else {
  6313. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6314. }
  6315. }
  6316. if minNumber == 0 && maxNumber != 1 {
  6317. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6318. }
  6319. }
  6320. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6321. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6322. if warehouse.StockMinNumber == 0 {
  6323. warehouse.StockMaxNumber = stock_max_number
  6324. }
  6325. }
  6326. if drup.MaxUnit != drup.MinUnit {
  6327. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6328. return errors.New("库存数量不足")
  6329. }
  6330. }
  6331. if warehouse.StockMinNumber <= 0 {
  6332. warehouse.StockMinNumber = 0
  6333. }
  6334. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6335. fmt.Println(errThree)
  6336. //查询默认仓库
  6337. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6338. //查询默认仓库剩余多少库存
  6339. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6340. var sum_count int64
  6341. var sum_in_count int64
  6342. for _, it := range list {
  6343. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6344. if it.MaxUnit == baseDrug.MaxUnit {
  6345. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6346. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6347. sum_count += it.StockMaxNumber + it.StockMinNumber
  6348. sum_in_count += it.WarehousingCount
  6349. }
  6350. }
  6351. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6352. return nil
  6353. } else {
  6354. warehouse.StockMaxNumber = 0
  6355. warehouse.StockMinNumber = 0
  6356. warehouse.Mtime = time.Now().Unix()
  6357. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6358. fmt.Println(errThree)
  6359. //查询默认仓库
  6360. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6361. //查询默认仓库剩余多少库存
  6362. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6363. var sum_count int64
  6364. var sum_in_count int64
  6365. for _, it := range list {
  6366. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6367. if it.MaxUnit == baseDrug.MaxUnit {
  6368. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6369. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6370. sum_count += it.StockMaxNumber + it.StockMinNumber
  6371. sum_in_count += it.WarehousingCount
  6372. }
  6373. }
  6374. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6375. if errThree != nil {
  6376. return errThree
  6377. }
  6378. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6379. prescribingNumber_two_temp := deliver_number - stock_number
  6380. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6381. }
  6382. return
  6383. }
  6384. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6385. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6386. return err
  6387. }
  6388. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6389. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6390. return err
  6391. }
  6392. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6393. config := models.DrugOutConfig{}
  6394. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6395. if err == gorm.ErrRecordNotFound {
  6396. return nil, err
  6397. }
  6398. if err != nil {
  6399. return nil, err
  6400. }
  6401. return &config, nil
  6402. }
  6403. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6404. config := models.DrugOutConfig{}
  6405. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6406. return config, err
  6407. }
  6408. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6409. config := models.XtHisConfig{}
  6410. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6411. return config, err
  6412. }
  6413. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6414. err := XTWriteDB().Create(&config).Error
  6415. return err
  6416. }
  6417. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6418. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6419. return err
  6420. }
  6421. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6422. config := models.DrugOutConfig{}
  6423. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6424. return config, err
  6425. }
  6426. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6427. config := models.HisSettleStockConfig{}
  6428. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6429. return config, err
  6430. }
  6431. func UpdatedWarehouseOut(id int64) error {
  6432. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6433. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6434. return err
  6435. }
  6436. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6437. tx := XTWriteDB().Begin()
  6438. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6439. if err != nil {
  6440. tx.Rollback()
  6441. return err
  6442. }
  6443. tx.Commit()
  6444. return err
  6445. }
  6446. func GetLastGoodWarehouseOutInfo(orgid int64, id int64) (info []*models.WarehouseOutInfo, err error) {
  6447. err = XTReadDB().Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6448. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6449. }).Find(&info).Error
  6450. return info, err
  6451. }
  6452. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6453. info := models.WarehouseOutInfo{}
  6454. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6455. return info, err
  6456. }
  6457. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6458. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "count": info.Count, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "storehouse_id": info.StorehouseId, "admin_user_id": info.AdminUserId, "buy_price": info.BuyPrice, "stock_count": info.StockCount, "register_number": info.RegisterNumber}).Error
  6459. return err
  6460. }
  6461. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6462. err := XTWriteDB().Model(&models.VmStockFlow{}).Where("warehouse_out_detail_id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "number": info.Number, "license_number": info.LicenseNumber, "count": info.Count, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "price": info.Price, "expire_date": info.ExpireDate, "product_date": info.ProductDate, "storehouse_id": info.StorehouseId, "admin_user_id": info.AdminUserId, "buy_price": info.BuyPrice, "stock_count": info.StockCount}).Error
  6463. return err
  6464. }
  6465. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6466. out := models.WarehouseOut{}
  6467. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6468. return out, err
  6469. }
  6470. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6471. ut := writeDb.Begin()
  6472. err := ut.Model(&models.WarehousingInfo{}).Where("good_id = ? and org_id = ? and status = 1 and id = ?", good_id, orgid, warehouse_info_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  6473. if err != nil {
  6474. ut.Rollback()
  6475. return err
  6476. }
  6477. ut.Commit()
  6478. return err
  6479. }
  6480. func ModifyGoodWarehouseOut(id int64) error {
  6481. ut := writeDb.Begin()
  6482. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6483. if err != nil {
  6484. ut.Rollback()
  6485. return err
  6486. }
  6487. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6488. if err != nil {
  6489. ut.Rollback()
  6490. return err
  6491. }
  6492. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6493. if err != nil {
  6494. ut.Rollback()
  6495. return err
  6496. }
  6497. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  6498. if err != nil {
  6499. ut.Rollback()
  6500. return err
  6501. }
  6502. ut.Commit()
  6503. return err
  6504. }
  6505. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6506. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6507. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6508. }).Find(&info).Error
  6509. return info, err
  6510. }
  6511. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  6512. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  6513. return info, err
  6514. }
  6515. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  6516. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "warehousing_unit": info.WarehousingUnit, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "license_number": info.LicenseNumber, "packing_price": info.PackingPrice, "register_number": info.RegisterNumber, "storehouse_id": info.StorehouseId, "good_code": info.GoodCode}).Error
  6517. return err
  6518. }
  6519. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  6520. tx := XTWriteDB().Begin()
  6521. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6522. if err != nil {
  6523. tx.Rollback()
  6524. return err
  6525. }
  6526. tx.Commit()
  6527. return err
  6528. }
  6529. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  6530. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6531. return info, err
  6532. }
  6533. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  6534. tx := XTWriteDB().Begin()
  6535. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  6536. if err != nil {
  6537. tx.Rollback()
  6538. return err
  6539. }
  6540. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6541. if err != nil {
  6542. tx.Rollback()
  6543. return err
  6544. }
  6545. tx.Commit()
  6546. return err
  6547. }
  6548. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  6549. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6550. return info, err
  6551. }
  6552. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  6553. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6554. return info, err
  6555. }
  6556. func FindLastDrugWarehousingInfoTwenty(org_id int64) (info models.DrugWarehouseInfo, err error) {
  6557. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6558. return info, err
  6559. }
  6560. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6561. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  6562. return info, err
  6563. }
  6564. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  6565. err := XTWriteDB().Create(&info).Error
  6566. return err
  6567. }
  6568. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  6569. err := XTWriteDB().Save(&info).Error
  6570. return err
  6571. }
  6572. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  6573. tx := XTWriteDB().Begin()
  6574. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6575. if err != nil {
  6576. tx.Rollback()
  6577. return err
  6578. }
  6579. tx.Commit()
  6580. return err
  6581. }
  6582. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6583. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6584. return info, err
  6585. }
  6586. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  6587. tx := XTWriteDB().Begin()
  6588. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6589. if err != nil {
  6590. tx.Rollback()
  6591. return err
  6592. }
  6593. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6594. if err != nil {
  6595. tx.Rollback()
  6596. return err
  6597. }
  6598. tx.Commit()
  6599. return err
  6600. }
  6601. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  6602. tx := XTWriteDB().Begin()
  6603. err := tx.Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  6604. if err != nil {
  6605. tx.Rollback()
  6606. return err
  6607. }
  6608. tx.Commit()
  6609. return err
  6610. }
  6611. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  6612. tx := XTWriteDB().Begin()
  6613. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6614. if err != nil {
  6615. tx.Rollback()
  6616. return err
  6617. }
  6618. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6619. tx.Commit()
  6620. return err
  6621. }
  6622. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  6623. tx := XTWriteDB().Begin()
  6624. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6625. if err != nil {
  6626. tx.Rollback()
  6627. return err
  6628. }
  6629. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6630. if err != nil {
  6631. tx.Rollback()
  6632. return err
  6633. }
  6634. tx.Commit()
  6635. return err
  6636. }
  6637. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  6638. tx := XTWriteDB().Begin()
  6639. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6640. if err != nil {
  6641. tx.Rollback()
  6642. return err
  6643. }
  6644. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6645. if err != nil {
  6646. tx.Rollback()
  6647. return err
  6648. }
  6649. tx.Commit()
  6650. return err
  6651. }
  6652. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  6653. tx := XTWriteDB().Begin()
  6654. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  6655. if err != nil {
  6656. tx.Rollback()
  6657. return err
  6658. }
  6659. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6660. if err != nil {
  6661. tx.Rollback()
  6662. return err
  6663. }
  6664. tx.Commit()
  6665. return err
  6666. }
  6667. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  6668. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "retail_price": info.RetailPrice, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "drug_code": info.DrugCode, "batch_number": info.BatchNumber}).Error
  6669. return err
  6670. }
  6671. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  6672. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  6673. return err
  6674. }
  6675. func UpdatedSelfDrugWarehouseInfo(info *models.XtSelfDrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  6676. err := tx.Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  6677. if err != gorm.ErrRecordNotFound {
  6678. return err
  6679. }
  6680. return err
  6681. }
  6682. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  6683. out := models.DrugWarehouseOut{}
  6684. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  6685. return out, err
  6686. }
  6687. func GetSelfDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  6688. out := models.XtSelfDrugWarehouseOut{}
  6689. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  6690. if err != gorm.ErrRecordNotFound {
  6691. if err != nil {
  6692. tx.Rollback()
  6693. return out, err
  6694. }
  6695. }
  6696. return out, err
  6697. }
  6698. func GetSelfDrugLastWarehouseOutOne(id int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  6699. out := models.XtSelfDrugWarehouseOut{}
  6700. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  6701. if err != gorm.ErrRecordNotFound {
  6702. if err != nil {
  6703. tx.Rollback()
  6704. return out, err
  6705. }
  6706. }
  6707. return out, err
  6708. }
  6709. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6710. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  6711. return info, err
  6712. }
  6713. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  6714. tx := XTWriteDB().Begin()
  6715. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  6716. if err != nil {
  6717. tx.Rollback()
  6718. return err
  6719. }
  6720. err = XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6721. if err != nil {
  6722. tx.Rollback()
  6723. return err
  6724. }
  6725. tx.Commit()
  6726. return err
  6727. }
  6728. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  6729. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  6730. return err
  6731. }
  6732. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  6733. out := models.DrugWarehouseOut{}
  6734. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  6735. return out, err
  6736. }
  6737. func UpdateDrugMaxNumber(count int64, id int64) error {
  6738. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6739. return err
  6740. }
  6741. func UpdateDrugMinNumber(count int64, id int64) error {
  6742. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6743. return err
  6744. }
  6745. func DeleteDrugOutFlow(id int64, orgid int64) error {
  6746. err := XTWriteDB().Model(models.DrugFlow{}).Where("warehouse_out_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  6747. return err
  6748. }
  6749. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  6750. err = XTReadDB().Where("warehouse_info_id = ? and good_id = ? and org_id = ? and is_check = 1 and status = 1", warehouse_info_id, good_id, orgid).Find(&outInfo).Error
  6751. return outInfo, err
  6752. }
  6753. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  6754. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and is_check = 1 and status = 1", warehouse_info_id, drug_id, orgid).Find(&outInfo).Error
  6755. return outInfo, err
  6756. }
  6757. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6758. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  6759. return info, err
  6760. }
  6761. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6762. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  6763. return info, err
  6764. }
  6765. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  6766. err = XTReadDB().Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", goodid, storehouse_id, orgId).Find(&info).Error
  6767. return info, err
  6768. }
  6769. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  6770. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6771. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_count)).Error
  6772. return err
  6773. }
  6774. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  6775. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6776. return err
  6777. }
  6778. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  6779. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6780. return err
  6781. }
  6782. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  6783. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6784. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6785. return err
  6786. }
  6787. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  6788. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id = ?", orgid).Find(&stock).Error
  6789. return stock, err
  6790. }
  6791. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  6792. err := XTWriteDB().Model(&models.CancelStockInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "count": info.Count, "price": info.Price, "total": info.Total, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "register_account": info.RegisterAccount, "remark": info.Remark, "storehouse_id": info.StorehouseId}).Error
  6793. return err
  6794. }
  6795. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6796. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6797. return info, err
  6798. }
  6799. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  6800. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6801. return info, err
  6802. }
  6803. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  6804. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6805. return info, err
  6806. }
  6807. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  6808. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6809. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6810. return err
  6811. }
  6812. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6813. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  6814. return info, err
  6815. }
  6816. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  6817. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and status = 1", warehouse_info_id, orgid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", stock_count)).Error
  6818. return err
  6819. }
  6820. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  6821. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6822. return err
  6823. }
  6824. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  6825. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6826. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6827. return err
  6828. }
  6829. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  6830. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("cancel_out_detail_id = ? and user_org_id = ? and status = 1 and good_id =?", id, orgid, goodid).Update(map[string]interface{}{"status": 0}).Error
  6831. return err
  6832. }
  6833. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  6834. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and good_id = ? and org_id = ? and status = 1", warehouse_info_id, good_id, org_id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", stock_count)).Error
  6835. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and status = 1 and org_id = ?", good_id, org_id).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", stock_count)).Error
  6836. return err
  6837. }
  6838. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6839. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6840. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_in_count)).Error
  6841. return err
  6842. }
  6843. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6844. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  6845. return info, err
  6846. }
  6847. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  6848. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6849. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_in_count)).Error
  6850. return err
  6851. }
  6852. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  6853. err = XTReadDB().Where("storehouse_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", storehouse_id, orgid, drugid).Find(&info).Error
  6854. return info, err
  6855. }
  6856. func GetSelfDrugAllStockInfoOne(patient_id int64, orgid int64, drugid int64) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  6857. err = XTReadDB().Where("patient_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", patient_id, orgid, drugid).Find(&info).Error
  6858. return info, err
  6859. }
  6860. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  6861. err = tx.Where("storehouse_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", storehouse_id, orgid, drugid).Find(&info).Error
  6862. if err != gorm.ErrRecordNotFound {
  6863. if err != nil {
  6864. tx.Rollback()
  6865. return info, err
  6866. }
  6867. }
  6868. return info, err
  6869. }
  6870. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  6871. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6872. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6873. return err
  6874. }
  6875. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  6876. err = writeDb.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6877. return err
  6878. }
  6879. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  6880. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6881. if err != nil {
  6882. tx.Rollback()
  6883. return err
  6884. }
  6885. return err
  6886. }
  6887. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6888. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", id, orgid).Preload("DrugWarehouseInfo", "status = 1 and org_id = ?", orgid).Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Find(&info).Error
  6889. return info, err
  6890. }
  6891. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6892. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6893. return info, err
  6894. }
  6895. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  6896. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6897. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6898. return err
  6899. }
  6900. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  6901. stock := models.DrugCancelStock{}
  6902. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  6903. return stock, err
  6904. }
  6905. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6906. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  6907. return stock, err
  6908. }
  6909. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6910. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  6911. return stock, err
  6912. }
  6913. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6914. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  6915. return info, err
  6916. }
  6917. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  6918. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6919. return err
  6920. }
  6921. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6922. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6923. if err != nil {
  6924. tx.Rollback()
  6925. return err
  6926. }
  6927. return err
  6928. }
  6929. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  6930. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6931. return err
  6932. }
  6933. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6934. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6935. if err != nil {
  6936. tx.Rollback()
  6937. return err
  6938. }
  6939. return err
  6940. }
  6941. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  6942. err := XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "price": info.Price, "total": info.Total, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "register_account": info.RegisterAccount, "remark": info.Remark, "batch_number": info.BatchNumber, "max_unit": info.MaxUnit, "batch_number_id": info.BatchNumberId, "storehouse_id": info.StorehouseId}).Error
  6943. return err
  6944. }
  6945. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  6946. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6947. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6948. return err
  6949. }
  6950. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6951. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  6952. return info, err
  6953. }
  6954. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  6955. err := XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and drug_id = ? and user_org_id = ? and status = 1", cancelstock_id, drugId, orgid).Update(map[string]interface{}{"status": 0}).Error
  6956. return err
  6957. }
  6958. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6959. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6960. return err
  6961. }
  6962. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6963. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  6964. return err
  6965. }
  6966. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  6967. info := models.DrugWarehouseInfo{}
  6968. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  6969. return info, err
  6970. }
  6971. func GetSelfDrugInfoSix(id int64, orgid int64) (models.XtSelfDrugWarehouseInfo, error) {
  6972. info := models.XtSelfDrugWarehouseInfo{}
  6973. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  6974. return info, err
  6975. }
  6976. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  6977. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  6978. if storehouse_id > 0 {
  6979. db = db.Where("storehouse_id = ?", storehouse_id)
  6980. }
  6981. if orgid > 0 {
  6982. db = db.Where("user_org_id = ?", orgid)
  6983. }
  6984. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  6985. return storeinventory, err
  6986. }
  6987. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  6988. detail := models.AutomaticReduceDetail{}
  6989. err := XTReadDB().Where("org_id =? and patient_id = ? and record_time = ? and good_id = ? and status= 1", orgid, patient_id, sys_record_time, good_id).Find(&detail).Error
  6990. return detail, err
  6991. }
  6992. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  6993. detail := models.WarehouseOutInfo{}
  6994. err := XTReadDB().Where("org_id =? and patient_id = ? and sys_record_time = ? and good_id = ? and status= 1", orgid, patient_id, sys_record_time, good_id).Find(&detail).Error
  6995. return detail, err
  6996. }
  6997. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  6998. stock := models.XtGoodStockCount{}
  6999. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  7000. return stock, err
  7001. }
  7002. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  7003. err := XTWriteDB().Create(&stock).Error
  7004. return err
  7005. }
  7006. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7007. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error
  7008. return err
  7009. }
  7010. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7011. ut := XTWriteDB().Begin()
  7012. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", count)).Error
  7013. if err != nil {
  7014. ut.Rollback()
  7015. return err
  7016. }
  7017. ut.Commit()
  7018. return err
  7019. }
  7020. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  7021. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7022. return err
  7023. }
  7024. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  7025. err := tx.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7026. if err != nil {
  7027. tx.Rollback()
  7028. return err
  7029. }
  7030. return err
  7031. }
  7032. func CreateDrugStock(stock models.XtDrugStockCount) error {
  7033. err := XTWriteDB().Create(&stock).Error
  7034. return err
  7035. }
  7036. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  7037. ut := XTWriteDB().Begin()
  7038. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7039. if err != nil {
  7040. ut.Rollback()
  7041. return err
  7042. }
  7043. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7044. if err != nil {
  7045. ut.Rollback()
  7046. return err
  7047. }
  7048. ut.Commit()
  7049. return err
  7050. }
  7051. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  7052. ut := XTWriteDB().Begin()
  7053. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7054. if err != nil {
  7055. ut.Rollback()
  7056. return err
  7057. }
  7058. ut.Commit()
  7059. return err
  7060. }
  7061. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  7062. ut := XTWriteDB().Begin()
  7063. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", sum_out_count)).Error
  7064. if err != nil {
  7065. ut.Rollback()
  7066. return err
  7067. }
  7068. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", sum_out_count)).Error
  7069. if err != nil {
  7070. ut.Rollback()
  7071. return err
  7072. }
  7073. ut.Commit()
  7074. return err
  7075. }
  7076. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  7077. ut := XTWriteDB().Begin()
  7078. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", sum_out_count)).Error
  7079. if err != nil {
  7080. ut.Rollback()
  7081. return err
  7082. }
  7083. ut.Commit()
  7084. return err
  7085. }
  7086. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7087. ut := XTWriteDB().Begin()
  7088. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count + ?", count)).Error
  7089. if err != nil {
  7090. ut.Rollback()
  7091. return err
  7092. }
  7093. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", count)).Error
  7094. if err != nil {
  7095. ut.Rollback()
  7096. return err
  7097. }
  7098. ut.Commit()
  7099. return err
  7100. }
  7101. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7102. ut := XTWriteDB().Begin()
  7103. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", count)).Error
  7104. if err != nil {
  7105. ut.Rollback()
  7106. return err
  7107. }
  7108. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count - ?", count)).Error
  7109. if err != nil {
  7110. ut.Rollback()
  7111. return err
  7112. }
  7113. ut.Commit()
  7114. return err
  7115. }
  7116. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  7117. if orgid == 10191 || orgid == 10138 || orgid == 10278 {
  7118. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id=? and is_check= 1 and org_id = ?", storehouse_id, orgid).Preload("GoodInfo", "status = 1").Order("good_id").Find(&info).Error
  7119. return info, err
  7120. } else {
  7121. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id=? and is_check= 1 and org_id = ? and stock_count >0", storehouse_id, orgid).Preload("GoodInfo", "status = 1").Order("good_id").Find(&info).Error
  7122. return info, err
  7123. }
  7124. }
  7125. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  7126. config := models.HisSettleStockConfig{}
  7127. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7128. if err == gorm.ErrRecordNotFound {
  7129. return nil, err
  7130. }
  7131. if err != nil {
  7132. return nil, err
  7133. }
  7134. return &config, nil
  7135. }
  7136. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  7137. err := XTWriteDB().Create(&config).Error
  7138. return err
  7139. }
  7140. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  7141. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  7142. return err
  7143. }
  7144. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  7145. config := models.DrugStockConfig{}
  7146. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7147. return config, err
  7148. }
  7149. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  7150. stockConfig := models.PharmacyConfig{}
  7151. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7152. return stockConfig, err
  7153. }
  7154. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  7155. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7156. return err
  7157. }
  7158. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  7159. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7160. return err
  7161. }
  7162. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  7163. config := models.HisSettleStockConfig{}
  7164. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7165. return config, nil
  7166. }
  7167. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  7168. info := models.GoodInformationFourty{}
  7169. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  7170. return info, err
  7171. }
  7172. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  7173. advice := models.HisDoctorAdvice{}
  7174. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7175. return advice, err
  7176. }
  7177. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  7178. advice := models.DoctorAdvice{}
  7179. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7180. return advice, err
  7181. }
  7182. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  7183. out := models.WarehouseOut{}
  7184. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys = ?", org_id, warehouse_out_time, is_sys).Find(&out).Error
  7185. return out, err
  7186. }
  7187. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  7188. config := models.XtGoodOutConfig{}
  7189. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7190. if err == gorm.ErrRecordNotFound {
  7191. return nil, err
  7192. }
  7193. if err != nil {
  7194. return nil, err
  7195. }
  7196. return &config, nil
  7197. }
  7198. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  7199. err := XTWriteDB().Create(&good).Error
  7200. return err
  7201. }
  7202. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  7203. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  7204. return err
  7205. }
  7206. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  7207. config := models.XtGoodOutConfig{}
  7208. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7209. return config, err
  7210. }
  7211. func UpdateGoodAutoConfig(user_org_id int64) error {
  7212. gobalConfig := models.GobalConfig{}
  7213. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7214. return err
  7215. }
  7216. func UpdateGoodOutConfig(user_org_id int64) error {
  7217. config := models.XtGoodOutConfig{}
  7218. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7219. return err
  7220. }
  7221. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  7222. err = XTReadDB().Where("patient_id = ? and record_date = ? and status = 1 and user_org_id = ? AND type = 3", patient_id, record_date, orgid).Find(&info).Error
  7223. return info, err
  7224. }
  7225. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  7226. err = XTReadDB().Where("good_id = ? and patient_id = ? and sys_record_time = ? and project_id = ? and status = 1", good_id, patient_id, sys_record_time, project_id).Find(&good).Error
  7227. return good, err
  7228. }
  7229. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  7230. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and good_id = ? and status = 1", warehouse_info_id, orgid, good_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  7231. return err
  7232. }
  7233. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7234. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("good_id = ? and sys_record_time = ? and org_id = ? and project_id = ? and status=1", good_id, sys_record_time, org_id, project_id).Updates(map[string]interface{}{"status": 0}).Error
  7235. return err
  7236. }
  7237. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7238. err := XTWriteDB().Model(&models.VmStockFlow{}).Where("good_id = ? and system_time = ? and user_org_id = ? and project_id = ? and status=1", good_id, sys_record_time, org_id, project_id).Updates(map[string]interface{}{"status": 0, "is_read": 1}).Error
  7239. return err
  7240. }
  7241. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7242. out := models.WarehouseOut{}
  7243. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Find(&out).Error
  7244. return out, err
  7245. }
  7246. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7247. out := models.WarehouseOut{}
  7248. err = tx.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Find(&out).Error
  7249. if err != gorm.ErrRecordNotFound {
  7250. if err != nil {
  7251. tx.Rollback()
  7252. return out, err
  7253. }
  7254. }
  7255. return out, err
  7256. }
  7257. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7258. out := models.WarehouseOut{}
  7259. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Last(&out).Error
  7260. return out, err
  7261. }
  7262. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7263. out := models.WarehouseOut{}
  7264. err = tx.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Last(&out).Error
  7265. if err != gorm.ErrRecordNotFound {
  7266. if err != nil {
  7267. tx.Rollback()
  7268. return out, err
  7269. }
  7270. }
  7271. return out, err
  7272. }
  7273. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  7274. goodsType := models.GoodsType{}
  7275. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  7276. return goodsType, err
  7277. }
  7278. func GetAllGoodTypeName(good_type_id int64, org_id int64) (good_type_name string) {
  7279. goodsTypeList, _ := GetAllGoodType(org_id)
  7280. for _, item := range goodsTypeList {
  7281. if good_type_id == item.ID {
  7282. good_type_name = item.TypeName
  7283. }
  7284. }
  7285. return good_type_name
  7286. }
  7287. func UpdateGoodTypeName(good_id int64, good_type_name string) (models.GoodInformation, error) {
  7288. information := models.GoodInformation{}
  7289. err := XTWriteDB().Model(&information).Where("id = ? and status =1", good_id).Updates(map[string]interface{}{"good_type_name": good_type_name}).Error
  7290. return information, err
  7291. }
  7292. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  7293. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  7294. return info, err
  7295. }
  7296. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  7297. ut := writeDb.Begin()
  7298. err := ut.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Updates(map[string]interface{}{"sum_count": sum_count}).Error
  7299. if err != nil {
  7300. ut.Rollback()
  7301. return err
  7302. }
  7303. ut.Commit()
  7304. return err
  7305. }
  7306. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  7307. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Updates(map[string]interface{}{"sum_count": sum_count}).Error
  7308. if err != nil {
  7309. tx.Rollback()
  7310. return err
  7311. }
  7312. return err
  7313. }
  7314. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  7315. info := models.WarehouseOutInfo{}
  7316. var err error
  7317. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  7318. if err == gorm.ErrRecordNotFound {
  7319. return nil, err
  7320. }
  7321. if err != nil {
  7322. return nil, err
  7323. }
  7324. return &info, nil
  7325. }
  7326. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  7327. info := models.WarehouseOutInfo{}
  7328. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  7329. return info, err
  7330. }
  7331. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7332. info := models.WarehouseOutInfo{}
  7333. err := tx.Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  7334. if err != gorm.ErrRecordNotFound {
  7335. if err != nil {
  7336. tx.Rollback()
  7337. return info, err
  7338. }
  7339. }
  7340. return info, err
  7341. }
  7342. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  7343. info := models.WarehouseOutInfo{}
  7344. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7345. return info, err
  7346. }
  7347. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7348. info := models.WarehouseOutInfo{}
  7349. err := tx.Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7350. if err != gorm.ErrRecordNotFound {
  7351. if err != nil {
  7352. tx.Rollback()
  7353. return info, err
  7354. }
  7355. }
  7356. return info, err
  7357. }
  7358. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  7359. info := models.WarehouseOutInfo{}
  7360. var err error
  7361. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7362. if err == gorm.ErrRecordNotFound {
  7363. return nil, err
  7364. }
  7365. if err != nil {
  7366. return nil, err
  7367. }
  7368. return &info, nil
  7369. }
  7370. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  7371. info := models.WarehouseOutInfo{}
  7372. var err error
  7373. err = tx.Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7374. if err != gorm.ErrRecordNotFound {
  7375. if err != nil {
  7376. tx.Rollback()
  7377. return &info, err
  7378. }
  7379. }
  7380. return &info, err
  7381. }
  7382. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  7383. db := XTReadDB().Model(&info).Where("status = 1")
  7384. if startime > 0 {
  7385. db = db.Where("ctime>=?", startime)
  7386. }
  7387. if endtime > 0 {
  7388. db = db.Where("ctime<=?", endtime)
  7389. }
  7390. if orgid > 0 {
  7391. db = db.Where("org_id = ?", orgid)
  7392. }
  7393. if len(good_id) > 0 {
  7394. db = db.Where("good_id in(?)", good_id)
  7395. }
  7396. if storehouse_id > 0 {
  7397. db = db.Where("storehouse_id = ?", storehouse_id)
  7398. }
  7399. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  7400. return info, err
  7401. }
  7402. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  7403. db := XTReadDB().Model(&info).Where("status= 1")
  7404. if start_time > 0 {
  7405. db = db.Where("ctime>=?", start_time)
  7406. }
  7407. if end_time > 0 {
  7408. db = db.Where("ctime <=?", end_time)
  7409. }
  7410. if orgid > 0 {
  7411. db = db.Where("org_id = ?", orgid)
  7412. }
  7413. if len(good_id) > 0 {
  7414. db = db.Where("good_id in(?)", good_id)
  7415. }
  7416. if storehouse_id > 0 {
  7417. db = db.Where("storehouse_id = ?", storehouse_id)
  7418. }
  7419. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  7420. return info, err
  7421. }
  7422. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  7423. config := models.XtGoodOutConfig{}
  7424. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7425. return config, err
  7426. }
  7427. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  7428. err := XTWriteDB().Model(&models.AutomaticReduceDetail{}).Where("good_id = ? and record_time = ? and patient_id = ?", good_id, record_date, patient_id).Updates(map[string]interface{}{"status": 1}).Error
  7429. err = XTWriteDB().Model(&models.DialysisBeforePrepare{}).Where("good_id = ? and record_date = ? and patient_id = ?", good_id, record_date, patient_id).Updates(map[string]interface{}{"status": 1}).Error
  7430. return err
  7431. }
  7432. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  7433. tx := XTWriteDB().Begin()
  7434. for _, item := range list {
  7435. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  7436. if err != nil {
  7437. tx.Rollback()
  7438. return err
  7439. }
  7440. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  7441. if err != nil {
  7442. tx.Rollback()
  7443. return err
  7444. }
  7445. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7446. if err != nil {
  7447. tx.Rollback()
  7448. return err
  7449. }
  7450. var goodList []*models.WarehousingInfo
  7451. err = tx.Where("org_id = ? and storehouse_id = ? and good_id = ? and status =1 and is_check = 1", orgId, item.StorehouseId, item.GoodId).Find(&goodList).Error
  7452. var flush_count int64
  7453. for _, it := range goodList {
  7454. flush_count += it.StockCount
  7455. }
  7456. err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", item.OrgId, item.GoodId, item.StorehouseId).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", item.WarehousingCount)).Error
  7457. if err != nil {
  7458. tx.Rollback()
  7459. return err
  7460. }
  7461. err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", item.OrgId, item.GoodId, item.StorehouseId).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7462. if err != nil {
  7463. tx.Rollback()
  7464. return err
  7465. }
  7466. storeHouseConfig := models.XtStorehouseConfig{}
  7467. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  7468. var stockList []*models.WarehousingInfo
  7469. //查询该耗材剩余多少库存
  7470. err = tx.Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", item.GoodId, storeHouseConfig.StorehouseOutInfo, orgId).Find(&stockList).Error
  7471. var sum_count int64
  7472. var sum_in_count int64
  7473. for _, it := range stockList {
  7474. sum_count += it.StockCount
  7475. sum_in_count += it.WarehousingCount
  7476. }
  7477. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", item.GoodId, orgId).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7478. if err != nil {
  7479. tx.Rollback()
  7480. return err
  7481. }
  7482. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", item.GoodId, orgId).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7483. if err != nil {
  7484. tx.Rollback()
  7485. return err
  7486. }
  7487. }
  7488. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7489. if err != nil {
  7490. tx.Rollback()
  7491. return err
  7492. }
  7493. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7494. tx.Commit()
  7495. return err
  7496. }
  7497. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  7498. if storehouse_id == 0 {
  7499. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  7500. }
  7501. if storehouse_id > 0 {
  7502. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").First(&info).Error
  7503. }
  7504. return info, err
  7505. }
  7506. func FindFirstWarehousingInfoByStockFour(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  7507. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").Last(&info).Error
  7508. return info, err
  7509. }
  7510. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  7511. err = readDb.Where("project_id = ? and record_date =? and user_org_id =? and status =1 and patient_id = ?", project_id, record_date, orgid, patient_id).Find(&list).Error
  7512. return list, err
  7513. }
  7514. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  7515. err = readDb.Where("good_id =? and sys_record_time = ? and org_id = ? and patient_id = ? and status=1", project_id, record_date, orgid, patient_id).Find(&infor).Error
  7516. return infor, err
  7517. }
  7518. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  7519. info := models.WarehouseOutInfo{}
  7520. err := XTWriteDB().Model(&info).Where("good_id = ? and sys_record_time = ? and org_id = ? and status =1 and patient_id = ?", project_id, record_date, orgid, patient_id).Updates(map[string]interface{}{"status": 0}).Error
  7521. return info, err
  7522. }
  7523. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  7524. info := models.WarehousingInfo{}
  7525. err := XTWriteDB().Where("id = ? and good_id = ? and org_id = ? and status= 1", id, good_id, user_org_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  7526. return info, err
  7527. }
  7528. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  7529. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  7530. return list, err
  7531. }
  7532. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  7533. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  7534. return list, err
  7535. }
  7536. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  7537. templateL := models.HisPrescriptionProjectTemplateL{}
  7538. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  7539. return templateL, err
  7540. }
  7541. func ModifyNewGoodWarehouseInfoStockMinNumber(count int64, goodid int64, orgid int64, id int64, tx *gorm.DB) error {
  7542. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and good_id = ? and status = 1", id, orgid, goodid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  7543. if err != nil {
  7544. tx.Rollback()
  7545. return err
  7546. }
  7547. return err
  7548. }
  7549. func GetDrugInvetoryByDrugId(user_org_id int64, drug_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  7550. err = XTReadDB().Where("org_id = ? and drug_id = ? and status=1", user_org_id, drug_id).Preload("XtBaseDrug", "org_id = ? and status=1", user_org_id).Order("id desc").Find(&info).Error
  7551. return info, err
  7552. }
  7553. func UpdateNewDrugWarehouseInfoById(id int64, last_stock_max_number int64, last_stock_min_number int64) error {
  7554. err := XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"stock_max_number": last_stock_max_number, "stock_min_number": last_stock_min_number}).Error
  7555. return err
  7556. }
  7557. func GeteSelfPrescriptionOpen(user_org_id int64) (models.XtSelfDrugInfoConfig, error) {
  7558. drugInfoConfig := models.XtSelfDrugInfoConfig{}
  7559. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugInfoConfig).Error
  7560. return drugInfoConfig, err
  7561. }
  7562. func CreaterSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  7563. err := XTWriteDB().Create(&infoConfig).Error
  7564. return err
  7565. }
  7566. func SaveSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  7567. err := XTWriteDB().Save(&infoConfig).Error
  7568. return err
  7569. }
  7570. func GetSelfPrescriptionOutOpen(user_org_id int64) (models.XtSelfDrugOutConfig, error) {
  7571. drugOutConfig := models.XtSelfDrugOutConfig{}
  7572. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugOutConfig).Error
  7573. return drugOutConfig, err
  7574. }
  7575. func CreateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  7576. err := XTWriteDB().Create(&config).Error
  7577. return err
  7578. }
  7579. func UpdateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  7580. err := XTWriteDB().Save(&config).Error
  7581. return err
  7582. }
  7583. func CreateSelfDrugFlowOne(flow models.XtSelfDrugFlow, tx *gorm.DB) error {
  7584. err := tx.Create(&flow).Error
  7585. if err != nil {
  7586. tx.Rollback()
  7587. return err
  7588. }
  7589. return err
  7590. }
  7591. func UpdateWarehouseOutInfoByPrice(good_id int64, packing_price float64) error {
  7592. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("good_id =? and status =1 and is_check=1", good_id).Updates(map[string]interface{}{"packing_price": packing_price}).Error
  7593. return err
  7594. }
  7595. func GetWarehosueOutInfoByPrice(good_id int64, org_id int64) (out []*models.WarehouseOutInfo, err error) {
  7596. err = XTReadDB().Where("good_id = ? and org_id = ? and status=1 and price =0", good_id, org_id).Find(&out).Error
  7597. return out, err
  7598. }
  7599. func UpdateWarehouseOut(good_id int64, packing_price float64) error {
  7600. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  7601. return err
  7602. }
  7603. func UpdateStockFlowByList(good_id int64, packing_price float64) error {
  7604. err := XTWriteDB().Model(models.VmStockFlow{}).Where("good_id = ? and user_org_id = 10633 and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  7605. return err
  7606. }
  7607. func GetGoodStockInventoryByRecordate(good_id int64, record_date int64, user_org_id int64) (models.XtGoodStockInventoryList, error) {
  7608. goodStockInventoryList := models.XtGoodStockInventoryList{}
  7609. err := XTReadDB().Where("good_id = ? and record_date = ? and user_org_id = ? and status=1", good_id, record_date, user_org_id).Find(&goodStockInventoryList).Error
  7610. return goodStockInventoryList, err
  7611. }
  7612. func CreateGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  7613. err := XTWriteDB().Create(&list).Error
  7614. return err
  7615. }
  7616. func SaveGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  7617. err := XTWriteDB().Save(&list).Error
  7618. return err
  7619. }
  7620. func GetGoodStockInventoryList(keyword string, limit int64, page int64, storehouse_id int64, startTime int64, endTime int64, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  7621. likeKey := "%" + keyword + "%"
  7622. offset := (page - 1) * limit
  7623. db := XTReadDB().Model(&models.XtGoodStockInventoryList{}).Where("status=1")
  7624. if storehouse_id > 0 {
  7625. db = db.Where("storehouse_id = ?", storehouse_id)
  7626. }
  7627. if startTime > 0 {
  7628. db = db.Where("record_date >=?", startTime)
  7629. }
  7630. if endTime > 0 {
  7631. db = db.Where("record_date<=?", endTime)
  7632. }
  7633. if len(keyword) > 0 {
  7634. db = db.Where("good_name like ? or specification_name like ?", likeKey, likeKey)
  7635. }
  7636. if orgId > 0 {
  7637. db = db.Where("user_org_id = ?", orgId)
  7638. }
  7639. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  7640. return list, total, err
  7641. }
  7642. func GetDrugStockInventoryList(drug_id int64, record_date int64, user_org_id int64) (models.XtDrugStockInventoryList, error) {
  7643. drugStockInventoryList := models.XtDrugStockInventoryList{}
  7644. err := XTReadDB().Where("drug_id = ? and record_date = ? and user_org_id = ? and status=1", drug_id, record_date, user_org_id).Find(&drugStockInventoryList).Error
  7645. return drugStockInventoryList, err
  7646. }
  7647. func CreatedDrugStockInventory(list models.XtDrugStockInventoryList) error {
  7648. err := XTWriteDB().Create(&list).Error
  7649. return err
  7650. }
  7651. func SaveDrugStockInventory(list models.XtDrugStockInventoryList) error {
  7652. err := XTWriteDB().Save(&list).Error
  7653. return err
  7654. }
  7655. func GetStockInventoryPrintList(storehouse_id int64, keyword string, limit int64, page int64, ids []string, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  7656. db := XTReadDB().Model(&list).Where("status=1")
  7657. likeKey := "%" + keyword + "%"
  7658. offset := (page - 1) * limit
  7659. if storehouse_id > 0 {
  7660. db = db.Where("storehouse_id = ?", storehouse_id)
  7661. }
  7662. if len(keyword) > 0 {
  7663. db = db.Where("good_name like ?", likeKey)
  7664. }
  7665. if orgId > 0 {
  7666. db = db.Where("user_org_id = ?", orgId)
  7667. }
  7668. if len(ids) > 0 {
  7669. db = db.Where("id in(?)", ids)
  7670. }
  7671. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  7672. return list, total, err
  7673. }
  7674. func GetGoodWarehouseInfoList(startTime int64, endTime int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  7675. db := XTReadDB().Model(&info).Where("status=1 and is_check=1")
  7676. if startTime > 0 {
  7677. db = db.Where("ctime>=?", startTime)
  7678. }
  7679. if endTime > 0 {
  7680. db = db.Where("ctime<=?", endTime)
  7681. }
  7682. if orgId > 0 {
  7683. db = db.Where("org_id = ?", orgId)
  7684. }
  7685. err = db.Find(&info).Error
  7686. return info, err
  7687. }
  7688. func GetGoodWarhouseOutList(startTime int64, endTime int64, orgId int64) (outinfo []*models.WarehouseOutInfo, err error) {
  7689. db := XTReadDB().Model(&outinfo).Where("status=1 and is_check =1")
  7690. if startTime > 0 {
  7691. db = db.Where("ctime>=?", startTime)
  7692. }
  7693. if endTime > 0 {
  7694. db = db.Where("ctime<=?", endTime)
  7695. }
  7696. if orgId > 0 {
  7697. db = db.Where("org_id = ?", orgId)
  7698. }
  7699. err = db.Find(&outinfo).Error
  7700. return outinfo, err
  7701. }
  7702. func GetGoodStockFlowProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  7703. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 10")
  7704. if startTime > 0 {
  7705. db = db.Where("ctime>=?", startTime)
  7706. }
  7707. if endTime > 0 {
  7708. db = db.Where("ctime<=?", endTime)
  7709. }
  7710. if orgId > 0 {
  7711. db = db.Where("user_org_id = ?", orgId)
  7712. }
  7713. err = db.Find(&flow).Error
  7714. return flow, err
  7715. }
  7716. func GetGoodStockFlowNoProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  7717. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 11")
  7718. if startTime > 0 {
  7719. db = db.Where("ctime>=?", startTime)
  7720. }
  7721. if endTime > 0 {
  7722. db = db.Where("ctime<=?", endTime)
  7723. }
  7724. if orgId > 0 {
  7725. db = db.Where("user_org_id = ?", orgId)
  7726. }
  7727. err = db.Find(&flow).Error
  7728. return flow, err
  7729. }
  7730. func GetCancelStockFlowList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  7731. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 7")
  7732. if startTime > 0 {
  7733. db = db.Where("ctime>=?", startTime)
  7734. }
  7735. if endTime > 0 {
  7736. db = db.Where("ctime<=?", endTime)
  7737. }
  7738. if orgId > 0 {
  7739. db = db.Where("user_org_id = ?", orgId)
  7740. }
  7741. err = db.Find(&flow).Error
  7742. return flow, err
  7743. }
  7744. func GetLastMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  7745. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 1 or consumable_type = 10)")
  7746. if startTime > 0 {
  7747. db = db.Where("ctime <?", startTime)
  7748. }
  7749. if orgId > 0 {
  7750. db = db.Where("user_org_id = ?", orgId)
  7751. }
  7752. err = db.Find(&flow).Error
  7753. return flow, err
  7754. }
  7755. func GetLastMonthOverCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  7756. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 2 or consumable_type=3 or consumable_type=11)")
  7757. if startTime > 0 {
  7758. db = db.Where("ctime <?", startTime)
  7759. }
  7760. if orgId > 0 {
  7761. db = db.Where("user_org_id = ?", orgId)
  7762. }
  7763. err = db.Find(&flow).Error
  7764. return flow, err
  7765. }
  7766. func GetLastCancelMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  7767. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 7)")
  7768. if startTime > 0 {
  7769. db = db.Where("ctime <?", startTime)
  7770. }
  7771. if orgId > 0 {
  7772. db = db.Where("user_org_id = ?", orgId)
  7773. }
  7774. err = db.Find(&flow).Error
  7775. return flow, err
  7776. }
  7777. func GetDrugCodeOpen(user_org_id int64) (models.XtDrugCodeOpen, error) {
  7778. drugCodeOpen := models.XtDrugCodeOpen{}
  7779. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugCodeOpen).Error
  7780. return drugCodeOpen, err
  7781. }
  7782. func CreateDrugOpen(open models.XtDrugCodeOpen) error {
  7783. err := XTWriteDB().Create(&open).Error
  7784. return err
  7785. }
  7786. func SaveDrugOpen(open models.XtDrugCodeOpen) error {
  7787. err := XTWriteDB().Save(&open).Error
  7788. return err
  7789. }
  7790. func GetHisDrugCodeQuery(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64) (list []*models.HisDoctorAdviceInfo, total int64, err error) {
  7791. db := XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("status=1")
  7792. if orgId > 0 {
  7793. db = db.Where("user_org_id=?", orgId)
  7794. }
  7795. if start_time > 0 {
  7796. db = db.Where("advice_date >= ?", start_time)
  7797. }
  7798. if end_time > 0 {
  7799. db = db.Where("advice_date<=?", end_time)
  7800. }
  7801. offset := (page - 1) * limit
  7802. if is_sale == 1 {
  7803. db = db.Where("is_upload = 1")
  7804. }
  7805. if is_sale == 2 {
  7806. db = db.Where("is_upload = 2 or is_upload =0")
  7807. }
  7808. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  7809. fmt.Println("totla====================", total)
  7810. return list, total, err
  7811. }
  7812. func GetDrugWarehouseOutInfoByDrugCode(drug_id int64, patient_id int64, advice_date int64, user_org_id int64) (outinfo []*models.XtDrugWarehouseOutInfo, err error) {
  7813. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time =? and org_id = ? and status =1", drug_id, patient_id, advice_date, user_org_id).Find(&outinfo).Error
  7814. return outinfo, err
  7815. }