stock_service.go 292KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833
  1. package service
  2. import (
  3. "XT_New/models"
  4. "errors"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "strconv"
  8. "strings"
  9. "time"
  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. }).Error
  243. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  244. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  245. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  246. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  247. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  248. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  249. return err, goodInfo
  250. }
  251. 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) {
  252. offset := (page - 1) * limit
  253. db := readDb.Model(&models.GoodInfo{})
  254. db = db.Where("org_id = ? AND status = 1", orgId)
  255. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  256. if len(keyword) > 0 {
  257. likeKey := "%" + keyword + "%"
  258. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  259. if is_use > 0 {
  260. if is_use == 2 {
  261. db = db.Where("find_in_set('停用',good_status) = 0")
  262. } else {
  263. db = db.Where("find_in_set('停用',good_status) > 0")
  264. }
  265. }
  266. if is_charge > 0 {
  267. if is_charge == 1 {
  268. db = db.Where("find_in_set('收费',good_status) > 0")
  269. } else {
  270. db = db.Where("find_in_set('收费',good_status) = 0")
  271. }
  272. }
  273. if good_kind > 0 {
  274. db = db.Where(" good_kind = ?", good_kind)
  275. }
  276. if is_mark > 0 {
  277. db = db.Where("is_mark = ?", is_mark)
  278. }
  279. if manufacturer > 0 {
  280. db = db.Where("manufacturer = ?", manufacturer)
  281. }
  282. if is_type > 0 {
  283. db = db.Where("good_type_id = ?", is_type)
  284. }
  285. } else {
  286. if is_use > 0 {
  287. if is_use == 2 {
  288. db = db.Where("find_in_set('停用',good_status) = 0")
  289. } else {
  290. db = db.Where("find_in_set('停用',good_status) > 0")
  291. }
  292. }
  293. if is_charge > 0 {
  294. if is_charge == 1 {
  295. db = db.Where("find_in_set('收费',good_status) > 0")
  296. } else {
  297. db = db.Where("find_in_set('收费',good_status) = 0")
  298. }
  299. }
  300. if good_kind > 0 {
  301. db = db.Where(" good_kind = ?", good_kind)
  302. }
  303. if is_mark > 0 {
  304. db = db.Where("is_mark = ?", is_mark)
  305. }
  306. if manufacturer > 0 {
  307. db = db.Where("manufacturer = ?", manufacturer)
  308. }
  309. if is_type > 0 {
  310. db = db.Where("good_type_id = ?", is_type)
  311. }
  312. }
  313. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  314. return
  315. }
  316. func DeleteGoodInfoById(id int64, operateId int64) error {
  317. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  318. return err
  319. }
  320. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  321. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  322. return list, err
  323. }
  324. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  325. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  326. return list, err
  327. }
  328. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  329. goodInfo := &models.GoodInfo{}
  330. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  331. return goodInfo, err
  332. }
  333. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  334. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  335. return goodType, err
  336. }
  337. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  338. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  339. return goodType, err
  340. }
  341. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  342. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 and is_warehouse = 1", org_id).Find(&goodInfo).Error
  343. return goodInfo, err
  344. }
  345. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  346. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  347. return goodInfo, err
  348. }
  349. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  350. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  351. return total, err
  352. }
  353. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  354. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  355. return total, err
  356. }
  357. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  358. err := writeDb.Create(&warehouse).Error
  359. return err
  360. }
  361. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  362. err := writeDb.Create(&warehouse).Error
  363. return err
  364. }
  365. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  366. err := writeDb.Create(&salesReturn).Error
  367. return err
  368. }
  369. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  370. err := writeDb.Create(&salesReturn).Error
  371. return err
  372. }
  373. func UpDateGoodReturnStatus(ids []string) {
  374. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  375. }
  376. func UpDateCanCelStockStatus(ids []string) {
  377. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  378. }
  379. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  380. if len(warehousingInfo) > 0 {
  381. utx := writeDb.Begin()
  382. if len(warehousingInfo) > 0 {
  383. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number,packing_price,storehouse_id) VALUES "
  384. insertParams := make([]string, 0)
  385. insertData := make([]interface{}, 0)
  386. for _, info := range warehousingInfo {
  387. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  388. insertData = append(insertData, info.WarehousingId)
  389. insertData = append(insertData, info.GoodId)
  390. insertData = append(insertData, info.GoodTypeId)
  391. insertData = append(insertData, info.Number)
  392. insertData = append(insertData, info.ProductDate)
  393. insertData = append(insertData, info.ExpiryDate)
  394. insertData = append(insertData, info.WarehousingCount)
  395. insertData = append(insertData, info.Price)
  396. insertData = append(insertData, info.TotalPrice)
  397. insertData = append(insertData, info.Dealer)
  398. insertData = append(insertData, info.Manufacturer)
  399. insertData = append(insertData, info.Remark)
  400. insertData = append(insertData, info.Ctime)
  401. insertData = append(insertData, info.Mtime)
  402. insertData = append(insertData, info.Status)
  403. insertData = append(insertData, info.OrgId)
  404. insertData = append(insertData, info.WarehousingOrder)
  405. insertData = append(insertData, info.Type)
  406. insertData = append(insertData, info.WarehousingCount)
  407. insertData = append(insertData, info.LicenseNumber)
  408. insertData = append(insertData, info.PackingPrice)
  409. insertData = append(insertData, info.StorehouseId)
  410. }
  411. thisSQL += strings.Join(insertParams, ", ")
  412. err = utx.Exec(thisSQL, insertData...).Error
  413. if err != nil {
  414. utx.Rollback()
  415. return
  416. }
  417. }
  418. utx.Commit()
  419. }
  420. return
  421. }
  422. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  423. if len(warehousingInfo) > 0 {
  424. utx := writeDb.Begin()
  425. if len(warehousingInfo) > 0 {
  426. 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 "
  427. insertParams := make([]string, 0)
  428. insertData := make([]interface{}, 0)
  429. for _, info := range warehousingInfo {
  430. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  431. insertData = append(insertData, info.WarehousingId)
  432. insertData = append(insertData, info.DrugId)
  433. insertData = append(insertData, info.Number)
  434. insertData = append(insertData, info.ProductDate)
  435. insertData = append(insertData, info.ExpiryDate)
  436. insertData = append(insertData, info.WarehousingCount)
  437. insertData = append(insertData, info.Price)
  438. insertData = append(insertData, info.TotalPrice)
  439. insertData = append(insertData, info.Dealer)
  440. insertData = append(insertData, info.Manufacturer)
  441. insertData = append(insertData, info.Remark)
  442. insertData = append(insertData, info.Ctime)
  443. insertData = append(insertData, info.Mtime)
  444. insertData = append(insertData, info.Status)
  445. insertData = append(insertData, info.OrgId)
  446. insertData = append(insertData, info.WarehousingOrder)
  447. insertData = append(insertData, info.Type)
  448. insertData = append(insertData, info.RetailPrice)
  449. insertData = append(insertData, info.RetailTotalPrice)
  450. insertData = append(insertData, info.StockMaxNumber)
  451. insertData = append(insertData, info.MaxUnit)
  452. insertData = append(insertData, info.MinUnit)
  453. insertData = append(insertData, info.BatchNumber)
  454. insertData = append(insertData, info.StockMinNumber)
  455. insertData = append(insertData, info.StorehouseId)
  456. }
  457. thisSQL += strings.Join(insertParams, ", ")
  458. err = utx.Exec(thisSQL, insertData...).Error
  459. if err != nil {
  460. utx.Rollback()
  461. return
  462. }
  463. }
  464. utx.Commit()
  465. }
  466. return
  467. }
  468. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  469. if len(warehousingInfo) > 0 {
  470. utx := writeDb.Begin()
  471. if len(warehousingInfo) > 0 {
  472. 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) VALUES "
  473. insertParams := make([]string, 0)
  474. insertData := make([]interface{}, 0)
  475. for _, info := range warehousingInfo {
  476. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  477. insertData = append(insertData, info.WarehousingId)
  478. insertData = append(insertData, info.DrugId)
  479. insertData = append(insertData, info.Number)
  480. insertData = append(insertData, info.ProductDate)
  481. insertData = append(insertData, info.ExpiryDate)
  482. insertData = append(insertData, info.WarehousingCount)
  483. insertData = append(insertData, info.Price)
  484. insertData = append(insertData, info.TotalPrice)
  485. insertData = append(insertData, info.Dealer)
  486. insertData = append(insertData, info.Manufacturer)
  487. insertData = append(insertData, info.Remark)
  488. insertData = append(insertData, info.Ctime)
  489. insertData = append(insertData, info.Mtime)
  490. insertData = append(insertData, info.Status)
  491. insertData = append(insertData, info.OrgId)
  492. insertData = append(insertData, info.WarehousingOrder)
  493. insertData = append(insertData, info.Type)
  494. insertData = append(insertData, info.RetailPrice)
  495. insertData = append(insertData, info.RetailTotalPrice)
  496. insertData = append(insertData, info.StockMaxNumber)
  497. insertData = append(insertData, info.MaxUnit)
  498. insertData = append(insertData, info.MinUnit)
  499. insertData = append(insertData, info.BatchNumber)
  500. insertData = append(insertData, info.StockMinNumber)
  501. insertData = append(insertData, info.SupplyWarehouseId)
  502. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  503. insertData = append(insertData, info.StorehouseId)
  504. }
  505. thisSQL += strings.Join(insertParams, ", ")
  506. err = utx.Exec(thisSQL, insertData...).Error
  507. if err != nil {
  508. utx.Rollback()
  509. return
  510. }
  511. }
  512. utx.Commit()
  513. }
  514. return
  515. }
  516. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  517. var total float64
  518. var min_number int64
  519. for _, item := range warehousingInfo {
  520. //查询刚入库耗材的库存
  521. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  522. //更新药品库的总量
  523. info, _ := FindeDrugInfo(item.DrugId)
  524. min_number = count.StockMaxNumber * info.MinNumber
  525. str := strconv.FormatInt(min_number, 10)
  526. minFloat, _ := strconv.ParseFloat(str, 64)
  527. total = info.Total + minFloat
  528. lib := models.BaseDrugLib{
  529. Total: total,
  530. }
  531. UpdateBaseDrug(&lib, item.ID)
  532. }
  533. return err
  534. }
  535. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  536. info := models.XtDrugWarehouseInfo{}
  537. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  538. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  539. return info, err
  540. }
  541. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  542. lib := models.BaseDrugLib{}
  543. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  544. return lib, err
  545. }
  546. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  547. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  548. return err
  549. }
  550. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  551. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  552. return err
  553. }
  554. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  555. info := models.DrugWarehouseInfo{}
  556. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  557. return info, err
  558. }
  559. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  560. out := models.DrugWarehouseOut{}
  561. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  562. return out, err
  563. }
  564. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  565. if len(drugflow) > 0 {
  566. utx := writeDb.Begin()
  567. if len(drugflow) > 0 {
  568. 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 "
  569. insertParams := make([]string, 0)
  570. insertData := make([]interface{}, 0)
  571. for _, info := range drugflow {
  572. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  573. insertData = append(insertData, info.WarehousingId)
  574. insertData = append(insertData, info.DrugId)
  575. insertData = append(insertData, info.Number)
  576. insertData = append(insertData, info.BatchNumber)
  577. insertData = append(insertData, info.Count)
  578. insertData = append(insertData, info.UserOrgId)
  579. insertData = append(insertData, info.PatientId)
  580. insertData = append(insertData, info.SystemTime)
  581. insertData = append(insertData, info.ConsumableType)
  582. insertData = append(insertData, info.IsSys)
  583. insertData = append(insertData, info.WarehousingOrder)
  584. insertData = append(insertData, info.WarehouseOutId)
  585. insertData = append(insertData, info.WarehouseOutOrderNumber)
  586. insertData = append(insertData, info.IsEdit)
  587. insertData = append(insertData, info.CancelStockId)
  588. insertData = append(insertData, info.CancelOrderNumber)
  589. insertData = append(insertData, info.Manufacturer)
  590. insertData = append(insertData, info.Dealer)
  591. insertData = append(insertData, info.Creator)
  592. insertData = append(insertData, info.UpdateCreator)
  593. insertData = append(insertData, info.Status)
  594. insertData = append(insertData, info.Ctime)
  595. insertData = append(insertData, info.Mtime)
  596. insertData = append(insertData, info.Price)
  597. insertData = append(insertData, info.WarehousingDetailId)
  598. insertData = append(insertData, info.WarehouseOutDetailId)
  599. insertData = append(insertData, info.CancelOutDetailId)
  600. insertData = append(insertData, info.ExpireDate)
  601. insertData = append(insertData, info.ProductDate)
  602. insertData = append(insertData, info.MaxUnit)
  603. insertData = append(insertData, info.MinUnit)
  604. insertData = append(insertData, info.StorehouseId)
  605. }
  606. thisSQL += strings.Join(insertParams, ", ")
  607. err = utx.Exec(thisSQL, insertData...).Error
  608. if err != nil {
  609. utx.Rollback()
  610. return
  611. }
  612. }
  613. utx.Commit()
  614. }
  615. return
  616. }
  617. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  618. info := models.DrugFlow{}
  619. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  620. if err == gorm.ErrRecordNotFound {
  621. return nil, err
  622. }
  623. if err != nil {
  624. return nil, err
  625. }
  626. return &info, nil
  627. }
  628. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  629. info := models.XtDrugWarehouseInfo{}
  630. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  631. return info, err
  632. }
  633. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  634. warehouseInfo := models.XtDrugWarehouseInfo{}
  635. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  636. return err
  637. }
  638. func CreateDrugFlowOne(flow models.DrugFlow) error {
  639. err := XTWriteDB().Create(&flow).Error
  640. return err
  641. }
  642. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  643. detail := models.DrugAutomaticReduceDetail{}
  644. 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
  645. 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
  646. return detail, err
  647. }
  648. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  649. flow := models.DrugFlow{}
  650. 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{}{"status": 0}).Error
  651. return flow, err
  652. }
  653. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  654. err := XTWriteDB().Create(&flow).Error
  655. return err
  656. }
  657. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  658. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  659. return info, err
  660. }
  661. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  662. info := models.WarehousingInfo{}
  663. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  664. return info, err
  665. }
  666. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  667. err := XTWriteDB().Create(&info).Error
  668. return err
  669. }
  670. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  671. if storehouse_id == 0 {
  672. 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
  673. }
  674. if storehouse_id > 0 {
  675. 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
  676. }
  677. return info, err
  678. }
  679. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  680. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  681. if good_id > 0 {
  682. db = db.Where("x.good_id = ?", good_id)
  683. }
  684. if good_type_id > 0 {
  685. db = db.Where("x.good_type_id = ?", good_type_id)
  686. }
  687. if storehouse_id > 0 {
  688. db = db.Where("x.storehouse_id = ?", storehouse_id)
  689. }
  690. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  691. return info, err
  692. }
  693. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  694. info := models.WarehousingInfo{}
  695. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  696. return info, err
  697. }
  698. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  699. info := models.WarehousingInfo{}
  700. var err error
  701. if storehouse_id == 0 {
  702. err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1", goodId).Find(&info).Error
  703. }
  704. if storehouse_id > 0 {
  705. err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and storehouse_id = ?", goodId, storehouse_id).Find(&info).Error
  706. }
  707. return info, err
  708. }
  709. //药品先进先出,找出最先入库的批次
  710. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  711. 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
  712. return info, err
  713. }
  714. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  715. if is_source == 1 {
  716. 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
  717. }
  718. if is_source == 2 || is_source == 0 {
  719. 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
  720. }
  721. return info, err
  722. }
  723. // 出库时先进先出,找出最先入库的批次
  724. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  725. 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
  726. return info, err
  727. }
  728. //
  729. //// 出库时先进先出,找出最先入库的批次
  730. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  731. // 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
  732. // return info, err
  733. //}
  734. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  735. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  736. return info, err
  737. }
  738. func FindAllWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64) (list []*models.Warehousing, total int64, err error) {
  739. db := readDb.Model(&models.Warehousing{})
  740. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  741. if len(keywords) > 0 {
  742. likeKey := "%" + keywords + "%"
  743. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  744. 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")
  745. }
  746. if startTime > 0 {
  747. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  748. }
  749. if endTime > 0 {
  750. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  751. }
  752. if storehouse_id > 0 {
  753. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  754. }
  755. db = db.Count(&total)
  756. offset := (page - 1) * limit
  757. err = db.Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  758. return
  759. }
  760. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  761. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  762. if orgid > 0 {
  763. db = db.Where("org_id = ?", orgid)
  764. }
  765. if len(keyword) > 0 {
  766. likeKey := "%" + keyword + "%"
  767. db = db.Where("good_name like ?", likeKey)
  768. }
  769. err = db.Find(&goodInfo).Error
  770. return goodInfo, err
  771. }
  772. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  773. db := XTReadDB().Model(&info).Where("status =1")
  774. if len(good_id) > 0 {
  775. db = db.Where("good_id in (?)", good_id)
  776. }
  777. if startime > 0 {
  778. db = db.Where("ctime >=?", startime)
  779. }
  780. if endtime > 0 {
  781. db = db.Where("ctime<=?", endtime)
  782. }
  783. if orgid > 0 {
  784. db = db.Where("org_id = ?", orgid)
  785. }
  786. if storehouse_id > 0 {
  787. db = db.Where("storehouse_id = ?", storehouse_id)
  788. }
  789. err = db.Find(&info).Error
  790. return info, err
  791. }
  792. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  793. db := XTReadDB().Model(&info).Where("status =1")
  794. if len(good_id) > 0 {
  795. db = db.Where("good_id in (?)", good_id)
  796. }
  797. if startime > 0 {
  798. db = db.Where("ctime >=?", startime)
  799. }
  800. if endtime > 0 {
  801. db = db.Where("ctime<=?", endtime)
  802. }
  803. if orgid > 0 {
  804. db = db.Where("org_id = ?", orgid)
  805. }
  806. if storehouse_id > 0 {
  807. db = db.Where("storehouse_id = ?", storehouse_id)
  808. }
  809. err = db.Find(&info).Error
  810. return info, err
  811. }
  812. type VMDrugWarehouse struct {
  813. ID int64 `gorm:"column:id" json:"id" form:"id"`
  814. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  815. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  816. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  817. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  818. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  819. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  820. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  821. Status int64 `gorm:"column:status" json:"status" form:"status"`
  822. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  823. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  824. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  825. Type int64 `gorm:"column:type" json:"type" form:"type"`
  826. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  827. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  828. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  829. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  830. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  831. }
  832. func (VMDrugWarehouse) TableName() string {
  833. return "xt_drug_warehouse"
  834. }
  835. type VMDrugWarehouseInfo struct {
  836. ID int64 `gorm:"column:id" json:"id" form:"id"`
  837. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  838. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  839. Number string `gorm:"column:number" json:"number" form:"number"`
  840. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  841. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  842. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  843. Price float64 `gorm:"column:price" json:"price" form:"price"`
  844. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  845. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  846. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  847. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  848. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  849. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  850. Status int64 `gorm:"column:status" json:"status" form:"status"`
  851. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  852. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  853. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  854. Type int64 `gorm:"column:type" json:"type" form:"type"`
  855. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  856. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  857. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  858. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  859. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  860. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  861. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  862. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  863. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  864. }
  865. func (VMDrugWarehouseInfo) TableName() string {
  866. return "xt_drug_warehouse_info"
  867. }
  868. type Drug struct {
  869. ID int64 `gorm:"column:id" json:"id" form:"id"`
  870. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  871. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  872. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  873. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  874. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  875. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  876. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  877. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  878. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  879. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  880. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  881. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  882. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  883. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  884. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  885. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  886. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  887. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  888. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  889. Number string `gorm:"column:number" json:"number" form:"number"`
  890. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  891. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  892. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  893. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  894. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  895. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  896. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  897. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  898. Code string `gorm:"column:code" json:"code" form:"code"`
  899. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  900. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  901. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  902. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  903. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  904. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  905. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  906. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  907. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  908. Label int64 `gorm:"column:label" json:"label" form:"label"`
  909. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  910. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  911. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  912. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  913. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  914. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  915. Status int64 `gorm:"column:status" json:"status" form:"status"`
  916. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  917. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  918. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  919. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  920. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  921. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  922. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  923. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  924. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  925. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  926. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  927. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  928. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  929. Total float64 `gorm:"column:total" json:"total" form:"total"`
  930. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  931. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  932. Dose float64 `gorm:"column:dose" json:"dose" form:"dose"`
  933. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  934. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  935. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  936. }
  937. func (Drug) TableName() string {
  938. return "xt_base_drug"
  939. }
  940. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  941. db := readDb.Model(&VMDrugWarehouse{})
  942. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  943. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  944. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  945. if len(keywords) > 0 {
  946. likeKey := "%" + keywords + "%"
  947. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  948. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  949. 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")
  950. }
  951. if startTime > 0 {
  952. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  953. }
  954. if endTime > 0 {
  955. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  956. }
  957. db = db.Count(&total)
  958. offset := (page - 1) * limit
  959. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  960. return
  961. }
  962. func FindAllDrugWarehousingListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64) (list []*VMDrugWarehouse, total int64, err error) {
  963. db := readDb.Model(&VMDrugWarehouse{})
  964. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  965. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  966. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  967. if len(keywords) > 0 {
  968. likeKey := "%" + keywords + "%"
  969. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  970. 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")
  971. }
  972. if startTime > 0 {
  973. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  974. }
  975. if endTime > 0 {
  976. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  977. }
  978. if storehouse_id > 0 {
  979. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  980. }
  981. db = db.Count(&total)
  982. offset := (page - 1) * limit
  983. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  984. return
  985. }
  986. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  987. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  988. return list, err
  989. }
  990. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  991. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  992. return list, err
  993. }
  994. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  995. 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
  996. return list, err
  997. }
  998. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  999. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1000. return list, err
  1001. }
  1002. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1003. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1004. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1005. fmt.Println(table)
  1006. if len(id) > 0 {
  1007. db = db.Where("x.warehousing_id in(?)", id)
  1008. }
  1009. if org_id > 0 {
  1010. db = db.Where("x.org_id = ?", org_id)
  1011. }
  1012. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.stock_max_number,x.stock_min_number,x.price,x.remark,x.batch_number,x.batch_number,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
  1013. return list, err
  1014. }
  1015. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1016. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1017. return warehousing, err
  1018. }
  1019. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1020. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1021. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1022. return warehousing, err
  1023. }
  1024. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1025. 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
  1026. return warehousing, err
  1027. }
  1028. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1029. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1030. if len(id) > 0 {
  1031. db = db.Where("x.id in(?)", id)
  1032. }
  1033. if org_id > 0 {
  1034. db = db.Where("x.org_id = ?", org_id)
  1035. }
  1036. 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
  1037. return warehousing, err
  1038. }
  1039. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1040. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1041. // return warehousing, err
  1042. //
  1043. //}
  1044. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1045. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1046. return
  1047. }
  1048. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1049. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1050. return
  1051. }
  1052. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1053. if len(salesReturnInfo) > 0 {
  1054. utx := writeDb.Begin()
  1055. if len(salesReturnInfo) > 0 {
  1056. 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 "
  1057. insertParams := make([]string, 0)
  1058. insertData := make([]interface{}, 0)
  1059. for _, info := range salesReturnInfo {
  1060. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1061. insertData = append(insertData, info.GoodId)
  1062. insertData = append(insertData, info.SalesReturnId)
  1063. insertData = append(insertData, info.GoodTypeId)
  1064. insertData = append(insertData, info.Count)
  1065. insertData = append(insertData, info.Price)
  1066. insertData = append(insertData, info.Total)
  1067. insertData = append(insertData, info.Ctime)
  1068. insertData = append(insertData, info.Status)
  1069. insertData = append(insertData, info.OrgId)
  1070. insertData = append(insertData, info.OrderNumber)
  1071. insertData = append(insertData, info.Type)
  1072. insertData = append(insertData, info.Dealer)
  1073. insertData = append(insertData, info.Manufacturer)
  1074. }
  1075. thisSQL += strings.Join(insertParams, ", ")
  1076. err = utx.Exec(thisSQL, insertData...).Error
  1077. if err != nil {
  1078. utx.Rollback()
  1079. return
  1080. }
  1081. }
  1082. utx.Commit()
  1083. }
  1084. return
  1085. }
  1086. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1087. if len(salesReturnInfo) > 0 {
  1088. utx := writeDb.Begin()
  1089. if len(salesReturnInfo) > 0 {
  1090. 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 "
  1091. insertParams := make([]string, 0)
  1092. insertData := make([]interface{}, 0)
  1093. for _, info := range salesReturnInfo {
  1094. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1095. insertData = append(insertData, info.DrugId)
  1096. insertData = append(insertData, info.SalesReturnId)
  1097. insertData = append(insertData, info.Count)
  1098. insertData = append(insertData, info.Price)
  1099. insertData = append(insertData, info.Total)
  1100. insertData = append(insertData, info.Ctime)
  1101. insertData = append(insertData, info.Status)
  1102. insertData = append(insertData, info.OrgId)
  1103. insertData = append(insertData, info.OrderNumber)
  1104. insertData = append(insertData, info.Type)
  1105. insertData = append(insertData, info.Dealer)
  1106. insertData = append(insertData, info.Manufacturer)
  1107. insertData = append(insertData, info.RetailPrice)
  1108. insertData = append(insertData, info.RetailTotalPrice)
  1109. }
  1110. thisSQL += strings.Join(insertParams, ", ")
  1111. err = utx.Exec(thisSQL, insertData...).Error
  1112. if err != nil {
  1113. utx.Rollback()
  1114. return
  1115. }
  1116. }
  1117. utx.Commit()
  1118. }
  1119. return
  1120. }
  1121. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1122. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1123. return total, err
  1124. }
  1125. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1126. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1127. return total, err
  1128. }
  1129. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1130. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1131. return total, err
  1132. }
  1133. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1134. db := readDb.Model(&models.SalesReturn{})
  1135. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1136. if len(keywords) > 0 {
  1137. likeKey := "%" + keywords + "%"
  1138. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1139. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1140. 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")
  1141. }
  1142. if startTime > 0 {
  1143. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1144. }
  1145. if endTime > 0 {
  1146. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1147. }
  1148. db = db.Count(&total)
  1149. offset := (page - 1) * limit
  1150. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1151. return
  1152. }
  1153. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1154. db := readDb.Model(&models.DrugSalesReturn{})
  1155. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1156. if len(keywords) > 0 {
  1157. likeKey := "%" + keywords + "%"
  1158. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1159. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1160. 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")
  1161. }
  1162. if startTime > 0 {
  1163. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1164. }
  1165. if endTime > 0 {
  1166. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1167. }
  1168. db = db.Count(&total)
  1169. offset := (page - 1) * limit
  1170. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1171. return
  1172. }
  1173. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1174. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1175. return list, err
  1176. }
  1177. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1178. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1179. return list, err
  1180. }
  1181. type DrugCancelStockInfo struct {
  1182. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1183. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1184. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1185. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1186. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1187. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1188. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1189. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1190. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1191. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1192. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1193. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1194. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1195. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1196. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1197. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1198. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1199. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1200. Number string `gorm:"column:number" json:"number" form:"number"`
  1201. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1202. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1203. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1204. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1205. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1206. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1207. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1208. }
  1209. func (DrugCancelStockInfo) TableName() string {
  1210. return "xt_drug_cancel_stock_info"
  1211. }
  1212. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1213. err = readDb.Model(&DrugCancelStockInfo{}).Preload("Drug", "status = 1 AND org_id = ? ", org_id).Where("cancel_stock_id = ? AND status = 1", id).Find(&list).Error
  1214. return list, err
  1215. }
  1216. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1217. db := readDb.Model(&models.WarehousingInfo{})
  1218. db = db.Where("org_id = ? AND status = 1", orgId)
  1219. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1220. if startTime > 0 {
  1221. db = db.Where("ctime >=?", startTime)
  1222. }
  1223. if endTime > 0 {
  1224. db = db.Where("ctime<= ?", endTime)
  1225. }
  1226. db = db.Count(&total)
  1227. offset := (page - 1) * limit
  1228. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1229. return list, total, err
  1230. }
  1231. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1232. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1233. return total, err
  1234. }
  1235. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1236. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1237. return total, err
  1238. }
  1239. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut) error {
  1240. err := writeDb.Create(&warehouseOut).Error
  1241. return err
  1242. }
  1243. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1244. err := writeDb.Create(&warehouseOut).Error
  1245. return err
  1246. }
  1247. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1248. if len(warehouseOutInfo) > 0 {
  1249. utx := writeDb.Begin()
  1250. if len(warehouseOutInfo) > 0 {
  1251. 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 "
  1252. insertParams := make([]string, 0)
  1253. insertData := make([]interface{}, 0)
  1254. for _, info := range warehouseOutInfo {
  1255. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1256. insertData = append(insertData, info.WarehouseOutId)
  1257. insertData = append(insertData, info.GoodId)
  1258. insertData = append(insertData, info.GoodTypeId)
  1259. insertData = append(insertData, info.ProductDate)
  1260. insertData = append(insertData, info.ExpiryDate)
  1261. insertData = append(insertData, info.Count)
  1262. insertData = append(insertData, info.Price)
  1263. insertData = append(insertData, info.TotalPrice)
  1264. insertData = append(insertData, info.Remark)
  1265. insertData = append(insertData, info.Ctime)
  1266. insertData = append(insertData, info.Status)
  1267. insertData = append(insertData, info.OrgId)
  1268. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1269. insertData = append(insertData, info.Type)
  1270. insertData = append(insertData, info.Dealer)
  1271. insertData = append(insertData, info.Manufacturer)
  1272. insertData = append(insertData, info.Number)
  1273. insertData = append(insertData, info.LicenseNumber)
  1274. }
  1275. thisSQL += strings.Join(insertParams, ", ")
  1276. err = utx.Exec(thisSQL, insertData...).Error
  1277. if err != nil {
  1278. utx.Rollback()
  1279. return
  1280. }
  1281. }
  1282. utx.Commit()
  1283. }
  1284. return
  1285. }
  1286. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1287. if len(warehouseOutInfo) > 0 {
  1288. utx := writeDb.Begin()
  1289. if len(warehouseOutInfo) > 0 {
  1290. 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 "
  1291. insertParams := make([]string, 0)
  1292. insertData := make([]interface{}, 0)
  1293. for _, info := range warehouseOutInfo {
  1294. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1295. insertData = append(insertData, info.WarehouseOutId)
  1296. insertData = append(insertData, info.DrugId)
  1297. insertData = append(insertData, info.ProductDate)
  1298. insertData = append(insertData, info.ExpiryDate)
  1299. insertData = append(insertData, info.Count)
  1300. insertData = append(insertData, info.Price)
  1301. insertData = append(insertData, info.TotalPrice)
  1302. insertData = append(insertData, info.Remark)
  1303. insertData = append(insertData, info.Ctime)
  1304. insertData = append(insertData, info.Status)
  1305. insertData = append(insertData, info.OrgId)
  1306. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1307. insertData = append(insertData, info.Type)
  1308. insertData = append(insertData, info.Dealer)
  1309. insertData = append(insertData, info.Manufacturer)
  1310. insertData = append(insertData, info.RetailPrice)
  1311. insertData = append(insertData, info.RetailTotalPrice)
  1312. insertData = append(insertData, info.Number)
  1313. insertData = append(insertData, info.BatchNumber)
  1314. insertData = append(insertData, info.CountUnit)
  1315. }
  1316. thisSQL += strings.Join(insertParams, ", ")
  1317. err = utx.Exec(thisSQL, insertData...).Error
  1318. if err != nil {
  1319. utx.Rollback()
  1320. return
  1321. }
  1322. }
  1323. utx.Commit()
  1324. }
  1325. return
  1326. }
  1327. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1328. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1329. return info, err
  1330. }
  1331. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1332. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1333. return info, err
  1334. }
  1335. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1336. db := readDb.Model(&models.WarehouseOut{})
  1337. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1338. if len(keywords) > 0 {
  1339. likeKey := "%" + keywords + "%"
  1340. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1341. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1342. 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")
  1343. }
  1344. if startTime > 0 {
  1345. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1346. }
  1347. if endTime > 0 {
  1348. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1349. }
  1350. db = db.Count(&total)
  1351. offset := (page - 1) * limit
  1352. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1353. return list, total, err
  1354. }
  1355. func FindAllWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, way_type int64) (list []*models.WarehouseOut, total int64, err error) {
  1356. db := readDb.Model(&models.WarehouseOut{})
  1357. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1358. if len(keywords) > 0 {
  1359. likeKey := "%" + keywords + "%"
  1360. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1361. 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")
  1362. }
  1363. if startTime > 0 {
  1364. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1365. }
  1366. if endTime > 0 {
  1367. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1368. }
  1369. if storehouse_id > 0 {
  1370. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1371. }
  1372. if way_type == 0 {
  1373. db = db.Where("xt_warehouse_out.is_sys = 0 || xt_warehouse_out.is_sys = 1 || xt_warehouse_out.is_sys = 12")
  1374. }
  1375. if way_type == 1 {
  1376. db = db.Where("xt_warehouse_out.is_sys = 0")
  1377. }
  1378. if way_type == 2 {
  1379. db = db.Where("xt_warehouse_out.is_sys = 1")
  1380. }
  1381. if way_type == 3 {
  1382. db = db.Where("xt_warehouse_out.is_sys = 12")
  1383. }
  1384. db = db.Count(&total)
  1385. offset := (page - 1) * limit
  1386. if orgId == 10191 {
  1387. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1388. } else {
  1389. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1390. }
  1391. return list, total, err
  1392. }
  1393. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1394. db := readDb.Model(&models.DrugWarehouseOut{})
  1395. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1396. if len(keywords) > 0 {
  1397. likeKey := "%" + keywords + "%"
  1398. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1399. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1400. 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")
  1401. }
  1402. if startTime > 0 {
  1403. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1404. }
  1405. if endTime > 0 {
  1406. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1407. }
  1408. db = db.Count(&total)
  1409. offset := (page - 1) * limit
  1410. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1411. return list, total, err
  1412. }
  1413. func FindAllDrugWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, way_type int64) (list []*models.DrugWarehouseOut, total int64, err error) {
  1414. db := readDb.Model(&models.DrugWarehouseOut{})
  1415. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1416. if len(keywords) > 0 {
  1417. likeKey := "%" + keywords + "%"
  1418. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1419. 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")
  1420. }
  1421. if startTime > 0 {
  1422. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1423. }
  1424. if endTime > 0 {
  1425. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1426. }
  1427. if storehouse_id > 0 {
  1428. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1429. }
  1430. if way_type == 0 {
  1431. db = db.Where("xt_drug_warehouse_out.is_sys = 0 || xt_drug_warehouse_out.is_sys = 1 || xt_drug_warehouse_out.is_sys = 12")
  1432. }
  1433. if way_type == 1 {
  1434. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1435. }
  1436. if way_type == 2 {
  1437. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1438. }
  1439. if way_type == 3 {
  1440. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1441. }
  1442. db = db.Count(&total)
  1443. offset := (page - 1) * limit
  1444. if orgId == 10191 {
  1445. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1446. } else {
  1447. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1448. }
  1449. return list, total, err
  1450. }
  1451. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1452. 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
  1453. return list, err
  1454. }
  1455. type DrugWarehouseOutInfo struct {
  1456. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1457. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1458. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1459. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1460. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1461. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1462. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1463. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1464. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1465. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1466. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1467. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1468. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1469. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1470. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1471. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1472. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1473. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1474. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1475. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1476. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1477. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1478. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1479. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1480. Number string `gorm:"column:number" json:"number" form:"number"`
  1481. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1482. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1483. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1484. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1485. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1486. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1487. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1488. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1489. }
  1490. func (DrugWarehouseOutInfo) TableName() string {
  1491. return "xt_drug_warehouse_out_info"
  1492. }
  1493. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1494. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1495. return list, err
  1496. }
  1497. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1498. info := &models.WarehousingInfo{}
  1499. type WarehouseInfoCountAmount struct {
  1500. Total int64
  1501. }
  1502. var result WarehouseInfoCountAmount
  1503. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1504. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1505. return result.Total, info
  1506. }
  1507. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1508. type SalesReturnCountAmount struct {
  1509. Total int64
  1510. }
  1511. var result SalesReturnCountAmount
  1512. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1513. return result.Total, err
  1514. }
  1515. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1516. type WarehouseOutInfoCountAmount struct {
  1517. Total int64
  1518. }
  1519. var result WarehouseOutInfoCountAmount
  1520. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1521. return result.Total, err
  1522. }
  1523. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1524. type CancleStockoCountAmount struct {
  1525. Total int64
  1526. }
  1527. var result CancleStockoCountAmount
  1528. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1529. return result.Total, err
  1530. }
  1531. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1532. info := models.WarehousingInfo{}
  1533. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  1534. return info, err
  1535. }
  1536. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  1537. stock := models.CancelStock{}
  1538. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  1539. if err == gorm.ErrRecordNotFound {
  1540. return nil, err
  1541. }
  1542. if err != nil {
  1543. return nil, err
  1544. }
  1545. return &stock, nil
  1546. }
  1547. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  1548. stock := models.CancelStock{}
  1549. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1550. if err == gorm.ErrRecordNotFound {
  1551. return nil, err
  1552. }
  1553. if err != nil {
  1554. return nil, err
  1555. }
  1556. return &stock, nil
  1557. }
  1558. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  1559. err := writeDb.Create(&cancelStock).Error
  1560. return err
  1561. }
  1562. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  1563. stock := models.CancelStock{}
  1564. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1565. return stock, err
  1566. }
  1567. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  1568. stock := models.DrugCancelStock{}
  1569. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1570. return stock, err
  1571. }
  1572. func GetManufactureById(id int64) (models.Manufacturer, error) {
  1573. manufacturer := models.Manufacturer{}
  1574. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1575. return manufacturer, err
  1576. }
  1577. func GetDealerById(id int64) (models.Dealer, error) {
  1578. dealer := models.Dealer{}
  1579. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1580. return dealer, err
  1581. }
  1582. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  1583. err := writeDb.Create(&cancelStock).Error
  1584. return err
  1585. }
  1586. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  1587. if len(cancelStockInfo) > 0 {
  1588. utx := writeDb.Begin()
  1589. if len(cancelStockInfo) > 0 {
  1590. 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 "
  1591. insertParams := make([]string, 0)
  1592. insertData := make([]interface{}, 0)
  1593. for _, info := range cancelStockInfo {
  1594. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1595. insertData = append(insertData, info.GoodId)
  1596. insertData = append(insertData, info.CancelStockId)
  1597. insertData = append(insertData, info.GoodTypeId)
  1598. insertData = append(insertData, info.Count)
  1599. insertData = append(insertData, info.Price)
  1600. insertData = append(insertData, info.Total)
  1601. insertData = append(insertData, info.Ctime)
  1602. insertData = append(insertData, info.Status)
  1603. insertData = append(insertData, info.OrgId)
  1604. insertData = append(insertData, info.OrderNumber)
  1605. insertData = append(insertData, info.Type)
  1606. insertData = append(insertData, info.Dealer)
  1607. insertData = append(insertData, info.Manufacturer)
  1608. insertData = append(insertData, info.Number)
  1609. insertData = append(insertData, info.RegisterAccount)
  1610. insertData = append(insertData, info.Remark)
  1611. insertData = append(insertData, info.ProductDate)
  1612. insertData = append(insertData, info.ExpiryDate)
  1613. insertData = append(insertData, info.WarehouseInfoId)
  1614. insertData = append(insertData, info.WarehouseInfoId)
  1615. }
  1616. thisSQL += strings.Join(insertParams, ", ")
  1617. err = utx.Exec(thisSQL, insertData...).Error
  1618. if err != nil {
  1619. utx.Rollback()
  1620. return
  1621. }
  1622. }
  1623. utx.Commit()
  1624. }
  1625. return
  1626. }
  1627. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  1628. err := XTWriteDB().Create(&info).Error
  1629. return err
  1630. }
  1631. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  1632. info := models.DrugCancelStockInfo{}
  1633. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1634. return info, err
  1635. }
  1636. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  1637. err := XTWriteDB().Create(&info).Error
  1638. return err
  1639. }
  1640. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  1641. info := models.CancelStockInfo{}
  1642. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1643. return info, err
  1644. }
  1645. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  1646. info := models.DrugCancelStockInfo{}
  1647. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  1648. return info, err
  1649. }
  1650. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  1651. if len(cancelStockInfo) > 0 {
  1652. utx := writeDb.Begin()
  1653. if len(cancelStockInfo) > 0 {
  1654. 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 "
  1655. insertParams := make([]string, 0)
  1656. insertData := make([]interface{}, 0)
  1657. for _, info := range cancelStockInfo {
  1658. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1659. insertData = append(insertData, info.DrugId)
  1660. insertData = append(insertData, info.CancelStockId)
  1661. insertData = append(insertData, info.Count)
  1662. insertData = append(insertData, info.Price)
  1663. insertData = append(insertData, info.Total)
  1664. insertData = append(insertData, info.Ctime)
  1665. insertData = append(insertData, info.Status)
  1666. insertData = append(insertData, info.OrgId)
  1667. insertData = append(insertData, info.OrderNumber)
  1668. insertData = append(insertData, info.Type)
  1669. insertData = append(insertData, info.Dealer)
  1670. insertData = append(insertData, info.Manufacturer)
  1671. insertData = append(insertData, info.RetailPrice)
  1672. insertData = append(insertData, info.RetailTotalPrice)
  1673. insertData = append(insertData, info.RegisterAccount)
  1674. insertData = append(insertData, info.BatchNumber)
  1675. insertData = append(insertData, info.Remark)
  1676. insertData = append(insertData, info.MaxUnit)
  1677. insertData = append(insertData, info.ProductDate)
  1678. insertData = append(insertData, info.ExpiryDate)
  1679. insertData = append(insertData, info.BatchNumberId)
  1680. }
  1681. thisSQL += strings.Join(insertParams, ", ")
  1682. err = utx.Exec(thisSQL, insertData...).Error
  1683. if err != nil {
  1684. utx.Rollback()
  1685. return
  1686. }
  1687. }
  1688. utx.Commit()
  1689. }
  1690. return
  1691. }
  1692. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  1693. db := readDb.Model(&models.CancelStock{})
  1694. db = db.Where("org_id = ? AND status = 1", orgId)
  1695. err = db.Order("ctime desc").Find(&list).Error
  1696. return
  1697. }
  1698. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  1699. db := readDb.Model(&models.WarehousingInfo{})
  1700. db = db.Where("org_id = ? AND status = 1", orgId)
  1701. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  1702. if startTime > 0 {
  1703. db = db.Where("ctime >=?", startTime)
  1704. }
  1705. if endTime > 0 {
  1706. db = db.Where("ctime<= ?", endTime)
  1707. }
  1708. db = db.Count(&total)
  1709. offset := (page - 1) * limit
  1710. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1711. return
  1712. }
  1713. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  1714. db := XTReadDB().Model(&models.StockInfo{})
  1715. if startime > 0 {
  1716. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1717. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  1718. })
  1719. }
  1720. if startime == 0 || endtime == 0 {
  1721. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1722. return db.Where("org_id = ? AND status = 1", orgId)
  1723. })
  1724. }
  1725. if endtime > 0 {
  1726. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1727. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  1728. })
  1729. }
  1730. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  1731. db = db.Group("xt_good_information.id")
  1732. if startime == 0 || endtime == 0 {
  1733. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1734. return db.Where("org_id = ? AND status = 1", orgId)
  1735. })
  1736. }
  1737. if startime > 0 {
  1738. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1739. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  1740. })
  1741. }
  1742. if endtime > 0 {
  1743. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1744. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  1745. })
  1746. }
  1747. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  1748. if startime == 0 || endtime == 0 {
  1749. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  1750. }
  1751. if startime > 0 {
  1752. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  1753. }
  1754. if endtime > 0 {
  1755. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  1756. }
  1757. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  1758. if len(keyword) > 0 {
  1759. likeKey := "%" + keyword + "%"
  1760. 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)
  1761. 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")
  1762. }
  1763. if type_name > 0 {
  1764. 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)
  1765. }
  1766. db = db.Count(&total)
  1767. offset := (page - 1) * limit
  1768. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1769. return
  1770. }
  1771. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  1772. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  1773. return
  1774. }
  1775. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  1776. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  1777. return
  1778. }
  1779. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  1780. 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
  1781. return
  1782. }
  1783. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  1784. 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
  1785. return
  1786. }
  1787. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  1788. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  1789. "good_id": warehouseInfo.GoodId,
  1790. "number": warehouseInfo.Number,
  1791. "product_date": warehouseInfo.ProductDate,
  1792. "expiry_date": warehouseInfo.ExpiryDate,
  1793. "warehousing_count": warehouseInfo.WarehousingCount,
  1794. "price": warehouseInfo.Price,
  1795. "total_price": warehouseInfo.TotalPrice,
  1796. "mtime": warehouseInfo.Mtime,
  1797. "remark": warehouseInfo.Remark,
  1798. }).Error
  1799. return
  1800. }
  1801. func UpDateWarehouseStatus(id int64) (err error) {
  1802. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1803. return
  1804. }
  1805. func UpdatedStockFlowById(id int64) (err error) {
  1806. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1807. return
  1808. }
  1809. func UpDateDrugWarehouseStatus(id int64) (err error) {
  1810. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1811. return
  1812. }
  1813. func UpdateDrugFlowById(id int64) (err error) {
  1814. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1815. return
  1816. }
  1817. func DeleteWarehouse(ids []string) (err error) {
  1818. ut := writeDb.Begin()
  1819. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1820. if err != nil {
  1821. ut.Rollback()
  1822. return
  1823. }
  1824. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1825. if err != nil {
  1826. ut.Rollback()
  1827. return
  1828. }
  1829. ut.Commit()
  1830. return
  1831. }
  1832. func DeleteStockFlow(ids []string) (err error) {
  1833. ut := writeDb.Begin()
  1834. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1835. if err != nil {
  1836. ut.Rollback()
  1837. return
  1838. }
  1839. ut.Commit()
  1840. return
  1841. }
  1842. func DeleteDrugWarehouse(ids []string) (err error) {
  1843. ut := writeDb.Begin()
  1844. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1845. if err != nil {
  1846. ut.Rollback()
  1847. return
  1848. }
  1849. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1850. if err != nil {
  1851. ut.Rollback()
  1852. return
  1853. }
  1854. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1855. if err != nil {
  1856. ut.Rollback()
  1857. return
  1858. }
  1859. ut.Commit()
  1860. return
  1861. }
  1862. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  1863. warehouse := &models.Warehousing{}
  1864. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  1865. return warehouse, err
  1866. }
  1867. func FindWareHouseOutById(id int64) (*models.WarehouseOut, error) {
  1868. warehouseOut := &models.WarehouseOut{}
  1869. err := readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  1870. return warehouseOut, err
  1871. }
  1872. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  1873. warehouseOut := &models.DrugWarehouseOut{}
  1874. 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
  1875. return warehouseOut, err
  1876. }
  1877. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  1878. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  1879. return
  1880. }
  1881. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  1882. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  1883. return
  1884. }
  1885. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  1886. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1887. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1888. return
  1889. }
  1890. func DeleteStockInRecordByReocordId(id int64) (err error) {
  1891. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1892. return
  1893. }
  1894. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  1895. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  1896. if manufacturer_id > 0 {
  1897. db = db.Where("manufacturer = ?", manufacturer_id)
  1898. }
  1899. if dealer_id > 0 {
  1900. db = db.Where("dealer = ?", dealer_id)
  1901. }
  1902. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  1903. return goodInfo, err
  1904. }
  1905. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  1906. db := readDb.Model(&models.Warehousing{})
  1907. likekey := "%" + keywords + "%"
  1908. db = db.Where("org_id = ? AND status = 1 ", orgId)
  1909. db = db.Where("warehousing_order LIKE ? ", likekey)
  1910. db = db.Count(&total)
  1911. offset := (page - 1) * limit
  1912. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1913. return
  1914. }
  1915. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  1916. info := models.WarehousingInfo{}
  1917. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  1918. return info, err
  1919. }
  1920. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  1921. info := models.WarehousingInfo{}
  1922. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  1923. return info, err
  1924. }
  1925. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  1926. err = writeDb.Save(&info).Error
  1927. return err
  1928. }
  1929. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  1930. err = writeDb.Save(&info).Error
  1931. return err
  1932. }
  1933. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  1934. 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
  1935. return err
  1936. }
  1937. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  1938. 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
  1939. return err
  1940. }
  1941. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  1942. info := models.DrugWarehouseInfo{}
  1943. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  1944. return info, err
  1945. }
  1946. func EditWarehousing(warehouse models.Warehousing) {
  1947. 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
  1948. }
  1949. func EditDrugWarehousing(warehouse models.DrugWarehouse) {
  1950. err = readDb.Model(&models.DrugWarehouse{}).Where("warehousing_order = ? AND status = 1", warehouse.WarehousingOrder).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehousing_time": warehouse.WarehousingTime, "modifier": warehouse.Modifier, "dealer": warehouse.Dealer, "manufacturer": warehouse.Manufacturer}).Error
  1951. }
  1952. func FindAllWarehouse(org_id int64, types int64) (list []*models.Warehousing, err error) {
  1953. err = readDb.Model(&models.Warehousing{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ? ", org_id).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  1954. return list, err
  1955. }
  1956. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  1957. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  1958. 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)
  1959. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  1960. return list, err
  1961. }
  1962. func DeleteSalesReturn(ids []string) (err error) {
  1963. ut := writeDb.Begin()
  1964. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1965. if err != nil {
  1966. ut.Rollback()
  1967. return
  1968. }
  1969. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1970. if err != nil {
  1971. ut.Rollback()
  1972. return
  1973. }
  1974. ut.Commit()
  1975. return
  1976. }
  1977. func DeleteDrugSalesReturn(ids []string) (err error) {
  1978. ut := writeDb.Begin()
  1979. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1980. if err != nil {
  1981. ut.Rollback()
  1982. return
  1983. }
  1984. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1985. if err != nil {
  1986. ut.Rollback()
  1987. return
  1988. }
  1989. ut.Commit()
  1990. return
  1991. }
  1992. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1993. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1994. return
  1995. }
  1996. type DrugSalesReturnInfo struct {
  1997. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1998. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1999. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2000. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2001. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2002. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2003. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2004. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2005. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2006. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2007. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2008. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2009. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2010. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2011. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2012. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2013. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2014. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2015. Number string `gorm:"column:number" json:"number" form:"number"`
  2016. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2017. }
  2018. func (DrugSalesReturnInfo) TableName() string {
  2019. return "xt_drug_sales_return_info"
  2020. }
  2021. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2022. 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
  2023. return
  2024. }
  2025. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2026. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2027. return
  2028. }
  2029. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2030. 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
  2031. return
  2032. }
  2033. func UpDateSaleReturnStatus(id int64) (err error) {
  2034. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2035. return
  2036. }
  2037. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2038. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2039. return
  2040. }
  2041. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2042. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2043. return salesReturn, err
  2044. }
  2045. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2046. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2047. return salesReturn, err
  2048. }
  2049. func EditSaleReturn(sales models.SalesReturn) {
  2050. 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
  2051. }
  2052. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2053. 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
  2054. }
  2055. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2056. err = writeDb.Save(&info).Error
  2057. return err
  2058. }
  2059. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2060. err = writeDb.Save(&info).Error
  2061. return err
  2062. }
  2063. func DeleteWarehouseOut(ids []string) (err error) {
  2064. ut := writeDb.Begin()
  2065. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2066. if err != nil {
  2067. ut.Rollback()
  2068. return
  2069. }
  2070. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2071. if err != nil {
  2072. ut.Rollback()
  2073. return
  2074. }
  2075. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2076. if err != nil {
  2077. ut.Rollback()
  2078. return
  2079. }
  2080. ut.Commit()
  2081. return
  2082. }
  2083. func GetWarehouseOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  2084. err = XTReadDB().Where("warehouse_out_id in(?) and status = 0", ids).Find(&info).Error
  2085. return info, err
  2086. }
  2087. func UpdateWarehouseInfoById(id int64, count int64) error {
  2088. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  2089. return err
  2090. }
  2091. func GetDrugWarhouseOutByIds(ids []string) (info []*models.DrugWarehouseOutInfo, err error) {
  2092. err = XTReadDB().Where("warehouse_out_id in(?) and status = 0", ids).Find(&info).Error
  2093. return info, err
  2094. }
  2095. func UpdateDrugInfoByIds(id int64, count int64) error {
  2096. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  2097. return err
  2098. }
  2099. func UpdateDrugInfoByIdsOne(id int64, count int64) error {
  2100. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  2101. return err
  2102. }
  2103. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2104. ut := writeDb.Begin()
  2105. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2106. if err != nil {
  2107. ut.Rollback()
  2108. return
  2109. }
  2110. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2111. if err != nil {
  2112. ut.Rollback()
  2113. return
  2114. }
  2115. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2116. if err != nil {
  2117. ut.Rollback()
  2118. return
  2119. }
  2120. ut.Commit()
  2121. return
  2122. }
  2123. func UpDateWarehouseOutStatus(id int64) (err error) {
  2124. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2125. return
  2126. }
  2127. func UpdateStockFlowWarehouseOutById(id int64) (err error) {
  2128. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehouse_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2129. return err
  2130. }
  2131. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2132. ut := writeDb.Begin()
  2133. err = ut.Save(&info).Error
  2134. if err != nil {
  2135. ut.Rollback()
  2136. return err
  2137. }
  2138. ut.Commit()
  2139. return err
  2140. }
  2141. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2142. ut := writeDb.Begin()
  2143. err = ut.Save(&info).Error
  2144. if err != nil {
  2145. ut.Rollback()
  2146. return err
  2147. }
  2148. ut.Commit()
  2149. return err
  2150. }
  2151. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2152. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2153. return
  2154. }
  2155. func DeleteDrugWarehouseOutInfoById(id int64) (err error) {
  2156. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehouse_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2157. return
  2158. }
  2159. func GetDrugWarehosueOutInfo(id int64) (models.DrugWarehouseOutInfo, error) {
  2160. info := models.DrugWarehouseOutInfo{}
  2161. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  2162. return info, err
  2163. }
  2164. func EditWarehouseOut(warehouseOut models.WarehouseOut) {
  2165. err = readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", warehouseOut.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehouse_out_time": warehouseOut.WarehouseOutTime, "dealer": warehouseOut.Dealer, "manufacturer": warehouseOut.Manufacturer, "storehouse_id": warehouseOut.StorehouseId}).Error
  2166. }
  2167. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2168. 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
  2169. }
  2170. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo) (err error) {
  2171. err = writeDb.Save(&info).Error
  2172. return err
  2173. }
  2174. func UpDateDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) (err error) {
  2175. err = writeDb.Save(&info).Error
  2176. return err
  2177. }
  2178. func UpdateDrugFlowSix(warehousoutid int64, drugid int64, warehouse_out_order_number string, flow models.DrugFlow) error {
  2179. err := XTWriteDB().Model(&flow).Where("warehouse_out_id = ? and drug_id = ? and warehouse_out_order_number = ?", warehousoutid, drugid, warehouse_out_order_number).Updates(map[string]interface{}{"count": flow.Count, "expire_date": flow.ExpireDate, "product_date": flow.ProductDate, "price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "number": flow.Number, "storehouse_id": flow.StorehouseId, "admin_user_id": flow.AdminUserId, "stock_count": flow.StockCount}).Error
  2180. return err
  2181. }
  2182. func FindAllCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, storehouse_id int64) (list []*models.CancelStock, total int64, err error) {
  2183. db := readDb.Model(&models.CancelStock{})
  2184. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2185. if len(keywords) > 0 {
  2186. likeKey := "%" + keywords + "%"
  2187. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2188. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2189. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2190. }
  2191. if startTime > 0 {
  2192. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2193. }
  2194. if endTime > 0 {
  2195. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2196. }
  2197. if storehouse_id > 0 {
  2198. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2199. }
  2200. db = db.Count(&total)
  2201. offset := (page - 1) * limit
  2202. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2203. return
  2204. }
  2205. func FindAllDrugCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, storehoue_id int64) (list []*models.DrugCancelStock, total int64, err error) {
  2206. db := readDb.Model(&models.DrugCancelStock{})
  2207. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2208. if len(keywords) > 0 {
  2209. likeKey := "%" + keywords + "%"
  2210. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2211. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2212. 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")
  2213. }
  2214. if startTime > 0 {
  2215. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2216. }
  2217. if endTime > 0 {
  2218. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2219. }
  2220. if storehoue_id > 0 {
  2221. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2222. }
  2223. db = db.Count(&total)
  2224. offset := (page - 1) * limit
  2225. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2226. return
  2227. }
  2228. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2229. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2230. return info, err
  2231. }
  2232. func DeleteCancelStock(ids []string) (err error) {
  2233. ut := writeDb.Begin()
  2234. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2235. if err != nil {
  2236. ut.Rollback()
  2237. return
  2238. }
  2239. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2240. if err != nil {
  2241. ut.Rollback()
  2242. return
  2243. }
  2244. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2245. if err != nil {
  2246. ut.Rollback()
  2247. return
  2248. }
  2249. ut.Commit()
  2250. return
  2251. }
  2252. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2253. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2254. return info, err
  2255. }
  2256. func DeleteDrugCancelStock(ids []string) (err error) {
  2257. ut := writeDb.Begin()
  2258. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2259. if err != nil {
  2260. ut.Rollback()
  2261. return
  2262. }
  2263. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2264. if err != nil {
  2265. ut.Rollback()
  2266. return
  2267. }
  2268. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2269. if err != nil {
  2270. ut.Rollback()
  2271. return
  2272. }
  2273. ut.Commit()
  2274. return
  2275. }
  2276. func UpDateCancleStockStatus(id int64) (err error) {
  2277. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2278. return
  2279. }
  2280. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2281. info := models.CancelStockInfo{}
  2282. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2283. return info, err
  2284. }
  2285. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2286. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2287. return err
  2288. }
  2289. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2290. 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
  2291. return
  2292. }
  2293. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2294. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2295. return
  2296. }
  2297. func UpDateDrugCancelStockById(id int64) (err error) {
  2298. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2299. return
  2300. }
  2301. func EditCancelStock(sales models.CancelStock) {
  2302. 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
  2303. }
  2304. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2305. 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
  2306. }
  2307. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2308. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2309. return cancelStock, err
  2310. }
  2311. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2312. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2313. return cancelStock, err
  2314. }
  2315. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2316. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2317. return cancelStock, err
  2318. }
  2319. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2320. err = writeDb.Save(&info).Error
  2321. return err
  2322. }
  2323. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2324. err = writeDb.Save(&info).Error
  2325. return err
  2326. }
  2327. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2328. 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
  2329. return list, err
  2330. }
  2331. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2332. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2333. 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)
  2334. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2335. return list, err
  2336. }
  2337. type StockDetailModel struct {
  2338. GoodId int64 `gorm:"good_id" json:"good_id"`
  2339. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2340. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2341. Price float64 `gorm:"price" json:"price"`
  2342. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2343. Type int `gorm:"type" json:"type"`
  2344. Creater int64 `gorm:"creater" json:"creater"`
  2345. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2346. Types int64 `gorm:"types" json:"types"`
  2347. }
  2348. func FindStockInDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.WarehousingInfo, total int64, err error) {
  2349. db := readDb.Model(&models.WarehousingInfo{})
  2350. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2351. if len(keywords) > 0 {
  2352. likeKey := "%" + keywords + "%"
  2353. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2354. 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)
  2355. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2356. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2357. 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")
  2358. } else {
  2359. 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)
  2360. }
  2361. if manufacturer > 0 {
  2362. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2363. }
  2364. if storehouse_id > 0 {
  2365. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2366. }
  2367. if order_type > 0 {
  2368. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2369. }
  2370. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2371. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2372. db = db.Count(&total)
  2373. offset := (page - 1) * limit
  2374. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2375. return
  2376. }
  2377. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2378. db := readDb.Model(&models.WarehousingInfo{})
  2379. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2380. if len(keywords) > 0 {
  2381. likeKey := "%" + keywords + "%"
  2382. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2383. 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)
  2384. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2385. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2386. 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")
  2387. } else {
  2388. 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)
  2389. }
  2390. if manufacturer > 0 {
  2391. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2392. }
  2393. if order_type > 0 {
  2394. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2395. }
  2396. if storehouse_id > 0 {
  2397. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2398. }
  2399. type PriceAmount struct {
  2400. Total float64
  2401. }
  2402. var result []PriceAmount
  2403. err = db.Select("total_price as total").Scan(&result).Error
  2404. var total float64
  2405. for _, value := range result {
  2406. total = total + value.Total
  2407. }
  2408. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2409. return err, total
  2410. }
  2411. func FloatRound(f float64, n int) float64 {
  2412. format := "%." + strconv.Itoa(n) + "f"
  2413. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  2414. return res
  2415. }
  2416. func FindStockOutDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2417. db := readDb.Model(&models.WarehouseOutInfo{})
  2418. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 ", orgId)
  2419. if len(keywords) > 0 {
  2420. likeKey := "%" + keywords + "%"
  2421. 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)
  2422. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2423. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2424. 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")
  2425. } else {
  2426. 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)
  2427. }
  2428. if manufacturer > 0 {
  2429. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2430. }
  2431. if order_type > 0 {
  2432. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2433. }
  2434. if storehouse_id > 0 {
  2435. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2436. }
  2437. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2438. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2439. db = db.Count(&total)
  2440. offset := (page - 1) * limit
  2441. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2442. return
  2443. }
  2444. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2445. db := readDb.Model(&models.WarehouseOutInfo{})
  2446. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0", orgId)
  2447. if len(keywords) > 0 {
  2448. likeKey := "%" + keywords + "%"
  2449. 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)
  2450. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2451. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2452. 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")
  2453. } else {
  2454. 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)
  2455. }
  2456. if manufacturer > 0 {
  2457. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2458. }
  2459. if order_type > 0 {
  2460. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2461. }
  2462. if storehouse_id > 0 {
  2463. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2464. }
  2465. type PriceAmount struct {
  2466. Total float64
  2467. }
  2468. var result []PriceAmount
  2469. err = db.Select("(count * price) as total").Scan(&result).Error
  2470. var total float64
  2471. for _, value := range result {
  2472. total = total + value.Total
  2473. }
  2474. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2475. fmt.Println(total)
  2476. return err, total
  2477. }
  2478. 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) {
  2479. db := readDb.Model(&models.SalesReturnInfo{})
  2480. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  2481. if len(keywords) > 0 {
  2482. likeKey := "%" + keywords + "%"
  2483. 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)
  2484. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  2485. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  2486. 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")
  2487. } else {
  2488. 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)
  2489. }
  2490. if manufacturer > 0 {
  2491. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  2492. }
  2493. if order_type > 0 {
  2494. db = db.Where("xt_sales_return_info.type = ?", order_type)
  2495. }
  2496. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  2497. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2498. offset := (page - 1) * limit
  2499. db = db.Count(&total)
  2500. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  2501. return
  2502. }
  2503. func FindeCancelGroup(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.CancelStockInfo, err error) {
  2504. db := readDb.Model(&models.CancelStockInfo{})
  2505. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2506. if len(keywords) > 0 {
  2507. likeKey := "%" + keywords + "%"
  2508. 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)
  2509. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2510. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2511. 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")
  2512. } else {
  2513. 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)
  2514. }
  2515. if manufacturer > 0 {
  2516. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2517. }
  2518. if order_type > 0 {
  2519. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2520. }
  2521. if storehouse_id > 0 {
  2522. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  2523. }
  2524. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2525. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2526. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  2527. return
  2528. }
  2529. func FindCancelDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (list []*models.CancelStockInfo, total int64, err error) {
  2530. db := readDb.Model(&models.CancelStockInfo{})
  2531. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2532. if len(keywords) > 0 {
  2533. likeKey := "%" + keywords + "%"
  2534. 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)
  2535. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2536. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2537. 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")
  2538. } else {
  2539. 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)
  2540. }
  2541. if manufacturer > 0 {
  2542. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2543. }
  2544. if order_type > 0 {
  2545. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2546. }
  2547. if storehouse_id > 0 {
  2548. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  2549. }
  2550. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2551. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2552. db = db.Count(&total)
  2553. offset := (page - 1) * limit
  2554. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  2555. return
  2556. }
  2557. func FindStockInByDealerId(id int64) (total int64, err error) {
  2558. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  2559. return
  2560. }
  2561. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  2562. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  2563. return
  2564. }
  2565. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  2566. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  2567. return
  2568. }
  2569. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  2570. 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)
  2571. return
  2572. }
  2573. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  2574. 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)
  2575. return
  2576. }
  2577. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  2578. 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)
  2579. return
  2580. }
  2581. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  2582. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  2583. return goodType, err
  2584. }
  2585. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  2586. 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
  2587. return info, err
  2588. }
  2589. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  2590. 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
  2591. return
  2592. }
  2593. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  2594. 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
  2595. return
  2596. }
  2597. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2598. 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
  2599. return
  2600. }
  2601. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  2602. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  2603. return
  2604. }
  2605. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  2606. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  2607. return
  2608. }
  2609. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2610. info := models.WarehouseOutInfo{}
  2611. 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
  2612. if err == gorm.ErrRecordNotFound {
  2613. return nil, err
  2614. }
  2615. if err != nil {
  2616. return nil, err
  2617. }
  2618. return &info, nil
  2619. }
  2620. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2621. info := models.WarehouseOutInfo{}
  2622. 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
  2623. if err == gorm.ErrRecordNotFound {
  2624. return nil, err
  2625. }
  2626. if err != nil {
  2627. return nil, err
  2628. }
  2629. return &info, nil
  2630. }
  2631. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2632. err := XTWriteDB().Model(&prepare).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and patient_id = ? and org_id = ? and status =1", goodid, goodtypeid, recordtime, patientid, orgid).Updates(map[string]interface{}{"count": prepare.Count}).Error
  2633. return err
  2634. }
  2635. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2636. 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
  2637. return err
  2638. }
  2639. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo) error {
  2640. err := writeDb.Create(&info).Error
  2641. return err
  2642. }
  2643. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  2644. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  2645. return err
  2646. }
  2647. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  2648. ut := writeDb.Begin()
  2649. err := ut.Create(&info).Error
  2650. if err != nil {
  2651. ut.Rollback()
  2652. return err
  2653. }
  2654. ut.Commit()
  2655. return err
  2656. }
  2657. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  2658. ut := writeDb.Begin()
  2659. err := ut.Create(&detail).Error
  2660. if err != nil {
  2661. ut.Rollback()
  2662. return err
  2663. }
  2664. ut.Commit()
  2665. return err
  2666. }
  2667. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  2668. err := writeDb.Create(&detail).Error
  2669. return err
  2670. }
  2671. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  2672. 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
  2673. return
  2674. }
  2675. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  2676. 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
  2677. return
  2678. }
  2679. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  2680. 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
  2681. return
  2682. }
  2683. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  2684. info := models.WarehouseOutInfo{}
  2685. 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
  2686. return info, err
  2687. }
  2688. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  2689. 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
  2690. return
  2691. }
  2692. func UpdateStockOutInfoCount(org_id int64, id int64) {
  2693. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  2694. }
  2695. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  2696. db := readDb.Model(&models.AutomaticReduceDetail{})
  2697. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  2698. db = db.Preload("GoodsType", "status = 1")
  2699. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  2700. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  2701. db = db.Count(&total)
  2702. err = db.Find(&user).Error
  2703. return
  2704. }
  2705. type DrugAutomaticReduceDetail struct {
  2706. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2707. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  2708. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  2709. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  2710. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2711. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2712. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2713. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  2714. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2715. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2716. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2717. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  2718. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2719. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  2720. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  2721. }
  2722. func (DrugAutomaticReduceDetail) TableName() string {
  2723. return "xt_drug_automatic_reduce_detail"
  2724. }
  2725. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  2726. db := readDb.Model(&DrugAutomaticReduceDetail{})
  2727. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  2728. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  2729. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  2730. db = db.Count(&total)
  2731. err = db.Find(&user).Error
  2732. return
  2733. }
  2734. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  2735. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  2736. }
  2737. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  2738. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  2739. }
  2740. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  2741. 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
  2742. return
  2743. }
  2744. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  2745. 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
  2746. return
  2747. }
  2748. type VMWarehousingInfo struct {
  2749. ID int64 `gorm:"column:id" json:"id"`
  2750. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  2751. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  2752. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  2753. Status int64 `gorm:"column:status" json:"status"`
  2754. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2755. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  2756. }
  2757. func (VMWarehousingInfo) TableName() string {
  2758. return "xt_warehouse_info"
  2759. }
  2760. type GoodInfo struct {
  2761. ID int64 `gorm:"column:id" json:"id"`
  2762. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  2763. Status int64 `gorm:"column:status" json:"status"`
  2764. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2765. }
  2766. func (GoodInfo) TableName() string {
  2767. return "xt_good_information"
  2768. }
  2769. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  2770. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  2771. return
  2772. }
  2773. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  2774. 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
  2775. return
  2776. }
  2777. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  2778. likeKey := "%" + keyword + "%"
  2779. db := XTReadDB().Model(&goods)
  2780. if len(keyword) > 0 {
  2781. db = db.Where("type_name like ?", likeKey)
  2782. }
  2783. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  2784. return goods, err
  2785. }
  2786. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  2787. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3)", orgid).
  2788. Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&goodstype).Error
  2789. return goodstype, err
  2790. }
  2791. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  2792. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  2793. if orgid > 0 {
  2794. db = db.Where("x.user_org_id = ?", orgid)
  2795. }
  2796. if recordtime > 0 {
  2797. db = db.Where("x.record_date = ?", recordtime)
  2798. }
  2799. if patientId > 0 {
  2800. db = db.Where("x.patient_id = ?", patientId)
  2801. }
  2802. 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
  2803. return prepare, err
  2804. }
  2805. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  2806. detail := models.XtAutomaticReduceDetail{}
  2807. 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
  2808. if err == gorm.ErrRecordNotFound {
  2809. return nil, err
  2810. }
  2811. if err != nil {
  2812. return nil, err
  2813. }
  2814. return &detail, nil
  2815. }
  2816. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  2817. 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
  2818. return autoMatic, err
  2819. }
  2820. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  2821. 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
  2822. return autoMatic, err
  2823. }
  2824. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  2825. var manufacturer models.Manufacturer
  2826. var err error
  2827. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  2828. if err == gorm.ErrRecordNotFound {
  2829. return nil, err
  2830. }
  2831. if err != nil {
  2832. return nil, err
  2833. }
  2834. return &manufacturer, nil
  2835. }
  2836. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  2837. err := XTWriteDB().Create(manufacturer).Error
  2838. return err
  2839. }
  2840. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  2841. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  2842. return err
  2843. }
  2844. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  2845. var good models.GoodInfo
  2846. var err error
  2847. 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
  2848. if err == gorm.ErrRecordNotFound {
  2849. return nil, err
  2850. }
  2851. if err != nil {
  2852. return nil, err
  2853. }
  2854. return &good, nil
  2855. }
  2856. func CreateGoodsInfomation(info *models.GoodInfo) error {
  2857. err := XTWriteDB().Create(&info).Error
  2858. return err
  2859. }
  2860. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  2861. err := XTWriteDB().Model(&info).Where("good_name = ? and good_type_id = ? and specification_name = ? and org_id = ? and status = 1", goodName, goodType, specificationName, orgid).Updates(map[string]interface{}{"good_name": info.GoodName, "good_type_id": info.GoodTypeId, "medical_insurance_level": info.MedicalInsuranceLevel, "good_kind": info.GoodKind, "specification_name": info.SpecificationName, "manufacturer": info.Manufacturer, "good_unit": info.GoodUnit, "retail_price": info.RetailPrice, "stock_warn_count": info.StockWarnCount, "dealer": info.Dealer, "pinyin": info.Pinyin, "wubi": info.Wubi, "buy_price": info.BuyPrice, "medical_insurance_number": info.MedicalInsuranceNumber, "is_special_diseases": info.IsSpecialDiseases, "is_record": info.IsRecord, "statistics_category": info.StatisticsCategory, "good_status": info.GoodStatus, "social_security_directory_code": info.SocialSecurityDirectoryCode, "production_type": info.ProductionType, "special_medical": info.SpecialMedical, "remark": info.Remark}).Error
  2862. return err
  2863. }
  2864. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  2865. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  2866. 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
  2867. return houseOutInfo, err
  2868. }
  2869. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  2870. key := "%" + keyword + "%"
  2871. if len(keyword) == 0 {
  2872. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  2873. } else {
  2874. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  2875. }
  2876. return goodInfo, err
  2877. }
  2878. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  2879. var drug models.BaseDrugLib
  2880. var err error
  2881. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  2882. if err == gorm.ErrRecordNotFound {
  2883. return nil, err
  2884. }
  2885. if err != nil {
  2886. return nil, err
  2887. }
  2888. return &drug, nil
  2889. }
  2890. func IsExistDrugByNameOne(drugname string, dosage float64, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  2891. var drug models.BaseDrugLib
  2892. var err error
  2893. 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
  2894. if err == gorm.ErrRecordNotFound {
  2895. return nil, err
  2896. }
  2897. if err != nil {
  2898. return nil, err
  2899. }
  2900. return &drug, nil
  2901. }
  2902. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  2903. err := XTWriteDB().Create(&info).Error
  2904. return err
  2905. }
  2906. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  2907. err := XTWriteDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and org_id = ? and status = 1", drugName, drugSpec, orgid).Updates(map[string]interface{}{"drug_name": drug.DrugName, "drug_alias": drug.DrugAlias, "drug_spec": drug.DrugSpec, "drug_type": drug.DrugType, "drug_stock_limit": drug.DrugStockLimit, "drug_origin_place": drug.DrugOriginPlace, "drug_dosage_form": drug.DrugDosageForm, "medical_insurance_level": drug.MedicalInsuranceLevel, "max_unit": drug.MaxUnit, "min_unit": drug.MinUnit, "unit_matrixing": drug.UnitMatrixing, "retail_price": drug.RetailPrice, "last_price": drug.LastPrice, "drug_classify": drug.DrugClassify, "manufacturer": drug.Manufacturer, "dealer": drug.Dealer, "pinyin": drug.Pinyin, "wubi": drug.Wubi, "drug_alias_pinyin": drug.DrugAliasPinyin, "drug_alias_wubi": drug.DrugAliasWubi, "drug_category": drug.DrugCategory, "drug_control": drug.DrugControl, "number": drug.Number, "hosp_appr_flag": drug.HospApprFlag, "medical_insurance_number": drug.MedicalInsuranceNumber, "pharmacology_category": drug.PharmacologyCategory, "statistics_category": drug.StatisticsCategory, "code": drug.Code, "is_special_diseases": drug.IsSpecialDiseases, "is_record": drug.IsRecord, "prescription_mark": drug.PrescriptionMark, "social_security_directory_code": drug.SocialSecurityDirectoryCode, "record_date": drug.RecordDate, "drug_remark": drug.DrugRemark, "drug_status": drug.DrugStatus, "limit_remark": drug.LimitRemark, "drug_dose": drug.DrugDose, "drug_dose_unit": drug.DrugDoseUnit, "lmt_used_flag": drug.LmtUsedFlag, "delivery_way": drug.DeliveryWay, "execution_frequency": drug.ExecutionFrequency, "drug_day": drug.DrugDay}).Error
  2908. return err
  2909. }
  2910. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  2911. var projoect models.HisProject
  2912. var err error
  2913. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  2914. if err == gorm.ErrRecordNotFound {
  2915. return nil, err
  2916. }
  2917. if err != nil {
  2918. return nil, err
  2919. }
  2920. return &projoect, nil
  2921. }
  2922. func CreateProjectInfomation(info *models.HisProject) error {
  2923. err := XTWriteDB().Create(&info).Error
  2924. return err
  2925. }
  2926. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  2927. 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
  2928. return err
  2929. }
  2930. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  2931. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  2932. return warehouseinfo, err
  2933. }
  2934. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  2935. 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
  2936. return err
  2937. }
  2938. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  2939. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  2940. return list, err
  2941. }
  2942. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  2943. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  2944. return list, err
  2945. }
  2946. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  2947. var dealer models.Dealer
  2948. var err error
  2949. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  2950. if err == gorm.ErrRecordNotFound {
  2951. return nil, err
  2952. }
  2953. if err != nil {
  2954. return nil, err
  2955. }
  2956. return &dealer, nil
  2957. }
  2958. func CreateDealer(dealer *models.Dealer) error {
  2959. err := XTWriteDB().Create(&dealer).Error
  2960. return err
  2961. }
  2962. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  2963. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  2964. return err
  2965. }
  2966. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  2967. dataconfig := models.DictDataconfig{}
  2968. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  2969. return dataconfig, err
  2970. }
  2971. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  2972. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  2973. return dataconfig, err
  2974. }
  2975. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  2976. var dataconfig models.DictDataconfig
  2977. var err error
  2978. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  2979. if err == gorm.ErrRecordNotFound {
  2980. return nil, err
  2981. }
  2982. if err != nil {
  2983. return nil, err
  2984. }
  2985. return &dataconfig, nil
  2986. }
  2987. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  2988. dataconfig := models.DictDataconfig{}
  2989. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  2990. return dataconfig, err
  2991. }
  2992. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  2993. err := XTWriteDB().Create(&dataconfig).Error
  2994. return err
  2995. }
  2996. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  2997. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  2998. return err
  2999. }
  3000. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3001. var goodType models.GoodsType
  3002. var err error
  3003. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3004. if err == gorm.ErrRecordNotFound {
  3005. return nil, err
  3006. }
  3007. if err != nil {
  3008. return nil, err
  3009. }
  3010. return &goodType, nil
  3011. }
  3012. func CreatedGoodType(goodsType *models.GoodsType) error {
  3013. err := XTWriteDB().Create(&goodsType).Error
  3014. return err
  3015. }
  3016. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3017. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3018. return goodsType, err
  3019. }
  3020. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3021. var department models.XtHisDepartment
  3022. var err error
  3023. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3024. if err == gorm.ErrRecordNotFound {
  3025. return nil, err
  3026. }
  3027. if err != nil {
  3028. return nil, err
  3029. }
  3030. return &department, nil
  3031. }
  3032. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3033. dataconfig := models.Dataconfig{}
  3034. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3035. return dataconfig, err
  3036. }
  3037. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3038. var dataconfig models.Dataconfig
  3039. var err error
  3040. 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
  3041. if err == gorm.ErrRecordNotFound {
  3042. return nil, err
  3043. }
  3044. if err != nil {
  3045. return nil, err
  3046. }
  3047. return &dataconfig, nil
  3048. }
  3049. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3050. dataconfig := models.Dataconfig{}
  3051. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3052. return dataconfig, err
  3053. }
  3054. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3055. err := XTWriteDB().Create(&dataconfig).Error
  3056. return err
  3057. }
  3058. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3059. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3060. return dataconfig, err
  3061. }
  3062. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3063. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3064. return list, err
  3065. }
  3066. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3067. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and drug_status<>'停用'", orgid)
  3068. if len(keywords) > 0 {
  3069. likekey := "%" + keywords + "%"
  3070. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3071. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3072. }).Find(&drug).Error
  3073. } else {
  3074. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3075. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3076. }).Find(&drug).Error
  3077. }
  3078. return
  3079. }
  3080. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3081. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and drug_status<>'停用'", orgid)
  3082. if len(keywords) > 0 {
  3083. likekey := "%" + keywords + "%"
  3084. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3085. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3086. }).Find(&drug).Error
  3087. } else {
  3088. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3089. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3090. }).Find(&drug).Error
  3091. }
  3092. return
  3093. }
  3094. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3095. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3096. if len(keywords) > 0 {
  3097. likekey := "%" + keywords + "%"
  3098. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3099. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3100. }).Find(&good).Error
  3101. } else {
  3102. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3103. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3104. }).Find(&good).Error
  3105. }
  3106. return
  3107. }
  3108. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfo, err error) {
  3109. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3110. if len(keywords) > 0 {
  3111. likekey := "%" + keywords + "%"
  3112. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3113. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3114. }).Find(&good).Error
  3115. } else {
  3116. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3117. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3118. }).Find(&good).Error
  3119. }
  3120. return
  3121. }
  3122. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3123. db := writeDb.Begin()
  3124. err := db.Create(&detail).Error
  3125. if err != nil {
  3126. db.Rollback()
  3127. return err
  3128. }
  3129. db.Commit()
  3130. return err
  3131. }
  3132. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3133. var autoreduece models.BloodAutomaticReduceDetail
  3134. var err error
  3135. 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
  3136. if err == gorm.ErrRecordNotFound {
  3137. return nil, err
  3138. }
  3139. if err != nil {
  3140. return nil, err
  3141. }
  3142. return &autoreduece, nil
  3143. }
  3144. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3145. detail := models.BloodAutomaticReduceDetail{}
  3146. 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
  3147. return err
  3148. }
  3149. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3150. 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
  3151. return stockflow, err
  3152. }
  3153. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64) error {
  3154. detail := models.BloodAutomaticReduceDetail{}
  3155. 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
  3156. info := models.WarehouseOutInfo{}
  3157. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3158. prepare := models.DialysisBeforePrepare{}
  3159. 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
  3160. flow := models.VmStockFlow{}
  3161. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3162. return err
  3163. }
  3164. func UpDateWarehouseInfoByStockDelete(id int64, count int64) (err error) {
  3165. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3166. return err
  3167. }
  3168. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3169. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  3170. return info, err
  3171. }
  3172. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  3173. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  3174. return list, err
  3175. }
  3176. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  3177. 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
  3178. return err
  3179. }
  3180. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  3181. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  3182. return err
  3183. }
  3184. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  3185. var autoreduece models.BloodAutomaticReduceDetail
  3186. var err error
  3187. 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
  3188. if err == gorm.ErrRecordNotFound {
  3189. return nil, err
  3190. }
  3191. if err != nil {
  3192. return nil, err
  3193. }
  3194. return &autoreduece, nil
  3195. }
  3196. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  3197. detail := models.WarehouseOutInfoSeven{}
  3198. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3199. return detail, err
  3200. }
  3201. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  3202. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3203. return detail, err
  3204. }
  3205. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  3206. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  3207. return project, err
  3208. }
  3209. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  3210. project := models.HisPrescriptionProject{}
  3211. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  3212. return project, err
  3213. }
  3214. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  3215. err = XTReadDB().Select("count,warehouse_info_id,good_id,org_id").Where(" patient_id = ? and sys_record_time = ? and status = 1 and is_sys = 1 and count>0", patientid, recorddate).Find(&auto).Error
  3216. return auto, err
  3217. }
  3218. //func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.VmStockFlow, err error) {
  3219. //
  3220. // err = XTReadDB().Select("count,warehousing_id").Where(" patient_id = ? and system_time = ? and status = 1 and is_sys = 1 and count>0",patientid,recorddate).Find(&auto).Error
  3221. // return auto, err
  3222. //
  3223. //}
  3224. func ModefyWarehouseInfo(count int64, id int64) error {
  3225. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3226. return err
  3227. }
  3228. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  3229. 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
  3230. 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
  3231. err = XTWriteDB().Model(models.VmStockFlow{}).Where("patient_id = ? and system_time = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3232. 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
  3233. fmt.Println("err2332332322323322323232323232332", err)
  3234. return err
  3235. }
  3236. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  3237. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3238. return
  3239. }
  3240. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  3241. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3242. return
  3243. }
  3244. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  3245. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3246. return info, err
  3247. }
  3248. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3249. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3250. return info, err
  3251. }
  3252. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3253. offset := (page - 1) * limit
  3254. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  3255. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3256. fmt.Println(table)
  3257. if startime > 0 {
  3258. db = db.Where("x.ctime>=?", startime)
  3259. }
  3260. if endtime > 0 {
  3261. db = db.Where("x.ctime<=?", endtime)
  3262. }
  3263. likeKey := "%" + keyword + "%"
  3264. if len(likeKey) > 0 {
  3265. db = db.Where("t.good_name like ?", likeKey)
  3266. }
  3267. if orgid > 0 {
  3268. db = db.Where("x.org_id = ?", orgid)
  3269. }
  3270. if good_type > 0 {
  3271. db = db.Where("t.good_type_id = ?", good_type)
  3272. }
  3273. 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
  3274. return info, total, err
  3275. }
  3276. 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) {
  3277. offset := (page - 1) * limit
  3278. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  3279. likeKey := "%" + keyword + "%"
  3280. if len(likeKey) > 0 {
  3281. db = db.Where("x.good_name like ?", likeKey)
  3282. }
  3283. if orgid > 0 {
  3284. db = db.Where("x.org_id = ?", orgid)
  3285. }
  3286. if good_type > 0 {
  3287. db = db.Where("x.good_type_id = ?", good_type)
  3288. }
  3289. if good_id > 0 {
  3290. db = db.Where("x.id = ?", good_id)
  3291. }
  3292. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3293. if startime > 0 {
  3294. db = db.Where("ctime>=?", startime)
  3295. }
  3296. if endtime > 0 {
  3297. db = db.Where("ctime<=?", endtime)
  3298. }
  3299. if good_id > 0 {
  3300. db = db.Where("good_id = ?", good_id)
  3301. }
  3302. if storehouse_id > 0 {
  3303. db = db.Where("storehouse_id = ?", storehouse_id)
  3304. }
  3305. return db.Where("status = 1")
  3306. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3307. if startime > 0 {
  3308. db = db.Where("ctime>=?", startime)
  3309. }
  3310. if endtime > 0 {
  3311. db = db.Where("ctime<=?", endtime)
  3312. }
  3313. if good_id > 0 {
  3314. db = db.Where("good_id = ?", good_id)
  3315. }
  3316. if storehouse_id > 0 {
  3317. db = db.Where("storehouse_id = ?", storehouse_id)
  3318. }
  3319. return db.Where("status = 1").Group("good_id,storehouse_id")
  3320. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3321. if startime > 0 {
  3322. db = db.Where("ctime>=?", startime)
  3323. }
  3324. if endtime > 0 {
  3325. db = db.Where("ctime<=?", endtime)
  3326. }
  3327. return db.Where("status = 1")
  3328. }).Find(&info).Error
  3329. return info, total, err
  3330. }
  3331. func GetStockListById(good_id int64, orgid int64, limit int64, page int64, startime int64, endtime int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3332. offset := (page - 1) * limit
  3333. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  3334. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3335. fmt.Println(table)
  3336. if startime > 0 {
  3337. db = db.Where("x.ctime>=?", startime)
  3338. }
  3339. if endtime > 0 {
  3340. db = db.Where("x.ctime<=?", endtime)
  3341. }
  3342. if orgid > 0 {
  3343. db = db.Where("x.org_id = ?", orgid)
  3344. }
  3345. if good_id > 0 {
  3346. db = db.Where("x.good_id = ?", good_id)
  3347. }
  3348. 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").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(&info).Error
  3349. return info, total, err
  3350. }
  3351. 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) {
  3352. offset := (page - 1) * limit
  3353. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  3354. if startime > 0 {
  3355. db = db.Where("x.ctime>=?", startime)
  3356. }
  3357. if endtime > 0 {
  3358. db = db.Where("x.ctime<=?", endtime)
  3359. }
  3360. if orgid > 0 {
  3361. db = db.Where("x.org_id = ?", orgid)
  3362. }
  3363. if good_id > 0 {
  3364. db = db.Where("x.good_id = ?", good_id)
  3365. }
  3366. if is_sys == 2 {
  3367. db = db.Where("x.is_sys = 1")
  3368. }
  3369. if is_sys == 3 {
  3370. db = db.Where("x.is_sys = 0")
  3371. }
  3372. 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
  3373. return info, total, err
  3374. }
  3375. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3376. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3377. if startime > 0 {
  3378. db = db.Where("x.ctime >=?", startime)
  3379. }
  3380. if endtime > 0 {
  3381. db = db.Where("x.ctime<=?", endtime)
  3382. }
  3383. if orgid > 0 {
  3384. db = db.Where("x.org_id = ?", orgid)
  3385. }
  3386. 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
  3387. return info, err
  3388. }
  3389. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3390. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3391. if startime > 0 {
  3392. db = db.Where("x.ctime >=?", startime)
  3393. }
  3394. if endtime > 0 {
  3395. db = db.Where("x.ctime<=?", endtime)
  3396. }
  3397. if orgid > 0 {
  3398. db = db.Where("x.org_id = ?", orgid)
  3399. }
  3400. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  3401. return info, err
  3402. }
  3403. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3404. if endtime > 0 {
  3405. 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
  3406. }
  3407. if endtime == 0 {
  3408. 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
  3409. }
  3410. return autoMatic, err
  3411. }
  3412. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  3413. out := models.WarehouseOut{}
  3414. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  3415. return out, err
  3416. }
  3417. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3418. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3419. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3420. fmt.Println(table)
  3421. if id > 0 {
  3422. db = db.Where("x.warehouse_out_id = ?", id)
  3423. }
  3424. if orgid > 0 {
  3425. db = db.Where("x.org_id = ?", orgid)
  3426. }
  3427. err = db.Select("x.id,x.warehouse_out_id,x.warehouse_info_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,t.manufacturer,x.is_sys,x.sys_record_time,x.number,x.remark,x.license_number,x.supply_cancel_out_id,x.supply_warehouse_id,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,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
  3428. return out, err
  3429. }
  3430. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  3431. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  3432. if len(id) > 0 {
  3433. db = db.Where("x.id in(?)", id)
  3434. }
  3435. if orgid > 0 {
  3436. db = db.Where("x.org_id= ?", orgid)
  3437. }
  3438. 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
  3439. return out, err
  3440. }
  3441. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3442. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3443. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3444. fmt.Println(table)
  3445. if len(id) > 0 {
  3446. db = db.Where("x.warehouse_out_id in (?)", id)
  3447. }
  3448. if orgid > 0 {
  3449. db = db.Where("x.org_id = ?", orgid)
  3450. }
  3451. err = db.Select("x.id,x.warehouse_out_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,t.manufacturer,t.social_security_directory_code,x.is_sys,x.sys_record_time,x.number,x.remark,x.license_number,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit,t.packing_price").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
  3452. return out, err
  3453. }
  3454. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  3455. db := XTReadDB().Model(&info)
  3456. db = db.Preload("GoodInfo", "status = 1")
  3457. 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
  3458. return info, err
  3459. }
  3460. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3461. db := readDb.Model(&models.AutomaticReduceDetail{})
  3462. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3463. db = db.Preload("GoodsType", "status = 1")
  3464. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3465. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  3466. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  3467. db = db.Count(&total)
  3468. err = db.Find(&user).Error
  3469. return
  3470. }
  3471. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  3472. info := models.GoodInfo{}
  3473. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3474. return info, err
  3475. }
  3476. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  3477. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  3478. return err
  3479. }
  3480. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  3481. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3482. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3483. fmt.Println(table)
  3484. if orgid > 0 {
  3485. db = db.Where("x.org_id = ?", orgid)
  3486. }
  3487. if len(orderid) > 0 {
  3488. db = db.Where("x.warehousing_id in(?)", orderid)
  3489. }
  3490. if startime > 0 {
  3491. db = db.Where("x.ctime>=?", startime)
  3492. }
  3493. if endtime > 0 {
  3494. db = db.Where("x.ctime<=?", endtime)
  3495. }
  3496. 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
  3497. return info, err
  3498. }
  3499. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  3500. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3501. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3502. fmt.Println(table)
  3503. if orgid > 0 {
  3504. db = db.Where("x.org_id = ?", orgid)
  3505. }
  3506. if len(orderid) > 0 {
  3507. db = db.Where("x.warehousing_id in(?)", orderid)
  3508. }
  3509. if startime > 0 {
  3510. db = db.Where("x.ctime>=?", startime)
  3511. }
  3512. if endtime > 0 {
  3513. db = db.Where("x.ctime<=?", endtime)
  3514. }
  3515. if storehouse_id > 0 {
  3516. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3517. }
  3518. 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
  3519. return info, err
  3520. }
  3521. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3522. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3523. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  3524. fmt.Println(table)
  3525. if startime > 0 {
  3526. db = db.Where("x.ctime>=?", startime)
  3527. }
  3528. if endtime > 0 {
  3529. db = db.Where("x.ctime<=?", endtime)
  3530. }
  3531. if orgid > 0 {
  3532. db = db.Where("x.org_id = ?", orgid)
  3533. }
  3534. 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
  3535. return info, err
  3536. }
  3537. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  3538. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  3539. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3540. fmt.Println(table)
  3541. if orgid > 0 {
  3542. db = db.Where("x.org_id = ?", orgid)
  3543. }
  3544. if len(ids) > 0 {
  3545. db = db.Where("x.warehouse_out_id in (?)", ids)
  3546. }
  3547. if startime > 0 {
  3548. db = db.Where("x.ctime>=?", startime)
  3549. }
  3550. if endtime > 0 {
  3551. db = db.Where("x.ctime<=?", endtime)
  3552. }
  3553. err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.number,x.license_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,t.manufacturer,t.dealer").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&out).Error
  3554. return out, err
  3555. }
  3556. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  3557. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  3558. if len(ids) > 0 {
  3559. db = db.Where("x.id in(?)", ids)
  3560. }
  3561. if orgid > 0 {
  3562. db = db.Where("x.org_id = ?", orgid)
  3563. }
  3564. 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
  3565. return info, err
  3566. }
  3567. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3568. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  3569. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  3570. fmt.Println(table)
  3571. if len(ids) > 0 {
  3572. db = db.Where("x.warehouse_out_id in(?)", ids)
  3573. }
  3574. if orgid > 0 {
  3575. db = db.Where("x.org_id = ?", orgid)
  3576. }
  3577. 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
  3578. return info, err
  3579. }
  3580. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  3581. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  3582. likeKey := "%" + keyword + "%"
  3583. if len(keyword) > 0 {
  3584. db = db.Where("x.drug_name like ?", likeKey)
  3585. }
  3586. if orgid > 0 {
  3587. db = db.Where("x.org_id = ?", orgid)
  3588. }
  3589. err = db.Find(&basedrug).Error
  3590. return basedrug, err
  3591. }
  3592. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  3593. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3594. if len(ids) > 0 {
  3595. db = db.Where("x.drug_id in(?)", ids)
  3596. }
  3597. if orgid > 0 {
  3598. db = db.Where("x.org_id = ?", orgid)
  3599. }
  3600. if startime > 0 {
  3601. db = db.Where("x.ctime>=?", startime)
  3602. }
  3603. if endtime > 0 {
  3604. db = db.Where("x.ctime<=?", endtime)
  3605. }
  3606. if storehouse_id > 0 {
  3607. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3608. }
  3609. err = db.Find(&info).Error
  3610. return info, err
  3611. }
  3612. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  3613. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3614. if len(ids) > 0 {
  3615. db = db.Where("x.drug_id in(?)", ids)
  3616. }
  3617. if orgid > 0 {
  3618. db = db.Where("x.org_id = ?", orgid)
  3619. }
  3620. if startime > 0 {
  3621. db = db.Where("x.ctime>=?", startime)
  3622. }
  3623. if endtime > 0 {
  3624. db = db.Where("x.ctime<=?", endtime)
  3625. }
  3626. if storehouse_id > 0 {
  3627. db = db.Where("storehouse_id = ?", storehouse_id)
  3628. }
  3629. err = db.Find(&info).Error
  3630. return info, err
  3631. }
  3632. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  3633. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3634. if drugid > 0 {
  3635. db = db.Where("x.drug_id = ?", drugid)
  3636. }
  3637. if orgid > 0 {
  3638. db = db.Where("x.org_id = ?", orgid)
  3639. }
  3640. if storehouse_id > 0 {
  3641. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3642. }
  3643. 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
  3644. return info, err
  3645. }
  3646. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  3647. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3648. if drugid > 0 {
  3649. db = db.Where("x.drug_id = ?", drugid)
  3650. }
  3651. if orgid > 0 {
  3652. db = db.Where("x.org_id = ?", orgid)
  3653. }
  3654. if storehouse_id > 0 {
  3655. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3656. }
  3657. 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
  3658. return info, err
  3659. }
  3660. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  3661. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3662. if id > 0 {
  3663. db = db.Where("x.id = ?", id)
  3664. }
  3665. if orgid > 0 {
  3666. db = db.Where("x.org_id = ?", orgid)
  3667. }
  3668. 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
  3669. return info, err
  3670. }
  3671. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  3672. info := models.VmDrugWarehouseInfo{}
  3673. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3674. if id == 0 {
  3675. db = db.Where("id = ?", id)
  3676. }
  3677. if drugid > 0 {
  3678. db = db.Where("x.drug_id = ?", drugid)
  3679. }
  3680. if orgid > 0 {
  3681. db = db.Where("x.org_id = ?", orgid)
  3682. }
  3683. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  3684. return info, err
  3685. }
  3686. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  3687. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3688. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  3689. fmt.Println(table)
  3690. if drugid > 0 {
  3691. db = db.Where("x.drug_id = ?", drugid)
  3692. }
  3693. if storehouse_id > 0 {
  3694. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3695. }
  3696. 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
  3697. return list, err
  3698. }
  3699. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  3700. lib := models.BaseDrugLib{}
  3701. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  3702. return lib, err
  3703. }
  3704. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  3705. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3706. if goodid > 0 {
  3707. db = db.Where("x.good_id = ?", goodid)
  3708. }
  3709. if orgid > 0 {
  3710. db = db.Where("x.org_id = ?", orgid)
  3711. }
  3712. 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
  3713. return info, err
  3714. }
  3715. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  3716. 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
  3717. return err
  3718. }
  3719. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  3720. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  3721. return info, err
  3722. }
  3723. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  3724. //遍历循环入库数据
  3725. for i, item := range list {
  3726. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  3727. if (item.WarehousingCount - item.StockCount) >= count {
  3728. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  3729. info := models.WarehousingInfo{
  3730. GoodId: item.GoodId,
  3731. OrgId: item.OrgId,
  3732. WarehousingId: item.WarehousingId,
  3733. StockCount: item.StockCount + count,
  3734. }
  3735. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  3736. fmt.Println("2323233232323232323232323232323", err2)
  3737. return err
  3738. }
  3739. if (item.WarehousingCount - item.StockCount) < count {
  3740. //如果该批次小于退库数量,则先出库该相差数量
  3741. var countOne = item.WarehousingCount - item.StockCount
  3742. infoOne := models.WarehousingInfo{
  3743. GoodId: item.GoodId,
  3744. OrgId: item.OrgId,
  3745. WarehousingId: item.WarehousingId,
  3746. StockCount: countOne + item.StockCount,
  3747. }
  3748. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  3749. if err == nil {
  3750. //剩余的出库到第二个批次
  3751. var countTwo = count - countOne
  3752. infos := append(list, list[i+1])
  3753. var idArray []*models.WarehousingInfo
  3754. for _, it := range infos {
  3755. if it.WarehousingId != item.WarehousingId {
  3756. idArray = append(idArray, it)
  3757. }
  3758. }
  3759. //查询更新后的库存
  3760. UpdateCancelOut(idArray, countTwo)
  3761. return err
  3762. } else {
  3763. return err
  3764. }
  3765. }
  3766. }
  3767. return err
  3768. }
  3769. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  3770. if len(idstr) > 0 {
  3771. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  3772. } else {
  3773. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  3774. }
  3775. return info, err
  3776. }
  3777. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  3778. if len(idstr) > 0 {
  3779. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  3780. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3781. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  3782. }).Find(&info).Error
  3783. }
  3784. return info, err
  3785. }
  3786. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  3787. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  3788. if startime > 0 {
  3789. db = db.Where("x.ctime >=?", startime)
  3790. }
  3791. if endtime > 0 {
  3792. db = db.Where("x.ctime <= ?", endtime)
  3793. }
  3794. if orgid > 0 {
  3795. db = db.Where("x.org_id = ?", orgid)
  3796. }
  3797. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  3798. return info, err
  3799. }
  3800. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3801. info := models.WarehouseOutInfo{}
  3802. var err error
  3803. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  3804. if err == gorm.ErrRecordNotFound {
  3805. return nil, err
  3806. }
  3807. if err != nil {
  3808. return nil, err
  3809. }
  3810. return &info, nil
  3811. }
  3812. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  3813. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3814. if goodid > 0 {
  3815. db = db.Where("x.good_id = ?", goodid)
  3816. }
  3817. if orgid > 0 {
  3818. db = db.Where("x.org_id = ?", orgid)
  3819. }
  3820. if id > 0 {
  3821. db = db.Where("x.warehouse_info_id = ?", id)
  3822. }
  3823. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  3824. return info, err
  3825. }
  3826. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  3827. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  3828. if goodid > 0 {
  3829. db = db.Where("x.good_id = ?", goodid)
  3830. }
  3831. if orgid > 0 {
  3832. db = db.Where("x.user_org_id = ?", orgid)
  3833. }
  3834. if id > 0 {
  3835. db = db.Where("x.warehousing_id = ?", id)
  3836. }
  3837. if storehouse_id > 0 {
  3838. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3839. }
  3840. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  3841. return info, err
  3842. }
  3843. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  3844. stock := models.CancelStock{}
  3845. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  3846. return stock, err
  3847. }
  3848. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  3849. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  3850. return info, err
  3851. }
  3852. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  3853. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  3854. return info, err
  3855. }
  3856. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  3857. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  3858. return stock, err
  3859. }
  3860. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  3861. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and count > 0")
  3862. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  3863. fmt.Println(table)
  3864. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  3865. fmt.Println(tableone)
  3866. if id > 0 {
  3867. db = db.Where("x.warehouse_out_id = ?", id)
  3868. }
  3869. if goodid > 0 {
  3870. db = db.Where("x.good_id = ?", goodid)
  3871. }
  3872. 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
  3873. //err =XTReadDB()()()()().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&stock).Error
  3874. return stock, err
  3875. }
  3876. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  3877. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  3878. return info, err
  3879. }
  3880. func ExportDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseInfo, err error) {
  3881. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1")
  3882. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  3883. fmt.Println("table", table)
  3884. if len(warehouse_out_id) > 0 {
  3885. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  3886. }
  3887. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.price,x.dealer,x.manufacturer,x.warehousing_order,x.batch_number,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  3888. return out, err
  3889. }
  3890. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  3891. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3892. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  3893. fmt.Println("table", table)
  3894. if len(warehouse_out_id) > 0 {
  3895. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  3896. }
  3897. 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
  3898. return out, err
  3899. }
  3900. func GetExportOutOrderDrugListOne(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  3901. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1")
  3902. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  3903. fmt.Println("table", table)
  3904. if len(warehouse_out_id) > 0 {
  3905. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  3906. }
  3907. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.count,x.max_unit as count_unit,x.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,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
  3908. return out, err
  3909. }
  3910. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  3911. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  3912. return out, err
  3913. }
  3914. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  3915. err = XTReadDB().Where("id in(?)", warehouse_out_id).
  3916. Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  3917. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid)
  3918. }).Find(&out).Error
  3919. return out, err
  3920. }
  3921. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  3922. info := models.WarehouseOutInfo{}
  3923. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  3924. return info, err
  3925. }
  3926. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  3927. info := models.WarehousingInfo{}
  3928. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3929. if goodid > 0 {
  3930. db = db.Where("x.good_id = ?", goodid)
  3931. }
  3932. if storehouse_id > 0 {
  3933. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3934. }
  3935. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  3936. return info, err
  3937. }
  3938. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  3939. info := models.WarehousingInfo{}
  3940. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  3941. return info, err
  3942. }
  3943. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  3944. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3945. return err
  3946. }
  3947. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  3948. if storehouse_id == 0 {
  3949. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  3950. }
  3951. if storehouse_id > 0 {
  3952. 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
  3953. }
  3954. return info, err
  3955. }
  3956. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  3957. if len(warehousingInfo) > 0 {
  3958. utx := writeDb.Begin()
  3959. if len(warehousingInfo) > 0 {
  3960. 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 "
  3961. insertParams := make([]string, 0)
  3962. insertData := make([]interface{}, 0)
  3963. for _, info := range warehousingInfo {
  3964. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  3965. insertData = append(insertData, info.WarehousingId)
  3966. insertData = append(insertData, info.GoodId)
  3967. insertData = append(insertData, info.Number)
  3968. insertData = append(insertData, info.LicenseNumber)
  3969. insertData = append(insertData, info.Count)
  3970. insertData = append(insertData, info.UserOrgId)
  3971. insertData = append(insertData, info.PatientId)
  3972. insertData = append(insertData, info.SystemTime)
  3973. insertData = append(insertData, info.ConsumableType)
  3974. insertData = append(insertData, info.IsSys)
  3975. insertData = append(insertData, info.WarehousingOrder)
  3976. insertData = append(insertData, info.WarehouseOutId)
  3977. insertData = append(insertData, info.WarehouseOutOrderNumber)
  3978. insertData = append(insertData, info.IsEdit)
  3979. insertData = append(insertData, info.CancelStockId)
  3980. insertData = append(insertData, info.CancelOrderNumber)
  3981. insertData = append(insertData, info.Manufacturer)
  3982. insertData = append(insertData, info.Dealer)
  3983. insertData = append(insertData, info.Creator)
  3984. insertData = append(insertData, info.UpdateCreator)
  3985. insertData = append(insertData, info.Status)
  3986. insertData = append(insertData, info.Ctime)
  3987. insertData = append(insertData, info.Mtime)
  3988. insertData = append(insertData, info.Price)
  3989. insertData = append(insertData, info.WarehousingDetailId)
  3990. insertData = append(insertData, info.WarehouseOutDetailId)
  3991. insertData = append(insertData, info.CancelOutDetailId)
  3992. insertData = append(insertData, info.ExpireDate)
  3993. insertData = append(insertData, info.ProductDate)
  3994. }
  3995. thisSQL += strings.Join(insertParams, ", ")
  3996. err = utx.Exec(thisSQL, insertData...).Error
  3997. if err != nil {
  3998. utx.Rollback()
  3999. return
  4000. }
  4001. }
  4002. utx.Commit()
  4003. }
  4004. return
  4005. }
  4006. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4007. err := XTWriteDB().Create(&flow).Error
  4008. return err
  4009. }
  4010. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4011. err := XTWriteDB().Create(&flow).Error
  4012. return err
  4013. }
  4014. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4015. err := XTWriteDB().Save(&flow).Error
  4016. return err
  4017. }
  4018. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4019. 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}).Error
  4020. return err
  4021. }
  4022. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  4023. stock := models.VmStockFlow{}
  4024. var err error
  4025. err = XTReadDB().Model(&stock).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", warehousing_id, patient_id, record_time, good_id).Find(&stock).Error
  4026. if err == gorm.ErrRecordNotFound {
  4027. return nil, err
  4028. }
  4029. if err != nil {
  4030. return nil, err
  4031. }
  4032. return &stock, nil
  4033. }
  4034. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  4035. info := models.WarehouseOutInfo{}
  4036. var err error
  4037. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4038. if err == gorm.ErrRecordNotFound {
  4039. return nil, err
  4040. }
  4041. if err != nil {
  4042. return nil, err
  4043. }
  4044. return &info, nil
  4045. }
  4046. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64) (*models.WarehouseOutInfo, error) {
  4047. info := models.WarehouseOutInfo{}
  4048. var err error
  4049. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ?", good_id, record_time, patient_id).Find(&info).Error
  4050. if err == gorm.ErrRecordNotFound {
  4051. return nil, err
  4052. }
  4053. if err != nil {
  4054. return nil, err
  4055. }
  4056. return &info, nil
  4057. }
  4058. 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) {
  4059. offset := (page - 1) * limit
  4060. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  4061. if consumable_type > 0 {
  4062. //入库
  4063. if consumable_type == 1 {
  4064. db = db.Where("consumable_type = ?", consumable_type)
  4065. }
  4066. //2 手动出库
  4067. if consumable_type == 2 {
  4068. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  4069. }
  4070. //3.自动出库
  4071. if consumable_type == 3 {
  4072. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  4073. }
  4074. //4.手动退库
  4075. if consumable_type == 4 {
  4076. db = db.Where(" consumable_type = ?", consumable_type)
  4077. }
  4078. if consumable_type == 10 {
  4079. db = db.Where(" consumable_type = ?", consumable_type)
  4080. }
  4081. if consumable_type == 11 {
  4082. db = db.Where(" consumable_type = ?", consumable_type)
  4083. }
  4084. if consumable_type == 12 {
  4085. db = db.Where(" consumable_type = ?", consumable_type)
  4086. }
  4087. if consumable_type == 13 {
  4088. db = db.Where(" consumable_type = ?", consumable_type)
  4089. }
  4090. }
  4091. if orgId > 0 {
  4092. db = db.Where("user_org_id = ?", orgId)
  4093. }
  4094. if startTime > 0 {
  4095. db = db.Where("ctime >= ?", startTime)
  4096. }
  4097. if endTime > 0 {
  4098. db = db.Where("ctime<=?", endTime)
  4099. }
  4100. if good_id > 0 {
  4101. db = db.Where("good_id = ?", good_id)
  4102. }
  4103. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  4104. return flow, total, err
  4105. }
  4106. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  4107. detail := models.AutomaticReduceDetail{}
  4108. 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
  4109. return detail, err
  4110. }
  4111. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  4112. detail := models.WarehouseOutInfo{}
  4113. 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
  4114. return detail, err
  4115. }
  4116. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64) error {
  4117. outInfo := models.WarehouseOutInfo{}
  4118. err := XTWriteDB().Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", good_id, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber}).Error
  4119. return err
  4120. }
  4121. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  4122. info := models.WarehouseOutInfo{}
  4123. 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
  4124. return info, err
  4125. }
  4126. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  4127. info := models.WarehousingInfo{}
  4128. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4129. return info, err
  4130. }
  4131. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  4132. info := models.WarehousingInfo{}
  4133. db := XTReadDB().Model(&info)
  4134. if good_id > 0 {
  4135. db = db.Where("good_id = ? and status = 1", good_id)
  4136. }
  4137. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  4138. return info, err
  4139. }
  4140. 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) {
  4141. offset := (page - 1) * limit
  4142. db := XTReadDB().Model(drug_flow).Where("status = 1")
  4143. if drugid > 0 {
  4144. db = db.Where("drug_id = ?", drugid)
  4145. }
  4146. if orgid > 0 {
  4147. db = db.Where("user_org_id = ?", orgid)
  4148. }
  4149. if startTime > 0 {
  4150. db = db.Where("ctime >=? ", startTime)
  4151. }
  4152. if endTime > 0 {
  4153. db = db.Where("ctime<=?", endTime)
  4154. }
  4155. if stock_type > 0 {
  4156. //手动入库
  4157. if stock_type == 1 {
  4158. db = db.Where("consumable_type = 1")
  4159. }
  4160. //手动出库
  4161. if stock_type == 2 {
  4162. db = db.Where("consumable_type = 2 and is_sys = 0")
  4163. }
  4164. //自动出库
  4165. if stock_type == 3 {
  4166. db = db.Where("consumable_type = 3 and is_sys = 1")
  4167. }
  4168. if stock_type == 4 {
  4169. db = db.Where("consumable_type = 4")
  4170. }
  4171. if stock_type == 10 {
  4172. db = db.Where("consumable_type = 10")
  4173. }
  4174. if stock_type == 11 {
  4175. db = db.Where("consumable_type = 11")
  4176. }
  4177. if stock_type == 12 {
  4178. db = db.Where("consumable_type = 12")
  4179. }
  4180. if stock_type == 13 {
  4181. db = db.Where("consumable_type = 13")
  4182. }
  4183. }
  4184. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Preload("BaseDrugLib", "status = 1").Preload("DrugWarehouseInfo", "status = 1").Find(&drug_flow).Error
  4185. return drug_flow, total, err
  4186. }
  4187. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  4188. outInfo := models.DrugWarehouseOutInfo{}
  4189. var err error
  4190. 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
  4191. if err == gorm.ErrRecordNotFound {
  4192. return nil, err
  4193. }
  4194. if err != nil {
  4195. return nil, err
  4196. }
  4197. return &outInfo, nil
  4198. }
  4199. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  4200. 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
  4201. return outInfo, err
  4202. }
  4203. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  4204. outInfo := models.DrugWarehouseOutInfo{}
  4205. 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
  4206. return err
  4207. }
  4208. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  4209. db := XTReadDB().Model(&cancle).Where("status = 1")
  4210. if startime > 0 {
  4211. db = db.Where("ctime >=?", startime)
  4212. }
  4213. if endtime > 0 {
  4214. db = db.Where("ctime <=?", endtime)
  4215. }
  4216. if len(ids) > 0 {
  4217. db = db.Where("cancel_stock_id in(?)", ids)
  4218. }
  4219. if orgid > 0 {
  4220. db = db.Where("org_id = ?", orgid)
  4221. }
  4222. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  4223. err = db.Find(&cancle).Error
  4224. return cancle, err
  4225. }
  4226. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  4227. db := XTReadDB().Model(&info)
  4228. if id > 0 {
  4229. db = db.Where("cancel_stock_id = ? and status = 1", id)
  4230. }
  4231. db = db.Preload("BaseDrugLib")
  4232. err = db.Find(&info).Error
  4233. return info, err
  4234. }
  4235. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  4236. info := models.XtDrugWarehouseInfo{}
  4237. db := XTReadDB().Model(&info).Where("status = 1")
  4238. if id > 0 {
  4239. db = db.Where("id = ?", id)
  4240. }
  4241. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4242. return info, err
  4243. }
  4244. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4245. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  4246. return info, err
  4247. }
  4248. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  4249. err = XTReadDB().Select("id,stock_max_number,stock_min_number").Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  4250. return info, err
  4251. }
  4252. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  4253. info := models.HisDoctorAdviceInfo{}
  4254. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  4255. return info, err
  4256. }
  4257. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  4258. info := models.HisDoctorAdviceInfo{}
  4259. 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
  4260. return info, err
  4261. }
  4262. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  4263. detail := models.DrugAutomaticReduceDetail{}
  4264. 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
  4265. return detail, err
  4266. }
  4267. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  4268. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  4269. return err
  4270. }
  4271. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  4272. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  4273. return err
  4274. }
  4275. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  4276. 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
  4277. 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
  4278. 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
  4279. return err
  4280. }
  4281. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  4282. 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
  4283. err = XTWriteDB().Model(models.DrugWarehouseOutInfo{}).Where("drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and advice_id = ?", drugid, patient_id, record_time, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  4284. 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
  4285. return err
  4286. }
  4287. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  4288. info := models.XtDrugWarehouseInfo{}
  4289. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  4290. return info, err
  4291. }
  4292. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  4293. info := models.XtDrugWarehouseInfo{}
  4294. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  4295. return info, err
  4296. }
  4297. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  4298. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  4299. return err
  4300. }
  4301. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  4302. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  4303. //return err
  4304. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  4305. return err
  4306. }
  4307. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  4308. info := models.DrugCancelStockInfo{}
  4309. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  4310. return info, err
  4311. }
  4312. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  4313. info := models.DrugCancelStockInfo{}
  4314. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  4315. return info, err
  4316. }
  4317. func UpdatedDrugInfo(id int64, count int64) (err error) {
  4318. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  4319. return err
  4320. }
  4321. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  4322. 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
  4323. return err
  4324. }
  4325. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  4326. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  4327. return err
  4328. }
  4329. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  4330. 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
  4331. return err
  4332. }
  4333. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  4334. 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
  4335. return err
  4336. }
  4337. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  4338. 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
  4339. return err
  4340. }
  4341. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  4342. 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
  4343. return err
  4344. }
  4345. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  4346. info := models.WarehousingInfo{}
  4347. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4348. return info, err
  4349. }
  4350. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4351. info := models.WarehousingInfo{}
  4352. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  4353. return info, err
  4354. }
  4355. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4356. info := models.WarehousingInfo{}
  4357. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  4358. return info, err
  4359. }
  4360. func CreateWarehouseOut(out models.WarehouseOut) error {
  4361. err := XTWriteDB().Create(&out).Error
  4362. return err
  4363. }
  4364. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  4365. out := models.WarehouseOut{}
  4366. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  4367. return out, err
  4368. }
  4369. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  4370. err := XTReadDB().Create(&info).Error
  4371. return err
  4372. }
  4373. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  4374. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4375. return err
  4376. }
  4377. func UpdateWareInfoById(id int64, count int64) error {
  4378. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4379. return err
  4380. }
  4381. func UpdateWareInfoByIdSix(id int64, count int64) error {
  4382. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  4383. return err
  4384. }
  4385. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  4386. info := models.CancelStockInfo{}
  4387. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4388. return info, err
  4389. }
  4390. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  4391. info := models.CancelStockInfo{}
  4392. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  4393. return info, err
  4394. }
  4395. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  4396. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4397. return info, err
  4398. }
  4399. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  4400. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4401. return info, err
  4402. }
  4403. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  4404. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  4405. return err
  4406. }
  4407. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  4408. drugFlow := models.DrugFlow{}
  4409. 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
  4410. return err
  4411. }
  4412. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  4413. drugFlow := models.DrugFlow{}
  4414. 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
  4415. return err
  4416. }
  4417. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  4418. stock := models.DrugCancelStock{}
  4419. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4420. return stock, err
  4421. }
  4422. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  4423. db := XTReadDB().Model(&info)
  4424. if warehouse_info_id > 0 {
  4425. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  4426. }
  4427. if drug_id > 0 {
  4428. db = db.Where("drug_id = ? and status =1", drug_id)
  4429. }
  4430. err = db.Find(&info).Error
  4431. return info, err
  4432. }
  4433. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  4434. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  4435. return info, err
  4436. }
  4437. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  4438. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  4439. return config, err
  4440. }
  4441. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  4442. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  4443. return config, err
  4444. }
  4445. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  4446. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4447. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  4448. fmt.Println(table)
  4449. likeKey := "%" + keyword + "%"
  4450. if orgid > 0 {
  4451. db = db.Where("x.org_id = ?", orgid)
  4452. }
  4453. if storehouse_id > 0 {
  4454. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4455. }
  4456. 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
  4457. return info, err
  4458. }
  4459. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  4460. err := XTWriteDB().Create(&adjustPrice).Error
  4461. return err
  4462. }
  4463. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  4464. likeKey := "%" + keyword + "%"
  4465. offset := (page - 1) * limit
  4466. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  4467. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4468. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4469. fmt.Println(table, tab)
  4470. if startime > 0 {
  4471. db = db.Where("x.start_time>=?", startime)
  4472. }
  4473. if endtime > 0 {
  4474. db = db.Where("x.start_time<=?", endtime)
  4475. }
  4476. if len(keyword) > 0 {
  4477. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  4478. }
  4479. if orgid > 0 {
  4480. db = db.Where("x.user_org_id = ?", orgid)
  4481. }
  4482. 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
  4483. return list, total, err
  4484. }
  4485. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  4486. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  4487. return err
  4488. }
  4489. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  4490. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  4491. return err
  4492. }
  4493. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  4494. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  4495. return info, err
  4496. }
  4497. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  4498. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  4499. return adjust, err
  4500. }
  4501. func CreateReportPrice(price *models.XtStockReportPrice) error {
  4502. err := XTWriteDB().Create(&price).Error
  4503. return err
  4504. }
  4505. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  4506. likeKey := "%" + keyword + "%"
  4507. offset := (page - 1) * limit
  4508. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  4509. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4510. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4511. fmt.Println(table, tab)
  4512. if startime > 0 {
  4513. db = db.Where("x.ctime>=?", startime)
  4514. }
  4515. if endtime > 0 {
  4516. db = db.Where("x.ctime<=?", endtime)
  4517. }
  4518. if len(keyword) > 0 {
  4519. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  4520. }
  4521. if orgid > 0 {
  4522. db = db.Where("x.user_org_id = ?", orgid)
  4523. }
  4524. 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
  4525. return adjust, total, err
  4526. }
  4527. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  4528. likeKey := "%" + keyword + "%"
  4529. offset := (page - 1) * limit
  4530. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  4531. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4532. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4533. fmt.Println(table, tab)
  4534. if len(keyword) > 0 {
  4535. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  4536. }
  4537. if orgid > 0 {
  4538. db = db.Where("x.user_org_id = ?", orgid)
  4539. }
  4540. if startime > 0 {
  4541. db = db.Where("x.ctime >=?", startime)
  4542. }
  4543. if endtime > 0 {
  4544. db = db.Where("x.ctime <=?", endtime)
  4545. }
  4546. if storehouse_id > 0 {
  4547. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4548. }
  4549. err = db.Group("x.id").Select("x.ctime,x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,x.storehouse_id,t.packing_unit,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  4550. return adjust, total, err
  4551. }
  4552. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  4553. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  4554. if inventory_type == 0 {
  4555. db = db.Where("x.inventory_type = 4")
  4556. }
  4557. if inventory_type > 0 {
  4558. db = db.Where("x.inventory_type = ?", inventory_type)
  4559. }
  4560. if len(keyword) > 0 {
  4561. likeKey := "%" + keyword + "%"
  4562. db = db.Where("x.warehousing_order like ?", likeKey)
  4563. }
  4564. if orgid > 0 {
  4565. db = db.Where("x.user_org_id = ?", orgid)
  4566. }
  4567. 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
  4568. return adjust, err
  4569. }
  4570. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  4571. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  4572. if orgid > 0 {
  4573. db = db.Where("x.user_org_id = ?", orgid)
  4574. }
  4575. 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
  4576. return adjust, err
  4577. }
  4578. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  4579. 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
  4580. return err
  4581. }
  4582. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  4583. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  4584. return report, err
  4585. }
  4586. func CreateInentory(inventory models.XtStockInventory) error {
  4587. err = XTWriteDB().Create(&inventory).Error
  4588. return err
  4589. }
  4590. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  4591. likeKey := "%" + keyword + "%"
  4592. offset := (page - 1) * limit
  4593. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  4594. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4595. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4596. fmt.Println(table, tab)
  4597. if startime > 0 {
  4598. db = db.Where("x.ctime>=?", startime)
  4599. }
  4600. if endtime > 0 {
  4601. db = db.Where("x.ctime<=?", endtime)
  4602. }
  4603. if len(keyword) > 0 {
  4604. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  4605. }
  4606. if orgid > 0 {
  4607. db = db.Where("x.user_org_id = ?", orgid)
  4608. }
  4609. if inventory_status > 0 {
  4610. db = db.Where("checker_status = ?", inventory_status)
  4611. }
  4612. 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
  4613. return adjust, total, err
  4614. }
  4615. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  4616. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  4617. return err
  4618. }
  4619. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  4620. info := models.XtStockAdjustPrice{}
  4621. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4622. return info, err
  4623. }
  4624. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  4625. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  4626. return list, err
  4627. }
  4628. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  4629. info := models.WarehousingInfo{}
  4630. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  4631. return info, err
  4632. }
  4633. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  4634. info := models.WarehousingInfo{}
  4635. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  4636. //return info, err
  4637. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4638. if id > 0 {
  4639. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  4640. }
  4641. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  4642. return info, err
  4643. }
  4644. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4645. info := models.WarehousingInfo{}
  4646. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4647. if id > 0 {
  4648. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  4649. }
  4650. if storehouse_id > 0 {
  4651. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4652. }
  4653. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  4654. return info, err
  4655. }
  4656. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  4657. 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
  4658. return err
  4659. }
  4660. func DeleteStockPrice(id int64) error {
  4661. info := models.XtStockAdjustPrice{}
  4662. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4663. return err
  4664. }
  4665. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  4666. report := models.XtStockReportPrice{}
  4667. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  4668. return report, err
  4669. }
  4670. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  4671. 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
  4672. return err
  4673. }
  4674. func DeleteDamage(id int64) error {
  4675. adjust := models.XtStockReportPrice{}
  4676. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4677. return err
  4678. }
  4679. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  4680. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  4681. return adjust, err
  4682. }
  4683. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  4684. inventory := models.XtStockInventory{}
  4685. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  4686. return inventory, err
  4687. }
  4688. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  4689. 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
  4690. return err
  4691. }
  4692. func DeleteInventory(id int64) error {
  4693. inventory := models.XtStockInventory{}
  4694. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4695. return err
  4696. }
  4697. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  4698. likeKey := "%" + keyword + "%"
  4699. offset := (page - 1) * limit
  4700. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  4701. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4702. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4703. fmt.Println(table, tab)
  4704. if len(keyword) > 0 {
  4705. db = db.Where("x.good_name like ?", likeKey)
  4706. }
  4707. if orgid > 0 {
  4708. db = db.Where("x.user_org_id = ?", orgid)
  4709. }
  4710. if storehouse_id > 0 {
  4711. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4712. }
  4713. 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
  4714. return list, total, err
  4715. }
  4716. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  4717. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  4718. return list, err
  4719. }
  4720. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  4721. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  4722. return list, err
  4723. }
  4724. func DeleteDrugPrice(id int64) error {
  4725. info := models.XtDrugAdjustPrice{}
  4726. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4727. return err
  4728. }
  4729. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  4730. info := models.WarehousingInfo{}
  4731. db := XTReadDB().Model(&info).Where("status = 1")
  4732. if goodid > 0 {
  4733. db = db.Where("good_id = ?", goodid)
  4734. }
  4735. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  4736. return info, err
  4737. }
  4738. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  4739. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4740. return err
  4741. }
  4742. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  4743. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  4744. return err
  4745. }
  4746. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  4747. err := XTWriteDB().Create(&record).Error
  4748. return err
  4749. }
  4750. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  4751. info := models.WarehousingInfo{}
  4752. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  4753. //return info, err
  4754. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4755. if id > 0 {
  4756. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  4757. }
  4758. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  4759. return info, err
  4760. }
  4761. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  4762. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  4763. return info, err
  4764. }
  4765. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  4766. info := models.DrugWarehouseInfo{}
  4767. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  4768. return info, err
  4769. }
  4770. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  4771. 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
  4772. return err
  4773. }
  4774. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  4775. 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
  4776. return err
  4777. }
  4778. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  4779. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=?", id, storehouse_id).Preload("GoodInfo", "status = 1").Find(&info).Error
  4780. return info, err
  4781. }
  4782. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4783. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4784. return err
  4785. }
  4786. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  4787. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  4788. return err
  4789. }
  4790. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  4791. db := XTReadDB().Model(&list).Where(" status = 1")
  4792. if good_id > 0 {
  4793. db = db.Where("good_id = ?", good_id)
  4794. }
  4795. if len(keyword) > 0 {
  4796. likeKey := "%" + keyword + "%"
  4797. db = db.Where("warehousing_order like ?", likeKey)
  4798. }
  4799. if inventory_type > 0 {
  4800. db = db.Where("inventory_type = ?", inventory_type)
  4801. }
  4802. if inventory_type == 0 {
  4803. db = db.Where("inventory_type = 4")
  4804. }
  4805. err = db.Find(&list).Error
  4806. return list, err
  4807. }
  4808. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  4809. flow := models.DrugFlow{}
  4810. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  4811. if err == gorm.ErrRecordNotFound {
  4812. return nil, err
  4813. }
  4814. if err != nil {
  4815. return nil, err
  4816. }
  4817. return &flow, nil
  4818. }
  4819. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  4820. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  4821. return info, err
  4822. }
  4823. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  4824. info := models.XtDrugWarehouseInfo{}
  4825. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  4826. return info, err
  4827. }
  4828. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4829. info := models.WarehousingInfo{}
  4830. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  4831. return info, err
  4832. }
  4833. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  4834. flow := models.VmStockFlow{}
  4835. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  4836. if err == gorm.ErrRecordNotFound {
  4837. return nil, err
  4838. }
  4839. if err != nil {
  4840. return nil, err
  4841. }
  4842. return &flow, nil
  4843. }
  4844. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  4845. info := models.GoodInfo{}
  4846. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  4847. return info, err
  4848. }
  4849. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  4850. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  4851. return err
  4852. }
  4853. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  4854. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  4855. return err
  4856. }
  4857. func UpdateWarehouseInfoByGoodIdOne(info models.WarehousingInfo, id int64) error {
  4858. 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}).Error
  4859. return err
  4860. }
  4861. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  4862. 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
  4863. return err
  4864. }
  4865. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  4866. 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}).Error
  4867. return err
  4868. }
  4869. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  4870. 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}).Error
  4871. return err
  4872. }
  4873. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  4874. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  4875. return err
  4876. }
  4877. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  4878. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4879. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4880. fmt.Println(table)
  4881. if orgid > 0 {
  4882. db = db.Where("x.org_id = ?", orgid)
  4883. }
  4884. err = db.Select("x.id,x.good_id,x.number,x.number,x.product_date,x.expiry_date,x.warehousing_unit,x.dealer,x.manufacturer,t.good_name,t.specification_name,t.packing_unit,t.remark").Joins("left join xt_good_information as t on t.id = x.good_id").Group("x.good_id,x.number").Scan(&info).Error
  4885. return info, err
  4886. }
  4887. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  4888. warehouseInfo := models.XtDrugWarehouseInfo{}
  4889. 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
  4890. return err
  4891. }
  4892. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  4893. err := XTWriteDB().Create(&info).Error
  4894. return err
  4895. }
  4896. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  4897. flow := models.VmStockFlow{}
  4898. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  4899. if err == gorm.ErrRecordNotFound {
  4900. return nil, err
  4901. }
  4902. if err != nil {
  4903. return nil, err
  4904. }
  4905. return &flow, nil
  4906. }
  4907. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  4908. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  4909. return err
  4910. }
  4911. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  4912. 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
  4913. return err
  4914. }
  4915. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  4916. flow := models.DrugWarehouseOutInfo{}
  4917. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  4918. return flow, err
  4919. }
  4920. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  4921. info := models.WarehouseOutInfo{}
  4922. 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
  4923. return info, err
  4924. }
  4925. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  4926. 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
  4927. return err
  4928. }
  4929. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  4930. 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
  4931. return err
  4932. }
  4933. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  4934. info := models.DrugWarehouseOutInfo{}
  4935. 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
  4936. if err == gorm.ErrRecordNotFound {
  4937. return nil, err
  4938. }
  4939. if err != nil {
  4940. return nil, err
  4941. }
  4942. return &info, nil
  4943. }
  4944. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  4945. drugFlow := models.DrugFlow{}
  4946. 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
  4947. if err == gorm.ErrRecordNotFound {
  4948. return nil, err
  4949. }
  4950. if err != nil {
  4951. return nil, err
  4952. }
  4953. return &drugFlow, nil
  4954. }
  4955. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  4956. 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
  4957. return err
  4958. }
  4959. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  4960. 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
  4961. return err
  4962. }
  4963. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  4964. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  4965. return detail, err
  4966. }
  4967. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  4968. detail := models.DrugAutomaticReduceDetail{}
  4969. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  4970. if err == gorm.ErrRecordNotFound {
  4971. return nil, err
  4972. }
  4973. if err != nil {
  4974. return nil, err
  4975. }
  4976. return &detail, nil
  4977. }
  4978. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  4979. 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
  4980. return err
  4981. }
  4982. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  4983. 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
  4984. return err
  4985. }
  4986. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  4987. info := models.DrugWarehouseInfo{}
  4988. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  4989. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  4990. return info, err
  4991. }
  4992. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  4993. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  4994. return err
  4995. }
  4996. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  4997. info := models.DrugWarehouseInfo{}
  4998. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  4999. return err
  5000. }
  5001. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  5002. info := models.DrugCancelStockInfo{}
  5003. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5004. return info, err
  5005. }
  5006. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  5007. info := models.DrugWarehouseInfo{}
  5008. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  5009. return err
  5010. }
  5011. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  5012. info := models.DrugWarehouseInfo{}
  5013. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  5014. return err
  5015. }
  5016. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  5017. 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
  5018. return advice, err
  5019. }
  5020. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5021. 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
  5022. return info, err
  5023. }
  5024. //耗材加库存
  5025. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5026. info := models.GoodInfo{}
  5027. 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
  5028. return info, err
  5029. }
  5030. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5031. info := models.GoodInfo{}
  5032. 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
  5033. 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
  5034. return info, err
  5035. }
  5036. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5037. info := models.GoodInfo{}
  5038. 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
  5039. return info, err
  5040. }
  5041. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5042. info := models.GoodInfo{}
  5043. 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
  5044. 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
  5045. return info, err
  5046. }
  5047. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  5048. info := models.GoodInfo{}
  5049. 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
  5050. 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
  5051. return info, err
  5052. }
  5053. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5054. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5055. return info, err
  5056. }
  5057. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5058. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5059. return info, err
  5060. }
  5061. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  5062. info := models.WarehousingInfo{}
  5063. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  5064. return info, err
  5065. }
  5066. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  5067. info := models.WarehousingInfo{}
  5068. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  5069. return info, err
  5070. }
  5071. //药品加库存
  5072. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5073. info := models.BaseDrugLib{}
  5074. 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
  5075. return info, err
  5076. }
  5077. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5078. info := models.BaseDrugLib{}
  5079. 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
  5080. 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
  5081. return info, err
  5082. }
  5083. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5084. info := models.BaseDrugLib{}
  5085. 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
  5086. return info, err
  5087. }
  5088. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5089. info := models.BaseDrugLib{}
  5090. 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
  5091. 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
  5092. return info, err
  5093. }
  5094. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  5095. info := models.BaseDrugLib{}
  5096. 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
  5097. 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
  5098. return info, err
  5099. }
  5100. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5101. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5102. return info, err
  5103. }
  5104. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5105. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5106. return info, err
  5107. }
  5108. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  5109. info := models.WarehouseOutInfo{}
  5110. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5111. return info, err
  5112. }
  5113. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  5114. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  5115. return out, err
  5116. }
  5117. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  5118. info := models.WarehousingInfo{}
  5119. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5120. return info, err
  5121. }
  5122. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  5123. err = XTReadDB().Where("status = 1").Find(&template).Error
  5124. return template, err
  5125. }
  5126. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  5127. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  5128. return good, err
  5129. }
  5130. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5131. info := models.GoodInfo{}
  5132. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5133. return info, err
  5134. }
  5135. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  5136. db := XTReadDB().Where("status = 1")
  5137. if good_id > 0 {
  5138. db = db.Where("good_id = ?", good_id)
  5139. }
  5140. err = db.Find(&info).Error
  5141. return info, err
  5142. }
  5143. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  5144. db := XTReadDB().Model(&info).Where("status = 1")
  5145. if drug_id > 0 {
  5146. db = db.Where("drug_id = ?", drug_id)
  5147. }
  5148. err = db.Find(&info).Error
  5149. return info, err
  5150. }
  5151. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5152. lib := models.BaseDrugLib{}
  5153. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5154. return lib, err
  5155. }
  5156. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  5157. info := models.Warehousing{}
  5158. 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
  5159. if err == gorm.ErrRecordNotFound {
  5160. return nil, err
  5161. }
  5162. if err != nil {
  5163. return nil, err
  5164. }
  5165. return &info, nil
  5166. }
  5167. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  5168. info := models.Warehousing{}
  5169. 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
  5170. return info, err
  5171. }
  5172. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  5173. out := models.DrugWarehouseOut{}
  5174. 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
  5175. if err == gorm.ErrRecordNotFound {
  5176. return nil, err
  5177. }
  5178. if err != nil {
  5179. return nil, err
  5180. }
  5181. return &out, nil
  5182. }
  5183. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  5184. info := models.DrugWarehouseOut{}
  5185. 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
  5186. return info, err
  5187. }
  5188. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  5189. info := models.DrugWarehouse{}
  5190. 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
  5191. if err == gorm.ErrRecordNotFound {
  5192. return nil, err
  5193. }
  5194. if err != nil {
  5195. return nil, err
  5196. }
  5197. return &info, nil
  5198. }
  5199. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  5200. err := XTWriteDB().Create(&warehouse).Error
  5201. return err
  5202. }
  5203. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  5204. info := models.DrugWarehouse{}
  5205. 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
  5206. return info, err
  5207. }
  5208. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5209. info := models.GoodInfo{}
  5210. 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
  5211. 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
  5212. return info, err
  5213. }
  5214. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5215. info := models.GoodInfo{}
  5216. 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
  5217. 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
  5218. return info, err
  5219. }
  5220. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  5221. err := XTWriteDB().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
  5222. return err
  5223. }
  5224. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  5225. 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
  5226. return err
  5227. }
  5228. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  5229. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  5230. table := XTReadDB().Table("xt_base_drug as x d")
  5231. fmt.Println(table)
  5232. if startime > 0 {
  5233. db = db.Where("x.advice_date >= ?", startime)
  5234. }
  5235. if endtime > 0 {
  5236. db = db.Where("x.advice_date <= ?", endtime)
  5237. }
  5238. if orgid > 0 {
  5239. db = db.Where("x.user_org_id = ?", orgid)
  5240. }
  5241. if len(keyword) > 0 {
  5242. likeKey := "%" + keyword + "%"
  5243. 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)
  5244. }
  5245. offset := (page - 1) * limit
  5246. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  5247. return adviceinfo, total, err
  5248. }
  5249. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  5250. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  5251. table := XTReadDB().Table("xt_base_drug as x d")
  5252. fmt.Println(table)
  5253. if startime > 0 {
  5254. db = db.Where("x.advice_date >= ?", startime)
  5255. }
  5256. if endtime > 0 {
  5257. db = db.Where("x.advice_date <= ?", endtime)
  5258. }
  5259. if orgid > 0 {
  5260. db = db.Where("x.user_org_id = ?", orgid)
  5261. }
  5262. if len(keyword) > 0 {
  5263. likeKey := "%" + keyword + "%"
  5264. 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)
  5265. }
  5266. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  5267. return adviceinfo, total, err
  5268. }
  5269. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  5270. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  5271. return drug, err
  5272. }
  5273. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  5274. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  5275. table := XTReadDB().Table("xt_good_information as d")
  5276. fmt.Println(table)
  5277. if startime > 0 {
  5278. db = db.Where("x.record_date >= ?", startime)
  5279. }
  5280. if endtime > 0 {
  5281. db = db.Where("x.record_date <= ?", endtime)
  5282. }
  5283. if orgid > 0 {
  5284. db = db.Where("x.user_org_id = ?", orgid)
  5285. }
  5286. if len(keyword) > 0 {
  5287. likeKey := "%" + keyword + "%"
  5288. 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")
  5289. }
  5290. offset := (page - 1) * limit
  5291. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  5292. return project, total, err
  5293. }
  5294. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  5295. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  5296. table := XTReadDB().Table("xt_good_information as d")
  5297. fmt.Println(table)
  5298. if startime > 0 {
  5299. db = db.Where("x.record_date >= ?", startime)
  5300. }
  5301. if endtime > 0 {
  5302. db = db.Where("x.record_date <= ?", endtime)
  5303. }
  5304. if orgid > 0 {
  5305. db = db.Where("x.user_org_id = ?", orgid)
  5306. }
  5307. if len(keyword) > 0 {
  5308. likeKey := "%" + keyword + "%"
  5309. 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")
  5310. }
  5311. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  5312. return project, total, err
  5313. }
  5314. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  5315. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  5316. return good, err
  5317. }
  5318. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  5319. setting := models.XtStockSetting{}
  5320. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  5321. if err == gorm.ErrRecordNotFound {
  5322. return nil, err
  5323. }
  5324. if err != nil {
  5325. return nil, err
  5326. }
  5327. return &setting, nil
  5328. }
  5329. func CreateStockSetting(setting models.XtStockSetting) error {
  5330. err := XTWriteDB().Create(&setting).Error
  5331. return err
  5332. }
  5333. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  5334. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  5335. return err
  5336. }
  5337. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  5338. setting := models.XtStockSetting{}
  5339. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  5340. return setting, err
  5341. }
  5342. func GetAutoStockOutById(good_id, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  5343. detail := models.BloodAutomaticReduceDetail{}
  5344. 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
  5345. return detail, err
  5346. }
  5347. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  5348. setting := models.XtAdviceSetting{}
  5349. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  5350. if err == gorm.ErrRecordNotFound {
  5351. return nil, err
  5352. }
  5353. if err != nil {
  5354. return nil, err
  5355. }
  5356. return &setting, nil
  5357. }
  5358. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  5359. err := XTWriteDB().Create(&setting).Error
  5360. return err
  5361. }
  5362. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  5363. 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
  5364. return err
  5365. }
  5366. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  5367. setting := models.XtAdviceSetting{}
  5368. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  5369. return setting, err
  5370. }
  5371. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  5372. config := models.XtPrescriptionConfig{}
  5373. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  5374. if err == gorm.ErrRecordNotFound {
  5375. return nil, err
  5376. }
  5377. if err != nil {
  5378. return nil, err
  5379. }
  5380. return &config, nil
  5381. }
  5382. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  5383. err := XTWriteDB().Create(&prescriptionConfig).Error
  5384. return err
  5385. }
  5386. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  5387. 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
  5388. return err
  5389. }
  5390. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  5391. config := models.XtPrescriptionConfig{}
  5392. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  5393. return config, err
  5394. }
  5395. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  5396. var deliver_number int64 = 0
  5397. var stock_number int64 = 0
  5398. deliver_number = maxNumber
  5399. // 根据先进先出原则,查询最先入库的批次,进行出库
  5400. // 如果没有对应的库存,则报错
  5401. //开启事物
  5402. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  5403. if err != nil {
  5404. return err
  5405. }
  5406. // 将该批次的剩余库存数量转换为拆零数量
  5407. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  5408. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  5409. if stock_number >= deliver_number {
  5410. var maxNumber int64 = 0
  5411. var minNumber int64 = 0
  5412. var stock_max_number int64 = 0
  5413. stock_max_number = warehouse.StockMaxNumber
  5414. maxNumber = deliver_number / drup.MinNumber
  5415. minNumber = deliver_number % drup.MinNumber
  5416. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  5417. minNumber = maxNumber
  5418. }
  5419. if drup.MaxUnit != drup.MinUnit {
  5420. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  5421. return errors.New("库存数量不足")
  5422. }
  5423. }
  5424. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  5425. if warehouse.StockMaxNumber < 0 {
  5426. warehouse.StockMaxNumber = 0
  5427. }
  5428. if warehouse.StockMinNumber < 0 {
  5429. warehouse.StockMinNumber = 0
  5430. }
  5431. warehouse.Mtime = time.Now().Unix()
  5432. if warehouse.StockMinNumber < minNumber {
  5433. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  5434. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  5435. } else {
  5436. if minNumber > 0 {
  5437. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  5438. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5439. } else {
  5440. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  5441. }
  5442. }
  5443. if minNumber == 0 && maxNumber != 1 {
  5444. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5445. }
  5446. }
  5447. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  5448. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  5449. if warehouse.StockMinNumber == 0 {
  5450. warehouse.StockMaxNumber = stock_max_number
  5451. }
  5452. }
  5453. if drup.MaxUnit != drup.MinUnit {
  5454. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  5455. return errors.New("库存数量不足")
  5456. }
  5457. }
  5458. if warehouse.StockMinNumber <= 0 {
  5459. warehouse.StockMinNumber = 0
  5460. }
  5461. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5462. fmt.Println(errThree)
  5463. //查询默认仓库
  5464. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  5465. //查询默认仓库剩余多少库存
  5466. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  5467. var sum_count int64
  5468. var sum_in_count int64
  5469. for _, it := range list {
  5470. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  5471. if it.MaxUnit == baseDrug.MaxUnit {
  5472. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  5473. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  5474. sum_count += it.StockMaxNumber + it.StockMinNumber
  5475. sum_in_count += it.WarehousingCount
  5476. }
  5477. }
  5478. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  5479. return nil
  5480. } else {
  5481. warehouse.StockMaxNumber = 0
  5482. warehouse.StockMinNumber = 0
  5483. warehouse.Mtime = time.Now().Unix()
  5484. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  5485. fmt.Println(errThree)
  5486. //查询默认仓库
  5487. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  5488. //查询默认仓库剩余多少库存
  5489. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  5490. var sum_count int64
  5491. var sum_in_count int64
  5492. for _, it := range list {
  5493. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  5494. if it.MaxUnit == baseDrug.MaxUnit {
  5495. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  5496. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  5497. sum_count += it.StockMaxNumber + it.StockMinNumber
  5498. sum_in_count += it.WarehousingCount
  5499. }
  5500. }
  5501. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  5502. if errThree != nil {
  5503. return errThree
  5504. }
  5505. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  5506. prescribingNumber_two_temp := deliver_number - stock_number
  5507. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  5508. }
  5509. return
  5510. }
  5511. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  5512. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  5513. return err
  5514. }
  5515. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  5516. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  5517. return err
  5518. }
  5519. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  5520. config := models.DrugOutConfig{}
  5521. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  5522. if err == gorm.ErrRecordNotFound {
  5523. return nil, err
  5524. }
  5525. if err != nil {
  5526. return nil, err
  5527. }
  5528. return &config, nil
  5529. }
  5530. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  5531. err := XTWriteDB().Create(&config).Error
  5532. return err
  5533. }
  5534. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  5535. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  5536. return err
  5537. }
  5538. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  5539. config := models.DrugOutConfig{}
  5540. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  5541. return config, err
  5542. }