stock_service.go 389KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152
  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. }).Error
  247. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  248. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  249. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  250. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  251. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. return err, goodInfo
  254. }
  255. 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) {
  256. offset := (page - 1) * limit
  257. db := readDb.Model(&models.GoodInfo{})
  258. db = db.Where("org_id = ? AND status = 1", orgId)
  259. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  260. if len(keyword) > 0 {
  261. likeKey := "%" + keyword + "%"
  262. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  263. if is_use > 0 {
  264. if is_use == 2 {
  265. db = db.Where("find_in_set('停用',good_status) = 0")
  266. } else {
  267. db = db.Where("find_in_set('停用',good_status) > 0")
  268. }
  269. }
  270. if is_charge > 0 {
  271. if is_charge == 1 {
  272. db = db.Where("find_in_set('收费',good_status) > 0")
  273. } else {
  274. db = db.Where("find_in_set('收费',good_status) = 0")
  275. }
  276. }
  277. if good_kind > 0 {
  278. db = db.Where(" good_kind = ?", good_kind)
  279. }
  280. if is_mark > 0 {
  281. db = db.Where("is_mark = ?", is_mark)
  282. }
  283. if manufacturer > 0 {
  284. db = db.Where("manufacturer = ?", manufacturer)
  285. }
  286. if is_type > 0 {
  287. db = db.Where("good_type_id = ?", is_type)
  288. }
  289. } else {
  290. if is_use > 0 {
  291. if is_use == 2 {
  292. db = db.Where("find_in_set('停用',good_status) = 0")
  293. } else {
  294. db = db.Where("find_in_set('停用',good_status) > 0")
  295. }
  296. }
  297. if is_charge > 0 {
  298. if is_charge == 1 {
  299. db = db.Where("find_in_set('收费',good_status) > 0")
  300. } else {
  301. db = db.Where("find_in_set('收费',good_status) = 0")
  302. }
  303. }
  304. if good_kind > 0 {
  305. db = db.Where(" good_kind = ?", good_kind)
  306. }
  307. if is_mark > 0 {
  308. db = db.Where("is_mark = ?", is_mark)
  309. }
  310. if manufacturer > 0 {
  311. db = db.Where("manufacturer = ?", manufacturer)
  312. }
  313. if is_type > 0 {
  314. db = db.Where("good_type_id = ?", is_type)
  315. }
  316. }
  317. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  318. return
  319. }
  320. func DeleteGoodInfoById(id int64, operateId int64) error {
  321. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  322. return err
  323. }
  324. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  325. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  326. return list, err
  327. }
  328. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  329. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  330. return list, err
  331. }
  332. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  333. goodInfo := &models.GoodInfo{}
  334. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  335. return goodInfo, err
  336. }
  337. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  338. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  339. return goodType, err
  340. }
  341. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  342. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  343. return goodType, err
  344. }
  345. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  346. 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
  347. return goodInfo, err
  348. }
  349. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  350. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  351. return goodInfo, err
  352. }
  353. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  354. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  355. return total, err
  356. }
  357. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  358. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  359. return total, err
  360. }
  361. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  362. err := writeDb.Create(&warehouse).Error
  363. return err
  364. }
  365. func UpdateWarehouse(id int64, storehouse_id int64) error {
  366. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  367. return err
  368. }
  369. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  370. err := writeDb.Create(&warehouse).Error
  371. return err
  372. }
  373. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  374. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  375. return err
  376. }
  377. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  378. err := writeDb.Create(&salesReturn).Error
  379. return err
  380. }
  381. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  382. err := writeDb.Create(&salesReturn).Error
  383. return err
  384. }
  385. func UpDateGoodReturnStatus(ids []string) {
  386. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  387. }
  388. func UpDateCanCelStockStatus(ids []string) {
  389. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  390. }
  391. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  392. if len(warehousingInfo) > 0 {
  393. utx := writeDb.Begin()
  394. if len(warehousingInfo) > 0 {
  395. 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) VALUES "
  396. insertParams := make([]string, 0)
  397. insertData := make([]interface{}, 0)
  398. for _, info := range warehousingInfo {
  399. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  400. insertData = append(insertData, info.WarehousingId)
  401. insertData = append(insertData, info.GoodId)
  402. insertData = append(insertData, info.GoodTypeId)
  403. insertData = append(insertData, info.Number)
  404. insertData = append(insertData, info.ProductDate)
  405. insertData = append(insertData, info.ExpiryDate)
  406. insertData = append(insertData, info.WarehousingCount)
  407. insertData = append(insertData, info.Price)
  408. insertData = append(insertData, info.TotalPrice)
  409. insertData = append(insertData, info.Dealer)
  410. insertData = append(insertData, info.Manufacturer)
  411. insertData = append(insertData, info.Remark)
  412. insertData = append(insertData, info.Ctime)
  413. insertData = append(insertData, info.Mtime)
  414. insertData = append(insertData, info.Status)
  415. insertData = append(insertData, info.OrgId)
  416. insertData = append(insertData, info.WarehousingOrder)
  417. insertData = append(insertData, info.Type)
  418. insertData = append(insertData, info.WarehousingCount)
  419. insertData = append(insertData, info.LicenseNumber)
  420. insertData = append(insertData, info.PackingPrice)
  421. insertData = append(insertData, info.StorehouseId)
  422. }
  423. thisSQL += strings.Join(insertParams, ", ")
  424. err = utx.Exec(thisSQL, insertData...).Error
  425. if err != nil {
  426. utx.Rollback()
  427. return
  428. }
  429. }
  430. utx.Commit()
  431. }
  432. return
  433. }
  434. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  435. if len(warehousingInfo) > 0 {
  436. utx := writeDb.Begin()
  437. if len(warehousingInfo) > 0 {
  438. 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 "
  439. insertParams := make([]string, 0)
  440. insertData := make([]interface{}, 0)
  441. for _, info := range warehousingInfo {
  442. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  443. insertData = append(insertData, info.WarehousingId)
  444. insertData = append(insertData, info.DrugId)
  445. insertData = append(insertData, info.Number)
  446. insertData = append(insertData, info.ProductDate)
  447. insertData = append(insertData, info.ExpiryDate)
  448. insertData = append(insertData, info.WarehousingCount)
  449. insertData = append(insertData, info.Price)
  450. insertData = append(insertData, info.TotalPrice)
  451. insertData = append(insertData, info.Dealer)
  452. insertData = append(insertData, info.Manufacturer)
  453. insertData = append(insertData, info.Remark)
  454. insertData = append(insertData, info.Ctime)
  455. insertData = append(insertData, info.Mtime)
  456. insertData = append(insertData, info.Status)
  457. insertData = append(insertData, info.OrgId)
  458. insertData = append(insertData, info.WarehousingOrder)
  459. insertData = append(insertData, info.Type)
  460. insertData = append(insertData, info.RetailPrice)
  461. insertData = append(insertData, info.RetailTotalPrice)
  462. insertData = append(insertData, info.StockMaxNumber)
  463. insertData = append(insertData, info.MaxUnit)
  464. insertData = append(insertData, info.MinUnit)
  465. insertData = append(insertData, info.BatchNumber)
  466. insertData = append(insertData, info.StockMinNumber)
  467. insertData = append(insertData, info.StorehouseId)
  468. }
  469. thisSQL += strings.Join(insertParams, ", ")
  470. err = utx.Exec(thisSQL, insertData...).Error
  471. if err != nil {
  472. utx.Rollback()
  473. return
  474. }
  475. }
  476. utx.Commit()
  477. }
  478. return
  479. }
  480. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  481. if len(warehousingInfo) > 0 {
  482. utx := writeDb.Begin()
  483. if len(warehousingInfo) > 0 {
  484. 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 "
  485. insertParams := make([]string, 0)
  486. insertData := make([]interface{}, 0)
  487. for _, info := range warehousingInfo {
  488. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  489. insertData = append(insertData, info.WarehousingId)
  490. insertData = append(insertData, info.DrugId)
  491. insertData = append(insertData, info.Number)
  492. insertData = append(insertData, info.ProductDate)
  493. insertData = append(insertData, info.ExpiryDate)
  494. insertData = append(insertData, info.WarehousingCount)
  495. insertData = append(insertData, info.Price)
  496. insertData = append(insertData, info.TotalPrice)
  497. insertData = append(insertData, info.Dealer)
  498. insertData = append(insertData, info.Manufacturer)
  499. insertData = append(insertData, info.Remark)
  500. insertData = append(insertData, info.Ctime)
  501. insertData = append(insertData, info.Mtime)
  502. insertData = append(insertData, info.Status)
  503. insertData = append(insertData, info.OrgId)
  504. insertData = append(insertData, info.WarehousingOrder)
  505. insertData = append(insertData, info.Type)
  506. insertData = append(insertData, info.RetailPrice)
  507. insertData = append(insertData, info.RetailTotalPrice)
  508. insertData = append(insertData, info.StockMaxNumber)
  509. insertData = append(insertData, info.MaxUnit)
  510. insertData = append(insertData, info.MinUnit)
  511. insertData = append(insertData, info.BatchNumber)
  512. insertData = append(insertData, info.StockMinNumber)
  513. insertData = append(insertData, info.SupplyWarehouseId)
  514. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  515. insertData = append(insertData, info.StorehouseId)
  516. insertData = append(insertData, info.IsCheck)
  517. }
  518. thisSQL += strings.Join(insertParams, ", ")
  519. err = utx.Exec(thisSQL, insertData...).Error
  520. if err != nil {
  521. utx.Rollback()
  522. return
  523. }
  524. }
  525. utx.Commit()
  526. }
  527. return
  528. }
  529. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  530. var total float64
  531. var min_number int64
  532. for _, item := range warehousingInfo {
  533. //查询刚入库耗材的库存
  534. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  535. //更新药品库的总量
  536. info, _ := FindeDrugInfo(item.DrugId)
  537. min_number = count.StockMaxNumber * info.MinNumber
  538. str := strconv.FormatInt(min_number, 10)
  539. minFloat, _ := strconv.ParseFloat(str, 64)
  540. total = info.Total + minFloat
  541. lib := models.BaseDrugLib{
  542. Total: total,
  543. }
  544. UpdateBaseDrug(&lib, item.ID)
  545. }
  546. return err
  547. }
  548. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  549. info := models.XtDrugWarehouseInfo{}
  550. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  551. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  552. return info, err
  553. }
  554. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  555. lib := models.BaseDrugLib{}
  556. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  557. return lib, err
  558. }
  559. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  560. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  561. return err
  562. }
  563. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  564. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  565. return err
  566. }
  567. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  568. info := models.DrugWarehouseInfo{}
  569. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  570. return info, err
  571. }
  572. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  573. out := models.DrugWarehouseOut{}
  574. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  575. return out, err
  576. }
  577. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  578. if len(drugflow) > 0 {
  579. utx := writeDb.Begin()
  580. if len(drugflow) > 0 {
  581. 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 "
  582. insertParams := make([]string, 0)
  583. insertData := make([]interface{}, 0)
  584. for _, info := range drugflow {
  585. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  586. insertData = append(insertData, info.WarehousingId)
  587. insertData = append(insertData, info.DrugId)
  588. insertData = append(insertData, info.Number)
  589. insertData = append(insertData, info.BatchNumber)
  590. insertData = append(insertData, info.Count)
  591. insertData = append(insertData, info.UserOrgId)
  592. insertData = append(insertData, info.PatientId)
  593. insertData = append(insertData, info.SystemTime)
  594. insertData = append(insertData, info.ConsumableType)
  595. insertData = append(insertData, info.IsSys)
  596. insertData = append(insertData, info.WarehousingOrder)
  597. insertData = append(insertData, info.WarehouseOutId)
  598. insertData = append(insertData, info.WarehouseOutOrderNumber)
  599. insertData = append(insertData, info.IsEdit)
  600. insertData = append(insertData, info.CancelStockId)
  601. insertData = append(insertData, info.CancelOrderNumber)
  602. insertData = append(insertData, info.Manufacturer)
  603. insertData = append(insertData, info.Dealer)
  604. insertData = append(insertData, info.Creator)
  605. insertData = append(insertData, info.UpdateCreator)
  606. insertData = append(insertData, info.Status)
  607. insertData = append(insertData, info.Ctime)
  608. insertData = append(insertData, info.Mtime)
  609. insertData = append(insertData, info.Price)
  610. insertData = append(insertData, info.WarehousingDetailId)
  611. insertData = append(insertData, info.WarehouseOutDetailId)
  612. insertData = append(insertData, info.CancelOutDetailId)
  613. insertData = append(insertData, info.ExpireDate)
  614. insertData = append(insertData, info.ProductDate)
  615. insertData = append(insertData, info.MaxUnit)
  616. insertData = append(insertData, info.MinUnit)
  617. insertData = append(insertData, info.StorehouseId)
  618. }
  619. thisSQL += strings.Join(insertParams, ", ")
  620. err = utx.Exec(thisSQL, insertData...).Error
  621. if err != nil {
  622. utx.Rollback()
  623. return
  624. }
  625. }
  626. utx.Commit()
  627. }
  628. return
  629. }
  630. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  631. info := models.DrugFlow{}
  632. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  633. if err == gorm.ErrRecordNotFound {
  634. return nil, err
  635. }
  636. if err != nil {
  637. return nil, err
  638. }
  639. return &info, nil
  640. }
  641. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  642. info := models.XtDrugWarehouseInfo{}
  643. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  644. return info, err
  645. }
  646. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  647. warehouseInfo := models.XtDrugWarehouseInfo{}
  648. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  649. return err
  650. }
  651. func CreateDrugFlowOne(flow models.DrugFlow) error {
  652. ut := writeDb.Begin()
  653. err := ut.Create(&flow).Error
  654. if err != nil {
  655. ut.Rollback()
  656. return err
  657. }
  658. ut.Commit()
  659. return err
  660. }
  661. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  662. err := tx.Create(&flow).Error
  663. if err != nil {
  664. tx.Rollback()
  665. return err
  666. }
  667. return err
  668. }
  669. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  670. detail := models.DrugAutomaticReduceDetail{}
  671. 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
  672. 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
  673. return detail, err
  674. }
  675. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  676. flow := models.DrugFlow{}
  677. 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
  678. return flow, err
  679. }
  680. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  681. tx := XTWriteDB().Begin()
  682. err := tx.Create(&flow).Error
  683. if err != nil {
  684. tx.Rollback()
  685. return err
  686. }
  687. tx.Commit()
  688. return err
  689. }
  690. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  691. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  692. return info, err
  693. }
  694. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  695. info := models.WarehousingInfo{}
  696. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  697. return info, err
  698. }
  699. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  700. err := XTWriteDB().Create(&info).Error
  701. return err
  702. }
  703. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  704. if storehouse_id == 0 {
  705. 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
  706. }
  707. if storehouse_id > 0 {
  708. 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
  709. }
  710. return info, err
  711. }
  712. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  713. if storehouse_id == 0 {
  714. 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
  715. if err != gorm.ErrRecordNotFound {
  716. if err != nil {
  717. tx.Rollback()
  718. return info, err
  719. }
  720. }
  721. }
  722. if storehouse_id > 0 {
  723. 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
  724. if err != gorm.ErrRecordNotFound {
  725. if err != nil {
  726. tx.Rollback()
  727. return info, err
  728. }
  729. }
  730. }
  731. return info, err
  732. }
  733. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  734. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  735. if good_id > 0 {
  736. db = db.Where("x.good_id = ?", good_id)
  737. }
  738. if good_type_id > 0 {
  739. db = db.Where("x.good_type_id = ?", good_type_id)
  740. }
  741. if storehouse_id > 0 {
  742. db = db.Where("x.storehouse_id = ?", storehouse_id)
  743. }
  744. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  745. return info, err
  746. }
  747. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  748. info := models.WarehousingInfo{}
  749. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  750. return info, err
  751. }
  752. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  753. info := models.WarehousingInfo{}
  754. var err error
  755. if storehouse_id == 0 {
  756. 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
  757. }
  758. if storehouse_id > 0 {
  759. 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
  760. }
  761. return info, err
  762. }
  763. //药品先进先出,找出最先入库的批次
  764. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  765. 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
  766. return info, err
  767. }
  768. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  769. 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
  770. if err != nil {
  771. tx.Rollback()
  772. return info, err
  773. }
  774. return info, err
  775. }
  776. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  777. if is_source == 1 {
  778. 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
  779. }
  780. if is_source == 2 || is_source == 0 {
  781. 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
  782. }
  783. return info, err
  784. }
  785. // 出库时先进先出,找出最先入库的批次
  786. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  787. 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
  788. return info, err
  789. }
  790. //
  791. //// 出库时先进先出,找出最先入库的批次
  792. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  793. // 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
  794. // return info, err
  795. //}
  796. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  797. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  798. return info, err
  799. }
  800. 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) {
  801. offset := (page - 1) * limit
  802. db := readDb.Model(&models.Warehousing{})
  803. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  804. if len(keywords) > 0 {
  805. likeKey := "%" + keywords + "%"
  806. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  807. 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")
  808. }
  809. if startTime > 0 {
  810. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  811. }
  812. if endTime > 0 {
  813. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  814. }
  815. if storehouse_id > 0 {
  816. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  817. }
  818. if check_type == 0 {
  819. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  820. }
  821. if check_type > 0 {
  822. db = db.Where("xt_warehouse.is_check = ?", check_type)
  823. }
  824. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  825. return
  826. }
  827. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  828. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  829. if orgid > 0 {
  830. db = db.Where("org_id = ?", orgid)
  831. }
  832. if len(keyword) > 0 {
  833. likeKey := "%" + keyword + "%"
  834. db = db.Where("good_name like ?", likeKey)
  835. }
  836. err = db.Find(&goodInfo).Error
  837. return goodInfo, err
  838. }
  839. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  840. db := XTReadDB().Model(&info).Where("status =1")
  841. if len(good_id) > 0 {
  842. db = db.Where("good_id in (?)", good_id)
  843. }
  844. if startime > 0 {
  845. db = db.Where("ctime >=?", startime)
  846. }
  847. if endtime > 0 {
  848. db = db.Where("ctime<=?", endtime)
  849. }
  850. if orgid > 0 {
  851. db = db.Where("org_id = ?", orgid)
  852. }
  853. if storehouse_id > 0 {
  854. db = db.Where("storehouse_id = ?", storehouse_id)
  855. }
  856. err = db.Find(&info).Error
  857. return info, err
  858. }
  859. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  860. db := XTReadDB().Model(&info).Where("status =1")
  861. if len(good_id) > 0 {
  862. db = db.Where("good_id in (?)", good_id)
  863. }
  864. if startime > 0 {
  865. db = db.Where("ctime >=?", startime)
  866. }
  867. if endtime > 0 {
  868. db = db.Where("ctime<=?", endtime)
  869. }
  870. if orgid > 0 {
  871. db = db.Where("org_id = ?", orgid)
  872. }
  873. if storehouse_id > 0 {
  874. db = db.Where("storehouse_id = ?", storehouse_id)
  875. }
  876. err = db.Find(&info).Error
  877. return info, err
  878. }
  879. type VMDrugWarehouse struct {
  880. ID int64 `gorm:"column:id" json:"id" form:"id"`
  881. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  882. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  883. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  884. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  885. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  886. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  887. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  888. Status int64 `gorm:"column:status" json:"status" form:"status"`
  889. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  890. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  891. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  892. Type int64 `gorm:"column:type" json:"type" form:"type"`
  893. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  894. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  895. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  896. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  897. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  898. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  899. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  900. }
  901. func (VMDrugWarehouse) TableName() string {
  902. return "xt_drug_warehouse"
  903. }
  904. type VMDrugWarehouseInfo struct {
  905. ID int64 `gorm:"column:id" json:"id" form:"id"`
  906. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  907. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  908. Number string `gorm:"column:number" json:"number" form:"number"`
  909. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  910. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  911. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  912. Price float64 `gorm:"column:price" json:"price" form:"price"`
  913. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  914. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  915. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  916. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  917. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  918. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  919. Status int64 `gorm:"column:status" json:"status" form:"status"`
  920. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  921. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  922. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  923. Type int64 `gorm:"column:type" json:"type" form:"type"`
  924. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  925. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  926. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  927. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  928. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  929. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  930. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  931. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  932. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  933. }
  934. func (VMDrugWarehouseInfo) TableName() string {
  935. return "xt_drug_warehouse_info"
  936. }
  937. type Drug struct {
  938. ID int64 `gorm:"column:id" json:"id" form:"id"`
  939. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  940. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  941. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  942. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  943. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  944. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  945. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  946. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  947. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  948. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  949. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  950. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  951. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  952. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  953. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  954. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  955. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  956. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  957. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  958. Number string `gorm:"column:number" json:"number" form:"number"`
  959. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  960. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  961. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  962. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  963. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  964. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  965. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  966. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  967. Code string `gorm:"column:code" json:"code" form:"code"`
  968. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  969. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  970. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  971. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  972. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  973. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  974. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  975. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  976. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  977. Label int64 `gorm:"column:label" json:"label" form:"label"`
  978. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  979. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  980. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  981. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  982. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  983. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  984. Status int64 `gorm:"column:status" json:"status" form:"status"`
  985. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  986. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  987. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  988. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  989. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  990. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  991. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  992. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  993. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  994. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  995. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  996. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  997. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  998. Total float64 `gorm:"column:total" json:"total" form:"total"`
  999. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1000. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1001. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1002. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1003. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1004. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1005. }
  1006. func (Drug) TableName() string {
  1007. return "xt_base_drug"
  1008. }
  1009. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1010. db := readDb.Model(&VMDrugWarehouse{})
  1011. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1012. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1013. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1014. if len(keywords) > 0 {
  1015. likeKey := "%" + keywords + "%"
  1016. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1017. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1018. 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")
  1019. }
  1020. if startTime > 0 {
  1021. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1022. }
  1023. if endTime > 0 {
  1024. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1025. }
  1026. db = db.Count(&total)
  1027. offset := (page - 1) * limit
  1028. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1029. return
  1030. }
  1031. 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) {
  1032. db := readDb.Model(&VMDrugWarehouse{})
  1033. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1034. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1035. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1036. if len(keywords) > 0 {
  1037. likeKey := "%" + keywords + "%"
  1038. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1039. 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")
  1040. }
  1041. if startTime > 0 {
  1042. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1043. }
  1044. if endTime > 0 {
  1045. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1046. }
  1047. if storehouse_id > 0 {
  1048. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1049. }
  1050. if check_type > 0 {
  1051. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1052. }
  1053. db = db.Count(&total)
  1054. offset := (page - 1) * limit
  1055. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1056. return
  1057. }
  1058. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1059. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1060. return list, err
  1061. }
  1062. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1063. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1064. return list, err
  1065. }
  1066. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1067. 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
  1068. return list, err
  1069. }
  1070. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1071. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1072. return list, err
  1073. }
  1074. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1075. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1076. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1077. fmt.Println(table)
  1078. if len(id) > 0 {
  1079. db = db.Where("x.warehousing_id in(?)", id)
  1080. }
  1081. if org_id > 0 {
  1082. db = db.Where("x.org_id = ?", org_id)
  1083. }
  1084. 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
  1085. return list, err
  1086. }
  1087. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1088. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1089. return warehousing, err
  1090. }
  1091. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1092. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1093. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1094. return warehousing, err
  1095. }
  1096. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1097. 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
  1098. return warehousing, err
  1099. }
  1100. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1101. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1102. if len(id) > 0 {
  1103. db = db.Where("x.id in(?)", id)
  1104. }
  1105. if org_id > 0 {
  1106. db = db.Where("x.org_id = ?", org_id)
  1107. }
  1108. 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
  1109. return warehousing, err
  1110. }
  1111. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1112. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1113. // return warehousing, err
  1114. //
  1115. //}
  1116. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1117. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1118. return
  1119. }
  1120. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1121. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1122. return
  1123. }
  1124. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1125. if len(salesReturnInfo) > 0 {
  1126. utx := writeDb.Begin()
  1127. if len(salesReturnInfo) > 0 {
  1128. 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 "
  1129. insertParams := make([]string, 0)
  1130. insertData := make([]interface{}, 0)
  1131. for _, info := range salesReturnInfo {
  1132. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1133. insertData = append(insertData, info.GoodId)
  1134. insertData = append(insertData, info.SalesReturnId)
  1135. insertData = append(insertData, info.GoodTypeId)
  1136. insertData = append(insertData, info.Count)
  1137. insertData = append(insertData, info.Price)
  1138. insertData = append(insertData, info.Total)
  1139. insertData = append(insertData, info.Ctime)
  1140. insertData = append(insertData, info.Status)
  1141. insertData = append(insertData, info.OrgId)
  1142. insertData = append(insertData, info.OrderNumber)
  1143. insertData = append(insertData, info.Type)
  1144. insertData = append(insertData, info.Dealer)
  1145. insertData = append(insertData, info.Manufacturer)
  1146. }
  1147. thisSQL += strings.Join(insertParams, ", ")
  1148. err = utx.Exec(thisSQL, insertData...).Error
  1149. if err != nil {
  1150. utx.Rollback()
  1151. return
  1152. }
  1153. }
  1154. utx.Commit()
  1155. }
  1156. return
  1157. }
  1158. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1159. if len(salesReturnInfo) > 0 {
  1160. utx := writeDb.Begin()
  1161. if len(salesReturnInfo) > 0 {
  1162. 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 "
  1163. insertParams := make([]string, 0)
  1164. insertData := make([]interface{}, 0)
  1165. for _, info := range salesReturnInfo {
  1166. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1167. insertData = append(insertData, info.DrugId)
  1168. insertData = append(insertData, info.SalesReturnId)
  1169. insertData = append(insertData, info.Count)
  1170. insertData = append(insertData, info.Price)
  1171. insertData = append(insertData, info.Total)
  1172. insertData = append(insertData, info.Ctime)
  1173. insertData = append(insertData, info.Status)
  1174. insertData = append(insertData, info.OrgId)
  1175. insertData = append(insertData, info.OrderNumber)
  1176. insertData = append(insertData, info.Type)
  1177. insertData = append(insertData, info.Dealer)
  1178. insertData = append(insertData, info.Manufacturer)
  1179. insertData = append(insertData, info.RetailPrice)
  1180. insertData = append(insertData, info.RetailTotalPrice)
  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 FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1194. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1195. return total, err
  1196. }
  1197. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1198. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1199. return total, err
  1200. }
  1201. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1202. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1203. if err != gorm.ErrRecordNotFound {
  1204. if err != nil {
  1205. tx.Rollback()
  1206. return total, err
  1207. }
  1208. }
  1209. return total, err
  1210. }
  1211. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1212. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1213. return total, err
  1214. }
  1215. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1216. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1217. if err != nil {
  1218. tx.Rollback()
  1219. return total, err
  1220. }
  1221. return total, err
  1222. }
  1223. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1224. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1225. // return total, err
  1226. //}
  1227. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1228. db := readDb.Model(&models.SalesReturn{})
  1229. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1230. if len(keywords) > 0 {
  1231. likeKey := "%" + keywords + "%"
  1232. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1233. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1234. 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")
  1235. }
  1236. if startTime > 0 {
  1237. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1238. }
  1239. if endTime > 0 {
  1240. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1241. }
  1242. db = db.Count(&total)
  1243. offset := (page - 1) * limit
  1244. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1245. return
  1246. }
  1247. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1248. db := readDb.Model(&models.DrugSalesReturn{})
  1249. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1250. if len(keywords) > 0 {
  1251. likeKey := "%" + keywords + "%"
  1252. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1253. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1254. 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")
  1255. }
  1256. if startTime > 0 {
  1257. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1258. }
  1259. if endTime > 0 {
  1260. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1261. }
  1262. db = db.Count(&total)
  1263. offset := (page - 1) * limit
  1264. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1265. return
  1266. }
  1267. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1268. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1269. return list, err
  1270. }
  1271. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1272. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1273. return list, err
  1274. }
  1275. type DrugCancelStockInfo struct {
  1276. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1277. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1278. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1279. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1280. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1281. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1282. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1283. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1284. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1285. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1286. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1287. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1288. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1289. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1290. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1291. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1292. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1293. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1294. Number string `gorm:"column:number" json:"number" form:"number"`
  1295. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1296. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1297. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1298. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1299. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1300. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1301. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1302. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1303. }
  1304. func (DrugCancelStockInfo) TableName() string {
  1305. return "xt_drug_cancel_stock_info"
  1306. }
  1307. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1308. 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
  1309. return list, err
  1310. }
  1311. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1312. db := readDb.Model(&models.WarehousingInfo{})
  1313. db = db.Where("org_id = ? AND status = 1", orgId)
  1314. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1315. if startTime > 0 {
  1316. db = db.Where("ctime >=?", startTime)
  1317. }
  1318. if endTime > 0 {
  1319. db = db.Where("ctime<= ?", endTime)
  1320. }
  1321. db = db.Count(&total)
  1322. offset := (page - 1) * limit
  1323. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1324. return list, total, err
  1325. }
  1326. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1327. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1328. return total, err
  1329. }
  1330. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1331. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1332. return total, err
  1333. }
  1334. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut) error {
  1335. err := writeDb.Create(&warehouseOut).Error
  1336. return err
  1337. }
  1338. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1339. err := tx.Create(&warehouseOut).Error
  1340. if err != nil {
  1341. tx.Rollback()
  1342. return err
  1343. }
  1344. return err
  1345. }
  1346. func UpdateSingleWarehoseOut(id int64, storehouse_id int64) error {
  1347. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1348. return err
  1349. }
  1350. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1351. err := writeDb.Create(&warehouseOut).Error
  1352. return err
  1353. }
  1354. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1355. err := tx.Create(&warehouseOut).Error
  1356. if err != nil {
  1357. tx.Rollback()
  1358. return err
  1359. }
  1360. return err
  1361. }
  1362. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1363. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1364. return err
  1365. }
  1366. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1367. if len(warehouseOutInfo) > 0 {
  1368. utx := writeDb.Begin()
  1369. if len(warehouseOutInfo) > 0 {
  1370. 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 "
  1371. insertParams := make([]string, 0)
  1372. insertData := make([]interface{}, 0)
  1373. for _, info := range warehouseOutInfo {
  1374. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1375. insertData = append(insertData, info.WarehouseOutId)
  1376. insertData = append(insertData, info.GoodId)
  1377. insertData = append(insertData, info.GoodTypeId)
  1378. insertData = append(insertData, info.ProductDate)
  1379. insertData = append(insertData, info.ExpiryDate)
  1380. insertData = append(insertData, info.Count)
  1381. insertData = append(insertData, info.Price)
  1382. insertData = append(insertData, info.TotalPrice)
  1383. insertData = append(insertData, info.Remark)
  1384. insertData = append(insertData, info.Ctime)
  1385. insertData = append(insertData, info.Status)
  1386. insertData = append(insertData, info.OrgId)
  1387. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1388. insertData = append(insertData, info.Type)
  1389. insertData = append(insertData, info.Dealer)
  1390. insertData = append(insertData, info.Manufacturer)
  1391. insertData = append(insertData, info.Number)
  1392. insertData = append(insertData, info.LicenseNumber)
  1393. }
  1394. thisSQL += strings.Join(insertParams, ", ")
  1395. err = utx.Exec(thisSQL, insertData...).Error
  1396. if err != nil {
  1397. utx.Rollback()
  1398. return
  1399. }
  1400. }
  1401. utx.Commit()
  1402. }
  1403. return
  1404. }
  1405. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1406. if len(warehouseOutInfo) > 0 {
  1407. utx := writeDb.Begin()
  1408. if len(warehouseOutInfo) > 0 {
  1409. 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 "
  1410. insertParams := make([]string, 0)
  1411. insertData := make([]interface{}, 0)
  1412. for _, info := range warehouseOutInfo {
  1413. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1414. insertData = append(insertData, info.WarehouseOutId)
  1415. insertData = append(insertData, info.DrugId)
  1416. insertData = append(insertData, info.ProductDate)
  1417. insertData = append(insertData, info.ExpiryDate)
  1418. insertData = append(insertData, info.Count)
  1419. insertData = append(insertData, info.Price)
  1420. insertData = append(insertData, info.TotalPrice)
  1421. insertData = append(insertData, info.Remark)
  1422. insertData = append(insertData, info.Ctime)
  1423. insertData = append(insertData, info.Status)
  1424. insertData = append(insertData, info.OrgId)
  1425. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1426. insertData = append(insertData, info.Type)
  1427. insertData = append(insertData, info.Dealer)
  1428. insertData = append(insertData, info.Manufacturer)
  1429. insertData = append(insertData, info.RetailPrice)
  1430. insertData = append(insertData, info.RetailTotalPrice)
  1431. insertData = append(insertData, info.Number)
  1432. insertData = append(insertData, info.BatchNumber)
  1433. insertData = append(insertData, info.CountUnit)
  1434. }
  1435. thisSQL += strings.Join(insertParams, ", ")
  1436. err = utx.Exec(thisSQL, insertData...).Error
  1437. if err != nil {
  1438. utx.Rollback()
  1439. return
  1440. }
  1441. }
  1442. utx.Commit()
  1443. }
  1444. return
  1445. }
  1446. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1447. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1448. return info, err
  1449. }
  1450. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1451. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1452. return info, err
  1453. }
  1454. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1455. db := readDb.Model(&models.WarehouseOut{})
  1456. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1457. if len(keywords) > 0 {
  1458. likeKey := "%" + keywords + "%"
  1459. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1460. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1461. 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")
  1462. }
  1463. if startTime > 0 {
  1464. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1465. }
  1466. if endTime > 0 {
  1467. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1468. }
  1469. db = db.Count(&total)
  1470. offset := (page - 1) * limit
  1471. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1472. return list, total, err
  1473. }
  1474. 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) {
  1475. db := readDb.Model(&models.WarehouseOut{})
  1476. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1477. if len(keywords) > 0 {
  1478. likeKey := "%" + keywords + "%"
  1479. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1480. 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")
  1481. }
  1482. if startTime > 0 {
  1483. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1484. }
  1485. if endTime > 0 {
  1486. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1487. }
  1488. if storehouse_id > 0 {
  1489. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1490. }
  1491. if way_type == 0 {
  1492. 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")
  1493. }
  1494. if way_type == 1 {
  1495. db = db.Where("xt_warehouse_out.is_sys = 0")
  1496. }
  1497. if way_type == 2 {
  1498. db = db.Where("xt_warehouse_out.is_sys = 1")
  1499. }
  1500. if way_type == 3 {
  1501. db = db.Where("xt_warehouse_out.is_sys = 12")
  1502. }
  1503. if way_type == 4 {
  1504. db = db.Where("xt_warehouse_out.is_sys = 5")
  1505. }
  1506. if check_type > 0 {
  1507. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1508. }
  1509. if check_type == 0 {
  1510. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1511. }
  1512. db = db.Count(&total)
  1513. offset := (page - 1) * limit
  1514. if orgId == 10191 {
  1515. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1516. } else {
  1517. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1518. }
  1519. return list, total, err
  1520. }
  1521. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1522. db := readDb.Model(&models.DrugWarehouseOut{})
  1523. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1524. if len(keywords) > 0 {
  1525. likeKey := "%" + keywords + "%"
  1526. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1527. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1528. 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")
  1529. }
  1530. if startTime > 0 {
  1531. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1532. }
  1533. if endTime > 0 {
  1534. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1535. }
  1536. db = db.Count(&total)
  1537. offset := (page - 1) * limit
  1538. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1539. return list, total, err
  1540. }
  1541. 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) {
  1542. db := readDb.Model(&models.DrugWarehouseOut{})
  1543. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1544. if len(keywords) > 0 {
  1545. likeKey := "%" + keywords + "%"
  1546. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1547. 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")
  1548. }
  1549. if startTime > 0 {
  1550. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1551. }
  1552. if endTime > 0 {
  1553. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1554. }
  1555. if storehouse_id > 0 {
  1556. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1557. }
  1558. if check_type > 0 {
  1559. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1560. }
  1561. if way_type == 0 {
  1562. 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")
  1563. }
  1564. if way_type == 1 {
  1565. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1566. }
  1567. if way_type == 2 {
  1568. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1569. }
  1570. if way_type == 3 {
  1571. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1572. }
  1573. if way_type == 4 {
  1574. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1575. }
  1576. db = db.Count(&total)
  1577. offset := (page - 1) * limit
  1578. if orgId == 10191 {
  1579. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1580. } else {
  1581. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1582. }
  1583. return list, total, err
  1584. }
  1585. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1586. 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
  1587. return list, err
  1588. }
  1589. type DrugWarehouseOutInfo struct {
  1590. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1591. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1592. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1593. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1594. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1595. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1596. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1597. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1598. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1599. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1600. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1601. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1602. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1603. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1604. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1605. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1606. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1607. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1608. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1609. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1610. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1611. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1612. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1613. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1614. Number string `gorm:"column:number" json:"number" form:"number"`
  1615. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1616. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1617. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1618. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1619. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1620. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1621. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1622. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1623. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1624. }
  1625. func (DrugWarehouseOutInfo) TableName() string {
  1626. return "xt_drug_warehouse_out_info"
  1627. }
  1628. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1629. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1630. return list, err
  1631. }
  1632. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1633. info := &models.WarehousingInfo{}
  1634. type WarehouseInfoCountAmount struct {
  1635. Total int64
  1636. }
  1637. var result WarehouseInfoCountAmount
  1638. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1639. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1640. return result.Total, info
  1641. }
  1642. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1643. type SalesReturnCountAmount struct {
  1644. Total int64
  1645. }
  1646. var result SalesReturnCountAmount
  1647. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1648. return result.Total, err
  1649. }
  1650. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1651. type WarehouseOutInfoCountAmount struct {
  1652. Total int64
  1653. }
  1654. var result WarehouseOutInfoCountAmount
  1655. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1656. return result.Total, err
  1657. }
  1658. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1659. type CancleStockoCountAmount struct {
  1660. Total int64
  1661. }
  1662. var result CancleStockoCountAmount
  1663. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1664. return result.Total, err
  1665. }
  1666. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1667. info := models.WarehousingInfo{}
  1668. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  1669. return info, err
  1670. }
  1671. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  1672. stock := models.CancelStock{}
  1673. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  1674. if err == gorm.ErrRecordNotFound {
  1675. return nil, err
  1676. }
  1677. if err != nil {
  1678. return nil, err
  1679. }
  1680. return &stock, nil
  1681. }
  1682. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  1683. stock := models.CancelStock{}
  1684. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1685. if err == gorm.ErrRecordNotFound {
  1686. return nil, err
  1687. }
  1688. if err != nil {
  1689. return nil, err
  1690. }
  1691. return &stock, nil
  1692. }
  1693. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  1694. stock := models.CancelStock{}
  1695. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1696. if err != gorm.ErrRecordNotFound {
  1697. if err != nil {
  1698. tx.Rollback()
  1699. return &stock, err
  1700. }
  1701. }
  1702. return &stock, err
  1703. }
  1704. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  1705. err := writeDb.Create(&cancelStock).Error
  1706. return err
  1707. }
  1708. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  1709. err := tx.Create(&cancelStock).Error
  1710. if err != nil {
  1711. tx.Rollback()
  1712. return err
  1713. }
  1714. return err
  1715. }
  1716. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  1717. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1718. return err
  1719. }
  1720. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  1721. stock := models.CancelStock{}
  1722. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1723. return stock, err
  1724. }
  1725. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  1726. stock := models.CancelStock{}
  1727. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1728. if err != gorm.ErrRecordNotFound {
  1729. if err != nil {
  1730. tx.Rollback()
  1731. return stock, err
  1732. }
  1733. }
  1734. return stock, err
  1735. }
  1736. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  1737. stock := models.DrugCancelStock{}
  1738. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1739. return stock, err
  1740. }
  1741. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  1742. stock := models.DrugCancelStock{}
  1743. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1744. if err != gorm.ErrRecordNotFound {
  1745. if err != nil {
  1746. tx.Rollback()
  1747. return stock, err
  1748. }
  1749. }
  1750. return stock, err
  1751. }
  1752. func GetManufactureById(id int64) (models.Manufacturer, error) {
  1753. manufacturer := models.Manufacturer{}
  1754. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1755. return manufacturer, err
  1756. }
  1757. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  1758. manufacturer := models.Manufacturer{}
  1759. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1760. if err != nil {
  1761. tx.Rollback()
  1762. return manufacturer, err
  1763. }
  1764. return manufacturer, err
  1765. }
  1766. func GetDealerById(id int64) (models.Dealer, error) {
  1767. dealer := models.Dealer{}
  1768. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1769. return dealer, err
  1770. }
  1771. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  1772. dealer := models.Dealer{}
  1773. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1774. if err != gorm.ErrRecordNotFound {
  1775. if err != nil {
  1776. tx.Rollback()
  1777. return dealer, err
  1778. }
  1779. }
  1780. return dealer, err
  1781. }
  1782. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  1783. err := writeDb.Create(&cancelStock).Error
  1784. return err
  1785. }
  1786. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  1787. err := tx.Create(&cancelStock).Error
  1788. if err != nil {
  1789. tx.Rollback()
  1790. return err
  1791. }
  1792. return err
  1793. }
  1794. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  1795. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1796. return err
  1797. }
  1798. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  1799. // err := tx.Create(&cancelStock).Error
  1800. // return err
  1801. //}
  1802. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  1803. if len(cancelStockInfo) > 0 {
  1804. utx := writeDb.Begin()
  1805. if len(cancelStockInfo) > 0 {
  1806. 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 "
  1807. insertParams := make([]string, 0)
  1808. insertData := make([]interface{}, 0)
  1809. for _, info := range cancelStockInfo {
  1810. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1811. insertData = append(insertData, info.GoodId)
  1812. insertData = append(insertData, info.CancelStockId)
  1813. insertData = append(insertData, info.GoodTypeId)
  1814. insertData = append(insertData, info.Count)
  1815. insertData = append(insertData, info.Price)
  1816. insertData = append(insertData, info.Total)
  1817. insertData = append(insertData, info.Ctime)
  1818. insertData = append(insertData, info.Status)
  1819. insertData = append(insertData, info.OrgId)
  1820. insertData = append(insertData, info.OrderNumber)
  1821. insertData = append(insertData, info.Type)
  1822. insertData = append(insertData, info.Dealer)
  1823. insertData = append(insertData, info.Manufacturer)
  1824. insertData = append(insertData, info.Number)
  1825. insertData = append(insertData, info.RegisterAccount)
  1826. insertData = append(insertData, info.Remark)
  1827. insertData = append(insertData, info.ProductDate)
  1828. insertData = append(insertData, info.ExpiryDate)
  1829. insertData = append(insertData, info.WarehouseInfoId)
  1830. insertData = append(insertData, info.WarehouseInfoId)
  1831. }
  1832. thisSQL += strings.Join(insertParams, ", ")
  1833. err = utx.Exec(thisSQL, insertData...).Error
  1834. if err != nil {
  1835. utx.Rollback()
  1836. return
  1837. }
  1838. }
  1839. utx.Commit()
  1840. }
  1841. return
  1842. }
  1843. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  1844. err := XTWriteDB().Create(&info).Error
  1845. return err
  1846. }
  1847. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  1848. // err := tx.Create(&info).Error
  1849. // return err
  1850. //}
  1851. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  1852. info := models.DrugCancelStockInfo{}
  1853. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1854. return info, err
  1855. }
  1856. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  1857. // info := models.DrugCancelStockInfo{}
  1858. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1859. // return info, err
  1860. //}
  1861. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  1862. err := XTWriteDB().Create(&info).Error
  1863. return err
  1864. }
  1865. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  1866. err := XTWriteDB().Create(&info).Error
  1867. if err != nil {
  1868. tx.Rollback()
  1869. return err
  1870. }
  1871. return err
  1872. }
  1873. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  1874. info := models.CancelStockInfo{}
  1875. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1876. return info, err
  1877. }
  1878. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  1879. info := models.CancelStockInfo{}
  1880. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1881. if err != gorm.ErrRecordNotFound {
  1882. if err != nil {
  1883. tx.Rollback()
  1884. return info, err
  1885. }
  1886. }
  1887. return info, err
  1888. }
  1889. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  1890. info := models.DrugCancelStockInfo{}
  1891. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  1892. return info, err
  1893. }
  1894. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  1895. if len(cancelStockInfo) > 0 {
  1896. utx := writeDb.Begin()
  1897. if len(cancelStockInfo) > 0 {
  1898. 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 "
  1899. insertParams := make([]string, 0)
  1900. insertData := make([]interface{}, 0)
  1901. for _, info := range cancelStockInfo {
  1902. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1903. insertData = append(insertData, info.DrugId)
  1904. insertData = append(insertData, info.CancelStockId)
  1905. insertData = append(insertData, info.Count)
  1906. insertData = append(insertData, info.Price)
  1907. insertData = append(insertData, info.Total)
  1908. insertData = append(insertData, info.Ctime)
  1909. insertData = append(insertData, info.Status)
  1910. insertData = append(insertData, info.OrgId)
  1911. insertData = append(insertData, info.OrderNumber)
  1912. insertData = append(insertData, info.Type)
  1913. insertData = append(insertData, info.Dealer)
  1914. insertData = append(insertData, info.Manufacturer)
  1915. insertData = append(insertData, info.RetailPrice)
  1916. insertData = append(insertData, info.RetailTotalPrice)
  1917. insertData = append(insertData, info.RegisterAccount)
  1918. insertData = append(insertData, info.BatchNumber)
  1919. insertData = append(insertData, info.Remark)
  1920. insertData = append(insertData, info.MaxUnit)
  1921. insertData = append(insertData, info.ProductDate)
  1922. insertData = append(insertData, info.ExpiryDate)
  1923. insertData = append(insertData, info.BatchNumberId)
  1924. }
  1925. thisSQL += strings.Join(insertParams, ", ")
  1926. err = utx.Exec(thisSQL, insertData...).Error
  1927. if err != nil {
  1928. utx.Rollback()
  1929. return
  1930. }
  1931. }
  1932. utx.Commit()
  1933. }
  1934. return
  1935. }
  1936. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  1937. db := readDb.Model(&models.CancelStock{})
  1938. db = db.Where("org_id = ? AND status = 1", orgId)
  1939. err = db.Order("ctime desc").Find(&list).Error
  1940. return
  1941. }
  1942. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  1943. db := readDb.Model(&models.WarehousingInfo{})
  1944. db = db.Where("org_id = ? AND status = 1", orgId)
  1945. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  1946. if startTime > 0 {
  1947. db = db.Where("ctime >=?", startTime)
  1948. }
  1949. if endTime > 0 {
  1950. db = db.Where("ctime<= ?", endTime)
  1951. }
  1952. db = db.Count(&total)
  1953. offset := (page - 1) * limit
  1954. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1955. return
  1956. }
  1957. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  1958. db := XTReadDB().Model(&models.StockInfo{})
  1959. if startime > 0 {
  1960. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1961. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  1962. })
  1963. }
  1964. if startime == 0 || endtime == 0 {
  1965. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1966. return db.Where("org_id = ? AND status = 1", orgId)
  1967. })
  1968. }
  1969. if endtime > 0 {
  1970. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1971. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  1972. })
  1973. }
  1974. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  1975. db = db.Group("xt_good_information.id")
  1976. if startime == 0 || endtime == 0 {
  1977. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1978. return db.Where("org_id = ? AND status = 1", orgId)
  1979. })
  1980. }
  1981. if startime > 0 {
  1982. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1983. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  1984. })
  1985. }
  1986. if endtime > 0 {
  1987. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1988. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  1989. })
  1990. }
  1991. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  1992. if startime == 0 || endtime == 0 {
  1993. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  1994. }
  1995. if startime > 0 {
  1996. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  1997. }
  1998. if endtime > 0 {
  1999. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2000. }
  2001. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2002. if len(keyword) > 0 {
  2003. likeKey := "%" + keyword + "%"
  2004. 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)
  2005. 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")
  2006. }
  2007. if type_name > 0 {
  2008. 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)
  2009. }
  2010. db = db.Count(&total)
  2011. offset := (page - 1) * limit
  2012. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2013. return
  2014. }
  2015. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2016. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2017. return
  2018. }
  2019. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2020. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2021. return
  2022. }
  2023. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2024. 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
  2025. return
  2026. }
  2027. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2028. 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
  2029. return
  2030. }
  2031. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2032. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2033. "good_id": warehouseInfo.GoodId,
  2034. "number": warehouseInfo.Number,
  2035. "product_date": warehouseInfo.ProductDate,
  2036. "expiry_date": warehouseInfo.ExpiryDate,
  2037. "warehousing_count": warehouseInfo.WarehousingCount,
  2038. "price": warehouseInfo.Price,
  2039. "total_price": warehouseInfo.TotalPrice,
  2040. "mtime": warehouseInfo.Mtime,
  2041. "remark": warehouseInfo.Remark,
  2042. }).Error
  2043. return
  2044. }
  2045. func UpDateWarehouseStatus(id int64) (err error) {
  2046. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2047. return
  2048. }
  2049. func UpdatedStockFlowById(id int64) (err error) {
  2050. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2051. return
  2052. }
  2053. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2054. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2055. return
  2056. }
  2057. func UpdateDrugFlowById(id int64) (err error) {
  2058. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2059. return
  2060. }
  2061. func DeleteWarehouse(ids []string) (err error) {
  2062. ut := writeDb.Begin()
  2063. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2064. if err != nil {
  2065. ut.Rollback()
  2066. return
  2067. }
  2068. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2069. if err != nil {
  2070. ut.Rollback()
  2071. return
  2072. }
  2073. ut.Commit()
  2074. return
  2075. }
  2076. func DeleteStockFlow(ids []string) (err error) {
  2077. ut := writeDb.Begin()
  2078. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2079. if err != nil {
  2080. ut.Rollback()
  2081. return
  2082. }
  2083. ut.Commit()
  2084. return
  2085. }
  2086. func DeleteDrugWarehouse(ids []string) (err error) {
  2087. ut := writeDb.Begin()
  2088. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2089. if err != nil {
  2090. ut.Rollback()
  2091. return
  2092. }
  2093. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2094. if err != nil {
  2095. ut.Rollback()
  2096. return
  2097. }
  2098. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2099. if err != nil {
  2100. ut.Rollback()
  2101. return
  2102. }
  2103. ut.Commit()
  2104. return
  2105. }
  2106. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2107. warehouse := &models.Warehousing{}
  2108. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2109. return warehouse, err
  2110. }
  2111. func FindWareHouseOutById(id int64) (*models.WarehouseOut, error) {
  2112. warehouseOut := &models.WarehouseOut{}
  2113. err := readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2114. return warehouseOut, err
  2115. }
  2116. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2117. warehouseOut := &models.DrugWarehouseOut{}
  2118. 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
  2119. return warehouseOut, err
  2120. }
  2121. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2122. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2123. return
  2124. }
  2125. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2126. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2127. return
  2128. }
  2129. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2130. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2131. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2132. return
  2133. }
  2134. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2135. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2136. return
  2137. }
  2138. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2139. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2140. if manufacturer_id > 0 {
  2141. db = db.Where("manufacturer = ?", manufacturer_id)
  2142. }
  2143. if dealer_id > 0 {
  2144. db = db.Where("dealer = ?", dealer_id)
  2145. }
  2146. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2147. return goodInfo, err
  2148. }
  2149. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2150. db := readDb.Model(&models.Warehousing{})
  2151. likekey := "%" + keywords + "%"
  2152. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2153. db = db.Where("warehousing_order LIKE ? ", likekey)
  2154. db = db.Count(&total)
  2155. offset := (page - 1) * limit
  2156. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2157. return
  2158. }
  2159. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2160. info := models.WarehousingInfo{}
  2161. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2162. return info, err
  2163. }
  2164. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2165. info := models.WarehousingInfo{}
  2166. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2167. return info, err
  2168. }
  2169. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2170. err = writeDb.Save(&info).Error
  2171. return err
  2172. }
  2173. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2174. err = writeDb.Save(&info).Error
  2175. return err
  2176. }
  2177. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2178. 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
  2179. return err
  2180. }
  2181. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2182. 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
  2183. return err
  2184. }
  2185. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2186. info := models.DrugWarehouseInfo{}
  2187. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2188. return info, err
  2189. }
  2190. func EditWarehousing(warehouse models.Warehousing) {
  2191. 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
  2192. }
  2193. func EditDrugWarehousing(warehouse models.DrugWarehouse) {
  2194. 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
  2195. }
  2196. func FindAllWarehouse(org_id int64, types int64) (list []*models.Warehousing, err error) {
  2197. 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
  2198. return list, err
  2199. }
  2200. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2201. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2202. 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)
  2203. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2204. return list, err
  2205. }
  2206. func DeleteSalesReturn(ids []string) (err error) {
  2207. ut := writeDb.Begin()
  2208. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2209. if err != nil {
  2210. ut.Rollback()
  2211. return
  2212. }
  2213. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2214. if err != nil {
  2215. ut.Rollback()
  2216. return
  2217. }
  2218. ut.Commit()
  2219. return
  2220. }
  2221. func DeleteDrugSalesReturn(ids []string) (err error) {
  2222. ut := writeDb.Begin()
  2223. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2224. if err != nil {
  2225. ut.Rollback()
  2226. return
  2227. }
  2228. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2229. if err != nil {
  2230. ut.Rollback()
  2231. return
  2232. }
  2233. ut.Commit()
  2234. return
  2235. }
  2236. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2237. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2238. return
  2239. }
  2240. type DrugSalesReturnInfo struct {
  2241. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2242. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2243. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2244. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2245. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2246. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2247. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2248. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2249. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2250. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2251. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2252. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2253. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2254. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2255. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2256. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2257. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2258. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2259. Number string `gorm:"column:number" json:"number" form:"number"`
  2260. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2261. }
  2262. func (DrugSalesReturnInfo) TableName() string {
  2263. return "xt_drug_sales_return_info"
  2264. }
  2265. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2266. 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
  2267. return
  2268. }
  2269. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2270. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2271. return
  2272. }
  2273. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2274. 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
  2275. return
  2276. }
  2277. func UpDateSaleReturnStatus(id int64) (err error) {
  2278. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2279. return
  2280. }
  2281. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2282. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2283. return
  2284. }
  2285. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2286. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2287. return salesReturn, err
  2288. }
  2289. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2290. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2291. return salesReturn, err
  2292. }
  2293. func EditSaleReturn(sales models.SalesReturn) {
  2294. 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
  2295. }
  2296. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2297. 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
  2298. }
  2299. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2300. err = writeDb.Save(&info).Error
  2301. return err
  2302. }
  2303. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2304. err = writeDb.Save(&info).Error
  2305. return err
  2306. }
  2307. func DeleteWarehouseOut(ids []string) (err error) {
  2308. ut := writeDb.Begin()
  2309. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2310. if err != nil {
  2311. ut.Rollback()
  2312. return
  2313. }
  2314. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2315. if err != nil {
  2316. ut.Rollback()
  2317. return
  2318. }
  2319. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2320. if err != nil {
  2321. ut.Rollback()
  2322. return
  2323. }
  2324. ut.Commit()
  2325. return
  2326. }
  2327. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2328. ut := writeDb.Begin()
  2329. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2330. if err != nil {
  2331. ut.Rollback()
  2332. return
  2333. }
  2334. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2335. if err != nil {
  2336. ut.Rollback()
  2337. return
  2338. }
  2339. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2340. if err != nil {
  2341. ut.Rollback()
  2342. return
  2343. }
  2344. ut.Commit()
  2345. return
  2346. }
  2347. func UpDateWarehouseOutStatus(id int64) (err error) {
  2348. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2349. return
  2350. }
  2351. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2352. ut := writeDb.Begin()
  2353. err = ut.Save(&info).Error
  2354. if err != nil {
  2355. ut.Rollback()
  2356. return err
  2357. }
  2358. ut.Commit()
  2359. return err
  2360. }
  2361. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2362. err = tx.Save(&info).Error
  2363. if err != nil {
  2364. tx.Rollback()
  2365. return err
  2366. }
  2367. return err
  2368. }
  2369. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2370. ut := writeDb.Begin()
  2371. err = ut.Save(&info).Error
  2372. if err != nil {
  2373. ut.Rollback()
  2374. return err
  2375. }
  2376. ut.Commit()
  2377. return err
  2378. }
  2379. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2380. err = tx.Save(&info).Error
  2381. if err != nil {
  2382. tx.Rollback()
  2383. return err
  2384. }
  2385. return err
  2386. }
  2387. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2388. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2389. return
  2390. }
  2391. func EditWarehouseOut(warehouseOut models.WarehouseOut) {
  2392. 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
  2393. }
  2394. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2395. 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
  2396. }
  2397. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo) (err error) {
  2398. err = writeDb.Save(&info).Error
  2399. return err
  2400. }
  2401. 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) {
  2402. db := readDb.Model(&models.CancelStock{})
  2403. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2404. if len(keywords) > 0 {
  2405. likeKey := "%" + keywords + "%"
  2406. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2407. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2408. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2409. }
  2410. if startTime > 0 {
  2411. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2412. }
  2413. if endTime > 0 {
  2414. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2415. }
  2416. if storehouse_id > 0 {
  2417. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2418. }
  2419. if check_type > 0 {
  2420. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2421. }
  2422. db = db.Count(&total)
  2423. offset := (page - 1) * limit
  2424. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2425. return
  2426. }
  2427. 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) {
  2428. db := readDb.Model(&models.DrugCancelStock{})
  2429. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2430. if len(keywords) > 0 {
  2431. likeKey := "%" + keywords + "%"
  2432. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2433. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2434. 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")
  2435. }
  2436. if startTime > 0 {
  2437. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2438. }
  2439. if endTime > 0 {
  2440. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2441. }
  2442. if storehoue_id > 0 {
  2443. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2444. }
  2445. if check_type > 0 {
  2446. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2447. }
  2448. db = db.Count(&total)
  2449. offset := (page - 1) * limit
  2450. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2451. return
  2452. }
  2453. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2454. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2455. return info, err
  2456. }
  2457. func DeleteCancelStock(ids []string) (err error) {
  2458. ut := writeDb.Begin()
  2459. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2460. if err != nil {
  2461. ut.Rollback()
  2462. return
  2463. }
  2464. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2465. if err != nil {
  2466. ut.Rollback()
  2467. return
  2468. }
  2469. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2470. if err != nil {
  2471. ut.Rollback()
  2472. return
  2473. }
  2474. ut.Commit()
  2475. return
  2476. }
  2477. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2478. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2479. return info, err
  2480. }
  2481. func DeleteDrugCancelStock(ids []string) (err error) {
  2482. ut := writeDb.Begin()
  2483. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2484. if err != nil {
  2485. ut.Rollback()
  2486. return
  2487. }
  2488. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2489. if err != nil {
  2490. ut.Rollback()
  2491. return
  2492. }
  2493. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2494. if err != nil {
  2495. ut.Rollback()
  2496. return
  2497. }
  2498. ut.Commit()
  2499. return
  2500. }
  2501. func UpDateCancleStockStatus(id int64) (err error) {
  2502. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2503. return
  2504. }
  2505. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2506. info := models.CancelStockInfo{}
  2507. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2508. return info, err
  2509. }
  2510. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2511. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2512. return err
  2513. }
  2514. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2515. 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
  2516. return
  2517. }
  2518. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2519. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2520. return
  2521. }
  2522. func UpDateDrugCancelStockById(id int64) (err error) {
  2523. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2524. return
  2525. }
  2526. func EditCancelStock(sales models.CancelStock) {
  2527. 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
  2528. }
  2529. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2530. 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
  2531. }
  2532. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2533. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2534. return cancelStock, err
  2535. }
  2536. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2537. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2538. return cancelStock, err
  2539. }
  2540. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2541. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2542. return cancelStock, err
  2543. }
  2544. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2545. err = writeDb.Save(&info).Error
  2546. return err
  2547. }
  2548. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2549. err = writeDb.Save(&info).Error
  2550. return err
  2551. }
  2552. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2553. 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
  2554. return list, err
  2555. }
  2556. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2557. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2558. 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)
  2559. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2560. return list, err
  2561. }
  2562. type StockDetailModel struct {
  2563. GoodId int64 `gorm:"good_id" json:"good_id"`
  2564. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2565. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2566. Price float64 `gorm:"price" json:"price"`
  2567. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2568. Type int `gorm:"type" json:"type"`
  2569. Creater int64 `gorm:"creater" json:"creater"`
  2570. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2571. Types int64 `gorm:"types" json:"types"`
  2572. }
  2573. 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) {
  2574. db := readDb.Model(&models.WarehousingInfo{})
  2575. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2576. if len(keywords) > 0 {
  2577. likeKey := "%" + keywords + "%"
  2578. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2579. 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)
  2580. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2581. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2582. 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")
  2583. } else {
  2584. 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)
  2585. }
  2586. if manufacturer > 0 {
  2587. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2588. }
  2589. if storehouse_id > 0 {
  2590. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2591. }
  2592. if order_type > 0 {
  2593. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2594. }
  2595. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2596. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2597. db = db.Count(&total)
  2598. offset := (page - 1) * limit
  2599. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2600. return
  2601. }
  2602. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2603. db := readDb.Model(&models.WarehousingInfo{})
  2604. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2605. if len(keywords) > 0 {
  2606. likeKey := "%" + keywords + "%"
  2607. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2608. 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)
  2609. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2610. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2611. 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")
  2612. } else {
  2613. 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)
  2614. }
  2615. if manufacturer > 0 {
  2616. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2617. }
  2618. if order_type > 0 {
  2619. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2620. }
  2621. if storehouse_id > 0 {
  2622. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2623. }
  2624. type PriceAmount struct {
  2625. Total float64
  2626. }
  2627. var result []PriceAmount
  2628. err = db.Select("total_price as total").Scan(&result).Error
  2629. var total float64
  2630. for _, value := range result {
  2631. total = total + value.Total
  2632. }
  2633. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2634. return err, total
  2635. }
  2636. func FloatRound(f float64, n int) float64 {
  2637. format := "%." + strconv.Itoa(n) + "f"
  2638. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  2639. return res
  2640. }
  2641. 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) {
  2642. if orgId == 9671 {
  2643. db := readDb.Model(&models.WarehouseOutInfo{})
  2644. 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)
  2645. if len(keywords) > 0 {
  2646. likeKey := "%" + keywords + "%"
  2647. 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)
  2648. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2649. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2650. 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")
  2651. } else {
  2652. 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)
  2653. }
  2654. if manufacturer > 0 {
  2655. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2656. }
  2657. if order_type > 0 {
  2658. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2659. }
  2660. if storehouse_id > 0 {
  2661. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2662. }
  2663. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2664. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2665. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2666. db = db.Count(&total)
  2667. offset := (page - 1) * limit
  2668. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2669. return
  2670. } else {
  2671. db := readDb.Model(&models.WarehouseOutInfo{})
  2672. 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)
  2673. if len(keywords) > 0 {
  2674. likeKey := "%" + keywords + "%"
  2675. 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)
  2676. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2677. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2678. 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")
  2679. } else {
  2680. 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)
  2681. }
  2682. if manufacturer > 0 {
  2683. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2684. }
  2685. if order_type > 0 {
  2686. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2687. }
  2688. if storehouse_id > 0 {
  2689. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2690. }
  2691. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2692. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2693. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2694. db = db.Count(&total)
  2695. offset := (page - 1) * limit
  2696. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2697. return
  2698. }
  2699. }
  2700. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2701. db := readDb.Model(&models.WarehouseOutInfo{})
  2702. 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)
  2703. if len(keywords) > 0 {
  2704. likeKey := "%" + keywords + "%"
  2705. 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)
  2706. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2707. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2708. 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")
  2709. } else {
  2710. 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)
  2711. }
  2712. if manufacturer > 0 {
  2713. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2714. }
  2715. if order_type > 0 {
  2716. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2717. }
  2718. if storehouse_id > 0 {
  2719. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2720. }
  2721. type PriceAmount struct {
  2722. Total float64
  2723. }
  2724. var result []PriceAmount
  2725. err = db.Select("(count * price) as total").Scan(&result).Error
  2726. var total float64
  2727. for _, value := range result {
  2728. total = total + value.Total
  2729. }
  2730. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2731. fmt.Println(total)
  2732. return err, total
  2733. }
  2734. 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) {
  2735. db := readDb.Model(&models.SalesReturnInfo{})
  2736. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  2737. if len(keywords) > 0 {
  2738. likeKey := "%" + keywords + "%"
  2739. 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)
  2740. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  2741. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  2742. 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")
  2743. } else {
  2744. 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)
  2745. }
  2746. if manufacturer > 0 {
  2747. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  2748. }
  2749. if order_type > 0 {
  2750. db = db.Where("xt_sales_return_info.type = ?", order_type)
  2751. }
  2752. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  2753. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2754. offset := (page - 1) * limit
  2755. db = db.Count(&total)
  2756. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  2757. return
  2758. }
  2759. 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) {
  2760. db := readDb.Model(&models.CancelStockInfo{})
  2761. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2762. if len(keywords) > 0 {
  2763. likeKey := "%" + keywords + "%"
  2764. 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)
  2765. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2766. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2767. 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")
  2768. } else {
  2769. 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)
  2770. }
  2771. if manufacturer > 0 {
  2772. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2773. }
  2774. if order_type > 0 {
  2775. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2776. }
  2777. if storehouse_id > 0 {
  2778. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  2779. }
  2780. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2781. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2782. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  2783. return
  2784. }
  2785. 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) {
  2786. db := readDb.Model(&models.CancelStockInfo{})
  2787. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2788. if len(keywords) > 0 {
  2789. likeKey := "%" + keywords + "%"
  2790. 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)
  2791. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2792. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2793. 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")
  2794. } else {
  2795. 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)
  2796. }
  2797. if manufacturer > 0 {
  2798. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2799. }
  2800. if order_type > 0 {
  2801. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2802. }
  2803. if storehouse_id > 0 {
  2804. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  2805. }
  2806. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2807. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2808. db = db.Count(&total)
  2809. offset := (page - 1) * limit
  2810. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  2811. return
  2812. }
  2813. func FindStockInByDealerId(id int64) (total int64, err error) {
  2814. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  2815. return
  2816. }
  2817. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  2818. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  2819. return
  2820. }
  2821. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  2822. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  2823. return
  2824. }
  2825. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  2826. 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)
  2827. return
  2828. }
  2829. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  2830. 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)
  2831. return
  2832. }
  2833. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  2834. 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)
  2835. return
  2836. }
  2837. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  2838. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  2839. return goodType, err
  2840. }
  2841. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  2842. 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
  2843. return info, err
  2844. }
  2845. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  2846. 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
  2847. return
  2848. }
  2849. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  2850. 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
  2851. return
  2852. }
  2853. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2854. 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
  2855. return
  2856. }
  2857. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  2858. 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
  2859. if err != gorm.ErrRecordNotFound {
  2860. if err != nil {
  2861. tx.Rollback()
  2862. return
  2863. }
  2864. }
  2865. return
  2866. }
  2867. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2868. 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
  2869. return
  2870. }
  2871. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  2872. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  2873. return
  2874. }
  2875. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  2876. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  2877. return
  2878. }
  2879. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2880. info := models.WarehouseOutInfo{}
  2881. 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
  2882. if err == gorm.ErrRecordNotFound {
  2883. return nil, err
  2884. }
  2885. if err != nil {
  2886. return nil, err
  2887. }
  2888. return &info, nil
  2889. }
  2890. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2891. info := models.WarehouseOutInfo{}
  2892. 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
  2893. if err == gorm.ErrRecordNotFound {
  2894. return nil, err
  2895. }
  2896. if err != nil {
  2897. return nil, err
  2898. }
  2899. return &info, nil
  2900. }
  2901. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2902. 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
  2903. return err
  2904. }
  2905. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2906. 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
  2907. return err
  2908. }
  2909. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo) error {
  2910. err := writeDb.Create(&info).Error
  2911. return err
  2912. }
  2913. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  2914. err := tx.Create(&info).Error
  2915. if err != nil {
  2916. tx.Rollback()
  2917. return err
  2918. }
  2919. return err
  2920. }
  2921. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  2922. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  2923. return err
  2924. }
  2925. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  2926. err := writeDb.Create(&info).Error
  2927. return err
  2928. }
  2929. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  2930. err := tx.Create(&info).Error
  2931. if err != nil {
  2932. tx.Rollback()
  2933. return err
  2934. }
  2935. return err
  2936. }
  2937. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  2938. ut := writeDb.Begin()
  2939. err := ut.Create(&detail).Error
  2940. if err != nil {
  2941. ut.Rollback()
  2942. return err
  2943. }
  2944. ut.Commit()
  2945. return err
  2946. }
  2947. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  2948. err := writeDb.Create(&detail).Error
  2949. return err
  2950. }
  2951. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  2952. err := tx.Create(&detail).Error
  2953. if err != nil {
  2954. tx.Rollback()
  2955. return err
  2956. }
  2957. return err
  2958. }
  2959. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  2960. 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
  2961. return
  2962. }
  2963. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  2964. 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
  2965. return
  2966. }
  2967. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  2968. 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
  2969. return
  2970. }
  2971. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  2972. info := models.WarehouseOutInfo{}
  2973. 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
  2974. return info, err
  2975. }
  2976. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  2977. 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
  2978. return
  2979. }
  2980. func UpdateStockOutInfoCount(org_id int64, id int64) {
  2981. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  2982. }
  2983. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  2984. db := readDb.Model(&models.AutomaticReduceDetail{})
  2985. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  2986. db = db.Preload("GoodsType", "status = 1")
  2987. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  2988. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  2989. db = db.Count(&total)
  2990. err = db.Find(&user).Error
  2991. return
  2992. }
  2993. type DrugAutomaticReduceDetail struct {
  2994. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2995. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  2996. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  2997. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  2998. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2999. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3000. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3001. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3002. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3003. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3004. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3005. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3006. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3007. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3008. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3009. }
  3010. func (DrugAutomaticReduceDetail) TableName() string {
  3011. return "xt_drug_automatic_reduce_detail"
  3012. }
  3013. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3014. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3015. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3016. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3017. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3018. db = db.Count(&total)
  3019. err = db.Find(&user).Error
  3020. return
  3021. }
  3022. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3023. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3024. }
  3025. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3026. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3027. }
  3028. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3029. 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
  3030. return
  3031. }
  3032. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3033. 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
  3034. return
  3035. }
  3036. type VMWarehousingInfo struct {
  3037. ID int64 `gorm:"column:id" json:"id"`
  3038. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3039. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3040. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3041. Status int64 `gorm:"column:status" json:"status"`
  3042. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3043. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3044. }
  3045. func (VMWarehousingInfo) TableName() string {
  3046. return "xt_warehouse_info"
  3047. }
  3048. type GoodInfo struct {
  3049. ID int64 `gorm:"column:id" json:"id"`
  3050. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3051. Status int64 `gorm:"column:status" json:"status"`
  3052. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3053. }
  3054. func (GoodInfo) TableName() string {
  3055. return "xt_good_information"
  3056. }
  3057. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3058. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3059. return
  3060. }
  3061. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3062. 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
  3063. return
  3064. }
  3065. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3066. likeKey := "%" + keyword + "%"
  3067. db := XTReadDB().Model(&goods)
  3068. if len(keyword) > 0 {
  3069. db = db.Where("type_name like ?", likeKey)
  3070. }
  3071. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3072. return goods, err
  3073. }
  3074. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3075. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3076. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3077. return goodstype, err
  3078. }
  3079. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3080. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3081. if orgid > 0 {
  3082. db = db.Where("x.user_org_id = ?", orgid)
  3083. }
  3084. if recordtime > 0 {
  3085. db = db.Where("x.record_date = ?", recordtime)
  3086. }
  3087. if patientId > 0 {
  3088. db = db.Where("x.patient_id = ?", patientId)
  3089. }
  3090. 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
  3091. return prepare, err
  3092. }
  3093. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3094. detail := models.XtAutomaticReduceDetail{}
  3095. 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
  3096. if err == gorm.ErrRecordNotFound {
  3097. return nil, err
  3098. }
  3099. if err != nil {
  3100. return nil, err
  3101. }
  3102. return &detail, nil
  3103. }
  3104. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3105. 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
  3106. return autoMatic, err
  3107. }
  3108. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3109. 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
  3110. return autoMatic, err
  3111. }
  3112. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3113. var manufacturer models.Manufacturer
  3114. var err error
  3115. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3116. if err == gorm.ErrRecordNotFound {
  3117. return nil, err
  3118. }
  3119. if err != nil {
  3120. return nil, err
  3121. }
  3122. return &manufacturer, nil
  3123. }
  3124. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3125. err := XTWriteDB().Create(manufacturer).Error
  3126. return err
  3127. }
  3128. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3129. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3130. return err
  3131. }
  3132. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3133. var good models.GoodInfo
  3134. var err error
  3135. 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
  3136. if err == gorm.ErrRecordNotFound {
  3137. return nil, err
  3138. }
  3139. if err != nil {
  3140. return nil, err
  3141. }
  3142. return &good, nil
  3143. }
  3144. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3145. err := XTWriteDB().Create(&info).Error
  3146. return err
  3147. }
  3148. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3149. err := XTWriteDB().Model(&info).Where("good_name = ? and good_type_id = ? and specification_name = ? and org_id = ? and status = 1", goodName, goodType, specificationName, orgid).Updates(map[string]interface{}{"good_name": info.GoodName, "good_type_id": info.GoodTypeId, "medical_insurance_level": info.MedicalInsuranceLevel, "good_kind": info.GoodKind, "specification_name": info.SpecificationName, "manufacturer": info.Manufacturer, "good_unit": info.GoodUnit, "retail_price": info.RetailPrice, "stock_warn_count": info.StockWarnCount, "dealer": info.Dealer, "pinyin": info.Pinyin, "wubi": info.Wubi, "buy_price": info.BuyPrice, "medical_insurance_number": info.MedicalInsuranceNumber, "is_special_diseases": info.IsSpecialDiseases, "is_record": info.IsRecord, "statistics_category": info.StatisticsCategory, "good_status": info.GoodStatus, "social_security_directory_code": info.SocialSecurityDirectoryCode, "production_type": info.ProductionType, "special_medical": info.SpecialMedical, "remark": info.Remark}).Error
  3150. return err
  3151. }
  3152. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3153. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3154. 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
  3155. return houseOutInfo, err
  3156. }
  3157. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3158. key := "%" + keyword + "%"
  3159. if len(keyword) == 0 {
  3160. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3161. } else {
  3162. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3163. }
  3164. return goodInfo, err
  3165. }
  3166. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3167. var drug models.BaseDrugLib
  3168. var err error
  3169. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3170. if err == gorm.ErrRecordNotFound {
  3171. return nil, err
  3172. }
  3173. if err != nil {
  3174. return nil, err
  3175. }
  3176. return &drug, nil
  3177. }
  3178. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3179. var drug models.BaseDrugLib
  3180. var err error
  3181. 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
  3182. if err == gorm.ErrRecordNotFound {
  3183. return nil, err
  3184. }
  3185. if err != nil {
  3186. return nil, err
  3187. }
  3188. return &drug, nil
  3189. }
  3190. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3191. err := XTWriteDB().Create(&info).Error
  3192. return err
  3193. }
  3194. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3195. err := XTWriteDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and org_id = ? and status = 1", drugName, drugSpec, orgid).Updates(map[string]interface{}{"drug_name": drug.DrugName, "drug_alias": drug.DrugAlias, "drug_spec": drug.DrugSpec, "drug_type": drug.DrugType, "drug_stock_limit": drug.DrugStockLimit, "drug_origin_place": drug.DrugOriginPlace, "drug_dosage_form": drug.DrugDosageForm, "medical_insurance_level": drug.MedicalInsuranceLevel, "max_unit": drug.MaxUnit, "min_unit": drug.MinUnit, "unit_matrixing": drug.UnitMatrixing, "retail_price": drug.RetailPrice, "last_price": drug.LastPrice, "drug_classify": drug.DrugClassify, "manufacturer": drug.Manufacturer, "dealer": drug.Dealer, "pinyin": drug.Pinyin, "wubi": drug.Wubi, "drug_alias_pinyin": drug.DrugAliasPinyin, "drug_alias_wubi": drug.DrugAliasWubi, "drug_category": drug.DrugCategory, "drug_control": drug.DrugControl, "number": drug.Number, "hosp_appr_flag": drug.HospApprFlag, "medical_insurance_number": drug.MedicalInsuranceNumber, "pharmacology_category": drug.PharmacologyCategory, "statistics_category": drug.StatisticsCategory, "code": drug.Code, "is_special_diseases": drug.IsSpecialDiseases, "is_record": drug.IsRecord, "prescription_mark": drug.PrescriptionMark, "social_security_directory_code": drug.SocialSecurityDirectoryCode, "record_date": drug.RecordDate, "drug_remark": drug.DrugRemark, "drug_status": drug.DrugStatus, "limit_remark": drug.LimitRemark, "drug_dose": drug.DrugDose, "drug_dose_unit": drug.DrugDoseUnit, "lmt_used_flag": drug.LmtUsedFlag, "delivery_way": drug.DeliveryWay, "execution_frequency": drug.ExecutionFrequency, "drug_day": drug.DrugDay}).Error
  3196. return err
  3197. }
  3198. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3199. var projoect models.HisProject
  3200. var err error
  3201. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3202. if err == gorm.ErrRecordNotFound {
  3203. return nil, err
  3204. }
  3205. if err != nil {
  3206. return nil, err
  3207. }
  3208. return &projoect, nil
  3209. }
  3210. func CreateProjectInfomation(info *models.HisProject) error {
  3211. err := XTWriteDB().Create(&info).Error
  3212. return err
  3213. }
  3214. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3215. 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
  3216. return err
  3217. }
  3218. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3219. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3220. return warehouseinfo, err
  3221. }
  3222. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3223. 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
  3224. return err
  3225. }
  3226. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3227. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3228. return list, err
  3229. }
  3230. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3231. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3232. return list, err
  3233. }
  3234. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3235. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3236. return list, err
  3237. }
  3238. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3239. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3240. // return list, err
  3241. // }
  3242. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3243. var dealer models.Dealer
  3244. var err error
  3245. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3246. if err == gorm.ErrRecordNotFound {
  3247. return nil, err
  3248. }
  3249. if err != nil {
  3250. return nil, err
  3251. }
  3252. return &dealer, nil
  3253. }
  3254. func CreateDealer(dealer *models.Dealer) error {
  3255. err := XTWriteDB().Create(&dealer).Error
  3256. return err
  3257. }
  3258. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3259. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3260. return err
  3261. }
  3262. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3263. dataconfig := models.DictDataconfig{}
  3264. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3265. return dataconfig, err
  3266. }
  3267. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3268. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3269. return dataconfig, err
  3270. }
  3271. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3272. var dataconfig models.DictDataconfig
  3273. var err error
  3274. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3275. if err == gorm.ErrRecordNotFound {
  3276. return nil, err
  3277. }
  3278. if err != nil {
  3279. return nil, err
  3280. }
  3281. return &dataconfig, nil
  3282. }
  3283. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3284. dataconfig := models.DictDataconfig{}
  3285. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3286. return dataconfig, err
  3287. }
  3288. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3289. err := XTWriteDB().Create(&dataconfig).Error
  3290. return err
  3291. }
  3292. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3293. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3294. return err
  3295. }
  3296. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3297. var goodType models.GoodsType
  3298. var err error
  3299. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3300. if err == gorm.ErrRecordNotFound {
  3301. return nil, err
  3302. }
  3303. if err != nil {
  3304. return nil, err
  3305. }
  3306. return &goodType, nil
  3307. }
  3308. func CreatedGoodType(goodsType *models.GoodsType) error {
  3309. err := XTWriteDB().Create(&goodsType).Error
  3310. return err
  3311. }
  3312. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3313. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3314. return goodsType, err
  3315. }
  3316. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3317. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3318. return goodsType, err
  3319. }
  3320. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3321. var department models.XtHisDepartment
  3322. var err error
  3323. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3324. if err == gorm.ErrRecordNotFound {
  3325. return nil, err
  3326. }
  3327. if err != nil {
  3328. return nil, err
  3329. }
  3330. return &department, nil
  3331. }
  3332. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3333. dataconfig := models.Dataconfig{}
  3334. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3335. return dataconfig, err
  3336. }
  3337. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3338. var dataconfig models.Dataconfig
  3339. var err error
  3340. 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
  3341. if err == gorm.ErrRecordNotFound {
  3342. return nil, err
  3343. }
  3344. if err != nil {
  3345. return nil, err
  3346. }
  3347. return &dataconfig, nil
  3348. }
  3349. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3350. dataconfig := models.Dataconfig{}
  3351. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3352. return dataconfig, err
  3353. }
  3354. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3355. err := XTWriteDB().Create(&dataconfig).Error
  3356. return err
  3357. }
  3358. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3359. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3360. return dataconfig, err
  3361. }
  3362. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3363. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3364. return list, err
  3365. }
  3366. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3367. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3368. if len(keywords) > 0 {
  3369. likekey := "%" + keywords + "%"
  3370. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3371. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3372. }).Find(&drug).Error
  3373. } else {
  3374. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3375. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3376. }).Find(&drug).Error
  3377. }
  3378. return
  3379. }
  3380. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3381. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3382. if len(keywords) > 0 {
  3383. likekey := "%" + keywords + "%"
  3384. 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 {
  3385. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3386. }).Find(&drug).Error
  3387. } else {
  3388. 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 {
  3389. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3390. }).Find(&drug).Error
  3391. }
  3392. return
  3393. }
  3394. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3395. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3396. if len(keywords) > 0 {
  3397. likekey := "%" + keywords + "%"
  3398. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3399. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3400. }).Find(&good).Error
  3401. } else {
  3402. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3403. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3404. }).Find(&good).Error
  3405. }
  3406. return
  3407. }
  3408. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3409. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3410. if len(keywords) > 0 {
  3411. likekey := "%" + keywords + "%"
  3412. 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 {
  3413. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3414. }).Find(&good).Error
  3415. } else {
  3416. 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 {
  3417. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3418. }).Find(&good).Error
  3419. }
  3420. return
  3421. }
  3422. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3423. db := writeDb.Begin()
  3424. err := db.Create(&detail).Error
  3425. if err != nil {
  3426. db.Rollback()
  3427. return err
  3428. }
  3429. db.Commit()
  3430. return err
  3431. }
  3432. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3433. var autoreduece models.BloodAutomaticReduceDetail
  3434. var err error
  3435. 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
  3436. if err == gorm.ErrRecordNotFound {
  3437. return nil, err
  3438. }
  3439. if err != nil {
  3440. return nil, err
  3441. }
  3442. return &autoreduece, nil
  3443. }
  3444. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3445. auto := models.BloodAutomaticReduceDetail{}
  3446. 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
  3447. return auto, err
  3448. }
  3449. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3450. detail := models.BloodAutomaticReduceDetail{}
  3451. 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
  3452. return err
  3453. }
  3454. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3455. 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
  3456. return stockflow, err
  3457. }
  3458. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3459. 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
  3460. 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
  3461. 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
  3462. return err
  3463. }
  3464. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3465. detail := models.BloodAutomaticReduceDetail{}
  3466. 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
  3467. info := models.WarehouseOutInfo{}
  3468. 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
  3469. prepare := models.DialysisBeforePrepare{}
  3470. 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
  3471. flow := models.VmStockFlow{}
  3472. 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
  3473. return err
  3474. }
  3475. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3476. detail := models.BloodAutomaticReduceDetail{}
  3477. 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
  3478. info := models.WarehouseOutInfo{}
  3479. 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
  3480. prepare := models.DialysisBeforePrepare{}
  3481. 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
  3482. flow := models.VmStockFlow{}
  3483. 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
  3484. return err
  3485. }
  3486. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  3487. detail := models.BloodAutomaticReduceDetail{}
  3488. 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
  3489. info := models.WarehouseOutInfo{}
  3490. 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
  3491. prepare := models.DialysisBeforePrepare{}
  3492. 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
  3493. flow := models.VmStockFlow{}
  3494. 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
  3495. return err
  3496. }
  3497. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  3498. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3499. //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
  3500. return err
  3501. }
  3502. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3503. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  3504. return info, err
  3505. }
  3506. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  3507. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  3508. return list, err
  3509. }
  3510. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  3511. 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
  3512. return err
  3513. }
  3514. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  3515. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  3516. return err
  3517. }
  3518. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  3519. var autoreduece models.BloodAutomaticReduceDetail
  3520. var err error
  3521. 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
  3522. if err == gorm.ErrRecordNotFound {
  3523. return nil, err
  3524. }
  3525. if err != nil {
  3526. return nil, err
  3527. }
  3528. return &autoreduece, nil
  3529. }
  3530. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  3531. detail := models.WarehouseOutInfoSeven{}
  3532. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3533. return detail, err
  3534. }
  3535. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  3536. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3537. return detail, err
  3538. }
  3539. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  3540. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  3541. return project, err
  3542. }
  3543. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  3544. project := models.HisPrescriptionProject{}
  3545. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  3546. return project, err
  3547. }
  3548. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  3549. 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
  3550. return auto, err
  3551. }
  3552. func ModefyWarehouseInfo(count int64, id int64) error {
  3553. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3554. return err
  3555. }
  3556. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  3557. 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
  3558. 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
  3559. 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
  3560. //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
  3561. return err
  3562. }
  3563. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  3564. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3565. return
  3566. }
  3567. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  3568. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3569. return
  3570. }
  3571. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  3572. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3573. return info, err
  3574. }
  3575. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3576. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3577. return info, err
  3578. }
  3579. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3580. 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
  3581. return info, err
  3582. }
  3583. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3584. offset := (page - 1) * limit
  3585. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  3586. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3587. fmt.Println(table)
  3588. if startime > 0 {
  3589. db = db.Where("x.ctime>=?", startime)
  3590. }
  3591. if endtime > 0 {
  3592. db = db.Where("x.ctime<=?", endtime)
  3593. }
  3594. likeKey := "%" + keyword + "%"
  3595. if len(likeKey) > 0 {
  3596. db = db.Where("t.good_name like ?", likeKey)
  3597. }
  3598. if orgid > 0 {
  3599. db = db.Where("x.org_id = ?", orgid)
  3600. }
  3601. if good_type > 0 {
  3602. db = db.Where("t.good_type_id = ?", good_type)
  3603. }
  3604. 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
  3605. return info, total, err
  3606. }
  3607. 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) {
  3608. offset := (page - 1) * limit
  3609. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  3610. likeKey := "%" + keyword + "%"
  3611. if len(likeKey) > 0 {
  3612. db = db.Where("x.good_name like ?", likeKey)
  3613. }
  3614. if orgid > 0 {
  3615. db = db.Where("x.org_id = ?", orgid)
  3616. }
  3617. if good_type > 0 {
  3618. db = db.Where("x.good_type_id = ?", good_type)
  3619. }
  3620. if good_id > 0 {
  3621. db = db.Where("x.id = ?", good_id)
  3622. }
  3623. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3624. if startime > 0 {
  3625. db = db.Where("ctime>=?", startime)
  3626. }
  3627. if endtime > 0 {
  3628. db = db.Where("ctime<=?", endtime)
  3629. }
  3630. if good_id > 0 {
  3631. db = db.Where("good_id = ?", good_id)
  3632. }
  3633. if storehouse_id > 0 {
  3634. db = db.Where("storehouse_id = ?", storehouse_id)
  3635. }
  3636. return db.Where("status = 1 and is_check = 1")
  3637. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3638. if startime > 0 {
  3639. db = db.Where("ctime>=?", startime)
  3640. }
  3641. if endtime > 0 {
  3642. db = db.Where("ctime<=?", endtime)
  3643. }
  3644. if good_id > 0 {
  3645. db = db.Where("good_id = ?", good_id)
  3646. }
  3647. if storehouse_id > 0 {
  3648. db = db.Where("storehouse_id = ?", storehouse_id)
  3649. }
  3650. return db.Where("status = 1").Group("good_id,storehouse_id")
  3651. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3652. if startime > 0 {
  3653. db = db.Where("ctime>=?", startime)
  3654. }
  3655. if endtime > 0 {
  3656. db = db.Where("ctime<=?", endtime)
  3657. }
  3658. return db.Where("status = 1")
  3659. }).Find(&info).Error
  3660. return info, total, err
  3661. }
  3662. 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) {
  3663. offset := (page - 1) * limit
  3664. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  3665. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3666. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  3667. fmt.Println(table, tables)
  3668. if startime > 0 {
  3669. db = db.Where("x.ctime>=?", startime)
  3670. }
  3671. if endtime > 0 {
  3672. db = db.Where("x.ctime<=?", endtime)
  3673. }
  3674. if startfirsttime > 0 {
  3675. db = db.Where("x.expiry_date >=?", startfirsttime)
  3676. }
  3677. if endfirsttime > 0 {
  3678. db = db.Where("x.expiry_date <=?", endfirsttime)
  3679. }
  3680. if orgid > 0 {
  3681. db = db.Where("x.org_id = ?", orgid)
  3682. }
  3683. if good_id > 0 {
  3684. db = db.Where("x.good_id = ?", good_id)
  3685. }
  3686. 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
  3687. return info, total, err
  3688. }
  3689. 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) {
  3690. offset := (page - 1) * limit
  3691. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  3692. if startime > 0 {
  3693. db = db.Where("x.ctime>=?", startime)
  3694. }
  3695. if endtime > 0 {
  3696. db = db.Where("x.ctime<=?", endtime)
  3697. }
  3698. if orgid > 0 {
  3699. db = db.Where("x.org_id = ?", orgid)
  3700. }
  3701. if good_id > 0 {
  3702. db = db.Where("x.good_id = ?", good_id)
  3703. }
  3704. if is_sys == 2 {
  3705. db = db.Where("x.is_sys = 1")
  3706. }
  3707. if is_sys == 3 {
  3708. db = db.Where("x.is_sys = 0")
  3709. }
  3710. 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
  3711. return info, total, err
  3712. }
  3713. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3714. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3715. if startime > 0 {
  3716. db = db.Where("x.ctime >=?", startime)
  3717. }
  3718. if endtime > 0 {
  3719. db = db.Where("x.ctime<=?", endtime)
  3720. }
  3721. if orgid > 0 {
  3722. db = db.Where("x.org_id = ?", orgid)
  3723. }
  3724. 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
  3725. return info, err
  3726. }
  3727. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3728. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3729. if startime > 0 {
  3730. db = db.Where("x.ctime >=?", startime)
  3731. }
  3732. if endtime > 0 {
  3733. db = db.Where("x.ctime<=?", endtime)
  3734. }
  3735. if orgid > 0 {
  3736. db = db.Where("x.org_id = ?", orgid)
  3737. }
  3738. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  3739. return info, err
  3740. }
  3741. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3742. if endtime > 0 {
  3743. 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
  3744. }
  3745. if endtime == 0 {
  3746. 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
  3747. }
  3748. return autoMatic, err
  3749. }
  3750. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  3751. out := models.WarehouseOut{}
  3752. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  3753. return out, err
  3754. }
  3755. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3756. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3757. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3758. fmt.Println(table)
  3759. if id > 0 {
  3760. db = db.Where("x.warehouse_out_id = ?", id)
  3761. }
  3762. if orgid > 0 {
  3763. db = db.Where("x.org_id = ?", orgid)
  3764. }
  3765. 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
  3766. return out, err
  3767. }
  3768. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  3769. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  3770. if len(id) > 0 {
  3771. db = db.Where("x.id in(?)", id)
  3772. }
  3773. if orgid > 0 {
  3774. db = db.Where("x.org_id= ?", orgid)
  3775. }
  3776. 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
  3777. return out, err
  3778. }
  3779. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3780. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3781. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3782. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  3783. fmt.Println(table, tableOne)
  3784. if len(id) > 0 {
  3785. db = db.Where("x.warehouse_out_id in (?)", id)
  3786. }
  3787. if orgid > 0 {
  3788. db = db.Where("x.org_id = ?", orgid)
  3789. }
  3790. 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").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
  3791. return out, err
  3792. }
  3793. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3794. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3795. if len(id) > 0 {
  3796. db = db.Where("warehouse_out_id in (?)", id)
  3797. }
  3798. if orgid > 0 {
  3799. db = db.Where("user_org_id = ?", orgid)
  3800. }
  3801. err = db.Group("warehousing_id").Find(&list).Error
  3802. return list, err
  3803. }
  3804. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3805. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3806. if len(id) > 0 {
  3807. db = db.Where("warehouse_out_id in (?)", id)
  3808. }
  3809. if orgid > 0 {
  3810. db = db.Where("user_org_id = ?", orgid)
  3811. }
  3812. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3813. return list, err
  3814. }
  3815. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3816. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  3817. if len(id) > 0 {
  3818. db = db.Where("warehouse_out_id in (?)", id)
  3819. }
  3820. if orgid > 0 {
  3821. db = db.Where("user_org_id = ?", orgid)
  3822. }
  3823. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3824. return list, err
  3825. }
  3826. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  3827. db := XTReadDB().Model(&info)
  3828. db = db.Preload("GoodInfo", "status = 1")
  3829. 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
  3830. return info, err
  3831. }
  3832. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3833. db := readDb.Model(&models.AutomaticReduceDetail{})
  3834. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3835. db = db.Preload("GoodsType", "status = 1")
  3836. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3837. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  3838. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  3839. db = db.Count(&total)
  3840. err = db.Find(&user).Error
  3841. return
  3842. }
  3843. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  3844. info := models.GoodInfo{}
  3845. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3846. return info, err
  3847. }
  3848. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  3849. info := models.GoodInformationThirty{}
  3850. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3851. return info, err
  3852. }
  3853. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  3854. info := models.GoodInformationThirty{}
  3855. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  3856. if err != gorm.ErrRecordNotFound {
  3857. if err != nil {
  3858. tx.Rollback()
  3859. return info, err
  3860. }
  3861. }
  3862. return info, err
  3863. }
  3864. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  3865. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  3866. return err
  3867. }
  3868. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  3869. db := XTReadDB().Table("xt_warehouse_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 orgid > 0 {
  3873. db = db.Where("x.org_id = ?", orgid)
  3874. }
  3875. if len(orderid) > 0 {
  3876. db = db.Where("x.warehousing_id in(?)", orderid)
  3877. }
  3878. if startime > 0 {
  3879. db = db.Where("x.ctime>=?", startime)
  3880. }
  3881. if endtime > 0 {
  3882. db = db.Where("x.ctime<=?", endtime)
  3883. }
  3884. 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
  3885. return info, err
  3886. }
  3887. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  3888. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3889. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3890. fmt.Println(table)
  3891. if orgid > 0 {
  3892. db = db.Where("x.org_id = ?", orgid)
  3893. }
  3894. if len(orderid) > 0 {
  3895. db = db.Where("x.warehousing_id in(?)", orderid)
  3896. }
  3897. if startime > 0 {
  3898. db = db.Where("x.ctime>=?", startime)
  3899. }
  3900. if endtime > 0 {
  3901. db = db.Where("x.ctime<=?", endtime)
  3902. }
  3903. if storehouse_id > 0 {
  3904. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3905. }
  3906. 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
  3907. return info, err
  3908. }
  3909. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3910. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3911. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  3912. fmt.Println(table)
  3913. if startime > 0 {
  3914. db = db.Where("x.ctime>=?", startime)
  3915. }
  3916. if endtime > 0 {
  3917. db = db.Where("x.ctime<=?", endtime)
  3918. }
  3919. if orgid > 0 {
  3920. db = db.Where("x.org_id = ?", orgid)
  3921. }
  3922. 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
  3923. return info, err
  3924. }
  3925. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  3926. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  3927. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3928. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  3929. fmt.Println(table, tableOne)
  3930. if orgid > 0 {
  3931. db = db.Where("x.org_id = ?", orgid)
  3932. }
  3933. if len(ids) > 0 {
  3934. db = db.Where("x.warehouse_out_id in (?)", ids)
  3935. }
  3936. if startime > 0 {
  3937. db = db.Where("x.ctime>=?", startime)
  3938. }
  3939. if endtime > 0 {
  3940. db = db.Where("x.ctime<=?", endtime)
  3941. }
  3942. err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.number,x.license_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,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
  3943. return out, err
  3944. }
  3945. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  3946. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  3947. if len(ids) > 0 {
  3948. db = db.Where("x.id in(?)", ids)
  3949. }
  3950. if orgid > 0 {
  3951. db = db.Where("x.org_id = ?", orgid)
  3952. }
  3953. 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
  3954. return info, err
  3955. }
  3956. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3957. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  3958. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  3959. fmt.Println(table)
  3960. if len(ids) > 0 {
  3961. db = db.Where("x.warehouse_out_id in(?)", ids)
  3962. }
  3963. if orgid > 0 {
  3964. db = db.Where("x.org_id = ?", orgid)
  3965. }
  3966. 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
  3967. return info, err
  3968. }
  3969. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  3970. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  3971. likeKey := "%" + keyword + "%"
  3972. if len(keyword) > 0 {
  3973. db = db.Where("x.drug_name like ?", likeKey)
  3974. }
  3975. if orgid > 0 {
  3976. db = db.Where("x.org_id = ?", orgid)
  3977. }
  3978. err = db.Find(&basedrug).Error
  3979. return basedrug, err
  3980. }
  3981. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  3982. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3983. if len(ids) > 0 {
  3984. db = db.Where("x.drug_id in(?)", ids)
  3985. }
  3986. if orgid > 0 {
  3987. db = db.Where("x.org_id = ?", orgid)
  3988. }
  3989. if startime > 0 {
  3990. db = db.Where("x.ctime>=?", startime)
  3991. }
  3992. if endtime > 0 {
  3993. db = db.Where("x.ctime<=?", endtime)
  3994. }
  3995. if storehouse_id > 0 {
  3996. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3997. }
  3998. err = db.Find(&info).Error
  3999. return info, err
  4000. }
  4001. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4002. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4003. if len(ids) > 0 {
  4004. db = db.Where("x.drug_id in(?)", ids)
  4005. }
  4006. if orgid > 0 {
  4007. db = db.Where("x.org_id = ?", orgid)
  4008. }
  4009. if startime > 0 {
  4010. db = db.Where("x.ctime>=?", startime)
  4011. }
  4012. if endtime > 0 {
  4013. db = db.Where("x.ctime<=?", endtime)
  4014. }
  4015. if storehouse_id > 0 {
  4016. db = db.Where("storehouse_id = ?", storehouse_id)
  4017. }
  4018. err = db.Find(&info).Error
  4019. return info, err
  4020. }
  4021. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4022. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4023. if drugid > 0 {
  4024. db = db.Where("x.drug_id = ?", drugid)
  4025. }
  4026. if orgid > 0 {
  4027. db = db.Where("x.org_id = ?", orgid)
  4028. }
  4029. if storehouse_id > 0 {
  4030. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4031. }
  4032. 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
  4033. return info, err
  4034. }
  4035. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4036. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4037. if drugid > 0 {
  4038. db = db.Where("x.drug_id = ?", drugid)
  4039. }
  4040. if orgid > 0 {
  4041. db = db.Where("x.org_id = ?", orgid)
  4042. }
  4043. if storehouse_id > 0 {
  4044. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4045. }
  4046. 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
  4047. return info, err
  4048. }
  4049. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4050. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4051. if id > 0 {
  4052. db = db.Where("x.id = ?", id)
  4053. }
  4054. if orgid > 0 {
  4055. db = db.Where("x.org_id = ?", orgid)
  4056. }
  4057. 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
  4058. return info, err
  4059. }
  4060. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4061. info := models.VmDrugWarehouseInfo{}
  4062. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4063. if id == 0 {
  4064. db = db.Where("id = ?", id)
  4065. }
  4066. if drugid > 0 {
  4067. db = db.Where("x.drug_id = ?", drugid)
  4068. }
  4069. if orgid > 0 {
  4070. db = db.Where("x.org_id = ?", orgid)
  4071. }
  4072. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4073. return info, err
  4074. }
  4075. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4076. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4077. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4078. fmt.Println(table)
  4079. if drugid > 0 {
  4080. db = db.Where("x.drug_id = ?", drugid)
  4081. }
  4082. if storehouse_id > 0 {
  4083. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4084. }
  4085. 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
  4086. return list, err
  4087. }
  4088. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4089. lib := models.BaseDrugLib{}
  4090. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4091. return lib, err
  4092. }
  4093. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4094. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4095. if goodid > 0 {
  4096. db = db.Where("x.good_id = ?", goodid)
  4097. }
  4098. if orgid > 0 {
  4099. db = db.Where("x.org_id = ?", orgid)
  4100. }
  4101. 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
  4102. return info, err
  4103. }
  4104. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4105. 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
  4106. return err
  4107. }
  4108. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4109. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4110. return info, err
  4111. }
  4112. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4113. //遍历循环入库数据
  4114. for i, item := range list {
  4115. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4116. if (item.WarehousingCount - item.StockCount) >= count {
  4117. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4118. info := models.WarehousingInfo{
  4119. GoodId: item.GoodId,
  4120. OrgId: item.OrgId,
  4121. WarehousingId: item.WarehousingId,
  4122. StockCount: item.StockCount + count,
  4123. }
  4124. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4125. fmt.Println("2323233232323232323232323232323", err2)
  4126. return err
  4127. }
  4128. if (item.WarehousingCount - item.StockCount) < count {
  4129. //如果该批次小于退库数量,则先出库该相差数量
  4130. var countOne = item.WarehousingCount - item.StockCount
  4131. infoOne := models.WarehousingInfo{
  4132. GoodId: item.GoodId,
  4133. OrgId: item.OrgId,
  4134. WarehousingId: item.WarehousingId,
  4135. StockCount: countOne + item.StockCount,
  4136. }
  4137. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4138. if err == nil {
  4139. //剩余的出库到第二个批次
  4140. var countTwo = count - countOne
  4141. infos := append(list, list[i+1])
  4142. var idArray []*models.WarehousingInfo
  4143. for _, it := range infos {
  4144. if it.WarehousingId != item.WarehousingId {
  4145. idArray = append(idArray, it)
  4146. }
  4147. }
  4148. //查询更新后的库存
  4149. UpdateCancelOut(idArray, countTwo)
  4150. return err
  4151. } else {
  4152. return err
  4153. }
  4154. }
  4155. }
  4156. return err
  4157. }
  4158. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4159. if len(idstr) > 0 {
  4160. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4161. } else {
  4162. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4163. }
  4164. return info, err
  4165. }
  4166. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4167. if len(idstr) > 0 {
  4168. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4169. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4170. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4171. }).Find(&info).Error
  4172. }
  4173. return info, err
  4174. }
  4175. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4176. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4177. if startime > 0 {
  4178. db = db.Where("x.ctime >=?", startime)
  4179. }
  4180. if endtime > 0 {
  4181. db = db.Where("x.ctime <= ?", endtime)
  4182. }
  4183. if orgid > 0 {
  4184. db = db.Where("x.org_id = ?", orgid)
  4185. }
  4186. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4187. return info, err
  4188. }
  4189. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4190. info := models.WarehouseOutInfo{}
  4191. var err error
  4192. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4193. if err == gorm.ErrRecordNotFound {
  4194. return nil, err
  4195. }
  4196. if err != nil {
  4197. return nil, err
  4198. }
  4199. return &info, nil
  4200. }
  4201. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4202. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4203. if goodid > 0 {
  4204. db = db.Where("x.good_id = ?", goodid)
  4205. }
  4206. if orgid > 0 {
  4207. db = db.Where("x.org_id = ?", orgid)
  4208. }
  4209. if id > 0 {
  4210. db = db.Where("x.warehouse_info_id = ?", id)
  4211. }
  4212. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4213. return info, err
  4214. }
  4215. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4216. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4217. if goodid > 0 {
  4218. db = db.Where("x.good_id = ?", goodid)
  4219. }
  4220. if orgid > 0 {
  4221. db = db.Where("x.user_org_id = ?", orgid)
  4222. }
  4223. if id > 0 {
  4224. db = db.Where("x.warehousing_id = ?", id)
  4225. }
  4226. if storehouse_id > 0 {
  4227. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4228. }
  4229. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4230. return info, err
  4231. }
  4232. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4233. stock := models.CancelStock{}
  4234. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4235. return stock, err
  4236. }
  4237. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4238. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4239. return info, err
  4240. }
  4241. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4242. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4243. return info, err
  4244. }
  4245. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4246. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4247. return stock, err
  4248. }
  4249. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4250. 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)")
  4251. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4252. fmt.Println(table)
  4253. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4254. fmt.Println(tableone)
  4255. if id > 0 {
  4256. db = db.Where("x.warehouse_out_id = ?", id)
  4257. }
  4258. if goodid > 0 {
  4259. db = db.Where("x.good_id = ?", goodid)
  4260. }
  4261. 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
  4262. return stock, err
  4263. }
  4264. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4265. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4266. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4267. fmt.Println(table)
  4268. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4269. fmt.Println(tableone)
  4270. if id > 0 {
  4271. db = db.Where("x.warehouse_out_id = ?", id)
  4272. }
  4273. if goodid > 0 {
  4274. db = db.Where("x.good_id = ?", goodid)
  4275. }
  4276. 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
  4277. return stock, err
  4278. }
  4279. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4280. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4281. return info, err
  4282. }
  4283. func ExportDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseInfo, err error) {
  4284. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1")
  4285. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4286. tableOne := XTReadDB().Table("xt_drug_warehouse as p")
  4287. fmt.Println("table", table)
  4288. fmt.Println("tableOne", tableOne)
  4289. if len(warehouse_out_id) > 0 {
  4290. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4291. }
  4292. 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
  4293. return out, err
  4294. }
  4295. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4296. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4297. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4298. fmt.Println("table", table)
  4299. if len(warehouse_out_id) > 0 {
  4300. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4301. }
  4302. 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
  4303. return out, err
  4304. }
  4305. func GetExportOutOrderDrugListOne(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4306. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1")
  4307. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4308. fmt.Println("table", table)
  4309. if len(warehouse_out_id) > 0 {
  4310. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4311. }
  4312. 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
  4313. return out, err
  4314. }
  4315. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4316. db := XTReadDB().Model(&info).Where("status = 1")
  4317. if len(warehouse_out_id) > 0 {
  4318. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4319. }
  4320. if orgid > 0 {
  4321. db = db.Where("org_id = ?", orgid)
  4322. }
  4323. err = db.Find(&info).Error
  4324. return info, err
  4325. }
  4326. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4327. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4328. return out, err
  4329. }
  4330. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4331. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4332. 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)
  4333. }).Find(&out).Error
  4334. return out, err
  4335. }
  4336. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4337. info := models.WarehouseOutInfo{}
  4338. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4339. return info, err
  4340. }
  4341. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4342. info := models.WarehousingInfo{}
  4343. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4344. if goodid > 0 {
  4345. db = db.Where("x.good_id = ?", goodid)
  4346. }
  4347. if storehouse_id > 0 {
  4348. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4349. }
  4350. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4351. return info, err
  4352. }
  4353. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4354. info := models.WarehousingInfo{}
  4355. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4356. return info, err
  4357. }
  4358. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4359. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4360. return err
  4361. }
  4362. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4363. if storehouse_id == 0 {
  4364. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4365. }
  4366. if storehouse_id > 0 {
  4367. 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
  4368. }
  4369. return info, err
  4370. }
  4371. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4372. if len(warehousingInfo) > 0 {
  4373. utx := writeDb.Begin()
  4374. if len(warehousingInfo) > 0 {
  4375. 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 "
  4376. insertParams := make([]string, 0)
  4377. insertData := make([]interface{}, 0)
  4378. for _, info := range warehousingInfo {
  4379. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4380. insertData = append(insertData, info.WarehousingId)
  4381. insertData = append(insertData, info.GoodId)
  4382. insertData = append(insertData, info.Number)
  4383. insertData = append(insertData, info.LicenseNumber)
  4384. insertData = append(insertData, info.Count)
  4385. insertData = append(insertData, info.UserOrgId)
  4386. insertData = append(insertData, info.PatientId)
  4387. insertData = append(insertData, info.SystemTime)
  4388. insertData = append(insertData, info.ConsumableType)
  4389. insertData = append(insertData, info.IsSys)
  4390. insertData = append(insertData, info.WarehousingOrder)
  4391. insertData = append(insertData, info.WarehouseOutId)
  4392. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4393. insertData = append(insertData, info.IsEdit)
  4394. insertData = append(insertData, info.CancelStockId)
  4395. insertData = append(insertData, info.CancelOrderNumber)
  4396. insertData = append(insertData, info.Manufacturer)
  4397. insertData = append(insertData, info.Dealer)
  4398. insertData = append(insertData, info.Creator)
  4399. insertData = append(insertData, info.UpdateCreator)
  4400. insertData = append(insertData, info.Status)
  4401. insertData = append(insertData, info.Ctime)
  4402. insertData = append(insertData, info.Mtime)
  4403. insertData = append(insertData, info.Price)
  4404. insertData = append(insertData, info.WarehousingDetailId)
  4405. insertData = append(insertData, info.WarehouseOutDetailId)
  4406. insertData = append(insertData, info.CancelOutDetailId)
  4407. insertData = append(insertData, info.ExpireDate)
  4408. insertData = append(insertData, info.ProductDate)
  4409. }
  4410. thisSQL += strings.Join(insertParams, ", ")
  4411. err = utx.Exec(thisSQL, insertData...).Error
  4412. if err != nil {
  4413. utx.Rollback()
  4414. return
  4415. }
  4416. }
  4417. utx.Commit()
  4418. }
  4419. return
  4420. }
  4421. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4422. utx := XTWriteDB().Begin()
  4423. err := utx.Create(&flow).Error
  4424. if err != nil {
  4425. utx.Rollback()
  4426. return err
  4427. }
  4428. utx.Commit()
  4429. return err
  4430. }
  4431. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4432. err := tx.Create(&flow).Error
  4433. if err != nil {
  4434. tx.Rollback()
  4435. return err
  4436. }
  4437. return err
  4438. }
  4439. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4440. err := XTWriteDB().Create(&flow).Error
  4441. return err
  4442. }
  4443. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4444. err := XTWriteDB().Save(&flow).Error
  4445. return err
  4446. }
  4447. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4448. 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
  4449. return err
  4450. }
  4451. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  4452. 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
  4453. return err
  4454. }
  4455. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  4456. stock := models.VmStockFlow{}
  4457. var err error
  4458. 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
  4459. if err == gorm.ErrRecordNotFound {
  4460. return nil, err
  4461. }
  4462. if err != nil {
  4463. return nil, err
  4464. }
  4465. return &stock, nil
  4466. }
  4467. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4468. 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
  4469. return flow, nil
  4470. }
  4471. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4472. 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
  4473. return flow, nil
  4474. }
  4475. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4476. 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
  4477. if err != gorm.ErrRecordNotFound {
  4478. if err != nil {
  4479. tx.Rollback()
  4480. return flow, nil
  4481. }
  4482. }
  4483. return flow, nil
  4484. }
  4485. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4486. 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
  4487. return flow, nil
  4488. }
  4489. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  4490. 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
  4491. if err != gorm.ErrRecordNotFound {
  4492. if err != nil {
  4493. tx.Rollback()
  4494. return flow, nil
  4495. }
  4496. }
  4497. return flow, nil
  4498. }
  4499. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  4500. 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
  4501. return flow, nil
  4502. }
  4503. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  4504. 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
  4505. if err != gorm.ErrRecordNotFound {
  4506. if err != nil {
  4507. tx.Rollback()
  4508. return flow, err
  4509. }
  4510. }
  4511. return flow, err
  4512. }
  4513. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4514. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  4515. return flow, nil
  4516. }
  4517. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4518. 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
  4519. return flow, nil
  4520. }
  4521. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4522. 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
  4523. return flow, nil
  4524. }
  4525. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4526. 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
  4527. if err != gorm.ErrRecordNotFound {
  4528. if err != nil {
  4529. tx.Rollback()
  4530. return flow, nil
  4531. }
  4532. }
  4533. return flow, nil
  4534. }
  4535. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4536. 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
  4537. return flow, nil
  4538. }
  4539. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  4540. info := models.WarehouseOutInfo{}
  4541. var err error
  4542. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4543. if err == gorm.ErrRecordNotFound {
  4544. return nil, err
  4545. }
  4546. if err != nil {
  4547. return nil, err
  4548. }
  4549. return &info, nil
  4550. }
  4551. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  4552. info := models.WarehouseOutInfo{}
  4553. var err error
  4554. 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
  4555. if err == gorm.ErrRecordNotFound {
  4556. return nil, err
  4557. }
  4558. if err != nil {
  4559. return nil, err
  4560. }
  4561. return &info, nil
  4562. }
  4563. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  4564. info := models.WarehouseOutInfo{}
  4565. 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
  4566. return info, err
  4567. }
  4568. 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) {
  4569. offset := (page - 1) * limit
  4570. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  4571. if consumable_type > 0 {
  4572. //入库
  4573. if consumable_type == 1 {
  4574. db = db.Where("consumable_type = ?", consumable_type)
  4575. }
  4576. //2 手动出库
  4577. if consumable_type == 2 {
  4578. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  4579. }
  4580. //3.自动出库
  4581. if consumable_type == 3 {
  4582. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  4583. }
  4584. //4.手动退库
  4585. if consumable_type == 4 {
  4586. db = db.Where(" consumable_type = ?", consumable_type)
  4587. }
  4588. //自动退库
  4589. if consumable_type == 7 {
  4590. db = db.Where(" consumable_type = ?", consumable_type)
  4591. }
  4592. if consumable_type == 10 {
  4593. db = db.Where(" consumable_type = ?", consumable_type)
  4594. }
  4595. if consumable_type == 11 {
  4596. db = db.Where(" consumable_type = ?", consumable_type)
  4597. }
  4598. if consumable_type == 12 {
  4599. db = db.Where(" consumable_type = ?", consumable_type)
  4600. }
  4601. if consumable_type == 13 {
  4602. db = db.Where(" consumable_type = ?", consumable_type)
  4603. }
  4604. if consumable_type == 15 {
  4605. db = db.Where(" consumable_type = ?", consumable_type)
  4606. }
  4607. }
  4608. if orgId > 0 {
  4609. db = db.Where("user_org_id = ?", orgId)
  4610. }
  4611. if startTime > 0 {
  4612. db = db.Where("ctime >= ?", startTime)
  4613. }
  4614. if endTime > 0 {
  4615. db = db.Where("ctime<=?", endTime)
  4616. }
  4617. if good_id > 0 {
  4618. db = db.Where("good_id = ?", good_id)
  4619. }
  4620. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  4621. return flow, total, err
  4622. }
  4623. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  4624. detail := models.AutomaticReduceDetail{}
  4625. 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
  4626. return detail, err
  4627. }
  4628. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  4629. detail := models.WarehouseOutInfo{}
  4630. 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
  4631. return detail, err
  4632. }
  4633. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  4634. outInfo := models.WarehouseOutInfo{}
  4635. 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
  4636. return err
  4637. }
  4638. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  4639. outInfo := models.WarehouseOutInfo{}
  4640. 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
  4641. if err != nil {
  4642. tx.Rollback()
  4643. return err
  4644. }
  4645. return err
  4646. }
  4647. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  4648. outInfo := models.WarehouseOutInfo{}
  4649. 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
  4650. return err
  4651. }
  4652. 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 {
  4653. outInfo := models.WarehouseOutInfo{}
  4654. 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
  4655. if err != nil {
  4656. tx.Rollback()
  4657. return err
  4658. }
  4659. return err
  4660. }
  4661. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  4662. info := models.WarehouseOutInfo{}
  4663. 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
  4664. return info, err
  4665. }
  4666. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  4667. info := models.WarehousingInfo{}
  4668. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4669. return info, err
  4670. }
  4671. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  4672. info := models.WarehousingInfo{}
  4673. db := XTReadDB().Model(&info)
  4674. if good_id > 0 {
  4675. db = db.Where("good_id = ? and status = 1", good_id)
  4676. }
  4677. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  4678. return info, err
  4679. }
  4680. 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) {
  4681. offset := (page - 1) * limit
  4682. db := XTReadDB().Model(drug_flow).Where("status = 1")
  4683. if drugid > 0 {
  4684. db = db.Where("drug_id = ?", drugid)
  4685. }
  4686. if orgid > 0 {
  4687. db = db.Where("user_org_id = ?", orgid)
  4688. }
  4689. if startTime > 0 {
  4690. db = db.Where("ctime >=? ", startTime)
  4691. }
  4692. if endTime > 0 {
  4693. db = db.Where("ctime<=?", endTime)
  4694. }
  4695. if stock_type > 0 {
  4696. //手动入库
  4697. if stock_type == 1 {
  4698. db = db.Where("consumable_type = 1")
  4699. }
  4700. //手动出库
  4701. if stock_type == 2 {
  4702. db = db.Where("consumable_type = 2 and is_sys = 0")
  4703. }
  4704. //自动出库
  4705. if stock_type == 3 {
  4706. db = db.Where("consumable_type = 3 and is_sys = 1")
  4707. }
  4708. if stock_type == 4 {
  4709. db = db.Where("consumable_type = 4")
  4710. }
  4711. if stock_type == 10 {
  4712. db = db.Where("consumable_type = 10")
  4713. }
  4714. if stock_type == 11 {
  4715. db = db.Where("consumable_type = 11")
  4716. }
  4717. if stock_type == 12 {
  4718. db = db.Where("consumable_type = 12")
  4719. }
  4720. if stock_type == 13 {
  4721. db = db.Where("consumable_type = 13")
  4722. }
  4723. if stock_type == 15 {
  4724. db = db.Where("consumable_type = 15")
  4725. }
  4726. }
  4727. 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
  4728. return drug_flow, total, err
  4729. }
  4730. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  4731. outInfo := models.DrugWarehouseOutInfo{}
  4732. var err error
  4733. 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
  4734. if err == gorm.ErrRecordNotFound {
  4735. return nil, err
  4736. }
  4737. if err != nil {
  4738. return nil, err
  4739. }
  4740. return &outInfo, nil
  4741. }
  4742. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  4743. 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
  4744. return outInfo, err
  4745. }
  4746. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  4747. outInfo := models.DrugWarehouseOutInfo{}
  4748. 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
  4749. return err
  4750. }
  4751. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  4752. db := XTReadDB().Model(&cancle).Where("status = 1")
  4753. if startime > 0 {
  4754. db = db.Where("ctime >=?", startime)
  4755. }
  4756. if endtime > 0 {
  4757. db = db.Where("ctime <=?", endtime)
  4758. }
  4759. if len(ids) > 0 {
  4760. db = db.Where("cancel_stock_id in(?)", ids)
  4761. }
  4762. if orgid > 0 {
  4763. db = db.Where("org_id = ?", orgid)
  4764. }
  4765. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  4766. err = db.Find(&cancle).Error
  4767. return cancle, err
  4768. }
  4769. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  4770. db := XTReadDB().Model(&info)
  4771. if id > 0 {
  4772. db = db.Where("cancel_stock_id = ? and status = 1", id)
  4773. }
  4774. db = db.Preload("BaseDrugLib")
  4775. err = db.Find(&info).Error
  4776. return info, err
  4777. }
  4778. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  4779. info := models.XtDrugWarehouseInfo{}
  4780. db := XTReadDB().Model(&info).Where("status = 1")
  4781. if id > 0 {
  4782. db = db.Where("id = ?", id)
  4783. }
  4784. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4785. return info, err
  4786. }
  4787. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  4788. // info := models.XtDrugWarehouseInfo{}
  4789. // db := tx.Model(&info).Where("status = 1")
  4790. // if id > 0 {
  4791. // db = db.Where("id = ?", id)
  4792. // }
  4793. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4794. // return info, err
  4795. //}
  4796. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4797. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  4798. return info, err
  4799. }
  4800. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  4801. 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
  4802. return info, err
  4803. }
  4804. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  4805. info := models.HisDoctorAdviceInfo{}
  4806. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  4807. return info, err
  4808. }
  4809. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  4810. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4811. return info, err
  4812. }
  4813. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  4814. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4815. if err != gorm.ErrRecordNotFound {
  4816. if err != nil {
  4817. tx.Rollback()
  4818. return info, err
  4819. }
  4820. }
  4821. return info, err
  4822. }
  4823. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  4824. adviceInfo := models.HisDoctorAdviceInfo{}
  4825. 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
  4826. if err != gorm.ErrRecordNotFound {
  4827. if err != nil {
  4828. tx.Rollback()
  4829. return adviceInfo, err
  4830. }
  4831. }
  4832. return adviceInfo, err
  4833. }
  4834. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  4835. info := models.HisDoctorAdviceInfo{}
  4836. 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
  4837. return info, err
  4838. }
  4839. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  4840. detail := models.DrugAutomaticReduceDetail{}
  4841. 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
  4842. return detail, err
  4843. }
  4844. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  4845. 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
  4846. return out, err
  4847. }
  4848. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  4849. 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
  4850. if err != gorm.ErrRecordNotFound {
  4851. if err != nil {
  4852. tx.Rollback()
  4853. return out, err
  4854. }
  4855. }
  4856. return out, err
  4857. }
  4858. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  4859. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  4860. return err
  4861. }
  4862. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  4863. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  4864. return err
  4865. }
  4866. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  4867. 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
  4868. 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
  4869. 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
  4870. return err
  4871. }
  4872. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  4873. 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
  4874. 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
  4875. return err
  4876. }
  4877. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64, tx *gorm.DB) error {
  4878. err := tx.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
  4879. err = tx.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
  4880. if err != nil {
  4881. tx.Rollback()
  4882. return err
  4883. }
  4884. return err
  4885. }
  4886. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  4887. info := models.XtDrugWarehouseInfo{}
  4888. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  4889. return info, err
  4890. }
  4891. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  4892. info := models.XtDrugWarehouseInfo{}
  4893. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  4894. return info, err
  4895. }
  4896. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  4897. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  4898. return err
  4899. }
  4900. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  4901. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  4902. // return err
  4903. //}
  4904. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  4905. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  4906. //return err
  4907. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  4908. return err
  4909. }
  4910. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  4911. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  4912. // return err
  4913. //}
  4914. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  4915. info := models.DrugCancelStockInfo{}
  4916. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  4917. return info, err
  4918. }
  4919. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  4920. info := models.DrugCancelStockInfo{}
  4921. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  4922. return info, err
  4923. }
  4924. func UpdatedDrugInfo(id int64, count int64) (err error) {
  4925. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  4926. return err
  4927. }
  4928. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  4929. 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
  4930. return err
  4931. }
  4932. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  4933. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  4934. return err
  4935. }
  4936. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  4937. 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
  4938. return err
  4939. }
  4940. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  4941. 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
  4942. return err
  4943. }
  4944. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  4945. 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
  4946. return err
  4947. }
  4948. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  4949. 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
  4950. return err
  4951. }
  4952. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  4953. info := models.WarehousingInfo{}
  4954. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4955. return info, err
  4956. }
  4957. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4958. info := models.WarehousingInfo{}
  4959. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  4960. return info, err
  4961. }
  4962. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4963. info := models.WarehousingInfo{}
  4964. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  4965. return info, err
  4966. }
  4967. func CreateWarehouseOut(out models.WarehouseOut) error {
  4968. err := XTWriteDB().Create(&out).Error
  4969. return err
  4970. }
  4971. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  4972. out := models.WarehouseOut{}
  4973. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  4974. return out, err
  4975. }
  4976. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  4977. err := XTWriteDB().Create(&info).Error
  4978. return err
  4979. }
  4980. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  4981. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4982. return err
  4983. }
  4984. func UpdateWareInfoById(id int64, count int64) error {
  4985. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4986. return err
  4987. }
  4988. func UpdateWareInfoByIdSix(id int64, count int64) error {
  4989. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  4990. return err
  4991. }
  4992. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  4993. info := models.CancelStockInfo{}
  4994. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4995. return info, err
  4996. }
  4997. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  4998. info := models.CancelStockInfo{}
  4999. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5000. return info, err
  5001. }
  5002. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5003. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5004. return info, err
  5005. }
  5006. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5007. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5008. return info, err
  5009. }
  5010. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5011. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5012. return err
  5013. }
  5014. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5015. drugFlow := models.DrugFlow{}
  5016. 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
  5017. return err
  5018. }
  5019. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5020. drugFlow := models.DrugFlow{}
  5021. 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
  5022. return err
  5023. }
  5024. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5025. stock := models.DrugCancelStock{}
  5026. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5027. return stock, err
  5028. }
  5029. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5030. // stock := models.DrugCancelStock{}
  5031. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5032. // return stock, err
  5033. //}
  5034. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5035. db := XTReadDB().Model(&info)
  5036. if warehouse_info_id > 0 {
  5037. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5038. }
  5039. if drug_id > 0 {
  5040. db = db.Where("drug_id = ? and status =1", drug_id)
  5041. }
  5042. err = db.Find(&info).Error
  5043. return info, err
  5044. }
  5045. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5046. // db := tx.Model(&info)
  5047. // if warehouse_info_id > 0 {
  5048. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5049. // }
  5050. // if drug_id > 0 {
  5051. // db = db.Where("drug_id = ? and status =1", drug_id)
  5052. // }
  5053. // err = db.Find(&info).Error
  5054. // return info, err
  5055. //}
  5056. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5057. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5058. return info, err
  5059. }
  5060. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5061. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5062. // return info, err
  5063. //}
  5064. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5065. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5066. return config, err
  5067. }
  5068. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5069. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5070. return config, err
  5071. }
  5072. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5073. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5074. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5075. fmt.Println(table)
  5076. likeKey := "%" + keyword + "%"
  5077. if orgid > 0 {
  5078. db = db.Where("x.org_id = ?", orgid)
  5079. }
  5080. if storehouse_id > 0 {
  5081. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5082. }
  5083. 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
  5084. return info, err
  5085. }
  5086. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5087. err := XTWriteDB().Create(&adjustPrice).Error
  5088. return err
  5089. }
  5090. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5091. likeKey := "%" + keyword + "%"
  5092. offset := (page - 1) * limit
  5093. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5094. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5095. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5096. fmt.Println(table, tab)
  5097. if startime > 0 {
  5098. db = db.Where("x.start_time>=?", startime)
  5099. }
  5100. if endtime > 0 {
  5101. db = db.Where("x.start_time<=?", endtime)
  5102. }
  5103. if len(keyword) > 0 {
  5104. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5105. }
  5106. if orgid > 0 {
  5107. db = db.Where("x.user_org_id = ?", orgid)
  5108. }
  5109. 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
  5110. return list, total, err
  5111. }
  5112. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5113. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5114. return err
  5115. }
  5116. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5117. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5118. return err
  5119. }
  5120. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5121. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5122. return info, err
  5123. }
  5124. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5125. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5126. return adjust, err
  5127. }
  5128. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5129. err := XTWriteDB().Create(&price).Error
  5130. return err
  5131. }
  5132. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5133. likeKey := "%" + keyword + "%"
  5134. offset := (page - 1) * limit
  5135. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5136. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5137. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5138. fmt.Println(table, tab)
  5139. if startime > 0 {
  5140. db = db.Where("x.ctime>=?", startime)
  5141. }
  5142. if endtime > 0 {
  5143. db = db.Where("x.ctime<=?", endtime)
  5144. }
  5145. if len(keyword) > 0 {
  5146. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5147. }
  5148. if orgid > 0 {
  5149. db = db.Where("x.user_org_id = ?", orgid)
  5150. }
  5151. 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
  5152. return adjust, total, err
  5153. }
  5154. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5155. likeKey := "%" + keyword + "%"
  5156. offset := (page - 1) * limit
  5157. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5158. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5159. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5160. fmt.Println(table, tab)
  5161. if len(keyword) > 0 {
  5162. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5163. }
  5164. if orgid > 0 {
  5165. db = db.Where("x.user_org_id = ?", orgid)
  5166. }
  5167. if startime > 0 {
  5168. db = db.Where("x.ctime >=?", startime)
  5169. }
  5170. if endtime > 0 {
  5171. db = db.Where("x.ctime <=?", endtime)
  5172. }
  5173. if storehouse_id > 0 {
  5174. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5175. }
  5176. err = db.Group("x.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
  5177. return adjust, total, err
  5178. }
  5179. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5180. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5181. if inventory_type == 0 {
  5182. db = db.Where("x.inventory_type = 4")
  5183. }
  5184. if inventory_type > 0 {
  5185. db = db.Where("x.inventory_type = ?", inventory_type)
  5186. }
  5187. if len(keyword) > 0 {
  5188. likeKey := "%" + keyword + "%"
  5189. db = db.Where("x.warehousing_order like ?", likeKey)
  5190. }
  5191. if orgid > 0 {
  5192. db = db.Where("x.user_org_id = ?", orgid)
  5193. }
  5194. 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
  5195. return adjust, err
  5196. }
  5197. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5198. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5199. if orgid > 0 {
  5200. db = db.Where("x.user_org_id = ?", orgid)
  5201. }
  5202. 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
  5203. return adjust, err
  5204. }
  5205. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5206. 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
  5207. return err
  5208. }
  5209. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5210. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5211. return report, err
  5212. }
  5213. func CreateInentory(inventory models.XtStockInventory) error {
  5214. err = XTWriteDB().Create(&inventory).Error
  5215. return err
  5216. }
  5217. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5218. likeKey := "%" + keyword + "%"
  5219. offset := (page - 1) * limit
  5220. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5221. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5222. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5223. fmt.Println(table, tab)
  5224. if startime > 0 {
  5225. db = db.Where("x.ctime>=?", startime)
  5226. }
  5227. if endtime > 0 {
  5228. db = db.Where("x.ctime<=?", endtime)
  5229. }
  5230. if len(keyword) > 0 {
  5231. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5232. }
  5233. if orgid > 0 {
  5234. db = db.Where("x.user_org_id = ?", orgid)
  5235. }
  5236. if inventory_status > 0 {
  5237. db = db.Where("checker_status = ?", inventory_status)
  5238. }
  5239. 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
  5240. return adjust, total, err
  5241. }
  5242. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5243. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5244. return err
  5245. }
  5246. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5247. info := models.XtStockAdjustPrice{}
  5248. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5249. return info, err
  5250. }
  5251. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5252. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5253. return list, err
  5254. }
  5255. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5256. info := models.WarehousingInfo{}
  5257. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5258. return info, err
  5259. }
  5260. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5261. info := models.WarehousingInfo{}
  5262. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5263. //return info, err
  5264. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5265. if id > 0 {
  5266. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5267. }
  5268. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5269. return info, err
  5270. }
  5271. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5272. info := models.WarehousingInfo{}
  5273. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5274. if id > 0 {
  5275. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5276. }
  5277. if storehouse_id > 0 {
  5278. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5279. }
  5280. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5281. return info, err
  5282. }
  5283. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5284. 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
  5285. return err
  5286. }
  5287. func DeleteStockPrice(id int64) error {
  5288. info := models.XtStockAdjustPrice{}
  5289. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5290. return err
  5291. }
  5292. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5293. report := models.XtStockReportPrice{}
  5294. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5295. return report, err
  5296. }
  5297. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5298. 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
  5299. return err
  5300. }
  5301. func DeleteDamage(id int64) error {
  5302. adjust := models.XtStockReportPrice{}
  5303. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5304. return err
  5305. }
  5306. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5307. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5308. return adjust, err
  5309. }
  5310. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5311. inventory := models.XtStockInventory{}
  5312. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5313. return inventory, err
  5314. }
  5315. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5316. 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
  5317. return err
  5318. }
  5319. func DeleteInventory(id int64) error {
  5320. inventory := models.XtStockInventory{}
  5321. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5322. return err
  5323. }
  5324. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5325. likeKey := "%" + keyword + "%"
  5326. offset := (page - 1) * limit
  5327. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5328. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5329. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5330. fmt.Println(table, tab)
  5331. if len(keyword) > 0 {
  5332. db = db.Where("x.good_name like ?", likeKey)
  5333. }
  5334. if orgid > 0 {
  5335. db = db.Where("x.user_org_id = ?", orgid)
  5336. }
  5337. if storehouse_id > 0 {
  5338. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5339. }
  5340. 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
  5341. return list, total, err
  5342. }
  5343. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5344. likeKey := "%" + keyword + "%"
  5345. offset := (page - 1) * limit
  5346. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5347. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5348. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5349. fmt.Println(table, tab)
  5350. if len(keyword) > 0 {
  5351. db = db.Where("x.good_name like ?", likeKey)
  5352. }
  5353. if orgid > 0 {
  5354. db = db.Where("x.user_org_id = ?", orgid)
  5355. }
  5356. if storehouse_id > 0 {
  5357. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5358. }
  5359. if len(ids) > 0 {
  5360. db = db.Where("x.id in(?)", ids)
  5361. }
  5362. 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
  5363. return list, total, err
  5364. }
  5365. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5366. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5367. return list, err
  5368. }
  5369. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5370. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5371. return list, err
  5372. }
  5373. func DeleteDrugPrice(id int64) error {
  5374. info := models.XtDrugAdjustPrice{}
  5375. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5376. return err
  5377. }
  5378. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5379. info := models.WarehousingInfo{}
  5380. db := XTReadDB().Model(&info).Where("status = 1")
  5381. if goodid > 0 {
  5382. db = db.Where("good_id = ?", goodid)
  5383. }
  5384. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5385. return info, err
  5386. }
  5387. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5388. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5389. return err
  5390. }
  5391. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5392. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5393. return err
  5394. }
  5395. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5396. err := XTWriteDB().Create(&record).Error
  5397. return err
  5398. }
  5399. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5400. info := models.WarehousingInfo{}
  5401. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5402. //return info, err
  5403. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5404. if id > 0 {
  5405. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5406. }
  5407. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5408. return info, err
  5409. }
  5410. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5411. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5412. return info, err
  5413. }
  5414. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  5415. info := models.DrugWarehouseInfo{}
  5416. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5417. return info, err
  5418. }
  5419. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  5420. 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
  5421. return err
  5422. }
  5423. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  5424. 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
  5425. return err
  5426. }
  5427. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  5428. 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
  5429. return info, err
  5430. }
  5431. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  5432. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5433. return err
  5434. }
  5435. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  5436. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  5437. return err
  5438. }
  5439. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  5440. db := XTReadDB().Model(&list).Where(" status = 1")
  5441. if good_id > 0 {
  5442. db = db.Where("good_id = ?", good_id)
  5443. }
  5444. if len(keyword) > 0 {
  5445. likeKey := "%" + keyword + "%"
  5446. db = db.Where("warehousing_order like ?", likeKey)
  5447. }
  5448. if inventory_type > 0 {
  5449. db = db.Where("inventory_type = ?", inventory_type)
  5450. }
  5451. if inventory_type == 0 {
  5452. db = db.Where("inventory_type = 4")
  5453. }
  5454. err = db.Find(&list).Error
  5455. return list, err
  5456. }
  5457. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  5458. flow := models.DrugFlow{}
  5459. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  5460. if err == gorm.ErrRecordNotFound {
  5461. return nil, err
  5462. }
  5463. if err != nil {
  5464. return nil, err
  5465. }
  5466. return &flow, nil
  5467. }
  5468. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  5469. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  5470. return info, err
  5471. }
  5472. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  5473. info := models.XtDrugWarehouseInfo{}
  5474. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  5475. return info, err
  5476. }
  5477. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5478. info := models.WarehousingInfo{}
  5479. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  5480. return info, err
  5481. }
  5482. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  5483. flow := models.VmStockFlow{}
  5484. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  5485. if err == gorm.ErrRecordNotFound {
  5486. return nil, err
  5487. }
  5488. if err != nil {
  5489. return nil, err
  5490. }
  5491. return &flow, nil
  5492. }
  5493. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  5494. info := models.GoodInfo{}
  5495. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  5496. return info, err
  5497. }
  5498. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  5499. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  5500. return err
  5501. }
  5502. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  5503. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  5504. return err
  5505. }
  5506. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  5507. 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}).Error
  5508. return err
  5509. }
  5510. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5511. 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
  5512. return err
  5513. }
  5514. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5515. 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
  5516. return err
  5517. }
  5518. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  5519. 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
  5520. return err
  5521. }
  5522. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  5523. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  5524. return err
  5525. }
  5526. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  5527. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5528. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5529. fmt.Println(table)
  5530. if orgid > 0 {
  5531. db = db.Where("x.org_id = ?", orgid)
  5532. }
  5533. 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
  5534. return info, err
  5535. }
  5536. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  5537. warehouseInfo := models.XtDrugWarehouseInfo{}
  5538. 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
  5539. return err
  5540. }
  5541. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  5542. err := XTWriteDB().Create(&info).Error
  5543. return err
  5544. }
  5545. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  5546. err := tx.Create(&info).Error
  5547. if err != nil {
  5548. tx.Rollback()
  5549. return err
  5550. }
  5551. return err
  5552. }
  5553. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  5554. flow := models.VmStockFlow{}
  5555. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  5556. if err == gorm.ErrRecordNotFound {
  5557. return nil, err
  5558. }
  5559. if err != nil {
  5560. return nil, err
  5561. }
  5562. return &flow, nil
  5563. }
  5564. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  5565. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5566. return err
  5567. }
  5568. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  5569. 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
  5570. return err
  5571. }
  5572. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  5573. flow := models.DrugWarehouseOutInfo{}
  5574. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  5575. return flow, err
  5576. }
  5577. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  5578. info := models.WarehouseOutInfo{}
  5579. 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
  5580. return info, err
  5581. }
  5582. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  5583. 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
  5584. return err
  5585. }
  5586. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  5587. 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
  5588. return err
  5589. }
  5590. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  5591. info := models.DrugWarehouseOutInfo{}
  5592. 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
  5593. if err == gorm.ErrRecordNotFound {
  5594. return nil, err
  5595. }
  5596. if err != nil {
  5597. return nil, err
  5598. }
  5599. return &info, nil
  5600. }
  5601. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  5602. drugFlow := models.DrugFlow{}
  5603. 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
  5604. if err == gorm.ErrRecordNotFound {
  5605. return nil, err
  5606. }
  5607. if err != nil {
  5608. return nil, err
  5609. }
  5610. return &drugFlow, nil
  5611. }
  5612. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  5613. 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
  5614. return err
  5615. }
  5616. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  5617. 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
  5618. return err
  5619. }
  5620. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  5621. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5622. return detail, err
  5623. }
  5624. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  5625. detail := models.DrugAutomaticReduceDetail{}
  5626. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5627. if err == gorm.ErrRecordNotFound {
  5628. return nil, err
  5629. }
  5630. if err != nil {
  5631. return nil, err
  5632. }
  5633. return &detail, nil
  5634. }
  5635. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  5636. 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
  5637. return err
  5638. }
  5639. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  5640. 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
  5641. return err
  5642. }
  5643. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  5644. info := models.DrugWarehouseInfo{}
  5645. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5646. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5647. return info, err
  5648. }
  5649. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  5650. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5651. return err
  5652. }
  5653. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  5654. info := models.DrugWarehouseInfo{}
  5655. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5656. return err
  5657. }
  5658. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  5659. info := models.DrugCancelStockInfo{}
  5660. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5661. return info, err
  5662. }
  5663. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  5664. info := models.DrugWarehouseInfo{}
  5665. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  5666. return err
  5667. }
  5668. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  5669. info := models.DrugWarehouseInfo{}
  5670. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  5671. return err
  5672. }
  5673. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  5674. 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
  5675. return advice, err
  5676. }
  5677. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5678. 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
  5679. return info, err
  5680. }
  5681. // 耗材加库存
  5682. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5683. info := models.GoodInfo{}
  5684. 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
  5685. return info, err
  5686. }
  5687. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5688. info := models.GoodInfo{}
  5689. 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
  5690. 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
  5691. return info, err
  5692. }
  5693. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5694. info := models.GoodInfo{}
  5695. 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
  5696. return info, err
  5697. }
  5698. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5699. info := models.GoodInfo{}
  5700. 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
  5701. 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
  5702. return info, err
  5703. }
  5704. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  5705. info := models.GoodInfo{}
  5706. 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
  5707. return info, err
  5708. }
  5709. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5710. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5711. return info, err
  5712. }
  5713. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5714. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5715. return info, err
  5716. }
  5717. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  5718. info := models.WarehousingInfo{}
  5719. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  5720. return info, err
  5721. }
  5722. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  5723. info := models.WarehousingInfo{}
  5724. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  5725. return info, err
  5726. }
  5727. // 药品加库存
  5728. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5729. info := models.BaseDrugLib{}
  5730. 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
  5731. return info, err
  5732. }
  5733. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5734. info := models.BaseDrugLib{}
  5735. 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
  5736. 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
  5737. return info, err
  5738. }
  5739. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5740. info := models.BaseDrugLib{}
  5741. 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
  5742. return info, err
  5743. }
  5744. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5745. info := models.BaseDrugLib{}
  5746. 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
  5747. 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
  5748. return info, err
  5749. }
  5750. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  5751. info := models.BaseDrugLib{}
  5752. 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
  5753. 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
  5754. return info, err
  5755. }
  5756. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5757. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5758. return info, err
  5759. }
  5760. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5761. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5762. return info, err
  5763. }
  5764. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  5765. info := models.WarehouseOutInfo{}
  5766. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5767. return info, err
  5768. }
  5769. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  5770. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  5771. return out, err
  5772. }
  5773. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  5774. info := models.WarehousingInfo{}
  5775. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5776. return info, err
  5777. }
  5778. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  5779. err = XTReadDB().Where("status = 1").Find(&template).Error
  5780. return template, err
  5781. }
  5782. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  5783. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  5784. return good, err
  5785. }
  5786. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5787. info := models.GoodInfo{}
  5788. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5789. return info, err
  5790. }
  5791. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  5792. db := XTReadDB().Where("status = 1")
  5793. if good_id > 0 {
  5794. db = db.Where("good_id = ?", good_id)
  5795. }
  5796. err = db.Find(&info).Error
  5797. return info, err
  5798. }
  5799. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  5800. db := XTReadDB().Model(&info).Where("status = 1")
  5801. if drug_id > 0 {
  5802. db = db.Where("drug_id = ?", drug_id)
  5803. }
  5804. err = db.Find(&info).Error
  5805. return info, err
  5806. }
  5807. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5808. lib := models.BaseDrugLib{}
  5809. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5810. return lib, err
  5811. }
  5812. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  5813. info := models.Warehousing{}
  5814. 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
  5815. if err == gorm.ErrRecordNotFound {
  5816. return nil, err
  5817. }
  5818. if err != nil {
  5819. return nil, err
  5820. }
  5821. return &info, nil
  5822. }
  5823. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  5824. info := models.Warehousing{}
  5825. 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
  5826. return info, err
  5827. }
  5828. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  5829. out := models.DrugWarehouseOut{}
  5830. 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
  5831. if err == gorm.ErrRecordNotFound {
  5832. return nil, err
  5833. }
  5834. if err != nil {
  5835. return nil, err
  5836. }
  5837. return &out, nil
  5838. }
  5839. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  5840. info := models.DrugWarehouseOut{}
  5841. 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
  5842. return info, err
  5843. }
  5844. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  5845. info := models.DrugWarehouse{}
  5846. 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
  5847. if err == gorm.ErrRecordNotFound {
  5848. return nil, err
  5849. }
  5850. if err != nil {
  5851. return nil, err
  5852. }
  5853. return &info, nil
  5854. }
  5855. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  5856. err := XTWriteDB().Create(&warehouse).Error
  5857. return err
  5858. }
  5859. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  5860. info := models.DrugWarehouse{}
  5861. 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
  5862. return info, err
  5863. }
  5864. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5865. info := models.GoodInfo{}
  5866. 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
  5867. 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
  5868. return info, err
  5869. }
  5870. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5871. info := models.GoodInfo{}
  5872. 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
  5873. 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
  5874. return info, err
  5875. }
  5876. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  5877. tx := XTWriteDB().Begin()
  5878. 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
  5879. if err != nil {
  5880. tx.Rollback()
  5881. return err
  5882. }
  5883. tx.Commit()
  5884. return err
  5885. }
  5886. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  5887. // 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
  5888. // return err
  5889. //}
  5890. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  5891. 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
  5892. return err
  5893. }
  5894. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  5895. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  5896. table := XTReadDB().Table("xt_base_drug as x d")
  5897. fmt.Println(table)
  5898. if startime > 0 {
  5899. db = db.Where("x.advice_date >= ?", startime)
  5900. }
  5901. if endtime > 0 {
  5902. db = db.Where("x.advice_date <= ?", endtime)
  5903. }
  5904. if orgid > 0 {
  5905. db = db.Where("x.user_org_id = ?", orgid)
  5906. }
  5907. if len(keyword) > 0 {
  5908. likeKey := "%" + keyword + "%"
  5909. 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)
  5910. }
  5911. offset := (page - 1) * limit
  5912. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  5913. return adviceinfo, total, err
  5914. }
  5915. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  5916. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  5917. table := XTReadDB().Table("xt_base_drug as x d")
  5918. fmt.Println(table)
  5919. if startime > 0 {
  5920. db = db.Where("x.advice_date >= ?", startime)
  5921. }
  5922. if endtime > 0 {
  5923. db = db.Where("x.advice_date <= ?", endtime)
  5924. }
  5925. if orgid > 0 {
  5926. db = db.Where("x.user_org_id = ?", orgid)
  5927. }
  5928. if len(keyword) > 0 {
  5929. likeKey := "%" + keyword + "%"
  5930. 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)
  5931. }
  5932. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  5933. return adviceinfo, total, err
  5934. }
  5935. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  5936. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  5937. return drug, err
  5938. }
  5939. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  5940. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  5941. table := XTReadDB().Table("xt_good_information as d")
  5942. fmt.Println(table)
  5943. if startime > 0 {
  5944. db = db.Where("x.record_date >= ?", startime)
  5945. }
  5946. if endtime > 0 {
  5947. db = db.Where("x.record_date <= ?", endtime)
  5948. }
  5949. if orgid > 0 {
  5950. db = db.Where("x.user_org_id = ?", orgid)
  5951. }
  5952. if len(keyword) > 0 {
  5953. likeKey := "%" + keyword + "%"
  5954. 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")
  5955. }
  5956. offset := (page - 1) * limit
  5957. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  5958. return project, total, err
  5959. }
  5960. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  5961. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  5962. table := XTReadDB().Table("xt_good_information as d")
  5963. fmt.Println(table)
  5964. if startime > 0 {
  5965. db = db.Where("x.record_date >= ?", startime)
  5966. }
  5967. if endtime > 0 {
  5968. db = db.Where("x.record_date <= ?", endtime)
  5969. }
  5970. if orgid > 0 {
  5971. db = db.Where("x.user_org_id = ?", orgid)
  5972. }
  5973. if len(keyword) > 0 {
  5974. likeKey := "%" + keyword + "%"
  5975. 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")
  5976. }
  5977. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  5978. return project, total, err
  5979. }
  5980. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  5981. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  5982. return good, err
  5983. }
  5984. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  5985. setting := models.XtStockSetting{}
  5986. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  5987. if err == gorm.ErrRecordNotFound {
  5988. return nil, err
  5989. }
  5990. if err != nil {
  5991. return nil, err
  5992. }
  5993. return &setting, nil
  5994. }
  5995. func CreateStockSetting(setting models.XtStockSetting) error {
  5996. err := XTWriteDB().Create(&setting).Error
  5997. return err
  5998. }
  5999. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6000. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6001. return err
  6002. }
  6003. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6004. setting := models.XtStockSetting{}
  6005. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6006. return setting, err
  6007. }
  6008. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6009. detail := models.BloodAutomaticReduceDetail{}
  6010. 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
  6011. return detail, err
  6012. }
  6013. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6014. detail := models.WarehouseOutInfoNight{}
  6015. 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
  6016. return detail, err
  6017. }
  6018. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6019. setting := models.XtAdviceSetting{}
  6020. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6021. if err == gorm.ErrRecordNotFound {
  6022. return nil, err
  6023. }
  6024. if err != nil {
  6025. return nil, err
  6026. }
  6027. return &setting, nil
  6028. }
  6029. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6030. err := XTWriteDB().Create(&setting).Error
  6031. return err
  6032. }
  6033. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6034. 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
  6035. return err
  6036. }
  6037. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6038. setting := models.XtAdviceSetting{}
  6039. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6040. return setting, err
  6041. }
  6042. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6043. config := models.XtPrescriptionConfig{}
  6044. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6045. if err == gorm.ErrRecordNotFound {
  6046. return nil, err
  6047. }
  6048. if err != nil {
  6049. return nil, err
  6050. }
  6051. return &config, nil
  6052. }
  6053. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6054. err := XTWriteDB().Create(&prescriptionConfig).Error
  6055. return err
  6056. }
  6057. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6058. 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
  6059. return err
  6060. }
  6061. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6062. config := models.XtPrescriptionConfig{}
  6063. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6064. return config, err
  6065. }
  6066. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6067. var deliver_number int64 = 0
  6068. var stock_number int64 = 0
  6069. deliver_number = maxNumber
  6070. // 根据先进先出原则,查询最先入库的批次,进行出库
  6071. // 如果没有对应的库存,则报错
  6072. //开启事物
  6073. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6074. if err != nil {
  6075. return err
  6076. }
  6077. // 将该批次的剩余库存数量转换为拆零数量
  6078. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6079. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6080. if stock_number >= deliver_number {
  6081. var maxNumber int64 = 0
  6082. var minNumber int64 = 0
  6083. var stock_max_number int64 = 0
  6084. stock_max_number = warehouse.StockMaxNumber
  6085. maxNumber = deliver_number / drup.MinNumber
  6086. minNumber = deliver_number % drup.MinNumber
  6087. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6088. minNumber = maxNumber
  6089. }
  6090. if drup.MaxUnit != drup.MinUnit {
  6091. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6092. return errors.New("库存数量不足")
  6093. }
  6094. }
  6095. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6096. if warehouse.StockMaxNumber < 0 {
  6097. warehouse.StockMaxNumber = 0
  6098. }
  6099. if warehouse.StockMinNumber < 0 {
  6100. warehouse.StockMinNumber = 0
  6101. }
  6102. warehouse.Mtime = time.Now().Unix()
  6103. if warehouse.StockMinNumber < minNumber {
  6104. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6105. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6106. } else {
  6107. if minNumber > 0 {
  6108. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6109. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6110. } else {
  6111. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6112. }
  6113. }
  6114. if minNumber == 0 && maxNumber != 1 {
  6115. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6116. }
  6117. }
  6118. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6119. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6120. if warehouse.StockMinNumber == 0 {
  6121. warehouse.StockMaxNumber = stock_max_number
  6122. }
  6123. }
  6124. if drup.MaxUnit != drup.MinUnit {
  6125. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6126. return errors.New("库存数量不足")
  6127. }
  6128. }
  6129. if warehouse.StockMinNumber <= 0 {
  6130. warehouse.StockMinNumber = 0
  6131. }
  6132. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6133. fmt.Println(errThree)
  6134. //查询默认仓库
  6135. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6136. //查询默认仓库剩余多少库存
  6137. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6138. var sum_count int64
  6139. var sum_in_count int64
  6140. for _, it := range list {
  6141. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6142. if it.MaxUnit == baseDrug.MaxUnit {
  6143. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6144. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6145. sum_count += it.StockMaxNumber + it.StockMinNumber
  6146. sum_in_count += it.WarehousingCount
  6147. }
  6148. }
  6149. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6150. return nil
  6151. } else {
  6152. warehouse.StockMaxNumber = 0
  6153. warehouse.StockMinNumber = 0
  6154. warehouse.Mtime = time.Now().Unix()
  6155. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6156. fmt.Println(errThree)
  6157. //查询默认仓库
  6158. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6159. //查询默认仓库剩余多少库存
  6160. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6161. var sum_count int64
  6162. var sum_in_count int64
  6163. for _, it := range list {
  6164. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6165. if it.MaxUnit == baseDrug.MaxUnit {
  6166. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6167. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6168. sum_count += it.StockMaxNumber + it.StockMinNumber
  6169. sum_in_count += it.WarehousingCount
  6170. }
  6171. }
  6172. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6173. if errThree != nil {
  6174. return errThree
  6175. }
  6176. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6177. prescribingNumber_two_temp := deliver_number - stock_number
  6178. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6179. }
  6180. return
  6181. }
  6182. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6183. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6184. return err
  6185. }
  6186. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6187. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6188. return err
  6189. }
  6190. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6191. config := models.DrugOutConfig{}
  6192. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6193. if err == gorm.ErrRecordNotFound {
  6194. return nil, err
  6195. }
  6196. if err != nil {
  6197. return nil, err
  6198. }
  6199. return &config, nil
  6200. }
  6201. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6202. config := models.DrugOutConfig{}
  6203. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6204. return config, err
  6205. }
  6206. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6207. config := models.XtHisConfig{}
  6208. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6209. return config, err
  6210. }
  6211. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6212. err := XTWriteDB().Create(&config).Error
  6213. return err
  6214. }
  6215. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6216. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6217. return err
  6218. }
  6219. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6220. config := models.DrugOutConfig{}
  6221. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6222. return config, err
  6223. }
  6224. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6225. config := models.HisSettleStockConfig{}
  6226. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6227. return config, err
  6228. }
  6229. func UpdatedWarehouseOut(id int64) error {
  6230. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6231. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6232. return err
  6233. }
  6234. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6235. tx := XTWriteDB().Begin()
  6236. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6237. if err != nil {
  6238. tx.Rollback()
  6239. return err
  6240. }
  6241. tx.Commit()
  6242. return err
  6243. }
  6244. func GetLastGoodWarehouseOutInfo(orgid int64, id int64) (info []*models.WarehouseOutInfo, err error) {
  6245. err = XTReadDB().Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6246. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6247. }).Find(&info).Error
  6248. return info, err
  6249. }
  6250. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6251. info := models.WarehouseOutInfo{}
  6252. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6253. return info, err
  6254. }
  6255. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6256. 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
  6257. return err
  6258. }
  6259. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6260. 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
  6261. return err
  6262. }
  6263. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6264. out := models.WarehouseOut{}
  6265. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6266. return out, err
  6267. }
  6268. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6269. ut := writeDb.Begin()
  6270. 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
  6271. if err != nil {
  6272. ut.Rollback()
  6273. return err
  6274. }
  6275. ut.Commit()
  6276. return err
  6277. }
  6278. func ModifyGoodWarehouseOut(id int64) error {
  6279. ut := writeDb.Begin()
  6280. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6281. if err != nil {
  6282. ut.Rollback()
  6283. return err
  6284. }
  6285. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6286. if err != nil {
  6287. ut.Rollback()
  6288. return err
  6289. }
  6290. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6291. if err != nil {
  6292. ut.Rollback()
  6293. return err
  6294. }
  6295. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  6296. if err != nil {
  6297. ut.Rollback()
  6298. return err
  6299. }
  6300. ut.Commit()
  6301. return err
  6302. }
  6303. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6304. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6305. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6306. }).Find(&info).Error
  6307. return info, err
  6308. }
  6309. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  6310. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  6311. return info, err
  6312. }
  6313. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  6314. 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}).Error
  6315. return err
  6316. }
  6317. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  6318. tx := XTWriteDB().Begin()
  6319. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6320. if err != nil {
  6321. tx.Rollback()
  6322. return err
  6323. }
  6324. tx.Commit()
  6325. return err
  6326. }
  6327. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  6328. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6329. return info, err
  6330. }
  6331. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  6332. tx := XTWriteDB().Begin()
  6333. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  6334. if err != nil {
  6335. tx.Rollback()
  6336. return err
  6337. }
  6338. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6339. if err != nil {
  6340. tx.Rollback()
  6341. return err
  6342. }
  6343. tx.Commit()
  6344. return err
  6345. }
  6346. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  6347. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6348. return info, err
  6349. }
  6350. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  6351. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6352. return info, err
  6353. }
  6354. func FindLastDrugWarehousingInfoTwenty(org_id int64) (info models.DrugWarehouseInfo, err error) {
  6355. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6356. return info, err
  6357. }
  6358. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6359. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  6360. return info, err
  6361. }
  6362. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  6363. err := XTWriteDB().Create(&info).Error
  6364. return err
  6365. }
  6366. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  6367. err := XTWriteDB().Save(&info).Error
  6368. return err
  6369. }
  6370. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  6371. tx := XTWriteDB().Begin()
  6372. 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
  6373. if err != nil {
  6374. tx.Rollback()
  6375. return err
  6376. }
  6377. tx.Commit()
  6378. return err
  6379. }
  6380. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6381. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6382. return info, err
  6383. }
  6384. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  6385. tx := XTWriteDB().Begin()
  6386. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6387. if err != nil {
  6388. tx.Rollback()
  6389. return err
  6390. }
  6391. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6392. if err != nil {
  6393. tx.Rollback()
  6394. return err
  6395. }
  6396. tx.Commit()
  6397. return err
  6398. }
  6399. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  6400. tx := XTWriteDB().Begin()
  6401. 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
  6402. if err != nil {
  6403. tx.Rollback()
  6404. return err
  6405. }
  6406. tx.Commit()
  6407. return err
  6408. }
  6409. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  6410. tx := XTWriteDB().Begin()
  6411. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6412. if err != nil {
  6413. tx.Rollback()
  6414. return err
  6415. }
  6416. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6417. tx.Commit()
  6418. return err
  6419. }
  6420. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  6421. tx := XTWriteDB().Begin()
  6422. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6423. if err != nil {
  6424. tx.Rollback()
  6425. return err
  6426. }
  6427. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6428. if err != nil {
  6429. tx.Rollback()
  6430. return err
  6431. }
  6432. tx.Commit()
  6433. return err
  6434. }
  6435. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  6436. tx := XTWriteDB().Begin()
  6437. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6438. if err != nil {
  6439. tx.Rollback()
  6440. return err
  6441. }
  6442. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6443. if err != nil {
  6444. tx.Rollback()
  6445. return err
  6446. }
  6447. tx.Commit()
  6448. return err
  6449. }
  6450. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  6451. tx := XTWriteDB().Begin()
  6452. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  6453. if err != nil {
  6454. tx.Rollback()
  6455. return err
  6456. }
  6457. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6458. if err != nil {
  6459. tx.Rollback()
  6460. return err
  6461. }
  6462. tx.Commit()
  6463. return err
  6464. }
  6465. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  6466. 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}).Error
  6467. return err
  6468. }
  6469. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  6470. 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}).Error
  6471. return err
  6472. }
  6473. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  6474. out := models.DrugWarehouseOut{}
  6475. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  6476. return out, err
  6477. }
  6478. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6479. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  6480. return info, err
  6481. }
  6482. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  6483. tx := XTWriteDB().Begin()
  6484. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  6485. if err != nil {
  6486. tx.Rollback()
  6487. return err
  6488. }
  6489. err = XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6490. if err != nil {
  6491. tx.Rollback()
  6492. return err
  6493. }
  6494. tx.Commit()
  6495. return err
  6496. }
  6497. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  6498. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  6499. return err
  6500. }
  6501. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  6502. out := models.DrugWarehouseOut{}
  6503. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  6504. return out, err
  6505. }
  6506. func UpdateDrugMaxNumber(count int64, id int64) error {
  6507. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6508. return err
  6509. }
  6510. func UpdateDrugMinNumber(count int64, id int64) error {
  6511. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6512. return err
  6513. }
  6514. func DeleteDrugOutFlow(id int64, orgid int64) error {
  6515. 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
  6516. return err
  6517. }
  6518. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  6519. 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
  6520. return outInfo, err
  6521. }
  6522. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  6523. 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
  6524. return outInfo, err
  6525. }
  6526. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6527. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  6528. return info, err
  6529. }
  6530. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6531. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  6532. return info, err
  6533. }
  6534. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  6535. 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
  6536. return info, err
  6537. }
  6538. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  6539. 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
  6540. 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
  6541. return err
  6542. }
  6543. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  6544. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6545. return err
  6546. }
  6547. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  6548. tx := XTWriteDB().Begin()
  6549. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6550. if err != nil {
  6551. tx.Rollback()
  6552. return err
  6553. }
  6554. tx.Commit()
  6555. return err
  6556. }
  6557. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  6558. tx := XTWriteDB().Begin()
  6559. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6560. if err != nil {
  6561. tx.Rollback()
  6562. return err
  6563. }
  6564. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6565. if err != nil {
  6566. tx.Rollback()
  6567. return err
  6568. }
  6569. tx.Commit()
  6570. return err
  6571. }
  6572. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  6573. 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
  6574. return stock, err
  6575. }
  6576. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  6577. 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
  6578. return err
  6579. }
  6580. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6581. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6582. return info, err
  6583. }
  6584. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  6585. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6586. return info, err
  6587. }
  6588. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  6589. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6590. return info, err
  6591. }
  6592. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  6593. ut := writeDb.Begin()
  6594. err := ut.Model(&models.CancelStock{}).Where("id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6595. if err != nil {
  6596. ut.Rollback()
  6597. return err
  6598. }
  6599. err = ut.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
  6600. if err != nil {
  6601. ut.Rollback()
  6602. return err
  6603. }
  6604. ut.Commit()
  6605. return err
  6606. }
  6607. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6608. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  6609. return info, err
  6610. }
  6611. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  6612. ut := writeDb.Begin()
  6613. err := ut.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
  6614. if err != nil {
  6615. ut.Rollback()
  6616. return err
  6617. }
  6618. ut.Commit()
  6619. return err
  6620. }
  6621. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  6622. ut := writeDb.Begin()
  6623. err := ut.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6624. if err != nil {
  6625. ut.Rollback()
  6626. return err
  6627. }
  6628. ut.Commit()
  6629. return err
  6630. }
  6631. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  6632. ut := writeDb.Begin()
  6633. err := ut.Model(&models.CancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6634. if err != nil {
  6635. ut.Rollback()
  6636. return err
  6637. }
  6638. err = ut.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
  6639. if err != nil {
  6640. ut.Rollback()
  6641. return err
  6642. }
  6643. ut.Commit()
  6644. return err
  6645. }
  6646. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  6647. ut := writeDb.Begin()
  6648. err = ut.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
  6649. if err != nil {
  6650. ut.Rollback()
  6651. return err
  6652. }
  6653. ut.Commit()
  6654. return err
  6655. }
  6656. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  6657. ut := writeDb.Begin()
  6658. 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
  6659. if err != nil {
  6660. ut.Rollback()
  6661. return err
  6662. }
  6663. 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
  6664. if err != nil {
  6665. ut.Rollback()
  6666. return err
  6667. }
  6668. ut.Commit()
  6669. return err
  6670. }
  6671. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6672. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6673. 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
  6674. return err
  6675. }
  6676. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6677. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  6678. return info, err
  6679. }
  6680. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  6681. 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
  6682. 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
  6683. return err
  6684. }
  6685. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  6686. 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
  6687. return info, err
  6688. }
  6689. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  6690. 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
  6691. if err != gorm.ErrRecordNotFound {
  6692. if err != nil {
  6693. tx.Rollback()
  6694. return info, err
  6695. }
  6696. }
  6697. return info, err
  6698. }
  6699. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  6700. ut := XTWriteDB().Begin()
  6701. err := ut.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6702. if err != nil {
  6703. ut.Rollback()
  6704. return err
  6705. }
  6706. err = ut.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6707. if err != nil {
  6708. ut.Rollback()
  6709. return err
  6710. }
  6711. ut.Commit()
  6712. return err
  6713. }
  6714. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  6715. ut := writeDb.Begin()
  6716. err = ut.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6717. if err != nil {
  6718. ut.Rollback()
  6719. return err
  6720. }
  6721. ut.Commit()
  6722. return err
  6723. }
  6724. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  6725. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6726. if err != nil {
  6727. tx.Rollback()
  6728. return err
  6729. }
  6730. return err
  6731. }
  6732. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6733. 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
  6734. return info, err
  6735. }
  6736. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6737. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6738. return info, err
  6739. }
  6740. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  6741. 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
  6742. 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
  6743. return err
  6744. }
  6745. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  6746. stock := models.DrugCancelStock{}
  6747. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  6748. return stock, err
  6749. }
  6750. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6751. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  6752. return stock, err
  6753. }
  6754. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6755. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  6756. return stock, err
  6757. }
  6758. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6759. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  6760. return info, err
  6761. }
  6762. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  6763. 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
  6764. return err
  6765. }
  6766. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6767. 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
  6768. if err != nil {
  6769. tx.Rollback()
  6770. return err
  6771. }
  6772. return err
  6773. }
  6774. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  6775. 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
  6776. return err
  6777. }
  6778. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6779. 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
  6780. if err != nil {
  6781. tx.Rollback()
  6782. return err
  6783. }
  6784. return err
  6785. }
  6786. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  6787. 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
  6788. return err
  6789. }
  6790. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  6791. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6792. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6793. return err
  6794. }
  6795. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6796. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  6797. return info, err
  6798. }
  6799. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  6800. 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
  6801. return err
  6802. }
  6803. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6804. ut := XTWriteDB().Begin()
  6805. err := ut.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6806. if err != nil {
  6807. ut.Rollback()
  6808. return err
  6809. }
  6810. ut.Commit()
  6811. return err
  6812. }
  6813. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6814. 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
  6815. return err
  6816. }
  6817. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  6818. info := models.DrugWarehouseInfo{}
  6819. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  6820. return info, err
  6821. }
  6822. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  6823. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  6824. if storehouse_id > 0 {
  6825. db = db.Where("storehouse_id = ?", storehouse_id)
  6826. }
  6827. if orgid > 0 {
  6828. db = db.Where("user_org_id = ?", orgid)
  6829. }
  6830. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  6831. return storeinventory, err
  6832. }
  6833. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  6834. detail := models.AutomaticReduceDetail{}
  6835. 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
  6836. return detail, err
  6837. }
  6838. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  6839. detail := models.WarehouseOutInfo{}
  6840. 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
  6841. return detail, err
  6842. }
  6843. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  6844. stock := models.XtGoodStockCount{}
  6845. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  6846. return stock, err
  6847. }
  6848. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  6849. err := XTWriteDB().Create(&stock).Error
  6850. return err
  6851. }
  6852. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6853. ut := XTWriteDB().Begin()
  6854. 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
  6855. if err != nil {
  6856. ut.Rollback()
  6857. return err
  6858. }
  6859. ut.Commit()
  6860. return err
  6861. }
  6862. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6863. ut := XTWriteDB().Begin()
  6864. 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
  6865. if err != nil {
  6866. ut.Rollback()
  6867. return err
  6868. }
  6869. ut.Commit()
  6870. return err
  6871. }
  6872. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  6873. ut := XTWriteDB().Begin()
  6874. 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).Update(map[string]interface{}{"flush_count": flush_count}).Error
  6875. if err != nil {
  6876. ut.Rollback()
  6877. return err
  6878. }
  6879. ut.Commit()
  6880. return err
  6881. }
  6882. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  6883. 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
  6884. if err != nil {
  6885. tx.Rollback()
  6886. return err
  6887. }
  6888. return err
  6889. }
  6890. func CreateDrugStock(stock models.XtDrugStockCount) error {
  6891. err := XTWriteDB().Create(&stock).Error
  6892. return err
  6893. }
  6894. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  6895. ut := XTWriteDB().Begin()
  6896. 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
  6897. if err != nil {
  6898. ut.Rollback()
  6899. return err
  6900. }
  6901. 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
  6902. if err != nil {
  6903. ut.Rollback()
  6904. return err
  6905. }
  6906. ut.Commit()
  6907. return err
  6908. }
  6909. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  6910. ut := XTWriteDB().Begin()
  6911. 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
  6912. if err != nil {
  6913. ut.Rollback()
  6914. return err
  6915. }
  6916. ut.Commit()
  6917. return err
  6918. }
  6919. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  6920. ut := XTWriteDB().Begin()
  6921. 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
  6922. if err != nil {
  6923. ut.Rollback()
  6924. return err
  6925. }
  6926. 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
  6927. if err != nil {
  6928. ut.Rollback()
  6929. return err
  6930. }
  6931. ut.Commit()
  6932. return err
  6933. }
  6934. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  6935. ut := XTWriteDB().Begin()
  6936. 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
  6937. if err != nil {
  6938. ut.Rollback()
  6939. return err
  6940. }
  6941. ut.Commit()
  6942. return err
  6943. }
  6944. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6945. ut := XTWriteDB().Begin()
  6946. 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
  6947. if err != nil {
  6948. ut.Rollback()
  6949. return err
  6950. }
  6951. 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
  6952. if err != nil {
  6953. ut.Rollback()
  6954. return err
  6955. }
  6956. ut.Commit()
  6957. return err
  6958. }
  6959. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6960. ut := XTWriteDB().Begin()
  6961. 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
  6962. if err != nil {
  6963. ut.Rollback()
  6964. return err
  6965. }
  6966. 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
  6967. if err != nil {
  6968. ut.Rollback()
  6969. return err
  6970. }
  6971. ut.Commit()
  6972. return err
  6973. }
  6974. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  6975. if orgid == 10191 {
  6976. 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
  6977. return info, err
  6978. } else {
  6979. 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
  6980. return info, err
  6981. }
  6982. }
  6983. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  6984. config := models.HisSettleStockConfig{}
  6985. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  6986. if err == gorm.ErrRecordNotFound {
  6987. return nil, err
  6988. }
  6989. if err != nil {
  6990. return nil, err
  6991. }
  6992. return &config, nil
  6993. }
  6994. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  6995. err := XTWriteDB().Create(&config).Error
  6996. return err
  6997. }
  6998. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  6999. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  7000. return err
  7001. }
  7002. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  7003. config := models.DrugStockConfig{}
  7004. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7005. return config, err
  7006. }
  7007. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  7008. stockConfig := models.PharmacyConfig{}
  7009. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7010. return stockConfig, err
  7011. }
  7012. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  7013. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7014. return err
  7015. }
  7016. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  7017. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7018. return err
  7019. }
  7020. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  7021. config := models.HisSettleStockConfig{}
  7022. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7023. return config, nil
  7024. }
  7025. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  7026. info := models.GoodInformationFourty{}
  7027. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  7028. return info, err
  7029. }
  7030. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  7031. advice := models.HisDoctorAdvice{}
  7032. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7033. return advice, err
  7034. }
  7035. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  7036. advice := models.DoctorAdvice{}
  7037. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7038. return advice, err
  7039. }
  7040. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  7041. out := models.WarehouseOut{}
  7042. 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
  7043. return out, err
  7044. }
  7045. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  7046. config := models.XtGoodOutConfig{}
  7047. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7048. if err == gorm.ErrRecordNotFound {
  7049. return nil, err
  7050. }
  7051. if err != nil {
  7052. return nil, err
  7053. }
  7054. return &config, nil
  7055. }
  7056. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  7057. err := XTWriteDB().Create(&good).Error
  7058. return err
  7059. }
  7060. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  7061. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  7062. return err
  7063. }
  7064. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  7065. config := models.XtGoodOutConfig{}
  7066. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7067. return config, err
  7068. }
  7069. func UpdateGoodAutoConfig(user_org_id int64) error {
  7070. gobalConfig := models.GobalConfig{}
  7071. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7072. return err
  7073. }
  7074. func UpdateGoodOutConfig(user_org_id int64) error {
  7075. config := models.XtGoodOutConfig{}
  7076. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7077. return err
  7078. }
  7079. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  7080. 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
  7081. return info, err
  7082. }
  7083. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  7084. 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
  7085. return good, err
  7086. }
  7087. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  7088. 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
  7089. return err
  7090. }
  7091. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7092. 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
  7093. return err
  7094. }
  7095. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7096. 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
  7097. return err
  7098. }
  7099. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7100. out := models.WarehouseOut{}
  7101. 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
  7102. return out, err
  7103. }
  7104. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7105. out := models.WarehouseOut{}
  7106. 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
  7107. if err != gorm.ErrRecordNotFound {
  7108. if err != nil {
  7109. tx.Rollback()
  7110. return out, err
  7111. }
  7112. }
  7113. return out, err
  7114. }
  7115. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7116. out := models.WarehouseOut{}
  7117. 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
  7118. return out, err
  7119. }
  7120. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7121. out := models.WarehouseOut{}
  7122. 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
  7123. if err != gorm.ErrRecordNotFound {
  7124. if err != nil {
  7125. tx.Rollback()
  7126. return out, err
  7127. }
  7128. }
  7129. return out, err
  7130. }
  7131. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  7132. goodsType := models.GoodsType{}
  7133. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  7134. return goodsType, err
  7135. }
  7136. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  7137. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  7138. return info, err
  7139. }
  7140. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  7141. ut := writeDb.Begin()
  7142. 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
  7143. if err != nil {
  7144. ut.Rollback()
  7145. return err
  7146. }
  7147. ut.Commit()
  7148. return err
  7149. }
  7150. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  7151. 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
  7152. if err != nil {
  7153. tx.Rollback()
  7154. return err
  7155. }
  7156. return err
  7157. }
  7158. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  7159. info := models.WarehouseOutInfo{}
  7160. var err error
  7161. 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
  7162. if err == gorm.ErrRecordNotFound {
  7163. return nil, err
  7164. }
  7165. if err != nil {
  7166. return nil, err
  7167. }
  7168. return &info, nil
  7169. }
  7170. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  7171. info := models.WarehouseOutInfo{}
  7172. 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
  7173. return info, err
  7174. }
  7175. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7176. info := models.WarehouseOutInfo{}
  7177. 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
  7178. if err != gorm.ErrRecordNotFound {
  7179. if err != nil {
  7180. tx.Rollback()
  7181. return info, err
  7182. }
  7183. }
  7184. return info, err
  7185. }
  7186. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  7187. info := models.WarehouseOutInfo{}
  7188. 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
  7189. return info, err
  7190. }
  7191. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7192. info := models.WarehouseOutInfo{}
  7193. 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
  7194. if err != gorm.ErrRecordNotFound {
  7195. if err != nil {
  7196. return info, err
  7197. }
  7198. }
  7199. return info, err
  7200. }
  7201. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  7202. info := models.WarehouseOutInfo{}
  7203. var err error
  7204. 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
  7205. if err == gorm.ErrRecordNotFound {
  7206. return nil, err
  7207. }
  7208. if err != nil {
  7209. return nil, err
  7210. }
  7211. return &info, nil
  7212. }
  7213. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  7214. info := models.WarehouseOutInfo{}
  7215. var err error
  7216. 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
  7217. if err != gorm.ErrRecordNotFound {
  7218. if err != nil {
  7219. tx.Rollback()
  7220. return &info, err
  7221. }
  7222. }
  7223. return &info, err
  7224. }
  7225. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  7226. db := XTReadDB().Model(&info).Where("status = 1")
  7227. if startime > 0 {
  7228. db = db.Where("ctime>=?", startime)
  7229. }
  7230. if endtime > 0 {
  7231. db = db.Where("ctime<=?", endtime)
  7232. }
  7233. if orgid > 0 {
  7234. db = db.Where("org_id = ?", orgid)
  7235. }
  7236. if len(good_id) > 0 {
  7237. db = db.Where("good_id in(?)", good_id)
  7238. }
  7239. if storehouse_id > 0 {
  7240. db = db.Where("storehouse_id = ?", storehouse_id)
  7241. }
  7242. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  7243. return info, err
  7244. }
  7245. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  7246. db := XTReadDB().Model(&info).Where("status= 1")
  7247. if start_time > 0 {
  7248. db = db.Where("ctime>=?", start_time)
  7249. }
  7250. if end_time > 0 {
  7251. db = db.Where("ctime <=?", end_time)
  7252. }
  7253. if orgid > 0 {
  7254. db = db.Where("org_id = ?", orgid)
  7255. }
  7256. if len(good_id) > 0 {
  7257. db = db.Where("good_id in(?)", good_id)
  7258. }
  7259. if storehouse_id > 0 {
  7260. db = db.Where("storehouse_id = ?", storehouse_id)
  7261. }
  7262. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  7263. return info, err
  7264. }
  7265. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  7266. config := models.XtGoodOutConfig{}
  7267. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7268. return config, err
  7269. }
  7270. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  7271. 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
  7272. 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
  7273. return err
  7274. }
  7275. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  7276. tx := XTWriteDB().Begin()
  7277. for _, item := range list {
  7278. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  7279. if err != nil {
  7280. tx.Rollback()
  7281. return err
  7282. }
  7283. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  7284. if err != nil {
  7285. tx.Rollback()
  7286. return err
  7287. }
  7288. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7289. if err != nil {
  7290. tx.Rollback()
  7291. return err
  7292. }
  7293. var goodList []*models.WarehousingInfo
  7294. 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
  7295. var flush_count int64
  7296. for _, it := range goodList {
  7297. flush_count += it.StockCount
  7298. }
  7299. 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
  7300. if err != nil {
  7301. tx.Rollback()
  7302. return err
  7303. }
  7304. 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
  7305. if err != nil {
  7306. tx.Rollback()
  7307. return err
  7308. }
  7309. storeHouseConfig := models.XtStorehouseConfig{}
  7310. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  7311. var stockList []*models.WarehousingInfo
  7312. //查询该耗材剩余多少库存
  7313. 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
  7314. var sum_count int64
  7315. var sum_in_count int64
  7316. for _, it := range stockList {
  7317. sum_count += it.StockCount
  7318. sum_in_count += it.WarehousingCount
  7319. }
  7320. 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
  7321. if err != nil {
  7322. tx.Rollback()
  7323. return err
  7324. }
  7325. 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
  7326. if err != nil {
  7327. tx.Rollback()
  7328. return err
  7329. }
  7330. }
  7331. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7332. if err != nil {
  7333. tx.Rollback()
  7334. return err
  7335. }
  7336. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7337. tx.Commit()
  7338. return err
  7339. }
  7340. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  7341. if storehouse_id == 0 {
  7342. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  7343. }
  7344. if storehouse_id > 0 {
  7345. 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
  7346. }
  7347. return info, err
  7348. }
  7349. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  7350. 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
  7351. return list, err
  7352. }
  7353. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  7354. 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
  7355. return infor, err
  7356. }
  7357. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  7358. info := models.WarehouseOutInfo{}
  7359. 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
  7360. return info, err
  7361. }
  7362. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  7363. info := models.WarehousingInfo{}
  7364. 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
  7365. return info, err
  7366. }
  7367. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  7368. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  7369. return list, err
  7370. }
  7371. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  7372. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  7373. return list, err
  7374. }
  7375. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  7376. templateL := models.HisPrescriptionProjectTemplateL{}
  7377. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  7378. return templateL, err
  7379. }