stock_service.go 398KB

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