stock_service.go 282KB

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