stock_service.go 399KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "github.com/jinzhu/gorm"
  10. )
  11. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  12. err := writeDb.Create(&dealer).Error
  13. return err, dealer
  14. }
  15. func ModifyDealer(dealer *models.Dealer) error {
  16. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  17. "dealer_name": dealer.DealerName,
  18. "contact": dealer.Contact,
  19. "contact_phone": dealer.ContactPhone,
  20. "platform_number": dealer.PlatformNumber,
  21. "email": dealer.Email,
  22. "contact_address": dealer.ContactAddress,
  23. "modifier": dealer.Modifier,
  24. "mtime": time.Now().Unix(),
  25. "remark": dealer.Remark,
  26. "wubi": dealer.WuBi,
  27. "pinyin": dealer.PinYin,
  28. }).Error
  29. return err
  30. }
  31. func FindAllDealerTotal(orgId int64) (total int64) {
  32. db := readDb.Model(&models.Dealer{})
  33. db = db.Where("org_id = ? ", orgId).Count(&total)
  34. return
  35. }
  36. // 获取经销商的名称
  37. func FindAllDealerName(orgId int64) (tmp []*models.Dealer, err error) {
  38. err = readDb.Model(&models.Dealer{}).Where("org_id = ? and status = 1", orgId).Find(&tmp).Error
  39. return
  40. }
  41. func FindAllDealerList(orgId int64, page int64, limit, key int64) (list []*models.Dealer, total int64, err error) {
  42. offset := (page - 1) * limit
  43. db := readDb.Model(&models.Dealer{})
  44. db = db.Where("org_id = ? AND status = 1", orgId)
  45. if key != 0 {
  46. db = db.Where("id = ? ", key)
  47. offset = 0
  48. limit = 10
  49. }
  50. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  51. return
  52. }
  53. func DeleteDealerById(id int64) error {
  54. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  55. return err
  56. }
  57. func FindDealerById(id int64) (*models.Dealer, error) {
  58. dealer := &models.Dealer{}
  59. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  60. return dealer, err
  61. }
  62. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  63. err := writeDb.Create(&manufacturer).Error
  64. return err, manufacturer
  65. }
  66. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  67. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  68. "manufacturer_name": manufacturer.ManufacturerName,
  69. "contact": manufacturer.Contact,
  70. "contact_phone": manufacturer.ContactPhone,
  71. "platform_number": manufacturer.PlatformNumber,
  72. "email": manufacturer.Email,
  73. "contact_address": manufacturer.ContactAddress,
  74. "modifier": manufacturer.Modifier,
  75. "mtime": time.Now().Unix(),
  76. "remark": manufacturer.Remark,
  77. "wubi": manufacturer.WuBi,
  78. "pinyin": manufacturer.PinYin,
  79. }).Error
  80. return err
  81. }
  82. // 获取生产厂商名称
  83. func FindAllManufacturerName(orgid int64) (tmp []*models.Manufacturer, err error) {
  84. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? and status = 1", orgid).Find(&tmp).Error
  85. return
  86. }
  87. func FindAllManufacturerList(orgId int64, page int64, limit, key int64) (list []*models.Manufacturer, total int64, err error) {
  88. offset := (page - 1) * limit
  89. db := readDb.Model(&models.Manufacturer{})
  90. db = db.Where("org_id = ? AND status = 1", orgId)
  91. if key != 0 {
  92. db = db.Where("id = ?", key)
  93. offset = 0
  94. limit = 10
  95. }
  96. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  97. return
  98. }
  99. func DeleteManufacturerById(id int64) error {
  100. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  101. return err
  102. }
  103. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  104. dealer := &models.Manufacturer{}
  105. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  106. return dealer, err
  107. }
  108. func FindManufacturerTotal(orgId int64) (total int64) {
  109. db := readDb.Model(&models.Manufacturer{})
  110. db = db.Where("org_id = ?", orgId).Count(&total)
  111. return
  112. }
  113. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  114. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  115. return dealers, err
  116. }
  117. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  118. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  119. return manufacturer, err
  120. }
  121. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  122. db := readDb.Model(&models.GoodsType{})
  123. db = db.Where("org_id = ?", orgId).Count(&total)
  124. return
  125. }
  126. func GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  127. goodsType := models.GoodsType{}
  128. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  129. if err == gorm.ErrRecordNotFound {
  130. return nil, err
  131. }
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &goodsType, nil
  136. }
  137. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  138. goodsType := models.GoodsType{}
  139. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  140. if err == gorm.ErrRecordNotFound {
  141. return nil, err
  142. }
  143. if err != nil {
  144. return nil, err
  145. }
  146. return &goodsType, nil
  147. }
  148. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  149. err := writeDb.Create(&goodType).Error
  150. return err, goodType
  151. }
  152. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  153. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  154. "modifier": goodType.Modifier,
  155. "mtime": time.Now().Unix(),
  156. "remark": goodType.Remark,
  157. "type_name": goodType.TypeName,
  158. "out_stock": goodType.OutStock,
  159. "stock_attribute": goodType.StockAttribute,
  160. }).Error
  161. return err, goodType
  162. }
  163. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  164. offset := (page - 1) * limit
  165. db := readDb.Model(&models.GoodsType{})
  166. db = db.Where("org_id=? AND status = 1", orgId)
  167. if len(keyword) > 0 {
  168. likeKey := "%" + keyword + "%"
  169. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  170. }
  171. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  172. return
  173. }
  174. func DeleteGoodTypeById(id int64, operateId int64) error {
  175. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  176. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  177. return err
  178. }
  179. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  180. goodType := &models.GoodsType{}
  181. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  182. return goodType, err
  183. }
  184. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  185. db := readDb.Model(&models.GoodInfo{})
  186. db = db.Where("org_id = ?", orgId).Count(&total)
  187. return
  188. }
  189. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  190. err := writeDb.Create(&goodInfo).Error
  191. return err, goodInfo
  192. }
  193. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  194. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  195. "good_type_id": goodInfo.GoodTypeId,
  196. "modifier": goodInfo.Modifier,
  197. "mtime": time.Now().Unix(),
  198. "remark": goodInfo.Remark,
  199. "specification_name": goodInfo.SpecificationName,
  200. "good_unit": goodInfo.GoodUnit,
  201. "buy_price": goodInfo.BuyPrice,
  202. "sell_price": goodInfo.SellPrice,
  203. "manufacturer": goodInfo.Manufacturer,
  204. "dealer": goodInfo.Dealer,
  205. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  206. "stock_warn_count": goodInfo.StockWarnCount,
  207. "is_reuse": goodInfo.IsReuse,
  208. "good_name": goodInfo.GoodName,
  209. "pinyin": goodInfo.Pinyin,
  210. "wubi": goodInfo.Wubi,
  211. "good_kind": goodInfo.GoodKind,
  212. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  213. "retail_price": goodInfo.PackingPrice,
  214. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  215. "is_special_diseases": goodInfo.IsSpecialDiseases,
  216. "is_record": goodInfo.IsRecord,
  217. "statistics_category": goodInfo.StatisticsCategory,
  218. "good_status": goodInfo.GoodStatus,
  219. "default_count": goodInfo.DefaultCount,
  220. "sign": goodInfo.Sign,
  221. "is_default": goodInfo.IsDefault,
  222. "is_charge_use": goodInfo.IsChargeUse,
  223. "is_charge_predict": goodInfo.IsChargePredict,
  224. "is_statistics_work": goodInfo.IsStatisticsWork,
  225. "sort": goodInfo.Sort,
  226. "is_doctor_use": goodInfo.IsDoctorUse,
  227. "agent": goodInfo.Agent,
  228. "good_number": goodInfo.GoodNumber,
  229. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  230. "special_medical": goodInfo.SpecialMedical,
  231. "production_type": goodInfo.ProductionType,
  232. "min_number": goodInfo.MinNumber,
  233. "packing_unit": goodInfo.PackingUnit,
  234. "packing_price": goodInfo.PackingPrice,
  235. "default_count_unit": goodInfo.DefaultCountUnit,
  236. "min_unit": goodInfo.MinUnit,
  237. "register_number": goodInfo.RegisterNumber,
  238. "provinces_code": goodInfo.ProvincesCode,
  239. "is_user": goodInfo.IsUser,
  240. "number": goodInfo.Number,
  241. "is_warehouse": goodInfo.IsWarehouse,
  242. "bbx01": goodInfo.Bbx01,
  243. "bby01": goodInfo.Bby01,
  244. "zuobiao_id": goodInfo.Bbx01,
  245. "first_letter": goodInfo.FirstLetter,
  246. "is_show": goodInfo.IsShow,
  247. }).Error
  248. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  249. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  250. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  251. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  254. return err, goodInfo
  255. }
  256. 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) {
  257. offset := (page - 1) * limit
  258. db := readDb.Model(&models.GoodInfo{})
  259. db = db.Where("org_id = ? AND status = 1", orgId)
  260. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  261. if len(keyword) > 0 {
  262. likeKey := "%" + keyword + "%"
  263. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  264. if is_use > 0 {
  265. if is_use == 2 {
  266. db = db.Where("find_in_set('停用',good_status) = 0")
  267. } else {
  268. db = db.Where("find_in_set('停用',good_status) > 0")
  269. }
  270. }
  271. if is_charge > 0 {
  272. if is_charge == 1 {
  273. db = db.Where("find_in_set('收费',good_status) > 0")
  274. } else {
  275. db = db.Where("find_in_set('收费',good_status) = 0")
  276. }
  277. }
  278. if good_kind > 0 {
  279. db = db.Where(" good_kind = ?", good_kind)
  280. }
  281. if is_mark > 0 {
  282. db = db.Where("is_mark = ?", is_mark)
  283. }
  284. if manufacturer > 0 {
  285. db = db.Where("manufacturer = ?", manufacturer)
  286. }
  287. if is_type > 0 {
  288. db = db.Where("good_type_id = ?", is_type)
  289. }
  290. } else {
  291. if is_use > 0 {
  292. if is_use == 2 {
  293. db = db.Where("find_in_set('停用',good_status) = 0")
  294. } else {
  295. db = db.Where("find_in_set('停用',good_status) > 0")
  296. }
  297. }
  298. if is_charge > 0 {
  299. if is_charge == 1 {
  300. db = db.Where("find_in_set('收费',good_status) > 0")
  301. } else {
  302. db = db.Where("find_in_set('收费',good_status) = 0")
  303. }
  304. }
  305. if good_kind > 0 {
  306. db = db.Where(" good_kind = ?", good_kind)
  307. }
  308. if is_mark > 0 {
  309. db = db.Where("is_mark = ?", is_mark)
  310. }
  311. if manufacturer > 0 {
  312. db = db.Where("manufacturer = ?", manufacturer)
  313. }
  314. if is_type > 0 {
  315. db = db.Where("good_type_id = ?", is_type)
  316. }
  317. }
  318. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  319. return
  320. }
  321. func DeleteGoodInfoById(id int64, operateId int64) error {
  322. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  323. return err
  324. }
  325. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  326. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  327. return list, err
  328. }
  329. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  330. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  331. return list, err
  332. }
  333. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  334. goodInfo := &models.GoodInfo{}
  335. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  336. return goodInfo, err
  337. }
  338. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  339. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  340. return goodType, err
  341. }
  342. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  343. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  344. return goodType, err
  345. }
  346. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  347. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 and is_warehouse = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  348. return goodInfo, err
  349. }
  350. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  351. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  352. return goodInfo, err
  353. }
  354. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  355. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  356. return total, err
  357. }
  358. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  359. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  360. return total, err
  361. }
  362. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  363. err := writeDb.Create(&warehouse).Error
  364. return err
  365. }
  366. func UpdateWarehouse(id int64, storehouse_id int64) error {
  367. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  368. return err
  369. }
  370. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  371. err := writeDb.Create(&warehouse).Error
  372. return err
  373. }
  374. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  375. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  376. return err
  377. }
  378. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  379. err := writeDb.Create(&salesReturn).Error
  380. return err
  381. }
  382. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  383. err := writeDb.Create(&salesReturn).Error
  384. return err
  385. }
  386. func UpDateGoodReturnStatus(ids []string) {
  387. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  388. }
  389. func UpDateCanCelStockStatus(ids []string) {
  390. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  391. }
  392. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  393. if len(warehousingInfo) > 0 {
  394. utx := writeDb.Begin()
  395. if len(warehousingInfo) > 0 {
  396. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number,packing_price,storehouse_id) VALUES "
  397. insertParams := make([]string, 0)
  398. insertData := make([]interface{}, 0)
  399. for _, info := range warehousingInfo {
  400. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  401. insertData = append(insertData, info.WarehousingId)
  402. insertData = append(insertData, info.GoodId)
  403. insertData = append(insertData, info.GoodTypeId)
  404. insertData = append(insertData, info.Number)
  405. insertData = append(insertData, info.ProductDate)
  406. insertData = append(insertData, info.ExpiryDate)
  407. insertData = append(insertData, info.WarehousingCount)
  408. insertData = append(insertData, info.Price)
  409. insertData = append(insertData, info.TotalPrice)
  410. insertData = append(insertData, info.Dealer)
  411. insertData = append(insertData, info.Manufacturer)
  412. insertData = append(insertData, info.Remark)
  413. insertData = append(insertData, info.Ctime)
  414. insertData = append(insertData, info.Mtime)
  415. insertData = append(insertData, info.Status)
  416. insertData = append(insertData, info.OrgId)
  417. insertData = append(insertData, info.WarehousingOrder)
  418. insertData = append(insertData, info.Type)
  419. insertData = append(insertData, info.WarehousingCount)
  420. insertData = append(insertData, info.LicenseNumber)
  421. insertData = append(insertData, info.PackingPrice)
  422. insertData = append(insertData, info.StorehouseId)
  423. }
  424. thisSQL += strings.Join(insertParams, ", ")
  425. err = utx.Exec(thisSQL, insertData...).Error
  426. if err != nil {
  427. utx.Rollback()
  428. return
  429. }
  430. }
  431. utx.Commit()
  432. }
  433. return
  434. }
  435. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  436. if len(warehousingInfo) > 0 {
  437. utx := writeDb.Begin()
  438. if len(warehousingInfo) > 0 {
  439. 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 "
  440. insertParams := make([]string, 0)
  441. insertData := make([]interface{}, 0)
  442. for _, info := range warehousingInfo {
  443. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  444. insertData = append(insertData, info.WarehousingId)
  445. insertData = append(insertData, info.DrugId)
  446. insertData = append(insertData, info.Number)
  447. insertData = append(insertData, info.ProductDate)
  448. insertData = append(insertData, info.ExpiryDate)
  449. insertData = append(insertData, info.WarehousingCount)
  450. insertData = append(insertData, info.Price)
  451. insertData = append(insertData, info.TotalPrice)
  452. insertData = append(insertData, info.Dealer)
  453. insertData = append(insertData, info.Manufacturer)
  454. insertData = append(insertData, info.Remark)
  455. insertData = append(insertData, info.Ctime)
  456. insertData = append(insertData, info.Mtime)
  457. insertData = append(insertData, info.Status)
  458. insertData = append(insertData, info.OrgId)
  459. insertData = append(insertData, info.WarehousingOrder)
  460. insertData = append(insertData, info.Type)
  461. insertData = append(insertData, info.RetailPrice)
  462. insertData = append(insertData, info.RetailTotalPrice)
  463. insertData = append(insertData, info.StockMaxNumber)
  464. insertData = append(insertData, info.MaxUnit)
  465. insertData = append(insertData, info.MinUnit)
  466. insertData = append(insertData, info.BatchNumber)
  467. insertData = append(insertData, info.StockMinNumber)
  468. insertData = append(insertData, info.StorehouseId)
  469. }
  470. thisSQL += strings.Join(insertParams, ", ")
  471. err = utx.Exec(thisSQL, insertData...).Error
  472. if err != nil {
  473. utx.Rollback()
  474. return
  475. }
  476. }
  477. utx.Commit()
  478. }
  479. return
  480. }
  481. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  482. if len(warehousingInfo) > 0 {
  483. utx := writeDb.Begin()
  484. if len(warehousingInfo) > 0 {
  485. thisSQL := "INSERT INTO xt_drug_warehouse_info (warehousing_id, drug_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,retail_price,retail_total_price,stock_max_number,max_unit,min_unit,batch_number,stock_min_number,supply_warehouse_id,supply_warehouse_detail_info,storehouse_id,is_check) VALUES "
  486. insertParams := make([]string, 0)
  487. insertData := make([]interface{}, 0)
  488. for _, info := range warehousingInfo {
  489. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  490. insertData = append(insertData, info.WarehousingId)
  491. insertData = append(insertData, info.DrugId)
  492. insertData = append(insertData, info.Number)
  493. insertData = append(insertData, info.ProductDate)
  494. insertData = append(insertData, info.ExpiryDate)
  495. insertData = append(insertData, info.WarehousingCount)
  496. insertData = append(insertData, info.Price)
  497. insertData = append(insertData, info.TotalPrice)
  498. insertData = append(insertData, info.Dealer)
  499. insertData = append(insertData, info.Manufacturer)
  500. insertData = append(insertData, info.Remark)
  501. insertData = append(insertData, info.Ctime)
  502. insertData = append(insertData, info.Mtime)
  503. insertData = append(insertData, info.Status)
  504. insertData = append(insertData, info.OrgId)
  505. insertData = append(insertData, info.WarehousingOrder)
  506. insertData = append(insertData, info.Type)
  507. insertData = append(insertData, info.RetailPrice)
  508. insertData = append(insertData, info.RetailTotalPrice)
  509. insertData = append(insertData, info.StockMaxNumber)
  510. insertData = append(insertData, info.MaxUnit)
  511. insertData = append(insertData, info.MinUnit)
  512. insertData = append(insertData, info.BatchNumber)
  513. insertData = append(insertData, info.StockMinNumber)
  514. insertData = append(insertData, info.SupplyWarehouseId)
  515. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  516. insertData = append(insertData, info.StorehouseId)
  517. insertData = append(insertData, info.IsCheck)
  518. }
  519. thisSQL += strings.Join(insertParams, ", ")
  520. err = utx.Exec(thisSQL, insertData...).Error
  521. if err != nil {
  522. utx.Rollback()
  523. return
  524. }
  525. }
  526. utx.Commit()
  527. }
  528. return
  529. }
  530. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  531. var total float64
  532. var min_number int64
  533. for _, item := range warehousingInfo {
  534. //查询刚入库耗材的库存
  535. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  536. //更新药品库的总量
  537. info, _ := FindeDrugInfo(item.DrugId)
  538. min_number = count.StockMaxNumber * info.MinNumber
  539. str := strconv.FormatInt(min_number, 10)
  540. minFloat, _ := strconv.ParseFloat(str, 64)
  541. total = info.Total + minFloat
  542. lib := models.BaseDrugLib{
  543. Total: total,
  544. }
  545. UpdateBaseDrug(&lib, item.ID)
  546. }
  547. return err
  548. }
  549. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  550. info := models.XtDrugWarehouseInfo{}
  551. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  552. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  553. return info, err
  554. }
  555. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  556. lib := models.BaseDrugLib{}
  557. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  558. return lib, err
  559. }
  560. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  561. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  562. return err
  563. }
  564. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  565. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  566. return err
  567. }
  568. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  569. info := models.DrugWarehouseInfo{}
  570. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  571. return info, err
  572. }
  573. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  574. out := models.DrugWarehouseOut{}
  575. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  576. return out, err
  577. }
  578. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  579. if len(drugflow) > 0 {
  580. utx := writeDb.Begin()
  581. if len(drugflow) > 0 {
  582. 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 "
  583. insertParams := make([]string, 0)
  584. insertData := make([]interface{}, 0)
  585. for _, info := range drugflow {
  586. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  587. insertData = append(insertData, info.WarehousingId)
  588. insertData = append(insertData, info.DrugId)
  589. insertData = append(insertData, info.Number)
  590. insertData = append(insertData, info.BatchNumber)
  591. insertData = append(insertData, info.Count)
  592. insertData = append(insertData, info.UserOrgId)
  593. insertData = append(insertData, info.PatientId)
  594. insertData = append(insertData, info.SystemTime)
  595. insertData = append(insertData, info.ConsumableType)
  596. insertData = append(insertData, info.IsSys)
  597. insertData = append(insertData, info.WarehousingOrder)
  598. insertData = append(insertData, info.WarehouseOutId)
  599. insertData = append(insertData, info.WarehouseOutOrderNumber)
  600. insertData = append(insertData, info.IsEdit)
  601. insertData = append(insertData, info.CancelStockId)
  602. insertData = append(insertData, info.CancelOrderNumber)
  603. insertData = append(insertData, info.Manufacturer)
  604. insertData = append(insertData, info.Dealer)
  605. insertData = append(insertData, info.Creator)
  606. insertData = append(insertData, info.UpdateCreator)
  607. insertData = append(insertData, info.Status)
  608. insertData = append(insertData, info.Ctime)
  609. insertData = append(insertData, info.Mtime)
  610. insertData = append(insertData, info.Price)
  611. insertData = append(insertData, info.WarehousingDetailId)
  612. insertData = append(insertData, info.WarehouseOutDetailId)
  613. insertData = append(insertData, info.CancelOutDetailId)
  614. insertData = append(insertData, info.ExpireDate)
  615. insertData = append(insertData, info.ProductDate)
  616. insertData = append(insertData, info.MaxUnit)
  617. insertData = append(insertData, info.MinUnit)
  618. insertData = append(insertData, info.StorehouseId)
  619. }
  620. thisSQL += strings.Join(insertParams, ", ")
  621. err = utx.Exec(thisSQL, insertData...).Error
  622. if err != nil {
  623. utx.Rollback()
  624. return
  625. }
  626. }
  627. utx.Commit()
  628. }
  629. return
  630. }
  631. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  632. info := models.DrugFlow{}
  633. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  634. if err == gorm.ErrRecordNotFound {
  635. return nil, err
  636. }
  637. if err != nil {
  638. return nil, err
  639. }
  640. return &info, nil
  641. }
  642. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  643. info := models.XtDrugWarehouseInfo{}
  644. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  645. return info, err
  646. }
  647. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  648. warehouseInfo := models.XtDrugWarehouseInfo{}
  649. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  650. return err
  651. }
  652. func CreateDrugFlowOne(flow models.DrugFlow) error {
  653. ut := writeDb.Begin()
  654. err := ut.Create(&flow).Error
  655. if err != nil {
  656. ut.Rollback()
  657. return err
  658. }
  659. ut.Commit()
  660. return err
  661. }
  662. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  663. err := tx.Create(&flow).Error
  664. if err != nil {
  665. tx.Rollback()
  666. return err
  667. }
  668. return err
  669. }
  670. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  671. detail := models.DrugAutomaticReduceDetail{}
  672. 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
  673. 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
  674. return detail, err
  675. }
  676. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  677. flow := models.DrugFlow{}
  678. err := XTWriteDB().Model(&flow).Where("patient_id =? and warehouse_out_order_number = ? and drug_id = ? and status = 1 and advice_id =?", patientid, warehouse_out_number, drug_id, advice_id).Updates(map[string]interface{}{"is_read": 1}).Error
  679. return flow, err
  680. }
  681. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  682. tx := XTWriteDB().Begin()
  683. err := tx.Create(&flow).Error
  684. if err != nil {
  685. tx.Rollback()
  686. return err
  687. }
  688. tx.Commit()
  689. return err
  690. }
  691. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  692. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  693. return info, err
  694. }
  695. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  696. info := models.WarehousingInfo{}
  697. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  698. return info, err
  699. }
  700. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  701. err := XTWriteDB().Create(&info).Error
  702. return err
  703. }
  704. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  705. if storehouse_id == 0 {
  706. 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
  707. }
  708. if storehouse_id > 0 {
  709. 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
  710. }
  711. return info, err
  712. }
  713. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  714. if storehouse_id == 0 {
  715. err = tx.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0", good_id, good_type_id).Order("ctime").First(&info).Error
  716. if err != gorm.ErrRecordNotFound {
  717. if err != nil {
  718. tx.Rollback()
  719. return info, err
  720. }
  721. }
  722. }
  723. if storehouse_id > 0 {
  724. err = tx.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, good_type_id, storehouse_id).Order("ctime").First(&info).Error
  725. if err != gorm.ErrRecordNotFound {
  726. if err != nil {
  727. tx.Rollback()
  728. return info, err
  729. }
  730. }
  731. }
  732. return info, err
  733. }
  734. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  735. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  736. if good_id > 0 {
  737. db = db.Where("x.good_id = ?", good_id)
  738. }
  739. if good_type_id > 0 {
  740. db = db.Where("x.good_type_id = ?", good_type_id)
  741. }
  742. if storehouse_id > 0 {
  743. db = db.Where("x.storehouse_id = ?", storehouse_id)
  744. }
  745. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  746. return info, err
  747. }
  748. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  749. info := models.WarehousingInfo{}
  750. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  751. return info, err
  752. }
  753. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  754. info := models.WarehousingInfo{}
  755. var err error
  756. if storehouse_id == 0 {
  757. err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and is_check = 1", goodId).Find(&info).Error
  758. }
  759. if storehouse_id > 0 {
  760. err = XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and storehouse_id = ? and is_check = 1", goodId, storehouse_id).Find(&info).Error
  761. }
  762. return info, err
  763. }
  764. //药品先进先出,找出最先入库的批次
  765. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  766. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").First(&info).Error
  767. return info, err
  768. }
  769. func FindLastDrugWarehousingInfoByIDTwo(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  770. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").Last(&info).Error
  771. return info, err
  772. }
  773. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  774. err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").First(&info).Error
  775. if err != gorm.ErrRecordNotFound {
  776. if err != nil {
  777. tx.Rollback()
  778. return info, err
  779. }
  780. }
  781. return info, err
  782. }
  783. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  784. if is_source == 1 {
  785. 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
  786. }
  787. if is_source == 2 || is_source == 0 {
  788. 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
  789. }
  790. return info, err
  791. }
  792. // 出库时先进先出,找出最先入库的批次
  793. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  794. 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
  795. return info, err
  796. }
  797. //
  798. //// 出库时先进先出,找出最先入库的批次
  799. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  800. // 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
  801. // return info, err
  802. //}
  803. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  804. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  805. return info, err
  806. }
  807. func FindAllWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, check_type int64) (list []*models.Warehousing, total int64, err error) {
  808. offset := (page - 1) * limit
  809. db := readDb.Model(&models.Warehousing{})
  810. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  811. if len(keywords) > 0 {
  812. likeKey := "%" + keywords + "%"
  813. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  814. 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")
  815. }
  816. if startTime > 0 {
  817. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  818. }
  819. if endTime > 0 {
  820. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  821. }
  822. if storehouse_id > 0 {
  823. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  824. }
  825. if check_type == 0 {
  826. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  827. }
  828. if check_type > 0 {
  829. db = db.Where("xt_warehouse.is_check = ?", check_type)
  830. }
  831. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  832. return
  833. }
  834. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  835. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  836. if orgid > 0 {
  837. db = db.Where("org_id = ?", orgid)
  838. }
  839. if len(keyword) > 0 {
  840. likeKey := "%" + keyword + "%"
  841. db = db.Where("good_name like ?", likeKey)
  842. }
  843. err = db.Find(&goodInfo).Error
  844. return goodInfo, err
  845. }
  846. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  847. db := XTReadDB().Model(&info).Where("status =1")
  848. if len(good_id) > 0 {
  849. db = db.Where("good_id in (?)", good_id)
  850. }
  851. if startime > 0 {
  852. db = db.Where("ctime >=?", startime)
  853. }
  854. if endtime > 0 {
  855. db = db.Where("ctime<=?", endtime)
  856. }
  857. if orgid > 0 {
  858. db = db.Where("org_id = ?", orgid)
  859. }
  860. if storehouse_id > 0 {
  861. db = db.Where("storehouse_id = ?", storehouse_id)
  862. }
  863. err = db.Find(&info).Error
  864. return info, err
  865. }
  866. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  867. db := XTReadDB().Model(&info).Where("status =1")
  868. if len(good_id) > 0 {
  869. db = db.Where("good_id in (?)", good_id)
  870. }
  871. if startime > 0 {
  872. db = db.Where("ctime >=?", startime)
  873. }
  874. if endtime > 0 {
  875. db = db.Where("ctime<=?", endtime)
  876. }
  877. if orgid > 0 {
  878. db = db.Where("org_id = ?", orgid)
  879. }
  880. if storehouse_id > 0 {
  881. db = db.Where("storehouse_id = ?", storehouse_id)
  882. }
  883. err = db.Find(&info).Error
  884. return info, err
  885. }
  886. type VMDrugWarehouse struct {
  887. ID int64 `gorm:"column:id" json:"id" form:"id"`
  888. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  889. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  890. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  891. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  892. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  893. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  894. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  895. Status int64 `gorm:"column:status" json:"status" form:"status"`
  896. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  897. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  898. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  899. Type int64 `gorm:"column:type" json:"type" form:"type"`
  900. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  901. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  902. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  903. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  904. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  905. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  906. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  907. }
  908. func (VMDrugWarehouse) TableName() string {
  909. return "xt_drug_warehouse"
  910. }
  911. type VMDrugWarehouseInfo struct {
  912. ID int64 `gorm:"column:id" json:"id" form:"id"`
  913. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  914. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  915. Number string `gorm:"column:number" json:"number" form:"number"`
  916. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  917. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  918. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  919. Price float64 `gorm:"column:price" json:"price" form:"price"`
  920. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  921. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  922. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  923. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  924. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  925. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  926. Status int64 `gorm:"column:status" json:"status" form:"status"`
  927. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  928. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  929. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  930. Type int64 `gorm:"column:type" json:"type" form:"type"`
  931. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  932. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  933. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  934. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  935. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  936. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  937. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  938. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  939. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  940. }
  941. func (VMDrugWarehouseInfo) TableName() string {
  942. return "xt_drug_warehouse_info"
  943. }
  944. type Drug struct {
  945. ID int64 `gorm:"column:id" json:"id" form:"id"`
  946. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  947. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  948. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  949. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  950. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  951. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  952. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  953. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  954. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  955. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  956. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  957. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  958. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  959. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  960. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  961. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  962. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  963. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  964. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  965. Number string `gorm:"column:number" json:"number" form:"number"`
  966. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  967. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  968. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  969. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  970. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  971. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  972. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  973. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  974. Code string `gorm:"column:code" json:"code" form:"code"`
  975. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  976. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  977. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  978. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  979. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  980. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  981. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  982. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  983. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  984. Label int64 `gorm:"column:label" json:"label" form:"label"`
  985. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  986. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  987. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  988. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  989. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  990. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  991. Status int64 `gorm:"column:status" json:"status" form:"status"`
  992. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  993. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  994. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  995. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  996. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  997. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  998. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  999. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  1000. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  1001. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  1002. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  1003. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  1004. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  1005. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1006. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1007. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1008. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1009. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1010. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1011. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1012. }
  1013. func (Drug) TableName() string {
  1014. return "xt_base_drug"
  1015. }
  1016. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1017. db := readDb.Model(&VMDrugWarehouse{})
  1018. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1019. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1020. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1021. if len(keywords) > 0 {
  1022. likeKey := "%" + keywords + "%"
  1023. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1024. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1025. 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")
  1026. }
  1027. if startTime > 0 {
  1028. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1029. }
  1030. if endTime > 0 {
  1031. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1032. }
  1033. db = db.Count(&total)
  1034. offset := (page - 1) * limit
  1035. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1036. return
  1037. }
  1038. func FindAllDrugWarehousingListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, check_type int64) (list []*VMDrugWarehouse, total int64, err error) {
  1039. db := readDb.Model(&VMDrugWarehouse{})
  1040. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1041. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1042. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1043. if len(keywords) > 0 {
  1044. likeKey := "%" + keywords + "%"
  1045. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1046. 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")
  1047. }
  1048. if startTime > 0 {
  1049. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1050. }
  1051. if endTime > 0 {
  1052. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1053. }
  1054. if storehouse_id > 0 {
  1055. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1056. }
  1057. if check_type > 0 {
  1058. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1059. }
  1060. db = db.Count(&total)
  1061. offset := (page - 1) * limit
  1062. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1063. return
  1064. }
  1065. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1066. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1067. return list, err
  1068. }
  1069. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1070. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1071. return list, err
  1072. }
  1073. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1074. 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
  1075. return list, err
  1076. }
  1077. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1078. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1079. return list, err
  1080. }
  1081. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1082. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1083. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1084. fmt.Println(table)
  1085. if len(id) > 0 {
  1086. db = db.Where("x.warehousing_id in(?)", id)
  1087. }
  1088. if org_id > 0 {
  1089. db = db.Where("x.org_id = ?", org_id)
  1090. }
  1091. err = db.Select("x.id,x.max_unit as limax_unit,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.stock_max_number,x.stock_min_number,x.price,x.remark,x.batch_number,x.batch_number,x.manufacturer,x.dealer,t.drug_name,t.last_price,t.dose_unit,t.dose,t.min_number,t.min_unit,t.max_unit,t.drug_type").Joins("left join xt_base_drug as t on t.id = x.drug_id").Scan(&list).Error
  1092. return list, err
  1093. }
  1094. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1095. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1096. return warehousing, err
  1097. }
  1098. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1099. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1100. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1101. return warehousing, err
  1102. }
  1103. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1104. 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
  1105. return warehousing, err
  1106. }
  1107. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1108. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1109. if len(id) > 0 {
  1110. db = db.Where("x.id in(?)", id)
  1111. }
  1112. if org_id > 0 {
  1113. db = db.Where("x.org_id = ?", org_id)
  1114. }
  1115. 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
  1116. return warehousing, err
  1117. }
  1118. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1119. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1120. // return warehousing, err
  1121. //
  1122. //}
  1123. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1124. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1125. return
  1126. }
  1127. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1128. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1129. return
  1130. }
  1131. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1132. if len(salesReturnInfo) > 0 {
  1133. utx := writeDb.Begin()
  1134. if len(salesReturnInfo) > 0 {
  1135. 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 "
  1136. insertParams := make([]string, 0)
  1137. insertData := make([]interface{}, 0)
  1138. for _, info := range salesReturnInfo {
  1139. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1140. insertData = append(insertData, info.GoodId)
  1141. insertData = append(insertData, info.SalesReturnId)
  1142. insertData = append(insertData, info.GoodTypeId)
  1143. insertData = append(insertData, info.Count)
  1144. insertData = append(insertData, info.Price)
  1145. insertData = append(insertData, info.Total)
  1146. insertData = append(insertData, info.Ctime)
  1147. insertData = append(insertData, info.Status)
  1148. insertData = append(insertData, info.OrgId)
  1149. insertData = append(insertData, info.OrderNumber)
  1150. insertData = append(insertData, info.Type)
  1151. insertData = append(insertData, info.Dealer)
  1152. insertData = append(insertData, info.Manufacturer)
  1153. }
  1154. thisSQL += strings.Join(insertParams, ", ")
  1155. err = utx.Exec(thisSQL, insertData...).Error
  1156. if err != nil {
  1157. utx.Rollback()
  1158. return
  1159. }
  1160. }
  1161. utx.Commit()
  1162. }
  1163. return
  1164. }
  1165. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1166. if len(salesReturnInfo) > 0 {
  1167. utx := writeDb.Begin()
  1168. if len(salesReturnInfo) > 0 {
  1169. 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 "
  1170. insertParams := make([]string, 0)
  1171. insertData := make([]interface{}, 0)
  1172. for _, info := range salesReturnInfo {
  1173. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1174. insertData = append(insertData, info.DrugId)
  1175. insertData = append(insertData, info.SalesReturnId)
  1176. insertData = append(insertData, info.Count)
  1177. insertData = append(insertData, info.Price)
  1178. insertData = append(insertData, info.Total)
  1179. insertData = append(insertData, info.Ctime)
  1180. insertData = append(insertData, info.Status)
  1181. insertData = append(insertData, info.OrgId)
  1182. insertData = append(insertData, info.OrderNumber)
  1183. insertData = append(insertData, info.Type)
  1184. insertData = append(insertData, info.Dealer)
  1185. insertData = append(insertData, info.Manufacturer)
  1186. insertData = append(insertData, info.RetailPrice)
  1187. insertData = append(insertData, info.RetailTotalPrice)
  1188. }
  1189. thisSQL += strings.Join(insertParams, ", ")
  1190. err = utx.Exec(thisSQL, insertData...).Error
  1191. if err != nil {
  1192. utx.Rollback()
  1193. return
  1194. }
  1195. }
  1196. utx.Commit()
  1197. }
  1198. return
  1199. }
  1200. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1201. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1202. return total, err
  1203. }
  1204. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1205. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1206. return total, err
  1207. }
  1208. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1209. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1210. if err != gorm.ErrRecordNotFound {
  1211. if err != nil {
  1212. tx.Rollback()
  1213. return total, err
  1214. }
  1215. }
  1216. return total, err
  1217. }
  1218. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1219. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1220. return total, err
  1221. }
  1222. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1223. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1224. if err != gorm.ErrRecordNotFound {
  1225. if err != nil {
  1226. tx.Rollback()
  1227. return total, err
  1228. }
  1229. }
  1230. return total, err
  1231. }
  1232. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1233. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1234. // return total, err
  1235. //}
  1236. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1237. db := readDb.Model(&models.SalesReturn{})
  1238. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1239. if len(keywords) > 0 {
  1240. likeKey := "%" + keywords + "%"
  1241. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1242. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1243. 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")
  1244. }
  1245. if startTime > 0 {
  1246. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1247. }
  1248. if endTime > 0 {
  1249. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1250. }
  1251. db = db.Count(&total)
  1252. offset := (page - 1) * limit
  1253. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1254. return
  1255. }
  1256. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1257. db := readDb.Model(&models.DrugSalesReturn{})
  1258. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1259. if len(keywords) > 0 {
  1260. likeKey := "%" + keywords + "%"
  1261. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1262. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1263. 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")
  1264. }
  1265. if startTime > 0 {
  1266. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1267. }
  1268. if endTime > 0 {
  1269. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1270. }
  1271. db = db.Count(&total)
  1272. offset := (page - 1) * limit
  1273. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1274. return
  1275. }
  1276. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1277. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1278. return list, err
  1279. }
  1280. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1281. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1282. return list, err
  1283. }
  1284. type DrugCancelStockInfo struct {
  1285. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1286. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1287. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1288. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1289. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1290. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1291. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1292. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1293. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1294. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1295. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1296. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1297. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1298. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1299. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1300. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1301. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1302. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1303. Number string `gorm:"column:number" json:"number" form:"number"`
  1304. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1305. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1306. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1307. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1308. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1309. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1310. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1311. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1312. }
  1313. func (DrugCancelStockInfo) TableName() string {
  1314. return "xt_drug_cancel_stock_info"
  1315. }
  1316. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1317. err = readDb.Model(&DrugCancelStockInfo{}).Preload("Drug", "status = 1 AND org_id = ? ", org_id).Preload("DrugWarehouseInfo", "status = 1 AND org_id = ? ", org_id).Where("cancel_stock_id = ? AND status = 1", id).Find(&list).Error
  1318. return list, err
  1319. }
  1320. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1321. db := readDb.Model(&models.WarehousingInfo{})
  1322. db = db.Where("org_id = ? AND status = 1", orgId)
  1323. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1324. if startTime > 0 {
  1325. db = db.Where("ctime >=?", startTime)
  1326. }
  1327. if endTime > 0 {
  1328. db = db.Where("ctime<= ?", endTime)
  1329. }
  1330. db = db.Count(&total)
  1331. offset := (page - 1) * limit
  1332. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1333. return list, total, err
  1334. }
  1335. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1336. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1337. return total, err
  1338. }
  1339. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1340. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1341. return total, err
  1342. }
  1343. func FindAllSelfDrugWarehouseOut(org_id int64, tx *gorm.DB) (total int64, err error) {
  1344. err = tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1345. if err != gorm.ErrRecordNotFound {
  1346. if err != nil {
  1347. tx.Rollback()
  1348. return
  1349. }
  1350. }
  1351. return total, err
  1352. }
  1353. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut) error {
  1354. err := writeDb.Create(&warehouseOut).Error
  1355. return err
  1356. }
  1357. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1358. err := tx.Create(&warehouseOut).Error
  1359. if err != nil {
  1360. tx.Rollback()
  1361. return err
  1362. }
  1363. return err
  1364. }
  1365. func UpdateSingleWarehoseOut(id int64, storehouse_id int64) error {
  1366. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1367. return err
  1368. }
  1369. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1370. err := writeDb.Create(&warehouseOut).Error
  1371. return err
  1372. }
  1373. func GetSigleDrugWarehouseOutByPatientId(warehousing_time int64, patient_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouse, error) {
  1374. selfDrugWarehouse := models.XtSelfDrugWarehouse{}
  1375. err := tx.Where("warehousing_time = ? and patient_id = ? and is_sys =1 and status=1", warehousing_time, patient_id).Find(&selfDrugWarehouse).Error
  1376. if err != gorm.ErrRecordNotFound {
  1377. if err != nil {
  1378. tx.Rollback()
  1379. return selfDrugWarehouse, err
  1380. }
  1381. }
  1382. return selfDrugWarehouse, err
  1383. }
  1384. func AddSelfSigleDrugWarehouseOut(warehouseOut *models.XtSelfDrugWarehouseOut, tx *gorm.DB) error {
  1385. err := tx.Create(&warehouseOut).Error
  1386. if err != nil {
  1387. tx.Rollback()
  1388. return err
  1389. }
  1390. return err
  1391. }
  1392. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1393. err := tx.Create(&warehouseOut).Error
  1394. if err != nil {
  1395. tx.Rollback()
  1396. return err
  1397. }
  1398. return err
  1399. }
  1400. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1401. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1402. return err
  1403. }
  1404. func UpdateSelfSingleDrugWarehouseOut(id int64, patient_id int64, tx *gorm.DB) error {
  1405. err := tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"patient_id": patient_id}).Error
  1406. return err
  1407. }
  1408. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1409. if len(warehouseOutInfo) > 0 {
  1410. utx := writeDb.Begin()
  1411. if len(warehouseOutInfo) > 0 {
  1412. 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 "
  1413. insertParams := make([]string, 0)
  1414. insertData := make([]interface{}, 0)
  1415. for _, info := range warehouseOutInfo {
  1416. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1417. insertData = append(insertData, info.WarehouseOutId)
  1418. insertData = append(insertData, info.GoodId)
  1419. insertData = append(insertData, info.GoodTypeId)
  1420. insertData = append(insertData, info.ProductDate)
  1421. insertData = append(insertData, info.ExpiryDate)
  1422. insertData = append(insertData, info.Count)
  1423. insertData = append(insertData, info.Price)
  1424. insertData = append(insertData, info.TotalPrice)
  1425. insertData = append(insertData, info.Remark)
  1426. insertData = append(insertData, info.Ctime)
  1427. insertData = append(insertData, info.Status)
  1428. insertData = append(insertData, info.OrgId)
  1429. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1430. insertData = append(insertData, info.Type)
  1431. insertData = append(insertData, info.Dealer)
  1432. insertData = append(insertData, info.Manufacturer)
  1433. insertData = append(insertData, info.Number)
  1434. insertData = append(insertData, info.LicenseNumber)
  1435. }
  1436. thisSQL += strings.Join(insertParams, ", ")
  1437. err = utx.Exec(thisSQL, insertData...).Error
  1438. if err != nil {
  1439. utx.Rollback()
  1440. return
  1441. }
  1442. }
  1443. utx.Commit()
  1444. }
  1445. return
  1446. }
  1447. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1448. if len(warehouseOutInfo) > 0 {
  1449. utx := writeDb.Begin()
  1450. if len(warehouseOutInfo) > 0 {
  1451. 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 "
  1452. insertParams := make([]string, 0)
  1453. insertData := make([]interface{}, 0)
  1454. for _, info := range warehouseOutInfo {
  1455. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1456. insertData = append(insertData, info.WarehouseOutId)
  1457. insertData = append(insertData, info.DrugId)
  1458. insertData = append(insertData, info.ProductDate)
  1459. insertData = append(insertData, info.ExpiryDate)
  1460. insertData = append(insertData, info.Count)
  1461. insertData = append(insertData, info.Price)
  1462. insertData = append(insertData, info.TotalPrice)
  1463. insertData = append(insertData, info.Remark)
  1464. insertData = append(insertData, info.Ctime)
  1465. insertData = append(insertData, info.Status)
  1466. insertData = append(insertData, info.OrgId)
  1467. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1468. insertData = append(insertData, info.Type)
  1469. insertData = append(insertData, info.Dealer)
  1470. insertData = append(insertData, info.Manufacturer)
  1471. insertData = append(insertData, info.RetailPrice)
  1472. insertData = append(insertData, info.RetailTotalPrice)
  1473. insertData = append(insertData, info.Number)
  1474. insertData = append(insertData, info.BatchNumber)
  1475. insertData = append(insertData, info.CountUnit)
  1476. }
  1477. thisSQL += strings.Join(insertParams, ", ")
  1478. err = utx.Exec(thisSQL, insertData...).Error
  1479. if err != nil {
  1480. utx.Rollback()
  1481. return
  1482. }
  1483. }
  1484. utx.Commit()
  1485. }
  1486. return
  1487. }
  1488. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1489. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1490. return info, err
  1491. }
  1492. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1493. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1494. return info, err
  1495. }
  1496. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1497. db := readDb.Model(&models.WarehouseOut{})
  1498. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1499. if len(keywords) > 0 {
  1500. likeKey := "%" + keywords + "%"
  1501. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1502. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1503. 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")
  1504. }
  1505. if startTime > 0 {
  1506. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1507. }
  1508. if endTime > 0 {
  1509. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1510. }
  1511. db = db.Count(&total)
  1512. offset := (page - 1) * limit
  1513. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1514. return list, total, err
  1515. }
  1516. func FindAllWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, way_type int64, check_type int64) (list []*models.WarehouseOut, total int64, err error) {
  1517. db := readDb.Model(&models.WarehouseOut{})
  1518. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1519. if len(keywords) > 0 {
  1520. likeKey := "%" + keywords + "%"
  1521. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1522. 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")
  1523. }
  1524. if startTime > 0 {
  1525. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1526. }
  1527. if endTime > 0 {
  1528. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1529. }
  1530. if storehouse_id > 0 {
  1531. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1532. }
  1533. if way_type == 0 {
  1534. db = db.Where("xt_warehouse_out.is_sys = 0 or xt_warehouse_out.is_sys = 1 or xt_warehouse_out.is_sys = 12 or xt_warehouse_out.is_sys = 5")
  1535. }
  1536. if way_type == 1 {
  1537. db = db.Where("xt_warehouse_out.is_sys = 0")
  1538. }
  1539. if way_type == 2 {
  1540. db = db.Where("xt_warehouse_out.is_sys = 1")
  1541. }
  1542. if way_type == 3 {
  1543. db = db.Where("xt_warehouse_out.is_sys = 12")
  1544. }
  1545. if way_type == 4 {
  1546. db = db.Where("xt_warehouse_out.is_sys = 5")
  1547. }
  1548. if check_type > 0 {
  1549. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1550. }
  1551. if check_type == 0 {
  1552. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1553. }
  1554. db = db.Count(&total)
  1555. offset := (page - 1) * limit
  1556. if orgId == 10191 {
  1557. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1558. } else {
  1559. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1560. }
  1561. return list, total, err
  1562. }
  1563. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1564. db := readDb.Model(&models.DrugWarehouseOut{})
  1565. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1566. if len(keywords) > 0 {
  1567. likeKey := "%" + keywords + "%"
  1568. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1569. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1570. 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")
  1571. }
  1572. if startTime > 0 {
  1573. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1574. }
  1575. if endTime > 0 {
  1576. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1577. }
  1578. db = db.Count(&total)
  1579. offset := (page - 1) * limit
  1580. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1581. return list, total, err
  1582. }
  1583. func FindAllDrugWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64, storehouse_id int64, way_type int64, check_type int64) (list []*models.DrugWarehouseOut, total int64, err error) {
  1584. db := readDb.Model(&models.DrugWarehouseOut{})
  1585. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1586. if len(keywords) > 0 {
  1587. likeKey := "%" + keywords + "%"
  1588. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1589. 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")
  1590. }
  1591. if startTime > 0 {
  1592. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1593. }
  1594. if endTime > 0 {
  1595. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1596. }
  1597. if storehouse_id > 0 {
  1598. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1599. }
  1600. if check_type > 0 {
  1601. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1602. }
  1603. if way_type == 0 {
  1604. db = db.Where("xt_drug_warehouse_out.is_sys = 0 || xt_drug_warehouse_out.is_sys = 1 || xt_drug_warehouse_out.is_sys = 12 || xt_drug_warehouse_out.is_sys = 5")
  1605. }
  1606. if way_type == 1 {
  1607. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1608. }
  1609. if way_type == 2 {
  1610. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1611. }
  1612. if way_type == 3 {
  1613. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1614. }
  1615. if way_type == 4 {
  1616. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1617. }
  1618. db = db.Count(&total)
  1619. offset := (page - 1) * limit
  1620. if orgId == 10191 {
  1621. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1622. } else {
  1623. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1624. }
  1625. return list, total, err
  1626. }
  1627. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1628. 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
  1629. return list, err
  1630. }
  1631. type DrugWarehouseOutInfo struct {
  1632. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1633. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1634. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1635. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1636. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1637. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1638. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1639. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1640. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1641. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1642. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1643. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1644. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1645. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1646. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1647. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1648. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1649. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1650. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1651. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1652. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1653. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1654. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1655. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1656. Number string `gorm:"column:number" json:"number" form:"number"`
  1657. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1658. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1659. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1660. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1661. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1662. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1663. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1664. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1665. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1666. ClassType string `gorm:"column:class_type" json:"class_type" form:"class_type"`
  1667. }
  1668. func (DrugWarehouseOutInfo) TableName() string {
  1669. return "xt_drug_warehouse_out_info"
  1670. }
  1671. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1672. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1673. return list, err
  1674. }
  1675. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1676. info := &models.WarehousingInfo{}
  1677. type WarehouseInfoCountAmount struct {
  1678. Total int64
  1679. }
  1680. var result WarehouseInfoCountAmount
  1681. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1682. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1683. return result.Total, info
  1684. }
  1685. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1686. type SalesReturnCountAmount struct {
  1687. Total int64
  1688. }
  1689. var result SalesReturnCountAmount
  1690. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1691. return result.Total, err
  1692. }
  1693. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1694. type WarehouseOutInfoCountAmount struct {
  1695. Total int64
  1696. }
  1697. var result WarehouseOutInfoCountAmount
  1698. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1699. return result.Total, err
  1700. }
  1701. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1702. type CancleStockoCountAmount struct {
  1703. Total int64
  1704. }
  1705. var result CancleStockoCountAmount
  1706. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1707. return result.Total, err
  1708. }
  1709. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1710. info := models.WarehousingInfo{}
  1711. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  1712. return info, err
  1713. }
  1714. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  1715. stock := models.CancelStock{}
  1716. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  1717. if err == gorm.ErrRecordNotFound {
  1718. return nil, err
  1719. }
  1720. if err != nil {
  1721. return nil, err
  1722. }
  1723. return &stock, nil
  1724. }
  1725. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  1726. stock := models.CancelStock{}
  1727. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1728. if err == gorm.ErrRecordNotFound {
  1729. return nil, err
  1730. }
  1731. if err != nil {
  1732. return nil, err
  1733. }
  1734. return &stock, nil
  1735. }
  1736. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  1737. stock := models.CancelStock{}
  1738. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1739. if err != gorm.ErrRecordNotFound {
  1740. if err != nil {
  1741. tx.Rollback()
  1742. return &stock, err
  1743. }
  1744. }
  1745. return &stock, err
  1746. }
  1747. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  1748. err := writeDb.Create(&cancelStock).Error
  1749. return err
  1750. }
  1751. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  1752. err := tx.Create(&cancelStock).Error
  1753. if err != nil {
  1754. tx.Rollback()
  1755. return err
  1756. }
  1757. return err
  1758. }
  1759. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  1760. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1761. return err
  1762. }
  1763. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  1764. stock := models.CancelStock{}
  1765. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1766. return stock, err
  1767. }
  1768. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  1769. stock := models.CancelStock{}
  1770. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1771. if err != gorm.ErrRecordNotFound {
  1772. if err != nil {
  1773. tx.Rollback()
  1774. return stock, err
  1775. }
  1776. }
  1777. return stock, err
  1778. }
  1779. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  1780. stock := models.DrugCancelStock{}
  1781. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1782. return stock, err
  1783. }
  1784. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  1785. stock := models.DrugCancelStock{}
  1786. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1787. if err != gorm.ErrRecordNotFound {
  1788. if err != nil {
  1789. tx.Rollback()
  1790. return stock, err
  1791. }
  1792. }
  1793. return stock, err
  1794. }
  1795. func GetManufactureById(id int64) (models.Manufacturer, error) {
  1796. manufacturer := models.Manufacturer{}
  1797. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1798. return manufacturer, err
  1799. }
  1800. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  1801. manufacturer := models.Manufacturer{}
  1802. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1803. if err != gorm.ErrRecordNotFound {
  1804. if err != nil {
  1805. tx.Rollback()
  1806. return manufacturer, err
  1807. }
  1808. }
  1809. return manufacturer, err
  1810. }
  1811. func GetDealerById(id int64) (models.Dealer, error) {
  1812. dealer := models.Dealer{}
  1813. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1814. return dealer, err
  1815. }
  1816. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  1817. dealer := models.Dealer{}
  1818. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1819. if err != gorm.ErrRecordNotFound {
  1820. if err != nil {
  1821. tx.Rollback()
  1822. return dealer, err
  1823. }
  1824. }
  1825. return dealer, err
  1826. }
  1827. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  1828. err := writeDb.Create(&cancelStock).Error
  1829. return err
  1830. }
  1831. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  1832. err := tx.Create(&cancelStock).Error
  1833. if err != nil {
  1834. tx.Rollback()
  1835. return err
  1836. }
  1837. return err
  1838. }
  1839. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  1840. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1841. return err
  1842. }
  1843. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  1844. // err := tx.Create(&cancelStock).Error
  1845. // return err
  1846. //}
  1847. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  1848. if len(cancelStockInfo) > 0 {
  1849. utx := writeDb.Begin()
  1850. if len(cancelStockInfo) > 0 {
  1851. 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 "
  1852. insertParams := make([]string, 0)
  1853. insertData := make([]interface{}, 0)
  1854. for _, info := range cancelStockInfo {
  1855. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1856. insertData = append(insertData, info.GoodId)
  1857. insertData = append(insertData, info.CancelStockId)
  1858. insertData = append(insertData, info.GoodTypeId)
  1859. insertData = append(insertData, info.Count)
  1860. insertData = append(insertData, info.Price)
  1861. insertData = append(insertData, info.Total)
  1862. insertData = append(insertData, info.Ctime)
  1863. insertData = append(insertData, info.Status)
  1864. insertData = append(insertData, info.OrgId)
  1865. insertData = append(insertData, info.OrderNumber)
  1866. insertData = append(insertData, info.Type)
  1867. insertData = append(insertData, info.Dealer)
  1868. insertData = append(insertData, info.Manufacturer)
  1869. insertData = append(insertData, info.Number)
  1870. insertData = append(insertData, info.RegisterAccount)
  1871. insertData = append(insertData, info.Remark)
  1872. insertData = append(insertData, info.ProductDate)
  1873. insertData = append(insertData, info.ExpiryDate)
  1874. insertData = append(insertData, info.WarehouseInfoId)
  1875. insertData = append(insertData, info.WarehouseInfoId)
  1876. }
  1877. thisSQL += strings.Join(insertParams, ", ")
  1878. err = utx.Exec(thisSQL, insertData...).Error
  1879. if err != nil {
  1880. utx.Rollback()
  1881. return
  1882. }
  1883. }
  1884. utx.Commit()
  1885. }
  1886. return
  1887. }
  1888. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  1889. err := XTWriteDB().Create(&info).Error
  1890. return err
  1891. }
  1892. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  1893. // err := tx.Create(&info).Error
  1894. // return err
  1895. //}
  1896. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  1897. info := models.DrugCancelStockInfo{}
  1898. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1899. return info, err
  1900. }
  1901. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  1902. // info := models.DrugCancelStockInfo{}
  1903. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1904. // return info, err
  1905. //}
  1906. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  1907. err := XTWriteDB().Create(&info).Error
  1908. return err
  1909. }
  1910. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  1911. err := XTWriteDB().Create(&info).Error
  1912. if err != nil {
  1913. tx.Rollback()
  1914. return err
  1915. }
  1916. return err
  1917. }
  1918. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  1919. info := models.CancelStockInfo{}
  1920. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1921. return info, err
  1922. }
  1923. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  1924. info := models.CancelStockInfo{}
  1925. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1926. if err != gorm.ErrRecordNotFound {
  1927. if err != nil {
  1928. tx.Rollback()
  1929. return info, err
  1930. }
  1931. }
  1932. return info, err
  1933. }
  1934. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  1935. info := models.DrugCancelStockInfo{}
  1936. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  1937. return info, err
  1938. }
  1939. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  1940. if len(cancelStockInfo) > 0 {
  1941. utx := writeDb.Begin()
  1942. if len(cancelStockInfo) > 0 {
  1943. 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 "
  1944. insertParams := make([]string, 0)
  1945. insertData := make([]interface{}, 0)
  1946. for _, info := range cancelStockInfo {
  1947. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1948. insertData = append(insertData, info.DrugId)
  1949. insertData = append(insertData, info.CancelStockId)
  1950. insertData = append(insertData, info.Count)
  1951. insertData = append(insertData, info.Price)
  1952. insertData = append(insertData, info.Total)
  1953. insertData = append(insertData, info.Ctime)
  1954. insertData = append(insertData, info.Status)
  1955. insertData = append(insertData, info.OrgId)
  1956. insertData = append(insertData, info.OrderNumber)
  1957. insertData = append(insertData, info.Type)
  1958. insertData = append(insertData, info.Dealer)
  1959. insertData = append(insertData, info.Manufacturer)
  1960. insertData = append(insertData, info.RetailPrice)
  1961. insertData = append(insertData, info.RetailTotalPrice)
  1962. insertData = append(insertData, info.RegisterAccount)
  1963. insertData = append(insertData, info.BatchNumber)
  1964. insertData = append(insertData, info.Remark)
  1965. insertData = append(insertData, info.MaxUnit)
  1966. insertData = append(insertData, info.ProductDate)
  1967. insertData = append(insertData, info.ExpiryDate)
  1968. insertData = append(insertData, info.BatchNumberId)
  1969. }
  1970. thisSQL += strings.Join(insertParams, ", ")
  1971. err = utx.Exec(thisSQL, insertData...).Error
  1972. if err != nil {
  1973. utx.Rollback()
  1974. return
  1975. }
  1976. }
  1977. utx.Commit()
  1978. }
  1979. return
  1980. }
  1981. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  1982. db := readDb.Model(&models.CancelStock{})
  1983. db = db.Where("org_id = ? AND status = 1", orgId)
  1984. err = db.Order("ctime desc").Find(&list).Error
  1985. return
  1986. }
  1987. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  1988. db := readDb.Model(&models.WarehousingInfo{})
  1989. db = db.Where("org_id = ? AND status = 1", orgId)
  1990. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  1991. if startTime > 0 {
  1992. db = db.Where("ctime >=?", startTime)
  1993. }
  1994. if endTime > 0 {
  1995. db = db.Where("ctime<= ?", endTime)
  1996. }
  1997. db = db.Count(&total)
  1998. offset := (page - 1) * limit
  1999. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2000. return
  2001. }
  2002. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  2003. db := XTReadDB().Model(&models.StockInfo{})
  2004. if startime > 0 {
  2005. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2006. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  2007. })
  2008. }
  2009. if startime == 0 || endtime == 0 {
  2010. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2011. return db.Where("org_id = ? AND status = 1", orgId)
  2012. })
  2013. }
  2014. if endtime > 0 {
  2015. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2016. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  2017. })
  2018. }
  2019. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  2020. db = db.Group("xt_good_information.id")
  2021. if startime == 0 || endtime == 0 {
  2022. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2023. return db.Where("org_id = ? AND status = 1", orgId)
  2024. })
  2025. }
  2026. if startime > 0 {
  2027. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2028. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  2029. })
  2030. }
  2031. if endtime > 0 {
  2032. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2033. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  2034. })
  2035. }
  2036. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  2037. if startime == 0 || endtime == 0 {
  2038. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  2039. }
  2040. if startime > 0 {
  2041. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  2042. }
  2043. if endtime > 0 {
  2044. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2045. }
  2046. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2047. if len(keyword) > 0 {
  2048. likeKey := "%" + keyword + "%"
  2049. 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)
  2050. 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")
  2051. }
  2052. if type_name > 0 {
  2053. 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)
  2054. }
  2055. db = db.Count(&total)
  2056. offset := (page - 1) * limit
  2057. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2058. return
  2059. }
  2060. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2061. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2062. return
  2063. }
  2064. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2065. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2066. return
  2067. }
  2068. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2069. 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
  2070. return
  2071. }
  2072. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2073. 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
  2074. return
  2075. }
  2076. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2077. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2078. "good_id": warehouseInfo.GoodId,
  2079. "number": warehouseInfo.Number,
  2080. "product_date": warehouseInfo.ProductDate,
  2081. "expiry_date": warehouseInfo.ExpiryDate,
  2082. "warehousing_count": warehouseInfo.WarehousingCount,
  2083. "price": warehouseInfo.Price,
  2084. "total_price": warehouseInfo.TotalPrice,
  2085. "mtime": warehouseInfo.Mtime,
  2086. "remark": warehouseInfo.Remark,
  2087. }).Error
  2088. return
  2089. }
  2090. func UpDateWarehouseStatus(id int64) (err error) {
  2091. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2092. return
  2093. }
  2094. func UpdatedStockFlowById(id int64) (err error) {
  2095. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2096. return
  2097. }
  2098. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2099. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2100. return
  2101. }
  2102. func UpdateDrugFlowById(id int64) (err error) {
  2103. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2104. return
  2105. }
  2106. func DeleteWarehouse(ids []string) (err error) {
  2107. ut := writeDb.Begin()
  2108. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2109. if err != nil {
  2110. ut.Rollback()
  2111. return
  2112. }
  2113. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2114. if err != nil {
  2115. ut.Rollback()
  2116. return
  2117. }
  2118. ut.Commit()
  2119. return
  2120. }
  2121. func DeleteStockFlow(ids []string) (err error) {
  2122. ut := writeDb.Begin()
  2123. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2124. if err != nil {
  2125. ut.Rollback()
  2126. return
  2127. }
  2128. ut.Commit()
  2129. return
  2130. }
  2131. func DeleteDrugWarehouse(ids []string) (err error) {
  2132. ut := writeDb.Begin()
  2133. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2134. if err != nil {
  2135. ut.Rollback()
  2136. return
  2137. }
  2138. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2139. if err != nil {
  2140. ut.Rollback()
  2141. return
  2142. }
  2143. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2144. if err != nil {
  2145. ut.Rollback()
  2146. return
  2147. }
  2148. ut.Commit()
  2149. return
  2150. }
  2151. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2152. warehouse := &models.Warehousing{}
  2153. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2154. return warehouse, err
  2155. }
  2156. func FindWareHouseOutById(id int64) (*models.WarehouseOut, error) {
  2157. warehouseOut := &models.WarehouseOut{}
  2158. err := readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2159. return warehouseOut, err
  2160. }
  2161. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2162. warehouseOut := &models.DrugWarehouseOut{}
  2163. 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
  2164. return warehouseOut, err
  2165. }
  2166. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2167. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2168. return
  2169. }
  2170. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2171. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2172. return
  2173. }
  2174. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2175. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2176. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2177. return
  2178. }
  2179. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2180. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2181. return
  2182. }
  2183. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2184. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2185. if manufacturer_id > 0 {
  2186. db = db.Where("manufacturer = ?", manufacturer_id)
  2187. }
  2188. if dealer_id > 0 {
  2189. db = db.Where("dealer = ?", dealer_id)
  2190. }
  2191. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2192. return goodInfo, err
  2193. }
  2194. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2195. db := readDb.Model(&models.Warehousing{})
  2196. likekey := "%" + keywords + "%"
  2197. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2198. db = db.Where("warehousing_order LIKE ? ", likekey)
  2199. db = db.Count(&total)
  2200. offset := (page - 1) * limit
  2201. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2202. return
  2203. }
  2204. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2205. info := models.WarehousingInfo{}
  2206. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2207. return info, err
  2208. }
  2209. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2210. info := models.WarehousingInfo{}
  2211. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2212. return info, err
  2213. }
  2214. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2215. err = writeDb.Save(&info).Error
  2216. return err
  2217. }
  2218. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2219. err = writeDb.Save(&info).Error
  2220. return err
  2221. }
  2222. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2223. 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
  2224. return err
  2225. }
  2226. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2227. err = writeDb.Model(&info).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "batch_number": info.BatchNumber, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "retail_price": info.RetailPrice, "storehouse_id": info.StorehouseId}).Error
  2228. return err
  2229. }
  2230. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2231. info := models.DrugWarehouseInfo{}
  2232. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2233. return info, err
  2234. }
  2235. func EditWarehousing(warehouse models.Warehousing) {
  2236. 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
  2237. }
  2238. func EditDrugWarehousing(warehouse models.DrugWarehouse) {
  2239. 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
  2240. }
  2241. func FindAllWarehouse(org_id int64, types int64) (list []*models.Warehousing, err error) {
  2242. 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
  2243. return list, err
  2244. }
  2245. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2246. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2247. 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)
  2248. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2249. return list, err
  2250. }
  2251. func DeleteSalesReturn(ids []string) (err error) {
  2252. ut := writeDb.Begin()
  2253. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2254. if err != nil {
  2255. ut.Rollback()
  2256. return
  2257. }
  2258. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2259. if err != nil {
  2260. ut.Rollback()
  2261. return
  2262. }
  2263. ut.Commit()
  2264. return
  2265. }
  2266. func DeleteDrugSalesReturn(ids []string) (err error) {
  2267. ut := writeDb.Begin()
  2268. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2269. if err != nil {
  2270. ut.Rollback()
  2271. return
  2272. }
  2273. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2274. if err != nil {
  2275. ut.Rollback()
  2276. return
  2277. }
  2278. ut.Commit()
  2279. return
  2280. }
  2281. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2282. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2283. return
  2284. }
  2285. type DrugSalesReturnInfo struct {
  2286. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2287. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2288. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2289. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2290. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2291. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2292. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2293. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2294. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2295. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2296. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2297. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2298. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2299. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2300. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2301. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2302. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2303. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2304. Number string `gorm:"column:number" json:"number" form:"number"`
  2305. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2306. }
  2307. func (DrugSalesReturnInfo) TableName() string {
  2308. return "xt_drug_sales_return_info"
  2309. }
  2310. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2311. 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
  2312. return
  2313. }
  2314. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2315. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2316. return
  2317. }
  2318. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2319. 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
  2320. return
  2321. }
  2322. func UpDateSaleReturnStatus(id int64) (err error) {
  2323. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2324. return
  2325. }
  2326. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2327. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2328. return
  2329. }
  2330. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2331. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2332. return salesReturn, err
  2333. }
  2334. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2335. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2336. return salesReturn, err
  2337. }
  2338. func EditSaleReturn(sales models.SalesReturn) {
  2339. 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
  2340. }
  2341. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2342. 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
  2343. }
  2344. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2345. err = writeDb.Save(&info).Error
  2346. return err
  2347. }
  2348. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2349. err = writeDb.Save(&info).Error
  2350. return err
  2351. }
  2352. func DeleteWarehouseOut(ids []string) (err error) {
  2353. ut := writeDb.Begin()
  2354. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2355. if err != nil {
  2356. ut.Rollback()
  2357. return
  2358. }
  2359. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2360. if err != nil {
  2361. ut.Rollback()
  2362. return
  2363. }
  2364. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2365. if err != nil {
  2366. ut.Rollback()
  2367. return
  2368. }
  2369. ut.Commit()
  2370. return
  2371. }
  2372. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2373. ut := writeDb.Begin()
  2374. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2375. if err != nil {
  2376. ut.Rollback()
  2377. return
  2378. }
  2379. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2380. if err != nil {
  2381. ut.Rollback()
  2382. return
  2383. }
  2384. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2385. if err != nil {
  2386. ut.Rollback()
  2387. return
  2388. }
  2389. ut.Commit()
  2390. return
  2391. }
  2392. func UpDateWarehouseOutStatus(id int64) (err error) {
  2393. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2394. return
  2395. }
  2396. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2397. ut := writeDb.Begin()
  2398. err = ut.Save(&info).Error
  2399. if err != nil {
  2400. ut.Rollback()
  2401. return err
  2402. }
  2403. ut.Commit()
  2404. return err
  2405. }
  2406. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2407. err = tx.Save(&info).Error
  2408. if err != nil {
  2409. tx.Rollback()
  2410. return err
  2411. }
  2412. return err
  2413. }
  2414. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2415. ut := writeDb.Begin()
  2416. err = ut.Save(&info).Error
  2417. if err != nil {
  2418. ut.Rollback()
  2419. return err
  2420. }
  2421. ut.Commit()
  2422. return err
  2423. }
  2424. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2425. err = tx.Save(&info).Error
  2426. if err != nil {
  2427. tx.Rollback()
  2428. return err
  2429. }
  2430. return err
  2431. }
  2432. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2433. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2434. return
  2435. }
  2436. func EditWarehouseOut(warehouseOut models.WarehouseOut) {
  2437. 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
  2438. }
  2439. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2440. 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
  2441. }
  2442. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo) (err error) {
  2443. err = writeDb.Save(&info).Error
  2444. return err
  2445. }
  2446. func FindAllCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, storehouse_id int64, check_type int64) (list []*models.CancelStock, total int64, err error) {
  2447. db := readDb.Model(&models.CancelStock{})
  2448. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2449. if len(keywords) > 0 {
  2450. likeKey := "%" + keywords + "%"
  2451. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2452. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2453. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2454. }
  2455. if startTime > 0 {
  2456. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2457. }
  2458. if endTime > 0 {
  2459. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2460. }
  2461. if storehouse_id > 0 {
  2462. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2463. }
  2464. if check_type > 0 {
  2465. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2466. }
  2467. db = db.Count(&total)
  2468. offset := (page - 1) * limit
  2469. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2470. return
  2471. }
  2472. func FindAllDrugCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, storehoue_id int64, check_type int64) (list []*models.DrugCancelStock, total int64, err error) {
  2473. db := readDb.Model(&models.DrugCancelStock{})
  2474. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2475. if len(keywords) > 0 {
  2476. likeKey := "%" + keywords + "%"
  2477. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2478. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2479. 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")
  2480. }
  2481. if startTime > 0 {
  2482. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2483. }
  2484. if endTime > 0 {
  2485. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2486. }
  2487. if storehoue_id > 0 {
  2488. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2489. }
  2490. if check_type > 0 {
  2491. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2492. }
  2493. db = db.Count(&total)
  2494. offset := (page - 1) * limit
  2495. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2496. return
  2497. }
  2498. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2499. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2500. return info, err
  2501. }
  2502. func DeleteCancelStock(ids []string) (err error) {
  2503. ut := writeDb.Begin()
  2504. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2505. if err != nil {
  2506. ut.Rollback()
  2507. return
  2508. }
  2509. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2510. if err != nil {
  2511. ut.Rollback()
  2512. return
  2513. }
  2514. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2515. if err != nil {
  2516. ut.Rollback()
  2517. return
  2518. }
  2519. ut.Commit()
  2520. return
  2521. }
  2522. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2523. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2524. return info, err
  2525. }
  2526. func DeleteDrugCancelStock(ids []string) (err error) {
  2527. ut := writeDb.Begin()
  2528. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2529. if err != nil {
  2530. ut.Rollback()
  2531. return
  2532. }
  2533. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2534. if err != nil {
  2535. ut.Rollback()
  2536. return
  2537. }
  2538. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2539. if err != nil {
  2540. ut.Rollback()
  2541. return
  2542. }
  2543. ut.Commit()
  2544. return
  2545. }
  2546. func UpDateCancleStockStatus(id int64) (err error) {
  2547. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2548. return
  2549. }
  2550. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2551. info := models.CancelStockInfo{}
  2552. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2553. return info, err
  2554. }
  2555. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2556. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2557. return err
  2558. }
  2559. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2560. 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
  2561. return
  2562. }
  2563. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2564. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2565. return
  2566. }
  2567. func UpDateDrugCancelStockById(id int64) (err error) {
  2568. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2569. return
  2570. }
  2571. func EditCancelStock(sales models.CancelStock) {
  2572. 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
  2573. }
  2574. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2575. 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
  2576. }
  2577. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2578. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2579. return cancelStock, err
  2580. }
  2581. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2582. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2583. return cancelStock, err
  2584. }
  2585. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2586. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2587. return cancelStock, err
  2588. }
  2589. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2590. err = writeDb.Save(&info).Error
  2591. return err
  2592. }
  2593. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2594. err = writeDb.Save(&info).Error
  2595. return err
  2596. }
  2597. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2598. 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
  2599. return list, err
  2600. }
  2601. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2602. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2603. 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)
  2604. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2605. return list, err
  2606. }
  2607. type StockDetailModel struct {
  2608. GoodId int64 `gorm:"good_id" json:"good_id"`
  2609. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2610. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2611. Price float64 `gorm:"price" json:"price"`
  2612. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2613. Type int `gorm:"type" json:"type"`
  2614. Creater int64 `gorm:"creater" json:"creater"`
  2615. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2616. Types int64 `gorm:"types" json:"types"`
  2617. }
  2618. 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) {
  2619. db := readDb.Model(&models.WarehousingInfo{})
  2620. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2621. if len(keywords) > 0 {
  2622. likeKey := "%" + keywords + "%"
  2623. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2624. 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)
  2625. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2626. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2627. 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")
  2628. } else {
  2629. 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)
  2630. }
  2631. if manufacturer > 0 {
  2632. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2633. }
  2634. if storehouse_id > 0 {
  2635. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2636. }
  2637. if order_type > 0 {
  2638. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2639. }
  2640. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2641. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2642. db = db.Count(&total)
  2643. offset := (page - 1) * limit
  2644. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2645. return
  2646. }
  2647. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2648. db := readDb.Model(&models.WarehousingInfo{})
  2649. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2650. if len(keywords) > 0 {
  2651. likeKey := "%" + keywords + "%"
  2652. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2653. 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)
  2654. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2655. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2656. 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")
  2657. } else {
  2658. 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)
  2659. }
  2660. if manufacturer > 0 {
  2661. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2662. }
  2663. if order_type > 0 {
  2664. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2665. }
  2666. if storehouse_id > 0 {
  2667. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2668. }
  2669. type PriceAmount struct {
  2670. Total float64
  2671. }
  2672. var result []PriceAmount
  2673. err = db.Select("total_price as total").Scan(&result).Error
  2674. var total float64
  2675. for _, value := range result {
  2676. total = total + value.Total
  2677. }
  2678. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2679. return err, total
  2680. }
  2681. func FloatRound(f float64, n int) float64 {
  2682. format := "%." + strconv.Itoa(n) + "f"
  2683. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  2684. return res
  2685. }
  2686. 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) {
  2687. if orgId == 9671 {
  2688. db := readDb.Model(&models.WarehouseOutInfo{})
  2689. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.is_check = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 AND xt_warehouse_out_info.sys_record_time>=? AND xt_warehouse_out_info.sys_record_time<=?", orgId, startTime, endTime)
  2690. if len(keywords) > 0 {
  2691. likeKey := "%" + keywords + "%"
  2692. 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)
  2693. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2694. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2695. 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")
  2696. } else {
  2697. 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)
  2698. }
  2699. if manufacturer > 0 {
  2700. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2701. }
  2702. if order_type > 0 {
  2703. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2704. }
  2705. if storehouse_id > 0 {
  2706. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2707. }
  2708. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2709. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2710. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2711. db = db.Count(&total)
  2712. offset := (page - 1) * limit
  2713. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2714. return
  2715. } else {
  2716. db := readDb.Model(&models.WarehouseOutInfo{})
  2717. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.is_check = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 ", orgId)
  2718. if len(keywords) > 0 {
  2719. likeKey := "%" + keywords + "%"
  2720. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  2721. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2722. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2723. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out_info.warehouse_out_order_number LIKE ? OR xt_good_information.specification_name LIKE ? OR xt_good_information.good_name LIKE ?", likeKey, likeKey, likeKey, likeKey).Group("xt_warehouse_out_info.id")
  2724. } else {
  2725. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  2726. }
  2727. if manufacturer > 0 {
  2728. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2729. }
  2730. if order_type > 0 {
  2731. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2732. }
  2733. if storehouse_id > 0 {
  2734. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2735. }
  2736. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2737. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  2738. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2739. db = db.Count(&total)
  2740. offset := (page - 1) * limit
  2741. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  2742. return
  2743. }
  2744. }
  2745. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2746. db := readDb.Model(&models.WarehouseOutInfo{})
  2747. 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)
  2748. if len(keywords) > 0 {
  2749. likeKey := "%" + keywords + "%"
  2750. 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)
  2751. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2752. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2753. 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")
  2754. } else {
  2755. 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)
  2756. }
  2757. if manufacturer > 0 {
  2758. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2759. }
  2760. if order_type > 0 {
  2761. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2762. }
  2763. if storehouse_id > 0 {
  2764. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  2765. }
  2766. type PriceAmount struct {
  2767. Total float64
  2768. }
  2769. var result []PriceAmount
  2770. err = db.Select("(count * price) as total").Scan(&result).Error
  2771. var total float64
  2772. for _, value := range result {
  2773. total = total + value.Total
  2774. }
  2775. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2776. fmt.Println(total)
  2777. return err, total
  2778. }
  2779. 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) {
  2780. db := readDb.Model(&models.SalesReturnInfo{})
  2781. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  2782. if len(keywords) > 0 {
  2783. likeKey := "%" + keywords + "%"
  2784. 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)
  2785. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  2786. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  2787. 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")
  2788. } else {
  2789. 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)
  2790. }
  2791. if manufacturer > 0 {
  2792. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  2793. }
  2794. if order_type > 0 {
  2795. db = db.Where("xt_sales_return_info.type = ?", order_type)
  2796. }
  2797. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  2798. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2799. offset := (page - 1) * limit
  2800. db = db.Count(&total)
  2801. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  2802. return
  2803. }
  2804. 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) {
  2805. db := readDb.Model(&models.CancelStockInfo{})
  2806. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2807. if len(keywords) > 0 {
  2808. likeKey := "%" + keywords + "%"
  2809. 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)
  2810. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2811. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2812. 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")
  2813. } else {
  2814. 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)
  2815. }
  2816. if manufacturer > 0 {
  2817. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2818. }
  2819. if order_type > 0 {
  2820. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2821. }
  2822. if storehouse_id > 0 {
  2823. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  2824. }
  2825. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2826. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2827. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  2828. return
  2829. }
  2830. 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) {
  2831. db := readDb.Model(&models.CancelStockInfo{})
  2832. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2833. if len(keywords) > 0 {
  2834. likeKey := "%" + keywords + "%"
  2835. 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)
  2836. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2837. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2838. 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")
  2839. } else {
  2840. 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)
  2841. }
  2842. if manufacturer > 0 {
  2843. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2844. }
  2845. if order_type > 0 {
  2846. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2847. }
  2848. if storehouse_id > 0 {
  2849. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  2850. }
  2851. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2852. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2853. db = db.Count(&total)
  2854. offset := (page - 1) * limit
  2855. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  2856. return
  2857. }
  2858. func FindStockInByDealerId(id int64) (total int64, err error) {
  2859. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  2860. return
  2861. }
  2862. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  2863. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  2864. return
  2865. }
  2866. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  2867. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  2868. return
  2869. }
  2870. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  2871. 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)
  2872. return
  2873. }
  2874. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  2875. 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)
  2876. return
  2877. }
  2878. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  2879. 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)
  2880. return
  2881. }
  2882. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  2883. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  2884. return goodType, err
  2885. }
  2886. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  2887. 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
  2888. return info, err
  2889. }
  2890. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  2891. 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
  2892. return
  2893. }
  2894. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  2895. 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
  2896. return
  2897. }
  2898. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2899. 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
  2900. return
  2901. }
  2902. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  2903. err = tx.Model(&models.DrugWarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ?", is_sys, org_id, record_time).First(&out).Error
  2904. if err != gorm.ErrRecordNotFound {
  2905. if err != nil {
  2906. tx.Rollback()
  2907. return
  2908. }
  2909. }
  2910. return
  2911. }
  2912. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2913. 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
  2914. return
  2915. }
  2916. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  2917. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  2918. return
  2919. }
  2920. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  2921. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  2922. return
  2923. }
  2924. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2925. info := models.WarehouseOutInfo{}
  2926. 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
  2927. if err == gorm.ErrRecordNotFound {
  2928. return nil, err
  2929. }
  2930. if err != nil {
  2931. return nil, err
  2932. }
  2933. return &info, nil
  2934. }
  2935. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2936. info := models.WarehouseOutInfo{}
  2937. 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
  2938. if err == gorm.ErrRecordNotFound {
  2939. return nil, err
  2940. }
  2941. if err != nil {
  2942. return nil, err
  2943. }
  2944. return &info, nil
  2945. }
  2946. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2947. err := XTWriteDB().Model(&prepare).Where("good_id = ? and good_type_id = ? and sys_record_time = ? and patient_id = ? and org_id = ? and status =1", goodid, goodtypeid, recordtime, patientid, orgid).Updates(map[string]interface{}{"count": prepare.Count, "over_count": prepare.OverCount}).Error
  2948. return err
  2949. }
  2950. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2951. 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
  2952. return err
  2953. }
  2954. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo) error {
  2955. err := writeDb.Create(&info).Error
  2956. return err
  2957. }
  2958. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  2959. err := tx.Create(&info).Error
  2960. if err != nil {
  2961. tx.Rollback()
  2962. return err
  2963. }
  2964. return err
  2965. }
  2966. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  2967. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  2968. return err
  2969. }
  2970. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  2971. err := writeDb.Create(&info).Error
  2972. return err
  2973. }
  2974. func AddSelfSigleDrugWarehouseOutInfo(info *models.XtSelfDrugWarehouseOutInfo, tx *gorm.DB) error {
  2975. err := tx.Create(&info).Error
  2976. if err != nil {
  2977. tx.Rollback()
  2978. return err
  2979. }
  2980. return err
  2981. }
  2982. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  2983. err := tx.Create(&info).Error
  2984. if err != nil {
  2985. tx.Rollback()
  2986. return err
  2987. }
  2988. return err
  2989. }
  2990. func UpdateSigleDrugWarehouseOutInfoDetail(id int64, count int64, tx *gorm.DB) error {
  2991. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  2992. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("over_count", gorm.Expr("over_count - ?", count)).Error
  2993. if err != nil {
  2994. tx.Rollback()
  2995. return err
  2996. }
  2997. return err
  2998. }
  2999. func UpdateSigleDrugWarehouseOutInfoDetailOne(id int64, count int64, tx *gorm.DB) error {
  3000. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"count": count}).Error
  3001. return err
  3002. }
  3003. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  3004. ut := writeDb.Begin()
  3005. err := ut.Create(&detail).Error
  3006. if err != nil {
  3007. ut.Rollback()
  3008. return err
  3009. }
  3010. ut.Commit()
  3011. return err
  3012. }
  3013. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  3014. err := writeDb.Create(&detail).Error
  3015. return err
  3016. }
  3017. func GetDrugAutoMaticReduceDetail(drug_id int64, patient_id int64, record_time int64, org_id int64, adivce_id int64, warehouse_info_id int64, tx *gorm.DB) (models.DrugAutomaticReduceDetail, error) {
  3018. detail := models.DrugAutomaticReduceDetail{}
  3019. err := tx.Where("drug_id = ? and patient_id =? and record_time =? and org_id = ? and advice_id = ? and warehouse_info_id =?", drug_id, patient_id, record_time, org_id, adivce_id, warehouse_info_id).Find(&detail).Error
  3020. if err != gorm.ErrRecordNotFound {
  3021. if err != nil {
  3022. tx.Rollback()
  3023. return detail, err
  3024. }
  3025. }
  3026. return detail, err
  3027. }
  3028. func UpdateNewSingleDrugAutoReduceRecordInfo(id int64, count int64, tx *gorm.DB) error {
  3029. err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3030. if err != nil {
  3031. tx.Rollback()
  3032. return err
  3033. }
  3034. return err
  3035. }
  3036. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  3037. err := tx.Create(&detail).Error
  3038. if err != nil {
  3039. tx.Rollback()
  3040. return err
  3041. }
  3042. return err
  3043. }
  3044. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  3045. 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
  3046. return
  3047. }
  3048. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  3049. 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
  3050. return
  3051. }
  3052. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  3053. 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
  3054. return
  3055. }
  3056. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  3057. info := models.WarehouseOutInfo{}
  3058. 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
  3059. return info, err
  3060. }
  3061. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  3062. 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
  3063. return
  3064. }
  3065. func UpdateStockOutInfoCount(org_id int64, id int64) {
  3066. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  3067. }
  3068. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3069. db := readDb.Model(&models.AutomaticReduceDetail{})
  3070. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3071. db = db.Preload("GoodsType", "status = 1")
  3072. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3073. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  3074. db = db.Count(&total)
  3075. err = db.Find(&user).Error
  3076. return
  3077. }
  3078. type DrugAutomaticReduceDetail struct {
  3079. ID int64 `gorm:"column:id" json:"id" form:"id"`
  3080. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  3081. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  3082. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  3083. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  3084. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3085. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3086. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3087. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3088. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3089. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3090. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3091. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3092. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3093. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3094. }
  3095. func (DrugAutomaticReduceDetail) TableName() string {
  3096. return "xt_drug_automatic_reduce_detail"
  3097. }
  3098. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3099. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3100. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3101. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3102. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3103. db = db.Count(&total)
  3104. err = db.Find(&user).Error
  3105. return
  3106. }
  3107. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3108. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3109. }
  3110. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3111. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3112. }
  3113. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3114. 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
  3115. return
  3116. }
  3117. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3118. 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
  3119. return
  3120. }
  3121. type VMWarehousingInfo struct {
  3122. ID int64 `gorm:"column:id" json:"id"`
  3123. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3124. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3125. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3126. Status int64 `gorm:"column:status" json:"status"`
  3127. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3128. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3129. }
  3130. func (VMWarehousingInfo) TableName() string {
  3131. return "xt_warehouse_info"
  3132. }
  3133. type GoodInfo struct {
  3134. ID int64 `gorm:"column:id" json:"id"`
  3135. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3136. Status int64 `gorm:"column:status" json:"status"`
  3137. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3138. }
  3139. func (GoodInfo) TableName() string {
  3140. return "xt_good_information"
  3141. }
  3142. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3143. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3144. return
  3145. }
  3146. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3147. 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
  3148. return
  3149. }
  3150. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3151. likeKey := "%" + keyword + "%"
  3152. db := XTReadDB().Model(&goods)
  3153. if len(keyword) > 0 {
  3154. db = db.Where("type_name like ?", likeKey)
  3155. }
  3156. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3157. return goods, err
  3158. }
  3159. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3160. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3161. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3162. return goodstype, err
  3163. }
  3164. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3165. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3166. if orgid > 0 {
  3167. db = db.Where("x.user_org_id = ?", orgid)
  3168. }
  3169. if recordtime > 0 {
  3170. db = db.Where("x.record_date = ?", recordtime)
  3171. }
  3172. if patientId > 0 {
  3173. db = db.Where("x.patient_id = ?", patientId)
  3174. }
  3175. 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
  3176. return prepare, err
  3177. }
  3178. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3179. detail := models.XtAutomaticReduceDetail{}
  3180. 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
  3181. if err == gorm.ErrRecordNotFound {
  3182. return nil, err
  3183. }
  3184. if err != nil {
  3185. return nil, err
  3186. }
  3187. return &detail, nil
  3188. }
  3189. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3190. 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
  3191. return autoMatic, err
  3192. }
  3193. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3194. 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
  3195. return autoMatic, err
  3196. }
  3197. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3198. var manufacturer models.Manufacturer
  3199. var err error
  3200. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3201. if err == gorm.ErrRecordNotFound {
  3202. return nil, err
  3203. }
  3204. if err != nil {
  3205. return nil, err
  3206. }
  3207. return &manufacturer, nil
  3208. }
  3209. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3210. err := XTWriteDB().Create(manufacturer).Error
  3211. return err
  3212. }
  3213. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3214. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3215. return err
  3216. }
  3217. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3218. var good models.GoodInfo
  3219. var err error
  3220. 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
  3221. if err == gorm.ErrRecordNotFound {
  3222. return nil, err
  3223. }
  3224. if err != nil {
  3225. return nil, err
  3226. }
  3227. return &good, nil
  3228. }
  3229. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3230. err := XTWriteDB().Create(&info).Error
  3231. return err
  3232. }
  3233. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3234. err := XTWriteDB().Model(&info).Where("good_name = ? and good_type_id = ? and specification_name = ? and org_id = ? and status = 1", goodName, goodType, specificationName, orgid).Updates(map[string]interface{}{"good_name": info.GoodName, "good_type_id": info.GoodTypeId, "medical_insurance_level": info.MedicalInsuranceLevel, "good_kind": info.GoodKind, "specification_name": info.SpecificationName, "manufacturer": info.Manufacturer, "good_unit": info.GoodUnit, "retail_price": info.RetailPrice, "stock_warn_count": info.StockWarnCount, "dealer": info.Dealer, "pinyin": info.Pinyin, "wubi": info.Wubi, "buy_price": info.BuyPrice, "medical_insurance_number": info.MedicalInsuranceNumber, "is_special_diseases": info.IsSpecialDiseases, "is_record": info.IsRecord, "statistics_category": info.StatisticsCategory, "good_status": info.GoodStatus, "social_security_directory_code": info.SocialSecurityDirectoryCode, "production_type": info.ProductionType, "special_medical": info.SpecialMedical, "remark": info.Remark, "number": info.Number, "register_number": info.RegisterNumber}).Error
  3235. return err
  3236. }
  3237. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3238. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3239. 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
  3240. return houseOutInfo, err
  3241. }
  3242. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3243. key := "%" + keyword + "%"
  3244. if len(keyword) == 0 {
  3245. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3246. } else {
  3247. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3248. }
  3249. return goodInfo, err
  3250. }
  3251. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3252. var drug models.BaseDrugLib
  3253. var err error
  3254. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3255. if err == gorm.ErrRecordNotFound {
  3256. return nil, err
  3257. }
  3258. if err != nil {
  3259. return nil, err
  3260. }
  3261. return &drug, nil
  3262. }
  3263. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3264. var drug models.BaseDrugLib
  3265. var err error
  3266. 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
  3267. if err == gorm.ErrRecordNotFound {
  3268. return nil, err
  3269. }
  3270. if err != nil {
  3271. return nil, err
  3272. }
  3273. return &drug, nil
  3274. }
  3275. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3276. err := XTWriteDB().Create(&info).Error
  3277. return err
  3278. }
  3279. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3280. err := XTWriteDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and org_id = ? and status = 1", drugName, drugSpec, orgid).Updates(map[string]interface{}{"drug_name": drug.DrugName, "drug_alias": drug.DrugAlias, "drug_spec": drug.DrugSpec, "drug_type": drug.DrugType, "drug_stock_limit": drug.DrugStockLimit, "drug_origin_place": drug.DrugOriginPlace, "drug_dosage_form": drug.DrugDosageForm, "medical_insurance_level": drug.MedicalInsuranceLevel, "max_unit": drug.MaxUnit, "min_unit": drug.MinUnit, "unit_matrixing": drug.UnitMatrixing, "retail_price": drug.RetailPrice, "last_price": drug.LastPrice, "drug_classify": drug.DrugClassify, "manufacturer": drug.Manufacturer, "dealer": drug.Dealer, "pinyin": drug.Pinyin, "wubi": drug.Wubi, "drug_alias_pinyin": drug.DrugAliasPinyin, "drug_alias_wubi": drug.DrugAliasWubi, "drug_category": drug.DrugCategory, "drug_control": drug.DrugControl, "number": drug.Number, "hosp_appr_flag": drug.HospApprFlag, "medical_insurance_number": drug.MedicalInsuranceNumber, "pharmacology_category": drug.PharmacologyCategory, "statistics_category": drug.StatisticsCategory, "code": drug.Code, "is_special_diseases": drug.IsSpecialDiseases, "is_record": drug.IsRecord, "prescription_mark": drug.PrescriptionMark, "social_security_directory_code": drug.SocialSecurityDirectoryCode, "record_date": drug.RecordDate, "drug_remark": drug.DrugRemark, "drug_status": drug.DrugStatus, "limit_remark": drug.LimitRemark, "drug_dose": drug.DrugDose, "single_dose": drug.DrugDose, "drug_dose_unit": drug.DrugDoseUnit, "lmt_used_flag": drug.LmtUsedFlag, "delivery_way": drug.DeliveryWay, "execution_frequency": drug.ExecutionFrequency, "drug_day": drug.DrugDay}).Error
  3281. return err
  3282. }
  3283. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3284. var projoect models.HisProject
  3285. var err error
  3286. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3287. if err == gorm.ErrRecordNotFound {
  3288. return nil, err
  3289. }
  3290. if err != nil {
  3291. return nil, err
  3292. }
  3293. return &projoect, nil
  3294. }
  3295. func CreateProjectInfomation(info *models.HisProject) error {
  3296. err := XTWriteDB().Create(&info).Error
  3297. return err
  3298. }
  3299. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3300. 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
  3301. return err
  3302. }
  3303. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3304. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3305. return warehouseinfo, err
  3306. }
  3307. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3308. 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
  3309. return err
  3310. }
  3311. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3312. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3313. return list, err
  3314. }
  3315. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3316. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3317. return list, err
  3318. }
  3319. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3320. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3321. return list, err
  3322. }
  3323. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3324. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3325. // return list, err
  3326. // }
  3327. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3328. var dealer models.Dealer
  3329. var err error
  3330. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3331. if err == gorm.ErrRecordNotFound {
  3332. return nil, err
  3333. }
  3334. if err != nil {
  3335. return nil, err
  3336. }
  3337. return &dealer, nil
  3338. }
  3339. func CreateDealer(dealer *models.Dealer) error {
  3340. err := XTWriteDB().Create(&dealer).Error
  3341. return err
  3342. }
  3343. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3344. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3345. return err
  3346. }
  3347. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3348. dataconfig := models.DictDataconfig{}
  3349. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3350. return dataconfig, err
  3351. }
  3352. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3353. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3354. return dataconfig, err
  3355. }
  3356. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3357. var dataconfig models.DictDataconfig
  3358. var err error
  3359. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3360. if err == gorm.ErrRecordNotFound {
  3361. return nil, err
  3362. }
  3363. if err != nil {
  3364. return nil, err
  3365. }
  3366. return &dataconfig, nil
  3367. }
  3368. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3369. dataconfig := models.DictDataconfig{}
  3370. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3371. return dataconfig, err
  3372. }
  3373. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3374. err := XTWriteDB().Create(&dataconfig).Error
  3375. return err
  3376. }
  3377. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3378. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3379. return err
  3380. }
  3381. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3382. var goodType models.GoodsType
  3383. var err error
  3384. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3385. if err == gorm.ErrRecordNotFound {
  3386. return nil, err
  3387. }
  3388. if err != nil {
  3389. return nil, err
  3390. }
  3391. return &goodType, nil
  3392. }
  3393. func CreatedGoodType(goodsType *models.GoodsType) error {
  3394. err := XTWriteDB().Create(&goodsType).Error
  3395. return err
  3396. }
  3397. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3398. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3399. return goodsType, err
  3400. }
  3401. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3402. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3403. return goodsType, err
  3404. }
  3405. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3406. var department models.XtHisDepartment
  3407. var err error
  3408. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3409. if err == gorm.ErrRecordNotFound {
  3410. return nil, err
  3411. }
  3412. if err != nil {
  3413. return nil, err
  3414. }
  3415. return &department, nil
  3416. }
  3417. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3418. dataconfig := models.Dataconfig{}
  3419. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3420. return dataconfig, err
  3421. }
  3422. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3423. var dataconfig models.Dataconfig
  3424. var err error
  3425. 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
  3426. if err == gorm.ErrRecordNotFound {
  3427. return nil, err
  3428. }
  3429. if err != nil {
  3430. return nil, err
  3431. }
  3432. return &dataconfig, nil
  3433. }
  3434. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3435. dataconfig := models.Dataconfig{}
  3436. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3437. return dataconfig, err
  3438. }
  3439. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3440. err := XTWriteDB().Create(&dataconfig).Error
  3441. return err
  3442. }
  3443. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3444. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3445. return dataconfig, err
  3446. }
  3447. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3448. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3449. return list, err
  3450. }
  3451. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3452. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3453. if len(keywords) > 0 {
  3454. likekey := "%" + keywords + "%"
  3455. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3456. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3457. }).Find(&drug).Error
  3458. } else {
  3459. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3460. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3461. }).Find(&drug).Error
  3462. }
  3463. return
  3464. }
  3465. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3466. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3467. if len(keywords) > 0 {
  3468. likekey := "%" + keywords + "%"
  3469. err = db.Where("(drug_name LIKE ?)", likekey).Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3470. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3471. }).Find(&drug).Error
  3472. } else {
  3473. err = db.Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3474. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3475. }).Find(&drug).Error
  3476. }
  3477. return
  3478. }
  3479. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3480. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3481. if len(keywords) > 0 {
  3482. likekey := "%" + keywords + "%"
  3483. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3484. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3485. }).Find(&good).Error
  3486. } else {
  3487. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3488. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3489. }).Find(&good).Error
  3490. }
  3491. return
  3492. }
  3493. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3494. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3495. if len(keywords) > 0 {
  3496. likekey := "%" + keywords + "%"
  3497. err = db.Where("(good_name Like ?)", likekey).Preload("LaStWarehousingInfo", "org_id=? and status = 1 and stock_count <> 0 and storehouse_id =?", orgid, storehouse_id).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3498. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3499. }).Find(&good).Error
  3500. } else {
  3501. err = db.Preload("LaStWarehousingInfo", "org_id=? and status = 1 and stock_count <> 0 and storehouse_id =?", orgid, storehouse_id).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3502. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3503. }).Find(&good).Error
  3504. }
  3505. return
  3506. }
  3507. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3508. db := writeDb.Begin()
  3509. err := db.Create(&detail).Error
  3510. if err != nil {
  3511. db.Rollback()
  3512. return err
  3513. }
  3514. db.Commit()
  3515. return err
  3516. }
  3517. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3518. var autoreduece models.BloodAutomaticReduceDetail
  3519. var err error
  3520. 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
  3521. if err == gorm.ErrRecordNotFound {
  3522. return nil, err
  3523. }
  3524. if err != nil {
  3525. return nil, err
  3526. }
  3527. return &autoreduece, nil
  3528. }
  3529. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3530. auto := models.BloodAutomaticReduceDetail{}
  3531. err = XTReadDB().Model(&auto).Where("org_id = ? and patient_id = ? and status = 1 and record_time = ? and good_id = ? and good_type_id = ? and count>0", orgid, patient_id, recordtime, goodid, goodtypeid).Find(&auto).Error
  3532. return auto, err
  3533. }
  3534. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3535. detail := models.BloodAutomaticReduceDetail{}
  3536. 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
  3537. return err
  3538. }
  3539. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3540. 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
  3541. return stockflow, err
  3542. }
  3543. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3544. err = XTWriteDB().Model(models.BloodAutomaticReduceDetail{}).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ?", orgid, patient_id, recordtime, goodid).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3545. err = XTWriteDB().Model(models.WarehouseOutInfo{}).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3546. err = XTWriteDB().Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? and patient_id = ? and record_date = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  3547. return err
  3548. }
  3549. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3550. detail := models.BloodAutomaticReduceDetail{}
  3551. 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
  3552. info := models.WarehouseOutInfo{}
  3553. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3554. prepare := models.DialysisBeforePrepare{}
  3555. 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
  3556. flow := models.VmStockFlow{}
  3557. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"is_read": 1}).Error
  3558. return err
  3559. }
  3560. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3561. detail := models.BloodAutomaticReduceDetail{}
  3562. err := XTWriteDB().Model(&detail).Where("org_id = ? and patient_id = ? and record_time = ? and status =1 and good_id = ? and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3563. info := models.WarehouseOutInfo{}
  3564. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3565. prepare := models.DialysisBeforePrepare{}
  3566. 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
  3567. flow := models.VmStockFlow{}
  3568. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and project_id = ?", orgid, patient_id, recordtime, goodid, project_id).Updates(map[string]interface{}{"is_read": 1}).Error
  3569. return err
  3570. }
  3571. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  3572. detail := models.BloodAutomaticReduceDetail{}
  3573. 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
  3574. info := models.WarehouseOutInfo{}
  3575. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and order_id = ?", orgid, patient_id, recordtime, goodid, order_id).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3576. prepare := models.DialysisBeforePrepare{}
  3577. 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
  3578. flow := models.VmStockFlow{}
  3579. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and order_id = ?", orgid, patient_id, recordtime, goodid, order_id).Updates(map[string]interface{}{"is_read": 1}).Error
  3580. return err
  3581. }
  3582. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  3583. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3584. //err = writeDb.Model(&models.VmStockFlow{}).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", id, patient_id, record_time, goodid).UpdateColumn("over_count", gorm.Expr("over_count + ?", count)).Error
  3585. return err
  3586. }
  3587. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3588. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  3589. return info, err
  3590. }
  3591. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  3592. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  3593. return list, err
  3594. }
  3595. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  3596. 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
  3597. return err
  3598. }
  3599. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  3600. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  3601. return err
  3602. }
  3603. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  3604. var autoreduece models.BloodAutomaticReduceDetail
  3605. var err error
  3606. 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
  3607. if err == gorm.ErrRecordNotFound {
  3608. return nil, err
  3609. }
  3610. if err != nil {
  3611. return nil, err
  3612. }
  3613. return &autoreduece, nil
  3614. }
  3615. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  3616. detail := models.WarehouseOutInfoSeven{}
  3617. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3618. return detail, err
  3619. }
  3620. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  3621. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3622. return detail, err
  3623. }
  3624. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  3625. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  3626. return project, err
  3627. }
  3628. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  3629. project := models.HisPrescriptionProject{}
  3630. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  3631. return project, err
  3632. }
  3633. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  3634. err = XTReadDB().Select("count,warehouse_info_id,good_id,org_id,sys_record_time,patient_id,project_id,warehouse_info_id,warehouse_out_order_number,storehouse_id,price,dealer,manufacturer,good_type_id,product_date,expiry_date,number,license_number").Where(" patient_id = ? and sys_record_time = ? and status = 1 and is_sys = 1 and count>0", patientid, recorddate).Find(&auto).Error
  3635. return auto, err
  3636. }
  3637. func ModefyWarehouseInfo(count int64, id int64) error {
  3638. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3639. return err
  3640. }
  3641. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  3642. 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
  3643. 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
  3644. err = XTWriteDB().Model(models.VmStockFlow{}).Where("patient_id = ? and system_time = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"is_read": 1}).Error
  3645. //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
  3646. return err
  3647. }
  3648. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  3649. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3650. return
  3651. }
  3652. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  3653. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3654. return
  3655. }
  3656. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  3657. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3658. return info, err
  3659. }
  3660. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3661. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3662. return info, err
  3663. }
  3664. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3665. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0 and storehouse_id = ?", good_id, storehouse_id).Find(&info).Error
  3666. return info, err
  3667. }
  3668. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3669. offset := (page - 1) * limit
  3670. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  3671. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3672. fmt.Println(table)
  3673. if startime > 0 {
  3674. db = db.Where("x.ctime>=?", startime)
  3675. }
  3676. if endtime > 0 {
  3677. db = db.Where("x.ctime<=?", endtime)
  3678. }
  3679. likeKey := "%" + keyword + "%"
  3680. if len(likeKey) > 0 {
  3681. db = db.Where("t.good_name like ?", likeKey)
  3682. }
  3683. if orgid > 0 {
  3684. db = db.Where("x.org_id = ?", orgid)
  3685. }
  3686. if good_type > 0 {
  3687. db = db.Where("t.good_type_id = ?", good_type)
  3688. }
  3689. 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
  3690. return info, total, err
  3691. }
  3692. 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) {
  3693. offset := (page - 1) * limit
  3694. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  3695. likeKey := "%" + keyword + "%"
  3696. if len(likeKey) > 0 {
  3697. db = db.Where("x.good_name like ?", likeKey)
  3698. }
  3699. if orgid > 0 {
  3700. db = db.Where("x.org_id = ?", orgid)
  3701. }
  3702. if good_type > 0 {
  3703. db = db.Where("x.good_type_id = ?", good_type)
  3704. }
  3705. if good_id > 0 {
  3706. db = db.Where("x.id = ?", good_id)
  3707. }
  3708. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3709. if startime > 0 {
  3710. db = db.Where("ctime>=?", startime)
  3711. }
  3712. if endtime > 0 {
  3713. db = db.Where("ctime<=?", endtime)
  3714. }
  3715. if good_id > 0 {
  3716. db = db.Where("good_id = ?", good_id)
  3717. }
  3718. if storehouse_id > 0 {
  3719. db = db.Where("storehouse_id = ?", storehouse_id)
  3720. }
  3721. return db.Where("status = 1 and is_check = 1")
  3722. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3723. if startime > 0 {
  3724. db = db.Where("ctime>=?", startime)
  3725. }
  3726. if endtime > 0 {
  3727. db = db.Where("ctime<=?", endtime)
  3728. }
  3729. if good_id > 0 {
  3730. db = db.Where("good_id = ?", good_id)
  3731. }
  3732. if storehouse_id > 0 {
  3733. db = db.Where("storehouse_id = ?", storehouse_id)
  3734. }
  3735. return db.Where("status = 1").Group("good_id,storehouse_id")
  3736. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3737. if startime > 0 {
  3738. db = db.Where("ctime>=?", startime)
  3739. }
  3740. if endtime > 0 {
  3741. db = db.Where("ctime<=?", endtime)
  3742. }
  3743. return db.Where("status = 1")
  3744. }).Find(&info).Error
  3745. return info, total, err
  3746. }
  3747. func GetStockListById(good_id int64, orgid int64, limit int64, page int64, startime int64, endtime int64, startfirsttime int64, endfirsttime int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3748. offset := (page - 1) * limit
  3749. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  3750. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3751. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  3752. fmt.Println(table, tables)
  3753. if startime > 0 {
  3754. db = db.Where("x.ctime>=?", startime)
  3755. }
  3756. if endtime > 0 {
  3757. db = db.Where("x.ctime<=?", endtime)
  3758. }
  3759. if startfirsttime > 0 {
  3760. db = db.Where("x.expiry_date >=?", startfirsttime)
  3761. }
  3762. if endfirsttime > 0 {
  3763. db = db.Where("x.expiry_date <=?", endfirsttime)
  3764. }
  3765. if orgid > 0 {
  3766. db = db.Where("x.org_id = ?", orgid)
  3767. }
  3768. if good_id > 0 {
  3769. db = db.Where("x.good_id = ?", good_id)
  3770. }
  3771. err = db.Select("x.id,x.warehousing_id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehousing_unit,x.stock_count,x.price,x.total_price,x.dealer,x.packing_price,t.manufacturer,x.remark,x.ctime,x.is_return,x.warehousing_order,x.type,x.storehouse_id,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.min_unit,t.buy_price,f.batch_number_count").Joins("left join xt_good_information as t on t.id = x.good_id").Joins("left join xt_stock_flow as f on f.warehousing_detail_id = x.id").Count(&total).Offset(offset).Limit(limit).Group("x.id").Order("x.id desc").Scan(&info).Error
  3772. return info, total, err
  3773. }
  3774. 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) {
  3775. offset := (page - 1) * limit
  3776. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  3777. if startime > 0 {
  3778. db = db.Where("x.ctime>=?", startime)
  3779. }
  3780. if endtime > 0 {
  3781. db = db.Where("x.ctime<=?", endtime)
  3782. }
  3783. if orgid > 0 {
  3784. db = db.Where("x.org_id = ?", orgid)
  3785. }
  3786. if good_id > 0 {
  3787. db = db.Where("x.good_id = ?", good_id)
  3788. }
  3789. if is_sys == 2 {
  3790. db = db.Where("x.is_sys = 1")
  3791. }
  3792. if is_sys == 3 {
  3793. db = db.Where("x.is_sys = 0")
  3794. }
  3795. 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
  3796. return info, total, err
  3797. }
  3798. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3799. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3800. if startime > 0 {
  3801. db = db.Where("x.ctime >=?", startime)
  3802. }
  3803. if endtime > 0 {
  3804. db = db.Where("x.ctime<=?", endtime)
  3805. }
  3806. if orgid > 0 {
  3807. db = db.Where("x.org_id = ?", orgid)
  3808. }
  3809. 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
  3810. return info, err
  3811. }
  3812. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3813. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3814. if startime > 0 {
  3815. db = db.Where("x.ctime >=?", startime)
  3816. }
  3817. if endtime > 0 {
  3818. db = db.Where("x.ctime<=?", endtime)
  3819. }
  3820. if orgid > 0 {
  3821. db = db.Where("x.org_id = ?", orgid)
  3822. }
  3823. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  3824. return info, err
  3825. }
  3826. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3827. if endtime > 0 {
  3828. 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
  3829. }
  3830. if endtime == 0 {
  3831. 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
  3832. }
  3833. return autoMatic, err
  3834. }
  3835. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  3836. out := models.WarehouseOut{}
  3837. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  3838. return out, err
  3839. }
  3840. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3841. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3842. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3843. fmt.Println(table)
  3844. if id > 0 {
  3845. db = db.Where("x.warehouse_out_id = ?", id)
  3846. }
  3847. if orgid > 0 {
  3848. db = db.Where("x.org_id = ?", orgid)
  3849. }
  3850. err = db.Select("x.id,x.warehouse_out_id,x.warehouse_info_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,x.patient_id,t.manufacturer,x.is_sys,x.sys_record_time,x.number,x.remark,x.license_number,x.supply_cancel_out_id,x.supply_warehouse_id,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.register_number,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.good_id,x.warehouse_info_id").Order("x.ctime desc").Scan(&out).Error
  3851. return out, err
  3852. }
  3853. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  3854. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  3855. if len(id) > 0 {
  3856. db = db.Where("x.id in(?)", id)
  3857. }
  3858. if orgid > 0 {
  3859. db = db.Where("x.org_id= ?", orgid)
  3860. }
  3861. 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
  3862. return out, err
  3863. }
  3864. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3865. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3866. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3867. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  3868. fmt.Println(table, tableOne)
  3869. if len(id) > 0 {
  3870. db = db.Where("x.warehouse_out_id in (?)", id)
  3871. }
  3872. if orgid > 0 {
  3873. db = db.Where("x.org_id = ?", orgid)
  3874. }
  3875. err = db.Select("x.id,x.warehouse_out_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,x.manufacturer,t.social_security_directory_code,x.is_sys,x.sys_record_time,n.number,x.remark,x.license_number,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.warehouse_info_id,x.remark,x.patient_id,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit,t.packing_price,x.register_number,x.class_type").Joins("left join xt_warehouse_info as n on n.id=x.warehouse_info_id").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.warehouse_out_id,x.good_id").Order("x.ctime desc").Scan(&out).Error
  3876. return out, err
  3877. }
  3878. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3879. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3880. if len(id) > 0 {
  3881. db = db.Where("warehouse_out_id in (?)", id)
  3882. }
  3883. if orgid > 0 {
  3884. db = db.Where("user_org_id = ?", orgid)
  3885. }
  3886. err = db.Group("warehousing_id").Find(&list).Error
  3887. return list, err
  3888. }
  3889. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3890. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  3891. if len(id) > 0 {
  3892. db = db.Where("warehouse_out_id in (?)", id)
  3893. }
  3894. if orgid > 0 {
  3895. db = db.Where("user_org_id = ?", orgid)
  3896. }
  3897. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3898. return list, err
  3899. }
  3900. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  3901. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  3902. if len(id) > 0 {
  3903. db = db.Where("warehouse_out_id in (?)", id)
  3904. }
  3905. if orgid > 0 {
  3906. db = db.Where("user_org_id = ?", orgid)
  3907. }
  3908. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  3909. return list, err
  3910. }
  3911. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  3912. db := XTReadDB().Model(&info)
  3913. db = db.Preload("GoodInfo", "status = 1")
  3914. 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
  3915. return info, err
  3916. }
  3917. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3918. db := readDb.Model(&models.AutomaticReduceDetail{})
  3919. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3920. db = db.Preload("GoodsType", "status = 1")
  3921. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3922. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  3923. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  3924. db = db.Count(&total)
  3925. err = db.Find(&user).Error
  3926. return
  3927. }
  3928. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  3929. info := models.GoodInfo{}
  3930. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3931. return info, err
  3932. }
  3933. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  3934. info := models.GoodInformationThirty{}
  3935. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3936. return info, err
  3937. }
  3938. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  3939. info := models.GoodInformationThirty{}
  3940. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  3941. if err != gorm.ErrRecordNotFound {
  3942. if err != nil {
  3943. tx.Rollback()
  3944. return info, err
  3945. }
  3946. }
  3947. return info, err
  3948. }
  3949. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  3950. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  3951. return err
  3952. }
  3953. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  3954. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3955. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3956. fmt.Println(table)
  3957. if orgid > 0 {
  3958. db = db.Where("x.org_id = ?", orgid)
  3959. }
  3960. if len(orderid) > 0 {
  3961. db = db.Where("x.warehousing_id in(?)", orderid)
  3962. }
  3963. if startime > 0 {
  3964. db = db.Where("x.ctime>=?", startime)
  3965. }
  3966. if endtime > 0 {
  3967. db = db.Where("x.ctime<=?", endtime)
  3968. }
  3969. 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
  3970. return info, err
  3971. }
  3972. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  3973. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3974. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3975. fmt.Println(table)
  3976. if orgid > 0 {
  3977. db = db.Where("x.org_id = ?", orgid)
  3978. }
  3979. if len(orderid) > 0 {
  3980. db = db.Where("x.warehousing_id in(?)", orderid)
  3981. }
  3982. if startime > 0 {
  3983. db = db.Where("x.ctime>=?", startime)
  3984. }
  3985. if endtime > 0 {
  3986. db = db.Where("x.ctime<=?", endtime)
  3987. }
  3988. if storehouse_id > 0 {
  3989. db = db.Where("x.storehouse_id = ?", storehouse_id)
  3990. }
  3991. err = db.Select("x.id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.dealer,x.manufacturer,x.remark,x.license_number,t.good_name,t.packing_unit,t.specification_name,p.warehousing_time").Joins("left join xt_warehouse as p on p.id =x.warehousing_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&info).Error
  3992. return info, err
  3993. }
  3994. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3995. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3996. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  3997. fmt.Println(table)
  3998. if startime > 0 {
  3999. db = db.Where("x.ctime>=?", startime)
  4000. }
  4001. if endtime > 0 {
  4002. db = db.Where("x.ctime<=?", endtime)
  4003. }
  4004. if orgid > 0 {
  4005. db = db.Where("x.org_id = ?", orgid)
  4006. }
  4007. 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
  4008. return info, err
  4009. }
  4010. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  4011. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  4012. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4013. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  4014. fmt.Println(table, tableOne)
  4015. if orgid > 0 {
  4016. db = db.Where("x.org_id = ?", orgid)
  4017. }
  4018. if len(ids) > 0 {
  4019. db = db.Where("x.warehouse_out_id in (?)", ids)
  4020. }
  4021. if startime > 0 {
  4022. db = db.Where("x.ctime>=?", startime)
  4023. }
  4024. if endtime > 0 {
  4025. db = db.Where("x.ctime<=?", endtime)
  4026. }
  4027. err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.number,x.license_number,x.warehouse_out_order_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,x.manufacturer,x.dealer,p.warehouse_out_time").Joins("left join xt_warehouse_out as p on p.id =x.warehouse_out_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&out).Error
  4028. return out, err
  4029. }
  4030. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  4031. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  4032. if len(ids) > 0 {
  4033. db = db.Where("x.id in(?)", ids)
  4034. }
  4035. if orgid > 0 {
  4036. db = db.Where("x.org_id = ?", orgid)
  4037. }
  4038. 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
  4039. return info, err
  4040. }
  4041. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4042. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  4043. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  4044. fmt.Println(table)
  4045. if len(ids) > 0 {
  4046. db = db.Where("x.warehouse_out_id in(?)", ids)
  4047. }
  4048. if orgid > 0 {
  4049. db = db.Where("x.org_id = ?", orgid)
  4050. }
  4051. 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
  4052. return info, err
  4053. }
  4054. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  4055. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  4056. likeKey := "%" + keyword + "%"
  4057. if len(keyword) > 0 {
  4058. db = db.Where("x.drug_name like ?", likeKey)
  4059. }
  4060. if orgid > 0 {
  4061. db = db.Where("x.org_id = ?", orgid)
  4062. }
  4063. err = db.Find(&basedrug).Error
  4064. return basedrug, err
  4065. }
  4066. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4067. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4068. if len(ids) > 0 {
  4069. db = db.Where("x.drug_id in(?)", ids)
  4070. }
  4071. if orgid > 0 {
  4072. db = db.Where("x.org_id = ?", orgid)
  4073. }
  4074. if startime > 0 {
  4075. db = db.Where("x.ctime>=?", startime)
  4076. }
  4077. if endtime > 0 {
  4078. db = db.Where("x.ctime<=?", endtime)
  4079. }
  4080. if storehouse_id > 0 {
  4081. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4082. }
  4083. err = db.Find(&info).Error
  4084. return info, err
  4085. }
  4086. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4087. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4088. if len(ids) > 0 {
  4089. db = db.Where("x.drug_id in(?)", ids)
  4090. }
  4091. if orgid > 0 {
  4092. db = db.Where("x.org_id = ?", orgid)
  4093. }
  4094. if startime > 0 {
  4095. db = db.Where("x.ctime>=?", startime)
  4096. }
  4097. if endtime > 0 {
  4098. db = db.Where("x.ctime<=?", endtime)
  4099. }
  4100. if storehouse_id > 0 {
  4101. db = db.Where("storehouse_id = ?", storehouse_id)
  4102. }
  4103. err = db.Find(&info).Error
  4104. return info, err
  4105. }
  4106. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4107. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4108. if drugid > 0 {
  4109. db = db.Where("x.drug_id = ?", drugid)
  4110. }
  4111. if orgid > 0 {
  4112. db = db.Where("x.org_id = ?", orgid)
  4113. }
  4114. if storehouse_id > 0 {
  4115. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4116. }
  4117. 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
  4118. return info, err
  4119. }
  4120. func GetDrugTotalCountTwoTwety(drugid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info models.VmDrugWarehouseInfo, err error) {
  4121. db := tx.Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4122. if drugid > 0 {
  4123. db = db.Where("x.drug_id = ?", drugid)
  4124. }
  4125. if orgid > 0 {
  4126. db = db.Where("x.org_id = ?", orgid)
  4127. }
  4128. if storehouse_id > 0 {
  4129. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4130. }
  4131. 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
  4132. if err != gorm.ErrRecordNotFound {
  4133. if err != nil {
  4134. tx.Rollback()
  4135. return info, err
  4136. }
  4137. }
  4138. return info, err
  4139. }
  4140. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4141. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4142. if drugid > 0 {
  4143. db = db.Where("x.drug_id = ?", drugid)
  4144. }
  4145. if orgid > 0 {
  4146. db = db.Where("x.org_id = ?", orgid)
  4147. }
  4148. if storehouse_id > 0 {
  4149. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4150. }
  4151. err = db.Select("x.drug_id,sum(x.stock_max_number) as count,sum(x.stock_min_number) as stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4152. return info, err
  4153. }
  4154. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4155. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4156. if id > 0 {
  4157. db = db.Where("x.id = ?", id)
  4158. }
  4159. if orgid > 0 {
  4160. db = db.Where("x.org_id = ?", orgid)
  4161. }
  4162. 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
  4163. return info, err
  4164. }
  4165. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4166. info := models.VmDrugWarehouseInfo{}
  4167. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4168. if id == 0 {
  4169. db = db.Where("id = ?", id)
  4170. }
  4171. if drugid > 0 {
  4172. db = db.Where("x.drug_id = ?", drugid)
  4173. }
  4174. if orgid > 0 {
  4175. db = db.Where("x.org_id = ?", orgid)
  4176. }
  4177. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4178. return info, err
  4179. }
  4180. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4181. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4182. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4183. fmt.Println(table)
  4184. if drugid > 0 {
  4185. db = db.Where("x.drug_id = ?", drugid)
  4186. }
  4187. if storehouse_id > 0 {
  4188. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4189. }
  4190. 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
  4191. return list, err
  4192. }
  4193. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4194. lib := models.BaseDrugLib{}
  4195. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4196. return lib, err
  4197. }
  4198. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4199. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4200. if goodid > 0 {
  4201. db = db.Where("x.good_id = ?", goodid)
  4202. }
  4203. if orgid > 0 {
  4204. db = db.Where("x.org_id = ?", orgid)
  4205. }
  4206. 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
  4207. return info, err
  4208. }
  4209. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4210. 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
  4211. return err
  4212. }
  4213. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4214. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4215. return info, err
  4216. }
  4217. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4218. //遍历循环入库数据
  4219. for i, item := range list {
  4220. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4221. if (item.WarehousingCount - item.StockCount) >= count {
  4222. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4223. info := models.WarehousingInfo{
  4224. GoodId: item.GoodId,
  4225. OrgId: item.OrgId,
  4226. WarehousingId: item.WarehousingId,
  4227. StockCount: item.StockCount + count,
  4228. }
  4229. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4230. fmt.Println("2323233232323232323232323232323", err2)
  4231. return err
  4232. }
  4233. if (item.WarehousingCount - item.StockCount) < count {
  4234. //如果该批次小于退库数量,则先出库该相差数量
  4235. var countOne = item.WarehousingCount - item.StockCount
  4236. infoOne := models.WarehousingInfo{
  4237. GoodId: item.GoodId,
  4238. OrgId: item.OrgId,
  4239. WarehousingId: item.WarehousingId,
  4240. StockCount: countOne + item.StockCount,
  4241. }
  4242. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4243. if err == nil {
  4244. //剩余的出库到第二个批次
  4245. var countTwo = count - countOne
  4246. infos := append(list, list[i+1])
  4247. var idArray []*models.WarehousingInfo
  4248. for _, it := range infos {
  4249. if it.WarehousingId != item.WarehousingId {
  4250. idArray = append(idArray, it)
  4251. }
  4252. }
  4253. //查询更新后的库存
  4254. UpdateCancelOut(idArray, countTwo)
  4255. return err
  4256. } else {
  4257. return err
  4258. }
  4259. }
  4260. }
  4261. return err
  4262. }
  4263. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4264. if len(idstr) > 0 {
  4265. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4266. } else {
  4267. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4268. }
  4269. return info, err
  4270. }
  4271. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4272. if len(idstr) > 0 {
  4273. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4274. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4275. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4276. }).Find(&info).Error
  4277. }
  4278. return info, err
  4279. }
  4280. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4281. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4282. if startime > 0 {
  4283. db = db.Where("x.ctime >=?", startime)
  4284. }
  4285. if endtime > 0 {
  4286. db = db.Where("x.ctime <= ?", endtime)
  4287. }
  4288. if orgid > 0 {
  4289. db = db.Where("x.org_id = ?", orgid)
  4290. }
  4291. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4292. return info, err
  4293. }
  4294. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4295. info := models.WarehouseOutInfo{}
  4296. var err error
  4297. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4298. if err == gorm.ErrRecordNotFound {
  4299. return nil, err
  4300. }
  4301. if err != nil {
  4302. return nil, err
  4303. }
  4304. return &info, nil
  4305. }
  4306. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4307. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4308. if goodid > 0 {
  4309. db = db.Where("x.good_id = ?", goodid)
  4310. }
  4311. if orgid > 0 {
  4312. db = db.Where("x.org_id = ?", orgid)
  4313. }
  4314. if id > 0 {
  4315. db = db.Where("x.warehouse_info_id = ?", id)
  4316. }
  4317. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4318. return info, err
  4319. }
  4320. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4321. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4322. if goodid > 0 {
  4323. db = db.Where("x.good_id = ?", goodid)
  4324. }
  4325. if orgid > 0 {
  4326. db = db.Where("x.user_org_id = ?", orgid)
  4327. }
  4328. if id > 0 {
  4329. db = db.Where("x.warehousing_id = ?", id)
  4330. }
  4331. if storehouse_id > 0 {
  4332. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4333. }
  4334. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4335. return info, err
  4336. }
  4337. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4338. stock := models.CancelStock{}
  4339. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4340. return stock, err
  4341. }
  4342. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4343. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4344. return info, err
  4345. }
  4346. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4347. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4348. return info, err
  4349. }
  4350. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4351. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4352. return stock, err
  4353. }
  4354. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4355. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and (x.consumable_type = 3 or x.consumable_type = 2 or x.consumable_type = 15)")
  4356. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4357. fmt.Println(table)
  4358. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4359. fmt.Println(tableone)
  4360. if id > 0 {
  4361. db = db.Where("x.warehouse_out_id = ?", id)
  4362. }
  4363. if goodid > 0 {
  4364. db = db.Where("x.good_id = ?", goodid)
  4365. }
  4366. 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
  4367. return stock, err
  4368. }
  4369. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4370. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4371. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4372. fmt.Println(table)
  4373. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4374. fmt.Println(tableone)
  4375. if id > 0 {
  4376. db = db.Where("x.warehouse_out_id = ?", id)
  4377. }
  4378. if goodid > 0 {
  4379. db = db.Where("x.good_id = ?", goodid)
  4380. }
  4381. 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
  4382. return stock, err
  4383. }
  4384. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4385. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4386. return info, err
  4387. }
  4388. func ExportDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseInfo, err error) {
  4389. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1")
  4390. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4391. tableOne := XTReadDB().Table("xt_drug_warehouse as p")
  4392. fmt.Println("table", table)
  4393. fmt.Println("tableOne", tableOne)
  4394. if len(warehouse_out_id) > 0 {
  4395. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4396. }
  4397. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.price,x.dealer,x.manufacturer,x.warehousing_order,x.batch_number,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type,p.warehousing_time").Joins("left join xt_drug_warehouse as p on p.id =x.warehousing_id").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  4398. return out, err
  4399. }
  4400. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4401. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4402. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4403. fmt.Println("table", table)
  4404. if len(warehouse_out_id) > 0 {
  4405. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4406. }
  4407. 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
  4408. return out, err
  4409. }
  4410. func GetExportOutOrderDrugListOne(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4411. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1")
  4412. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4413. fmt.Println("table", table)
  4414. if len(warehouse_out_id) > 0 {
  4415. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4416. }
  4417. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.count,x.max_unit as count_unit,x.price,x.retail_price,x.product_date,x.expire_date,x.warehouse_out_order_number,x.dealer,x.manufacturer,x.is_sys,x.number,x.batch_number,x.patient_id,x.ctime,x.consumable_type,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type,b.min_price").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  4418. return out, err
  4419. }
  4420. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4421. db := XTReadDB().Model(&info).Where("status = 1")
  4422. if len(warehouse_out_id) > 0 {
  4423. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4424. }
  4425. if orgid > 0 {
  4426. db = db.Where("org_id = ?", orgid)
  4427. }
  4428. err = db.Find(&info).Error
  4429. return info, err
  4430. }
  4431. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4432. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4433. return out, err
  4434. }
  4435. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4436. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4437. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Preload("DrugWarehouseInfo", "status = 1 and org_id = ?", orgid)
  4438. }).Find(&out).Error
  4439. return out, err
  4440. }
  4441. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4442. info := models.WarehouseOutInfo{}
  4443. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4444. return info, err
  4445. }
  4446. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4447. info := models.WarehousingInfo{}
  4448. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4449. if goodid > 0 {
  4450. db = db.Where("x.good_id = ?", goodid)
  4451. }
  4452. if storehouse_id > 0 {
  4453. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4454. }
  4455. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4456. return info, err
  4457. }
  4458. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4459. info := models.WarehousingInfo{}
  4460. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4461. return info, err
  4462. }
  4463. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4464. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4465. return err
  4466. }
  4467. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4468. if storehouse_id == 0 {
  4469. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4470. }
  4471. if storehouse_id > 0 {
  4472. 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
  4473. }
  4474. return info, err
  4475. }
  4476. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4477. if len(warehousingInfo) > 0 {
  4478. utx := writeDb.Begin()
  4479. if len(warehousingInfo) > 0 {
  4480. 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 "
  4481. insertParams := make([]string, 0)
  4482. insertData := make([]interface{}, 0)
  4483. for _, info := range warehousingInfo {
  4484. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4485. insertData = append(insertData, info.WarehousingId)
  4486. insertData = append(insertData, info.GoodId)
  4487. insertData = append(insertData, info.Number)
  4488. insertData = append(insertData, info.LicenseNumber)
  4489. insertData = append(insertData, info.Count)
  4490. insertData = append(insertData, info.UserOrgId)
  4491. insertData = append(insertData, info.PatientId)
  4492. insertData = append(insertData, info.SystemTime)
  4493. insertData = append(insertData, info.ConsumableType)
  4494. insertData = append(insertData, info.IsSys)
  4495. insertData = append(insertData, info.WarehousingOrder)
  4496. insertData = append(insertData, info.WarehouseOutId)
  4497. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4498. insertData = append(insertData, info.IsEdit)
  4499. insertData = append(insertData, info.CancelStockId)
  4500. insertData = append(insertData, info.CancelOrderNumber)
  4501. insertData = append(insertData, info.Manufacturer)
  4502. insertData = append(insertData, info.Dealer)
  4503. insertData = append(insertData, info.Creator)
  4504. insertData = append(insertData, info.UpdateCreator)
  4505. insertData = append(insertData, info.Status)
  4506. insertData = append(insertData, info.Ctime)
  4507. insertData = append(insertData, info.Mtime)
  4508. insertData = append(insertData, info.Price)
  4509. insertData = append(insertData, info.WarehousingDetailId)
  4510. insertData = append(insertData, info.WarehouseOutDetailId)
  4511. insertData = append(insertData, info.CancelOutDetailId)
  4512. insertData = append(insertData, info.ExpireDate)
  4513. insertData = append(insertData, info.ProductDate)
  4514. }
  4515. thisSQL += strings.Join(insertParams, ", ")
  4516. err = utx.Exec(thisSQL, insertData...).Error
  4517. if err != nil {
  4518. utx.Rollback()
  4519. return
  4520. }
  4521. }
  4522. utx.Commit()
  4523. }
  4524. return
  4525. }
  4526. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4527. utx := XTWriteDB().Begin()
  4528. err := utx.Create(&flow).Error
  4529. if err != nil {
  4530. utx.Rollback()
  4531. return err
  4532. }
  4533. utx.Commit()
  4534. return err
  4535. }
  4536. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4537. err := tx.Create(&flow).Error
  4538. if err != nil {
  4539. tx.Rollback()
  4540. return err
  4541. }
  4542. return err
  4543. }
  4544. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4545. err := XTWriteDB().Create(&flow).Error
  4546. return err
  4547. }
  4548. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4549. err := XTWriteDB().Save(&flow).Error
  4550. return err
  4551. }
  4552. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4553. err = XTWriteDB().Model(&flow).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", warehousing_id, patient_id, record_time, good_id).Updates(map[string]interface{}{"number": flow.Number, "license_number": flow.LicenseNumber, "count": flow.Count, "expire_date": flow.ExpireDate, "product_date": flow.ProductDate, "price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "over_count": flow.OverCount}).Error
  4554. return err
  4555. }
  4556. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  4557. err = XTWriteDB().Model(&flow).Where("warehouse_out_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", warehouse_out_id, patient_id, record_time, good_id).Updates(map[string]interface{}{"number": flow.Number, "license_number": flow.LicenseNumber, "count": flow.Count, "expire_date": flow.ExpireDate, "product_date": flow.ProductDate, "price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "over_count": flow.OverCount}).Error
  4558. return err
  4559. }
  4560. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  4561. stock := models.VmStockFlow{}
  4562. var err error
  4563. err = XTReadDB().Model(&stock).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1 and is_read = 0", warehousing_id, patient_id, record_time, good_id).Find(&stock).Error
  4564. if err == gorm.ErrRecordNotFound {
  4565. return nil, err
  4566. }
  4567. if err != nil {
  4568. return nil, err
  4569. }
  4570. return &stock, nil
  4571. }
  4572. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4573. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3", patient_id, record_time, good_id).Find(&flow).Error
  4574. return flow, nil
  4575. }
  4576. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4577. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4578. return flow, nil
  4579. }
  4580. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4581. err = tx.Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4582. if err != gorm.ErrRecordNotFound {
  4583. if err != nil {
  4584. tx.Rollback()
  4585. return flow, nil
  4586. }
  4587. }
  4588. return flow, nil
  4589. }
  4590. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4591. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4592. return flow, nil
  4593. }
  4594. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  4595. err = tx.Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4596. if err != gorm.ErrRecordNotFound {
  4597. if err != nil {
  4598. tx.Rollback()
  4599. return flow, nil
  4600. }
  4601. }
  4602. return flow, nil
  4603. }
  4604. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  4605. err = XTReadDB().Where("patient_id = ? and record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4606. return flow, nil
  4607. }
  4608. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  4609. err = tx.Where("patient_id = ? and record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4610. if err != gorm.ErrRecordNotFound {
  4611. if err != nil {
  4612. tx.Rollback()
  4613. return flow, err
  4614. }
  4615. }
  4616. return flow, err
  4617. }
  4618. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4619. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  4620. return flow, nil
  4621. }
  4622. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4623. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7", patient_id, record_time, good_id).Find(&flow).Error
  4624. return flow, nil
  4625. }
  4626. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4627. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4628. return flow, nil
  4629. }
  4630. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4631. err = tx.Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4632. if err != gorm.ErrRecordNotFound {
  4633. if err != nil {
  4634. tx.Rollback()
  4635. return flow, nil
  4636. }
  4637. }
  4638. return flow, nil
  4639. }
  4640. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4641. err = XTReadDB().Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  4642. return flow, nil
  4643. }
  4644. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  4645. info := models.WarehouseOutInfo{}
  4646. var err error
  4647. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4648. if err == gorm.ErrRecordNotFound {
  4649. return nil, err
  4650. }
  4651. if err != nil {
  4652. return nil, err
  4653. }
  4654. return &info, nil
  4655. }
  4656. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  4657. info := models.WarehouseOutInfo{}
  4658. var err error
  4659. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and is_sys=1", good_id, record_time, patient_id).Find(&info).Error
  4660. if err == gorm.ErrRecordNotFound {
  4661. return nil, err
  4662. }
  4663. if err != nil {
  4664. return nil, err
  4665. }
  4666. return &info, nil
  4667. }
  4668. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  4669. info := models.WarehouseOutInfo{}
  4670. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and is_sys=1", good_id, record_time, patient_id).Find(&info).Error
  4671. return info, err
  4672. }
  4673. 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) {
  4674. offset := (page - 1) * limit
  4675. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  4676. if consumable_type > 0 {
  4677. //入库
  4678. if consumable_type == 1 {
  4679. db = db.Where("consumable_type = ?", consumable_type)
  4680. }
  4681. //2 手动出库
  4682. if consumable_type == 2 {
  4683. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  4684. }
  4685. //3.自动出库
  4686. if consumable_type == 3 {
  4687. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  4688. }
  4689. //4.手动退库
  4690. if consumable_type == 4 {
  4691. db = db.Where(" consumable_type = ?", consumable_type)
  4692. }
  4693. //自动退库
  4694. if consumable_type == 7 {
  4695. db = db.Where(" consumable_type = ?", consumable_type)
  4696. }
  4697. if consumable_type == 10 {
  4698. db = db.Where(" consumable_type = ?", consumable_type)
  4699. }
  4700. if consumable_type == 11 {
  4701. db = db.Where(" consumable_type = ?", consumable_type)
  4702. }
  4703. if consumable_type == 12 {
  4704. db = db.Where(" consumable_type = ?", consumable_type)
  4705. }
  4706. if consumable_type == 13 {
  4707. db = db.Where(" consumable_type = ?", consumable_type)
  4708. }
  4709. if consumable_type == 15 {
  4710. db = db.Where(" consumable_type = ?", consumable_type)
  4711. }
  4712. }
  4713. if orgId > 0 {
  4714. db = db.Where("user_org_id = ?", orgId)
  4715. }
  4716. if startTime > 0 {
  4717. db = db.Where("ctime >= ?", startTime)
  4718. }
  4719. if endTime > 0 {
  4720. db = db.Where("ctime<=?", endTime)
  4721. }
  4722. if good_id > 0 {
  4723. db = db.Where("good_id = ?", good_id)
  4724. }
  4725. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  4726. return flow, total, err
  4727. }
  4728. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  4729. detail := models.AutomaticReduceDetail{}
  4730. 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
  4731. return detail, err
  4732. }
  4733. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  4734. detail := models.WarehouseOutInfo{}
  4735. 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
  4736. return detail, err
  4737. }
  4738. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  4739. outInfo := models.WarehouseOutInfo{}
  4740. err := XTWriteDB().Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", good_id, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "over_count": info.OverCount}).Error
  4741. return err
  4742. }
  4743. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  4744. outInfo := models.WarehouseOutInfo{}
  4745. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", good_id, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "over_count": info.OverCount}).Error
  4746. if err != nil {
  4747. tx.Rollback()
  4748. return err
  4749. }
  4750. return err
  4751. }
  4752. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  4753. outInfo := models.WarehouseOutInfo{}
  4754. err := XTWriteDB().Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1 and project_id = ? and warehouse_info_id =? ", good_id, patient_id, record_time, project_id, warehouse_info_id).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "over_count": info.OverCount}).Error
  4755. return err
  4756. }
  4757. func UpdatedNewWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) error {
  4758. outInfo := models.WarehouseOutInfo{}
  4759. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1 and project_id = ? and warehouse_info_id =? ", good_id, patient_id, record_time, project_id, warehouse_info_id).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "over_count": info.OverCount}).Error
  4760. if err != nil {
  4761. tx.Rollback()
  4762. return err
  4763. }
  4764. return err
  4765. }
  4766. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  4767. info := models.WarehouseOutInfo{}
  4768. 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
  4769. return info, err
  4770. }
  4771. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  4772. info := models.WarehousingInfo{}
  4773. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4774. return info, err
  4775. }
  4776. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  4777. info := models.WarehousingInfo{}
  4778. db := XTReadDB().Model(&info)
  4779. if good_id > 0 {
  4780. db = db.Where("good_id = ? and status = 1", good_id)
  4781. }
  4782. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  4783. return info, err
  4784. }
  4785. 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) {
  4786. offset := (page - 1) * limit
  4787. db := XTReadDB().Model(drug_flow).Where("status = 1")
  4788. if drugid > 0 {
  4789. db = db.Where("drug_id = ?", drugid)
  4790. }
  4791. if orgid > 0 {
  4792. db = db.Where("user_org_id = ?", orgid)
  4793. }
  4794. if startTime > 0 {
  4795. db = db.Where("ctime >=? ", startTime)
  4796. }
  4797. if endTime > 0 {
  4798. db = db.Where("ctime<=?", endTime)
  4799. }
  4800. if stock_type > 0 {
  4801. //手动入库
  4802. if stock_type == 1 {
  4803. db = db.Where("consumable_type = 1")
  4804. }
  4805. //手动出库
  4806. if stock_type == 2 {
  4807. db = db.Where("consumable_type = 2 and is_sys = 0")
  4808. }
  4809. //自动出库
  4810. if stock_type == 3 {
  4811. db = db.Where("consumable_type = 3 and is_sys = 1")
  4812. }
  4813. if stock_type == 4 {
  4814. db = db.Where("consumable_type = 4")
  4815. }
  4816. if stock_type == 10 {
  4817. db = db.Where("consumable_type = 10")
  4818. }
  4819. if stock_type == 11 {
  4820. db = db.Where("consumable_type = 11")
  4821. }
  4822. if stock_type == 12 {
  4823. db = db.Where("consumable_type = 12")
  4824. }
  4825. if stock_type == 13 {
  4826. db = db.Where("consumable_type = 13")
  4827. }
  4828. if stock_type == 15 {
  4829. db = db.Where("consumable_type = 15")
  4830. }
  4831. }
  4832. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Preload("BaseDrugLib", "status = 1").Preload("DrugWarehouseInfo", "status = 1").Preload("XtDrugWarehouseOutInfo", "status = 1").Find(&drug_flow).Error
  4833. return drug_flow, total, err
  4834. }
  4835. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  4836. outInfo := models.DrugWarehouseOutInfo{}
  4837. var err error
  4838. 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
  4839. if err == gorm.ErrRecordNotFound {
  4840. return nil, err
  4841. }
  4842. if err != nil {
  4843. return nil, err
  4844. }
  4845. return &outInfo, nil
  4846. }
  4847. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  4848. 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
  4849. return outInfo, err
  4850. }
  4851. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  4852. outInfo := models.DrugWarehouseOutInfo{}
  4853. 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
  4854. return err
  4855. }
  4856. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  4857. db := XTReadDB().Model(&cancle).Where("status = 1")
  4858. if startime > 0 {
  4859. db = db.Where("ctime >=?", startime)
  4860. }
  4861. if endtime > 0 {
  4862. db = db.Where("ctime <=?", endtime)
  4863. }
  4864. if len(ids) > 0 {
  4865. db = db.Where("cancel_stock_id in(?)", ids)
  4866. }
  4867. if orgid > 0 {
  4868. db = db.Where("org_id = ?", orgid)
  4869. }
  4870. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  4871. err = db.Find(&cancle).Error
  4872. return cancle, err
  4873. }
  4874. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  4875. db := XTReadDB().Model(&info)
  4876. if id > 0 {
  4877. db = db.Where("cancel_stock_id = ? and status = 1", id)
  4878. }
  4879. db = db.Preload("BaseDrugLib")
  4880. err = db.Find(&info).Error
  4881. return info, err
  4882. }
  4883. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  4884. info := models.XtDrugWarehouseInfo{}
  4885. db := XTReadDB().Model(&info).Where("status = 1")
  4886. if id > 0 {
  4887. db = db.Where("id = ?", id)
  4888. }
  4889. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4890. return info, err
  4891. }
  4892. func GetDrugWarehouseInfoOne(id int64) (models.XtDrugWarehouseInfo, error) {
  4893. info := models.XtDrugWarehouseInfo{}
  4894. err := XTReadDB().Where("status = 1 and id=?", id).Find(&info).Error
  4895. return info, err
  4896. }
  4897. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  4898. // info := models.XtDrugWarehouseInfo{}
  4899. // db := tx.Model(&info).Where("status = 1")
  4900. // if id > 0 {
  4901. // db = db.Where("id = ?", id)
  4902. // }
  4903. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  4904. // return info, err
  4905. //}
  4906. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4907. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  4908. return info, err
  4909. }
  4910. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  4911. err = XTReadDB().Select("id,stock_max_number,stock_min_number").Where("drug_id = ? and org_id =? and status = 1 and storehouse_id = ? and is_check = 1", drugid, orgid, storehouse_id).Find(&info).Error
  4912. return info, err
  4913. }
  4914. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  4915. info := models.HisDoctorAdviceInfo{}
  4916. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  4917. return info, err
  4918. }
  4919. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  4920. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4921. return info, err
  4922. }
  4923. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  4924. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  4925. if err != gorm.ErrRecordNotFound {
  4926. if err != nil {
  4927. tx.Rollback()
  4928. return info, err
  4929. }
  4930. }
  4931. return info, err
  4932. }
  4933. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  4934. adviceInfo := models.HisDoctorAdviceInfo{}
  4935. err := tx.Where("patient_id = ? and advice_date= ? and user_org_id = ? and status=1", patient_id, advice_date, user_org_id).Last(&adviceInfo).Error
  4936. if err != gorm.ErrRecordNotFound {
  4937. if err != nil {
  4938. tx.Rollback()
  4939. return adviceInfo, err
  4940. }
  4941. }
  4942. return adviceInfo, err
  4943. }
  4944. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  4945. info := models.HisDoctorAdviceInfo{}
  4946. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1 and id =?", drugid, patient_id, advice_date, advice_id).First(&info).Error
  4947. return info, err
  4948. }
  4949. func GetHisAdviceListByDrugIdNight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  4950. info := models.HisDoctorAdviceInfo{}
  4951. 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
  4952. return info, err
  4953. }
  4954. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  4955. detail := models.DrugAutomaticReduceDetail{}
  4956. 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
  4957. return detail, err
  4958. }
  4959. func GetWarehouseOutListExport(drug_id int64, patient_id int64, record_date int64, advice_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  4960. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and advice_id =? and status =1", drug_id, patient_id, record_date, advice_id).Find(&out).Error
  4961. return out, err
  4962. }
  4963. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  4964. err = XTReadDB().Where("advice_id = ? and drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and is_check = 1", advice_id, drugid, patient_id, advice_date).Find(&out).Error
  4965. return out, err
  4966. }
  4967. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  4968. err = tx.Where("advice_id = ? and drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and is_check = 1", advice_id, drugid, patient_id, advice_date).Find(&out).Error
  4969. if err != gorm.ErrRecordNotFound {
  4970. if err != nil {
  4971. tx.Rollback()
  4972. return out, err
  4973. }
  4974. }
  4975. return out, err
  4976. }
  4977. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  4978. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  4979. return err
  4980. }
  4981. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  4982. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  4983. return err
  4984. }
  4985. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  4986. 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
  4987. 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
  4988. 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
  4989. return err
  4990. }
  4991. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  4992. 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
  4993. 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
  4994. return err
  4995. }
  4996. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  4997. 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
  4998. 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
  4999. return err
  5000. }
  5001. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  5002. info := models.XtDrugWarehouseInfo{}
  5003. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  5004. return info, err
  5005. }
  5006. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  5007. info := models.XtDrugWarehouseInfo{}
  5008. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5009. return info, err
  5010. }
  5011. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  5012. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5013. return err
  5014. }
  5015. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5016. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5017. // return err
  5018. //}
  5019. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  5020. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  5021. //return err
  5022. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5023. return err
  5024. }
  5025. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5026. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5027. // return err
  5028. //}
  5029. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  5030. info := models.DrugCancelStockInfo{}
  5031. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5032. return info, err
  5033. }
  5034. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  5035. info := models.DrugCancelStockInfo{}
  5036. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  5037. return info, err
  5038. }
  5039. func UpdatedDrugInfo(id int64, count int64) (err error) {
  5040. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5041. return err
  5042. }
  5043. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  5044. 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
  5045. return err
  5046. }
  5047. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  5048. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5049. return err
  5050. }
  5051. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5052. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1 and id = ?", drugid, orgid, warehouse_info_id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5053. return err
  5054. }
  5055. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  5056. 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
  5057. return err
  5058. }
  5059. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5060. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? and org_id = ? and status = 1 and id = ?", drugid, orgid, warehouse_info_id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5061. return err
  5062. }
  5063. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  5064. 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
  5065. return err
  5066. }
  5067. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  5068. info := models.WarehousingInfo{}
  5069. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5070. return info, err
  5071. }
  5072. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5073. info := models.WarehousingInfo{}
  5074. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  5075. return info, err
  5076. }
  5077. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5078. info := models.WarehousingInfo{}
  5079. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  5080. return info, err
  5081. }
  5082. func CreateWarehouseOut(out models.WarehouseOut) error {
  5083. err := XTWriteDB().Create(&out).Error
  5084. return err
  5085. }
  5086. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  5087. out := models.WarehouseOut{}
  5088. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  5089. return out, err
  5090. }
  5091. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  5092. err := XTWriteDB().Create(&info).Error
  5093. return err
  5094. }
  5095. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  5096. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5097. return err
  5098. }
  5099. func UpdateWareInfoById(id int64, count int64) error {
  5100. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5101. return err
  5102. }
  5103. func UpdateWareInfoByIdSix(id int64, count int64) error {
  5104. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  5105. return err
  5106. }
  5107. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  5108. info := models.CancelStockInfo{}
  5109. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5110. return info, err
  5111. }
  5112. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  5113. info := models.CancelStockInfo{}
  5114. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5115. return info, err
  5116. }
  5117. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5118. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5119. return info, err
  5120. }
  5121. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5122. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5123. return info, err
  5124. }
  5125. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5126. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5127. return err
  5128. }
  5129. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5130. drugFlow := models.DrugFlow{}
  5131. 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
  5132. return err
  5133. }
  5134. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5135. drugFlow := models.DrugFlow{}
  5136. 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
  5137. return err
  5138. }
  5139. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5140. stock := models.DrugCancelStock{}
  5141. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5142. return stock, err
  5143. }
  5144. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5145. // stock := models.DrugCancelStock{}
  5146. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5147. // return stock, err
  5148. //}
  5149. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5150. db := XTReadDB().Model(&info)
  5151. if warehouse_info_id > 0 {
  5152. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5153. }
  5154. if drug_id > 0 {
  5155. db = db.Where("drug_id = ? and status =1", drug_id)
  5156. }
  5157. err = db.Find(&info).Error
  5158. return info, err
  5159. }
  5160. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5161. // db := tx.Model(&info)
  5162. // if warehouse_info_id > 0 {
  5163. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5164. // }
  5165. // if drug_id > 0 {
  5166. // db = db.Where("drug_id = ? and status =1", drug_id)
  5167. // }
  5168. // err = db.Find(&info).Error
  5169. // return info, err
  5170. //}
  5171. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5172. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5173. return info, err
  5174. }
  5175. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5176. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5177. // return info, err
  5178. //}
  5179. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5180. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5181. return config, err
  5182. }
  5183. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5184. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5185. return config, err
  5186. }
  5187. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5188. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5189. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5190. fmt.Println(table)
  5191. likeKey := "%" + keyword + "%"
  5192. if orgid > 0 {
  5193. db = db.Where("x.org_id = ?", orgid)
  5194. }
  5195. if storehouse_id > 0 {
  5196. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5197. }
  5198. 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
  5199. return info, err
  5200. }
  5201. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5202. err := XTWriteDB().Create(&adjustPrice).Error
  5203. return err
  5204. }
  5205. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5206. likeKey := "%" + keyword + "%"
  5207. offset := (page - 1) * limit
  5208. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5209. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5210. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5211. fmt.Println(table, tab)
  5212. if startime > 0 {
  5213. db = db.Where("x.start_time>=?", startime)
  5214. }
  5215. if endtime > 0 {
  5216. db = db.Where("x.start_time<=?", endtime)
  5217. }
  5218. if len(keyword) > 0 {
  5219. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5220. }
  5221. if orgid > 0 {
  5222. db = db.Where("x.user_org_id = ?", orgid)
  5223. }
  5224. 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
  5225. return list, total, err
  5226. }
  5227. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5228. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5229. return err
  5230. }
  5231. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5232. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5233. return err
  5234. }
  5235. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5236. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5237. return info, err
  5238. }
  5239. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5240. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5241. return adjust, err
  5242. }
  5243. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5244. err := XTWriteDB().Create(&price).Error
  5245. return err
  5246. }
  5247. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5248. likeKey := "%" + keyword + "%"
  5249. offset := (page - 1) * limit
  5250. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5251. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5252. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5253. fmt.Println(table, tab)
  5254. if startime > 0 {
  5255. db = db.Where("x.ctime>=?", startime)
  5256. }
  5257. if endtime > 0 {
  5258. db = db.Where("x.ctime<=?", endtime)
  5259. }
  5260. if len(keyword) > 0 {
  5261. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5262. }
  5263. if orgid > 0 {
  5264. db = db.Where("x.user_org_id = ?", orgid)
  5265. }
  5266. 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
  5267. return adjust, total, err
  5268. }
  5269. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5270. likeKey := "%" + keyword + "%"
  5271. offset := (page - 1) * limit
  5272. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5273. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5274. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5275. fmt.Println(table, tab)
  5276. if len(keyword) > 0 {
  5277. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5278. }
  5279. if orgid > 0 {
  5280. db = db.Where("x.user_org_id = ?", orgid)
  5281. }
  5282. if startime > 0 {
  5283. db = db.Where("x.ctime >=?", startime)
  5284. }
  5285. if endtime > 0 {
  5286. db = db.Where("x.ctime <=?", endtime)
  5287. }
  5288. if storehouse_id > 0 {
  5289. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5290. }
  5291. err = db.Group("x.good_id").Select("x.ctime,x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,x.storehouse_id,t.packing_unit,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  5292. return adjust, total, err
  5293. }
  5294. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5295. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5296. if inventory_type == 0 {
  5297. db = db.Where("x.inventory_type = 4")
  5298. }
  5299. if inventory_type > 0 {
  5300. db = db.Where("x.inventory_type = ?", inventory_type)
  5301. }
  5302. if len(keyword) > 0 {
  5303. likeKey := "%" + keyword + "%"
  5304. db = db.Where("x.warehousing_order like ?", likeKey)
  5305. }
  5306. if orgid > 0 {
  5307. db = db.Where("x.user_org_id = ?", orgid)
  5308. }
  5309. 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
  5310. return adjust, err
  5311. }
  5312. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5313. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5314. if orgid > 0 {
  5315. db = db.Where("x.user_org_id = ?", orgid)
  5316. }
  5317. 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
  5318. return adjust, err
  5319. }
  5320. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5321. 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
  5322. return err
  5323. }
  5324. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5325. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5326. return report, err
  5327. }
  5328. func CreateInentory(inventory models.XtStockInventory) error {
  5329. err = XTWriteDB().Create(&inventory).Error
  5330. return err
  5331. }
  5332. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5333. likeKey := "%" + keyword + "%"
  5334. offset := (page - 1) * limit
  5335. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5336. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5337. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5338. fmt.Println(table, tab)
  5339. if startime > 0 {
  5340. db = db.Where("x.ctime>=?", startime)
  5341. }
  5342. if endtime > 0 {
  5343. db = db.Where("x.ctime<=?", endtime)
  5344. }
  5345. if len(keyword) > 0 {
  5346. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5347. }
  5348. if orgid > 0 {
  5349. db = db.Where("x.user_org_id = ?", orgid)
  5350. }
  5351. if inventory_status > 0 {
  5352. db = db.Where("checker_status = ?", inventory_status)
  5353. }
  5354. 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
  5355. return adjust, total, err
  5356. }
  5357. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5358. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5359. return err
  5360. }
  5361. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5362. info := models.XtStockAdjustPrice{}
  5363. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5364. return info, err
  5365. }
  5366. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5367. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5368. return list, err
  5369. }
  5370. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5371. info := models.WarehousingInfo{}
  5372. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5373. return info, err
  5374. }
  5375. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5376. info := models.WarehousingInfo{}
  5377. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5378. //return info, err
  5379. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5380. if id > 0 {
  5381. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5382. }
  5383. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5384. return info, err
  5385. }
  5386. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5387. info := models.WarehousingInfo{}
  5388. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5389. if id > 0 {
  5390. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5391. }
  5392. if storehouse_id > 0 {
  5393. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5394. }
  5395. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5396. return info, err
  5397. }
  5398. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5399. 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
  5400. return err
  5401. }
  5402. func DeleteStockPrice(id int64) error {
  5403. info := models.XtStockAdjustPrice{}
  5404. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5405. return err
  5406. }
  5407. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5408. report := models.XtStockReportPrice{}
  5409. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5410. return report, err
  5411. }
  5412. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5413. 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
  5414. return err
  5415. }
  5416. func DeleteDamage(id int64) error {
  5417. adjust := models.XtStockReportPrice{}
  5418. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5419. return err
  5420. }
  5421. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5422. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5423. return adjust, err
  5424. }
  5425. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5426. inventory := models.XtStockInventory{}
  5427. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5428. return inventory, err
  5429. }
  5430. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5431. 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
  5432. return err
  5433. }
  5434. func DeleteInventory(id int64) error {
  5435. inventory := models.XtStockInventory{}
  5436. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5437. return err
  5438. }
  5439. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5440. likeKey := "%" + keyword + "%"
  5441. offset := (page - 1) * limit
  5442. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5443. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5444. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5445. fmt.Println(table, tab)
  5446. if len(keyword) > 0 {
  5447. db = db.Where("x.good_name like ?", likeKey)
  5448. }
  5449. if orgid > 0 {
  5450. db = db.Where("x.user_org_id = ?", orgid)
  5451. }
  5452. if storehouse_id > 0 {
  5453. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5454. }
  5455. err = db.Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,x.expire_date,x.product_date,t.packing_unit,x.last_stock_count,x.type,x.inventory_type,x.stock_count,x.storehouse_id,x.ctime").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Order("x.id desc").Scan(&list).Error
  5456. return list, total, err
  5457. }
  5458. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5459. likeKey := "%" + keyword + "%"
  5460. offset := (page - 1) * limit
  5461. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5462. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5463. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5464. fmt.Println(table, tab)
  5465. if len(keyword) > 0 {
  5466. db = db.Where("x.good_name like ?", likeKey)
  5467. }
  5468. if orgid > 0 {
  5469. db = db.Where("x.user_org_id = ?", orgid)
  5470. }
  5471. if storehouse_id > 0 {
  5472. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5473. }
  5474. if len(ids) > 0 {
  5475. db = db.Where("x.id in(?)", ids)
  5476. }
  5477. 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
  5478. return list, total, err
  5479. }
  5480. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5481. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5482. return list, err
  5483. }
  5484. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5485. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5486. return list, err
  5487. }
  5488. func DeleteDrugPrice(id int64) error {
  5489. info := models.XtDrugAdjustPrice{}
  5490. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5491. return err
  5492. }
  5493. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5494. info := models.WarehousingInfo{}
  5495. db := XTReadDB().Model(&info).Where("status = 1")
  5496. if goodid > 0 {
  5497. db = db.Where("good_id = ?", goodid)
  5498. }
  5499. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5500. return info, err
  5501. }
  5502. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5503. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5504. return err
  5505. }
  5506. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5507. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5508. return err
  5509. }
  5510. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5511. err := XTWriteDB().Create(&record).Error
  5512. return err
  5513. }
  5514. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5515. info := models.WarehousingInfo{}
  5516. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5517. //return info, err
  5518. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5519. if id > 0 {
  5520. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5521. }
  5522. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5523. return info, err
  5524. }
  5525. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5526. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5527. return info, err
  5528. }
  5529. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  5530. info := models.DrugWarehouseInfo{}
  5531. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5532. return info, err
  5533. }
  5534. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  5535. 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
  5536. return err
  5537. }
  5538. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  5539. 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
  5540. return err
  5541. }
  5542. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  5543. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=? and is_check= 1 and stock_count>0", id, storehouse_id).Preload("GoodInfo", "status = 1").Find(&info).Error
  5544. return info, err
  5545. }
  5546. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  5547. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5548. return err
  5549. }
  5550. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  5551. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  5552. return err
  5553. }
  5554. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  5555. db := XTReadDB().Model(&list).Where(" status = 1")
  5556. if good_id > 0 {
  5557. db = db.Where("good_id = ?", good_id)
  5558. }
  5559. if len(keyword) > 0 {
  5560. likeKey := "%" + keyword + "%"
  5561. db = db.Where("warehousing_order like ?", likeKey)
  5562. }
  5563. if inventory_type > 0 {
  5564. db = db.Where("inventory_type = ?", inventory_type)
  5565. }
  5566. if inventory_type == 0 {
  5567. db = db.Where("inventory_type = 4")
  5568. }
  5569. err = db.Find(&list).Error
  5570. return list, err
  5571. }
  5572. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  5573. flow := models.DrugFlow{}
  5574. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  5575. if err == gorm.ErrRecordNotFound {
  5576. return nil, err
  5577. }
  5578. if err != nil {
  5579. return nil, err
  5580. }
  5581. return &flow, nil
  5582. }
  5583. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  5584. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  5585. return info, err
  5586. }
  5587. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  5588. info := models.XtDrugWarehouseInfo{}
  5589. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  5590. return info, err
  5591. }
  5592. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5593. info := models.WarehousingInfo{}
  5594. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  5595. return info, err
  5596. }
  5597. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  5598. flow := models.VmStockFlow{}
  5599. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  5600. if err == gorm.ErrRecordNotFound {
  5601. return nil, err
  5602. }
  5603. if err != nil {
  5604. return nil, err
  5605. }
  5606. return &flow, nil
  5607. }
  5608. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  5609. info := models.GoodInfo{}
  5610. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  5611. return info, err
  5612. }
  5613. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  5614. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  5615. return err
  5616. }
  5617. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  5618. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  5619. return err
  5620. }
  5621. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  5622. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount, "price": info.Price, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "license_number": info.LicenseNumber, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "remark": info.Remark, "packing_price": info.PackingPrice, "storehouse_id": info.StorehouseId, "register_number": info.RegisterNumber, "good_id": info.GoodId}).Error
  5623. return err
  5624. }
  5625. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5626. 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
  5627. return err
  5628. }
  5629. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  5630. err := XTWriteDB().Model(&flow).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, good_id).Updates(map[string]interface{}{"count": flow.Count, "number": flow.Number, "product_date": flow.ProductDate, "expire_date": flow.ExpireDate, "Price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "license_number": flow.LicenseNumber, "storehouse_id": flow.StorehouseId, "register_number": flow.RegisterNumber}).Error
  5631. return err
  5632. }
  5633. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  5634. err := XTWriteDB().Model(&flow).Where("warehouse_out_detail_id = ? and status = 1", wareoutdetailid).Updates(map[string]interface{}{"count": flow.Count, "number": flow.Number, "product_date": flow.ProductDate, "expire_date": flow.ExpireDate, "Price": flow.Price, "manufacturer": flow.Manufacturer, "dealer": flow.Dealer, "license_number": flow.LicenseNumber, "price": flow.Price, "storehouse_id": flow.StorehouseId, "admin_user_id": flow.AdminUserId, "stock_count": flow.StockCount, "register_number": flow.RegisterNumber}).Error
  5635. return err
  5636. }
  5637. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  5638. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  5639. return err
  5640. }
  5641. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  5642. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.stock_count>0")
  5643. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5644. fmt.Println(table)
  5645. if orgid > 0 {
  5646. db = db.Where("x.org_id = ?", orgid)
  5647. }
  5648. 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
  5649. return info, err
  5650. }
  5651. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  5652. warehouseInfo := models.XtDrugWarehouseInfo{}
  5653. 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
  5654. return err
  5655. }
  5656. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  5657. err := XTWriteDB().Create(&info).Error
  5658. return err
  5659. }
  5660. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  5661. err := tx.Create(&info).Error
  5662. if err != nil {
  5663. tx.Rollback()
  5664. return err
  5665. }
  5666. return err
  5667. }
  5668. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  5669. flow := models.VmStockFlow{}
  5670. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  5671. if err == gorm.ErrRecordNotFound {
  5672. return nil, err
  5673. }
  5674. if err != nil {
  5675. return nil, err
  5676. }
  5677. return &flow, nil
  5678. }
  5679. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  5680. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5681. return err
  5682. }
  5683. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  5684. 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
  5685. return err
  5686. }
  5687. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  5688. flow := models.DrugWarehouseOutInfo{}
  5689. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  5690. return flow, err
  5691. }
  5692. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  5693. info := models.WarehouseOutInfo{}
  5694. 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
  5695. return info, err
  5696. }
  5697. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  5698. 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
  5699. return err
  5700. }
  5701. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  5702. 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
  5703. return err
  5704. }
  5705. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  5706. info := models.DrugWarehouseOutInfo{}
  5707. 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
  5708. if err == gorm.ErrRecordNotFound {
  5709. return nil, err
  5710. }
  5711. if err != nil {
  5712. return nil, err
  5713. }
  5714. return &info, nil
  5715. }
  5716. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  5717. drugFlow := models.DrugFlow{}
  5718. 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
  5719. if err == gorm.ErrRecordNotFound {
  5720. return nil, err
  5721. }
  5722. if err != nil {
  5723. return nil, err
  5724. }
  5725. return &drugFlow, nil
  5726. }
  5727. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  5728. 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
  5729. return err
  5730. }
  5731. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  5732. 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
  5733. return err
  5734. }
  5735. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  5736. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5737. return detail, err
  5738. }
  5739. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  5740. detail := models.DrugAutomaticReduceDetail{}
  5741. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  5742. if err == gorm.ErrRecordNotFound {
  5743. return nil, err
  5744. }
  5745. if err != nil {
  5746. return nil, err
  5747. }
  5748. return &detail, nil
  5749. }
  5750. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  5751. 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
  5752. return err
  5753. }
  5754. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  5755. 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
  5756. return err
  5757. }
  5758. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  5759. info := models.DrugWarehouseInfo{}
  5760. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5761. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5762. return info, err
  5763. }
  5764. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  5765. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5766. return err
  5767. }
  5768. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  5769. info := models.DrugWarehouseInfo{}
  5770. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5771. return err
  5772. }
  5773. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  5774. info := models.DrugCancelStockInfo{}
  5775. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5776. return info, err
  5777. }
  5778. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  5779. info := models.DrugWarehouseInfo{}
  5780. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  5781. return err
  5782. }
  5783. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  5784. info := models.DrugWarehouseInfo{}
  5785. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  5786. return err
  5787. }
  5788. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  5789. 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
  5790. return advice, err
  5791. }
  5792. func GetAllHisDoctorAdviceByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (advice []*models.HisDoctorAdviceInfo, err error) {
  5793. err = tx.Where("drug_id = ? and patient_id = ? and advice_date = ? and execution_state = 1 and status = 1 and user_org_id = ?", drugid, patient_id, advicedate, orgid).Find(&advice).Error
  5794. if err != gorm.ErrRecordNotFound {
  5795. if err != nil {
  5796. tx.Rollback()
  5797. return advice, err
  5798. }
  5799. }
  5800. return advice, err
  5801. }
  5802. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5803. 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
  5804. return info, err
  5805. }
  5806. func GetAllDrugFlowByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5807. err = tx.Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", drugid, patient_id, advicedate, orgid).Find(&info).Error
  5808. if err != gorm.ErrRecordNotFound {
  5809. if err != nil {
  5810. tx.Rollback()
  5811. return info, err
  5812. }
  5813. }
  5814. return info, err
  5815. }
  5816. // 耗材加库存
  5817. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5818. info := models.GoodInfo{}
  5819. 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
  5820. return info, err
  5821. }
  5822. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5823. info := models.GoodInfo{}
  5824. 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
  5825. 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
  5826. return info, err
  5827. }
  5828. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5829. info := models.GoodInfo{}
  5830. 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
  5831. return info, err
  5832. }
  5833. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5834. info := models.GoodInfo{}
  5835. 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
  5836. 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
  5837. return info, err
  5838. }
  5839. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  5840. info := models.GoodInfo{}
  5841. 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
  5842. return info, err
  5843. }
  5844. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5845. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5846. return info, err
  5847. }
  5848. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  5849. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5850. return info, err
  5851. }
  5852. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  5853. info := models.WarehousingInfo{}
  5854. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  5855. return info, err
  5856. }
  5857. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  5858. info := models.WarehousingInfo{}
  5859. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  5860. return info, err
  5861. }
  5862. // 药品加库存
  5863. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5864. info := models.BaseDrugLib{}
  5865. 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
  5866. return info, err
  5867. }
  5868. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5869. info := models.BaseDrugLib{}
  5870. 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
  5871. 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
  5872. return info, err
  5873. }
  5874. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5875. info := models.BaseDrugLib{}
  5876. 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
  5877. return info, err
  5878. }
  5879. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5880. info := models.BaseDrugLib{}
  5881. 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
  5882. 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
  5883. return info, err
  5884. }
  5885. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  5886. info := models.BaseDrugLib{}
  5887. 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
  5888. 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
  5889. return info, err
  5890. }
  5891. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5892. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  5893. return info, err
  5894. }
  5895. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5896. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  5897. return info, err
  5898. }
  5899. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  5900. info := models.WarehouseOutInfo{}
  5901. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5902. return info, err
  5903. }
  5904. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  5905. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  5906. return out, err
  5907. }
  5908. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  5909. info := models.WarehousingInfo{}
  5910. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5911. return info, err
  5912. }
  5913. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  5914. err = XTReadDB().Where("status = 1").Find(&template).Error
  5915. return template, err
  5916. }
  5917. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  5918. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  5919. return good, err
  5920. }
  5921. func GetAllMobileProjectList(orgid int64) (project []*models.HisProject, err error) {
  5922. err = XTReadDB().Where("user_org_id =? and status =1", orgid).Find(&project).Error
  5923. return project, err
  5924. }
  5925. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  5926. info := models.GoodInfo{}
  5927. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5928. return info, err
  5929. }
  5930. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  5931. db := XTReadDB().Where("status = 1")
  5932. if good_id > 0 {
  5933. db = db.Where("good_id = ?", good_id)
  5934. }
  5935. err = db.Find(&info).Error
  5936. return info, err
  5937. }
  5938. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  5939. db := XTReadDB().Model(&info).Where("status = 1")
  5940. if drug_id > 0 {
  5941. db = db.Where("drug_id = ?", drug_id)
  5942. }
  5943. err = db.Find(&info).Error
  5944. return info, err
  5945. }
  5946. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  5947. lib := models.BaseDrugLib{}
  5948. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  5949. return lib, err
  5950. }
  5951. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  5952. info := models.Warehousing{}
  5953. 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
  5954. if err == gorm.ErrRecordNotFound {
  5955. return nil, err
  5956. }
  5957. if err != nil {
  5958. return nil, err
  5959. }
  5960. return &info, nil
  5961. }
  5962. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  5963. info := models.Warehousing{}
  5964. 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
  5965. return info, err
  5966. }
  5967. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  5968. out := models.DrugWarehouseOut{}
  5969. 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
  5970. if err == gorm.ErrRecordNotFound {
  5971. return nil, err
  5972. }
  5973. if err != nil {
  5974. return nil, err
  5975. }
  5976. return &out, nil
  5977. }
  5978. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  5979. info := models.DrugWarehouseOut{}
  5980. 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
  5981. return info, err
  5982. }
  5983. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  5984. info := models.DrugWarehouse{}
  5985. 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
  5986. if err == gorm.ErrRecordNotFound {
  5987. return nil, err
  5988. }
  5989. if err != nil {
  5990. return nil, err
  5991. }
  5992. return &info, nil
  5993. }
  5994. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  5995. err := XTWriteDB().Create(&warehouse).Error
  5996. return err
  5997. }
  5998. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  5999. info := models.DrugWarehouse{}
  6000. 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
  6001. return info, err
  6002. }
  6003. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6004. info := models.GoodInfo{}
  6005. 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
  6006. 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
  6007. return info, err
  6008. }
  6009. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6010. info := models.GoodInfo{}
  6011. 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
  6012. 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
  6013. return info, err
  6014. }
  6015. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6016. tx := XTWriteDB().Begin()
  6017. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count, "sum_in_count": sum_in_count}).Error
  6018. if err != nil {
  6019. tx.Rollback()
  6020. return err
  6021. }
  6022. tx.Commit()
  6023. return err
  6024. }
  6025. func UpdateMedicalSumCountOne(drug_id int64, sum_count int64, orgid int64) error {
  6026. tx := XTWriteDB().Begin()
  6027. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6028. if err != nil {
  6029. tx.Rollback()
  6030. return err
  6031. }
  6032. tx.Commit()
  6033. return err
  6034. }
  6035. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  6036. // err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count, "sum_in_count": sum_in_count}).Error
  6037. // return err
  6038. //}
  6039. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6040. 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
  6041. return err
  6042. }
  6043. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6044. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6045. table := XTReadDB().Table("xt_base_drug as x d")
  6046. fmt.Println(table)
  6047. if startime > 0 {
  6048. db = db.Where("x.advice_date >= ?", startime)
  6049. }
  6050. if endtime > 0 {
  6051. db = db.Where("x.advice_date <= ?", endtime)
  6052. }
  6053. if orgid > 0 {
  6054. db = db.Where("x.user_org_id = ?", orgid)
  6055. }
  6056. if len(keyword) > 0 {
  6057. likeKey := "%" + keyword + "%"
  6058. 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)
  6059. }
  6060. offset := (page - 1) * limit
  6061. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  6062. return adviceinfo, total, err
  6063. }
  6064. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6065. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6066. table := XTReadDB().Table("xt_base_drug as x d")
  6067. fmt.Println(table)
  6068. if startime > 0 {
  6069. db = db.Where("x.advice_date >= ?", startime)
  6070. }
  6071. if endtime > 0 {
  6072. db = db.Where("x.advice_date <= ?", endtime)
  6073. }
  6074. if orgid > 0 {
  6075. db = db.Where("x.user_org_id = ?", orgid)
  6076. }
  6077. if len(keyword) > 0 {
  6078. likeKey := "%" + keyword + "%"
  6079. 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)
  6080. }
  6081. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  6082. return adviceinfo, total, err
  6083. }
  6084. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  6085. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  6086. return drug, err
  6087. }
  6088. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6089. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6090. table := XTReadDB().Table("xt_good_information as d")
  6091. fmt.Println(table)
  6092. if startime > 0 {
  6093. db = db.Where("x.record_date >= ?", startime)
  6094. }
  6095. if endtime > 0 {
  6096. db = db.Where("x.record_date <= ?", endtime)
  6097. }
  6098. if orgid > 0 {
  6099. db = db.Where("x.user_org_id = ?", orgid)
  6100. }
  6101. if len(keyword) > 0 {
  6102. likeKey := "%" + keyword + "%"
  6103. 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")
  6104. }
  6105. offset := (page - 1) * limit
  6106. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  6107. return project, total, err
  6108. }
  6109. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6110. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6111. table := XTReadDB().Table("xt_good_information as d")
  6112. fmt.Println(table)
  6113. if startime > 0 {
  6114. db = db.Where("x.record_date >= ?", startime)
  6115. }
  6116. if endtime > 0 {
  6117. db = db.Where("x.record_date <= ?", endtime)
  6118. }
  6119. if orgid > 0 {
  6120. db = db.Where("x.user_org_id = ?", orgid)
  6121. }
  6122. if len(keyword) > 0 {
  6123. likeKey := "%" + keyword + "%"
  6124. 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")
  6125. }
  6126. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  6127. return project, total, err
  6128. }
  6129. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  6130. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  6131. return good, err
  6132. }
  6133. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  6134. setting := models.XtStockSetting{}
  6135. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6136. if err == gorm.ErrRecordNotFound {
  6137. return nil, err
  6138. }
  6139. if err != nil {
  6140. return nil, err
  6141. }
  6142. return &setting, nil
  6143. }
  6144. func CreateStockSetting(setting models.XtStockSetting) error {
  6145. err := XTWriteDB().Create(&setting).Error
  6146. return err
  6147. }
  6148. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6149. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6150. return err
  6151. }
  6152. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6153. setting := models.XtStockSetting{}
  6154. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6155. return setting, err
  6156. }
  6157. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6158. detail := models.BloodAutomaticReduceDetail{}
  6159. 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
  6160. return detail, err
  6161. }
  6162. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6163. detail := models.WarehouseOutInfoNight{}
  6164. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and org_id = ? and status = 1 and patient_id = ?", good_id, record_date, orgid, patient_id).Find(&detail).Error
  6165. return detail, err
  6166. }
  6167. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6168. setting := models.XtAdviceSetting{}
  6169. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6170. if err == gorm.ErrRecordNotFound {
  6171. return nil, err
  6172. }
  6173. if err != nil {
  6174. return nil, err
  6175. }
  6176. return &setting, nil
  6177. }
  6178. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6179. err := XTWriteDB().Create(&setting).Error
  6180. return err
  6181. }
  6182. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6183. 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
  6184. return err
  6185. }
  6186. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6187. setting := models.XtAdviceSetting{}
  6188. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6189. return setting, err
  6190. }
  6191. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6192. config := models.XtPrescriptionConfig{}
  6193. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6194. if err == gorm.ErrRecordNotFound {
  6195. return nil, err
  6196. }
  6197. if err != nil {
  6198. return nil, err
  6199. }
  6200. return &config, nil
  6201. }
  6202. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6203. err := XTWriteDB().Create(&prescriptionConfig).Error
  6204. return err
  6205. }
  6206. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6207. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": config.IsOpen, "mtime": time.Now().Unix()}).Error
  6208. return err
  6209. }
  6210. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6211. config := models.XtPrescriptionConfig{}
  6212. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6213. return config, err
  6214. }
  6215. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6216. var deliver_number int64 = 0
  6217. var stock_number int64 = 0
  6218. deliver_number = maxNumber
  6219. // 根据先进先出原则,查询最先入库的批次,进行出库
  6220. // 如果没有对应的库存,则报错
  6221. //开启事物
  6222. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6223. if err != nil {
  6224. return err
  6225. }
  6226. // 将该批次的剩余库存数量转换为拆零数量
  6227. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6228. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6229. if stock_number >= deliver_number {
  6230. var maxNumber int64 = 0
  6231. var minNumber int64 = 0
  6232. var stock_max_number int64 = 0
  6233. stock_max_number = warehouse.StockMaxNumber
  6234. maxNumber = deliver_number / drup.MinNumber
  6235. minNumber = deliver_number % drup.MinNumber
  6236. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6237. minNumber = maxNumber
  6238. }
  6239. if drup.MaxUnit != drup.MinUnit {
  6240. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6241. return errors.New("库存数量不足")
  6242. }
  6243. }
  6244. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6245. if warehouse.StockMaxNumber < 0 {
  6246. warehouse.StockMaxNumber = 0
  6247. }
  6248. if warehouse.StockMinNumber < 0 {
  6249. warehouse.StockMinNumber = 0
  6250. }
  6251. warehouse.Mtime = time.Now().Unix()
  6252. if warehouse.StockMinNumber < minNumber {
  6253. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6254. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6255. } else {
  6256. if minNumber > 0 {
  6257. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6258. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6259. } else {
  6260. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6261. }
  6262. }
  6263. if minNumber == 0 && maxNumber != 1 {
  6264. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6265. }
  6266. }
  6267. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6268. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6269. if warehouse.StockMinNumber == 0 {
  6270. warehouse.StockMaxNumber = stock_max_number
  6271. }
  6272. }
  6273. if drup.MaxUnit != drup.MinUnit {
  6274. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6275. return errors.New("库存数量不足")
  6276. }
  6277. }
  6278. if warehouse.StockMinNumber <= 0 {
  6279. warehouse.StockMinNumber = 0
  6280. }
  6281. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6282. fmt.Println(errThree)
  6283. //查询默认仓库
  6284. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6285. //查询默认仓库剩余多少库存
  6286. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6287. var sum_count int64
  6288. var sum_in_count int64
  6289. for _, it := range list {
  6290. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6291. if it.MaxUnit == baseDrug.MaxUnit {
  6292. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6293. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6294. sum_count += it.StockMaxNumber + it.StockMinNumber
  6295. sum_in_count += it.WarehousingCount
  6296. }
  6297. }
  6298. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6299. return nil
  6300. } else {
  6301. warehouse.StockMaxNumber = 0
  6302. warehouse.StockMinNumber = 0
  6303. warehouse.Mtime = time.Now().Unix()
  6304. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6305. fmt.Println(errThree)
  6306. //查询默认仓库
  6307. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6308. //查询默认仓库剩余多少库存
  6309. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6310. var sum_count int64
  6311. var sum_in_count int64
  6312. for _, it := range list {
  6313. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6314. if it.MaxUnit == baseDrug.MaxUnit {
  6315. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6316. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6317. sum_count += it.StockMaxNumber + it.StockMinNumber
  6318. sum_in_count += it.WarehousingCount
  6319. }
  6320. }
  6321. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6322. if errThree != nil {
  6323. return errThree
  6324. }
  6325. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6326. prescribingNumber_two_temp := deliver_number - stock_number
  6327. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6328. }
  6329. return
  6330. }
  6331. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6332. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6333. return err
  6334. }
  6335. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6336. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6337. return err
  6338. }
  6339. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6340. config := models.DrugOutConfig{}
  6341. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6342. if err == gorm.ErrRecordNotFound {
  6343. return nil, err
  6344. }
  6345. if err != nil {
  6346. return nil, err
  6347. }
  6348. return &config, nil
  6349. }
  6350. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6351. config := models.DrugOutConfig{}
  6352. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6353. return config, err
  6354. }
  6355. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6356. config := models.XtHisConfig{}
  6357. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6358. return config, err
  6359. }
  6360. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6361. err := XTWriteDB().Create(&config).Error
  6362. return err
  6363. }
  6364. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6365. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6366. return err
  6367. }
  6368. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6369. config := models.DrugOutConfig{}
  6370. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6371. return config, err
  6372. }
  6373. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6374. config := models.HisSettleStockConfig{}
  6375. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6376. return config, err
  6377. }
  6378. func UpdatedWarehouseOut(id int64) error {
  6379. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6380. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6381. return err
  6382. }
  6383. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6384. tx := XTWriteDB().Begin()
  6385. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6386. if err != nil {
  6387. tx.Rollback()
  6388. return err
  6389. }
  6390. tx.Commit()
  6391. return err
  6392. }
  6393. func GetLastGoodWarehouseOutInfo(orgid int64, id int64) (info []*models.WarehouseOutInfo, err error) {
  6394. err = XTReadDB().Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6395. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6396. }).Find(&info).Error
  6397. return info, err
  6398. }
  6399. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6400. info := models.WarehouseOutInfo{}
  6401. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6402. return info, err
  6403. }
  6404. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6405. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "count": info.Count, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "storehouse_id": info.StorehouseId, "admin_user_id": info.AdminUserId, "buy_price": info.BuyPrice, "stock_count": info.StockCount, "register_number": info.RegisterNumber}).Error
  6406. return err
  6407. }
  6408. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6409. err := XTWriteDB().Model(&models.VmStockFlow{}).Where("warehouse_out_detail_id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "number": info.Number, "license_number": info.LicenseNumber, "count": info.Count, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "price": info.Price, "expire_date": info.ExpireDate, "product_date": info.ProductDate, "storehouse_id": info.StorehouseId, "admin_user_id": info.AdminUserId, "buy_price": info.BuyPrice, "stock_count": info.StockCount}).Error
  6410. return err
  6411. }
  6412. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6413. out := models.WarehouseOut{}
  6414. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6415. return out, err
  6416. }
  6417. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6418. ut := writeDb.Begin()
  6419. err := ut.Model(&models.WarehousingInfo{}).Where("good_id = ? and org_id = ? and status = 1 and id = ?", good_id, orgid, warehouse_info_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  6420. if err != nil {
  6421. ut.Rollback()
  6422. return err
  6423. }
  6424. ut.Commit()
  6425. return err
  6426. }
  6427. func ModifyGoodWarehouseOut(id int64) error {
  6428. ut := writeDb.Begin()
  6429. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6430. if err != nil {
  6431. ut.Rollback()
  6432. return err
  6433. }
  6434. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6435. if err != nil {
  6436. ut.Rollback()
  6437. return err
  6438. }
  6439. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6440. if err != nil {
  6441. ut.Rollback()
  6442. return err
  6443. }
  6444. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  6445. if err != nil {
  6446. ut.Rollback()
  6447. return err
  6448. }
  6449. ut.Commit()
  6450. return err
  6451. }
  6452. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6453. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6454. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6455. }).Find(&info).Error
  6456. return info, err
  6457. }
  6458. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  6459. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  6460. return info, err
  6461. }
  6462. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  6463. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "warehousing_unit": info.WarehousingUnit, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "license_number": info.LicenseNumber, "packing_price": info.PackingPrice, "register_number": info.RegisterNumber, "storehouse_id": info.StorehouseId}).Error
  6464. return err
  6465. }
  6466. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  6467. tx := XTWriteDB().Begin()
  6468. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6469. if err != nil {
  6470. tx.Rollback()
  6471. return err
  6472. }
  6473. tx.Commit()
  6474. return err
  6475. }
  6476. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  6477. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6478. return info, err
  6479. }
  6480. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  6481. tx := XTWriteDB().Begin()
  6482. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  6483. if err != nil {
  6484. tx.Rollback()
  6485. return err
  6486. }
  6487. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6488. if err != nil {
  6489. tx.Rollback()
  6490. return err
  6491. }
  6492. tx.Commit()
  6493. return err
  6494. }
  6495. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  6496. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6497. return info, err
  6498. }
  6499. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  6500. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6501. return info, err
  6502. }
  6503. func FindLastDrugWarehousingInfoTwenty(org_id int64) (info models.DrugWarehouseInfo, err error) {
  6504. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  6505. return info, err
  6506. }
  6507. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6508. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  6509. return info, err
  6510. }
  6511. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  6512. err := XTWriteDB().Create(&info).Error
  6513. return err
  6514. }
  6515. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  6516. err := XTWriteDB().Save(&info).Error
  6517. return err
  6518. }
  6519. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  6520. tx := XTWriteDB().Begin()
  6521. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6522. if err != nil {
  6523. tx.Rollback()
  6524. return err
  6525. }
  6526. tx.Commit()
  6527. return err
  6528. }
  6529. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6530. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6531. return info, err
  6532. }
  6533. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  6534. tx := XTWriteDB().Begin()
  6535. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6536. if err != nil {
  6537. tx.Rollback()
  6538. return err
  6539. }
  6540. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6541. if err != nil {
  6542. tx.Rollback()
  6543. return err
  6544. }
  6545. tx.Commit()
  6546. return err
  6547. }
  6548. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  6549. tx := XTWriteDB().Begin()
  6550. err := tx.Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  6551. if err != nil {
  6552. tx.Rollback()
  6553. return err
  6554. }
  6555. tx.Commit()
  6556. return err
  6557. }
  6558. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  6559. tx := XTWriteDB().Begin()
  6560. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6561. if err != nil {
  6562. tx.Rollback()
  6563. return err
  6564. }
  6565. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6566. tx.Commit()
  6567. return err
  6568. }
  6569. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  6570. tx := XTWriteDB().Begin()
  6571. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6572. if err != nil {
  6573. tx.Rollback()
  6574. return err
  6575. }
  6576. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6577. if err != nil {
  6578. tx.Rollback()
  6579. return err
  6580. }
  6581. tx.Commit()
  6582. return err
  6583. }
  6584. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  6585. tx := XTWriteDB().Begin()
  6586. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6587. if err != nil {
  6588. tx.Rollback()
  6589. return err
  6590. }
  6591. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6592. if err != nil {
  6593. tx.Rollback()
  6594. return err
  6595. }
  6596. tx.Commit()
  6597. return err
  6598. }
  6599. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  6600. tx := XTWriteDB().Begin()
  6601. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  6602. if err != nil {
  6603. tx.Rollback()
  6604. return err
  6605. }
  6606. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6607. if err != nil {
  6608. tx.Rollback()
  6609. return err
  6610. }
  6611. tx.Commit()
  6612. return err
  6613. }
  6614. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  6615. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "retail_price": info.RetailPrice, "max_unit": info.MaxUnit, "min_unit": info.MinUnit}).Error
  6616. return err
  6617. }
  6618. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  6619. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  6620. return err
  6621. }
  6622. func UpdatedSelfDrugWarehouseInfo(info *models.XtSelfDrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  6623. err := tx.Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  6624. if err != gorm.ErrRecordNotFound {
  6625. return err
  6626. }
  6627. return err
  6628. }
  6629. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  6630. out := models.DrugWarehouseOut{}
  6631. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  6632. return out, err
  6633. }
  6634. func GetSelfDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  6635. out := models.XtSelfDrugWarehouseOut{}
  6636. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  6637. if err != gorm.ErrRecordNotFound {
  6638. if err != nil {
  6639. tx.Rollback()
  6640. return out, err
  6641. }
  6642. }
  6643. return out, err
  6644. }
  6645. func GetSelfDrugLastWarehouseOutOne(id int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  6646. out := models.XtSelfDrugWarehouseOut{}
  6647. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  6648. if err != gorm.ErrRecordNotFound {
  6649. if err != nil {
  6650. tx.Rollback()
  6651. return out, err
  6652. }
  6653. }
  6654. return out, err
  6655. }
  6656. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6657. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  6658. return info, err
  6659. }
  6660. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  6661. tx := XTWriteDB().Begin()
  6662. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  6663. if err != nil {
  6664. tx.Rollback()
  6665. return err
  6666. }
  6667. err = XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6668. if err != nil {
  6669. tx.Rollback()
  6670. return err
  6671. }
  6672. tx.Commit()
  6673. return err
  6674. }
  6675. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  6676. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  6677. return err
  6678. }
  6679. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  6680. out := models.DrugWarehouseOut{}
  6681. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  6682. return out, err
  6683. }
  6684. func UpdateDrugMaxNumber(count int64, id int64) error {
  6685. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6686. return err
  6687. }
  6688. func UpdateDrugMinNumber(count int64, id int64) error {
  6689. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6690. return err
  6691. }
  6692. func DeleteDrugOutFlow(id int64, orgid int64) error {
  6693. err := XTWriteDB().Model(models.DrugFlow{}).Where("warehouse_out_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  6694. return err
  6695. }
  6696. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  6697. err = XTReadDB().Where("warehouse_info_id = ? and good_id = ? and org_id = ? and is_check = 1 and status = 1", warehouse_info_id, good_id, orgid).Find(&outInfo).Error
  6698. return outInfo, err
  6699. }
  6700. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  6701. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and is_check = 1 and status = 1", warehouse_info_id, drug_id, orgid).Find(&outInfo).Error
  6702. return outInfo, err
  6703. }
  6704. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6705. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  6706. return info, err
  6707. }
  6708. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  6709. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  6710. return info, err
  6711. }
  6712. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  6713. err = XTReadDB().Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", goodid, storehouse_id, orgId).Find(&info).Error
  6714. return info, err
  6715. }
  6716. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  6717. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6718. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_count)).Error
  6719. return err
  6720. }
  6721. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  6722. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6723. return err
  6724. }
  6725. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  6726. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6727. return err
  6728. }
  6729. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  6730. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6731. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6732. return err
  6733. }
  6734. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  6735. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id = ?", orgid).Find(&stock).Error
  6736. return stock, err
  6737. }
  6738. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  6739. err := XTWriteDB().Model(&models.CancelStockInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "count": info.Count, "price": info.Price, "total": info.Total, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "register_account": info.RegisterAccount, "remark": info.Remark, "storehouse_id": info.StorehouseId}).Error
  6740. return err
  6741. }
  6742. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6743. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6744. return info, err
  6745. }
  6746. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  6747. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6748. return info, err
  6749. }
  6750. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  6751. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  6752. return info, err
  6753. }
  6754. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  6755. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6756. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6757. return err
  6758. }
  6759. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  6760. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  6761. return info, err
  6762. }
  6763. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  6764. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and status = 1", warehouse_info_id, orgid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", stock_count)).Error
  6765. return err
  6766. }
  6767. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  6768. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6769. return err
  6770. }
  6771. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  6772. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6773. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  6774. return err
  6775. }
  6776. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  6777. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("cancel_out_detail_id = ? and user_org_id = ? and status = 1 and good_id =?", id, orgid, goodid).Update(map[string]interface{}{"status": 0}).Error
  6778. return err
  6779. }
  6780. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  6781. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and good_id = ? and org_id = ? and status = 1", warehouse_info_id, good_id, org_id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", stock_count)).Error
  6782. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and status = 1 and org_id = ?", good_id, org_id).UpdateColumn("sum_count", gorm.Expr("sum_count - ?", stock_count)).Error
  6783. return err
  6784. }
  6785. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6786. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6787. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_in_count)).Error
  6788. return err
  6789. }
  6790. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6791. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  6792. return info, err
  6793. }
  6794. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  6795. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  6796. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", sum_in_count)).Error
  6797. return err
  6798. }
  6799. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  6800. err = XTReadDB().Where("storehouse_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", storehouse_id, orgid, drugid).Find(&info).Error
  6801. return info, err
  6802. }
  6803. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  6804. err = tx.Where("storehouse_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", storehouse_id, orgid, drugid).Find(&info).Error
  6805. if err != gorm.ErrRecordNotFound {
  6806. if err != nil {
  6807. tx.Rollback()
  6808. return info, err
  6809. }
  6810. }
  6811. return info, err
  6812. }
  6813. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  6814. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6815. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6816. return err
  6817. }
  6818. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  6819. err = writeDb.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6820. return err
  6821. }
  6822. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  6823. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6824. if err != nil {
  6825. tx.Rollback()
  6826. return err
  6827. }
  6828. return err
  6829. }
  6830. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6831. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", id, orgid).Preload("DrugWarehouseInfo", "status = 1 and org_id = ?", orgid).Preload("BaseDrugLib", "status = 1 and org_id = ?", orgid).Find(&info).Error
  6832. return info, err
  6833. }
  6834. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6835. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  6836. return info, err
  6837. }
  6838. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  6839. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6840. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  6841. return err
  6842. }
  6843. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  6844. stock := models.DrugCancelStock{}
  6845. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  6846. return stock, err
  6847. }
  6848. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6849. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  6850. return stock, err
  6851. }
  6852. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  6853. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  6854. return stock, err
  6855. }
  6856. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6857. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  6858. return info, err
  6859. }
  6860. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  6861. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6862. return err
  6863. }
  6864. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6865. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  6866. if err != nil {
  6867. tx.Rollback()
  6868. return err
  6869. }
  6870. return err
  6871. }
  6872. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  6873. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6874. return err
  6875. }
  6876. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  6877. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  6878. if err != nil {
  6879. tx.Rollback()
  6880. return err
  6881. }
  6882. return err
  6883. }
  6884. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  6885. err := XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "price": info.Price, "total": info.Total, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "register_account": info.RegisterAccount, "remark": info.Remark, "batch_number": info.BatchNumber, "max_unit": info.MaxUnit, "batch_number_id": info.BatchNumberId, "storehouse_id": info.StorehouseId}).Error
  6886. return err
  6887. }
  6888. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  6889. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6890. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  6891. return err
  6892. }
  6893. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  6894. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  6895. return info, err
  6896. }
  6897. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  6898. err := XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and drug_id = ? and user_org_id = ? and status = 1", cancelstock_id, drugId, orgid).Update(map[string]interface{}{"status": 0}).Error
  6899. return err
  6900. }
  6901. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6902. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6903. return err
  6904. }
  6905. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  6906. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  6907. return err
  6908. }
  6909. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  6910. info := models.DrugWarehouseInfo{}
  6911. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  6912. return info, err
  6913. }
  6914. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  6915. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  6916. if storehouse_id > 0 {
  6917. db = db.Where("storehouse_id = ?", storehouse_id)
  6918. }
  6919. if orgid > 0 {
  6920. db = db.Where("user_org_id = ?", orgid)
  6921. }
  6922. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  6923. return storeinventory, err
  6924. }
  6925. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  6926. detail := models.AutomaticReduceDetail{}
  6927. err := XTReadDB().Where("org_id =? and patient_id = ? and record_time = ? and good_id = ? and status= 1", orgid, patient_id, sys_record_time, good_id).Find(&detail).Error
  6928. return detail, err
  6929. }
  6930. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  6931. detail := models.WarehouseOutInfo{}
  6932. err := XTReadDB().Where("org_id =? and patient_id = ? and sys_record_time = ? and good_id = ? and status= 1", orgid, patient_id, sys_record_time, good_id).Find(&detail).Error
  6933. return detail, err
  6934. }
  6935. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  6936. stock := models.XtGoodStockCount{}
  6937. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  6938. return stock, err
  6939. }
  6940. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  6941. err := XTWriteDB().Create(&stock).Error
  6942. return err
  6943. }
  6944. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6945. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error
  6946. return err
  6947. }
  6948. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  6949. ut := XTWriteDB().Begin()
  6950. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", count)).Error
  6951. if err != nil {
  6952. ut.Rollback()
  6953. return err
  6954. }
  6955. ut.Commit()
  6956. return err
  6957. }
  6958. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  6959. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  6960. return err
  6961. }
  6962. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  6963. err := tx.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  6964. if err != nil {
  6965. tx.Rollback()
  6966. return err
  6967. }
  6968. return err
  6969. }
  6970. func CreateDrugStock(stock models.XtDrugStockCount) error {
  6971. err := XTWriteDB().Create(&stock).Error
  6972. return err
  6973. }
  6974. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  6975. ut := XTWriteDB().Begin()
  6976. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  6977. if err != nil {
  6978. ut.Rollback()
  6979. return err
  6980. }
  6981. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  6982. if err != nil {
  6983. ut.Rollback()
  6984. return err
  6985. }
  6986. ut.Commit()
  6987. return err
  6988. }
  6989. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  6990. ut := XTWriteDB().Begin()
  6991. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  6992. if err != nil {
  6993. ut.Rollback()
  6994. return err
  6995. }
  6996. ut.Commit()
  6997. return err
  6998. }
  6999. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  7000. ut := XTWriteDB().Begin()
  7001. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", sum_out_count)).Error
  7002. if err != nil {
  7003. ut.Rollback()
  7004. return err
  7005. }
  7006. err = ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", sum_out_count)).Error
  7007. if err != nil {
  7008. ut.Rollback()
  7009. return err
  7010. }
  7011. ut.Commit()
  7012. return err
  7013. }
  7014. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  7015. ut := XTWriteDB().Begin()
  7016. err := ut.Model(&models.XtDrugStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and drug_id =?", storehouse_id, user_org_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", sum_out_count)).Error
  7017. if err != nil {
  7018. ut.Rollback()
  7019. return err
  7020. }
  7021. ut.Commit()
  7022. return err
  7023. }
  7024. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7025. ut := XTWriteDB().Begin()
  7026. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count + ?", count)).Error
  7027. if err != nil {
  7028. ut.Rollback()
  7029. return err
  7030. }
  7031. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", count)).Error
  7032. if err != nil {
  7033. ut.Rollback()
  7034. return err
  7035. }
  7036. ut.Commit()
  7037. return err
  7038. }
  7039. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7040. ut := XTWriteDB().Begin()
  7041. err := ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", count)).Error
  7042. if err != nil {
  7043. ut.Rollback()
  7044. return err
  7045. }
  7046. err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count - ?", count)).Error
  7047. if err != nil {
  7048. ut.Rollback()
  7049. return err
  7050. }
  7051. ut.Commit()
  7052. return err
  7053. }
  7054. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  7055. if orgid == 10191 || orgid == 10138 || orgid == 10278 {
  7056. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id=? and is_check= 1 and org_id = ?", storehouse_id, orgid).Preload("GoodInfo", "status = 1").Order("good_id").Find(&info).Error
  7057. return info, err
  7058. } else {
  7059. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id=? and is_check= 1 and org_id = ? and stock_count >0", storehouse_id, orgid).Preload("GoodInfo", "status = 1").Order("good_id").Find(&info).Error
  7060. return info, err
  7061. }
  7062. }
  7063. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  7064. config := models.HisSettleStockConfig{}
  7065. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7066. if err == gorm.ErrRecordNotFound {
  7067. return nil, err
  7068. }
  7069. if err != nil {
  7070. return nil, err
  7071. }
  7072. return &config, nil
  7073. }
  7074. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  7075. err := XTWriteDB().Create(&config).Error
  7076. return err
  7077. }
  7078. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  7079. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  7080. return err
  7081. }
  7082. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  7083. config := models.DrugStockConfig{}
  7084. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7085. return config, err
  7086. }
  7087. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  7088. stockConfig := models.PharmacyConfig{}
  7089. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7090. return stockConfig, err
  7091. }
  7092. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  7093. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7094. return err
  7095. }
  7096. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  7097. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7098. return err
  7099. }
  7100. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  7101. config := models.HisSettleStockConfig{}
  7102. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7103. return config, nil
  7104. }
  7105. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  7106. info := models.GoodInformationFourty{}
  7107. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  7108. return info, err
  7109. }
  7110. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  7111. advice := models.HisDoctorAdvice{}
  7112. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7113. return advice, err
  7114. }
  7115. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  7116. advice := models.DoctorAdvice{}
  7117. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7118. return advice, err
  7119. }
  7120. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  7121. out := models.WarehouseOut{}
  7122. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys = ?", org_id, warehouse_out_time, is_sys).Find(&out).Error
  7123. return out, err
  7124. }
  7125. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  7126. config := models.XtGoodOutConfig{}
  7127. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7128. if err == gorm.ErrRecordNotFound {
  7129. return nil, err
  7130. }
  7131. if err != nil {
  7132. return nil, err
  7133. }
  7134. return &config, nil
  7135. }
  7136. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  7137. err := XTWriteDB().Create(&good).Error
  7138. return err
  7139. }
  7140. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  7141. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  7142. return err
  7143. }
  7144. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  7145. config := models.XtGoodOutConfig{}
  7146. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7147. return config, err
  7148. }
  7149. func UpdateGoodAutoConfig(user_org_id int64) error {
  7150. gobalConfig := models.GobalConfig{}
  7151. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7152. return err
  7153. }
  7154. func UpdateGoodOutConfig(user_org_id int64) error {
  7155. config := models.XtGoodOutConfig{}
  7156. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  7157. return err
  7158. }
  7159. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  7160. err = XTReadDB().Where("patient_id = ? and record_date = ? and status = 1 and user_org_id = ? AND type = 3", patient_id, record_date, orgid).Find(&info).Error
  7161. return info, err
  7162. }
  7163. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  7164. err = XTReadDB().Where("good_id = ? and patient_id = ? and sys_record_time = ? and project_id = ? and status = 1", good_id, patient_id, sys_record_time, project_id).Find(&good).Error
  7165. return good, err
  7166. }
  7167. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  7168. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and good_id = ? and status = 1", warehouse_info_id, orgid, good_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  7169. return err
  7170. }
  7171. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7172. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("good_id = ? and sys_record_time = ? and org_id = ? and project_id = ? and status=1", good_id, sys_record_time, org_id, project_id).Updates(map[string]interface{}{"status": 0}).Error
  7173. return err
  7174. }
  7175. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  7176. err := XTWriteDB().Model(&models.VmStockFlow{}).Where("good_id = ? and system_time = ? and user_org_id = ? and project_id = ? and status=1", good_id, sys_record_time, org_id, project_id).Updates(map[string]interface{}{"status": 0, "is_read": 1}).Error
  7177. return err
  7178. }
  7179. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7180. out := models.WarehouseOut{}
  7181. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Find(&out).Error
  7182. return out, err
  7183. }
  7184. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7185. out := models.WarehouseOut{}
  7186. err = tx.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Find(&out).Error
  7187. if err != gorm.ErrRecordNotFound {
  7188. if err != nil {
  7189. tx.Rollback()
  7190. return out, err
  7191. }
  7192. }
  7193. return out, err
  7194. }
  7195. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  7196. out := models.WarehouseOut{}
  7197. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Last(&out).Error
  7198. return out, err
  7199. }
  7200. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  7201. out := models.WarehouseOut{}
  7202. err = tx.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Last(&out).Error
  7203. if err != gorm.ErrRecordNotFound {
  7204. if err != nil {
  7205. tx.Rollback()
  7206. return out, err
  7207. }
  7208. }
  7209. return out, err
  7210. }
  7211. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  7212. goodsType := models.GoodsType{}
  7213. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  7214. return goodsType, err
  7215. }
  7216. func GetAllGoodTypeName(good_type_id int64, org_id int64) (good_type_name string) {
  7217. goodsTypeList, _ := GetAllGoodType(org_id)
  7218. for _, item := range goodsTypeList {
  7219. if good_type_id == item.ID {
  7220. good_type_name = item.TypeName
  7221. }
  7222. }
  7223. return good_type_name
  7224. }
  7225. func UpdateGoodTypeName(good_id int64, good_type_name string) (models.GoodInformation, error) {
  7226. information := models.GoodInformation{}
  7227. err := XTWriteDB().Model(&information).Where("id = ? and status =1", good_id).Updates(map[string]interface{}{"good_type_name": good_type_name}).Error
  7228. return information, err
  7229. }
  7230. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  7231. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  7232. return info, err
  7233. }
  7234. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  7235. ut := writeDb.Begin()
  7236. err := ut.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Updates(map[string]interface{}{"sum_count": sum_count}).Error
  7237. if err != nil {
  7238. ut.Rollback()
  7239. return err
  7240. }
  7241. ut.Commit()
  7242. return err
  7243. }
  7244. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  7245. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Updates(map[string]interface{}{"sum_count": sum_count}).Error
  7246. if err != nil {
  7247. tx.Rollback()
  7248. return err
  7249. }
  7250. return err
  7251. }
  7252. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  7253. info := models.WarehouseOutInfo{}
  7254. var err error
  7255. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  7256. if err == gorm.ErrRecordNotFound {
  7257. return nil, err
  7258. }
  7259. if err != nil {
  7260. return nil, err
  7261. }
  7262. return &info, nil
  7263. }
  7264. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  7265. info := models.WarehouseOutInfo{}
  7266. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  7267. return info, err
  7268. }
  7269. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7270. info := models.WarehouseOutInfo{}
  7271. err := tx.Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  7272. if err != gorm.ErrRecordNotFound {
  7273. if err != nil {
  7274. tx.Rollback()
  7275. return info, err
  7276. }
  7277. }
  7278. return info, err
  7279. }
  7280. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  7281. info := models.WarehouseOutInfo{}
  7282. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7283. return info, err
  7284. }
  7285. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  7286. info := models.WarehouseOutInfo{}
  7287. err := tx.Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7288. if err != gorm.ErrRecordNotFound {
  7289. if err != nil {
  7290. tx.Rollback()
  7291. return info, err
  7292. }
  7293. }
  7294. return info, err
  7295. }
  7296. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  7297. info := models.WarehouseOutInfo{}
  7298. var err error
  7299. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7300. if err == gorm.ErrRecordNotFound {
  7301. return nil, err
  7302. }
  7303. if err != nil {
  7304. return nil, err
  7305. }
  7306. return &info, nil
  7307. }
  7308. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  7309. info := models.WarehouseOutInfo{}
  7310. var err error
  7311. err = tx.Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  7312. if err != gorm.ErrRecordNotFound {
  7313. if err != nil {
  7314. tx.Rollback()
  7315. return &info, err
  7316. }
  7317. }
  7318. return &info, err
  7319. }
  7320. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  7321. db := XTReadDB().Model(&info).Where("status = 1")
  7322. if startime > 0 {
  7323. db = db.Where("ctime>=?", startime)
  7324. }
  7325. if endtime > 0 {
  7326. db = db.Where("ctime<=?", endtime)
  7327. }
  7328. if orgid > 0 {
  7329. db = db.Where("org_id = ?", orgid)
  7330. }
  7331. if len(good_id) > 0 {
  7332. db = db.Where("good_id in(?)", good_id)
  7333. }
  7334. if storehouse_id > 0 {
  7335. db = db.Where("storehouse_id = ?", storehouse_id)
  7336. }
  7337. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  7338. return info, err
  7339. }
  7340. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  7341. db := XTReadDB().Model(&info).Where("status= 1")
  7342. if start_time > 0 {
  7343. db = db.Where("ctime>=?", start_time)
  7344. }
  7345. if end_time > 0 {
  7346. db = db.Where("ctime <=?", end_time)
  7347. }
  7348. if orgid > 0 {
  7349. db = db.Where("org_id = ?", orgid)
  7350. }
  7351. if len(good_id) > 0 {
  7352. db = db.Where("good_id in(?)", good_id)
  7353. }
  7354. if storehouse_id > 0 {
  7355. db = db.Where("storehouse_id = ?", storehouse_id)
  7356. }
  7357. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  7358. return info, err
  7359. }
  7360. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  7361. config := models.XtGoodOutConfig{}
  7362. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7363. return config, err
  7364. }
  7365. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  7366. err := XTWriteDB().Model(&models.AutomaticReduceDetail{}).Where("good_id = ? and record_time = ? and patient_id = ?", good_id, record_date, patient_id).Updates(map[string]interface{}{"status": 1}).Error
  7367. err = XTWriteDB().Model(&models.DialysisBeforePrepare{}).Where("good_id = ? and record_date = ? and patient_id = ?", good_id, record_date, patient_id).Updates(map[string]interface{}{"status": 1}).Error
  7368. return err
  7369. }
  7370. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  7371. tx := XTWriteDB().Begin()
  7372. for _, item := range list {
  7373. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  7374. if err != nil {
  7375. tx.Rollback()
  7376. return err
  7377. }
  7378. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  7379. if err != nil {
  7380. tx.Rollback()
  7381. return err
  7382. }
  7383. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7384. if err != nil {
  7385. tx.Rollback()
  7386. return err
  7387. }
  7388. var goodList []*models.WarehousingInfo
  7389. err = tx.Where("org_id = ? and storehouse_id = ? and good_id = ? and status =1 and is_check = 1", orgId, item.StorehouseId, item.GoodId).Find(&goodList).Error
  7390. var flush_count int64
  7391. for _, it := range goodList {
  7392. flush_count += it.StockCount
  7393. }
  7394. err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", item.OrgId, item.GoodId, item.StorehouseId).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", item.WarehousingCount)).Error
  7395. if err != nil {
  7396. tx.Rollback()
  7397. return err
  7398. }
  7399. err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", item.OrgId, item.GoodId, item.StorehouseId).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7400. if err != nil {
  7401. tx.Rollback()
  7402. return err
  7403. }
  7404. storeHouseConfig := models.XtStorehouseConfig{}
  7405. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  7406. var stockList []*models.WarehousingInfo
  7407. //查询该耗材剩余多少库存
  7408. err = tx.Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", item.GoodId, storeHouseConfig.StorehouseOutInfo, orgId).Find(&stockList).Error
  7409. var sum_count int64
  7410. var sum_in_count int64
  7411. for _, it := range stockList {
  7412. sum_count += it.StockCount
  7413. sum_in_count += it.WarehousingCount
  7414. }
  7415. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", item.GoodId, orgId).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7416. if err != nil {
  7417. tx.Rollback()
  7418. return err
  7419. }
  7420. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", item.GoodId, orgId).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7421. if err != nil {
  7422. tx.Rollback()
  7423. return err
  7424. }
  7425. }
  7426. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7427. if err != nil {
  7428. tx.Rollback()
  7429. return err
  7430. }
  7431. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  7432. tx.Commit()
  7433. return err
  7434. }
  7435. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  7436. if storehouse_id == 0 {
  7437. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  7438. }
  7439. if storehouse_id > 0 {
  7440. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").First(&info).Error
  7441. }
  7442. return info, err
  7443. }
  7444. func FindFirstWarehousingInfoByStockFour(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  7445. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").Last(&info).Error
  7446. return info, err
  7447. }
  7448. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  7449. err = readDb.Where("project_id = ? and record_date =? and user_org_id =? and status =1 and patient_id = ?", project_id, record_date, orgid, patient_id).Find(&list).Error
  7450. return list, err
  7451. }
  7452. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  7453. err = readDb.Where("good_id =? and sys_record_time = ? and org_id = ? and patient_id = ? and status=1", project_id, record_date, orgid, patient_id).Find(&infor).Error
  7454. return infor, err
  7455. }
  7456. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  7457. info := models.WarehouseOutInfo{}
  7458. err := XTWriteDB().Model(&info).Where("good_id = ? and sys_record_time = ? and org_id = ? and status =1 and patient_id = ?", project_id, record_date, orgid, patient_id).Updates(map[string]interface{}{"status": 0}).Error
  7459. return info, err
  7460. }
  7461. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  7462. info := models.WarehousingInfo{}
  7463. err := XTWriteDB().Where("id = ? and good_id = ? and org_id = ? and status= 1", id, good_id, user_org_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  7464. return info, err
  7465. }
  7466. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  7467. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  7468. return list, err
  7469. }
  7470. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  7471. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  7472. return list, err
  7473. }
  7474. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  7475. templateL := models.HisPrescriptionProjectTemplateL{}
  7476. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  7477. return templateL, err
  7478. }
  7479. func ModifyNewGoodWarehouseInfoStockMinNumber(count int64, goodid int64, orgid int64, id int64, tx *gorm.DB) error {
  7480. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and good_id = ? and status = 1", id, orgid, goodid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  7481. if err != nil {
  7482. tx.Rollback()
  7483. return err
  7484. }
  7485. return err
  7486. }
  7487. func GetDrugInvetoryByDrugId(user_org_id int64, drug_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  7488. err = XTReadDB().Where("org_id = ? and drug_id = ? and status=1", user_org_id, drug_id).Preload("XtBaseDrug", "org_id = ? and status=1", user_org_id).Order("id desc").Find(&info).Error
  7489. return info, err
  7490. }
  7491. func UpdateNewDrugWarehouseInfoById(id int64, last_stock_max_number int64, last_stock_min_number int64) error {
  7492. err := XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"stock_max_number": last_stock_max_number, "stock_min_number": last_stock_min_number}).Error
  7493. return err
  7494. }
  7495. func GeteSelfPrescriptionOpen(user_org_id int64) (models.XtSelfDrugInfoConfig, error) {
  7496. drugInfoConfig := models.XtSelfDrugInfoConfig{}
  7497. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugInfoConfig).Error
  7498. return drugInfoConfig, err
  7499. }
  7500. func CreaterSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  7501. err := XTWriteDB().Create(&infoConfig).Error
  7502. return err
  7503. }
  7504. func SaveSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  7505. err := XTWriteDB().Save(&infoConfig).Error
  7506. return err
  7507. }
  7508. func GetSelfPrescriptionOutOpen(user_org_id int64) (models.XtSelfDrugOutConfig, error) {
  7509. drugOutConfig := models.XtSelfDrugOutConfig{}
  7510. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugOutConfig).Error
  7511. return drugOutConfig, err
  7512. }
  7513. func CreateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  7514. err := XTWriteDB().Create(&config).Error
  7515. return err
  7516. }
  7517. func UpdateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  7518. err := XTWriteDB().Save(&config).Error
  7519. return err
  7520. }
  7521. func CreateSelfDrugFlowOne(flow models.XtSelfDrugFlow, tx *gorm.DB) error {
  7522. err := tx.Create(&flow).Error
  7523. if err != nil {
  7524. tx.Rollback()
  7525. return err
  7526. }
  7527. return err
  7528. }