stock_service.go 460KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "github.com/jinzhu/gorm"
  10. )
  11. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  12. err := writeDb.Create(&dealer).Error
  13. return err, dealer
  14. }
  15. func ModifyDealer(dealer *models.Dealer) error {
  16. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  17. "dealer_name": dealer.DealerName,
  18. "contact": dealer.Contact,
  19. "contact_phone": dealer.ContactPhone,
  20. "platform_number": dealer.PlatformNumber,
  21. "email": dealer.Email,
  22. "contact_address": dealer.ContactAddress,
  23. "modifier": dealer.Modifier,
  24. "mtime": time.Now().Unix(),
  25. "remark": dealer.Remark,
  26. "wubi": dealer.WuBi,
  27. "pinyin": dealer.PinYin,
  28. }).Error
  29. return err
  30. }
  31. func FindAllDealerTotal(orgId int64) (total int64) {
  32. db := readDb.Model(&models.Dealer{})
  33. db = db.Where("org_id = ? ", orgId).Count(&total)
  34. return
  35. }
  36. // 获取经销商的名称
  37. func FindAllDealerName(orgId int64) (tmp []*models.Dealer, err error) {
  38. err = readDb.Model(&models.Dealer{}).Where("org_id = ? and status = 1", orgId).Find(&tmp).Error
  39. return
  40. }
  41. func FindAllDealerList(orgId int64, page int64, limit, key int64) (list []*models.Dealer, total int64, err error) {
  42. offset := (page - 1) * limit
  43. db := readDb.Model(&models.Dealer{})
  44. db = db.Where("org_id = ? AND status = 1", orgId)
  45. if key != 0 {
  46. db = db.Where("id = ? ", key)
  47. offset = 0
  48. limit = 10
  49. }
  50. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  51. return
  52. }
  53. func DeleteDealerById(id int64) error {
  54. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  55. return err
  56. }
  57. func FindDealerById(id int64) (*models.Dealer, error) {
  58. dealer := &models.Dealer{}
  59. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  60. return dealer, err
  61. }
  62. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  63. err := writeDb.Create(&manufacturer).Error
  64. return err, manufacturer
  65. }
  66. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  67. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  68. "manufacturer_name": manufacturer.ManufacturerName,
  69. "contact": manufacturer.Contact,
  70. "contact_phone": manufacturer.ContactPhone,
  71. "platform_number": manufacturer.PlatformNumber,
  72. "email": manufacturer.Email,
  73. "contact_address": manufacturer.ContactAddress,
  74. "modifier": manufacturer.Modifier,
  75. "mtime": time.Now().Unix(),
  76. "remark": manufacturer.Remark,
  77. "wubi": manufacturer.WuBi,
  78. "pinyin": manufacturer.PinYin,
  79. }).Error
  80. return err
  81. }
  82. // 获取生产厂商名称
  83. func FindAllManufacturerName(orgid int64) (tmp []*models.Manufacturer, err error) {
  84. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? and status = 1", orgid).Find(&tmp).Error
  85. return
  86. }
  87. func FindAllManufacturerList(orgId int64, page int64, limit, key int64) (list []*models.Manufacturer, total int64, err error) {
  88. offset := (page - 1) * limit
  89. db := readDb.Model(&models.Manufacturer{})
  90. db = db.Where("org_id = ? AND status = 1", orgId)
  91. if key != 0 {
  92. db = db.Where("id = ?", key)
  93. offset = 0
  94. limit = 10
  95. }
  96. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  97. return
  98. }
  99. func DeleteManufacturerById(id int64) error {
  100. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  101. return err
  102. }
  103. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  104. dealer := &models.Manufacturer{}
  105. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  106. return dealer, err
  107. }
  108. func FindManufacturerTotal(orgId int64) (total int64) {
  109. db := readDb.Model(&models.Manufacturer{})
  110. db = db.Where("org_id = ?", orgId).Count(&total)
  111. return
  112. }
  113. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  114. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  115. return dealers, err
  116. }
  117. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  118. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  119. return manufacturer, err
  120. }
  121. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  122. db := readDb.Model(&models.GoodsType{})
  123. db = db.Where("org_id = ?", orgId).Count(&total)
  124. return
  125. }
  126. func GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  127. goodsType := models.GoodsType{}
  128. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  129. if err == gorm.ErrRecordNotFound {
  130. return nil, err
  131. }
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &goodsType, nil
  136. }
  137. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  138. goodsType := models.GoodsType{}
  139. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  140. if err == gorm.ErrRecordNotFound {
  141. return nil, err
  142. }
  143. if err != nil {
  144. return nil, err
  145. }
  146. return &goodsType, nil
  147. }
  148. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  149. err := writeDb.Create(&goodType).Error
  150. return err, goodType
  151. }
  152. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  153. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  154. "modifier": goodType.Modifier,
  155. "mtime": time.Now().Unix(),
  156. "remark": goodType.Remark,
  157. "type_name": goodType.TypeName,
  158. "out_stock": goodType.OutStock,
  159. "stock_attribute": goodType.StockAttribute,
  160. }).Error
  161. return err, goodType
  162. }
  163. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  164. offset := (page - 1) * limit
  165. db := readDb.Model(&models.GoodsType{})
  166. db = db.Where("org_id=? AND status = 1", orgId)
  167. if len(keyword) > 0 {
  168. likeKey := "%" + keyword + "%"
  169. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  170. }
  171. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  172. return
  173. }
  174. func DeleteGoodTypeById(id int64, operateId int64) error {
  175. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  176. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  177. return err
  178. }
  179. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  180. goodType := &models.GoodsType{}
  181. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  182. return goodType, err
  183. }
  184. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  185. db := readDb.Model(&models.GoodInfo{})
  186. db = db.Where("org_id = ?", orgId).Count(&total)
  187. return
  188. }
  189. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  190. err := writeDb.Create(&goodInfo).Error
  191. return err, goodInfo
  192. }
  193. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  194. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  195. "good_type_id": goodInfo.GoodTypeId,
  196. "modifier": goodInfo.Modifier,
  197. "mtime": time.Now().Unix(),
  198. "remark": goodInfo.Remark,
  199. "specification_name": goodInfo.SpecificationName,
  200. "good_unit": goodInfo.GoodUnit,
  201. "buy_price": goodInfo.BuyPrice,
  202. "sell_price": goodInfo.SellPrice,
  203. "manufacturer": goodInfo.Manufacturer,
  204. "dealer": goodInfo.Dealer,
  205. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  206. "stock_warn_count": goodInfo.StockWarnCount,
  207. "is_reuse": goodInfo.IsReuse,
  208. "good_name": goodInfo.GoodName,
  209. "pinyin": goodInfo.Pinyin,
  210. "wubi": goodInfo.Wubi,
  211. "good_kind": goodInfo.GoodKind,
  212. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  213. "retail_price": goodInfo.PackingPrice,
  214. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  215. "is_special_diseases": goodInfo.IsSpecialDiseases,
  216. "is_record": goodInfo.IsRecord,
  217. "statistics_category": goodInfo.StatisticsCategory,
  218. "good_status": goodInfo.GoodStatus,
  219. "default_count": goodInfo.DefaultCount,
  220. "sign": goodInfo.Sign,
  221. "is_default": goodInfo.IsDefault,
  222. "is_charge_use": goodInfo.IsChargeUse,
  223. "is_charge_predict": goodInfo.IsChargePredict,
  224. "is_statistics_work": goodInfo.IsStatisticsWork,
  225. "sort": goodInfo.Sort,
  226. "is_doctor_use": goodInfo.IsDoctorUse,
  227. "agent": goodInfo.Agent,
  228. "good_number": goodInfo.GoodNumber,
  229. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  230. "special_medical": goodInfo.SpecialMedical,
  231. "production_type": goodInfo.ProductionType,
  232. "min_number": goodInfo.MinNumber,
  233. "packing_unit": goodInfo.PackingUnit,
  234. "packing_price": goodInfo.PackingPrice,
  235. "default_count_unit": goodInfo.DefaultCountUnit,
  236. "min_unit": goodInfo.MinUnit,
  237. "register_number": goodInfo.RegisterNumber,
  238. "provinces_code": goodInfo.ProvincesCode,
  239. "is_user": goodInfo.IsUser,
  240. "number": goodInfo.Number,
  241. "is_warehouse": goodInfo.IsWarehouse,
  242. "bbx01": goodInfo.Bbx01,
  243. "bby01": goodInfo.Bby01,
  244. "zuobiao_id": goodInfo.Bbx01,
  245. "first_letter": goodInfo.FirstLetter,
  246. "is_show": goodInfo.IsShow,
  247. "is_zero_flag": goodInfo.IsZeroFlag,
  248. "split_of_number": goodInfo.SplitOfNumber,
  249. "register_number_name": goodInfo.RegisterNumberName,
  250. }).Error
  251. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  254. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  255. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  256. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  257. return err, goodInfo
  258. }
  259. 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) {
  260. offset := (page - 1) * limit
  261. db := readDb.Model(&models.GoodInfo{})
  262. db = db.Where("org_id = ? AND status = 1", orgId)
  263. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  264. if len(keyword) > 0 {
  265. likeKey := "%" + keyword + "%"
  266. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  267. if is_use > 0 {
  268. if is_use == 2 {
  269. db = db.Where("find_in_set('停用',good_status) = 0")
  270. } else {
  271. db = db.Where("find_in_set('停用',good_status) > 0")
  272. }
  273. }
  274. if is_charge > 0 {
  275. if is_charge == 1 {
  276. db = db.Where("find_in_set('收费',good_status) > 0")
  277. } else {
  278. db = db.Where("find_in_set('收费',good_status) = 0")
  279. }
  280. }
  281. if good_kind > 0 {
  282. db = db.Where(" good_kind = ?", good_kind)
  283. }
  284. if is_mark > 0 {
  285. db = db.Where("is_mark = ?", is_mark)
  286. }
  287. if manufacturer > 0 {
  288. db = db.Where("manufacturer = ?", manufacturer)
  289. }
  290. if is_type > 0 {
  291. db = db.Where("good_type_id = ?", is_type)
  292. }
  293. } else {
  294. if is_use > 0 {
  295. if is_use == 2 {
  296. db = db.Where("find_in_set('停用',good_status) = 0")
  297. } else {
  298. db = db.Where("find_in_set('停用',good_status) > 0")
  299. }
  300. }
  301. if is_charge > 0 {
  302. if is_charge == 1 {
  303. db = db.Where("find_in_set('收费',good_status) > 0")
  304. } else {
  305. db = db.Where("find_in_set('收费',good_status) = 0")
  306. }
  307. }
  308. if good_kind > 0 {
  309. db = db.Where(" good_kind = ?", good_kind)
  310. }
  311. if is_mark > 0 {
  312. db = db.Where("is_mark = ?", is_mark)
  313. }
  314. if manufacturer > 0 {
  315. db = db.Where("manufacturer = ?", manufacturer)
  316. }
  317. if is_type > 0 {
  318. db = db.Where("good_type_id = ?", is_type)
  319. }
  320. }
  321. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  322. return
  323. }
  324. func DeleteGoodInfoById(id int64, operateId int64) error {
  325. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  326. return err
  327. }
  328. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  329. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  330. return list, err
  331. }
  332. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  333. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  334. return list, err
  335. }
  336. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  337. goodInfo := &models.GoodInfo{}
  338. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  339. return goodInfo, err
  340. }
  341. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  342. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  343. return goodType, err
  344. }
  345. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  346. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  347. return goodType, err
  348. }
  349. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  350. 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
  351. return goodInfo, err
  352. }
  353. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  354. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  355. return goodInfo, err
  356. }
  357. func FindAllGoodInfoThree(org_id int64) (goodInfo []*models.XtGoodInformationFourty, err error) {
  358. err = XTReadDB().Model(&models.XtGoodInformationFourty{}).Where("org_id = ? AND status = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  359. return goodInfo, err
  360. }
  361. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  362. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  363. return total, err
  364. }
  365. func FindNewAllWarehouseTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  366. err = tx.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  367. if err != gorm.ErrRecordNotFound {
  368. if err != nil {
  369. tx.Rollback()
  370. return total, err
  371. }
  372. }
  373. return total, err
  374. }
  375. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  376. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  377. return total, err
  378. }
  379. func FindNewAllWarehouseTotalOne(org_id int64, tx *gorm.DB) (total int64, err error) {
  380. err = tx.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  381. if err != gorm.ErrRecordNotFound {
  382. if err != nil {
  383. tx.Rollback()
  384. return total, err
  385. }
  386. }
  387. return total, err
  388. }
  389. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  390. err := writeDb.Create(&warehouse).Error
  391. return err
  392. }
  393. func AddNewSigleWarehouse(warehouse *models.Warehousing, tx *gorm.DB) error {
  394. err := writeDb.Create(&warehouse).Error
  395. if err != nil {
  396. tx.Rollback()
  397. return err
  398. }
  399. return err
  400. }
  401. func UpdateWarehouse(id int64, storehouse_id int64) error {
  402. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  403. return err
  404. }
  405. func UpdateNewWarehouse(id int64, storehouse_id int64, tx *gorm.DB) error {
  406. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  407. if err != nil {
  408. tx.Rollback()
  409. return err
  410. }
  411. return err
  412. }
  413. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  414. err := writeDb.Create(&warehouse).Error
  415. return err
  416. }
  417. func AddNewSigleDrugWarehouse(warehouse *models.DrugWarehouse, tx *gorm.DB) error {
  418. err := tx.Create(&warehouse).Error
  419. if err != nil {
  420. tx.Rollback()
  421. return err
  422. }
  423. return err
  424. }
  425. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  426. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  427. return err
  428. }
  429. func UpdateNewSigleDrugWarehouse(id int64, storehosue_id int64, tx *gorm.DB) error {
  430. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  431. if err != nil {
  432. tx.Rollback()
  433. return err
  434. }
  435. return err
  436. }
  437. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  438. err := writeDb.Create(&salesReturn).Error
  439. return err
  440. }
  441. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  442. err := writeDb.Create(&salesReturn).Error
  443. return err
  444. }
  445. func UpDateGoodReturnStatus(ids []string) {
  446. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  447. }
  448. func UpDateCanCelStockStatus(ids []string) {
  449. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  450. }
  451. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  452. if len(warehousingInfo) > 0 {
  453. utx := writeDb.Begin()
  454. if len(warehousingInfo) > 0 {
  455. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number,packing_price,storehouse_id,good_code) VALUES "
  456. insertParams := make([]string, 0)
  457. insertData := make([]interface{}, 0)
  458. for _, info := range warehousingInfo {
  459. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  460. insertData = append(insertData, info.WarehousingId)
  461. insertData = append(insertData, info.GoodId)
  462. insertData = append(insertData, info.GoodTypeId)
  463. insertData = append(insertData, info.Number)
  464. insertData = append(insertData, info.ProductDate)
  465. insertData = append(insertData, info.ExpiryDate)
  466. insertData = append(insertData, info.WarehousingCount)
  467. insertData = append(insertData, info.Price)
  468. insertData = append(insertData, info.TotalPrice)
  469. insertData = append(insertData, info.Dealer)
  470. insertData = append(insertData, info.Manufacturer)
  471. insertData = append(insertData, info.Remark)
  472. insertData = append(insertData, info.Ctime)
  473. insertData = append(insertData, info.Mtime)
  474. insertData = append(insertData, info.Status)
  475. insertData = append(insertData, info.OrgId)
  476. insertData = append(insertData, info.WarehousingOrder)
  477. insertData = append(insertData, info.Type)
  478. insertData = append(insertData, info.WarehousingCount)
  479. insertData = append(insertData, info.LicenseNumber)
  480. insertData = append(insertData, info.PackingPrice)
  481. insertData = append(insertData, info.StorehouseId)
  482. insertData = append(insertData, info.GoodCode)
  483. }
  484. thisSQL += strings.Join(insertParams, ", ")
  485. err = utx.Exec(thisSQL, insertData...).Error
  486. if err != nil {
  487. utx.Rollback()
  488. return
  489. }
  490. }
  491. utx.Commit()
  492. }
  493. return
  494. }
  495. func CreateNewWarehousingInfo(warehousingInfo []*models.WarehousingInfo, tx *gorm.DB) (err error) {
  496. if len(warehousingInfo) > 0 {
  497. if len(warehousingInfo) > 0 {
  498. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number,packing_price,storehouse_id,good_code) VALUES "
  499. insertParams := make([]string, 0)
  500. insertData := make([]interface{}, 0)
  501. for _, info := range warehousingInfo {
  502. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  503. insertData = append(insertData, info.WarehousingId)
  504. insertData = append(insertData, info.GoodId)
  505. insertData = append(insertData, info.GoodTypeId)
  506. insertData = append(insertData, info.Number)
  507. insertData = append(insertData, info.ProductDate)
  508. insertData = append(insertData, info.ExpiryDate)
  509. insertData = append(insertData, info.WarehousingCount)
  510. insertData = append(insertData, info.Price)
  511. insertData = append(insertData, info.TotalPrice)
  512. insertData = append(insertData, info.Dealer)
  513. insertData = append(insertData, info.Manufacturer)
  514. insertData = append(insertData, info.Remark)
  515. insertData = append(insertData, info.Ctime)
  516. insertData = append(insertData, info.Mtime)
  517. insertData = append(insertData, info.Status)
  518. insertData = append(insertData, info.OrgId)
  519. insertData = append(insertData, info.WarehousingOrder)
  520. insertData = append(insertData, info.Type)
  521. insertData = append(insertData, info.WarehousingCount)
  522. insertData = append(insertData, info.LicenseNumber)
  523. insertData = append(insertData, info.PackingPrice)
  524. insertData = append(insertData, info.StorehouseId)
  525. insertData = append(insertData, info.GoodCode)
  526. }
  527. thisSQL += strings.Join(insertParams, ", ")
  528. err = tx.Exec(thisSQL, insertData...).Error
  529. if err != nil {
  530. tx.Rollback()
  531. return
  532. }
  533. }
  534. }
  535. return
  536. }
  537. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  538. if len(warehousingInfo) > 0 {
  539. utx := writeDb.Begin()
  540. if len(warehousingInfo) > 0 {
  541. 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 "
  542. insertParams := make([]string, 0)
  543. insertData := make([]interface{}, 0)
  544. for _, info := range warehousingInfo {
  545. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  546. insertData = append(insertData, info.WarehousingId)
  547. insertData = append(insertData, info.DrugId)
  548. insertData = append(insertData, info.Number)
  549. insertData = append(insertData, info.ProductDate)
  550. insertData = append(insertData, info.ExpiryDate)
  551. insertData = append(insertData, info.WarehousingCount)
  552. insertData = append(insertData, info.Price)
  553. insertData = append(insertData, info.TotalPrice)
  554. insertData = append(insertData, info.Dealer)
  555. insertData = append(insertData, info.Manufacturer)
  556. insertData = append(insertData, info.Remark)
  557. insertData = append(insertData, info.Ctime)
  558. insertData = append(insertData, info.Mtime)
  559. insertData = append(insertData, info.Status)
  560. insertData = append(insertData, info.OrgId)
  561. insertData = append(insertData, info.WarehousingOrder)
  562. insertData = append(insertData, info.Type)
  563. insertData = append(insertData, info.RetailPrice)
  564. insertData = append(insertData, info.RetailTotalPrice)
  565. insertData = append(insertData, info.StockMaxNumber)
  566. insertData = append(insertData, info.MaxUnit)
  567. insertData = append(insertData, info.MinUnit)
  568. insertData = append(insertData, info.BatchNumber)
  569. insertData = append(insertData, info.StockMinNumber)
  570. insertData = append(insertData, info.StorehouseId)
  571. }
  572. thisSQL += strings.Join(insertParams, ", ")
  573. err = utx.Exec(thisSQL, insertData...).Error
  574. if err != nil {
  575. utx.Rollback()
  576. return
  577. }
  578. }
  579. utx.Commit()
  580. }
  581. return
  582. }
  583. func CreateNewDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo, tx *gorm.DB) (err error) {
  584. if len(warehousingInfo) > 0 {
  585. if len(warehousingInfo) > 0 {
  586. 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,drug_code) VALUES "
  587. insertParams := make([]string, 0)
  588. insertData := make([]interface{}, 0)
  589. for _, info := range warehousingInfo {
  590. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  591. insertData = append(insertData, info.WarehousingId)
  592. insertData = append(insertData, info.DrugId)
  593. insertData = append(insertData, info.Number)
  594. insertData = append(insertData, info.ProductDate)
  595. insertData = append(insertData, info.ExpiryDate)
  596. insertData = append(insertData, info.WarehousingCount)
  597. insertData = append(insertData, info.Price)
  598. insertData = append(insertData, info.TotalPrice)
  599. insertData = append(insertData, info.Dealer)
  600. insertData = append(insertData, info.Manufacturer)
  601. insertData = append(insertData, info.Remark)
  602. insertData = append(insertData, info.Ctime)
  603. insertData = append(insertData, info.Mtime)
  604. insertData = append(insertData, info.Status)
  605. insertData = append(insertData, info.OrgId)
  606. insertData = append(insertData, info.WarehousingOrder)
  607. insertData = append(insertData, info.Type)
  608. insertData = append(insertData, info.RetailPrice)
  609. insertData = append(insertData, info.RetailTotalPrice)
  610. insertData = append(insertData, info.StockMaxNumber)
  611. insertData = append(insertData, info.MaxUnit)
  612. insertData = append(insertData, info.MinUnit)
  613. insertData = append(insertData, info.BatchNumber)
  614. insertData = append(insertData, info.StockMinNumber)
  615. insertData = append(insertData, info.StorehouseId)
  616. insertData = append(insertData, info.DrugCode)
  617. }
  618. thisSQL += strings.Join(insertParams, ", ")
  619. err = tx.Exec(thisSQL, insertData...).Error
  620. if err != nil {
  621. tx.Rollback()
  622. return
  623. }
  624. }
  625. }
  626. return
  627. }
  628. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  629. if len(warehousingInfo) > 0 {
  630. utx := writeDb.Begin()
  631. if len(warehousingInfo) > 0 {
  632. 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 "
  633. insertParams := make([]string, 0)
  634. insertData := make([]interface{}, 0)
  635. for _, info := range warehousingInfo {
  636. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  637. insertData = append(insertData, info.WarehousingId)
  638. insertData = append(insertData, info.DrugId)
  639. insertData = append(insertData, info.Number)
  640. insertData = append(insertData, info.ProductDate)
  641. insertData = append(insertData, info.ExpiryDate)
  642. insertData = append(insertData, info.WarehousingCount)
  643. insertData = append(insertData, info.Price)
  644. insertData = append(insertData, info.TotalPrice)
  645. insertData = append(insertData, info.Dealer)
  646. insertData = append(insertData, info.Manufacturer)
  647. insertData = append(insertData, info.Remark)
  648. insertData = append(insertData, info.Ctime)
  649. insertData = append(insertData, info.Mtime)
  650. insertData = append(insertData, info.Status)
  651. insertData = append(insertData, info.OrgId)
  652. insertData = append(insertData, info.WarehousingOrder)
  653. insertData = append(insertData, info.Type)
  654. insertData = append(insertData, info.RetailPrice)
  655. insertData = append(insertData, info.RetailTotalPrice)
  656. insertData = append(insertData, info.StockMaxNumber)
  657. insertData = append(insertData, info.MaxUnit)
  658. insertData = append(insertData, info.MinUnit)
  659. insertData = append(insertData, info.BatchNumber)
  660. insertData = append(insertData, info.StockMinNumber)
  661. insertData = append(insertData, info.SupplyWarehouseId)
  662. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  663. insertData = append(insertData, info.StorehouseId)
  664. insertData = append(insertData, info.IsCheck)
  665. }
  666. thisSQL += strings.Join(insertParams, ", ")
  667. err = utx.Exec(thisSQL, insertData...).Error
  668. if err != nil {
  669. utx.Rollback()
  670. return
  671. }
  672. }
  673. utx.Commit()
  674. }
  675. return
  676. }
  677. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  678. var total float64
  679. var min_number int64
  680. for _, item := range warehousingInfo {
  681. //查询刚入库耗材的库存
  682. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  683. //更新药品库的总量
  684. info, _ := FindeDrugInfo(item.DrugId)
  685. min_number = count.StockMaxNumber * info.MinNumber
  686. str := strconv.FormatInt(min_number, 10)
  687. minFloat, _ := strconv.ParseFloat(str, 64)
  688. total = info.Total + minFloat
  689. lib := models.BaseDrugLib{
  690. Total: total,
  691. }
  692. UpdateBaseDrug(&lib, item.ID)
  693. }
  694. return err
  695. }
  696. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  697. info := models.XtDrugWarehouseInfo{}
  698. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  699. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  700. return info, err
  701. }
  702. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  703. lib := models.BaseDrugLib{}
  704. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  705. return lib, err
  706. }
  707. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  708. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  709. return err
  710. }
  711. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  712. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  713. return err
  714. }
  715. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  716. info := models.DrugWarehouseInfo{}
  717. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  718. return info, err
  719. }
  720. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  721. out := models.DrugWarehouseOut{}
  722. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  723. return out, err
  724. }
  725. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  726. if len(drugflow) > 0 {
  727. utx := writeDb.Begin()
  728. if len(drugflow) > 0 {
  729. 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 "
  730. insertParams := make([]string, 0)
  731. insertData := make([]interface{}, 0)
  732. for _, info := range drugflow {
  733. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  734. insertData = append(insertData, info.WarehousingId)
  735. insertData = append(insertData, info.DrugId)
  736. insertData = append(insertData, info.Number)
  737. insertData = append(insertData, info.BatchNumber)
  738. insertData = append(insertData, info.Count)
  739. insertData = append(insertData, info.UserOrgId)
  740. insertData = append(insertData, info.PatientId)
  741. insertData = append(insertData, info.SystemTime)
  742. insertData = append(insertData, info.ConsumableType)
  743. insertData = append(insertData, info.IsSys)
  744. insertData = append(insertData, info.WarehousingOrder)
  745. insertData = append(insertData, info.WarehouseOutId)
  746. insertData = append(insertData, info.WarehouseOutOrderNumber)
  747. insertData = append(insertData, info.IsEdit)
  748. insertData = append(insertData, info.CancelStockId)
  749. insertData = append(insertData, info.CancelOrderNumber)
  750. insertData = append(insertData, info.Manufacturer)
  751. insertData = append(insertData, info.Dealer)
  752. insertData = append(insertData, info.Creator)
  753. insertData = append(insertData, info.UpdateCreator)
  754. insertData = append(insertData, info.Status)
  755. insertData = append(insertData, info.Ctime)
  756. insertData = append(insertData, info.Mtime)
  757. insertData = append(insertData, info.Price)
  758. insertData = append(insertData, info.WarehousingDetailId)
  759. insertData = append(insertData, info.WarehouseOutDetailId)
  760. insertData = append(insertData, info.CancelOutDetailId)
  761. insertData = append(insertData, info.ExpireDate)
  762. insertData = append(insertData, info.ProductDate)
  763. insertData = append(insertData, info.MaxUnit)
  764. insertData = append(insertData, info.MinUnit)
  765. insertData = append(insertData, info.StorehouseId)
  766. }
  767. thisSQL += strings.Join(insertParams, ", ")
  768. err = utx.Exec(thisSQL, insertData...).Error
  769. if err != nil {
  770. utx.Rollback()
  771. return
  772. }
  773. }
  774. utx.Commit()
  775. }
  776. return
  777. }
  778. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  779. info := models.DrugFlow{}
  780. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  781. if err == gorm.ErrRecordNotFound {
  782. return nil, err
  783. }
  784. if err != nil {
  785. return nil, err
  786. }
  787. return &info, nil
  788. }
  789. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  790. info := models.XtDrugWarehouseInfo{}
  791. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  792. return info, err
  793. }
  794. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  795. warehouseInfo := models.XtDrugWarehouseInfo{}
  796. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  797. return err
  798. }
  799. func CreateDrugFlowOne(flow models.DrugFlow) error {
  800. ut := writeDb.Begin()
  801. err := ut.Create(&flow).Error
  802. if err != nil {
  803. ut.Rollback()
  804. return err
  805. }
  806. ut.Commit()
  807. return err
  808. }
  809. func CreateSelfDrugFlowTwo(flow models.XtSelfDrugFlow) error {
  810. ut := writeDb.Begin()
  811. err := ut.Create(&flow).Error
  812. if err != nil {
  813. ut.Rollback()
  814. return err
  815. }
  816. ut.Commit()
  817. return err
  818. }
  819. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  820. err := tx.Create(&flow).Error
  821. if err != nil {
  822. tx.Rollback()
  823. return err
  824. }
  825. return err
  826. }
  827. func CreateDrugOutInfoLog(log models.XtDrugWarehouseOutInfoLog, tx *gorm.DB) error {
  828. err := tx.Create(&log).Error
  829. if err != nil {
  830. tx.Rollback()
  831. return err
  832. }
  833. return err
  834. }
  835. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  836. detail := models.DrugAutomaticReduceDetail{}
  837. 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
  838. 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
  839. return detail, err
  840. }
  841. func UpdateSelfDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.XtSelfDrugWarehouseOutInfo, error) {
  842. detail := models.XtSelfDrugWarehouseOutInfo{}
  843. err = XTWriteDB().Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("patient_id =? and sys_record_time =? and drug_id = ? and status = 1 and advice_id =?", patientid, recorddate, drugid, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  844. return detail, err
  845. }
  846. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  847. flow := models.DrugFlow{}
  848. 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
  849. return flow, err
  850. }
  851. func UpdateDrugFlowTensOne(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.XtSelfDrugFlow, error) {
  852. flow := models.XtSelfDrugFlow{}
  853. 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
  854. return flow, err
  855. }
  856. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  857. tx := XTWriteDB().Begin()
  858. err := tx.Create(&flow).Error
  859. if err != nil {
  860. tx.Rollback()
  861. return err
  862. }
  863. tx.Commit()
  864. return err
  865. }
  866. func CreateNewDrugFlowTwo(flow *models.DrugFlow, tx *gorm.DB) error {
  867. err := tx.Create(&flow).Error
  868. if err != nil {
  869. tx.Rollback()
  870. return err
  871. }
  872. return err
  873. }
  874. func CreateInfoLog(log models.XtDrugWarehouseInfoLog, tx *gorm.DB) error {
  875. err := tx.Create(&log).Error
  876. if err != nil {
  877. tx.Rollback()
  878. return err
  879. }
  880. return err
  881. }
  882. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  883. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  884. return info, err
  885. }
  886. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  887. info := models.WarehousingInfo{}
  888. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  889. return info, err
  890. }
  891. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  892. err := XTWriteDB().Create(&info).Error
  893. return err
  894. }
  895. func CreatedNewWarehouseingDetail(info *models.WarehousingInfo, tx *gorm.DB) error {
  896. err := tx.Create(&info).Error
  897. if err != nil {
  898. tx.Rollback()
  899. return err
  900. }
  901. return err
  902. }
  903. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  904. if storehouse_id == 0 {
  905. 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
  906. }
  907. if storehouse_id > 0 {
  908. 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
  909. }
  910. return info, err
  911. }
  912. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  913. if storehouse_id == 0 {
  914. 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
  915. if err != gorm.ErrRecordNotFound {
  916. if err != nil {
  917. tx.Rollback()
  918. return info, err
  919. }
  920. }
  921. }
  922. if storehouse_id > 0 {
  923. 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
  924. if err != gorm.ErrRecordNotFound {
  925. if err != nil {
  926. tx.Rollback()
  927. return info, err
  928. }
  929. }
  930. }
  931. return info, err
  932. }
  933. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  934. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  935. if good_id > 0 {
  936. db = db.Where("x.good_id = ?", good_id)
  937. }
  938. if good_type_id > 0 {
  939. db = db.Where("x.good_type_id = ?", good_type_id)
  940. }
  941. if storehouse_id > 0 {
  942. db = db.Where("x.storehouse_id = ?", storehouse_id)
  943. }
  944. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  945. return info, err
  946. }
  947. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  948. info := models.WarehousingInfo{}
  949. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  950. return info, err
  951. }
  952. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  953. info := models.WarehousingInfo{}
  954. var err error
  955. if storehouse_id == 0 {
  956. 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
  957. }
  958. if storehouse_id > 0 {
  959. 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
  960. }
  961. return info, err
  962. }
  963. func FindNewWarehousingInfoTen(goodId int64, storehouse_id int64, tx *gorm.DB) (models.WarehousingInfo, error) {
  964. info := models.WarehousingInfo{}
  965. var err error
  966. if storehouse_id == 0 {
  967. err = tx.Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and is_check = 1", goodId).Find(&info).Error
  968. if err != gorm.ErrRecordNotFound {
  969. if err != nil {
  970. tx.Rollback()
  971. return info, err
  972. }
  973. }
  974. }
  975. if storehouse_id > 0 {
  976. err := tx.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
  977. if err != gorm.ErrRecordNotFound {
  978. if err != nil {
  979. tx.Rollback()
  980. return info, err
  981. }
  982. }
  983. }
  984. return info, err
  985. }
  986. //药品先进先出,找出最先入库的批次
  987. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  988. 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
  989. return info, err
  990. }
  991. func FindLastDrugWarehousingInfoByIDTwo(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  992. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").Last(&info).Error
  993. return info, err
  994. }
  995. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  996. 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
  997. if err != gorm.ErrRecordNotFound {
  998. if err != nil {
  999. tx.Rollback()
  1000. return info, err
  1001. }
  1002. }
  1003. return info, err
  1004. }
  1005. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  1006. if is_source == 1 {
  1007. 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
  1008. }
  1009. if is_source == 2 || is_source == 0 {
  1010. 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
  1011. }
  1012. return info, err
  1013. }
  1014. // 出库时先进先出,找出最先入库的批次
  1015. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  1016. 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
  1017. return info, err
  1018. }
  1019. //
  1020. //// 出库时先进先出,找出最先入库的批次
  1021. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  1022. // 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
  1023. // return info, err
  1024. //}
  1025. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  1026. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  1027. return info, err
  1028. }
  1029. 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) {
  1030. offset := (page - 1) * limit
  1031. db := readDb.Model(&models.Warehousing{})
  1032. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  1033. if len(keywords) > 0 {
  1034. likeKey := "%" + keywords + "%"
  1035. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  1036. 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")
  1037. }
  1038. if startTime > 0 {
  1039. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  1040. }
  1041. if endTime > 0 {
  1042. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  1043. }
  1044. if storehouse_id > 0 {
  1045. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  1046. }
  1047. if check_type == 0 {
  1048. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  1049. }
  1050. if check_type > 0 {
  1051. db = db.Where("xt_warehouse.is_check = ?", check_type)
  1052. }
  1053. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  1054. return
  1055. }
  1056. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  1057. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  1058. if orgid > 0 {
  1059. db = db.Where("org_id = ?", orgid)
  1060. }
  1061. if len(keyword) > 0 {
  1062. likeKey := "%" + keyword + "%"
  1063. db = db.Where("good_name like ?", likeKey)
  1064. }
  1065. err = db.Find(&goodInfo).Error
  1066. return goodInfo, err
  1067. }
  1068. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  1069. db := XTReadDB().Model(&info).Where("status =1")
  1070. if len(good_id) > 0 {
  1071. db = db.Where("good_id in (?)", good_id)
  1072. }
  1073. if startime > 0 {
  1074. db = db.Where("ctime >=?", startime)
  1075. }
  1076. if endtime > 0 {
  1077. db = db.Where("ctime<=?", endtime)
  1078. }
  1079. if orgid > 0 {
  1080. db = db.Where("org_id = ?", orgid)
  1081. }
  1082. if storehouse_id > 0 {
  1083. db = db.Where("storehouse_id = ?", storehouse_id)
  1084. }
  1085. err = db.Find(&info).Error
  1086. return info, err
  1087. }
  1088. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  1089. db := XTReadDB().Model(&info).Where("status =1")
  1090. if len(good_id) > 0 {
  1091. db = db.Where("good_id in (?)", good_id)
  1092. }
  1093. if startime > 0 {
  1094. db = db.Where("ctime >=?", startime)
  1095. }
  1096. if endtime > 0 {
  1097. db = db.Where("ctime<=?", endtime)
  1098. }
  1099. if orgid > 0 {
  1100. db = db.Where("org_id = ?", orgid)
  1101. }
  1102. if storehouse_id > 0 {
  1103. db = db.Where("storehouse_id = ?", storehouse_id)
  1104. }
  1105. err = db.Find(&info).Error
  1106. return info, err
  1107. }
  1108. type VMDrugWarehouse struct {
  1109. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1110. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1111. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  1112. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1113. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  1114. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1115. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  1116. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1117. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1118. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  1119. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1120. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1121. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1122. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  1123. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  1124. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1125. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1126. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1127. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  1128. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  1129. }
  1130. func (VMDrugWarehouse) TableName() string {
  1131. return "xt_drug_warehouse"
  1132. }
  1133. type VMDrugWarehouseInfo struct {
  1134. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1135. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  1136. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1137. Number string `gorm:"column:number" json:"number" form:"number"`
  1138. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1139. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1140. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  1141. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1142. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1143. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1144. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1145. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1146. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1147. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1148. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1149. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1150. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  1151. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1152. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1153. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1154. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1155. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1156. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1157. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1158. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1159. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1160. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  1161. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1162. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1163. }
  1164. func (VMDrugWarehouseInfo) TableName() string {
  1165. return "xt_drug_warehouse_info"
  1166. }
  1167. type Drug struct {
  1168. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1169. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  1170. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  1171. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  1172. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  1173. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  1174. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  1175. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  1176. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  1177. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  1178. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  1179. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  1180. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  1181. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  1182. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1183. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1184. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  1185. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1186. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1187. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  1188. Number string `gorm:"column:number" json:"number" form:"number"`
  1189. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  1190. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  1191. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  1192. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  1193. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  1194. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1195. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  1196. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  1197. Code string `gorm:"column:code" json:"code" form:"code"`
  1198. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  1199. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  1200. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  1201. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  1202. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  1203. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  1204. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  1205. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  1206. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  1207. Label int64 `gorm:"column:label" json:"label" form:"label"`
  1208. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  1209. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  1210. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  1211. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  1212. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  1213. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  1214. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1215. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1216. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1217. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1218. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1219. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1220. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  1221. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  1222. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  1223. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  1224. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  1225. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  1226. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  1227. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  1228. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1229. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1230. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1231. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1232. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1233. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1234. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1235. IsZeroFlag int64 `gorm:"column:is_zero_flag" json:"is_zero_flag" form:"is_zero_flag"`
  1236. }
  1237. func (Drug) TableName() string {
  1238. return "xt_base_drug"
  1239. }
  1240. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1241. db := readDb.Model(&VMDrugWarehouse{})
  1242. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1243. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1244. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1245. if len(keywords) > 0 {
  1246. likeKey := "%" + keywords + "%"
  1247. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1248. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1249. 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")
  1250. }
  1251. if startTime > 0 {
  1252. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1253. }
  1254. if endTime > 0 {
  1255. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1256. }
  1257. db = db.Count(&total)
  1258. offset := (page - 1) * limit
  1259. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1260. return
  1261. }
  1262. 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) {
  1263. db := readDb.Model(&VMDrugWarehouse{})
  1264. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1265. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1266. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1267. if len(keywords) > 0 {
  1268. likeKey := "%" + keywords + "%"
  1269. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1270. 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")
  1271. }
  1272. if startTime > 0 {
  1273. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1274. }
  1275. if endTime > 0 {
  1276. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1277. }
  1278. if storehouse_id > 0 {
  1279. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1280. }
  1281. if check_type > 0 {
  1282. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1283. }
  1284. db = db.Count(&total)
  1285. offset := (page - 1) * limit
  1286. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1287. return
  1288. }
  1289. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1290. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1291. return list, err
  1292. }
  1293. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1294. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1295. return list, err
  1296. }
  1297. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1298. 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
  1299. return list, err
  1300. }
  1301. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1302. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1303. return list, err
  1304. }
  1305. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1306. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1307. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1308. fmt.Println(table)
  1309. if len(id) > 0 {
  1310. db = db.Where("x.warehousing_id in(?)", id)
  1311. }
  1312. if org_id > 0 {
  1313. db = db.Where("x.org_id = ?", org_id)
  1314. }
  1315. 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
  1316. return list, err
  1317. }
  1318. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1319. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1320. return warehousing, err
  1321. }
  1322. func FindNewWarehousingById(id int64, tx *gorm.DB) (warehousing models.Warehousing, err error) {
  1323. err = tx.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1324. if err != gorm.ErrRecordNotFound {
  1325. if err != nil {
  1326. tx.Rollback()
  1327. return warehousing, err
  1328. }
  1329. }
  1330. return warehousing, err
  1331. }
  1332. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1333. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1334. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1335. return warehousing, err
  1336. }
  1337. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1338. 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
  1339. return warehousing, err
  1340. }
  1341. func FindNewDrugWarehousingById(id int64, org_id int64, tx *gorm.DB) (warehousing VMDrugWarehouse, err error) {
  1342. err = tx.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
  1343. if err != gorm.ErrRecordNotFound {
  1344. if err != nil {
  1345. tx.Rollback()
  1346. return warehousing, err
  1347. }
  1348. }
  1349. return warehousing, err
  1350. }
  1351. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1352. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1353. if len(id) > 0 {
  1354. db = db.Where("x.id in(?)", id)
  1355. }
  1356. if org_id > 0 {
  1357. db = db.Where("x.org_id = ?", org_id)
  1358. }
  1359. 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
  1360. return warehousing, err
  1361. }
  1362. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1363. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1364. // return warehousing, err
  1365. //
  1366. //}
  1367. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1368. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1369. return
  1370. }
  1371. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1372. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1373. return
  1374. }
  1375. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1376. if len(salesReturnInfo) > 0 {
  1377. utx := writeDb.Begin()
  1378. if len(salesReturnInfo) > 0 {
  1379. 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 "
  1380. insertParams := make([]string, 0)
  1381. insertData := make([]interface{}, 0)
  1382. for _, info := range salesReturnInfo {
  1383. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1384. insertData = append(insertData, info.GoodId)
  1385. insertData = append(insertData, info.SalesReturnId)
  1386. insertData = append(insertData, info.GoodTypeId)
  1387. insertData = append(insertData, info.Count)
  1388. insertData = append(insertData, info.Price)
  1389. insertData = append(insertData, info.Total)
  1390. insertData = append(insertData, info.Ctime)
  1391. insertData = append(insertData, info.Status)
  1392. insertData = append(insertData, info.OrgId)
  1393. insertData = append(insertData, info.OrderNumber)
  1394. insertData = append(insertData, info.Type)
  1395. insertData = append(insertData, info.Dealer)
  1396. insertData = append(insertData, info.Manufacturer)
  1397. }
  1398. thisSQL += strings.Join(insertParams, ", ")
  1399. err = utx.Exec(thisSQL, insertData...).Error
  1400. if err != nil {
  1401. utx.Rollback()
  1402. return
  1403. }
  1404. }
  1405. utx.Commit()
  1406. }
  1407. return
  1408. }
  1409. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1410. if len(salesReturnInfo) > 0 {
  1411. utx := writeDb.Begin()
  1412. if len(salesReturnInfo) > 0 {
  1413. 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 "
  1414. insertParams := make([]string, 0)
  1415. insertData := make([]interface{}, 0)
  1416. for _, info := range salesReturnInfo {
  1417. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1418. insertData = append(insertData, info.DrugId)
  1419. insertData = append(insertData, info.SalesReturnId)
  1420. insertData = append(insertData, info.Count)
  1421. insertData = append(insertData, info.Price)
  1422. insertData = append(insertData, info.Total)
  1423. insertData = append(insertData, info.Ctime)
  1424. insertData = append(insertData, info.Status)
  1425. insertData = append(insertData, info.OrgId)
  1426. insertData = append(insertData, info.OrderNumber)
  1427. insertData = append(insertData, info.Type)
  1428. insertData = append(insertData, info.Dealer)
  1429. insertData = append(insertData, info.Manufacturer)
  1430. insertData = append(insertData, info.RetailPrice)
  1431. insertData = append(insertData, info.RetailTotalPrice)
  1432. }
  1433. thisSQL += strings.Join(insertParams, ", ")
  1434. err = utx.Exec(thisSQL, insertData...).Error
  1435. if err != nil {
  1436. utx.Rollback()
  1437. return
  1438. }
  1439. }
  1440. utx.Commit()
  1441. }
  1442. return
  1443. }
  1444. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1445. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1446. return total, err
  1447. }
  1448. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1449. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1450. return total, err
  1451. }
  1452. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1453. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1454. if err != gorm.ErrRecordNotFound {
  1455. if err != nil {
  1456. tx.Rollback()
  1457. return total, err
  1458. }
  1459. }
  1460. return total, err
  1461. }
  1462. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1463. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1464. return total, err
  1465. }
  1466. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1467. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1468. if err != gorm.ErrRecordNotFound {
  1469. if err != nil {
  1470. tx.Rollback()
  1471. return total, err
  1472. }
  1473. }
  1474. return total, err
  1475. }
  1476. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1477. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1478. // return total, err
  1479. //}
  1480. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1481. db := readDb.Model(&models.SalesReturn{})
  1482. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1483. if len(keywords) > 0 {
  1484. likeKey := "%" + keywords + "%"
  1485. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1486. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1487. 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")
  1488. }
  1489. if startTime > 0 {
  1490. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1491. }
  1492. if endTime > 0 {
  1493. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1494. }
  1495. db = db.Count(&total)
  1496. offset := (page - 1) * limit
  1497. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1498. return
  1499. }
  1500. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1501. db := readDb.Model(&models.DrugSalesReturn{})
  1502. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1503. if len(keywords) > 0 {
  1504. likeKey := "%" + keywords + "%"
  1505. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1506. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1507. 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")
  1508. }
  1509. if startTime > 0 {
  1510. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1511. }
  1512. if endTime > 0 {
  1513. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1514. }
  1515. db = db.Count(&total)
  1516. offset := (page - 1) * limit
  1517. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1518. return
  1519. }
  1520. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1521. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1522. return list, err
  1523. }
  1524. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1525. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1526. return list, err
  1527. }
  1528. type DrugCancelStockInfo struct {
  1529. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1530. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1531. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1532. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1533. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1534. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1535. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1536. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1537. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1538. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1539. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1540. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1541. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1542. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1543. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1544. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1545. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1546. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1547. Number string `gorm:"column:number" json:"number" form:"number"`
  1548. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1549. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1550. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1551. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1552. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1553. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1554. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1555. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1556. }
  1557. func (DrugCancelStockInfo) TableName() string {
  1558. return "xt_drug_cancel_stock_info"
  1559. }
  1560. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1561. 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
  1562. return list, err
  1563. }
  1564. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1565. db := readDb.Model(&models.WarehousingInfo{})
  1566. db = db.Where("org_id = ? AND status = 1", orgId)
  1567. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1568. if startTime > 0 {
  1569. db = db.Where("ctime >=?", startTime)
  1570. }
  1571. if endTime > 0 {
  1572. db = db.Where("ctime<= ?", endTime)
  1573. }
  1574. db = db.Count(&total)
  1575. offset := (page - 1) * limit
  1576. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1577. return list, total, err
  1578. }
  1579. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1580. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1581. return total, err
  1582. }
  1583. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1584. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1585. return total, err
  1586. }
  1587. func FindAllSelfDrugWarehouseOut(org_id int64, tx *gorm.DB) (total int64, err error) {
  1588. err = tx.Model(&models.XtDrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1589. if err != gorm.ErrRecordNotFound {
  1590. if err != nil {
  1591. tx.Rollback()
  1592. return
  1593. }
  1594. }
  1595. return total, err
  1596. }
  1597. func AddSigleWarehouseOutOne(warehouseOut *models.WarehouseOut) error {
  1598. err := XTWriteDB().Create(&warehouseOut).Error
  1599. return err
  1600. }
  1601. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1602. err := tx.Create(&warehouseOut).Error
  1603. if err != nil {
  1604. tx.Rollback()
  1605. return err
  1606. }
  1607. return err
  1608. }
  1609. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1610. err := tx.Create(&warehouseOut).Error
  1611. if err != nil {
  1612. tx.Rollback()
  1613. return err
  1614. }
  1615. return err
  1616. }
  1617. func UpdateSingleWarehoseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1618. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1619. if err != nil {
  1620. tx.Rollback()
  1621. return err
  1622. }
  1623. return err
  1624. }
  1625. func GetDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string) (models.DrugWarehouseOut, error) {
  1626. drugWarehouseOut := models.DrugWarehouseOut{}
  1627. err := XTReadDB().Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).Find(&drugWarehouseOut).Error
  1628. return drugWarehouseOut, err
  1629. }
  1630. func GetNewDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  1631. drugWarehouseOut := models.DrugWarehouseOut{}
  1632. err := tx.Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).Find(&drugWarehouseOut).Error
  1633. if err != gorm.ErrRecordNotFound {
  1634. if err != nil {
  1635. tx.Rollback()
  1636. return drugWarehouseOut, err
  1637. }
  1638. }
  1639. return drugWarehouseOut, err
  1640. }
  1641. func GetNewDrugWarehouseOutIsExitOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB, id int64) (models.DrugWarehouseOut, error) {
  1642. drugWarehouseOut := models.DrugWarehouseOut{}
  1643. err := tx.Where("is_sys = ? AND status = 1 AND org_id = ? AND id = ?", is_sys, org_id, id).Find(&drugWarehouseOut).Error
  1644. if err != gorm.ErrRecordNotFound {
  1645. if err != nil {
  1646. tx.Rollback()
  1647. return drugWarehouseOut, err
  1648. }
  1649. }
  1650. return drugWarehouseOut, err
  1651. }
  1652. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1653. err := writeDb.Create(&warehouseOut).Error
  1654. return err
  1655. }
  1656. func GetSigleDrugWarehouseOutByPatientId(warehousing_time int64, patient_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouse, error) {
  1657. selfDrugWarehouse := models.XtSelfDrugWarehouse{}
  1658. err := tx.Where("warehousing_time = ? and patient_id = ? and is_sys =1 and status=1", warehousing_time, patient_id).Find(&selfDrugWarehouse).Error
  1659. if err != gorm.ErrRecordNotFound {
  1660. if err != nil {
  1661. tx.Rollback()
  1662. return selfDrugWarehouse, err
  1663. }
  1664. }
  1665. return selfDrugWarehouse, err
  1666. }
  1667. func AddSelfSigleDrugWarehouseOut(warehouseOut *models.XtSelfDrugWarehouseOut, tx *gorm.DB) error {
  1668. err := tx.Create(&warehouseOut).Error
  1669. if err != nil {
  1670. tx.Rollback()
  1671. return err
  1672. }
  1673. return err
  1674. }
  1675. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1676. err := tx.Create(&warehouseOut).Error
  1677. if err != nil {
  1678. tx.Rollback()
  1679. return err
  1680. }
  1681. return err
  1682. }
  1683. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1684. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1685. return err
  1686. }
  1687. func UpdateNewSingleDrugWarehouseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1688. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1689. if err != nil {
  1690. tx.Rollback()
  1691. return err
  1692. }
  1693. return err
  1694. }
  1695. func UpdateSelfSingleDrugWarehouseOut(id int64, patient_id int64, tx *gorm.DB) error {
  1696. err := tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"patient_id": patient_id}).Error
  1697. return err
  1698. }
  1699. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1700. if len(warehouseOutInfo) > 0 {
  1701. utx := writeDb.Begin()
  1702. if len(warehouseOutInfo) > 0 {
  1703. 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 "
  1704. insertParams := make([]string, 0)
  1705. insertData := make([]interface{}, 0)
  1706. for _, info := range warehouseOutInfo {
  1707. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1708. insertData = append(insertData, info.WarehouseOutId)
  1709. insertData = append(insertData, info.GoodId)
  1710. insertData = append(insertData, info.GoodTypeId)
  1711. insertData = append(insertData, info.ProductDate)
  1712. insertData = append(insertData, info.ExpiryDate)
  1713. insertData = append(insertData, info.Count)
  1714. insertData = append(insertData, info.Price)
  1715. insertData = append(insertData, info.TotalPrice)
  1716. insertData = append(insertData, info.Remark)
  1717. insertData = append(insertData, info.Ctime)
  1718. insertData = append(insertData, info.Status)
  1719. insertData = append(insertData, info.OrgId)
  1720. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1721. insertData = append(insertData, info.Type)
  1722. insertData = append(insertData, info.Dealer)
  1723. insertData = append(insertData, info.Manufacturer)
  1724. insertData = append(insertData, info.Number)
  1725. insertData = append(insertData, info.LicenseNumber)
  1726. }
  1727. thisSQL += strings.Join(insertParams, ", ")
  1728. err = utx.Exec(thisSQL, insertData...).Error
  1729. if err != nil {
  1730. utx.Rollback()
  1731. return
  1732. }
  1733. }
  1734. utx.Commit()
  1735. }
  1736. return
  1737. }
  1738. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1739. if len(warehouseOutInfo) > 0 {
  1740. utx := writeDb.Begin()
  1741. if len(warehouseOutInfo) > 0 {
  1742. 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 "
  1743. insertParams := make([]string, 0)
  1744. insertData := make([]interface{}, 0)
  1745. for _, info := range warehouseOutInfo {
  1746. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1747. insertData = append(insertData, info.WarehouseOutId)
  1748. insertData = append(insertData, info.DrugId)
  1749. insertData = append(insertData, info.ProductDate)
  1750. insertData = append(insertData, info.ExpiryDate)
  1751. insertData = append(insertData, info.Count)
  1752. insertData = append(insertData, info.Price)
  1753. insertData = append(insertData, info.TotalPrice)
  1754. insertData = append(insertData, info.Remark)
  1755. insertData = append(insertData, info.Ctime)
  1756. insertData = append(insertData, info.Status)
  1757. insertData = append(insertData, info.OrgId)
  1758. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1759. insertData = append(insertData, info.Type)
  1760. insertData = append(insertData, info.Dealer)
  1761. insertData = append(insertData, info.Manufacturer)
  1762. insertData = append(insertData, info.RetailPrice)
  1763. insertData = append(insertData, info.RetailTotalPrice)
  1764. insertData = append(insertData, info.Number)
  1765. insertData = append(insertData, info.BatchNumber)
  1766. insertData = append(insertData, info.CountUnit)
  1767. }
  1768. thisSQL += strings.Join(insertParams, ", ")
  1769. err = utx.Exec(thisSQL, insertData...).Error
  1770. if err != nil {
  1771. utx.Rollback()
  1772. return
  1773. }
  1774. }
  1775. utx.Commit()
  1776. }
  1777. return
  1778. }
  1779. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1780. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1781. return info, err
  1782. }
  1783. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1784. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1785. return info, err
  1786. }
  1787. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1788. db := readDb.Model(&models.WarehouseOut{})
  1789. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1790. if len(keywords) > 0 {
  1791. likeKey := "%" + keywords + "%"
  1792. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1793. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1794. 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")
  1795. }
  1796. if startTime > 0 {
  1797. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1798. }
  1799. if endTime > 0 {
  1800. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1801. }
  1802. db = db.Count(&total)
  1803. offset := (page - 1) * limit
  1804. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1805. return list, total, err
  1806. }
  1807. 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) {
  1808. db := readDb.Model(&models.WarehouseOut{})
  1809. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1", orgId)
  1810. if len(keywords) > 0 {
  1811. likeKey := "%" + keywords + "%"
  1812. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1813. 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")
  1814. }
  1815. if startTime > 0 {
  1816. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1817. }
  1818. if endTime > 0 {
  1819. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1820. }
  1821. if storehouse_id > 0 {
  1822. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1823. }
  1824. if way_type == 0 {
  1825. 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")
  1826. }
  1827. if way_type == 1 {
  1828. db = db.Where("xt_warehouse_out.is_sys = 0")
  1829. }
  1830. if way_type == 2 {
  1831. db = db.Where("xt_warehouse_out.is_sys = 1")
  1832. }
  1833. if way_type == 3 {
  1834. db = db.Where("xt_warehouse_out.is_sys = 12")
  1835. }
  1836. if way_type == 4 {
  1837. db = db.Where("xt_warehouse_out.is_sys = 5")
  1838. }
  1839. if check_type > 0 {
  1840. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1841. }
  1842. if check_type == 0 {
  1843. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1844. }
  1845. db = db.Count(&total)
  1846. offset := (page - 1) * limit
  1847. if orgId == 10191 {
  1848. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1849. } else {
  1850. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1851. }
  1852. return list, total, err
  1853. }
  1854. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1855. db := readDb.Model(&models.DrugWarehouseOut{})
  1856. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1857. if len(keywords) > 0 {
  1858. likeKey := "%" + keywords + "%"
  1859. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1860. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1861. 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")
  1862. }
  1863. if startTime > 0 {
  1864. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1865. }
  1866. if endTime > 0 {
  1867. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1868. }
  1869. db = db.Count(&total)
  1870. offset := (page - 1) * limit
  1871. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1872. return list, total, err
  1873. }
  1874. 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) {
  1875. db := readDb.Model(&models.DrugWarehouseOut{})
  1876. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1877. if len(keywords) > 0 {
  1878. likeKey := "%" + keywords + "%"
  1879. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1880. 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")
  1881. }
  1882. if startTime > 0 {
  1883. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1884. }
  1885. if endTime > 0 {
  1886. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1887. }
  1888. if storehouse_id > 0 {
  1889. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1890. }
  1891. if check_type > 0 {
  1892. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1893. }
  1894. if way_type == 0 {
  1895. 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")
  1896. }
  1897. if way_type == 1 {
  1898. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1899. }
  1900. if way_type == 2 {
  1901. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1902. }
  1903. if way_type == 3 {
  1904. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1905. }
  1906. if way_type == 4 {
  1907. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1908. }
  1909. db = db.Count(&total)
  1910. offset := (page - 1) * limit
  1911. if orgId == 10191 {
  1912. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1913. } else {
  1914. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1915. }
  1916. return list, total, err
  1917. }
  1918. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1919. 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
  1920. return list, err
  1921. }
  1922. type DrugWarehouseOutInfo struct {
  1923. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1924. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1925. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1926. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1927. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1928. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1929. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1930. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1931. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1932. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1933. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1934. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1935. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1936. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1937. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1938. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1939. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1940. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1941. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1942. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1943. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1944. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1945. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1946. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1947. Number string `gorm:"column:number" json:"number" form:"number"`
  1948. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1949. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1950. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1951. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1952. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1953. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1954. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1955. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1956. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1957. ClassType string `gorm:"column:class_type" json:"class_type" form:"class_type"`
  1958. }
  1959. func (DrugWarehouseOutInfo) TableName() string {
  1960. return "xt_drug_warehouse_out_info"
  1961. }
  1962. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1963. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1964. return list, err
  1965. }
  1966. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1967. info := &models.WarehousingInfo{}
  1968. type WarehouseInfoCountAmount struct {
  1969. Total int64
  1970. }
  1971. var result WarehouseInfoCountAmount
  1972. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1973. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1974. return result.Total, info
  1975. }
  1976. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1977. type SalesReturnCountAmount struct {
  1978. Total int64
  1979. }
  1980. var result SalesReturnCountAmount
  1981. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1982. return result.Total, err
  1983. }
  1984. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1985. type WarehouseOutInfoCountAmount struct {
  1986. Total int64
  1987. }
  1988. var result WarehouseOutInfoCountAmount
  1989. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1990. return result.Total, err
  1991. }
  1992. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1993. type CancleStockoCountAmount struct {
  1994. Total int64
  1995. }
  1996. var result CancleStockoCountAmount
  1997. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1998. return result.Total, err
  1999. }
  2000. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  2001. info := models.WarehousingInfo{}
  2002. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  2003. return info, err
  2004. }
  2005. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  2006. stock := models.CancelStock{}
  2007. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  2008. if err == gorm.ErrRecordNotFound {
  2009. return nil, err
  2010. }
  2011. if err != nil {
  2012. return nil, err
  2013. }
  2014. return &stock, nil
  2015. }
  2016. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  2017. stock := models.CancelStock{}
  2018. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2019. if err == gorm.ErrRecordNotFound {
  2020. return nil, err
  2021. }
  2022. if err != nil {
  2023. return nil, err
  2024. }
  2025. return &stock, nil
  2026. }
  2027. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  2028. stock := models.CancelStock{}
  2029. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2030. if err != gorm.ErrRecordNotFound {
  2031. if err != nil {
  2032. tx.Rollback()
  2033. return &stock, err
  2034. }
  2035. }
  2036. return &stock, err
  2037. }
  2038. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  2039. err := writeDb.Create(&cancelStock).Error
  2040. return err
  2041. }
  2042. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  2043. err := tx.Create(&cancelStock).Error
  2044. if err != nil {
  2045. tx.Rollback()
  2046. return err
  2047. }
  2048. return err
  2049. }
  2050. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  2051. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2052. return err
  2053. }
  2054. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  2055. stock := models.CancelStock{}
  2056. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2057. return stock, err
  2058. }
  2059. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  2060. stock := models.CancelStock{}
  2061. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2062. if err != gorm.ErrRecordNotFound {
  2063. if err != nil {
  2064. tx.Rollback()
  2065. return stock, err
  2066. }
  2067. }
  2068. return stock, err
  2069. }
  2070. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  2071. stock := models.DrugCancelStock{}
  2072. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2073. return stock, err
  2074. }
  2075. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  2076. stock := models.DrugCancelStock{}
  2077. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2078. if err != gorm.ErrRecordNotFound {
  2079. if err != nil {
  2080. tx.Rollback()
  2081. return stock, err
  2082. }
  2083. }
  2084. return stock, err
  2085. }
  2086. func GetManufactureById(id int64) (models.Manufacturer, error) {
  2087. manufacturer := models.Manufacturer{}
  2088. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2089. return manufacturer, err
  2090. }
  2091. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  2092. manufacturer := models.Manufacturer{}
  2093. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2094. if err != gorm.ErrRecordNotFound {
  2095. if err != nil {
  2096. tx.Rollback()
  2097. return manufacturer, err
  2098. }
  2099. }
  2100. return manufacturer, err
  2101. }
  2102. func GetDealerById(id int64) (models.Dealer, error) {
  2103. dealer := models.Dealer{}
  2104. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2105. return dealer, err
  2106. }
  2107. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  2108. dealer := models.Dealer{}
  2109. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2110. if err != gorm.ErrRecordNotFound {
  2111. if err != nil {
  2112. tx.Rollback()
  2113. return dealer, err
  2114. }
  2115. }
  2116. return dealer, err
  2117. }
  2118. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  2119. err := writeDb.Create(&cancelStock).Error
  2120. return err
  2121. }
  2122. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  2123. err := tx.Create(&cancelStock).Error
  2124. if err != nil {
  2125. tx.Rollback()
  2126. return err
  2127. }
  2128. return err
  2129. }
  2130. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  2131. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2132. return err
  2133. }
  2134. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  2135. // err := tx.Create(&cancelStock).Error
  2136. // return err
  2137. //}
  2138. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  2139. if len(cancelStockInfo) > 0 {
  2140. utx := writeDb.Begin()
  2141. if len(cancelStockInfo) > 0 {
  2142. 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 "
  2143. insertParams := make([]string, 0)
  2144. insertData := make([]interface{}, 0)
  2145. for _, info := range cancelStockInfo {
  2146. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2147. insertData = append(insertData, info.GoodId)
  2148. insertData = append(insertData, info.CancelStockId)
  2149. insertData = append(insertData, info.GoodTypeId)
  2150. insertData = append(insertData, info.Count)
  2151. insertData = append(insertData, info.Price)
  2152. insertData = append(insertData, info.Total)
  2153. insertData = append(insertData, info.Ctime)
  2154. insertData = append(insertData, info.Status)
  2155. insertData = append(insertData, info.OrgId)
  2156. insertData = append(insertData, info.OrderNumber)
  2157. insertData = append(insertData, info.Type)
  2158. insertData = append(insertData, info.Dealer)
  2159. insertData = append(insertData, info.Manufacturer)
  2160. insertData = append(insertData, info.Number)
  2161. insertData = append(insertData, info.RegisterAccount)
  2162. insertData = append(insertData, info.Remark)
  2163. insertData = append(insertData, info.ProductDate)
  2164. insertData = append(insertData, info.ExpiryDate)
  2165. insertData = append(insertData, info.WarehouseInfoId)
  2166. insertData = append(insertData, info.WarehouseInfoId)
  2167. }
  2168. thisSQL += strings.Join(insertParams, ", ")
  2169. err = utx.Exec(thisSQL, insertData...).Error
  2170. if err != nil {
  2171. utx.Rollback()
  2172. return
  2173. }
  2174. }
  2175. utx.Commit()
  2176. }
  2177. return
  2178. }
  2179. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  2180. err := XTWriteDB().Create(&info).Error
  2181. return err
  2182. }
  2183. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  2184. // err := tx.Create(&info).Error
  2185. // return err
  2186. //}
  2187. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  2188. info := models.DrugCancelStockInfo{}
  2189. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2190. return info, err
  2191. }
  2192. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  2193. // info := models.DrugCancelStockInfo{}
  2194. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2195. // return info, err
  2196. //}
  2197. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  2198. err := XTWriteDB().Create(&info).Error
  2199. return err
  2200. }
  2201. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  2202. err := XTWriteDB().Create(&info).Error
  2203. if err != nil {
  2204. tx.Rollback()
  2205. return err
  2206. }
  2207. return err
  2208. }
  2209. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  2210. info := models.CancelStockInfo{}
  2211. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2212. return info, err
  2213. }
  2214. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  2215. info := models.CancelStockInfo{}
  2216. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2217. if err != gorm.ErrRecordNotFound {
  2218. if err != nil {
  2219. tx.Rollback()
  2220. return info, err
  2221. }
  2222. }
  2223. return info, err
  2224. }
  2225. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  2226. info := models.DrugCancelStockInfo{}
  2227. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  2228. return info, err
  2229. }
  2230. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  2231. if len(cancelStockInfo) > 0 {
  2232. utx := writeDb.Begin()
  2233. if len(cancelStockInfo) > 0 {
  2234. 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 "
  2235. insertParams := make([]string, 0)
  2236. insertData := make([]interface{}, 0)
  2237. for _, info := range cancelStockInfo {
  2238. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2239. insertData = append(insertData, info.DrugId)
  2240. insertData = append(insertData, info.CancelStockId)
  2241. insertData = append(insertData, info.Count)
  2242. insertData = append(insertData, info.Price)
  2243. insertData = append(insertData, info.Total)
  2244. insertData = append(insertData, info.Ctime)
  2245. insertData = append(insertData, info.Status)
  2246. insertData = append(insertData, info.OrgId)
  2247. insertData = append(insertData, info.OrderNumber)
  2248. insertData = append(insertData, info.Type)
  2249. insertData = append(insertData, info.Dealer)
  2250. insertData = append(insertData, info.Manufacturer)
  2251. insertData = append(insertData, info.RetailPrice)
  2252. insertData = append(insertData, info.RetailTotalPrice)
  2253. insertData = append(insertData, info.RegisterAccount)
  2254. insertData = append(insertData, info.BatchNumber)
  2255. insertData = append(insertData, info.Remark)
  2256. insertData = append(insertData, info.MaxUnit)
  2257. insertData = append(insertData, info.ProductDate)
  2258. insertData = append(insertData, info.ExpiryDate)
  2259. insertData = append(insertData, info.BatchNumberId)
  2260. }
  2261. thisSQL += strings.Join(insertParams, ", ")
  2262. err = utx.Exec(thisSQL, insertData...).Error
  2263. if err != nil {
  2264. utx.Rollback()
  2265. return
  2266. }
  2267. }
  2268. utx.Commit()
  2269. }
  2270. return
  2271. }
  2272. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  2273. db := readDb.Model(&models.CancelStock{})
  2274. db = db.Where("org_id = ? AND status = 1", orgId)
  2275. err = db.Order("ctime desc").Find(&list).Error
  2276. return
  2277. }
  2278. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2279. db := readDb.Model(&models.WarehousingInfo{})
  2280. db = db.Where("org_id = ? AND status = 1", orgId)
  2281. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2282. if startTime > 0 {
  2283. db = db.Where("ctime >=?", startTime)
  2284. }
  2285. if endTime > 0 {
  2286. db = db.Where("ctime<= ?", endTime)
  2287. }
  2288. db = db.Count(&total)
  2289. offset := (page - 1) * limit
  2290. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2291. return
  2292. }
  2293. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  2294. db := XTReadDB().Model(&models.StockInfo{})
  2295. if startime > 0 {
  2296. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2297. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  2298. })
  2299. }
  2300. if startime == 0 || endtime == 0 {
  2301. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2302. return db.Where("org_id = ? AND status = 1", orgId)
  2303. })
  2304. }
  2305. if endtime > 0 {
  2306. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2307. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  2308. })
  2309. }
  2310. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  2311. db = db.Group("xt_good_information.id")
  2312. if startime == 0 || endtime == 0 {
  2313. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2314. return db.Where("org_id = ? AND status = 1", orgId)
  2315. })
  2316. }
  2317. if startime > 0 {
  2318. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2319. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  2320. })
  2321. }
  2322. if endtime > 0 {
  2323. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2324. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  2325. })
  2326. }
  2327. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  2328. if startime == 0 || endtime == 0 {
  2329. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  2330. }
  2331. if startime > 0 {
  2332. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  2333. }
  2334. if endtime > 0 {
  2335. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2336. }
  2337. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2338. if len(keyword) > 0 {
  2339. likeKey := "%" + keyword + "%"
  2340. 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)
  2341. 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")
  2342. }
  2343. if type_name > 0 {
  2344. 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)
  2345. }
  2346. db = db.Count(&total)
  2347. offset := (page - 1) * limit
  2348. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2349. return
  2350. }
  2351. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2352. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2353. return
  2354. }
  2355. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2356. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2357. return
  2358. }
  2359. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2360. 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
  2361. return
  2362. }
  2363. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2364. 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
  2365. return
  2366. }
  2367. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2368. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2369. "good_id": warehouseInfo.GoodId,
  2370. "number": warehouseInfo.Number,
  2371. "product_date": warehouseInfo.ProductDate,
  2372. "expiry_date": warehouseInfo.ExpiryDate,
  2373. "warehousing_count": warehouseInfo.WarehousingCount,
  2374. "price": warehouseInfo.Price,
  2375. "total_price": warehouseInfo.TotalPrice,
  2376. "mtime": warehouseInfo.Mtime,
  2377. "remark": warehouseInfo.Remark,
  2378. }).Error
  2379. return
  2380. }
  2381. func UpDateWarehouseStatus(id int64) (err error) {
  2382. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2383. return
  2384. }
  2385. func UpdatedStockFlowById(id int64) (err error) {
  2386. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2387. return
  2388. }
  2389. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2390. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2391. return
  2392. }
  2393. func UpdateDrugFlowById(id int64) (err error) {
  2394. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2395. return
  2396. }
  2397. func DeleteWarehouse(ids []string) (err error) {
  2398. ut := writeDb.Begin()
  2399. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2400. if err != nil {
  2401. ut.Rollback()
  2402. return
  2403. }
  2404. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2405. if err != nil {
  2406. ut.Rollback()
  2407. return
  2408. }
  2409. ut.Commit()
  2410. return
  2411. }
  2412. func DeleteStockFlow(ids []string) (err error) {
  2413. ut := writeDb.Begin()
  2414. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2415. if err != nil {
  2416. ut.Rollback()
  2417. return
  2418. }
  2419. ut.Commit()
  2420. return
  2421. }
  2422. func DeleteDrugWarehouse(ids []string) (err error) {
  2423. ut := writeDb.Begin()
  2424. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2425. if err != nil {
  2426. ut.Rollback()
  2427. return
  2428. }
  2429. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2430. if err != nil {
  2431. ut.Rollback()
  2432. return
  2433. }
  2434. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2435. if err != nil {
  2436. ut.Rollback()
  2437. return
  2438. }
  2439. ut.Commit()
  2440. return
  2441. }
  2442. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2443. warehouse := &models.Warehousing{}
  2444. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2445. return warehouse, err
  2446. }
  2447. func FindWareHouseOutByIdOne(id int64) (*models.WarehouseOut, error) {
  2448. warehouseOut := &models.WarehouseOut{}
  2449. err := XTReadDB().Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2450. return warehouseOut, err
  2451. }
  2452. func FindWareHouseOutById(id int64, tx *gorm.DB) (*models.WarehouseOut, error) {
  2453. warehouseOut := &models.WarehouseOut{}
  2454. err := tx.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2455. if err != gorm.ErrRecordNotFound {
  2456. if err != nil {
  2457. tx.Rollback()
  2458. return warehouseOut, err
  2459. }
  2460. }
  2461. return warehouseOut, err
  2462. }
  2463. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2464. warehouseOut := &models.DrugWarehouseOut{}
  2465. 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
  2466. return warehouseOut, err
  2467. }
  2468. func FindNewDrugWareHouseOutById(id int64, org_id int64, tx *gorm.DB) (*models.DrugWarehouseOut, error) {
  2469. warehouseOut := &models.DrugWarehouseOut{}
  2470. err := tx.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
  2471. if err != nil {
  2472. tx.Rollback()
  2473. return warehouseOut, err
  2474. }
  2475. return warehouseOut, err
  2476. }
  2477. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2478. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2479. return
  2480. }
  2481. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2482. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2483. return
  2484. }
  2485. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2486. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2487. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2488. return
  2489. }
  2490. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2491. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2492. return
  2493. }
  2494. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2495. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2496. if manufacturer_id > 0 {
  2497. db = db.Where("manufacturer = ?", manufacturer_id)
  2498. }
  2499. if dealer_id > 0 {
  2500. db = db.Where("dealer = ?", dealer_id)
  2501. }
  2502. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2503. return goodInfo, err
  2504. }
  2505. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2506. db := readDb.Model(&models.Warehousing{})
  2507. likekey := "%" + keywords + "%"
  2508. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2509. db = db.Where("warehousing_order LIKE ? ", likekey)
  2510. db = db.Count(&total)
  2511. offset := (page - 1) * limit
  2512. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2513. return
  2514. }
  2515. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2516. info := models.WarehousingInfo{}
  2517. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2518. return info, err
  2519. }
  2520. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2521. info := models.WarehousingInfo{}
  2522. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2523. return info, err
  2524. }
  2525. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2526. err = writeDb.Save(&info).Error
  2527. return err
  2528. }
  2529. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2530. err = writeDb.Save(&info).Error
  2531. return err
  2532. }
  2533. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2534. 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
  2535. return err
  2536. }
  2537. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2538. 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
  2539. return err
  2540. }
  2541. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2542. info := models.DrugWarehouseInfo{}
  2543. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2544. return info, err
  2545. }
  2546. func EditWarehousing(warehouse models.Warehousing) {
  2547. 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
  2548. }
  2549. func EditNewWarehousing(warehouse models.Warehousing, tx *gorm.DB) {
  2550. err = tx.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
  2551. if err != nil {
  2552. tx.Rollback()
  2553. return
  2554. }
  2555. return
  2556. }
  2557. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2558. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2559. 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)
  2560. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2561. return list, err
  2562. }
  2563. func DeleteSalesReturn(ids []string) (err error) {
  2564. ut := writeDb.Begin()
  2565. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2566. if err != nil {
  2567. ut.Rollback()
  2568. return
  2569. }
  2570. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2571. if err != nil {
  2572. ut.Rollback()
  2573. return
  2574. }
  2575. ut.Commit()
  2576. return
  2577. }
  2578. func DeleteDrugSalesReturn(ids []string) (err error) {
  2579. ut := writeDb.Begin()
  2580. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2581. if err != nil {
  2582. ut.Rollback()
  2583. return
  2584. }
  2585. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2586. if err != nil {
  2587. ut.Rollback()
  2588. return
  2589. }
  2590. ut.Commit()
  2591. return
  2592. }
  2593. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2594. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2595. return
  2596. }
  2597. type DrugSalesReturnInfo struct {
  2598. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2599. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2600. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2601. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2602. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2603. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2604. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2605. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2606. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2607. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2608. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2609. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2610. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2611. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2612. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2613. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2614. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2615. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2616. Number string `gorm:"column:number" json:"number" form:"number"`
  2617. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2618. }
  2619. func (DrugSalesReturnInfo) TableName() string {
  2620. return "xt_drug_sales_return_info"
  2621. }
  2622. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2623. 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
  2624. return
  2625. }
  2626. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2627. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2628. return
  2629. }
  2630. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2631. 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
  2632. return
  2633. }
  2634. func UpDateSaleReturnStatus(id int64) (err error) {
  2635. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2636. return
  2637. }
  2638. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2639. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2640. return
  2641. }
  2642. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2643. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2644. return salesReturn, err
  2645. }
  2646. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2647. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2648. return salesReturn, err
  2649. }
  2650. func EditSaleReturn(sales models.SalesReturn) {
  2651. 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
  2652. }
  2653. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2654. 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
  2655. }
  2656. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2657. err = writeDb.Save(&info).Error
  2658. return err
  2659. }
  2660. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2661. err = writeDb.Save(&info).Error
  2662. return err
  2663. }
  2664. func DeleteWarehouseOut(ids []string) (err error) {
  2665. ut := writeDb.Begin()
  2666. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2667. if err != nil {
  2668. ut.Rollback()
  2669. return
  2670. }
  2671. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2672. if err != nil {
  2673. ut.Rollback()
  2674. return
  2675. }
  2676. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2677. if err != nil {
  2678. ut.Rollback()
  2679. return
  2680. }
  2681. ut.Commit()
  2682. return
  2683. }
  2684. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2685. ut := writeDb.Begin()
  2686. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2687. if err != nil {
  2688. ut.Rollback()
  2689. return
  2690. }
  2691. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2692. if err != nil {
  2693. ut.Rollback()
  2694. return
  2695. }
  2696. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2697. if err != nil {
  2698. ut.Rollback()
  2699. return
  2700. }
  2701. ut.Commit()
  2702. return
  2703. }
  2704. func UpDateWarehouseOutStatus(id int64) (err error) {
  2705. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2706. return
  2707. }
  2708. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2709. ut := writeDb.Begin()
  2710. err = ut.Save(&info).Error
  2711. if err != nil {
  2712. ut.Rollback()
  2713. return err
  2714. }
  2715. ut.Commit()
  2716. return err
  2717. }
  2718. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2719. err = tx.Save(&info).Error
  2720. if err != nil {
  2721. tx.Rollback()
  2722. return err
  2723. }
  2724. return err
  2725. }
  2726. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2727. ut := writeDb.Begin()
  2728. err = ut.Save(&info).Error
  2729. if err != nil {
  2730. ut.Rollback()
  2731. return err
  2732. }
  2733. ut.Commit()
  2734. return err
  2735. }
  2736. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2737. err = tx.Save(&info).Error
  2738. if err != nil {
  2739. tx.Rollback()
  2740. return err
  2741. }
  2742. return err
  2743. }
  2744. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2745. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2746. return
  2747. }
  2748. func EditWarehouseOut(warehouseOut models.WarehouseOut, tx *gorm.DB) {
  2749. err = tx.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
  2750. if err != nil {
  2751. tx.Rollback()
  2752. return
  2753. }
  2754. return
  2755. }
  2756. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2757. 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
  2758. }
  2759. func EditNewDrugWarehouseOut(warehouseOut models.DrugWarehouseOut, tx *gorm.DB) {
  2760. err = tx.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
  2761. if err != nil {
  2762. tx.Rollback()
  2763. return
  2764. }
  2765. return
  2766. }
  2767. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) (err error) {
  2768. err = tx.Save(&info).Error
  2769. if err != nil {
  2770. tx.Rollback()
  2771. return err
  2772. }
  2773. return err
  2774. }
  2775. 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) {
  2776. db := readDb.Model(&models.CancelStock{})
  2777. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2778. if len(keywords) > 0 {
  2779. likeKey := "%" + keywords + "%"
  2780. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2781. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2782. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2783. }
  2784. if startTime > 0 {
  2785. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2786. }
  2787. if endTime > 0 {
  2788. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2789. }
  2790. if storehouse_id > 0 {
  2791. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2792. }
  2793. if check_type > 0 {
  2794. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2795. }
  2796. db = db.Count(&total)
  2797. offset := (page - 1) * limit
  2798. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2799. return
  2800. }
  2801. 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) {
  2802. db := readDb.Model(&models.DrugCancelStock{})
  2803. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2804. if len(keywords) > 0 {
  2805. likeKey := "%" + keywords + "%"
  2806. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2807. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2808. 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")
  2809. }
  2810. if startTime > 0 {
  2811. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2812. }
  2813. if endTime > 0 {
  2814. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2815. }
  2816. if storehoue_id > 0 {
  2817. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2818. }
  2819. if check_type > 0 {
  2820. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2821. }
  2822. db = db.Count(&total)
  2823. offset := (page - 1) * limit
  2824. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2825. return
  2826. }
  2827. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2828. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2829. return info, err
  2830. }
  2831. func DeleteCancelStock(ids []string) (err error) {
  2832. ut := writeDb.Begin()
  2833. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2834. if err != nil {
  2835. ut.Rollback()
  2836. return
  2837. }
  2838. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2839. if err != nil {
  2840. ut.Rollback()
  2841. return
  2842. }
  2843. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2844. if err != nil {
  2845. ut.Rollback()
  2846. return
  2847. }
  2848. ut.Commit()
  2849. return
  2850. }
  2851. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2852. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2853. return info, err
  2854. }
  2855. func DeleteDrugCancelStock(ids []string) (err error) {
  2856. ut := writeDb.Begin()
  2857. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2858. if err != nil {
  2859. ut.Rollback()
  2860. return
  2861. }
  2862. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2863. if err != nil {
  2864. ut.Rollback()
  2865. return
  2866. }
  2867. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2868. if err != nil {
  2869. ut.Rollback()
  2870. return
  2871. }
  2872. ut.Commit()
  2873. return
  2874. }
  2875. func UpDateCancleStockStatus(id int64) (err error) {
  2876. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2877. return
  2878. }
  2879. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2880. info := models.CancelStockInfo{}
  2881. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2882. return info, err
  2883. }
  2884. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2885. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2886. return err
  2887. }
  2888. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2889. 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
  2890. return
  2891. }
  2892. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2893. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2894. return
  2895. }
  2896. func UpDateDrugCancelStockById(id int64) (err error) {
  2897. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2898. return
  2899. }
  2900. func EditCancelStock(sales models.CancelStock) {
  2901. 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
  2902. }
  2903. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2904. 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
  2905. }
  2906. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2907. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2908. return cancelStock, err
  2909. }
  2910. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2911. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2912. return cancelStock, err
  2913. }
  2914. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2915. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2916. return cancelStock, err
  2917. }
  2918. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2919. err = writeDb.Save(&info).Error
  2920. return err
  2921. }
  2922. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2923. err = writeDb.Save(&info).Error
  2924. return err
  2925. }
  2926. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2927. 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
  2928. return list, err
  2929. }
  2930. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2931. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2932. 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)
  2933. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2934. return list, err
  2935. }
  2936. type StockDetailModel struct {
  2937. GoodId int64 `gorm:"good_id" json:"good_id"`
  2938. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2939. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2940. Price float64 `gorm:"price" json:"price"`
  2941. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2942. Type int `gorm:"type" json:"type"`
  2943. Creater int64 `gorm:"creater" json:"creater"`
  2944. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2945. Types int64 `gorm:"types" json:"types"`
  2946. }
  2947. func FindStockInDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (list []*models.WarehousingInfo, total int64, err error) {
  2948. db := readDb.Model(&models.WarehousingInfo{})
  2949. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2950. if good_id > 0 {
  2951. db = db.Where("xt_warehouse_info.good_id = ?", good_id)
  2952. }
  2953. if len(keywords) > 0 {
  2954. likeKey := "%" + keywords + "%"
  2955. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2956. 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)
  2957. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2958. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2959. 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")
  2960. } else {
  2961. 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)
  2962. }
  2963. if manufacturer > 0 {
  2964. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2965. }
  2966. if storehouse_id > 0 {
  2967. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2968. }
  2969. if order_type > 0 {
  2970. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2971. }
  2972. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2973. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2974. db = db.Count(&total)
  2975. offset := (page - 1) * limit
  2976. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2977. return
  2978. }
  2979. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (err error, totalPrice float64) {
  2980. db := readDb.Model(&models.WarehousingInfo{})
  2981. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2982. if good_id > 0 {
  2983. db = db.Where("good_id = ?", good_id)
  2984. }
  2985. if len(keywords) > 0 {
  2986. likeKey := "%" + keywords + "%"
  2987. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2988. 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)
  2989. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2990. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2991. 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")
  2992. } else {
  2993. 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)
  2994. }
  2995. if manufacturer > 0 {
  2996. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2997. }
  2998. if order_type > 0 {
  2999. db = db.Where("xt_warehouse_info.type = ?", order_type)
  3000. }
  3001. if storehouse_id > 0 {
  3002. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  3003. }
  3004. type PriceAmount struct {
  3005. Total float64
  3006. }
  3007. var result []PriceAmount
  3008. err = db.Select("total_price as total").Scan(&result).Error
  3009. var total float64
  3010. for _, value := range result {
  3011. total = total + value.Total
  3012. }
  3013. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3014. return err, total
  3015. }
  3016. func FloatRound(f float64, n int) float64 {
  3017. format := "%." + strconv.Itoa(n) + "f"
  3018. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  3019. return res
  3020. }
  3021. func FindStockOutDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64, order_way int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  3022. if orgId == 9671 {
  3023. db := readDb.Model(&models.WarehouseOutInfo{})
  3024. 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)
  3025. if len(keywords) > 0 {
  3026. likeKey := "%" + keywords + "%"
  3027. 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)
  3028. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3029. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3030. 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")
  3031. } else {
  3032. 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)
  3033. }
  3034. if manufacturer > 0 {
  3035. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3036. }
  3037. if order_type > 0 {
  3038. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3039. }
  3040. if storehouse_id > 0 {
  3041. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3042. }
  3043. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  3044. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  3045. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3046. db = db.Count(&total)
  3047. offset := (page - 1) * limit
  3048. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3049. return
  3050. } else {
  3051. db := readDb.Model(&models.WarehouseOutInfo{})
  3052. 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.count <> 0 ", orgId)
  3053. if good_id > 0 {
  3054. db = db.Where("good_id = ?", good_id)
  3055. }
  3056. if order_way == 1 {
  3057. db = db.Where("xt_warehouse_out_info.is_sys =0")
  3058. }
  3059. if order_way == 2 {
  3060. db = db.Where("xt_warehouse_out_info.is_sys !=0")
  3061. }
  3062. if len(keywords) > 0 {
  3063. likeKey := "%" + keywords + "%"
  3064. 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)
  3065. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3066. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3067. 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")
  3068. } else {
  3069. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id and xt_warehouse_out.org_id = xt_warehouse_out_info.org_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)
  3070. }
  3071. if manufacturer > 0 {
  3072. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3073. }
  3074. if order_type > 0 {
  3075. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3076. }
  3077. if storehouse_id > 0 {
  3078. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3079. }
  3080. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ? and is_check =1", orgId)
  3081. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ? and is_check =1", orgId)
  3082. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3083. db = db.Count(&total)
  3084. offset := (page - 1) * limit
  3085. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3086. return
  3087. }
  3088. }
  3089. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64, order_way int64) (err error, totalPrice float64) {
  3090. db := readDb.Model(&models.WarehouseOutInfo{})
  3091. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.count > 0", orgId)
  3092. if len(keywords) > 0 {
  3093. likeKey := "%" + keywords + "%"
  3094. 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)
  3095. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3096. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3097. 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")
  3098. } else {
  3099. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id and xt_warehouse_out.org_id = xt_warehouse_out_info.org_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)
  3100. }
  3101. if good_id > 0 {
  3102. db = db.Where("xt_warehouse_out_info.good_id = ?", good_id)
  3103. }
  3104. if order_way == 1 {
  3105. db = db.Where("xt_warehouse_out_info.is_sys = ?", 0)
  3106. }
  3107. if order_way == 2 {
  3108. db = db.Where("xt_warehouse_out_info.is_sys != ?", 0)
  3109. }
  3110. if manufacturer > 0 {
  3111. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3112. }
  3113. if order_type > 0 {
  3114. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3115. }
  3116. if storehouse_id > 0 {
  3117. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3118. }
  3119. type PriceAmount struct {
  3120. Total float64
  3121. }
  3122. var result []PriceAmount
  3123. err = db.Select("(count * price) as total").Scan(&result).Error
  3124. var total float64
  3125. for _, value := range result {
  3126. total = total + value.Total
  3127. }
  3128. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3129. fmt.Println(total)
  3130. return err, total
  3131. }
  3132. 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) {
  3133. db := readDb.Model(&models.SalesReturnInfo{})
  3134. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  3135. if len(keywords) > 0 {
  3136. likeKey := "%" + keywords + "%"
  3137. 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)
  3138. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  3139. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  3140. 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")
  3141. } else {
  3142. 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)
  3143. }
  3144. if manufacturer > 0 {
  3145. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  3146. }
  3147. if order_type > 0 {
  3148. db = db.Where("xt_sales_return_info.type = ?", order_type)
  3149. }
  3150. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  3151. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3152. offset := (page - 1) * limit
  3153. db = db.Count(&total)
  3154. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  3155. return
  3156. }
  3157. func FindeCancelGroup(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (list []*models.CancelStockInfo, err error) {
  3158. db := readDb.Model(&models.CancelStockInfo{})
  3159. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3160. if len(keywords) > 0 {
  3161. likeKey := "%" + keywords + "%"
  3162. 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)
  3163. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3164. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3165. 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")
  3166. } else {
  3167. 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)
  3168. }
  3169. if manufacturer > 0 {
  3170. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3171. }
  3172. if order_type > 0 {
  3173. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3174. }
  3175. if storehouse_id > 0 {
  3176. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  3177. }
  3178. if good_id > 0 {
  3179. db = db.Where("xt_cancel_stock_info.good_id = ?", good_id)
  3180. }
  3181. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3182. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3183. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  3184. return
  3185. }
  3186. func FindCancelDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (list []*models.CancelStockInfo, total int64, err error) {
  3187. db := readDb.Model(&models.CancelStockInfo{})
  3188. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3189. if len(keywords) > 0 {
  3190. likeKey := "%" + keywords + "%"
  3191. 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)
  3192. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3193. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3194. 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")
  3195. } else {
  3196. 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)
  3197. }
  3198. if manufacturer > 0 {
  3199. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3200. }
  3201. if order_type > 0 {
  3202. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3203. }
  3204. if storehouse_id > 0 {
  3205. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  3206. }
  3207. if good_id > 0 {
  3208. db = db.Where("xt_cancel_stock_info.good_id =?", good_id)
  3209. }
  3210. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3211. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3212. db = db.Count(&total)
  3213. offset := (page - 1) * limit
  3214. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  3215. return
  3216. }
  3217. func FindStockInByDealerId(id int64) (total int64, err error) {
  3218. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  3219. return
  3220. }
  3221. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  3222. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  3223. return
  3224. }
  3225. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  3226. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  3227. return
  3228. }
  3229. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  3230. 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)
  3231. return
  3232. }
  3233. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  3234. 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)
  3235. return
  3236. }
  3237. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  3238. 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)
  3239. return
  3240. }
  3241. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  3242. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  3243. return goodType, err
  3244. }
  3245. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  3246. 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
  3247. return info, err
  3248. }
  3249. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  3250. 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
  3251. return
  3252. }
  3253. func FindStockOutByIsSysOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (out models.WarehouseOut, err error) {
  3254. err = tx.Model(&models.WarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).First(&out).Error
  3255. if err != gorm.ErrRecordNotFound {
  3256. if err != nil {
  3257. tx.Rollback()
  3258. return
  3259. }
  3260. }
  3261. return
  3262. }
  3263. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  3264. 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
  3265. return
  3266. }
  3267. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3268. 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
  3269. return
  3270. }
  3271. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  3272. 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
  3273. if err != gorm.ErrRecordNotFound {
  3274. if err != nil {
  3275. tx.Rollback()
  3276. return
  3277. }
  3278. }
  3279. return
  3280. }
  3281. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3282. 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
  3283. return
  3284. }
  3285. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  3286. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  3287. return
  3288. }
  3289. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  3290. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  3291. return
  3292. }
  3293. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3294. info := models.WarehouseOutInfo{}
  3295. 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
  3296. if err == gorm.ErrRecordNotFound {
  3297. return nil, err
  3298. }
  3299. if err != nil {
  3300. return nil, err
  3301. }
  3302. return &info, nil
  3303. }
  3304. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3305. info := models.WarehouseOutInfo{}
  3306. 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
  3307. if err == gorm.ErrRecordNotFound {
  3308. return nil, err
  3309. }
  3310. if err != nil {
  3311. return nil, err
  3312. }
  3313. return &info, nil
  3314. }
  3315. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3316. 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
  3317. return err
  3318. }
  3319. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3320. 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
  3321. return err
  3322. }
  3323. func GetGoodsIsExsit(good_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  3324. outInfo := models.WarehouseOutInfo{}
  3325. err := tx.Where("good_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", good_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3326. if err != gorm.ErrRecordNotFound {
  3327. if err != nil {
  3328. tx.Rollback()
  3329. return outInfo, err
  3330. }
  3331. }
  3332. return outInfo, err
  3333. }
  3334. func GetDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64) (models.XtDrugWarehouseOutInfo, error) {
  3335. outInfo := models.XtDrugWarehouseOutInfo{}
  3336. err := XTReadDB().Where("drug_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", drug_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3337. return outInfo, err
  3338. }
  3339. func GetNewDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.XtDrugWarehouseOutInfo, error) {
  3340. outInfo := models.XtDrugWarehouseOutInfo{}
  3341. err := tx.Where("drug_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", drug_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3342. if err != gorm.ErrRecordNotFound {
  3343. if err != nil {
  3344. tx.Rollback()
  3345. return outInfo, err
  3346. }
  3347. }
  3348. return outInfo, err
  3349. }
  3350. func AddSigleWarehouseOutInfoOne(info *models.WarehouseOutInfo) error {
  3351. err := XTWriteDB().Create(&info).Error
  3352. return err
  3353. }
  3354. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3355. err := tx.Create(&info).Error
  3356. if err != nil {
  3357. tx.Rollback()
  3358. return err
  3359. }
  3360. return err
  3361. }
  3362. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3363. err := tx.Create(&info).Error
  3364. if err != nil {
  3365. tx.Rollback()
  3366. return err
  3367. }
  3368. return err
  3369. }
  3370. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  3371. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3372. return err
  3373. }
  3374. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  3375. err := writeDb.Create(&info).Error
  3376. return err
  3377. }
  3378. func AddSelfSigleDrugWarehouseOutInfo(info *models.XtSelfDrugWarehouseOutInfo, tx *gorm.DB) error {
  3379. err := tx.Create(&info).Error
  3380. if err != nil {
  3381. tx.Rollback()
  3382. return err
  3383. }
  3384. return err
  3385. }
  3386. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  3387. err := tx.Create(&info).Error
  3388. if err != nil {
  3389. tx.Rollback()
  3390. return err
  3391. }
  3392. return err
  3393. }
  3394. func UpdateSigleDrugWarehouseOutInfoDetail(id int64, count int64, tx *gorm.DB) error {
  3395. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3396. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("over_count", gorm.Expr("over_count - ?", count)).Error
  3397. if err != nil {
  3398. tx.Rollback()
  3399. return err
  3400. }
  3401. return err
  3402. }
  3403. func UpdateSigleDrugWarehouseOutInfoDetailOne(id int64, count int64, tx *gorm.DB) error {
  3404. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"count": count}).Error
  3405. return err
  3406. }
  3407. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  3408. ut := writeDb.Begin()
  3409. err := ut.Create(&detail).Error
  3410. if err != nil {
  3411. ut.Rollback()
  3412. return err
  3413. }
  3414. ut.Commit()
  3415. return err
  3416. }
  3417. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  3418. err := writeDb.Create(&detail).Error
  3419. return err
  3420. }
  3421. func GetDrugAutoMaticReduceDetail(drug_id int64, patient_id int64, record_time int64, org_id int64, adivce_id int64, warehouse_info_id int64, tx *gorm.DB) (models.DrugAutomaticReduceDetail, error) {
  3422. detail := models.DrugAutomaticReduceDetail{}
  3423. err := tx.Where("drug_id = ? and patient_id =? and record_time =? and org_id = ? and advice_id = ? and warehouse_info_id =?", drug_id, patient_id, record_time, org_id, adivce_id, warehouse_info_id).Find(&detail).Error
  3424. if err != gorm.ErrRecordNotFound {
  3425. if err != nil {
  3426. tx.Rollback()
  3427. return detail, err
  3428. }
  3429. }
  3430. return detail, err
  3431. }
  3432. func UpdateNewSingleDrugAutoReduceRecordInfo(id int64, count int64, tx *gorm.DB) error {
  3433. err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3434. if err != nil {
  3435. tx.Rollback()
  3436. return err
  3437. }
  3438. return err
  3439. }
  3440. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  3441. err := tx.Create(&detail).Error
  3442. if err != nil {
  3443. tx.Rollback()
  3444. return err
  3445. }
  3446. return err
  3447. }
  3448. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  3449. 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
  3450. return
  3451. }
  3452. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  3453. 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
  3454. return
  3455. }
  3456. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  3457. 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
  3458. return
  3459. }
  3460. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  3461. info := models.WarehouseOutInfo{}
  3462. 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
  3463. return info, err
  3464. }
  3465. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  3466. 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
  3467. return
  3468. }
  3469. func UpdateStockOutInfoCount(org_id int64, id int64) {
  3470. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  3471. }
  3472. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3473. db := readDb.Model(&models.AutomaticReduceDetail{})
  3474. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3475. db = db.Preload("GoodsType", "status = 1")
  3476. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3477. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  3478. db = db.Count(&total)
  3479. err = db.Find(&user).Error
  3480. return
  3481. }
  3482. type DrugAutomaticReduceDetail struct {
  3483. ID int64 `gorm:"column:id" json:"id" form:"id"`
  3484. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  3485. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  3486. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  3487. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  3488. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3489. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3490. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3491. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3492. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3493. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3494. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3495. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3496. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3497. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3498. }
  3499. func (DrugAutomaticReduceDetail) TableName() string {
  3500. return "xt_drug_automatic_reduce_detail"
  3501. }
  3502. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3503. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3504. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3505. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3506. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3507. db = db.Count(&total)
  3508. err = db.Find(&user).Error
  3509. return
  3510. }
  3511. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3512. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3513. }
  3514. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3515. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3516. }
  3517. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3518. 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
  3519. return
  3520. }
  3521. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3522. 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
  3523. return
  3524. }
  3525. type VMWarehousingInfo struct {
  3526. ID int64 `gorm:"column:id" json:"id"`
  3527. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3528. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3529. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3530. Status int64 `gorm:"column:status" json:"status"`
  3531. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3532. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3533. }
  3534. func (VMWarehousingInfo) TableName() string {
  3535. return "xt_warehouse_info"
  3536. }
  3537. type GoodInfo struct {
  3538. ID int64 `gorm:"column:id" json:"id"`
  3539. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3540. Status int64 `gorm:"column:status" json:"status"`
  3541. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3542. }
  3543. func (GoodInfo) TableName() string {
  3544. return "xt_good_information"
  3545. }
  3546. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3547. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3548. return
  3549. }
  3550. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3551. 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
  3552. return
  3553. }
  3554. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3555. likeKey := "%" + keyword + "%"
  3556. db := XTReadDB().Model(&goods)
  3557. if len(keyword) > 0 {
  3558. db = db.Where("type_name like ?", likeKey)
  3559. }
  3560. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3561. return goods, err
  3562. }
  3563. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3564. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3565. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3566. return goodstype, err
  3567. }
  3568. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3569. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3570. if orgid > 0 {
  3571. db = db.Where("x.user_org_id = ?", orgid)
  3572. }
  3573. if recordtime > 0 {
  3574. db = db.Where("x.record_date = ?", recordtime)
  3575. }
  3576. if patientId > 0 {
  3577. db = db.Where("x.patient_id = ?", patientId)
  3578. }
  3579. 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
  3580. return prepare, err
  3581. }
  3582. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3583. detail := models.XtAutomaticReduceDetail{}
  3584. 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
  3585. if err == gorm.ErrRecordNotFound {
  3586. return nil, err
  3587. }
  3588. if err != nil {
  3589. return nil, err
  3590. }
  3591. return &detail, nil
  3592. }
  3593. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3594. 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
  3595. return autoMatic, err
  3596. }
  3597. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3598. 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
  3599. return autoMatic, err
  3600. }
  3601. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3602. var manufacturer models.Manufacturer
  3603. var err error
  3604. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3605. if err == gorm.ErrRecordNotFound {
  3606. return nil, err
  3607. }
  3608. if err != nil {
  3609. return nil, err
  3610. }
  3611. return &manufacturer, nil
  3612. }
  3613. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3614. err := XTWriteDB().Create(manufacturer).Error
  3615. return err
  3616. }
  3617. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3618. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3619. return err
  3620. }
  3621. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3622. var good models.GoodInfo
  3623. var err error
  3624. 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
  3625. if err == gorm.ErrRecordNotFound {
  3626. return nil, err
  3627. }
  3628. if err != nil {
  3629. return nil, err
  3630. }
  3631. return &good, nil
  3632. }
  3633. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3634. err := XTWriteDB().Create(&info).Error
  3635. return err
  3636. }
  3637. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3638. err := XTWriteDB().Model(&info).Where("good_name = ? and good_type_id = ? and specification_name = ? and org_id = ? and status = 1", goodName, goodType, specificationName, orgid).Updates(map[string]interface{}{"good_name": info.GoodName, "good_type_id": info.GoodTypeId, "medical_insurance_level": info.MedicalInsuranceLevel, "good_kind": info.GoodKind, "specification_name": info.SpecificationName, "manufacturer": info.Manufacturer, "good_unit": info.GoodUnit, "retail_price": info.RetailPrice, "stock_warn_count": info.StockWarnCount, "dealer": info.Dealer, "pinyin": info.Pinyin, "wubi": info.Wubi, "buy_price": info.BuyPrice, "medical_insurance_number": info.MedicalInsuranceNumber, "is_special_diseases": info.IsSpecialDiseases, "is_record": info.IsRecord, "statistics_category": info.StatisticsCategory, "good_status": info.GoodStatus, "social_security_directory_code": info.SocialSecurityDirectoryCode, "production_type": info.ProductionType, "special_medical": info.SpecialMedical, "remark": info.Remark, "number": info.Number, "register_number": info.RegisterNumber}).Error
  3639. return err
  3640. }
  3641. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3642. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3643. 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
  3644. return houseOutInfo, err
  3645. }
  3646. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3647. key := "%" + keyword + "%"
  3648. if len(keyword) == 0 {
  3649. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3650. } else {
  3651. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3652. }
  3653. return goodInfo, err
  3654. }
  3655. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3656. var drug models.BaseDrugLib
  3657. var err error
  3658. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3659. if err == gorm.ErrRecordNotFound {
  3660. return nil, err
  3661. }
  3662. if err != nil {
  3663. return nil, err
  3664. }
  3665. return &drug, nil
  3666. }
  3667. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3668. var drug models.BaseDrugLib
  3669. var err error
  3670. 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
  3671. if err == gorm.ErrRecordNotFound {
  3672. return nil, err
  3673. }
  3674. if err != nil {
  3675. return nil, err
  3676. }
  3677. return &drug, nil
  3678. }
  3679. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3680. err := XTWriteDB().Create(&info).Error
  3681. return err
  3682. }
  3683. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3684. err := XTWriteDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and org_id = ? and status = 1", drugName, drugSpec, orgid).Updates(map[string]interface{}{"drug_name": drug.DrugName, "drug_alias": drug.DrugAlias, "drug_spec": drug.DrugSpec, "drug_type": drug.DrugType, "drug_stock_limit": drug.DrugStockLimit, "drug_origin_place": drug.DrugOriginPlace, "drug_dosage_form": drug.DrugDosageForm, "medical_insurance_level": drug.MedicalInsuranceLevel, "max_unit": drug.MaxUnit, "min_unit": drug.MinUnit, "unit_matrixing": drug.UnitMatrixing, "retail_price": drug.RetailPrice, "last_price": drug.LastPrice, "drug_classify": drug.DrugClassify, "manufacturer": drug.Manufacturer, "dealer": drug.Dealer, "pinyin": drug.Pinyin, "wubi": drug.Wubi, "drug_alias_pinyin": drug.DrugAliasPinyin, "drug_alias_wubi": drug.DrugAliasWubi, "drug_category": drug.DrugCategory, "drug_control": drug.DrugControl, "number": drug.Number, "hosp_appr_flag": drug.HospApprFlag, "medical_insurance_number": drug.MedicalInsuranceNumber, "pharmacology_category": drug.PharmacologyCategory, "statistics_category": drug.StatisticsCategory, "code": drug.Code, "is_special_diseases": drug.IsSpecialDiseases, "is_record": drug.IsRecord, "prescription_mark": drug.PrescriptionMark, "social_security_directory_code": drug.SocialSecurityDirectoryCode, "record_date": drug.RecordDate, "drug_remark": drug.DrugRemark, "drug_status": drug.DrugStatus, "limit_remark": drug.LimitRemark, "drug_dose": drug.DrugDose, "single_dose": drug.DrugDose, "drug_dose_unit": drug.DrugDoseUnit, "lmt_used_flag": drug.LmtUsedFlag, "delivery_way": drug.DeliveryWay, "execution_frequency": drug.ExecutionFrequency, "drug_day": drug.DrugDay}).Error
  3685. return err
  3686. }
  3687. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3688. var projoect models.HisProject
  3689. var err error
  3690. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3691. if err == gorm.ErrRecordNotFound {
  3692. return nil, err
  3693. }
  3694. if err != nil {
  3695. return nil, err
  3696. }
  3697. return &projoect, nil
  3698. }
  3699. func CreateProjectInfomation(info *models.HisProject) error {
  3700. err := XTWriteDB().Create(&info).Error
  3701. return err
  3702. }
  3703. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3704. 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
  3705. return err
  3706. }
  3707. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3708. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3709. return warehouseinfo, err
  3710. }
  3711. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3712. 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
  3713. return err
  3714. }
  3715. func EditNewDrugWarehousingOne(warehouse models.DrugWarehouse, id int64, tx *gorm.DB) error {
  3716. err := tx.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
  3717. if err != nil {
  3718. tx.Rollback()
  3719. return err
  3720. }
  3721. return err
  3722. }
  3723. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3724. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3725. return list, err
  3726. }
  3727. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3728. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3729. return list, err
  3730. }
  3731. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3732. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3733. return list, err
  3734. }
  3735. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3736. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3737. // return list, err
  3738. // }
  3739. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3740. var dealer models.Dealer
  3741. var err error
  3742. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3743. if err == gorm.ErrRecordNotFound {
  3744. return nil, err
  3745. }
  3746. if err != nil {
  3747. return nil, err
  3748. }
  3749. return &dealer, nil
  3750. }
  3751. func CreateDealer(dealer *models.Dealer) error {
  3752. err := XTWriteDB().Create(&dealer).Error
  3753. return err
  3754. }
  3755. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3756. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3757. return err
  3758. }
  3759. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3760. dataconfig := models.DictDataconfig{}
  3761. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3762. return dataconfig, err
  3763. }
  3764. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3765. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3766. return dataconfig, err
  3767. }
  3768. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3769. var dataconfig models.DictDataconfig
  3770. var err error
  3771. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3772. if err == gorm.ErrRecordNotFound {
  3773. return nil, err
  3774. }
  3775. if err != nil {
  3776. return nil, err
  3777. }
  3778. return &dataconfig, nil
  3779. }
  3780. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3781. dataconfig := models.DictDataconfig{}
  3782. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3783. return dataconfig, err
  3784. }
  3785. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3786. err := XTWriteDB().Create(&dataconfig).Error
  3787. return err
  3788. }
  3789. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3790. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3791. return err
  3792. }
  3793. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3794. var goodType models.GoodsType
  3795. var err error
  3796. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3797. if err == gorm.ErrRecordNotFound {
  3798. return nil, err
  3799. }
  3800. if err != nil {
  3801. return nil, err
  3802. }
  3803. return &goodType, nil
  3804. }
  3805. func CreatedGoodType(goodsType *models.GoodsType) error {
  3806. err := XTWriteDB().Create(&goodsType).Error
  3807. return err
  3808. }
  3809. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3810. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3811. return goodsType, err
  3812. }
  3813. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3814. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3815. return goodsType, err
  3816. }
  3817. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3818. var department models.XtHisDepartment
  3819. var err error
  3820. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3821. if err == gorm.ErrRecordNotFound {
  3822. return nil, err
  3823. }
  3824. if err != nil {
  3825. return nil, err
  3826. }
  3827. return &department, nil
  3828. }
  3829. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3830. dataconfig := models.Dataconfig{}
  3831. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3832. return dataconfig, err
  3833. }
  3834. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3835. var dataconfig models.Dataconfig
  3836. var err error
  3837. 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
  3838. if err == gorm.ErrRecordNotFound {
  3839. return nil, err
  3840. }
  3841. if err != nil {
  3842. return nil, err
  3843. }
  3844. return &dataconfig, nil
  3845. }
  3846. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3847. dataconfig := models.Dataconfig{}
  3848. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3849. return dataconfig, err
  3850. }
  3851. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3852. err := XTWriteDB().Create(&dataconfig).Error
  3853. return err
  3854. }
  3855. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3856. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3857. return dataconfig, err
  3858. }
  3859. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3860. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3861. return list, err
  3862. }
  3863. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3864. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3865. if len(keywords) > 0 {
  3866. likekey := "%" + keywords + "%"
  3867. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3868. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3869. }).Find(&drug).Error
  3870. } else {
  3871. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3872. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3873. }).Find(&drug).Error
  3874. }
  3875. return
  3876. }
  3877. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3878. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3879. if len(keywords) > 0 {
  3880. likekey := "%" + keywords + "%"
  3881. 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 {
  3882. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3883. }).Find(&drug).Error
  3884. } else {
  3885. 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 {
  3886. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3887. }).Find(&drug).Error
  3888. }
  3889. return
  3890. }
  3891. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3892. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3893. if len(keywords) > 0 {
  3894. likekey := "%" + keywords + "%"
  3895. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3896. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3897. }).Find(&good).Error
  3898. } else {
  3899. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3900. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3901. }).Find(&good).Error
  3902. }
  3903. return
  3904. }
  3905. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3906. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3907. if len(keywords) > 0 {
  3908. likekey := "%" + keywords + "%"
  3909. 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 {
  3910. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3911. }).Find(&good).Error
  3912. } else {
  3913. 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 {
  3914. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3915. }).Find(&good).Error
  3916. }
  3917. return
  3918. }
  3919. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3920. db := writeDb.Begin()
  3921. err := db.Create(&detail).Error
  3922. if err != nil {
  3923. db.Rollback()
  3924. return err
  3925. }
  3926. db.Commit()
  3927. return err
  3928. }
  3929. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3930. var autoreduece models.BloodAutomaticReduceDetail
  3931. var err error
  3932. 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
  3933. if err == gorm.ErrRecordNotFound {
  3934. return nil, err
  3935. }
  3936. if err != nil {
  3937. return nil, err
  3938. }
  3939. return &autoreduece, nil
  3940. }
  3941. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3942. auto := models.BloodAutomaticReduceDetail{}
  3943. 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
  3944. return auto, err
  3945. }
  3946. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3947. detail := models.BloodAutomaticReduceDetail{}
  3948. 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
  3949. return err
  3950. }
  3951. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3952. 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
  3953. return stockflow, err
  3954. }
  3955. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3956. 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
  3957. 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
  3958. 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
  3959. return err
  3960. }
  3961. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3962. detail := models.BloodAutomaticReduceDetail{}
  3963. 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
  3964. info := models.WarehouseOutInfo{}
  3965. 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
  3966. prepare := models.DialysisBeforePrepare{}
  3967. 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
  3968. flow := models.VmStockFlow{}
  3969. 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
  3970. return err
  3971. }
  3972. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3973. detail := models.BloodAutomaticReduceDetail{}
  3974. 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
  3975. info := models.WarehouseOutInfo{}
  3976. 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
  3977. prepare := models.DialysisBeforePrepare{}
  3978. 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
  3979. flow := models.VmStockFlow{}
  3980. 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
  3981. return err
  3982. }
  3983. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  3984. detail := models.BloodAutomaticReduceDetail{}
  3985. 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
  3986. info := models.WarehouseOutInfo{}
  3987. 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
  3988. prepare := models.DialysisBeforePrepare{}
  3989. 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
  3990. flow := models.VmStockFlow{}
  3991. 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
  3992. return err
  3993. }
  3994. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  3995. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3996. //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
  3997. return err
  3998. }
  3999. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  4000. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  4001. return info, err
  4002. }
  4003. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  4004. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  4005. return list, err
  4006. }
  4007. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  4008. 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
  4009. return err
  4010. }
  4011. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  4012. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  4013. return err
  4014. }
  4015. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  4016. var autoreduece models.BloodAutomaticReduceDetail
  4017. var err error
  4018. 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
  4019. if err == gorm.ErrRecordNotFound {
  4020. return nil, err
  4021. }
  4022. if err != nil {
  4023. return nil, err
  4024. }
  4025. return &autoreduece, nil
  4026. }
  4027. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  4028. detail := models.WarehouseOutInfoSeven{}
  4029. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  4030. return detail, err
  4031. }
  4032. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  4033. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  4034. return detail, err
  4035. }
  4036. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  4037. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  4038. return project, err
  4039. }
  4040. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  4041. project := models.HisPrescriptionProject{}
  4042. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  4043. return project, err
  4044. }
  4045. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  4046. 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
  4047. return auto, err
  4048. }
  4049. func ModefyWarehouseInfo(count int64, id int64) error {
  4050. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4051. return err
  4052. }
  4053. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  4054. 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
  4055. 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
  4056. 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
  4057. //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
  4058. return err
  4059. }
  4060. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  4061. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4062. return
  4063. }
  4064. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  4065. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4066. return
  4067. }
  4068. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  4069. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4070. return info, err
  4071. }
  4072. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4073. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4074. return info, err
  4075. }
  4076. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4077. 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
  4078. return info, err
  4079. }
  4080. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  4081. offset := (page - 1) * limit
  4082. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  4083. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4084. fmt.Println(table)
  4085. if startime > 0 {
  4086. db = db.Where("x.ctime>=?", startime)
  4087. }
  4088. if endtime > 0 {
  4089. db = db.Where("x.ctime<=?", endtime)
  4090. }
  4091. likeKey := "%" + keyword + "%"
  4092. if len(likeKey) > 0 {
  4093. db = db.Where("t.good_name like ?", likeKey)
  4094. }
  4095. if orgid > 0 {
  4096. db = db.Where("x.org_id = ?", orgid)
  4097. }
  4098. if good_type > 0 {
  4099. db = db.Where("t.good_type_id = ?", good_type)
  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.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
  4102. return info, total, err
  4103. }
  4104. 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) {
  4105. offset := (page - 1) * limit
  4106. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  4107. likeKey := "%" + keyword + "%"
  4108. if len(likeKey) > 0 {
  4109. db = db.Where("x.good_name like ?", likeKey)
  4110. }
  4111. if orgid > 0 {
  4112. db = db.Where("x.org_id = ?", orgid)
  4113. }
  4114. if good_type > 0 {
  4115. db = db.Where("x.good_type_id = ?", good_type)
  4116. }
  4117. if good_id > 0 {
  4118. db = db.Where("x.id = ?", good_id)
  4119. }
  4120. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4121. if startime > 0 {
  4122. db = db.Where("ctime>=?", startime)
  4123. }
  4124. if endtime > 0 {
  4125. db = db.Where("ctime<=?", endtime)
  4126. }
  4127. if good_id > 0 {
  4128. db = db.Where("good_id = ?", good_id)
  4129. }
  4130. if storehouse_id > 0 {
  4131. db = db.Where("storehouse_id = ?", storehouse_id)
  4132. }
  4133. return db.Where("status = 1 and is_check = 1")
  4134. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4135. if startime > 0 {
  4136. db = db.Where("ctime>=?", startime)
  4137. }
  4138. if endtime > 0 {
  4139. db = db.Where("ctime<=?", endtime)
  4140. }
  4141. if good_id > 0 {
  4142. db = db.Where("good_id = ?", good_id)
  4143. }
  4144. if storehouse_id > 0 {
  4145. db = db.Where("storehouse_id = ?", storehouse_id)
  4146. }
  4147. return db.Where("status = 1").Group("good_id,storehouse_id")
  4148. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4149. if startime > 0 {
  4150. db = db.Where("ctime>=?", startime)
  4151. }
  4152. if endtime > 0 {
  4153. db = db.Where("ctime<=?", endtime)
  4154. }
  4155. return db.Where("status = 1")
  4156. }).Find(&info).Error
  4157. return info, total, err
  4158. }
  4159. 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) {
  4160. offset := (page - 1) * limit
  4161. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  4162. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4163. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  4164. fmt.Println(table, tables)
  4165. if startime > 0 {
  4166. db = db.Where("x.ctime>=?", startime)
  4167. }
  4168. if endtime > 0 {
  4169. db = db.Where("x.ctime<=?", endtime)
  4170. }
  4171. if startfirsttime > 0 {
  4172. db = db.Where("x.expiry_date >=?", startfirsttime)
  4173. }
  4174. if endfirsttime > 0 {
  4175. db = db.Where("x.expiry_date <=?", endfirsttime)
  4176. }
  4177. if orgid > 0 {
  4178. db = db.Where("x.org_id = ?", orgid)
  4179. }
  4180. if good_id > 0 {
  4181. db = db.Where("x.good_id = ?", good_id)
  4182. }
  4183. 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
  4184. return info, total, err
  4185. }
  4186. 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) {
  4187. offset := (page - 1) * limit
  4188. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  4189. if startime > 0 {
  4190. db = db.Where("x.ctime>=?", startime)
  4191. }
  4192. if endtime > 0 {
  4193. db = db.Where("x.ctime<=?", endtime)
  4194. }
  4195. if orgid > 0 {
  4196. db = db.Where("x.org_id = ?", orgid)
  4197. }
  4198. if good_id > 0 {
  4199. db = db.Where("x.good_id = ?", good_id)
  4200. }
  4201. if is_sys == 2 {
  4202. db = db.Where("x.is_sys = 1")
  4203. }
  4204. if is_sys == 3 {
  4205. db = db.Where("x.is_sys = 0")
  4206. }
  4207. 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
  4208. return info, total, err
  4209. }
  4210. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4211. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4212. if startime > 0 {
  4213. db = db.Where("x.ctime >=?", startime)
  4214. }
  4215. if endtime > 0 {
  4216. db = db.Where("x.ctime<=?", endtime)
  4217. }
  4218. if orgid > 0 {
  4219. db = db.Where("x.org_id = ?", orgid)
  4220. }
  4221. 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
  4222. return info, err
  4223. }
  4224. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4225. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4226. if startime > 0 {
  4227. db = db.Where("x.ctime >=?", startime)
  4228. }
  4229. if endtime > 0 {
  4230. db = db.Where("x.ctime<=?", endtime)
  4231. }
  4232. if orgid > 0 {
  4233. db = db.Where("x.org_id = ?", orgid)
  4234. }
  4235. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  4236. return info, err
  4237. }
  4238. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  4239. if endtime > 0 {
  4240. 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
  4241. }
  4242. if endtime == 0 {
  4243. 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
  4244. }
  4245. return autoMatic, err
  4246. }
  4247. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  4248. out := models.WarehouseOut{}
  4249. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  4250. return out, err
  4251. }
  4252. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4253. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4254. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4255. fmt.Println(table)
  4256. if id > 0 {
  4257. db = db.Where("x.warehouse_out_id = ?", id)
  4258. }
  4259. if orgid > 0 {
  4260. db = db.Where("x.org_id = ?", orgid)
  4261. }
  4262. 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
  4263. return out, err
  4264. }
  4265. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  4266. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  4267. if len(id) > 0 {
  4268. db = db.Where("x.id in(?)", id)
  4269. }
  4270. if orgid > 0 {
  4271. db = db.Where("x.org_id= ?", orgid)
  4272. }
  4273. 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
  4274. return out, err
  4275. }
  4276. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4277. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4278. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4279. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  4280. fmt.Println(table, tableOne)
  4281. if len(id) > 0 {
  4282. db = db.Where("x.warehouse_out_id in (?)", id)
  4283. }
  4284. if orgid > 0 {
  4285. db = db.Where("x.org_id = ?", orgid)
  4286. }
  4287. err = db.Select("x.id,x.warehouse_out_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,x.manufacturer,t.social_security_directory_code,x.is_sys,x.sys_record_time,n.number,x.remark,x.license_number,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.warehouse_info_id,x.remark,x.patient_id,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit,t.packing_price,x.register_number,x.class_type").Joins("left join xt_warehouse_info as n on n.id=x.warehouse_info_id").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.warehouse_out_id,x.good_id").Order("x.ctime desc").Scan(&out).Error
  4288. return out, err
  4289. }
  4290. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4291. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4292. if len(id) > 0 {
  4293. db = db.Where("warehouse_out_id in (?)", id)
  4294. }
  4295. if orgid > 0 {
  4296. db = db.Where("user_org_id = ?", orgid)
  4297. }
  4298. err = db.Group("warehousing_id").Find(&list).Error
  4299. return list, err
  4300. }
  4301. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4302. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4303. if len(id) > 0 {
  4304. db = db.Where("warehouse_out_id in (?)", id)
  4305. }
  4306. if orgid > 0 {
  4307. db = db.Where("user_org_id = ?", orgid)
  4308. }
  4309. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4310. return list, err
  4311. }
  4312. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4313. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  4314. if len(id) > 0 {
  4315. db = db.Where("warehouse_out_id in (?)", id)
  4316. }
  4317. if orgid > 0 {
  4318. db = db.Where("user_org_id = ?", orgid)
  4319. }
  4320. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4321. return list, err
  4322. }
  4323. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  4324. db := XTReadDB().Model(&info)
  4325. db = db.Preload("GoodInfo", "status = 1")
  4326. 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
  4327. return info, err
  4328. }
  4329. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  4330. db := readDb.Model(&models.AutomaticReduceDetail{})
  4331. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  4332. db = db.Preload("GoodsType", "status = 1")
  4333. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  4334. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  4335. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  4336. db = db.Count(&total)
  4337. err = db.Find(&user).Error
  4338. return
  4339. }
  4340. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  4341. info := models.GoodInfo{}
  4342. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4343. return info, err
  4344. }
  4345. func GetNewGoodInformationByGoodId(good_id int64, tx *gorm.DB) (models.GoodInfo, error) {
  4346. info := models.GoodInfo{}
  4347. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4348. if err != gorm.ErrRecordNotFound {
  4349. if err != nil {
  4350. tx.Rollback()
  4351. return info, err
  4352. }
  4353. }
  4354. return info, err
  4355. }
  4356. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  4357. info := models.GoodInformationThirty{}
  4358. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4359. return info, err
  4360. }
  4361. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  4362. info := models.GoodInformationThirty{}
  4363. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4364. if err != gorm.ErrRecordNotFound {
  4365. if err != nil {
  4366. tx.Rollback()
  4367. return info, err
  4368. }
  4369. }
  4370. return info, err
  4371. }
  4372. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  4373. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  4374. return err
  4375. }
  4376. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  4377. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4378. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4379. fmt.Println(table)
  4380. if orgid > 0 {
  4381. db = db.Where("x.org_id = ?", orgid)
  4382. }
  4383. if len(orderid) > 0 {
  4384. db = db.Where("x.warehousing_id in(?)", orderid)
  4385. }
  4386. if startime > 0 {
  4387. db = db.Where("x.ctime>=?", startime)
  4388. }
  4389. if endtime > 0 {
  4390. db = db.Where("x.ctime<=?", endtime)
  4391. }
  4392. 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
  4393. return info, err
  4394. }
  4395. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  4396. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4397. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4398. fmt.Println(table)
  4399. if orgid > 0 {
  4400. db = db.Where("x.org_id = ?", orgid)
  4401. }
  4402. if len(orderid) > 0 {
  4403. db = db.Where("x.warehousing_id in(?)", orderid)
  4404. }
  4405. if startime > 0 {
  4406. db = db.Where("x.ctime>=?", startime)
  4407. }
  4408. if endtime > 0 {
  4409. db = db.Where("x.ctime<=?", endtime)
  4410. }
  4411. if storehouse_id > 0 {
  4412. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4413. }
  4414. 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
  4415. return info, err
  4416. }
  4417. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4418. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4419. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  4420. fmt.Println(table)
  4421. if startime > 0 {
  4422. db = db.Where("x.ctime>=?", startime)
  4423. }
  4424. if endtime > 0 {
  4425. db = db.Where("x.ctime<=?", endtime)
  4426. }
  4427. if orgid > 0 {
  4428. db = db.Where("x.org_id = ?", orgid)
  4429. }
  4430. 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
  4431. return info, err
  4432. }
  4433. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  4434. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  4435. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4436. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  4437. fmt.Println(table, tableOne)
  4438. if orgid > 0 {
  4439. db = db.Where("x.org_id = ?", orgid)
  4440. }
  4441. if orgid > 0 {
  4442. db = db.Where("t.org_id = ?", orgid)
  4443. }
  4444. if orgid > 0 {
  4445. db = db.Where("p.org_id = ?", orgid)
  4446. }
  4447. if len(ids) > 0 {
  4448. db = db.Where("x.warehouse_out_id in (?)", ids)
  4449. }
  4450. if startime > 0 {
  4451. db = db.Where("x.ctime>=?", startime)
  4452. }
  4453. if endtime > 0 {
  4454. db = db.Where("x.ctime<=?", endtime)
  4455. }
  4456. err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.number,x.license_number,x.warehouse_out_order_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,x.manufacturer,x.dealer,p.warehouse_out_time").Joins("left join xt_warehouse_out as p on p.id =x.warehouse_out_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&out).Error
  4457. return out, err
  4458. }
  4459. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  4460. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  4461. if len(ids) > 0 {
  4462. db = db.Where("x.id in(?)", ids)
  4463. }
  4464. if orgid > 0 {
  4465. db = db.Where("x.org_id = ?", orgid)
  4466. }
  4467. 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
  4468. return info, err
  4469. }
  4470. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4471. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  4472. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  4473. fmt.Println(table)
  4474. if len(ids) > 0 {
  4475. db = db.Where("x.warehouse_out_id in(?)", ids)
  4476. }
  4477. if orgid > 0 {
  4478. db = db.Where("x.org_id = ?", orgid)
  4479. }
  4480. 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
  4481. return info, err
  4482. }
  4483. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  4484. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  4485. likeKey := "%" + keyword + "%"
  4486. if len(keyword) > 0 {
  4487. db = db.Where("x.drug_name like ?", likeKey)
  4488. }
  4489. if orgid > 0 {
  4490. db = db.Where("x.org_id = ?", orgid)
  4491. }
  4492. err = db.Find(&basedrug).Error
  4493. return basedrug, err
  4494. }
  4495. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4496. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4497. if len(ids) > 0 {
  4498. db = db.Where("x.drug_id in(?)", ids)
  4499. }
  4500. if orgid > 0 {
  4501. db = db.Where("x.org_id = ?", orgid)
  4502. }
  4503. if startime > 0 {
  4504. db = db.Where("x.ctime>=?", startime)
  4505. }
  4506. if endtime > 0 {
  4507. db = db.Where("x.ctime<=?", endtime)
  4508. }
  4509. if storehouse_id > 0 {
  4510. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4511. }
  4512. err = db.Find(&info).Error
  4513. return info, err
  4514. }
  4515. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4516. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4517. if len(ids) > 0 {
  4518. db = db.Where("x.drug_id in(?)", ids)
  4519. }
  4520. if orgid > 0 {
  4521. db = db.Where("x.org_id = ?", orgid)
  4522. }
  4523. if startime > 0 {
  4524. db = db.Where("x.ctime>=?", startime)
  4525. }
  4526. if endtime > 0 {
  4527. db = db.Where("x.ctime<=?", endtime)
  4528. }
  4529. if storehouse_id > 0 {
  4530. db = db.Where("storehouse_id = ?", storehouse_id)
  4531. }
  4532. err = db.Find(&info).Error
  4533. return info, err
  4534. }
  4535. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4536. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4537. if drugid > 0 {
  4538. db = db.Where("x.drug_id = ?", drugid)
  4539. }
  4540. if orgid > 0 {
  4541. db = db.Where("x.org_id = ?", orgid)
  4542. }
  4543. if storehouse_id > 0 {
  4544. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4545. }
  4546. 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
  4547. return info, err
  4548. }
  4549. func GetDrugTotalCountTwoTwety(drugid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info models.VmDrugWarehouseInfo, err error) {
  4550. db := tx.Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4551. if drugid > 0 {
  4552. db = db.Where("x.drug_id = ?", drugid)
  4553. }
  4554. if orgid > 0 {
  4555. db = db.Where("x.org_id = ?", orgid)
  4556. }
  4557. if storehouse_id > 0 {
  4558. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4559. }
  4560. 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
  4561. if err != gorm.ErrRecordNotFound {
  4562. if err != nil {
  4563. tx.Rollback()
  4564. return info, err
  4565. }
  4566. }
  4567. return info, err
  4568. }
  4569. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4570. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4571. if drugid > 0 {
  4572. db = db.Where("x.drug_id = ?", drugid)
  4573. }
  4574. if orgid > 0 {
  4575. db = db.Where("x.org_id = ?", orgid)
  4576. }
  4577. if storehouse_id > 0 {
  4578. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4579. }
  4580. 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
  4581. return info, err
  4582. }
  4583. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4584. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4585. if id > 0 {
  4586. db = db.Where("x.id = ?", id)
  4587. }
  4588. if orgid > 0 {
  4589. db = db.Where("x.org_id = ?", orgid)
  4590. }
  4591. 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
  4592. return info, err
  4593. }
  4594. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4595. info := models.VmDrugWarehouseInfo{}
  4596. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4597. if id == 0 {
  4598. db = db.Where("id = ?", id)
  4599. }
  4600. if drugid > 0 {
  4601. db = db.Where("x.drug_id = ?", drugid)
  4602. }
  4603. if orgid > 0 {
  4604. db = db.Where("x.org_id = ?", orgid)
  4605. }
  4606. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4607. return info, err
  4608. }
  4609. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4610. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4611. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4612. fmt.Println(table)
  4613. if drugid > 0 {
  4614. db = db.Where("x.drug_id = ?", drugid)
  4615. }
  4616. if storehouse_id > 0 {
  4617. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4618. }
  4619. 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
  4620. return list, err
  4621. }
  4622. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4623. lib := models.BaseDrugLib{}
  4624. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4625. return lib, err
  4626. }
  4627. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4628. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4629. if goodid > 0 {
  4630. db = db.Where("x.good_id = ?", goodid)
  4631. }
  4632. if orgid > 0 {
  4633. db = db.Where("x.org_id = ?", orgid)
  4634. }
  4635. 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
  4636. return info, err
  4637. }
  4638. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4639. 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
  4640. return err
  4641. }
  4642. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4643. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4644. return info, err
  4645. }
  4646. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4647. //遍历循环入库数据
  4648. for i, item := range list {
  4649. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4650. if (item.WarehousingCount - item.StockCount) >= count {
  4651. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4652. info := models.WarehousingInfo{
  4653. GoodId: item.GoodId,
  4654. OrgId: item.OrgId,
  4655. WarehousingId: item.WarehousingId,
  4656. StockCount: item.StockCount + count,
  4657. }
  4658. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4659. fmt.Println("2323233232323232323232323232323", err2)
  4660. return err
  4661. }
  4662. if (item.WarehousingCount - item.StockCount) < count {
  4663. //如果该批次小于退库数量,则先出库该相差数量
  4664. var countOne = item.WarehousingCount - item.StockCount
  4665. infoOne := models.WarehousingInfo{
  4666. GoodId: item.GoodId,
  4667. OrgId: item.OrgId,
  4668. WarehousingId: item.WarehousingId,
  4669. StockCount: countOne + item.StockCount,
  4670. }
  4671. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4672. if err == nil {
  4673. //剩余的出库到第二个批次
  4674. var countTwo = count - countOne
  4675. infos := append(list, list[i+1])
  4676. var idArray []*models.WarehousingInfo
  4677. for _, it := range infos {
  4678. if it.WarehousingId != item.WarehousingId {
  4679. idArray = append(idArray, it)
  4680. }
  4681. }
  4682. //查询更新后的库存
  4683. UpdateCancelOut(idArray, countTwo)
  4684. return err
  4685. } else {
  4686. return err
  4687. }
  4688. }
  4689. }
  4690. return err
  4691. }
  4692. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4693. if len(idstr) > 0 {
  4694. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4695. } else {
  4696. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4697. }
  4698. return info, err
  4699. }
  4700. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4701. if len(idstr) > 0 {
  4702. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4703. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4704. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4705. }).Find(&info).Error
  4706. }
  4707. return info, err
  4708. }
  4709. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4710. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4711. if startime > 0 {
  4712. db = db.Where("x.ctime >=?", startime)
  4713. }
  4714. if endtime > 0 {
  4715. db = db.Where("x.ctime <= ?", endtime)
  4716. }
  4717. if orgid > 0 {
  4718. db = db.Where("x.org_id = ?", orgid)
  4719. }
  4720. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4721. return info, err
  4722. }
  4723. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4724. info := models.WarehouseOutInfo{}
  4725. var err error
  4726. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4727. if err == gorm.ErrRecordNotFound {
  4728. return nil, err
  4729. }
  4730. if err != nil {
  4731. return nil, err
  4732. }
  4733. return &info, nil
  4734. }
  4735. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4736. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4737. if goodid > 0 {
  4738. db = db.Where("x.good_id = ?", goodid)
  4739. }
  4740. if orgid > 0 {
  4741. db = db.Where("x.org_id = ?", orgid)
  4742. }
  4743. if id > 0 {
  4744. db = db.Where("x.warehouse_info_id = ?", id)
  4745. }
  4746. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4747. return info, err
  4748. }
  4749. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4750. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4751. if goodid > 0 {
  4752. db = db.Where("x.good_id = ?", goodid)
  4753. }
  4754. if orgid > 0 {
  4755. db = db.Where("x.user_org_id = ?", orgid)
  4756. }
  4757. if id > 0 {
  4758. db = db.Where("x.warehousing_id = ?", id)
  4759. }
  4760. if storehouse_id > 0 {
  4761. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4762. }
  4763. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4764. return info, err
  4765. }
  4766. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4767. stock := models.CancelStock{}
  4768. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4769. return stock, err
  4770. }
  4771. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4772. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4773. return info, err
  4774. }
  4775. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4776. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4777. return info, err
  4778. }
  4779. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4780. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4781. return stock, err
  4782. }
  4783. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4784. 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)")
  4785. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4786. fmt.Println(table)
  4787. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4788. fmt.Println(tableone)
  4789. if id > 0 {
  4790. db = db.Where("x.warehouse_out_id = ?", id)
  4791. }
  4792. if goodid > 0 {
  4793. db = db.Where("x.good_id = ?", goodid)
  4794. }
  4795. 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
  4796. return stock, err
  4797. }
  4798. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4799. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4800. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4801. fmt.Println(table)
  4802. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4803. fmt.Println(tableone)
  4804. if id > 0 {
  4805. db = db.Where("x.warehouse_out_id = ?", id)
  4806. }
  4807. if goodid > 0 {
  4808. db = db.Where("x.good_id = ?", goodid)
  4809. }
  4810. 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
  4811. return stock, err
  4812. }
  4813. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4814. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4815. return info, err
  4816. }
  4817. func ExportDrugList(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseInfo, err error) {
  4818. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1 AND x.org_id=?", org_id)
  4819. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4820. tableOne := XTReadDB().Table("xt_drug_warehouse as p").Where("p.status=1 and p.org_id=?", org_id)
  4821. fmt.Println("table", table)
  4822. fmt.Println("tableOne", tableOne)
  4823. if len(warehouse_out_id) > 0 {
  4824. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4825. }
  4826. 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
  4827. return out, err
  4828. }
  4829. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4830. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4831. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4832. fmt.Println("table", table)
  4833. if len(warehouse_out_id) > 0 {
  4834. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4835. }
  4836. 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
  4837. return out, err
  4838. }
  4839. func GetExportOutOrderDrugListOne(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseOutInfo, err error) {
  4840. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1 and x.user_org_id =?", org_id)
  4841. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4842. fmt.Println("table", table)
  4843. if len(warehouse_out_id) > 0 {
  4844. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4845. }
  4846. 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
  4847. return out, err
  4848. }
  4849. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4850. db := XTReadDB().Model(&info).Where("status = 1")
  4851. if len(warehouse_out_id) > 0 {
  4852. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4853. }
  4854. if orgid > 0 {
  4855. db = db.Where("org_id = ?", orgid)
  4856. }
  4857. err = db.Find(&info).Error
  4858. return info, err
  4859. }
  4860. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4861. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4862. return out, err
  4863. }
  4864. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4865. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4866. 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)
  4867. }).Find(&out).Error
  4868. return out, err
  4869. }
  4870. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4871. info := models.WarehouseOutInfo{}
  4872. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4873. return info, err
  4874. }
  4875. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4876. info := models.WarehousingInfo{}
  4877. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4878. if goodid > 0 {
  4879. db = db.Where("x.good_id = ?", goodid)
  4880. }
  4881. if storehouse_id > 0 {
  4882. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4883. }
  4884. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4885. return info, err
  4886. }
  4887. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4888. info := models.WarehousingInfo{}
  4889. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4890. return info, err
  4891. }
  4892. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4893. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4894. return err
  4895. }
  4896. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4897. if storehouse_id == 0 {
  4898. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4899. }
  4900. if storehouse_id > 0 {
  4901. 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
  4902. }
  4903. return info, err
  4904. }
  4905. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4906. if len(warehousingInfo) > 0 {
  4907. utx := writeDb.Begin()
  4908. if len(warehousingInfo) > 0 {
  4909. 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 "
  4910. insertParams := make([]string, 0)
  4911. insertData := make([]interface{}, 0)
  4912. for _, info := range warehousingInfo {
  4913. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4914. insertData = append(insertData, info.WarehousingId)
  4915. insertData = append(insertData, info.GoodId)
  4916. insertData = append(insertData, info.Number)
  4917. insertData = append(insertData, info.LicenseNumber)
  4918. insertData = append(insertData, info.Count)
  4919. insertData = append(insertData, info.UserOrgId)
  4920. insertData = append(insertData, info.PatientId)
  4921. insertData = append(insertData, info.SystemTime)
  4922. insertData = append(insertData, info.ConsumableType)
  4923. insertData = append(insertData, info.IsSys)
  4924. insertData = append(insertData, info.WarehousingOrder)
  4925. insertData = append(insertData, info.WarehouseOutId)
  4926. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4927. insertData = append(insertData, info.IsEdit)
  4928. insertData = append(insertData, info.CancelStockId)
  4929. insertData = append(insertData, info.CancelOrderNumber)
  4930. insertData = append(insertData, info.Manufacturer)
  4931. insertData = append(insertData, info.Dealer)
  4932. insertData = append(insertData, info.Creator)
  4933. insertData = append(insertData, info.UpdateCreator)
  4934. insertData = append(insertData, info.Status)
  4935. insertData = append(insertData, info.Ctime)
  4936. insertData = append(insertData, info.Mtime)
  4937. insertData = append(insertData, info.Price)
  4938. insertData = append(insertData, info.WarehousingDetailId)
  4939. insertData = append(insertData, info.WarehouseOutDetailId)
  4940. insertData = append(insertData, info.CancelOutDetailId)
  4941. insertData = append(insertData, info.ExpireDate)
  4942. insertData = append(insertData, info.ProductDate)
  4943. }
  4944. thisSQL += strings.Join(insertParams, ", ")
  4945. err = utx.Exec(thisSQL, insertData...).Error
  4946. if err != nil {
  4947. utx.Rollback()
  4948. return
  4949. }
  4950. }
  4951. utx.Commit()
  4952. }
  4953. return
  4954. }
  4955. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4956. utx := XTWriteDB().Begin()
  4957. err := utx.Create(&flow).Error
  4958. if err != nil {
  4959. utx.Rollback()
  4960. return err
  4961. }
  4962. utx.Commit()
  4963. return err
  4964. }
  4965. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4966. err := tx.Create(&flow).Error
  4967. if err != nil {
  4968. tx.Rollback()
  4969. return err
  4970. }
  4971. return err
  4972. }
  4973. func CreateOutInfoLog(log models.XtGoodWarehouseOutInfoLog, tx *gorm.DB) error {
  4974. err := tx.Create(&log).Error
  4975. if err != nil {
  4976. tx.Rollback()
  4977. return err
  4978. }
  4979. return err
  4980. }
  4981. func CreateGoodWarehouseInfoLog(log models.XtGoodWarehouseInfoLog, tx *gorm.DB) error {
  4982. err := tx.Create(&log).Error
  4983. if err != nil {
  4984. tx.Rollback()
  4985. return err
  4986. }
  4987. return err
  4988. }
  4989. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4990. err := XTWriteDB().Create(&flow).Error
  4991. return err
  4992. }
  4993. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4994. err := XTWriteDB().Save(&flow).Error
  4995. return err
  4996. }
  4997. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4998. 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
  4999. return err
  5000. }
  5001. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  5002. 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
  5003. return err
  5004. }
  5005. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  5006. stock := models.VmStockFlow{}
  5007. var err error
  5008. 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
  5009. if err == gorm.ErrRecordNotFound {
  5010. return nil, err
  5011. }
  5012. if err != nil {
  5013. return nil, err
  5014. }
  5015. return &stock, nil
  5016. }
  5017. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  5018. 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
  5019. return flow, nil
  5020. }
  5021. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5022. 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
  5023. return flow, nil
  5024. }
  5025. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5026. 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
  5027. if err != gorm.ErrRecordNotFound {
  5028. if err != nil {
  5029. tx.Rollback()
  5030. return flow, nil
  5031. }
  5032. }
  5033. return flow, nil
  5034. }
  5035. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  5036. 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
  5037. return flow, nil
  5038. }
  5039. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  5040. 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
  5041. if err != gorm.ErrRecordNotFound {
  5042. if err != nil {
  5043. tx.Rollback()
  5044. return flow, nil
  5045. }
  5046. }
  5047. return flow, nil
  5048. }
  5049. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  5050. 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
  5051. return flow, nil
  5052. }
  5053. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  5054. 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
  5055. if err != gorm.ErrRecordNotFound {
  5056. if err != nil {
  5057. tx.Rollback()
  5058. return flow, err
  5059. }
  5060. }
  5061. return flow, err
  5062. }
  5063. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  5064. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  5065. return flow, nil
  5066. }
  5067. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  5068. 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
  5069. return flow, nil
  5070. }
  5071. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5072. 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
  5073. return flow, nil
  5074. }
  5075. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5076. 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
  5077. if err != gorm.ErrRecordNotFound {
  5078. if err != nil {
  5079. tx.Rollback()
  5080. return flow, nil
  5081. }
  5082. }
  5083. return flow, nil
  5084. }
  5085. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5086. 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
  5087. return flow, nil
  5088. }
  5089. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  5090. info := models.WarehouseOutInfo{}
  5091. var err error
  5092. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5093. if err == gorm.ErrRecordNotFound {
  5094. return nil, err
  5095. }
  5096. if err != nil {
  5097. return nil, err
  5098. }
  5099. return &info, nil
  5100. }
  5101. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  5102. info := models.WarehouseOutInfo{}
  5103. var err error
  5104. 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
  5105. if err == gorm.ErrRecordNotFound {
  5106. return nil, err
  5107. }
  5108. if err != nil {
  5109. return nil, err
  5110. }
  5111. return &info, nil
  5112. }
  5113. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  5114. info := models.WarehouseOutInfo{}
  5115. 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
  5116. return info, err
  5117. }
  5118. 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) {
  5119. offset := (page - 1) * limit
  5120. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  5121. if consumable_type > 0 {
  5122. //入库
  5123. if consumable_type == 1 {
  5124. db = db.Where("consumable_type = ?", consumable_type)
  5125. }
  5126. //2 手动出库
  5127. if consumable_type == 2 {
  5128. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  5129. }
  5130. //3.自动出库
  5131. if consumable_type == 3 {
  5132. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  5133. }
  5134. //4.手动退库
  5135. if consumable_type == 4 {
  5136. db = db.Where(" consumable_type = ?", consumable_type)
  5137. }
  5138. //自动退库
  5139. if consumable_type == 7 {
  5140. db = db.Where(" consumable_type = ?", consumable_type)
  5141. }
  5142. if consumable_type == 10 {
  5143. db = db.Where(" consumable_type = ?", consumable_type)
  5144. }
  5145. if consumable_type == 11 {
  5146. db = db.Where(" consumable_type = ?", consumable_type)
  5147. }
  5148. if consumable_type == 12 {
  5149. db = db.Where(" consumable_type = ?", consumable_type)
  5150. }
  5151. if consumable_type == 13 {
  5152. db = db.Where(" consumable_type = ?", consumable_type)
  5153. }
  5154. if consumable_type == 15 {
  5155. db = db.Where(" consumable_type = ?", consumable_type)
  5156. }
  5157. }
  5158. if orgId > 0 {
  5159. db = db.Where("user_org_id = ?", orgId)
  5160. }
  5161. if startTime > 0 {
  5162. db = db.Where("ctime >= ?", startTime)
  5163. }
  5164. if endTime > 0 {
  5165. db = db.Where("ctime<=?", endTime)
  5166. }
  5167. if good_id > 0 {
  5168. db = db.Where("good_id = ?", good_id)
  5169. }
  5170. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  5171. return flow, total, err
  5172. }
  5173. func GetHidsDoctorAdvice(good_id int64, user_org_id int64, patient_id int64, record_date int64) (models.HisPrescriptionProject, error) {
  5174. project := models.HisPrescriptionProject{}
  5175. err = XTReadDB().Where("project_id = ? and type =3 and user_org_id = ? and record_date =? and patient_id = ?", good_id, user_org_id, record_date, patient_id).Find(&project).Error
  5176. return project, err
  5177. }
  5178. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  5179. detail := models.AutomaticReduceDetail{}
  5180. 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
  5181. return detail, err
  5182. }
  5183. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  5184. detail := models.WarehouseOutInfo{}
  5185. 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
  5186. return detail, err
  5187. }
  5188. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  5189. outInfo := models.WarehouseOutInfo{}
  5190. 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
  5191. return err
  5192. }
  5193. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  5194. outInfo := models.WarehouseOutInfo{}
  5195. 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
  5196. if err != nil {
  5197. tx.Rollback()
  5198. return err
  5199. }
  5200. return err
  5201. }
  5202. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  5203. outInfo := models.WarehouseOutInfo{}
  5204. 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
  5205. return err
  5206. }
  5207. 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 {
  5208. outInfo := models.WarehouseOutInfo{}
  5209. 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
  5210. if err != nil {
  5211. tx.Rollback()
  5212. return err
  5213. }
  5214. return err
  5215. }
  5216. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  5217. info := models.WarehouseOutInfo{}
  5218. 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
  5219. return info, err
  5220. }
  5221. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  5222. info := models.WarehousingInfo{}
  5223. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5224. return info, err
  5225. }
  5226. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  5227. info := models.WarehousingInfo{}
  5228. db := XTReadDB().Model(&info)
  5229. if good_id > 0 {
  5230. db = db.Where("good_id = ? and status = 1", good_id)
  5231. }
  5232. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  5233. return info, err
  5234. }
  5235. 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) {
  5236. offset := (page - 1) * limit
  5237. db := XTReadDB().Model(drug_flow).Where("status = 1")
  5238. if drugid > 0 {
  5239. db = db.Where("drug_id = ?", drugid)
  5240. }
  5241. if orgid > 0 {
  5242. db = db.Where("user_org_id = ?", orgid)
  5243. }
  5244. if startTime > 0 {
  5245. db = db.Where("ctime >=? ", startTime)
  5246. }
  5247. if endTime > 0 {
  5248. db = db.Where("ctime<=?", endTime)
  5249. }
  5250. if stock_type > 0 {
  5251. //手动入库
  5252. if stock_type == 1 {
  5253. db = db.Where("consumable_type = 1")
  5254. }
  5255. //手动出库
  5256. if stock_type == 2 {
  5257. db = db.Where("consumable_type = 2 and is_sys = 0")
  5258. }
  5259. //自动出库
  5260. if stock_type == 3 {
  5261. db = db.Where("consumable_type = 3 and is_sys = 1")
  5262. }
  5263. if stock_type == 4 {
  5264. db = db.Where("consumable_type = 4")
  5265. }
  5266. if stock_type == 10 {
  5267. db = db.Where("consumable_type = 10")
  5268. }
  5269. if stock_type == 11 {
  5270. db = db.Where("consumable_type = 11")
  5271. }
  5272. if stock_type == 12 {
  5273. db = db.Where("consumable_type = 12")
  5274. }
  5275. if stock_type == 13 {
  5276. db = db.Where("consumable_type = 13")
  5277. }
  5278. if stock_type == 15 {
  5279. db = db.Where("consumable_type = 15")
  5280. }
  5281. }
  5282. 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
  5283. return drug_flow, total, err
  5284. }
  5285. func HisDoctorAdviceMap(patient_id int64, advice_date int64, drug_id int64, user_org_id int64) (models.HisDoctorAdviceInfo, error) {
  5286. advicelist := models.HisDoctorAdviceInfo{}
  5287. err = XTReadDB().Where("patient_id = ? and drug_id = ? and user_org_id = ? and advice_date = ? and status=1", patient_id, drug_id, user_org_id, advice_date).Find(&advicelist).Error
  5288. return advicelist, err
  5289. }
  5290. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  5291. outInfo := models.DrugWarehouseOutInfo{}
  5292. var err error
  5293. 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
  5294. if err == gorm.ErrRecordNotFound {
  5295. return nil, err
  5296. }
  5297. if err != nil {
  5298. return nil, err
  5299. }
  5300. return &outInfo, nil
  5301. }
  5302. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  5303. 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
  5304. return outInfo, err
  5305. }
  5306. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  5307. outInfo := models.DrugWarehouseOutInfo{}
  5308. 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
  5309. return err
  5310. }
  5311. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  5312. db := XTReadDB().Model(&cancle).Where("status = 1")
  5313. if startime > 0 {
  5314. db = db.Where("ctime >=?", startime)
  5315. }
  5316. if endtime > 0 {
  5317. db = db.Where("ctime <=?", endtime)
  5318. }
  5319. if len(ids) > 0 {
  5320. db = db.Where("cancel_stock_id in(?)", ids)
  5321. }
  5322. if orgid > 0 {
  5323. db = db.Where("org_id = ?", orgid)
  5324. }
  5325. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  5326. err = db.Find(&cancle).Error
  5327. return cancle, err
  5328. }
  5329. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  5330. db := XTReadDB().Model(&info)
  5331. if id > 0 {
  5332. db = db.Where("cancel_stock_id = ? and status = 1", id)
  5333. }
  5334. db = db.Preload("BaseDrugLib")
  5335. err = db.Find(&info).Error
  5336. return info, err
  5337. }
  5338. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  5339. info := models.XtDrugWarehouseInfo{}
  5340. db := XTReadDB().Model(&info).Where("status = 1")
  5341. if id > 0 {
  5342. db = db.Where("id = ?", id)
  5343. }
  5344. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5345. return info, err
  5346. }
  5347. func GetDrugWarehouseInfoOne(id int64) (models.XtDrugWarehouseInfo, error) {
  5348. info := models.XtDrugWarehouseInfo{}
  5349. err := XTReadDB().Where("status = 1 and id=?", id).Find(&info).Error
  5350. return info, err
  5351. }
  5352. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  5353. // info := models.XtDrugWarehouseInfo{}
  5354. // db := tx.Model(&info).Where("status = 1")
  5355. // if id > 0 {
  5356. // db = db.Where("id = ?", id)
  5357. // }
  5358. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5359. // return info, err
  5360. //}
  5361. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5362. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  5363. return info, err
  5364. }
  5365. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  5366. 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
  5367. return info, err
  5368. }
  5369. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  5370. info := models.HisDoctorAdviceInfo{}
  5371. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  5372. return info, err
  5373. }
  5374. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  5375. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5376. return info, err
  5377. }
  5378. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  5379. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5380. if err != gorm.ErrRecordNotFound {
  5381. if err != nil {
  5382. tx.Rollback()
  5383. return info, err
  5384. }
  5385. }
  5386. return info, err
  5387. }
  5388. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  5389. adviceInfo := models.HisDoctorAdviceInfo{}
  5390. 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
  5391. if err != gorm.ErrRecordNotFound {
  5392. if err != nil {
  5393. tx.Rollback()
  5394. return adviceInfo, err
  5395. }
  5396. }
  5397. return adviceInfo, err
  5398. }
  5399. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5400. info := models.HisDoctorAdviceInfo{}
  5401. 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
  5402. return info, err
  5403. }
  5404. func GetHisAdviceListByDrugIdNight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5405. info := models.HisDoctorAdviceInfo{}
  5406. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1 and id =?", drugid, patient_id, advice_date, advice_id).Find(&info).Error
  5407. return info, err
  5408. }
  5409. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  5410. detail := models.DrugAutomaticReduceDetail{}
  5411. 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
  5412. return detail, err
  5413. }
  5414. func GetWarehouseOutListExport(drug_id int64, patient_id int64, record_date int64, advice_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  5415. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and advice_id =? and status =1", drug_id, patient_id, record_date, advice_id).Find(&out).Error
  5416. return out, err
  5417. }
  5418. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  5419. 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
  5420. return out, err
  5421. }
  5422. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  5423. 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
  5424. if err != gorm.ErrRecordNotFound {
  5425. if err != nil {
  5426. tx.Rollback()
  5427. return out, err
  5428. }
  5429. }
  5430. return out, err
  5431. }
  5432. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  5433. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5434. return err
  5435. }
  5436. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  5437. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5438. return err
  5439. }
  5440. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  5441. 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
  5442. 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
  5443. 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
  5444. return err
  5445. }
  5446. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5447. 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
  5448. 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
  5449. return err
  5450. }
  5451. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5452. 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
  5453. 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
  5454. return err
  5455. }
  5456. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  5457. info := models.XtDrugWarehouseInfo{}
  5458. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  5459. return info, err
  5460. }
  5461. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  5462. info := models.XtDrugWarehouseInfo{}
  5463. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5464. return info, err
  5465. }
  5466. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  5467. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5468. return err
  5469. }
  5470. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5471. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5472. // return err
  5473. //}
  5474. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  5475. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  5476. //return err
  5477. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5478. return err
  5479. }
  5480. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5481. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5482. // return err
  5483. //}
  5484. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  5485. info := models.DrugCancelStockInfo{}
  5486. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5487. return info, err
  5488. }
  5489. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  5490. info := models.DrugCancelStockInfo{}
  5491. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  5492. return info, err
  5493. }
  5494. func UpdatedDrugInfo(id int64, count int64) (err error) {
  5495. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5496. return err
  5497. }
  5498. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  5499. 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
  5500. return err
  5501. }
  5502. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  5503. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5504. return err
  5505. }
  5506. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5507. 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
  5508. return err
  5509. }
  5510. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  5511. 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
  5512. return err
  5513. }
  5514. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5515. 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
  5516. return err
  5517. }
  5518. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  5519. 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
  5520. return err
  5521. }
  5522. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  5523. info := models.WarehousingInfo{}
  5524. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5525. return info, err
  5526. }
  5527. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5528. info := models.WarehousingInfo{}
  5529. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  5530. return info, err
  5531. }
  5532. func GetLastWarehouseInfoTwo(goodid int64, storehouse_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  5533. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=? and org_id = ? and stock_count > 0", goodid, storehouse_id, user_org_id).Order("ctime").First(&info).Error
  5534. return info, err
  5535. }
  5536. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5537. info := models.WarehousingInfo{}
  5538. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  5539. return info, err
  5540. }
  5541. func CreateWarehouseOut(out models.WarehouseOut) error {
  5542. err := XTWriteDB().Create(&out).Error
  5543. return err
  5544. }
  5545. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  5546. out := models.WarehouseOut{}
  5547. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  5548. return out, err
  5549. }
  5550. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  5551. err := XTWriteDB().Create(&info).Error
  5552. return err
  5553. }
  5554. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  5555. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5556. return err
  5557. }
  5558. func UpdateWareInfoById(id int64, count int64) error {
  5559. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5560. return err
  5561. }
  5562. func UpdateWareInfoByIdSix(id int64, count int64) error {
  5563. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  5564. return err
  5565. }
  5566. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  5567. info := models.CancelStockInfo{}
  5568. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5569. return info, err
  5570. }
  5571. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  5572. info := models.CancelStockInfo{}
  5573. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5574. return info, err
  5575. }
  5576. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5577. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5578. return info, err
  5579. }
  5580. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5581. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5582. return info, err
  5583. }
  5584. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5585. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5586. return err
  5587. }
  5588. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5589. drugFlow := models.DrugFlow{}
  5590. 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
  5591. return err
  5592. }
  5593. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5594. drugFlow := models.DrugFlow{}
  5595. 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
  5596. return err
  5597. }
  5598. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5599. stock := models.DrugCancelStock{}
  5600. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5601. return stock, err
  5602. }
  5603. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5604. // stock := models.DrugCancelStock{}
  5605. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5606. // return stock, err
  5607. //}
  5608. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5609. db := XTReadDB().Model(&info)
  5610. if warehouse_info_id > 0 {
  5611. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5612. }
  5613. if drug_id > 0 {
  5614. db = db.Where("drug_id = ? and status =1", drug_id)
  5615. }
  5616. err = db.Find(&info).Error
  5617. return info, err
  5618. }
  5619. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5620. // db := tx.Model(&info)
  5621. // if warehouse_info_id > 0 {
  5622. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5623. // }
  5624. // if drug_id > 0 {
  5625. // db = db.Where("drug_id = ? and status =1", drug_id)
  5626. // }
  5627. // err = db.Find(&info).Error
  5628. // return info, err
  5629. //}
  5630. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5631. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5632. return info, err
  5633. }
  5634. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5635. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5636. // return info, err
  5637. //}
  5638. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5639. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5640. return config, err
  5641. }
  5642. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5643. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5644. return config, err
  5645. }
  5646. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5647. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5648. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5649. fmt.Println(table)
  5650. likeKey := "%" + keyword + "%"
  5651. if orgid > 0 {
  5652. db = db.Where("x.org_id = ?", orgid)
  5653. }
  5654. if storehouse_id > 0 {
  5655. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5656. }
  5657. 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
  5658. return info, err
  5659. }
  5660. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5661. err := XTWriteDB().Create(&adjustPrice).Error
  5662. return err
  5663. }
  5664. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5665. likeKey := "%" + keyword + "%"
  5666. offset := (page - 1) * limit
  5667. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5668. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5669. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5670. fmt.Println(table, tab)
  5671. if startime > 0 {
  5672. db = db.Where("x.start_time>=?", startime)
  5673. }
  5674. if endtime > 0 {
  5675. db = db.Where("x.start_time<=?", endtime)
  5676. }
  5677. if len(keyword) > 0 {
  5678. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5679. }
  5680. if orgid > 0 {
  5681. db = db.Where("x.user_org_id = ?", orgid)
  5682. }
  5683. 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
  5684. return list, total, err
  5685. }
  5686. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5687. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5688. return err
  5689. }
  5690. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5691. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5692. return err
  5693. }
  5694. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5695. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5696. return info, err
  5697. }
  5698. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5699. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5700. return adjust, err
  5701. }
  5702. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5703. err := XTWriteDB().Create(&price).Error
  5704. return err
  5705. }
  5706. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5707. likeKey := "%" + keyword + "%"
  5708. offset := (page - 1) * limit
  5709. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5710. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5711. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5712. fmt.Println(table, tab)
  5713. if startime > 0 {
  5714. db = db.Where("x.ctime>=?", startime)
  5715. }
  5716. if endtime > 0 {
  5717. db = db.Where("x.ctime<=?", endtime)
  5718. }
  5719. if len(keyword) > 0 {
  5720. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5721. }
  5722. if orgid > 0 {
  5723. db = db.Where("x.user_org_id = ?", orgid)
  5724. }
  5725. 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
  5726. return adjust, total, err
  5727. }
  5728. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5729. likeKey := "%" + keyword + "%"
  5730. offset := (page - 1) * limit
  5731. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5732. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5733. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5734. fmt.Println(table, tab)
  5735. if len(keyword) > 0 {
  5736. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5737. }
  5738. if orgid > 0 {
  5739. db = db.Where("x.user_org_id = ?", orgid)
  5740. }
  5741. if startime > 0 {
  5742. db = db.Where("x.ctime >=?", startime)
  5743. }
  5744. if endtime > 0 {
  5745. db = db.Where("x.ctime <=?", endtime)
  5746. }
  5747. if storehouse_id > 0 {
  5748. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5749. }
  5750. err = db.Group("x.good_id").Select("x.ctime,x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,x.storehouse_id,t.packing_unit,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  5751. return adjust, total, err
  5752. }
  5753. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5754. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5755. if inventory_type == 0 {
  5756. db = db.Where("x.inventory_type = 4")
  5757. }
  5758. if inventory_type > 0 {
  5759. db = db.Where("x.inventory_type = ?", inventory_type)
  5760. }
  5761. if len(keyword) > 0 {
  5762. likeKey := "%" + keyword + "%"
  5763. db = db.Where("x.warehousing_order like ?", likeKey)
  5764. }
  5765. if orgid > 0 {
  5766. db = db.Where("x.user_org_id = ?", orgid)
  5767. }
  5768. 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
  5769. return adjust, err
  5770. }
  5771. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5772. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5773. if orgid > 0 {
  5774. db = db.Where("x.user_org_id = ?", orgid)
  5775. }
  5776. 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
  5777. return adjust, err
  5778. }
  5779. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5780. 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
  5781. return err
  5782. }
  5783. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5784. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5785. return report, err
  5786. }
  5787. func CreateInentory(inventory models.XtStockInventory) error {
  5788. err = XTWriteDB().Create(&inventory).Error
  5789. return err
  5790. }
  5791. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5792. likeKey := "%" + keyword + "%"
  5793. offset := (page - 1) * limit
  5794. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5795. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5796. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5797. fmt.Println(table, tab)
  5798. if startime > 0 {
  5799. db = db.Where("x.ctime>=?", startime)
  5800. }
  5801. if endtime > 0 {
  5802. db = db.Where("x.ctime<=?", endtime)
  5803. }
  5804. if len(keyword) > 0 {
  5805. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5806. }
  5807. if orgid > 0 {
  5808. db = db.Where("x.user_org_id = ?", orgid)
  5809. }
  5810. if inventory_status > 0 {
  5811. db = db.Where("checker_status = ?", inventory_status)
  5812. }
  5813. 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
  5814. return adjust, total, err
  5815. }
  5816. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5817. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5818. return err
  5819. }
  5820. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5821. info := models.XtStockAdjustPrice{}
  5822. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5823. return info, err
  5824. }
  5825. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5826. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5827. return list, err
  5828. }
  5829. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5830. info := models.WarehousingInfo{}
  5831. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5832. return info, err
  5833. }
  5834. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5835. info := models.WarehousingInfo{}
  5836. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5837. //return info, err
  5838. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5839. if id > 0 {
  5840. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5841. }
  5842. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5843. return info, err
  5844. }
  5845. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5846. info := models.WarehousingInfo{}
  5847. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5848. if id > 0 {
  5849. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5850. }
  5851. if storehouse_id > 0 {
  5852. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5853. }
  5854. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5855. return info, err
  5856. }
  5857. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5858. 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
  5859. return err
  5860. }
  5861. func DeleteStockPrice(id int64) error {
  5862. info := models.XtStockAdjustPrice{}
  5863. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5864. return err
  5865. }
  5866. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5867. report := models.XtStockReportPrice{}
  5868. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5869. return report, err
  5870. }
  5871. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5872. 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
  5873. return err
  5874. }
  5875. func DeleteDamage(id int64) error {
  5876. adjust := models.XtStockReportPrice{}
  5877. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5878. return err
  5879. }
  5880. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5881. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5882. return adjust, err
  5883. }
  5884. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5885. inventory := models.XtStockInventory{}
  5886. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5887. return inventory, err
  5888. }
  5889. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5890. 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
  5891. return err
  5892. }
  5893. func DeleteInventory(id int64) error {
  5894. inventory := models.XtStockInventory{}
  5895. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5896. return err
  5897. }
  5898. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5899. likeKey := "%" + keyword + "%"
  5900. offset := (page - 1) * limit
  5901. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5902. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5903. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5904. fmt.Println(table, tab)
  5905. if len(keyword) > 0 {
  5906. db = db.Where("x.good_name like ?", likeKey)
  5907. }
  5908. if orgid > 0 {
  5909. db = db.Where("x.user_org_id = ?", orgid)
  5910. }
  5911. if storehouse_id > 0 {
  5912. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5913. }
  5914. 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
  5915. return list, total, err
  5916. }
  5917. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5918. likeKey := "%" + keyword + "%"
  5919. offset := (page - 1) * limit
  5920. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5921. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5922. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5923. fmt.Println(table, tab)
  5924. if len(keyword) > 0 {
  5925. db = db.Where("x.good_name like ?", likeKey)
  5926. }
  5927. if orgid > 0 {
  5928. db = db.Where("x.user_org_id = ?", orgid)
  5929. }
  5930. if storehouse_id > 0 {
  5931. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5932. }
  5933. if len(ids) > 0 {
  5934. db = db.Where("x.id in(?)", ids)
  5935. }
  5936. 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
  5937. return list, total, err
  5938. }
  5939. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5940. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5941. return list, err
  5942. }
  5943. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5944. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5945. return list, err
  5946. }
  5947. func DeleteDrugPrice(id int64) error {
  5948. info := models.XtDrugAdjustPrice{}
  5949. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5950. return err
  5951. }
  5952. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5953. info := models.WarehousingInfo{}
  5954. db := XTReadDB().Model(&info).Where("status = 1")
  5955. if goodid > 0 {
  5956. db = db.Where("good_id = ?", goodid)
  5957. }
  5958. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5959. return info, err
  5960. }
  5961. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5962. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5963. return err
  5964. }
  5965. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5966. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5967. return err
  5968. }
  5969. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5970. err := XTWriteDB().Create(&record).Error
  5971. return err
  5972. }
  5973. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5974. info := models.WarehousingInfo{}
  5975. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5976. //return info, err
  5977. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5978. if id > 0 {
  5979. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5980. }
  5981. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5982. return info, err
  5983. }
  5984. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5985. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5986. return info, err
  5987. }
  5988. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  5989. info := models.DrugWarehouseInfo{}
  5990. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5991. return info, err
  5992. }
  5993. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  5994. 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
  5995. return err
  5996. }
  5997. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  5998. 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
  5999. return err
  6000. }
  6001. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  6002. 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
  6003. return info, err
  6004. }
  6005. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  6006. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  6007. return err
  6008. }
  6009. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  6010. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  6011. return err
  6012. }
  6013. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  6014. db := XTReadDB().Model(&list).Where(" status = 1")
  6015. if good_id > 0 {
  6016. db = db.Where("good_id = ?", good_id)
  6017. }
  6018. if len(keyword) > 0 {
  6019. likeKey := "%" + keyword + "%"
  6020. db = db.Where("warehousing_order like ?", likeKey)
  6021. }
  6022. if inventory_type > 0 {
  6023. db = db.Where("inventory_type = ?", inventory_type)
  6024. }
  6025. if inventory_type == 0 {
  6026. db = db.Where("inventory_type = 4")
  6027. }
  6028. err = db.Find(&list).Error
  6029. return list, err
  6030. }
  6031. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  6032. flow := models.DrugFlow{}
  6033. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  6034. if err == gorm.ErrRecordNotFound {
  6035. return nil, err
  6036. }
  6037. if err != nil {
  6038. return nil, err
  6039. }
  6040. return &flow, nil
  6041. }
  6042. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  6043. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  6044. return info, err
  6045. }
  6046. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  6047. info := models.XtDrugWarehouseInfo{}
  6048. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  6049. return info, err
  6050. }
  6051. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  6052. info := models.WarehousingInfo{}
  6053. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  6054. return info, err
  6055. }
  6056. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  6057. flow := models.VmStockFlow{}
  6058. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  6059. if err == gorm.ErrRecordNotFound {
  6060. return nil, err
  6061. }
  6062. if err != nil {
  6063. return nil, err
  6064. }
  6065. return &flow, nil
  6066. }
  6067. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  6068. info := models.GoodInfo{}
  6069. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  6070. return info, err
  6071. }
  6072. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  6073. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  6074. return err
  6075. }
  6076. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  6077. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  6078. return err
  6079. }
  6080. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  6081. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount, "price": info.Price, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "license_number": info.LicenseNumber, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "remark": info.Remark, "packing_price": info.PackingPrice, "storehouse_id": info.StorehouseId, "register_number": info.RegisterNumber, "good_id": info.GoodId, "good_code": info.GoodCode}).Error
  6082. return err
  6083. }
  6084. func UpdateNewWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  6085. err := tx.Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount, "price": info.Price, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "license_number": info.LicenseNumber, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "remark": info.Remark, "packing_price": info.PackingPrice, "storehouse_id": info.StorehouseId, "register_number": info.RegisterNumber, "good_id": info.GoodId, "good_code": info.GoodCode}).Error
  6086. if err != nil {
  6087. tx.Rollback()
  6088. return err
  6089. }
  6090. return err
  6091. }
  6092. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6093. 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
  6094. return err
  6095. }
  6096. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6097. 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
  6098. return err
  6099. }
  6100. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  6101. 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
  6102. return err
  6103. }
  6104. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  6105. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  6106. return err
  6107. }
  6108. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  6109. if orgid == 10653 {
  6110. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0 and x.stock_count>0")
  6111. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6112. fmt.Println(table)
  6113. if orgid > 0 {
  6114. db = db.Where("x.org_id = ?", orgid)
  6115. }
  6116. 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,x.stock_count,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
  6117. return info, err
  6118. } else {
  6119. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0")
  6120. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6121. fmt.Println(table)
  6122. if orgid > 0 {
  6123. db = db.Where("x.org_id = ?", orgid)
  6124. }
  6125. 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,x.stock_count,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
  6126. return info, err
  6127. }
  6128. }
  6129. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  6130. warehouseInfo := models.XtDrugWarehouseInfo{}
  6131. 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
  6132. return err
  6133. }
  6134. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  6135. err := XTWriteDB().Create(&info).Error
  6136. return err
  6137. }
  6138. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  6139. err := tx.Create(&info).Error
  6140. if err != nil {
  6141. tx.Rollback()
  6142. return err
  6143. }
  6144. return err
  6145. }
  6146. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  6147. flow := models.VmStockFlow{}
  6148. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  6149. if err == gorm.ErrRecordNotFound {
  6150. return nil, err
  6151. }
  6152. if err != nil {
  6153. return nil, err
  6154. }
  6155. return &flow, nil
  6156. }
  6157. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  6158. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  6159. return err
  6160. }
  6161. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  6162. 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
  6163. return err
  6164. }
  6165. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  6166. flow := models.DrugWarehouseOutInfo{}
  6167. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6168. return flow, err
  6169. }
  6170. func GetLastNewWarehouseOutInfoById(drugid int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
  6171. flow := models.DrugWarehouseOutInfo{}
  6172. err := tx.Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6173. if err != gorm.ErrRecordNotFound {
  6174. if err != nil {
  6175. tx.Rollback()
  6176. return flow, err
  6177. }
  6178. }
  6179. return flow, err
  6180. }
  6181. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  6182. info := models.WarehouseOutInfo{}
  6183. 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
  6184. return info, err
  6185. }
  6186. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  6187. 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
  6188. return err
  6189. }
  6190. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  6191. 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
  6192. return err
  6193. }
  6194. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  6195. info := models.DrugWarehouseOutInfo{}
  6196. 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
  6197. if err == gorm.ErrRecordNotFound {
  6198. return nil, err
  6199. }
  6200. if err != nil {
  6201. return nil, err
  6202. }
  6203. return &info, nil
  6204. }
  6205. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  6206. drugFlow := models.DrugFlow{}
  6207. 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
  6208. if err == gorm.ErrRecordNotFound {
  6209. return nil, err
  6210. }
  6211. if err != nil {
  6212. return nil, err
  6213. }
  6214. return &drugFlow, nil
  6215. }
  6216. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  6217. 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
  6218. return err
  6219. }
  6220. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  6221. 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
  6222. return err
  6223. }
  6224. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  6225. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6226. return detail, err
  6227. }
  6228. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  6229. detail := models.DrugAutomaticReduceDetail{}
  6230. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6231. if err == gorm.ErrRecordNotFound {
  6232. return nil, err
  6233. }
  6234. if err != nil {
  6235. return nil, err
  6236. }
  6237. return &detail, nil
  6238. }
  6239. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  6240. 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
  6241. return err
  6242. }
  6243. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  6244. 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
  6245. return err
  6246. }
  6247. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  6248. info := models.DrugWarehouseInfo{}
  6249. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6250. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6251. return info, err
  6252. }
  6253. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  6254. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6255. return err
  6256. }
  6257. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  6258. info := models.DrugWarehouseInfo{}
  6259. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6260. return err
  6261. }
  6262. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  6263. info := models.DrugCancelStockInfo{}
  6264. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6265. return info, err
  6266. }
  6267. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  6268. info := models.DrugWarehouseInfo{}
  6269. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  6270. return err
  6271. }
  6272. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  6273. info := models.DrugWarehouseInfo{}
  6274. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  6275. return err
  6276. }
  6277. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  6278. 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
  6279. return advice, err
  6280. }
  6281. func GetAllHisDoctorAdviceByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (advice []*models.HisDoctorAdviceInfo, err error) {
  6282. err = tx.Where("drug_id = ? and patient_id = ? and advice_date = ? and execution_state = 1 and status = 1 and user_org_id = ?", drugid, patient_id, advicedate, orgid).Find(&advice).Error
  6283. if err != gorm.ErrRecordNotFound {
  6284. if err != nil {
  6285. tx.Rollback()
  6286. return advice, err
  6287. }
  6288. }
  6289. return advice, err
  6290. }
  6291. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6292. 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
  6293. return info, err
  6294. }
  6295. func GetAllDrugFlowByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  6296. err = tx.Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", drugid, patient_id, advicedate, orgid).Find(&info).Error
  6297. if err != gorm.ErrRecordNotFound {
  6298. if err != nil {
  6299. tx.Rollback()
  6300. return info, err
  6301. }
  6302. }
  6303. return info, err
  6304. }
  6305. // 耗材加库存
  6306. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6307. info := models.GoodInfo{}
  6308. 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
  6309. return info, err
  6310. }
  6311. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6312. info := models.GoodInfo{}
  6313. 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
  6314. 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
  6315. return info, err
  6316. }
  6317. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6318. info := models.GoodInfo{}
  6319. 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
  6320. return info, err
  6321. }
  6322. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6323. info := models.GoodInfo{}
  6324. 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
  6325. 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
  6326. return info, err
  6327. }
  6328. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  6329. info := models.GoodInfo{}
  6330. 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
  6331. return info, err
  6332. }
  6333. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6334. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6335. return info, err
  6336. }
  6337. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6338. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6339. return info, err
  6340. }
  6341. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  6342. info := models.WarehousingInfo{}
  6343. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  6344. return info, err
  6345. }
  6346. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  6347. info := models.WarehousingInfo{}
  6348. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  6349. return info, err
  6350. }
  6351. // 药品加库存
  6352. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6353. info := models.BaseDrugLib{}
  6354. 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
  6355. return info, err
  6356. }
  6357. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6358. info := models.BaseDrugLib{}
  6359. 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
  6360. 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
  6361. return info, err
  6362. }
  6363. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6364. info := models.BaseDrugLib{}
  6365. 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
  6366. return info, err
  6367. }
  6368. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6369. info := models.BaseDrugLib{}
  6370. 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
  6371. 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
  6372. return info, err
  6373. }
  6374. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  6375. info := models.BaseDrugLib{}
  6376. 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
  6377. 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
  6378. return info, err
  6379. }
  6380. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6381. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6382. return info, err
  6383. }
  6384. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6385. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6386. return info, err
  6387. }
  6388. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  6389. info := models.WarehouseOutInfo{}
  6390. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6391. return info, err
  6392. }
  6393. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  6394. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  6395. return out, err
  6396. }
  6397. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  6398. info := models.WarehousingInfo{}
  6399. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  6400. return info, err
  6401. }
  6402. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  6403. err = XTReadDB().Where("status = 1").Find(&template).Error
  6404. return template, err
  6405. }
  6406. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  6407. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  6408. return good, err
  6409. }
  6410. func GetMobileGoodList(org_id int64) (list []*models.GoodInformationThirty, err error) {
  6411. err = XTReadDB().Where("org_id = ? and status =1", org_id).Find(&list).Error
  6412. return list, err
  6413. }
  6414. func GetAllMobileProjectList(orgid int64) (project []*models.HisProject, err error) {
  6415. err = XTReadDB().Where("user_org_id =? and status =1", orgid).Find(&project).Error
  6416. return project, err
  6417. }
  6418. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6419. info := models.GoodInfo{}
  6420. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6421. return info, err
  6422. }
  6423. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  6424. db := XTReadDB().Where("status = 1")
  6425. if good_id > 0 {
  6426. db = db.Where("good_id = ?", good_id)
  6427. }
  6428. err = db.Find(&info).Error
  6429. return info, err
  6430. }
  6431. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6432. db := XTReadDB().Model(&info).Where("status = 1")
  6433. if drug_id > 0 {
  6434. db = db.Where("drug_id = ?", drug_id)
  6435. }
  6436. err = db.Find(&info).Error
  6437. return info, err
  6438. }
  6439. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6440. lib := models.BaseDrugLib{}
  6441. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6442. return lib, err
  6443. }
  6444. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  6445. info := models.Warehousing{}
  6446. 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
  6447. if err == gorm.ErrRecordNotFound {
  6448. return nil, err
  6449. }
  6450. if err != nil {
  6451. return nil, err
  6452. }
  6453. return &info, nil
  6454. }
  6455. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  6456. info := models.Warehousing{}
  6457. 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
  6458. return info, err
  6459. }
  6460. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  6461. out := models.DrugWarehouseOut{}
  6462. 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
  6463. if err == gorm.ErrRecordNotFound {
  6464. return nil, err
  6465. }
  6466. if err != nil {
  6467. return nil, err
  6468. }
  6469. return &out, nil
  6470. }
  6471. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  6472. info := models.DrugWarehouseOut{}
  6473. 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
  6474. return info, err
  6475. }
  6476. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  6477. info := models.DrugWarehouse{}
  6478. 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
  6479. if err == gorm.ErrRecordNotFound {
  6480. return nil, err
  6481. }
  6482. if err != nil {
  6483. return nil, err
  6484. }
  6485. return &info, nil
  6486. }
  6487. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  6488. err := XTWriteDB().Create(&warehouse).Error
  6489. return err
  6490. }
  6491. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  6492. info := models.DrugWarehouse{}
  6493. 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
  6494. return info, err
  6495. }
  6496. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6497. info := models.GoodInfo{}
  6498. 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
  6499. 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
  6500. return info, err
  6501. }
  6502. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6503. info := models.GoodInfo{}
  6504. 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
  6505. 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
  6506. return info, err
  6507. }
  6508. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6509. tx := XTWriteDB().Begin()
  6510. 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
  6511. if err != nil {
  6512. tx.Rollback()
  6513. return err
  6514. }
  6515. tx.Commit()
  6516. return err
  6517. }
  6518. func UpdateNewMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64, tx *gorm.DB) error {
  6519. 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
  6520. if err != nil {
  6521. tx.Rollback()
  6522. return err
  6523. }
  6524. return err
  6525. }
  6526. func UpdateMedicalSumCountOne(drug_id int64, sum_count int64, orgid int64) error {
  6527. tx := XTWriteDB().Begin()
  6528. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6529. if err != nil {
  6530. tx.Rollback()
  6531. return err
  6532. }
  6533. tx.Commit()
  6534. return err
  6535. }
  6536. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  6537. // 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
  6538. // return err
  6539. //}
  6540. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6541. 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
  6542. return err
  6543. }
  6544. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6545. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6546. table := XTReadDB().Table("xt_base_drug as x d")
  6547. fmt.Println(table)
  6548. if startime > 0 {
  6549. db = db.Where("x.advice_date >= ?", startime)
  6550. }
  6551. if endtime > 0 {
  6552. db = db.Where("x.advice_date <= ?", endtime)
  6553. }
  6554. if orgid > 0 {
  6555. db = db.Where("x.user_org_id = ?", orgid)
  6556. }
  6557. if len(keyword) > 0 {
  6558. likeKey := "%" + keyword + "%"
  6559. 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)
  6560. }
  6561. offset := (page - 1) * limit
  6562. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  6563. return adviceinfo, total, err
  6564. }
  6565. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6566. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6567. table := XTReadDB().Table("xt_base_drug as x d")
  6568. fmt.Println(table)
  6569. if startime > 0 {
  6570. db = db.Where("x.advice_date >= ?", startime)
  6571. }
  6572. if endtime > 0 {
  6573. db = db.Where("x.advice_date <= ?", endtime)
  6574. }
  6575. if orgid > 0 {
  6576. db = db.Where("x.user_org_id = ?", orgid)
  6577. }
  6578. if len(keyword) > 0 {
  6579. likeKey := "%" + keyword + "%"
  6580. 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)
  6581. }
  6582. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  6583. return adviceinfo, total, err
  6584. }
  6585. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  6586. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  6587. return drug, err
  6588. }
  6589. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6590. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6591. table := XTReadDB().Table("xt_good_information as d")
  6592. fmt.Println(table)
  6593. if startime > 0 {
  6594. db = db.Where("x.record_date >= ?", startime)
  6595. }
  6596. if endtime > 0 {
  6597. db = db.Where("x.record_date <= ?", endtime)
  6598. }
  6599. if orgid > 0 {
  6600. db = db.Where("x.user_org_id = ?", orgid)
  6601. }
  6602. if len(keyword) > 0 {
  6603. likeKey := "%" + keyword + "%"
  6604. 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")
  6605. }
  6606. offset := (page - 1) * limit
  6607. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  6608. return project, total, err
  6609. }
  6610. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6611. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6612. table := XTReadDB().Table("xt_good_information as d")
  6613. fmt.Println(table)
  6614. if startime > 0 {
  6615. db = db.Where("x.record_date >= ?", startime)
  6616. }
  6617. if endtime > 0 {
  6618. db = db.Where("x.record_date <= ?", endtime)
  6619. }
  6620. if orgid > 0 {
  6621. db = db.Where("x.user_org_id = ?", orgid)
  6622. }
  6623. if len(keyword) > 0 {
  6624. likeKey := "%" + keyword + "%"
  6625. 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")
  6626. }
  6627. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  6628. return project, total, err
  6629. }
  6630. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  6631. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  6632. return good, err
  6633. }
  6634. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  6635. setting := models.XtStockSetting{}
  6636. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6637. if err == gorm.ErrRecordNotFound {
  6638. return nil, err
  6639. }
  6640. if err != nil {
  6641. return nil, err
  6642. }
  6643. return &setting, nil
  6644. }
  6645. func CreateStockSetting(setting models.XtStockSetting) error {
  6646. err := XTWriteDB().Create(&setting).Error
  6647. return err
  6648. }
  6649. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6650. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6651. return err
  6652. }
  6653. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6654. setting := models.XtStockSetting{}
  6655. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6656. return setting, err
  6657. }
  6658. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6659. detail := models.BloodAutomaticReduceDetail{}
  6660. 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
  6661. return detail, err
  6662. }
  6663. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6664. detail := models.WarehouseOutInfoNight{}
  6665. 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
  6666. return detail, err
  6667. }
  6668. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6669. setting := models.XtAdviceSetting{}
  6670. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6671. if err == gorm.ErrRecordNotFound {
  6672. return nil, err
  6673. }
  6674. if err != nil {
  6675. return nil, err
  6676. }
  6677. return &setting, nil
  6678. }
  6679. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6680. err := XTWriteDB().Create(&setting).Error
  6681. return err
  6682. }
  6683. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6684. 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
  6685. return err
  6686. }
  6687. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6688. setting := models.XtAdviceSetting{}
  6689. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6690. return setting, err
  6691. }
  6692. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6693. config := models.XtPrescriptionConfig{}
  6694. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6695. if err == gorm.ErrRecordNotFound {
  6696. return nil, err
  6697. }
  6698. if err != nil {
  6699. return nil, err
  6700. }
  6701. return &config, nil
  6702. }
  6703. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6704. err := XTWriteDB().Create(&prescriptionConfig).Error
  6705. return err
  6706. }
  6707. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6708. 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
  6709. return err
  6710. }
  6711. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6712. config := models.XtPrescriptionConfig{}
  6713. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6714. return config, err
  6715. }
  6716. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6717. var deliver_number int64 = 0
  6718. var stock_number int64 = 0
  6719. deliver_number = maxNumber
  6720. // 根据先进先出原则,查询最先入库的批次,进行出库
  6721. // 如果没有对应的库存,则报错
  6722. //开启事物
  6723. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6724. if err != nil {
  6725. return err
  6726. }
  6727. // 将该批次的剩余库存数量转换为拆零数量
  6728. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6729. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6730. if stock_number >= deliver_number {
  6731. var maxNumber int64 = 0
  6732. var minNumber int64 = 0
  6733. var stock_max_number int64 = 0
  6734. stock_max_number = warehouse.StockMaxNumber
  6735. maxNumber = deliver_number / drup.MinNumber
  6736. minNumber = deliver_number % drup.MinNumber
  6737. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6738. minNumber = maxNumber
  6739. }
  6740. if drup.MaxUnit != drup.MinUnit {
  6741. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6742. return errors.New("库存数量不足")
  6743. }
  6744. }
  6745. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6746. if warehouse.StockMaxNumber < 0 {
  6747. warehouse.StockMaxNumber = 0
  6748. }
  6749. if warehouse.StockMinNumber < 0 {
  6750. warehouse.StockMinNumber = 0
  6751. }
  6752. warehouse.Mtime = time.Now().Unix()
  6753. if warehouse.StockMinNumber < minNumber {
  6754. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6755. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6756. } else {
  6757. if minNumber > 0 {
  6758. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6759. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6760. } else {
  6761. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6762. }
  6763. }
  6764. if minNumber == 0 && maxNumber != 1 {
  6765. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6766. }
  6767. }
  6768. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6769. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6770. if warehouse.StockMinNumber == 0 {
  6771. warehouse.StockMaxNumber = stock_max_number
  6772. }
  6773. }
  6774. if drup.MaxUnit != drup.MinUnit {
  6775. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6776. return errors.New("库存数量不足")
  6777. }
  6778. }
  6779. if warehouse.StockMinNumber <= 0 {
  6780. warehouse.StockMinNumber = 0
  6781. }
  6782. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6783. fmt.Println(errThree)
  6784. //查询默认仓库
  6785. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6786. //查询默认仓库剩余多少库存
  6787. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6788. var sum_count int64
  6789. var sum_in_count int64
  6790. for _, it := range list {
  6791. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6792. if it.MaxUnit == baseDrug.MaxUnit {
  6793. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6794. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6795. sum_count += it.StockMaxNumber + it.StockMinNumber
  6796. sum_in_count += it.WarehousingCount
  6797. }
  6798. }
  6799. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6800. return nil
  6801. } else {
  6802. warehouse.StockMaxNumber = 0
  6803. warehouse.StockMinNumber = 0
  6804. warehouse.Mtime = time.Now().Unix()
  6805. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6806. fmt.Println(errThree)
  6807. //查询默认仓库
  6808. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6809. //查询默认仓库剩余多少库存
  6810. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6811. var sum_count int64
  6812. var sum_in_count int64
  6813. for _, it := range list {
  6814. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6815. if it.MaxUnit == baseDrug.MaxUnit {
  6816. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6817. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6818. sum_count += it.StockMaxNumber + it.StockMinNumber
  6819. sum_in_count += it.WarehousingCount
  6820. }
  6821. }
  6822. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6823. if errThree != nil {
  6824. return errThree
  6825. }
  6826. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6827. prescribingNumber_two_temp := deliver_number - stock_number
  6828. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6829. }
  6830. return
  6831. }
  6832. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6833. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6834. return err
  6835. }
  6836. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6837. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6838. return err
  6839. }
  6840. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6841. config := models.DrugOutConfig{}
  6842. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6843. if err == gorm.ErrRecordNotFound {
  6844. return nil, err
  6845. }
  6846. if err != nil {
  6847. return nil, err
  6848. }
  6849. return &config, nil
  6850. }
  6851. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6852. config := models.DrugOutConfig{}
  6853. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6854. return config, err
  6855. }
  6856. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6857. config := models.XtHisConfig{}
  6858. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6859. return config, err
  6860. }
  6861. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6862. err := XTWriteDB().Create(&config).Error
  6863. return err
  6864. }
  6865. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6866. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6867. return err
  6868. }
  6869. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6870. config := models.DrugOutConfig{}
  6871. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6872. return config, err
  6873. }
  6874. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6875. config := models.HisSettleStockConfig{}
  6876. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6877. return config, err
  6878. }
  6879. func UpdatedWarehouseOut(id int64) error {
  6880. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6881. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6882. return err
  6883. }
  6884. func UpdatedNewWarehouseOut(id int64, tx *gorm.DB) error {
  6885. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6886. if err != nil {
  6887. tx.Rollback()
  6888. return err
  6889. }
  6890. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6891. if err != nil {
  6892. tx.Rollback()
  6893. return err
  6894. }
  6895. return err
  6896. }
  6897. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6898. tx := XTWriteDB().Begin()
  6899. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6900. if err != nil {
  6901. tx.Rollback()
  6902. return err
  6903. }
  6904. tx.Commit()
  6905. return err
  6906. }
  6907. func UpdatedNewSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6908. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6909. if err != nil {
  6910. tx.Rollback()
  6911. return err
  6912. }
  6913. return err
  6914. }
  6915. func GetLastGoodWarehouseOutInfo(orgid int64, id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  6916. err = tx.Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6917. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6918. }).Find(&info).Error
  6919. if err != nil {
  6920. tx.Rollback()
  6921. return info, err
  6922. }
  6923. return info, err
  6924. }
  6925. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6926. info := models.WarehouseOutInfo{}
  6927. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6928. return info, err
  6929. }
  6930. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6931. err := tx.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
  6932. if err != nil {
  6933. tx.Rollback()
  6934. return err
  6935. }
  6936. return err
  6937. }
  6938. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6939. 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
  6940. return err
  6941. }
  6942. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6943. out := models.WarehouseOut{}
  6944. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6945. return out, err
  6946. }
  6947. func GetLastGoodWarehouseOutTwentyOne(orgid int64, tx *gorm.DB) (models.WarehouseOut, error) {
  6948. out := models.WarehouseOut{}
  6949. err := tx.Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6950. if err != gorm.ErrRecordNotFound {
  6951. if err != nil {
  6952. tx.Rollback()
  6953. return out, err
  6954. }
  6955. }
  6956. return out, err
  6957. }
  6958. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6959. ut := writeDb.Begin()
  6960. 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
  6961. if err != nil {
  6962. ut.Rollback()
  6963. return err
  6964. }
  6965. ut.Commit()
  6966. return err
  6967. }
  6968. func ModifyGoodOutInfoLog(good_id int64, id int64, tx *gorm.DB) (models.XtGoodWarehouseOutInfoLog, error) {
  6969. outInfoLog := models.XtGoodWarehouseOutInfoLog{}
  6970. err := tx.Model(&outInfoLog).Where("good_id = ? and warehouse_out_info_id = ?", good_id, id).Updates(map[string]interface{}{"status": 0}).Error
  6971. if err != nil {
  6972. tx.Rollback()
  6973. return outInfoLog, err
  6974. }
  6975. return outInfoLog, err
  6976. }
  6977. func UpdateaNewGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64, tx *gorm.DB) error {
  6978. err := tx.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
  6979. if err != nil {
  6980. tx.Rollback()
  6981. return err
  6982. }
  6983. return err
  6984. }
  6985. func ModifyGoodWarehouseOut(id int64) error {
  6986. ut := writeDb.Begin()
  6987. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6988. if err != nil {
  6989. ut.Rollback()
  6990. return err
  6991. }
  6992. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6993. if err != nil {
  6994. ut.Rollback()
  6995. return err
  6996. }
  6997. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6998. if err != nil {
  6999. ut.Rollback()
  7000. return err
  7001. }
  7002. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  7003. if err != nil {
  7004. ut.Rollback()
  7005. return err
  7006. }
  7007. ut.Commit()
  7008. return err
  7009. }
  7010. func ModifyNewGoodWarehouseOut(id int64, tx *gorm.DB) error {
  7011. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7012. if err != nil {
  7013. tx.Rollback()
  7014. return err
  7015. }
  7016. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7017. if err != nil {
  7018. tx.Rollback()
  7019. return err
  7020. }
  7021. err = tx.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7022. if err != nil {
  7023. tx.Rollback()
  7024. return err
  7025. }
  7026. err = tx.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  7027. if err != nil {
  7028. tx.Rollback()
  7029. return err
  7030. }
  7031. return err
  7032. }
  7033. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7034. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  7035. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  7036. }).Find(&info).Error
  7037. return info, err
  7038. }
  7039. func GetNewGoodWarehouseInfoById(waresing_id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7040. err = tx.Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  7041. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  7042. }).Find(&info).Error
  7043. if err != gorm.ErrRecordNotFound {
  7044. if err != nil {
  7045. tx.Rollback()
  7046. return info, err
  7047. }
  7048. }
  7049. return info, err
  7050. }
  7051. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  7052. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  7053. return info, err
  7054. }
  7055. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  7056. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "warehousing_unit": info.WarehousingUnit, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "license_number": info.LicenseNumber, "packing_price": info.PackingPrice, "register_number": info.RegisterNumber, "storehouse_id": info.StorehouseId, "good_code": info.GoodCode}).Error
  7057. return err
  7058. }
  7059. func UpdateNewWarehouseDetail(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  7060. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "warehousing_unit": info.WarehousingUnit, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "license_number": info.LicenseNumber, "packing_price": info.PackingPrice, "register_number": info.RegisterNumber, "storehouse_id": info.StorehouseId, "good_code": info.GoodCode}).Error
  7061. if err != nil {
  7062. tx.Rollback()
  7063. return err
  7064. }
  7065. return err
  7066. }
  7067. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  7068. tx := XTWriteDB().Begin()
  7069. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7070. if err != nil {
  7071. tx.Rollback()
  7072. return err
  7073. }
  7074. tx.Commit()
  7075. return err
  7076. }
  7077. func UpdateNewCheckWarehouseInfo(id int64, checker int64, tx *gorm.DB) error {
  7078. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7079. if err != nil {
  7080. tx.Rollback()
  7081. return err
  7082. }
  7083. return err
  7084. }
  7085. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  7086. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7087. return info, err
  7088. }
  7089. func GetNewWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7090. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7091. if err != gorm.ErrRecordNotFound {
  7092. if err != nil {
  7093. tx.Rollback()
  7094. return info, err
  7095. }
  7096. }
  7097. return info, err
  7098. }
  7099. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  7100. tx := XTWriteDB().Begin()
  7101. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7102. if err != nil {
  7103. tx.Rollback()
  7104. return err
  7105. }
  7106. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7107. if err != nil {
  7108. tx.Rollback()
  7109. return err
  7110. }
  7111. tx.Commit()
  7112. return err
  7113. }
  7114. func UpdateNewWarehouseInfoByIdListThree(count int64, id int64, tx *gorm.DB) error {
  7115. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7116. if err != nil {
  7117. tx.Rollback()
  7118. return err
  7119. }
  7120. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7121. if err != nil {
  7122. tx.Rollback()
  7123. return err
  7124. }
  7125. return err
  7126. }
  7127. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  7128. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7129. return info, err
  7130. }
  7131. func FindNewLastWarehousingSix(org_id int64, tx *gorm.DB) (info models.Warehousing, err error) {
  7132. err = tx.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7133. if err != gorm.ErrRecordNotFound {
  7134. if err != nil {
  7135. tx.Rollback()
  7136. return info, err
  7137. }
  7138. }
  7139. return info, err
  7140. }
  7141. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  7142. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7143. return info, err
  7144. }
  7145. func FindNewLastWarehousingSeven(org_id int64, tx *gorm.DB) (info models.DrugWarehouse, err error) {
  7146. err = tx.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7147. if err != gorm.ErrRecordNotFound {
  7148. if err != nil {
  7149. tx.Rollback()
  7150. return info, err
  7151. }
  7152. }
  7153. return info, err
  7154. }
  7155. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7156. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7157. return info, err
  7158. }
  7159. func FindNewDrugWarehouseInfoListById(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7160. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7161. if err != gorm.ErrRecordNotFound {
  7162. if err != nil {
  7163. tx.Rollback()
  7164. return info, err
  7165. }
  7166. }
  7167. return info, err
  7168. }
  7169. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  7170. err := XTWriteDB().Create(&info).Error
  7171. return err
  7172. }
  7173. func CreatedNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7174. err := tx.Create(&info).Error
  7175. if err != nil {
  7176. tx.Rollback()
  7177. return err
  7178. }
  7179. return err
  7180. }
  7181. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  7182. err := XTWriteDB().Save(&info).Error
  7183. return err
  7184. }
  7185. func UpdateNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7186. err := tx.Save(&info).Error
  7187. if err != nil {
  7188. tx.Rollback()
  7189. return err
  7190. }
  7191. return err
  7192. }
  7193. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  7194. tx := XTWriteDB().Begin()
  7195. 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
  7196. if err != nil {
  7197. tx.Rollback()
  7198. return err
  7199. }
  7200. tx.Commit()
  7201. return err
  7202. }
  7203. func CheckNewWarehousingInfo(id int64, orgid int64, checker int64, tx *gorm.DB) error {
  7204. 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
  7205. if err != nil {
  7206. tx.Rollback()
  7207. return err
  7208. }
  7209. return err
  7210. }
  7211. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  7212. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7213. return info, err
  7214. }
  7215. func GetNewWarehousingInfoByList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7216. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).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 AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  7226. tx := XTWriteDB().Begin()
  7227. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7228. if err != nil {
  7229. tx.Rollback()
  7230. return err
  7231. }
  7232. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7233. if err != nil {
  7234. tx.Rollback()
  7235. return err
  7236. }
  7237. tx.Commit()
  7238. return err
  7239. }
  7240. func AddNewDrugWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7241. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7242. if err != nil {
  7243. tx.Rollback()
  7244. return err
  7245. }
  7246. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7247. if err != nil {
  7248. tx.Rollback()
  7249. return err
  7250. }
  7251. return err
  7252. }
  7253. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  7254. tx := XTWriteDB().Begin()
  7255. 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
  7256. if err != nil {
  7257. tx.Rollback()
  7258. return err
  7259. }
  7260. tx.Commit()
  7261. return err
  7262. }
  7263. func UpdateNewWarehousingInfoFlow(id int64, orgid int64, tx *gorm.DB) error {
  7264. 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
  7265. if err != nil {
  7266. tx.Rollback()
  7267. return err
  7268. }
  7269. return err
  7270. }
  7271. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  7272. tx := XTWriteDB().Begin()
  7273. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7274. if err != nil {
  7275. tx.Rollback()
  7276. return err
  7277. }
  7278. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7279. tx.Commit()
  7280. return err
  7281. }
  7282. func AddNewDrugWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7283. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7284. if err != nil {
  7285. tx.Rollback()
  7286. return err
  7287. }
  7288. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7289. if err != nil {
  7290. tx.Rollback()
  7291. return err
  7292. }
  7293. return err
  7294. }
  7295. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  7296. tx := XTWriteDB().Begin()
  7297. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7298. if err != nil {
  7299. tx.Rollback()
  7300. return err
  7301. }
  7302. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7303. if err != nil {
  7304. tx.Rollback()
  7305. return err
  7306. }
  7307. tx.Commit()
  7308. return err
  7309. }
  7310. func ReturnNewCheckWarehouseingInfo(id int64, orgid int64, tx *gorm.DB) error {
  7311. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7312. if err != nil {
  7313. tx.Rollback()
  7314. return err
  7315. }
  7316. err = tx.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7317. if err != nil {
  7318. tx.Rollback()
  7319. return err
  7320. }
  7321. err = tx.Model(models.XtDrugWarehouseInfoLog{}).Where("warehouse_info_id = ? and status=1", id).Update(map[string]interface{}{"status": 0}).Error
  7322. if err != nil {
  7323. tx.Rollback()
  7324. return err
  7325. }
  7326. return err
  7327. }
  7328. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  7329. tx := XTWriteDB().Begin()
  7330. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7331. if err != nil {
  7332. tx.Rollback()
  7333. return err
  7334. }
  7335. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7336. if err != nil {
  7337. tx.Rollback()
  7338. return err
  7339. }
  7340. tx.Commit()
  7341. return err
  7342. }
  7343. func AddNewDrugReturnWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7344. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7345. if err != nil {
  7346. tx.Rollback()
  7347. return err
  7348. }
  7349. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7350. if err != nil {
  7351. tx.Rollback()
  7352. return err
  7353. }
  7354. return err
  7355. }
  7356. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  7357. tx := XTWriteDB().Begin()
  7358. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7359. if err != nil {
  7360. tx.Rollback()
  7361. return err
  7362. }
  7363. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7364. if err != nil {
  7365. tx.Rollback()
  7366. return err
  7367. }
  7368. tx.Commit()
  7369. return err
  7370. }
  7371. func AddNewDrugReturnWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7372. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7373. if err != nil {
  7374. tx.Rollback()
  7375. return err
  7376. }
  7377. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7378. if err != nil {
  7379. tx.Rollback()
  7380. return err
  7381. }
  7382. return err
  7383. }
  7384. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  7385. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "retail_price": info.RetailPrice, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "drug_code": info.DrugCode, "batch_number": info.BatchNumber}).Error
  7386. return err
  7387. }
  7388. func UpdateNewDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7389. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "retail_price": info.RetailPrice, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "drug_code": info.DrugCode, "batch_number": info.BatchNumber}).Error
  7390. if err != nil {
  7391. tx.Rollback()
  7392. return err
  7393. }
  7394. return err
  7395. }
  7396. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  7397. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7398. return err
  7399. }
  7400. func UpdatedNewDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7401. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7402. if err != nil {
  7403. tx.Rollback()
  7404. return err
  7405. }
  7406. return err
  7407. }
  7408. func UpdatedSelfDrugWarehouseInfo(info *models.XtSelfDrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7409. err := tx.Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7410. if err != gorm.ErrRecordNotFound {
  7411. return err
  7412. }
  7413. return err
  7414. }
  7415. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  7416. out := models.DrugWarehouseOut{}
  7417. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7418. return out, err
  7419. }
  7420. func GetNewDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7421. out := models.DrugWarehouseOut{}
  7422. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7423. if err != gorm.ErrRecordNotFound {
  7424. if err != nil {
  7425. tx.Rollback()
  7426. return out, err
  7427. }
  7428. }
  7429. return out, err
  7430. }
  7431. func GetSelfDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7432. out := models.XtSelfDrugWarehouseOut{}
  7433. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7434. if err != gorm.ErrRecordNotFound {
  7435. if err != nil {
  7436. tx.Rollback()
  7437. return out, err
  7438. }
  7439. }
  7440. return out, err
  7441. }
  7442. func GetSelfDrugLastWarehouseOutOne(id int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7443. out := models.XtSelfDrugWarehouseOut{}
  7444. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7445. if err != gorm.ErrRecordNotFound {
  7446. if err != nil {
  7447. tx.Rollback()
  7448. return out, err
  7449. }
  7450. }
  7451. return out, err
  7452. }
  7453. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7454. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7455. return info, err
  7456. }
  7457. func GetNewDrugWarehouseOutListById(warehouse_out_id int64, orgId int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  7458. err = tx.Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7459. if err != gorm.ErrRecordNotFound {
  7460. if err != nil {
  7461. tx.Rollback()
  7462. return info, err
  7463. }
  7464. }
  7465. return info, err
  7466. }
  7467. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  7468. tx := XTWriteDB().Begin()
  7469. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7470. if err != nil {
  7471. tx.Rollback()
  7472. return err
  7473. }
  7474. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7475. if err != nil {
  7476. tx.Rollback()
  7477. return err
  7478. }
  7479. tx.Commit()
  7480. return err
  7481. }
  7482. func UpdateNewCheckDrugOut(out models.DrugWarehouseOut, id int64, tx *gorm.DB) error {
  7483. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7484. if err != nil {
  7485. tx.Rollback()
  7486. return err
  7487. }
  7488. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7489. if err != nil {
  7490. tx.Rollback()
  7491. return err
  7492. }
  7493. return err
  7494. }
  7495. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  7496. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7497. return err
  7498. }
  7499. func UpdateNewCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64, tx *gorm.DB) error {
  7500. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7501. if err != nil {
  7502. tx.Rollback()
  7503. return err
  7504. }
  7505. return err
  7506. }
  7507. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  7508. out := models.DrugWarehouseOut{}
  7509. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  7510. return out, err
  7511. }
  7512. func GetNewDrugWarehouseOutByLastId(id int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7513. out := models.DrugWarehouseOut{}
  7514. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7515. if err != gorm.ErrRecordNotFound {
  7516. if err != nil {
  7517. tx.Rollback()
  7518. return out, err
  7519. }
  7520. }
  7521. return out, err
  7522. }
  7523. func UpdateDrugMaxNumber(count int64, id int64) error {
  7524. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7525. return err
  7526. }
  7527. func UpdateNewDrugMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7528. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7529. if err != gorm.ErrRecordNotFound {
  7530. if err != nil {
  7531. tx.Rollback()
  7532. return err
  7533. }
  7534. }
  7535. return err
  7536. }
  7537. func UpdateNewDrugMaxNumberOne(count int64, id int64, tx *gorm.DB) error {
  7538. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7539. if err != gorm.ErrRecordNotFound {
  7540. if err != nil {
  7541. tx.Rollback()
  7542. return err
  7543. }
  7544. }
  7545. return err
  7546. }
  7547. func UpdateDrugMinNumber(count int64, id int64) error {
  7548. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7549. return err
  7550. }
  7551. func UpdateNewDrugMinNumber(count int64, id int64, tx *gorm.DB) error {
  7552. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7553. if err != nil {
  7554. tx.Rollback()
  7555. return err
  7556. }
  7557. return err
  7558. }
  7559. func DeleteDrugOutFlow(id int64, orgid int64) error {
  7560. 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
  7561. return err
  7562. }
  7563. func DeleteNewDrugOutFlow(id int64, orgid int64, tx *gorm.DB) error {
  7564. err := tx.Model(models.DrugFlow{}).Where("warehouse_out_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  7565. if err != nil {
  7566. tx.Rollback()
  7567. return err
  7568. }
  7569. return err
  7570. }
  7571. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  7572. 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
  7573. return outInfo, err
  7574. }
  7575. func GetNewWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.WarehouseOutInfo, err error) {
  7576. err = tx.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
  7577. if err != gorm.ErrRecordNotFound {
  7578. if err != nil {
  7579. tx.Rollback()
  7580. return outInfo, err
  7581. }
  7582. }
  7583. return outInfo, err
  7584. }
  7585. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7586. 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
  7587. return outInfo, err
  7588. }
  7589. func GetNewDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7590. err = tx.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
  7591. if err != gorm.ErrRecordNotFound {
  7592. if err != nil {
  7593. tx.Rollback()
  7594. return outInfo, err
  7595. }
  7596. }
  7597. return outInfo, err
  7598. }
  7599. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7600. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7601. return info, err
  7602. }
  7603. func GetNewAllStockInList(id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7604. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7605. if err != gorm.ErrRecordNotFound {
  7606. if err != nil {
  7607. tx.Rollback()
  7608. return info, err
  7609. }
  7610. }
  7611. return info, err
  7612. }
  7613. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7614. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7615. return info, err
  7616. }
  7617. func GetNewDrugWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7618. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7619. if err != gorm.ErrRecordNotFound {
  7620. if err != nil {
  7621. tx.Rollback()
  7622. return info, err
  7623. }
  7624. }
  7625. return info, err
  7626. }
  7627. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  7628. 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
  7629. return info, err
  7630. }
  7631. func GetNewStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7632. err = tx.Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", goodid, storehouse_id, orgId).Find(&info).Error
  7633. if err != gorm.ErrRecordNotFound {
  7634. if err != nil {
  7635. tx.Rollback()
  7636. return info, err
  7637. }
  7638. }
  7639. return info, err
  7640. }
  7641. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  7642. 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
  7643. 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
  7644. return err
  7645. }
  7646. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  7647. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7648. return err
  7649. }
  7650. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  7651. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7652. return err
  7653. }
  7654. func UpdateNewGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7655. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7656. if err != gorm.ErrRecordNotFound {
  7657. if err != nil {
  7658. tx.Rollback()
  7659. return err
  7660. }
  7661. }
  7662. return err
  7663. }
  7664. func GetGoodFlowByWarehouseOutId(good_id int64, user_org_id int64, id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  7665. err = tx.Where("good_id = ? and user_org_id = ? and warehouse_out_detail_id = ? and status=1", good_id, user_org_id, id).Find(&flow).Error
  7666. if err != gorm.ErrRecordNotFound {
  7667. if err != nil {
  7668. tx.Rollback()
  7669. return flow, err
  7670. }
  7671. }
  7672. return
  7673. }
  7674. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  7675. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7676. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7677. return err
  7678. }
  7679. func UpdateNewGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7680. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7681. if err != nil {
  7682. tx.Rollback()
  7683. return err
  7684. }
  7685. 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
  7686. if err != nil {
  7687. tx.Rollback()
  7688. return err
  7689. }
  7690. return err
  7691. }
  7692. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  7693. 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
  7694. return stock, err
  7695. }
  7696. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  7697. 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
  7698. return err
  7699. }
  7700. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7701. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7702. return info, err
  7703. }
  7704. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  7705. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7706. return info, err
  7707. }
  7708. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  7709. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7710. return info, err
  7711. }
  7712. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  7713. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  7714. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  7715. return err
  7716. }
  7717. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7718. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  7719. return info, err
  7720. }
  7721. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  7722. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and status = 1", warehouse_info_id, orgid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", stock_count)).Error
  7723. return err
  7724. }
  7725. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  7726. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  7727. return err
  7728. }
  7729. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  7730. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7731. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7732. return err
  7733. }
  7734. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  7735. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("cancel_out_detail_id = ? and user_org_id = ? and status = 1 and good_id =?", id, orgid, goodid).Update(map[string]interface{}{"status": 0}).Error
  7736. return err
  7737. }
  7738. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  7739. 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
  7740. 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
  7741. return err
  7742. }
  7743. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  7744. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  7745. 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
  7746. return err
  7747. }
  7748. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  7749. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  7750. return info, err
  7751. }
  7752. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  7753. 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
  7754. 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
  7755. return err
  7756. }
  7757. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  7758. 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
  7759. return info, err
  7760. }
  7761. func GetSelfDrugAllStockInfoOne(patient_id int64, orgid int64, drugid int64) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  7762. err = XTReadDB().Where("patient_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", patient_id, orgid, drugid).Find(&info).Error
  7763. return info, err
  7764. }
  7765. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7766. 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
  7767. if err != gorm.ErrRecordNotFound {
  7768. if err != nil {
  7769. tx.Rollback()
  7770. return info, err
  7771. }
  7772. }
  7773. return info, err
  7774. }
  7775. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  7776. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7777. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7778. return err
  7779. }
  7780. func UpdateNewBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7781. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7782. if err != gorm.ErrRecordNotFound {
  7783. if err != nil {
  7784. tx.Rollback()
  7785. return err
  7786. }
  7787. }
  7788. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7789. if err != gorm.ErrRecordNotFound {
  7790. if err != nil {
  7791. tx.Rollback()
  7792. return err
  7793. }
  7794. }
  7795. return err
  7796. }
  7797. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  7798. err = writeDb.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7799. return err
  7800. }
  7801. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7802. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7803. if err != nil {
  7804. tx.Rollback()
  7805. return err
  7806. }
  7807. return err
  7808. }
  7809. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7810. 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
  7811. return info, err
  7812. }
  7813. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7814. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7815. return info, err
  7816. }
  7817. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  7818. 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
  7819. 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
  7820. return err
  7821. }
  7822. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  7823. stock := models.DrugCancelStock{}
  7824. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  7825. return stock, err
  7826. }
  7827. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7828. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  7829. return stock, err
  7830. }
  7831. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7832. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  7833. return stock, err
  7834. }
  7835. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7836. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  7837. return info, err
  7838. }
  7839. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  7840. 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
  7841. return err
  7842. }
  7843. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7844. 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
  7845. if err != nil {
  7846. tx.Rollback()
  7847. return err
  7848. }
  7849. return err
  7850. }
  7851. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  7852. 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
  7853. return err
  7854. }
  7855. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7856. 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
  7857. if err != nil {
  7858. tx.Rollback()
  7859. return err
  7860. }
  7861. return err
  7862. }
  7863. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  7864. 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
  7865. return err
  7866. }
  7867. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  7868. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7869. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7870. return err
  7871. }
  7872. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7873. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  7874. return info, err
  7875. }
  7876. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  7877. 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
  7878. return err
  7879. }
  7880. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7881. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7882. return err
  7883. }
  7884. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7885. 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
  7886. return err
  7887. }
  7888. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  7889. info := models.DrugWarehouseInfo{}
  7890. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7891. return info, err
  7892. }
  7893. func GetSelfDrugInfoSix(id int64, orgid int64) (models.XtSelfDrugWarehouseInfo, error) {
  7894. info := models.XtSelfDrugWarehouseInfo{}
  7895. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7896. return info, err
  7897. }
  7898. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  7899. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  7900. if storehouse_id > 0 {
  7901. db = db.Where("storehouse_id = ?", storehouse_id)
  7902. }
  7903. if orgid > 0 {
  7904. db = db.Where("user_org_id = ?", orgid)
  7905. }
  7906. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  7907. return storeinventory, err
  7908. }
  7909. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  7910. detail := models.AutomaticReduceDetail{}
  7911. 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
  7912. return detail, err
  7913. }
  7914. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  7915. detail := models.WarehouseOutInfo{}
  7916. 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
  7917. return detail, err
  7918. }
  7919. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  7920. stock := models.XtGoodStockCount{}
  7921. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  7922. return stock, err
  7923. }
  7924. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  7925. err := XTWriteDB().Create(&stock).Error
  7926. return err
  7927. }
  7928. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7929. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error
  7930. return err
  7931. }
  7932. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7933. ut := XTWriteDB().Begin()
  7934. 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
  7935. if err != nil {
  7936. ut.Rollback()
  7937. return err
  7938. }
  7939. ut.Commit()
  7940. return err
  7941. }
  7942. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  7943. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7944. return err
  7945. }
  7946. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  7947. 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
  7948. if err != nil {
  7949. tx.Rollback()
  7950. return err
  7951. }
  7952. return err
  7953. }
  7954. func CreateDrugStock(stock models.XtDrugStockCount) error {
  7955. err := XTWriteDB().Create(&stock).Error
  7956. return err
  7957. }
  7958. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  7959. ut := XTWriteDB().Begin()
  7960. 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
  7961. if err != nil {
  7962. ut.Rollback()
  7963. return err
  7964. }
  7965. 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
  7966. if err != nil {
  7967. ut.Rollback()
  7968. return err
  7969. }
  7970. ut.Commit()
  7971. return err
  7972. }
  7973. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  7974. ut := XTWriteDB().Begin()
  7975. 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
  7976. if err != nil {
  7977. ut.Rollback()
  7978. return err
  7979. }
  7980. ut.Commit()
  7981. return err
  7982. }
  7983. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  7984. ut := XTWriteDB().Begin()
  7985. 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
  7986. if err != nil {
  7987. ut.Rollback()
  7988. return err
  7989. }
  7990. 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
  7991. if err != nil {
  7992. ut.Rollback()
  7993. return err
  7994. }
  7995. ut.Commit()
  7996. return err
  7997. }
  7998. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  7999. ut := XTWriteDB().Begin()
  8000. 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
  8001. if err != nil {
  8002. ut.Rollback()
  8003. return err
  8004. }
  8005. ut.Commit()
  8006. return err
  8007. }
  8008. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  8009. ut := XTWriteDB().Begin()
  8010. 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
  8011. if err != nil {
  8012. ut.Rollback()
  8013. return err
  8014. }
  8015. 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
  8016. if err != nil {
  8017. ut.Rollback()
  8018. return err
  8019. }
  8020. ut.Commit()
  8021. return err
  8022. }
  8023. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  8024. ut := XTWriteDB().Begin()
  8025. 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
  8026. if err != nil {
  8027. ut.Rollback()
  8028. return err
  8029. }
  8030. 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
  8031. if err != nil {
  8032. ut.Rollback()
  8033. return err
  8034. }
  8035. ut.Commit()
  8036. return err
  8037. }
  8038. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  8039. if orgid == 10191 || orgid == 10138 || orgid == 10278 {
  8040. 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
  8041. return info, err
  8042. } else {
  8043. 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
  8044. return info, err
  8045. }
  8046. }
  8047. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  8048. config := models.HisSettleStockConfig{}
  8049. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8050. if err == gorm.ErrRecordNotFound {
  8051. return nil, err
  8052. }
  8053. if err != nil {
  8054. return nil, err
  8055. }
  8056. return &config, nil
  8057. }
  8058. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  8059. err := XTWriteDB().Create(&config).Error
  8060. return err
  8061. }
  8062. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  8063. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  8064. return err
  8065. }
  8066. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  8067. config := models.DrugStockConfig{}
  8068. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  8069. return config, err
  8070. }
  8071. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  8072. stockConfig := models.PharmacyConfig{}
  8073. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  8074. return stockConfig, err
  8075. }
  8076. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  8077. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  8078. return err
  8079. }
  8080. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  8081. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  8082. return err
  8083. }
  8084. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  8085. config := models.HisSettleStockConfig{}
  8086. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8087. return config, nil
  8088. }
  8089. func GetGoodWareHouseOutInfoByGood(good_id int64, sys_record_time int64, user_org_id int64) (info []*models.WarehouseOutInfo, err error) {
  8090. err = XTReadDB().Where("good_id= ? and sys_record_time = ? and org_id = ? and status=1", good_id, sys_record_time, user_org_id).Find(&info).Error
  8091. return info, err
  8092. }
  8093. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  8094. info := models.GoodInformationFourty{}
  8095. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  8096. return info, err
  8097. }
  8098. func GetGoodWarehouseInfoByGoodList(good_id int64) (info []*models.WarehousingInfo, err error) {
  8099. err = XTReadDB().Where("good_id = ? and status =1 and stock_count>0 and is_check = 1", good_id).Find(&info).Error
  8100. return info, err
  8101. }
  8102. func GetAllWarehouseInfo(good_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  8103. err = XTReadDB().Where("good_id = ? and user_org_id =? and status=1 and is_check =1", good_id, user_org_id).Find(&info).Error
  8104. return info, err
  8105. }
  8106. func GetHisPrescriptionByGoodId(good_id int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  8107. err = XTReadDB().Where("project_id = ? and user_org_id =? and status=1 and type =3", good_id, user_org_id).Find(&project).Error
  8108. return project, err
  8109. }
  8110. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  8111. advice := models.HisDoctorAdvice{}
  8112. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8113. return advice, err
  8114. }
  8115. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  8116. advice := models.DoctorAdvice{}
  8117. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8118. return advice, err
  8119. }
  8120. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  8121. out := models.WarehouseOut{}
  8122. 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
  8123. return out, err
  8124. }
  8125. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  8126. config := models.XtGoodOutConfig{}
  8127. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8128. if err == gorm.ErrRecordNotFound {
  8129. return nil, err
  8130. }
  8131. if err != nil {
  8132. return nil, err
  8133. }
  8134. return &config, nil
  8135. }
  8136. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  8137. err := XTWriteDB().Create(&good).Error
  8138. return err
  8139. }
  8140. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  8141. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  8142. return err
  8143. }
  8144. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  8145. config := models.XtGoodOutConfig{}
  8146. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8147. return config, err
  8148. }
  8149. func UpdateGoodAutoConfig(user_org_id int64) error {
  8150. gobalConfig := models.GobalConfig{}
  8151. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8152. return err
  8153. }
  8154. func UpdateGoodOutConfig(user_org_id int64) error {
  8155. config := models.XtGoodOutConfig{}
  8156. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8157. return err
  8158. }
  8159. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  8160. 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
  8161. return info, err
  8162. }
  8163. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  8164. 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
  8165. return good, err
  8166. }
  8167. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  8168. 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
  8169. return err
  8170. }
  8171. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8172. 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
  8173. return err
  8174. }
  8175. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8176. 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
  8177. return err
  8178. }
  8179. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8180. out := models.WarehouseOut{}
  8181. 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
  8182. return out, err
  8183. }
  8184. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8185. out := models.WarehouseOut{}
  8186. 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
  8187. if err != gorm.ErrRecordNotFound {
  8188. if err != nil {
  8189. tx.Rollback()
  8190. return out, err
  8191. }
  8192. }
  8193. return out, err
  8194. }
  8195. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8196. out := models.WarehouseOut{}
  8197. 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
  8198. return out, err
  8199. }
  8200. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8201. out := models.WarehouseOut{}
  8202. 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
  8203. if err != gorm.ErrRecordNotFound {
  8204. if err != nil {
  8205. tx.Rollback()
  8206. return out, err
  8207. }
  8208. }
  8209. return out, err
  8210. }
  8211. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  8212. goodsType := models.GoodsType{}
  8213. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  8214. return goodsType, err
  8215. }
  8216. func GetAllGoodTypeName(good_type_id int64, org_id int64) (good_type_name string) {
  8217. goodsTypeList, _ := GetAllGoodType(org_id)
  8218. for _, item := range goodsTypeList {
  8219. if good_type_id == item.ID {
  8220. good_type_name = item.TypeName
  8221. }
  8222. }
  8223. return good_type_name
  8224. }
  8225. func UpdateGoodTypeName(good_id int64, good_type_name string) (models.GoodInformation, error) {
  8226. information := models.GoodInformation{}
  8227. err := XTWriteDB().Model(&information).Where("id = ? and status =1", good_id).Updates(map[string]interface{}{"good_type_name": good_type_name}).Error
  8228. return information, err
  8229. }
  8230. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  8231. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  8232. return info, err
  8233. }
  8234. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  8235. ut := writeDb.Begin()
  8236. 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
  8237. if err != nil {
  8238. ut.Rollback()
  8239. return err
  8240. }
  8241. ut.Commit()
  8242. return err
  8243. }
  8244. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  8245. 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
  8246. if err != nil {
  8247. tx.Rollback()
  8248. return err
  8249. }
  8250. return err
  8251. }
  8252. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  8253. info := models.WarehouseOutInfo{}
  8254. var err error
  8255. 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
  8256. if err == gorm.ErrRecordNotFound {
  8257. return nil, err
  8258. }
  8259. if err != nil {
  8260. return nil, err
  8261. }
  8262. return &info, nil
  8263. }
  8264. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  8265. info := models.WarehouseOutInfo{}
  8266. 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
  8267. return info, err
  8268. }
  8269. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8270. info := models.WarehouseOutInfo{}
  8271. 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
  8272. if err != gorm.ErrRecordNotFound {
  8273. if err != nil {
  8274. tx.Rollback()
  8275. return info, err
  8276. }
  8277. }
  8278. return info, err
  8279. }
  8280. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  8281. info := models.WarehouseOutInfo{}
  8282. 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
  8283. return info, err
  8284. }
  8285. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8286. info := models.WarehouseOutInfo{}
  8287. 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
  8288. if err != gorm.ErrRecordNotFound {
  8289. if err != nil {
  8290. tx.Rollback()
  8291. return info, err
  8292. }
  8293. }
  8294. return info, err
  8295. }
  8296. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  8297. info := models.WarehouseOutInfo{}
  8298. var err error
  8299. 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
  8300. if err == gorm.ErrRecordNotFound {
  8301. return nil, err
  8302. }
  8303. if err != nil {
  8304. return nil, err
  8305. }
  8306. return &info, nil
  8307. }
  8308. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  8309. info := models.WarehouseOutInfo{}
  8310. var err error
  8311. 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
  8312. if err != gorm.ErrRecordNotFound {
  8313. if err != nil {
  8314. tx.Rollback()
  8315. return &info, err
  8316. }
  8317. }
  8318. return &info, err
  8319. }
  8320. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  8321. db := XTReadDB().Model(&info).Where("status = 1")
  8322. if startime > 0 {
  8323. db = db.Where("ctime>=?", startime)
  8324. }
  8325. if endtime > 0 {
  8326. db = db.Where("ctime<=?", endtime)
  8327. }
  8328. if orgid > 0 {
  8329. db = db.Where("org_id = ?", orgid)
  8330. }
  8331. if len(good_id) > 0 {
  8332. db = db.Where("good_id in(?)", good_id)
  8333. }
  8334. if storehouse_id > 0 {
  8335. db = db.Where("storehouse_id = ?", storehouse_id)
  8336. }
  8337. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  8338. return info, err
  8339. }
  8340. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  8341. db := XTReadDB().Model(&info).Where("status= 1")
  8342. if start_time > 0 {
  8343. db = db.Where("ctime>=?", start_time)
  8344. }
  8345. if end_time > 0 {
  8346. db = db.Where("ctime <=?", end_time)
  8347. }
  8348. if orgid > 0 {
  8349. db = db.Where("org_id = ?", orgid)
  8350. }
  8351. if len(good_id) > 0 {
  8352. db = db.Where("good_id in(?)", good_id)
  8353. }
  8354. if storehouse_id > 0 {
  8355. db = db.Where("storehouse_id = ?", storehouse_id)
  8356. }
  8357. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  8358. return info, err
  8359. }
  8360. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  8361. config := models.XtGoodOutConfig{}
  8362. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8363. return config, err
  8364. }
  8365. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  8366. 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
  8367. 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
  8368. return err
  8369. }
  8370. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  8371. tx := XTWriteDB().Begin()
  8372. for _, item := range list {
  8373. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8374. if err != nil {
  8375. tx.Rollback()
  8376. return err
  8377. }
  8378. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8379. if err != nil {
  8380. tx.Rollback()
  8381. return err
  8382. }
  8383. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8384. if err != nil {
  8385. tx.Rollback()
  8386. return err
  8387. }
  8388. var goodList []*models.WarehousingInfo
  8389. 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
  8390. var flush_count int64
  8391. for _, it := range goodList {
  8392. flush_count += it.StockCount
  8393. }
  8394. 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
  8395. if err != nil {
  8396. tx.Rollback()
  8397. return err
  8398. }
  8399. 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
  8400. if err != nil {
  8401. tx.Rollback()
  8402. return err
  8403. }
  8404. storeHouseConfig := models.XtStorehouseConfig{}
  8405. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8406. var stockList []*models.WarehousingInfo
  8407. //查询该耗材剩余多少库存
  8408. 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
  8409. var sum_count int64
  8410. var sum_in_count int64
  8411. for _, it := range stockList {
  8412. sum_count += it.StockCount
  8413. sum_in_count += it.WarehousingCount
  8414. }
  8415. 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
  8416. if err != nil {
  8417. tx.Rollback()
  8418. return err
  8419. }
  8420. 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
  8421. if err != nil {
  8422. tx.Rollback()
  8423. return err
  8424. }
  8425. }
  8426. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8427. if err != nil {
  8428. tx.Rollback()
  8429. return err
  8430. }
  8431. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8432. tx.Commit()
  8433. return err
  8434. }
  8435. func UpdateNewWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64, tx *gorm.DB) error {
  8436. for _, item := range list {
  8437. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8438. if err != nil {
  8439. tx.Rollback()
  8440. return err
  8441. }
  8442. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8443. if err != nil {
  8444. tx.Rollback()
  8445. return err
  8446. }
  8447. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8448. if err != nil {
  8449. tx.Rollback()
  8450. return err
  8451. }
  8452. var goodList []*models.WarehousingInfo
  8453. 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
  8454. var flush_count int64
  8455. for _, it := range goodList {
  8456. flush_count += it.StockCount
  8457. }
  8458. 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
  8459. if err != nil {
  8460. tx.Rollback()
  8461. return err
  8462. }
  8463. 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
  8464. if err != nil {
  8465. tx.Rollback()
  8466. return err
  8467. }
  8468. storeHouseConfig := models.XtStorehouseConfig{}
  8469. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8470. var stockList []*models.WarehousingInfo
  8471. //查询该耗材剩余多少库存
  8472. 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
  8473. var sum_count int64
  8474. var sum_in_count int64
  8475. for _, it := range stockList {
  8476. sum_count += it.StockCount
  8477. sum_in_count += it.WarehousingCount
  8478. }
  8479. 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
  8480. if err != nil {
  8481. tx.Rollback()
  8482. return err
  8483. }
  8484. 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
  8485. if err != nil {
  8486. tx.Rollback()
  8487. return err
  8488. }
  8489. err = tx.Model(models.XtGoodWarehouseInfoLog{}).Where("warehouse_info_id = ? and status=1", item.ID).Update(map[string]interface{}{"status": 0}).Error
  8490. if err != nil {
  8491. tx.Rollback()
  8492. return err
  8493. }
  8494. }
  8495. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8496. if err != nil {
  8497. tx.Rollback()
  8498. return err
  8499. }
  8500. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8501. return err
  8502. }
  8503. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8504. if storehouse_id == 0 {
  8505. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  8506. }
  8507. if storehouse_id > 0 {
  8508. 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
  8509. }
  8510. return info, err
  8511. }
  8512. func FindFirstWarehousingInfoByStockFour(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8513. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").Last(&info).Error
  8514. return info, err
  8515. }
  8516. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  8517. 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
  8518. return list, err
  8519. }
  8520. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  8521. 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
  8522. return infor, err
  8523. }
  8524. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  8525. info := models.WarehouseOutInfo{}
  8526. 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
  8527. return info, err
  8528. }
  8529. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  8530. info := models.WarehousingInfo{}
  8531. 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
  8532. return info, err
  8533. }
  8534. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8535. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8536. return list, err
  8537. }
  8538. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8539. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8540. return list, err
  8541. }
  8542. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  8543. templateL := models.HisPrescriptionProjectTemplateL{}
  8544. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  8545. return templateL, err
  8546. }
  8547. func ModifyNewGoodWarehouseInfoStockMinNumber(count int64, goodid int64, orgid int64, id int64, tx *gorm.DB) error {
  8548. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and good_id = ? and status = 1", id, orgid, goodid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  8549. if err != nil {
  8550. tx.Rollback()
  8551. return err
  8552. }
  8553. return err
  8554. }
  8555. func GetDrugInvetoryByDrugId(user_org_id int64, drug_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  8556. err = XTReadDB().Where("org_id = ? and drug_id = ? and status=1", user_org_id, drug_id).Preload("XtBaseDrug", "org_id = ? and status=1", user_org_id).Order("id desc").Find(&info).Error
  8557. return info, err
  8558. }
  8559. func UpdateNewDrugWarehouseInfoById(id int64, last_stock_max_number int64, last_stock_min_number int64) error {
  8560. err := XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"stock_max_number": last_stock_max_number, "stock_min_number": last_stock_min_number}).Error
  8561. return err
  8562. }
  8563. func GeteSelfPrescriptionOpen(user_org_id int64) (models.XtSelfDrugInfoConfig, error) {
  8564. drugInfoConfig := models.XtSelfDrugInfoConfig{}
  8565. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugInfoConfig).Error
  8566. return drugInfoConfig, err
  8567. }
  8568. func CreaterSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8569. err := XTWriteDB().Create(&infoConfig).Error
  8570. return err
  8571. }
  8572. func SaveSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8573. err := XTWriteDB().Save(&infoConfig).Error
  8574. return err
  8575. }
  8576. func GetSelfPrescriptionOutOpen(user_org_id int64) (models.XtSelfDrugOutConfig, error) {
  8577. drugOutConfig := models.XtSelfDrugOutConfig{}
  8578. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugOutConfig).Error
  8579. return drugOutConfig, err
  8580. }
  8581. func CreateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8582. err := XTWriteDB().Create(&config).Error
  8583. return err
  8584. }
  8585. func UpdateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8586. err := XTWriteDB().Save(&config).Error
  8587. return err
  8588. }
  8589. func CreateSelfDrugFlowOne(flow models.XtSelfDrugFlow, tx *gorm.DB) error {
  8590. err := tx.Create(&flow).Error
  8591. if err != nil {
  8592. tx.Rollback()
  8593. return err
  8594. }
  8595. return err
  8596. }
  8597. func UpdateWarehouseOutInfoByPrice(good_id int64, packing_price float64) error {
  8598. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("good_id =? and status =1 and is_check=1", good_id).Updates(map[string]interface{}{"packing_price": packing_price}).Error
  8599. return err
  8600. }
  8601. func GetWarehosueOutInfoByPrice(good_id int64, org_id int64) (out []*models.WarehouseOutInfo, err error) {
  8602. err = XTReadDB().Where("good_id = ? and org_id = ? and status=1 and price =0", good_id, org_id).Find(&out).Error
  8603. return out, err
  8604. }
  8605. func UpdateWarehouseOut(good_id int64, packing_price float64) error {
  8606. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  8607. return err
  8608. }
  8609. func UpdateStockFlowByList(good_id int64, packing_price float64) error {
  8610. err := XTWriteDB().Model(models.VmStockFlow{}).Where("good_id = ? and user_org_id = 10633 and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  8611. return err
  8612. }
  8613. func GetGoodStockInventoryByRecordate(good_id int64, record_date int64, user_org_id int64) (models.XtGoodStockInventoryList, error) {
  8614. goodStockInventoryList := models.XtGoodStockInventoryList{}
  8615. err := XTReadDB().Where("good_id = ? and record_date = ? and user_org_id = ? and status=1", good_id, record_date, user_org_id).Find(&goodStockInventoryList).Error
  8616. return goodStockInventoryList, err
  8617. }
  8618. func CreateGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8619. err := XTWriteDB().Create(&list).Error
  8620. return err
  8621. }
  8622. func SaveGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8623. err := XTWriteDB().Save(&list).Error
  8624. return err
  8625. }
  8626. func GetGoodStockInventoryList(keyword string, limit int64, page int64, storehouse_id int64, startTime int64, endTime int64, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8627. likeKey := "%" + keyword + "%"
  8628. offset := (page - 1) * limit
  8629. db := XTReadDB().Model(&models.XtGoodStockInventoryList{}).Where("status=1")
  8630. if storehouse_id > 0 {
  8631. db = db.Where("storehouse_id = ?", storehouse_id)
  8632. }
  8633. if startTime > 0 {
  8634. db = db.Where("record_date >=?", startTime)
  8635. }
  8636. if endTime > 0 {
  8637. db = db.Where("record_date<=?", endTime)
  8638. }
  8639. if len(keyword) > 0 {
  8640. db = db.Where("good_name like ? or specification_name like ?", likeKey, likeKey)
  8641. }
  8642. if orgId > 0 {
  8643. db = db.Where("user_org_id = ?", orgId)
  8644. }
  8645. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8646. return list, total, err
  8647. }
  8648. func GetDrugStockInventoryList(drug_id int64, record_date int64, user_org_id int64) (models.XtDrugStockInventoryList, error) {
  8649. drugStockInventoryList := models.XtDrugStockInventoryList{}
  8650. err := XTReadDB().Where("drug_id = ? and record_date = ? and user_org_id = ? and status=1", drug_id, record_date, user_org_id).Find(&drugStockInventoryList).Error
  8651. return drugStockInventoryList, err
  8652. }
  8653. func CreatedDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8654. err := XTWriteDB().Create(&list).Error
  8655. return err
  8656. }
  8657. func SaveDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8658. err := XTWriteDB().Save(&list).Error
  8659. return err
  8660. }
  8661. func GetStockInventoryPrintList(storehouse_id int64, keyword string, limit int64, page int64, ids []string, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8662. db := XTReadDB().Model(&list).Where("status=1")
  8663. likeKey := "%" + keyword + "%"
  8664. offset := (page - 1) * limit
  8665. if storehouse_id > 0 {
  8666. db = db.Where("storehouse_id = ?", storehouse_id)
  8667. }
  8668. if len(keyword) > 0 {
  8669. db = db.Where("good_name like ?", likeKey)
  8670. }
  8671. if orgId > 0 {
  8672. db = db.Where("user_org_id = ?", orgId)
  8673. }
  8674. if len(ids) > 0 {
  8675. db = db.Where("id in(?)", ids)
  8676. }
  8677. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8678. return list, total, err
  8679. }
  8680. func GetGoodWarehouseInfoList(startTime int64, endTime int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  8681. db := XTReadDB().Model(&info).Where("status=1 and is_check=1")
  8682. if startTime > 0 {
  8683. db = db.Where("ctime>=?", startTime)
  8684. }
  8685. if endTime > 0 {
  8686. db = db.Where("ctime<=?", endTime)
  8687. }
  8688. if orgId > 0 {
  8689. db = db.Where("org_id = ?", orgId)
  8690. }
  8691. err = db.Find(&info).Error
  8692. return info, err
  8693. }
  8694. func GetGoodWarhouseOutList(startTime int64, endTime int64, orgId int64) (outinfo []*models.WarehouseOutInfo, err error) {
  8695. db := XTReadDB().Model(&outinfo).Where("status=1 and is_check =1")
  8696. if startTime > 0 {
  8697. db = db.Where("ctime>=?", startTime)
  8698. }
  8699. if endTime > 0 {
  8700. db = db.Where("ctime<=?", endTime)
  8701. }
  8702. if orgId > 0 {
  8703. db = db.Where("org_id = ?", orgId)
  8704. }
  8705. err = db.Find(&outinfo).Error
  8706. return outinfo, err
  8707. }
  8708. func GetGoodStockFlowProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8709. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 10")
  8710. if startTime > 0 {
  8711. db = db.Where("ctime>=?", startTime)
  8712. }
  8713. if endTime > 0 {
  8714. db = db.Where("ctime<=?", endTime)
  8715. }
  8716. if orgId > 0 {
  8717. db = db.Where("user_org_id = ?", orgId)
  8718. }
  8719. err = db.Find(&flow).Error
  8720. return flow, err
  8721. }
  8722. func GetGoodStockFlowNoProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8723. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 11")
  8724. if startTime > 0 {
  8725. db = db.Where("ctime>=?", startTime)
  8726. }
  8727. if endTime > 0 {
  8728. db = db.Where("ctime<=?", endTime)
  8729. }
  8730. if orgId > 0 {
  8731. db = db.Where("user_org_id = ?", orgId)
  8732. }
  8733. err = db.Find(&flow).Error
  8734. return flow, err
  8735. }
  8736. func GetCancelStockFlowList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8737. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 7")
  8738. if startTime > 0 {
  8739. db = db.Where("ctime>=?", startTime)
  8740. }
  8741. if endTime > 0 {
  8742. db = db.Where("ctime<=?", endTime)
  8743. }
  8744. if orgId > 0 {
  8745. db = db.Where("user_org_id = ?", orgId)
  8746. }
  8747. err = db.Find(&flow).Error
  8748. return flow, err
  8749. }
  8750. func GetLastMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8751. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 1 or consumable_type = 10)")
  8752. if startTime > 0 {
  8753. db = db.Where("ctime <?", startTime)
  8754. }
  8755. if orgId > 0 {
  8756. db = db.Where("user_org_id = ?", orgId)
  8757. }
  8758. err = db.Find(&flow).Error
  8759. return flow, err
  8760. }
  8761. func GetLastMonthOverCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8762. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 2 or consumable_type=3 or consumable_type=11)")
  8763. if startTime > 0 {
  8764. db = db.Where("ctime <?", startTime)
  8765. }
  8766. if orgId > 0 {
  8767. db = db.Where("user_org_id = ?", orgId)
  8768. }
  8769. err = db.Find(&flow).Error
  8770. return flow, err
  8771. }
  8772. func GetLastCancelMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8773. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 7)")
  8774. if startTime > 0 {
  8775. db = db.Where("ctime <?", startTime)
  8776. }
  8777. if orgId > 0 {
  8778. db = db.Where("user_org_id = ?", orgId)
  8779. }
  8780. err = db.Find(&flow).Error
  8781. return flow, err
  8782. }
  8783. func GetDrugCodeOpen(user_org_id int64) (models.XtDrugCodeOpen, error) {
  8784. drugCodeOpen := models.XtDrugCodeOpen{}
  8785. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugCodeOpen).Error
  8786. return drugCodeOpen, err
  8787. }
  8788. func CreateDrugOpen(open models.XtDrugCodeOpen) error {
  8789. err := XTWriteDB().Create(&open).Error
  8790. return err
  8791. }
  8792. func SaveDrugOpen(open models.XtDrugCodeOpen) error {
  8793. err := XTWriteDB().Save(&open).Error
  8794. return err
  8795. }
  8796. func GetHisDrugCodeQuery(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64, is_settle int64, keywords string, is_code int64) (list []*models.NewHisDoctorAdviceInfo, total int64, err error) {
  8797. db := XTReadDB().Model(&models.NewHisDoctorAdviceInfo{}).Preload("NewHisPrescription", "status = 1").Where("his_doctor_advice_info.status=1")
  8798. if is_settle == 2 {
  8799. db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8800. } else if is_settle == 1 {
  8801. db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status <> 2 and his_prescription.status = 1")
  8802. }
  8803. if is_code == 2 {
  8804. //db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8805. db = db.Where("his_doctor_advice_info.drug_code <> ''")
  8806. } else if is_code == 1 {
  8807. db = db.Where("length(his_doctor_advice_info.drug_code) = 0")
  8808. }
  8809. if len(keywords) > 0 {
  8810. keywords = "%" + keywords + "%"
  8811. db = db.Joins("join xt_patients on xt_patients.id = his_doctor_advice_info.patient_id and xt_patients.user_org_id = ? and xt_patients.name like ?", orgId, keywords)
  8812. }
  8813. if orgId > 0 {
  8814. db = db.Where("his_doctor_advice_info.user_org_id=?", orgId)
  8815. }
  8816. if start_time > 0 {
  8817. db = db.Where("his_doctor_advice_info.advice_date >= ?", start_time)
  8818. }
  8819. if end_time > 0 {
  8820. db = db.Where("his_doctor_advice_info.advice_date<=?", end_time)
  8821. }
  8822. offset := (page - 1) * limit
  8823. if is_sale == 1 {
  8824. db = db.Where("his_doctor_advice_info.is_upload = 1")
  8825. }
  8826. if is_sale == 2 {
  8827. db = db.Where("his_doctor_advice_info.is_upload = 2 or his_doctor_advice_info.is_upload =0")
  8828. }
  8829. err = db.Count(&total).Offset(offset).Limit(limit).Order("his_doctor_advice_info.record_date desc").Find(&list).Error
  8830. return list, total, err
  8831. }
  8832. func GetHisGoodCodeQuery(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64, is_settle int64, keywords string, is_code int64) (list []*models.NewHisPrintPrescriptionProject, total int64, err error) {
  8833. db := XTReadDB().Model(&models.NewHisPrintPrescriptionProject{}).Preload("NewHisPrescription", "status = 1").Preload("GoodInfo", "status = 1").Where("his_prescription_project.status=1")
  8834. if is_settle == 2 {
  8835. db = db.Joins("join his_prescription on his_prescription.id = his_prescription_project.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8836. } else if is_settle == 1 {
  8837. db = db.Joins("join his_prescription on his_prescription.id = his_prescription_project.prescription_id and his_prescription.order_status <> 2 and his_prescription.status = 1")
  8838. }
  8839. if is_code == 2 {
  8840. //db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8841. db = db.Where("his_prescription_project.drug_code <> ''")
  8842. } else if is_code == 1 {
  8843. db = db.Where("length(his_prescription_project.drug_code) = 0")
  8844. }
  8845. if len(keywords) > 0 {
  8846. keywords = "%" + keywords + "%"
  8847. db = db.Joins("join xt_patients on xt_patients.id = his_doctor_advice_info.patient_id and xt_patients.user_org_id = ? and xt_patients.name like ?", orgId, keywords)
  8848. }
  8849. if orgId > 0 {
  8850. db = db.Where("his_prescription_project.user_org_id=?", orgId)
  8851. }
  8852. if start_time > 0 {
  8853. db = db.Where("his_prescription_project.record_date >= ?", start_time)
  8854. }
  8855. if end_time > 0 {
  8856. db = db.Where("his_prescription_project.record_date<=?", end_time)
  8857. }
  8858. offset := (page - 1) * limit
  8859. if is_sale == 1 {
  8860. db = db.Where("his_prescription_project.is_upload = 1")
  8861. }
  8862. if is_sale == 2 {
  8863. db = db.Where("his_prescription_project.is_upload = 2 or his_prescription_project.is_upload =0")
  8864. }
  8865. db = db.Where("his_prescription_project.type = 3")
  8866. err = db.Count(&total).Offset(offset).Limit(limit).Order("his_prescription_project.record_date desc").Find(&list).Error
  8867. return list, total, err
  8868. }
  8869. func GetBloodDrugCode(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64) (advice []*models.DoctorAdvice, total int64, err error) {
  8870. db := XTReadDB().Model(&advice).Where("status=1 and advice_type !=1")
  8871. if orgId > 0 {
  8872. db = db.Where("user_org_id = ?", orgId)
  8873. }
  8874. if start_time > 0 {
  8875. db = db.Where("advice_date>=?", start_time)
  8876. }
  8877. if end_time > 0 {
  8878. db = db.Where("advice_date<=?", end_time)
  8879. }
  8880. if is_sale > 0 {
  8881. db = db.Where("is_upload = ?", is_sale)
  8882. }
  8883. offset := (page - 1) * limit
  8884. err = db.Count(&total).Offset(offset).Limit(limit).Order("advice_date desc").Find(&advice).Error
  8885. return advice, total, err
  8886. }
  8887. func GetDoctorAdviceInfoList(page int64, limit int64, orgId int64, start_time int64, end_time int64, is_sale int64) ([]models.HisDoctorAdviceInfo, int64, error) {
  8888. var list []models.HisDoctorAdviceInfo
  8889. var total int64
  8890. // 计算偏移量
  8891. offset := (page - 1) * limit
  8892. // 初始化数据库查询
  8893. db := XTReadDB().Model(&models.HisDoctorAdviceInfo{})
  8894. // 构建 WHERE 子句
  8895. query := db.Where("status=1")
  8896. if orgId > 0 {
  8897. query = query.Where("user_org_id=?", orgId)
  8898. }
  8899. if start_time > 0 {
  8900. query = query.Where("advice_date >= ?", start_time)
  8901. }
  8902. if end_time > 0 {
  8903. query = query.Where("advice_date <= ?", end_time)
  8904. }
  8905. if is_sale == 1 {
  8906. query = query.Where("is_upload = 1")
  8907. }
  8908. if is_sale == 2 {
  8909. query = query.Where("is_upload IN (?, ?)", 2, 0) // 使用 IN 替代 OR,通常性能更好
  8910. }
  8911. // 获取总记录数
  8912. err := query.Count(&total).Error
  8913. if err != nil {
  8914. return nil, 0, err
  8915. }
  8916. // 获取分页后的数据
  8917. err = query.Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8918. if err != nil {
  8919. return nil, 0, err
  8920. }
  8921. return list, total, nil
  8922. }
  8923. func GetDrugWarehouseOutInfoByDrugCode(drug_id int64, patient_id int64, advice_date int64, user_org_id int64) (outinfo []*models.XtDrugWarehouseOutInfo, err error) {
  8924. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time =? and org_id = ? and status =1", drug_id, patient_id, advice_date, user_org_id).Find(&outinfo).Error
  8925. return outinfo, err
  8926. }
  8927. func GetNewGoodWarehouseOutInfo(good_id int64, user_org_id int64, warehouse_out_id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  8928. err = tx.Where("good_id = ? and org_id = ? and warehouse_out_id = ? and status= 1", good_id, user_org_id, warehouse_out_id).Find(&info).Error
  8929. if err != gorm.ErrRecordNotFound {
  8930. if err != nil {
  8931. tx.Rollback()
  8932. return info, err
  8933. }
  8934. }
  8935. return info, err
  8936. }
  8937. func GetInitGoodFlow(user_org_id int64, good_id int64, warehousing_detail_id int64, tx *gorm.DB) (list []*models.VmStockFlow, err error) {
  8938. err = tx.Where("user_org_id = ? and good_id = ? and warehousing_detail_id = ? and status=1", user_org_id, good_id, warehousing_detail_id).Find(&list).Error
  8939. if err != gorm.ErrRecordNotFound {
  8940. if err != nil {
  8941. tx.Rollback()
  8942. return list, err
  8943. }
  8944. }
  8945. return list, err
  8946. }
  8947. func GetNewDrugFlowList(drug_id int64, user_org_id int64, warehousing_detail_id int64, tx *gorm.DB) (flow []*models.DrugFlow, err error) {
  8948. err = tx.Where("drug_id = ? and user_org_id =? and warehousing_detail_id = ? and status=1", drug_id, user_org_id, warehousing_detail_id).Find(&flow).Error
  8949. if err != gorm.ErrRecordNotFound {
  8950. if err != nil {
  8951. tx.Rollback()
  8952. return flow, err
  8953. }
  8954. }
  8955. return flow, err
  8956. }
  8957. func GetNewPatientAdviceList(patient_id int64, start_time int64, end_time int64, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  8958. db := XTReadDB().Model(&advice).Where("status=1 and advice_type =2")
  8959. if patient_id > 0 {
  8960. db = db.Where("patient_id = ?", patient_id)
  8961. }
  8962. if start_time > 0 {
  8963. db = db.Where("advice_date>=?", start_time)
  8964. }
  8965. if end_time > 0 {
  8966. db = db.Where("advice_date<=?", end_time)
  8967. }
  8968. if user_org_id > 0 {
  8969. db = db.Where("user_org_id = ?", user_org_id)
  8970. }
  8971. err = db.Order("drug_id desc").Find(&advice).Error
  8972. return advice, err
  8973. }
  8974. func GetSelfDrugWarehouseInfoByPatientId(patient_id int64, user_org_id int64) (drugInfo []*models.XtSelfDrugWarehouseInfo, err error) {
  8975. err = XTReadDB().Where("patient_id = ? and org_id = ? and status =1 and is_check =1", patient_id, user_org_id).Find(&drugInfo).Error
  8976. return drugInfo, err
  8977. }
  8978. func GetPatientAdviceListDetail(patient_id int64, start_time int64, end_time int64, user_org_id int64, advice_name string) (advice []*models.DoctorAdvice, err error) {
  8979. db := XTReadDB().Model(&advice).Where("status = 1")
  8980. if patient_id > 0 {
  8981. db = db.Where("patient_id = ?", patient_id)
  8982. }
  8983. if start_time > 0 {
  8984. db = db.Where("advice_date>=?", start_time)
  8985. }
  8986. if end_time > 0 {
  8987. db = db.Where("advice_date<=?", end_time)
  8988. }
  8989. if user_org_id > 0 {
  8990. db = db.Where("user_org_id = ?", user_org_id)
  8991. }
  8992. if len(advice_name) > 0 {
  8993. db = db.Where("advice_name =?", advice_name)
  8994. }
  8995. err = db.Order("advice_date asc").Find(&advice).Error
  8996. return advice, err
  8997. }
  8998. func GetGoodWarehosueInfoPrintList(good_id int64, start_time int64, end_time int64, user_org_id int64) (info []*models.PrintWarehousingInfo, err error) {
  8999. db := XTReadDB().Model(&info).Where("status=1")
  9000. if good_id > 0 {
  9001. db = db.Where("good_id = ?", good_id)
  9002. }
  9003. if start_time > 0 {
  9004. db = db.Where("ctime>=?", start_time)
  9005. }
  9006. if end_time > 0 {
  9007. db = db.Where("ctime<=?", end_time)
  9008. }
  9009. if user_org_id > 0 {
  9010. db = db.Where("org_id = ?", user_org_id)
  9011. }
  9012. err = db.Find(&info).Error
  9013. return info, err
  9014. }
  9015. func GetDrugWarehouseInfoPrintList(drug_id int64, user_org_id int64, start_time int64, end_time int64) (drugInfo []*models.PrintDrugWarehouseInfo, err error) {
  9016. db := XTReadDB().Model(&drug_id).Where("status=1")
  9017. if drug_id > 0 {
  9018. db = db.Where("drug_id = ?", drug_id)
  9019. }
  9020. if start_time > 0 {
  9021. db = db.Where("ctime>=?", start_time)
  9022. }
  9023. if end_time > 0 {
  9024. db = db.Where("ctime<=?", end_time)
  9025. }
  9026. if user_org_id > 0 {
  9027. db = db.Where("org_id = ?", user_org_id)
  9028. }
  9029. err = db.Find(&drugInfo).Error
  9030. return drugInfo, err
  9031. }