stock_service.go 389KB

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