stock_service.go 447KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "github.com/jinzhu/gorm"
  10. )
  11. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  12. err := writeDb.Create(&dealer).Error
  13. return err, dealer
  14. }
  15. func ModifyDealer(dealer *models.Dealer) error {
  16. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  17. "dealer_name": dealer.DealerName,
  18. "contact": dealer.Contact,
  19. "contact_phone": dealer.ContactPhone,
  20. "platform_number": dealer.PlatformNumber,
  21. "email": dealer.Email,
  22. "contact_address": dealer.ContactAddress,
  23. "modifier": dealer.Modifier,
  24. "mtime": time.Now().Unix(),
  25. "remark": dealer.Remark,
  26. "wubi": dealer.WuBi,
  27. "pinyin": dealer.PinYin,
  28. }).Error
  29. return err
  30. }
  31. func FindAllDealerTotal(orgId int64) (total int64) {
  32. db := readDb.Model(&models.Dealer{})
  33. db = db.Where("org_id = ? ", orgId).Count(&total)
  34. return
  35. }
  36. // 获取经销商的名称
  37. func FindAllDealerName(orgId int64) (tmp []*models.Dealer, err error) {
  38. err = readDb.Model(&models.Dealer{}).Where("org_id = ? and status = 1", orgId).Find(&tmp).Error
  39. return
  40. }
  41. func FindAllDealerList(orgId int64, page int64, limit, key int64) (list []*models.Dealer, total int64, err error) {
  42. offset := (page - 1) * limit
  43. db := readDb.Model(&models.Dealer{})
  44. db = db.Where("org_id = ? AND status = 1", orgId)
  45. if key != 0 {
  46. db = db.Where("id = ? ", key)
  47. offset = 0
  48. limit = 10
  49. }
  50. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  51. return
  52. }
  53. func DeleteDealerById(id int64) error {
  54. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  55. return err
  56. }
  57. func FindDealerById(id int64) (*models.Dealer, error) {
  58. dealer := &models.Dealer{}
  59. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  60. return dealer, err
  61. }
  62. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  63. err := writeDb.Create(&manufacturer).Error
  64. return err, manufacturer
  65. }
  66. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  67. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  68. "manufacturer_name": manufacturer.ManufacturerName,
  69. "contact": manufacturer.Contact,
  70. "contact_phone": manufacturer.ContactPhone,
  71. "platform_number": manufacturer.PlatformNumber,
  72. "email": manufacturer.Email,
  73. "contact_address": manufacturer.ContactAddress,
  74. "modifier": manufacturer.Modifier,
  75. "mtime": time.Now().Unix(),
  76. "remark": manufacturer.Remark,
  77. "wubi": manufacturer.WuBi,
  78. "pinyin": manufacturer.PinYin,
  79. }).Error
  80. return err
  81. }
  82. // 获取生产厂商名称
  83. func FindAllManufacturerName(orgid int64) (tmp []*models.Manufacturer, err error) {
  84. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? and status = 1", orgid).Find(&tmp).Error
  85. return
  86. }
  87. func FindAllManufacturerList(orgId int64, page int64, limit, key int64) (list []*models.Manufacturer, total int64, err error) {
  88. offset := (page - 1) * limit
  89. db := readDb.Model(&models.Manufacturer{})
  90. db = db.Where("org_id = ? AND status = 1", orgId)
  91. if key != 0 {
  92. db = db.Where("id = ?", key)
  93. offset = 0
  94. limit = 10
  95. }
  96. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  97. return
  98. }
  99. func DeleteManufacturerById(id int64) error {
  100. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  101. return err
  102. }
  103. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  104. dealer := &models.Manufacturer{}
  105. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  106. return dealer, err
  107. }
  108. func FindManufacturerTotal(orgId int64) (total int64) {
  109. db := readDb.Model(&models.Manufacturer{})
  110. db = db.Where("org_id = ?", orgId).Count(&total)
  111. return
  112. }
  113. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  114. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  115. return dealers, err
  116. }
  117. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  118. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  119. return manufacturer, err
  120. }
  121. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  122. db := readDb.Model(&models.GoodsType{})
  123. db = db.Where("org_id = ?", orgId).Count(&total)
  124. return
  125. }
  126. func GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  127. goodsType := models.GoodsType{}
  128. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  129. if err == gorm.ErrRecordNotFound {
  130. return nil, err
  131. }
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &goodsType, nil
  136. }
  137. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  138. goodsType := models.GoodsType{}
  139. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  140. if err == gorm.ErrRecordNotFound {
  141. return nil, err
  142. }
  143. if err != nil {
  144. return nil, err
  145. }
  146. return &goodsType, nil
  147. }
  148. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  149. err := writeDb.Create(&goodType).Error
  150. return err, goodType
  151. }
  152. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  153. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  154. "modifier": goodType.Modifier,
  155. "mtime": time.Now().Unix(),
  156. "remark": goodType.Remark,
  157. "type_name": goodType.TypeName,
  158. "out_stock": goodType.OutStock,
  159. "stock_attribute": goodType.StockAttribute,
  160. }).Error
  161. return err, goodType
  162. }
  163. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  164. offset := (page - 1) * limit
  165. db := readDb.Model(&models.GoodsType{})
  166. db = db.Where("org_id=? AND status = 1", orgId)
  167. if len(keyword) > 0 {
  168. likeKey := "%" + keyword + "%"
  169. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  170. }
  171. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  172. return
  173. }
  174. func DeleteGoodTypeById(id int64, operateId int64) error {
  175. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  176. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  177. return err
  178. }
  179. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  180. goodType := &models.GoodsType{}
  181. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  182. return goodType, err
  183. }
  184. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  185. db := readDb.Model(&models.GoodInfo{})
  186. db = db.Where("org_id = ?", orgId).Count(&total)
  187. return
  188. }
  189. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  190. err := writeDb.Create(&goodInfo).Error
  191. return err, goodInfo
  192. }
  193. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  194. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  195. "good_type_id": goodInfo.GoodTypeId,
  196. "modifier": goodInfo.Modifier,
  197. "mtime": time.Now().Unix(),
  198. "remark": goodInfo.Remark,
  199. "specification_name": goodInfo.SpecificationName,
  200. "good_unit": goodInfo.GoodUnit,
  201. "buy_price": goodInfo.BuyPrice,
  202. "sell_price": goodInfo.SellPrice,
  203. "manufacturer": goodInfo.Manufacturer,
  204. "dealer": goodInfo.Dealer,
  205. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  206. "stock_warn_count": goodInfo.StockWarnCount,
  207. "is_reuse": goodInfo.IsReuse,
  208. "good_name": goodInfo.GoodName,
  209. "pinyin": goodInfo.Pinyin,
  210. "wubi": goodInfo.Wubi,
  211. "good_kind": goodInfo.GoodKind,
  212. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  213. "retail_price": goodInfo.PackingPrice,
  214. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  215. "is_special_diseases": goodInfo.IsSpecialDiseases,
  216. "is_record": goodInfo.IsRecord,
  217. "statistics_category": goodInfo.StatisticsCategory,
  218. "good_status": goodInfo.GoodStatus,
  219. "default_count": goodInfo.DefaultCount,
  220. "sign": goodInfo.Sign,
  221. "is_default": goodInfo.IsDefault,
  222. "is_charge_use": goodInfo.IsChargeUse,
  223. "is_charge_predict": goodInfo.IsChargePredict,
  224. "is_statistics_work": goodInfo.IsStatisticsWork,
  225. "sort": goodInfo.Sort,
  226. "is_doctor_use": goodInfo.IsDoctorUse,
  227. "agent": goodInfo.Agent,
  228. "good_number": goodInfo.GoodNumber,
  229. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  230. "special_medical": goodInfo.SpecialMedical,
  231. "production_type": goodInfo.ProductionType,
  232. "min_number": goodInfo.MinNumber,
  233. "packing_unit": goodInfo.PackingUnit,
  234. "packing_price": goodInfo.PackingPrice,
  235. "default_count_unit": goodInfo.DefaultCountUnit,
  236. "min_unit": goodInfo.MinUnit,
  237. "register_number": goodInfo.RegisterNumber,
  238. "provinces_code": goodInfo.ProvincesCode,
  239. "is_user": goodInfo.IsUser,
  240. "number": goodInfo.Number,
  241. "is_warehouse": goodInfo.IsWarehouse,
  242. "bbx01": goodInfo.Bbx01,
  243. "bby01": goodInfo.Bby01,
  244. "zuobiao_id": goodInfo.Bbx01,
  245. "first_letter": goodInfo.FirstLetter,
  246. "is_show": goodInfo.IsShow,
  247. "is_zero_flag": goodInfo.IsZeroFlag,
  248. }).Error
  249. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  250. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  251. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  254. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  255. return err, goodInfo
  256. }
  257. 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) {
  258. offset := (page - 1) * limit
  259. db := readDb.Model(&models.GoodInfo{})
  260. db = db.Where("org_id = ? AND status = 1", orgId)
  261. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  262. if len(keyword) > 0 {
  263. likeKey := "%" + keyword + "%"
  264. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  265. if is_use > 0 {
  266. if is_use == 2 {
  267. db = db.Where("find_in_set('停用',good_status) = 0")
  268. } else {
  269. db = db.Where("find_in_set('停用',good_status) > 0")
  270. }
  271. }
  272. if is_charge > 0 {
  273. if is_charge == 1 {
  274. db = db.Where("find_in_set('收费',good_status) > 0")
  275. } else {
  276. db = db.Where("find_in_set('收费',good_status) = 0")
  277. }
  278. }
  279. if good_kind > 0 {
  280. db = db.Where(" good_kind = ?", good_kind)
  281. }
  282. if is_mark > 0 {
  283. db = db.Where("is_mark = ?", is_mark)
  284. }
  285. if manufacturer > 0 {
  286. db = db.Where("manufacturer = ?", manufacturer)
  287. }
  288. if is_type > 0 {
  289. db = db.Where("good_type_id = ?", is_type)
  290. }
  291. } else {
  292. if is_use > 0 {
  293. if is_use == 2 {
  294. db = db.Where("find_in_set('停用',good_status) = 0")
  295. } else {
  296. db = db.Where("find_in_set('停用',good_status) > 0")
  297. }
  298. }
  299. if is_charge > 0 {
  300. if is_charge == 1 {
  301. db = db.Where("find_in_set('收费',good_status) > 0")
  302. } else {
  303. db = db.Where("find_in_set('收费',good_status) = 0")
  304. }
  305. }
  306. if good_kind > 0 {
  307. db = db.Where(" good_kind = ?", good_kind)
  308. }
  309. if is_mark > 0 {
  310. db = db.Where("is_mark = ?", is_mark)
  311. }
  312. if manufacturer > 0 {
  313. db = db.Where("manufacturer = ?", manufacturer)
  314. }
  315. if is_type > 0 {
  316. db = db.Where("good_type_id = ?", is_type)
  317. }
  318. }
  319. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  320. return
  321. }
  322. func DeleteGoodInfoById(id int64, operateId int64) error {
  323. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  324. return err
  325. }
  326. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  327. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  328. return list, err
  329. }
  330. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  331. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  332. return list, err
  333. }
  334. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  335. goodInfo := &models.GoodInfo{}
  336. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  337. return goodInfo, err
  338. }
  339. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  340. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  341. return goodType, err
  342. }
  343. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  344. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  345. return goodType, err
  346. }
  347. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  348. 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
  349. return goodInfo, err
  350. }
  351. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  352. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  353. return goodInfo, err
  354. }
  355. func FindAllGoodInfoThree(org_id int64) (goodInfo []*models.XtGoodInformationFourty, err error) {
  356. err = XTReadDB().Model(&models.XtGoodInformationFourty{}).Where("org_id = ? AND status = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  357. return goodInfo, err
  358. }
  359. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  360. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  361. return total, err
  362. }
  363. func FindNewAllWarehouseTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  364. err = tx.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  365. if err != gorm.ErrRecordNotFound {
  366. if err != nil {
  367. tx.Rollback()
  368. return total, err
  369. }
  370. }
  371. return total, err
  372. }
  373. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  374. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  375. return total, err
  376. }
  377. func FindNewAllWarehouseTotalOne(org_id int64, tx *gorm.DB) (total int64, err error) {
  378. err = tx.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  379. if err != gorm.ErrRecordNotFound {
  380. if err != nil {
  381. tx.Rollback()
  382. return total, err
  383. }
  384. }
  385. return total, err
  386. }
  387. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  388. err := writeDb.Create(&warehouse).Error
  389. return err
  390. }
  391. func AddNewSigleWarehouse(warehouse *models.Warehousing, tx *gorm.DB) error {
  392. err := writeDb.Create(&warehouse).Error
  393. if err != nil {
  394. tx.Rollback()
  395. return err
  396. }
  397. return err
  398. }
  399. func UpdateWarehouse(id int64, storehouse_id int64) error {
  400. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  401. return err
  402. }
  403. func UpdateNewWarehouse(id int64, storehouse_id int64, tx *gorm.DB) error {
  404. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  405. if err != nil {
  406. tx.Rollback()
  407. return err
  408. }
  409. return err
  410. }
  411. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  412. err := writeDb.Create(&warehouse).Error
  413. return err
  414. }
  415. func AddNewSigleDrugWarehouse(warehouse *models.DrugWarehouse, tx *gorm.DB) error {
  416. err := tx.Create(&warehouse).Error
  417. if err != nil {
  418. tx.Rollback()
  419. return err
  420. }
  421. return err
  422. }
  423. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  424. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  425. return err
  426. }
  427. func UpdateNewSigleDrugWarehouse(id int64, storehosue_id int64, tx *gorm.DB) error {
  428. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  429. if err != nil {
  430. tx.Rollback()
  431. return err
  432. }
  433. return err
  434. }
  435. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  436. err := writeDb.Create(&salesReturn).Error
  437. return err
  438. }
  439. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  440. err := writeDb.Create(&salesReturn).Error
  441. return err
  442. }
  443. func UpDateGoodReturnStatus(ids []string) {
  444. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  445. }
  446. func UpDateCanCelStockStatus(ids []string) {
  447. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  448. }
  449. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  450. if len(warehousingInfo) > 0 {
  451. utx := writeDb.Begin()
  452. if len(warehousingInfo) > 0 {
  453. 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,good_code) VALUES "
  454. insertParams := make([]string, 0)
  455. insertData := make([]interface{}, 0)
  456. for _, info := range warehousingInfo {
  457. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  458. insertData = append(insertData, info.WarehousingId)
  459. insertData = append(insertData, info.GoodId)
  460. insertData = append(insertData, info.GoodTypeId)
  461. insertData = append(insertData, info.Number)
  462. insertData = append(insertData, info.ProductDate)
  463. insertData = append(insertData, info.ExpiryDate)
  464. insertData = append(insertData, info.WarehousingCount)
  465. insertData = append(insertData, info.Price)
  466. insertData = append(insertData, info.TotalPrice)
  467. insertData = append(insertData, info.Dealer)
  468. insertData = append(insertData, info.Manufacturer)
  469. insertData = append(insertData, info.Remark)
  470. insertData = append(insertData, info.Ctime)
  471. insertData = append(insertData, info.Mtime)
  472. insertData = append(insertData, info.Status)
  473. insertData = append(insertData, info.OrgId)
  474. insertData = append(insertData, info.WarehousingOrder)
  475. insertData = append(insertData, info.Type)
  476. insertData = append(insertData, info.WarehousingCount)
  477. insertData = append(insertData, info.LicenseNumber)
  478. insertData = append(insertData, info.PackingPrice)
  479. insertData = append(insertData, info.StorehouseId)
  480. insertData = append(insertData, info.GoodCode)
  481. }
  482. thisSQL += strings.Join(insertParams, ", ")
  483. err = utx.Exec(thisSQL, insertData...).Error
  484. if err != nil {
  485. utx.Rollback()
  486. return
  487. }
  488. }
  489. utx.Commit()
  490. }
  491. return
  492. }
  493. func CreateNewWarehousingInfo(warehousingInfo []*models.WarehousingInfo, tx *gorm.DB) (err error) {
  494. if len(warehousingInfo) > 0 {
  495. if len(warehousingInfo) > 0 {
  496. 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,good_code) VALUES "
  497. insertParams := make([]string, 0)
  498. insertData := make([]interface{}, 0)
  499. for _, info := range warehousingInfo {
  500. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  501. insertData = append(insertData, info.WarehousingId)
  502. insertData = append(insertData, info.GoodId)
  503. insertData = append(insertData, info.GoodTypeId)
  504. insertData = append(insertData, info.Number)
  505. insertData = append(insertData, info.ProductDate)
  506. insertData = append(insertData, info.ExpiryDate)
  507. insertData = append(insertData, info.WarehousingCount)
  508. insertData = append(insertData, info.Price)
  509. insertData = append(insertData, info.TotalPrice)
  510. insertData = append(insertData, info.Dealer)
  511. insertData = append(insertData, info.Manufacturer)
  512. insertData = append(insertData, info.Remark)
  513. insertData = append(insertData, info.Ctime)
  514. insertData = append(insertData, info.Mtime)
  515. insertData = append(insertData, info.Status)
  516. insertData = append(insertData, info.OrgId)
  517. insertData = append(insertData, info.WarehousingOrder)
  518. insertData = append(insertData, info.Type)
  519. insertData = append(insertData, info.WarehousingCount)
  520. insertData = append(insertData, info.LicenseNumber)
  521. insertData = append(insertData, info.PackingPrice)
  522. insertData = append(insertData, info.StorehouseId)
  523. insertData = append(insertData, info.GoodCode)
  524. }
  525. thisSQL += strings.Join(insertParams, ", ")
  526. err = tx.Exec(thisSQL, insertData...).Error
  527. if err != nil {
  528. tx.Rollback()
  529. return
  530. }
  531. }
  532. }
  533. return
  534. }
  535. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  536. if len(warehousingInfo) > 0 {
  537. utx := writeDb.Begin()
  538. if len(warehousingInfo) > 0 {
  539. 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 "
  540. insertParams := make([]string, 0)
  541. insertData := make([]interface{}, 0)
  542. for _, info := range warehousingInfo {
  543. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  544. insertData = append(insertData, info.WarehousingId)
  545. insertData = append(insertData, info.DrugId)
  546. insertData = append(insertData, info.Number)
  547. insertData = append(insertData, info.ProductDate)
  548. insertData = append(insertData, info.ExpiryDate)
  549. insertData = append(insertData, info.WarehousingCount)
  550. insertData = append(insertData, info.Price)
  551. insertData = append(insertData, info.TotalPrice)
  552. insertData = append(insertData, info.Dealer)
  553. insertData = append(insertData, info.Manufacturer)
  554. insertData = append(insertData, info.Remark)
  555. insertData = append(insertData, info.Ctime)
  556. insertData = append(insertData, info.Mtime)
  557. insertData = append(insertData, info.Status)
  558. insertData = append(insertData, info.OrgId)
  559. insertData = append(insertData, info.WarehousingOrder)
  560. insertData = append(insertData, info.Type)
  561. insertData = append(insertData, info.RetailPrice)
  562. insertData = append(insertData, info.RetailTotalPrice)
  563. insertData = append(insertData, info.StockMaxNumber)
  564. insertData = append(insertData, info.MaxUnit)
  565. insertData = append(insertData, info.MinUnit)
  566. insertData = append(insertData, info.BatchNumber)
  567. insertData = append(insertData, info.StockMinNumber)
  568. insertData = append(insertData, info.StorehouseId)
  569. }
  570. thisSQL += strings.Join(insertParams, ", ")
  571. err = utx.Exec(thisSQL, insertData...).Error
  572. if err != nil {
  573. utx.Rollback()
  574. return
  575. }
  576. }
  577. utx.Commit()
  578. }
  579. return
  580. }
  581. func CreateNewDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo, tx *gorm.DB) (err error) {
  582. if len(warehousingInfo) > 0 {
  583. if len(warehousingInfo) > 0 {
  584. 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 "
  585. insertParams := make([]string, 0)
  586. insertData := make([]interface{}, 0)
  587. for _, info := range warehousingInfo {
  588. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  589. insertData = append(insertData, info.WarehousingId)
  590. insertData = append(insertData, info.DrugId)
  591. insertData = append(insertData, info.Number)
  592. insertData = append(insertData, info.ProductDate)
  593. insertData = append(insertData, info.ExpiryDate)
  594. insertData = append(insertData, info.WarehousingCount)
  595. insertData = append(insertData, info.Price)
  596. insertData = append(insertData, info.TotalPrice)
  597. insertData = append(insertData, info.Dealer)
  598. insertData = append(insertData, info.Manufacturer)
  599. insertData = append(insertData, info.Remark)
  600. insertData = append(insertData, info.Ctime)
  601. insertData = append(insertData, info.Mtime)
  602. insertData = append(insertData, info.Status)
  603. insertData = append(insertData, info.OrgId)
  604. insertData = append(insertData, info.WarehousingOrder)
  605. insertData = append(insertData, info.Type)
  606. insertData = append(insertData, info.RetailPrice)
  607. insertData = append(insertData, info.RetailTotalPrice)
  608. insertData = append(insertData, info.StockMaxNumber)
  609. insertData = append(insertData, info.MaxUnit)
  610. insertData = append(insertData, info.MinUnit)
  611. insertData = append(insertData, info.BatchNumber)
  612. insertData = append(insertData, info.StockMinNumber)
  613. insertData = append(insertData, info.StorehouseId)
  614. }
  615. thisSQL += strings.Join(insertParams, ", ")
  616. err = tx.Exec(thisSQL, insertData...).Error
  617. if err != nil {
  618. tx.Rollback()
  619. return
  620. }
  621. }
  622. }
  623. return
  624. }
  625. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  626. if len(warehousingInfo) > 0 {
  627. utx := writeDb.Begin()
  628. if len(warehousingInfo) > 0 {
  629. 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 "
  630. insertParams := make([]string, 0)
  631. insertData := make([]interface{}, 0)
  632. for _, info := range warehousingInfo {
  633. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  634. insertData = append(insertData, info.WarehousingId)
  635. insertData = append(insertData, info.DrugId)
  636. insertData = append(insertData, info.Number)
  637. insertData = append(insertData, info.ProductDate)
  638. insertData = append(insertData, info.ExpiryDate)
  639. insertData = append(insertData, info.WarehousingCount)
  640. insertData = append(insertData, info.Price)
  641. insertData = append(insertData, info.TotalPrice)
  642. insertData = append(insertData, info.Dealer)
  643. insertData = append(insertData, info.Manufacturer)
  644. insertData = append(insertData, info.Remark)
  645. insertData = append(insertData, info.Ctime)
  646. insertData = append(insertData, info.Mtime)
  647. insertData = append(insertData, info.Status)
  648. insertData = append(insertData, info.OrgId)
  649. insertData = append(insertData, info.WarehousingOrder)
  650. insertData = append(insertData, info.Type)
  651. insertData = append(insertData, info.RetailPrice)
  652. insertData = append(insertData, info.RetailTotalPrice)
  653. insertData = append(insertData, info.StockMaxNumber)
  654. insertData = append(insertData, info.MaxUnit)
  655. insertData = append(insertData, info.MinUnit)
  656. insertData = append(insertData, info.BatchNumber)
  657. insertData = append(insertData, info.StockMinNumber)
  658. insertData = append(insertData, info.SupplyWarehouseId)
  659. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  660. insertData = append(insertData, info.StorehouseId)
  661. insertData = append(insertData, info.IsCheck)
  662. }
  663. thisSQL += strings.Join(insertParams, ", ")
  664. err = utx.Exec(thisSQL, insertData...).Error
  665. if err != nil {
  666. utx.Rollback()
  667. return
  668. }
  669. }
  670. utx.Commit()
  671. }
  672. return
  673. }
  674. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  675. var total float64
  676. var min_number int64
  677. for _, item := range warehousingInfo {
  678. //查询刚入库耗材的库存
  679. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  680. //更新药品库的总量
  681. info, _ := FindeDrugInfo(item.DrugId)
  682. min_number = count.StockMaxNumber * info.MinNumber
  683. str := strconv.FormatInt(min_number, 10)
  684. minFloat, _ := strconv.ParseFloat(str, 64)
  685. total = info.Total + minFloat
  686. lib := models.BaseDrugLib{
  687. Total: total,
  688. }
  689. UpdateBaseDrug(&lib, item.ID)
  690. }
  691. return err
  692. }
  693. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  694. info := models.XtDrugWarehouseInfo{}
  695. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  696. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  697. return info, err
  698. }
  699. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  700. lib := models.BaseDrugLib{}
  701. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  702. return lib, err
  703. }
  704. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  705. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  706. return err
  707. }
  708. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  709. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  710. return err
  711. }
  712. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  713. info := models.DrugWarehouseInfo{}
  714. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  715. return info, err
  716. }
  717. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  718. out := models.DrugWarehouseOut{}
  719. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  720. return out, err
  721. }
  722. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  723. if len(drugflow) > 0 {
  724. utx := writeDb.Begin()
  725. if len(drugflow) > 0 {
  726. 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 "
  727. insertParams := make([]string, 0)
  728. insertData := make([]interface{}, 0)
  729. for _, info := range drugflow {
  730. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  731. insertData = append(insertData, info.WarehousingId)
  732. insertData = append(insertData, info.DrugId)
  733. insertData = append(insertData, info.Number)
  734. insertData = append(insertData, info.BatchNumber)
  735. insertData = append(insertData, info.Count)
  736. insertData = append(insertData, info.UserOrgId)
  737. insertData = append(insertData, info.PatientId)
  738. insertData = append(insertData, info.SystemTime)
  739. insertData = append(insertData, info.ConsumableType)
  740. insertData = append(insertData, info.IsSys)
  741. insertData = append(insertData, info.WarehousingOrder)
  742. insertData = append(insertData, info.WarehouseOutId)
  743. insertData = append(insertData, info.WarehouseOutOrderNumber)
  744. insertData = append(insertData, info.IsEdit)
  745. insertData = append(insertData, info.CancelStockId)
  746. insertData = append(insertData, info.CancelOrderNumber)
  747. insertData = append(insertData, info.Manufacturer)
  748. insertData = append(insertData, info.Dealer)
  749. insertData = append(insertData, info.Creator)
  750. insertData = append(insertData, info.UpdateCreator)
  751. insertData = append(insertData, info.Status)
  752. insertData = append(insertData, info.Ctime)
  753. insertData = append(insertData, info.Mtime)
  754. insertData = append(insertData, info.Price)
  755. insertData = append(insertData, info.WarehousingDetailId)
  756. insertData = append(insertData, info.WarehouseOutDetailId)
  757. insertData = append(insertData, info.CancelOutDetailId)
  758. insertData = append(insertData, info.ExpireDate)
  759. insertData = append(insertData, info.ProductDate)
  760. insertData = append(insertData, info.MaxUnit)
  761. insertData = append(insertData, info.MinUnit)
  762. insertData = append(insertData, info.StorehouseId)
  763. }
  764. thisSQL += strings.Join(insertParams, ", ")
  765. err = utx.Exec(thisSQL, insertData...).Error
  766. if err != nil {
  767. utx.Rollback()
  768. return
  769. }
  770. }
  771. utx.Commit()
  772. }
  773. return
  774. }
  775. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  776. info := models.DrugFlow{}
  777. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  778. if err == gorm.ErrRecordNotFound {
  779. return nil, err
  780. }
  781. if err != nil {
  782. return nil, err
  783. }
  784. return &info, nil
  785. }
  786. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  787. info := models.XtDrugWarehouseInfo{}
  788. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  789. return info, err
  790. }
  791. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  792. warehouseInfo := models.XtDrugWarehouseInfo{}
  793. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  794. return err
  795. }
  796. func CreateDrugFlowOne(flow models.DrugFlow) error {
  797. ut := writeDb.Begin()
  798. err := ut.Create(&flow).Error
  799. if err != nil {
  800. ut.Rollback()
  801. return err
  802. }
  803. ut.Commit()
  804. return err
  805. }
  806. func CreateSelfDrugFlowTwo(flow models.XtSelfDrugFlow) error {
  807. ut := writeDb.Begin()
  808. err := ut.Create(&flow).Error
  809. if err != nil {
  810. ut.Rollback()
  811. return err
  812. }
  813. ut.Commit()
  814. return err
  815. }
  816. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  817. err := tx.Create(&flow).Error
  818. if err != nil {
  819. tx.Rollback()
  820. return err
  821. }
  822. return err
  823. }
  824. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  825. detail := models.DrugAutomaticReduceDetail{}
  826. 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
  827. 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
  828. return detail, err
  829. }
  830. func UpdateSelfDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.XtSelfDrugWarehouseOutInfo, error) {
  831. detail := models.XtSelfDrugWarehouseOutInfo{}
  832. err = XTWriteDB().Model(&models.XtSelfDrugWarehouseOutInfo{}).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
  833. return detail, err
  834. }
  835. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  836. flow := models.DrugFlow{}
  837. 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
  838. return flow, err
  839. }
  840. func UpdateDrugFlowTensOne(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.XtSelfDrugFlow, error) {
  841. flow := models.XtSelfDrugFlow{}
  842. 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
  843. return flow, err
  844. }
  845. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  846. tx := XTWriteDB().Begin()
  847. err := tx.Create(&flow).Error
  848. if err != nil {
  849. tx.Rollback()
  850. return err
  851. }
  852. tx.Commit()
  853. return err
  854. }
  855. func CreateNewDrugFlowTwo(flow *models.DrugFlow, tx *gorm.DB) error {
  856. err := tx.Create(&flow).Error
  857. if err != nil {
  858. tx.Rollback()
  859. return err
  860. }
  861. return err
  862. }
  863. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  864. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  865. return info, err
  866. }
  867. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  868. info := models.WarehousingInfo{}
  869. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  870. return info, err
  871. }
  872. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  873. err := XTWriteDB().Create(&info).Error
  874. return err
  875. }
  876. func CreatedNewWarehouseingDetail(info *models.WarehousingInfo, tx *gorm.DB) error {
  877. err := tx.Create(&info).Error
  878. if err != nil {
  879. tx.Rollback()
  880. return err
  881. }
  882. return err
  883. }
  884. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  885. if storehouse_id == 0 {
  886. 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
  887. }
  888. if storehouse_id > 0 {
  889. 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
  890. }
  891. return info, err
  892. }
  893. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  894. if storehouse_id == 0 {
  895. 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
  896. if err != gorm.ErrRecordNotFound {
  897. if err != nil {
  898. tx.Rollback()
  899. return info, err
  900. }
  901. }
  902. }
  903. if storehouse_id > 0 {
  904. 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
  905. if err != gorm.ErrRecordNotFound {
  906. if err != nil {
  907. tx.Rollback()
  908. return info, err
  909. }
  910. }
  911. }
  912. return info, err
  913. }
  914. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  915. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  916. if good_id > 0 {
  917. db = db.Where("x.good_id = ?", good_id)
  918. }
  919. if good_type_id > 0 {
  920. db = db.Where("x.good_type_id = ?", good_type_id)
  921. }
  922. if storehouse_id > 0 {
  923. db = db.Where("x.storehouse_id = ?", storehouse_id)
  924. }
  925. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  926. return info, err
  927. }
  928. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  929. info := models.WarehousingInfo{}
  930. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  931. return info, err
  932. }
  933. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  934. info := models.WarehousingInfo{}
  935. var err error
  936. if storehouse_id == 0 {
  937. 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
  938. }
  939. if storehouse_id > 0 {
  940. 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
  941. }
  942. return info, err
  943. }
  944. func FindNewWarehousingInfoTen(goodId int64, storehouse_id int64, tx *gorm.DB) (models.WarehousingInfo, error) {
  945. info := models.WarehousingInfo{}
  946. var err error
  947. if storehouse_id == 0 {
  948. err = tx.Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and is_check = 1", goodId).Find(&info).Error
  949. if err != gorm.ErrRecordNotFound {
  950. if err != nil {
  951. tx.Rollback()
  952. return info, err
  953. }
  954. }
  955. }
  956. if storehouse_id > 0 {
  957. err := tx.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
  958. if err != gorm.ErrRecordNotFound {
  959. if err != nil {
  960. tx.Rollback()
  961. return info, err
  962. }
  963. }
  964. }
  965. return info, err
  966. }
  967. //药品先进先出,找出最先入库的批次
  968. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  969. 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
  970. return info, err
  971. }
  972. func FindLastDrugWarehousingInfoByIDTwo(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  973. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").Last(&info).Error
  974. return info, err
  975. }
  976. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  977. 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
  978. if err != gorm.ErrRecordNotFound {
  979. if err != nil {
  980. tx.Rollback()
  981. return info, err
  982. }
  983. }
  984. return info, err
  985. }
  986. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  987. if is_source == 1 {
  988. 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
  989. }
  990. if is_source == 2 || is_source == 0 {
  991. 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
  992. }
  993. return info, err
  994. }
  995. // 出库时先进先出,找出最先入库的批次
  996. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  997. 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
  998. return info, err
  999. }
  1000. //
  1001. //// 出库时先进先出,找出最先入库的批次
  1002. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  1003. // 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
  1004. // return info, err
  1005. //}
  1006. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  1007. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  1008. return info, err
  1009. }
  1010. 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) {
  1011. offset := (page - 1) * limit
  1012. db := readDb.Model(&models.Warehousing{})
  1013. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  1014. if len(keywords) > 0 {
  1015. likeKey := "%" + keywords + "%"
  1016. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  1017. 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")
  1018. }
  1019. if startTime > 0 {
  1020. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  1021. }
  1022. if endTime > 0 {
  1023. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  1024. }
  1025. if storehouse_id > 0 {
  1026. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  1027. }
  1028. if check_type == 0 {
  1029. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  1030. }
  1031. if check_type > 0 {
  1032. db = db.Where("xt_warehouse.is_check = ?", check_type)
  1033. }
  1034. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  1035. return
  1036. }
  1037. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  1038. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  1039. if orgid > 0 {
  1040. db = db.Where("org_id = ?", orgid)
  1041. }
  1042. if len(keyword) > 0 {
  1043. likeKey := "%" + keyword + "%"
  1044. db = db.Where("good_name like ?", likeKey)
  1045. }
  1046. err = db.Find(&goodInfo).Error
  1047. return goodInfo, err
  1048. }
  1049. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  1050. db := XTReadDB().Model(&info).Where("status =1")
  1051. if len(good_id) > 0 {
  1052. db = db.Where("good_id in (?)", good_id)
  1053. }
  1054. if startime > 0 {
  1055. db = db.Where("ctime >=?", startime)
  1056. }
  1057. if endtime > 0 {
  1058. db = db.Where("ctime<=?", endtime)
  1059. }
  1060. if orgid > 0 {
  1061. db = db.Where("org_id = ?", orgid)
  1062. }
  1063. if storehouse_id > 0 {
  1064. db = db.Where("storehouse_id = ?", storehouse_id)
  1065. }
  1066. err = db.Find(&info).Error
  1067. return info, err
  1068. }
  1069. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  1070. db := XTReadDB().Model(&info).Where("status =1")
  1071. if len(good_id) > 0 {
  1072. db = db.Where("good_id in (?)", good_id)
  1073. }
  1074. if startime > 0 {
  1075. db = db.Where("ctime >=?", startime)
  1076. }
  1077. if endtime > 0 {
  1078. db = db.Where("ctime<=?", endtime)
  1079. }
  1080. if orgid > 0 {
  1081. db = db.Where("org_id = ?", orgid)
  1082. }
  1083. if storehouse_id > 0 {
  1084. db = db.Where("storehouse_id = ?", storehouse_id)
  1085. }
  1086. err = db.Find(&info).Error
  1087. return info, err
  1088. }
  1089. type VMDrugWarehouse struct {
  1090. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1091. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1092. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  1093. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1094. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  1095. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1096. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  1097. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1098. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1099. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  1100. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1101. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1102. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1103. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  1104. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  1105. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1106. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1107. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1108. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  1109. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  1110. }
  1111. func (VMDrugWarehouse) TableName() string {
  1112. return "xt_drug_warehouse"
  1113. }
  1114. type VMDrugWarehouseInfo struct {
  1115. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1116. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  1117. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1118. Number string `gorm:"column:number" json:"number" form:"number"`
  1119. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1120. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1121. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  1122. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1123. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1124. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1125. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1126. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1127. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1128. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1129. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1130. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1131. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  1132. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1133. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1134. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1135. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1136. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1137. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1138. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1139. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1140. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1141. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  1142. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1143. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1144. }
  1145. func (VMDrugWarehouseInfo) TableName() string {
  1146. return "xt_drug_warehouse_info"
  1147. }
  1148. type Drug struct {
  1149. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1150. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  1151. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  1152. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  1153. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  1154. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  1155. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  1156. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  1157. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  1158. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  1159. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  1160. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  1161. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  1162. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  1163. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1164. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1165. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  1166. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1167. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1168. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  1169. Number string `gorm:"column:number" json:"number" form:"number"`
  1170. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  1171. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  1172. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  1173. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  1174. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  1175. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1176. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  1177. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  1178. Code string `gorm:"column:code" json:"code" form:"code"`
  1179. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  1180. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  1181. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  1182. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  1183. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  1184. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  1185. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  1186. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  1187. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  1188. Label int64 `gorm:"column:label" json:"label" form:"label"`
  1189. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  1190. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  1191. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  1192. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  1193. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  1194. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  1195. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1196. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1197. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1198. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1199. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1200. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1201. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  1202. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  1203. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  1204. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  1205. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  1206. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  1207. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  1208. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  1209. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1210. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1211. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1212. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1213. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1214. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1215. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1216. IsZeroFlag int64 `gorm:"column:is_zero_flag" json:"is_zero_flag" form:"is_zero_flag"`
  1217. }
  1218. func (Drug) TableName() string {
  1219. return "xt_base_drug"
  1220. }
  1221. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1222. db := readDb.Model(&VMDrugWarehouse{})
  1223. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1224. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1225. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1226. if len(keywords) > 0 {
  1227. likeKey := "%" + keywords + "%"
  1228. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1229. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1230. 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")
  1231. }
  1232. if startTime > 0 {
  1233. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1234. }
  1235. if endTime > 0 {
  1236. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1237. }
  1238. db = db.Count(&total)
  1239. offset := (page - 1) * limit
  1240. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1241. return
  1242. }
  1243. 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) {
  1244. db := readDb.Model(&VMDrugWarehouse{})
  1245. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1246. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1247. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1248. if len(keywords) > 0 {
  1249. likeKey := "%" + keywords + "%"
  1250. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1251. 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")
  1252. }
  1253. if startTime > 0 {
  1254. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1255. }
  1256. if endTime > 0 {
  1257. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1258. }
  1259. if storehouse_id > 0 {
  1260. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1261. }
  1262. if check_type > 0 {
  1263. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1264. }
  1265. db = db.Count(&total)
  1266. offset := (page - 1) * limit
  1267. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1268. return
  1269. }
  1270. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1271. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1272. return list, err
  1273. }
  1274. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1275. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1276. return list, err
  1277. }
  1278. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1279. 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
  1280. return list, err
  1281. }
  1282. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1283. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1284. return list, err
  1285. }
  1286. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1287. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1288. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1289. fmt.Println(table)
  1290. if len(id) > 0 {
  1291. db = db.Where("x.warehousing_id in(?)", id)
  1292. }
  1293. if org_id > 0 {
  1294. db = db.Where("x.org_id = ?", org_id)
  1295. }
  1296. 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
  1297. return list, err
  1298. }
  1299. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1300. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1301. return warehousing, err
  1302. }
  1303. func FindNewWarehousingById(id int64, tx *gorm.DB) (warehousing models.Warehousing, err error) {
  1304. err = tx.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1305. if err != gorm.ErrRecordNotFound {
  1306. if err != nil {
  1307. tx.Rollback()
  1308. return warehousing, err
  1309. }
  1310. }
  1311. return warehousing, err
  1312. }
  1313. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1314. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1315. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1316. return warehousing, err
  1317. }
  1318. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1319. 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
  1320. return warehousing, err
  1321. }
  1322. func FindNewDrugWarehousingById(id int64, org_id int64, tx *gorm.DB) (warehousing VMDrugWarehouse, err error) {
  1323. err = tx.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
  1324. if err != gorm.ErrRecordNotFound {
  1325. if err != nil {
  1326. tx.Rollback()
  1327. return warehousing, err
  1328. }
  1329. }
  1330. return warehousing, err
  1331. }
  1332. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1333. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1334. if len(id) > 0 {
  1335. db = db.Where("x.id in(?)", id)
  1336. }
  1337. if org_id > 0 {
  1338. db = db.Where("x.org_id = ?", org_id)
  1339. }
  1340. 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
  1341. return warehousing, err
  1342. }
  1343. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1344. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1345. // return warehousing, err
  1346. //
  1347. //}
  1348. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1349. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1350. return
  1351. }
  1352. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1353. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1354. return
  1355. }
  1356. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1357. if len(salesReturnInfo) > 0 {
  1358. utx := writeDb.Begin()
  1359. if len(salesReturnInfo) > 0 {
  1360. 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 "
  1361. insertParams := make([]string, 0)
  1362. insertData := make([]interface{}, 0)
  1363. for _, info := range salesReturnInfo {
  1364. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1365. insertData = append(insertData, info.GoodId)
  1366. insertData = append(insertData, info.SalesReturnId)
  1367. insertData = append(insertData, info.GoodTypeId)
  1368. insertData = append(insertData, info.Count)
  1369. insertData = append(insertData, info.Price)
  1370. insertData = append(insertData, info.Total)
  1371. insertData = append(insertData, info.Ctime)
  1372. insertData = append(insertData, info.Status)
  1373. insertData = append(insertData, info.OrgId)
  1374. insertData = append(insertData, info.OrderNumber)
  1375. insertData = append(insertData, info.Type)
  1376. insertData = append(insertData, info.Dealer)
  1377. insertData = append(insertData, info.Manufacturer)
  1378. }
  1379. thisSQL += strings.Join(insertParams, ", ")
  1380. err = utx.Exec(thisSQL, insertData...).Error
  1381. if err != nil {
  1382. utx.Rollback()
  1383. return
  1384. }
  1385. }
  1386. utx.Commit()
  1387. }
  1388. return
  1389. }
  1390. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1391. if len(salesReturnInfo) > 0 {
  1392. utx := writeDb.Begin()
  1393. if len(salesReturnInfo) > 0 {
  1394. 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 "
  1395. insertParams := make([]string, 0)
  1396. insertData := make([]interface{}, 0)
  1397. for _, info := range salesReturnInfo {
  1398. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1399. insertData = append(insertData, info.DrugId)
  1400. insertData = append(insertData, info.SalesReturnId)
  1401. insertData = append(insertData, info.Count)
  1402. insertData = append(insertData, info.Price)
  1403. insertData = append(insertData, info.Total)
  1404. insertData = append(insertData, info.Ctime)
  1405. insertData = append(insertData, info.Status)
  1406. insertData = append(insertData, info.OrgId)
  1407. insertData = append(insertData, info.OrderNumber)
  1408. insertData = append(insertData, info.Type)
  1409. insertData = append(insertData, info.Dealer)
  1410. insertData = append(insertData, info.Manufacturer)
  1411. insertData = append(insertData, info.RetailPrice)
  1412. insertData = append(insertData, info.RetailTotalPrice)
  1413. }
  1414. thisSQL += strings.Join(insertParams, ", ")
  1415. err = utx.Exec(thisSQL, insertData...).Error
  1416. if err != nil {
  1417. utx.Rollback()
  1418. return
  1419. }
  1420. }
  1421. utx.Commit()
  1422. }
  1423. return
  1424. }
  1425. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1426. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1427. return total, err
  1428. }
  1429. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1430. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1431. return total, err
  1432. }
  1433. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1434. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1435. if err != gorm.ErrRecordNotFound {
  1436. if err != nil {
  1437. tx.Rollback()
  1438. return total, err
  1439. }
  1440. }
  1441. return total, err
  1442. }
  1443. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1444. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1445. return total, err
  1446. }
  1447. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1448. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1449. if err != gorm.ErrRecordNotFound {
  1450. if err != nil {
  1451. tx.Rollback()
  1452. return total, err
  1453. }
  1454. }
  1455. return total, err
  1456. }
  1457. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1458. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1459. // return total, err
  1460. //}
  1461. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1462. db := readDb.Model(&models.SalesReturn{})
  1463. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1464. if len(keywords) > 0 {
  1465. likeKey := "%" + keywords + "%"
  1466. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1467. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1468. 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")
  1469. }
  1470. if startTime > 0 {
  1471. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1472. }
  1473. if endTime > 0 {
  1474. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1475. }
  1476. db = db.Count(&total)
  1477. offset := (page - 1) * limit
  1478. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1479. return
  1480. }
  1481. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1482. db := readDb.Model(&models.DrugSalesReturn{})
  1483. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1484. if len(keywords) > 0 {
  1485. likeKey := "%" + keywords + "%"
  1486. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1487. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1488. 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")
  1489. }
  1490. if startTime > 0 {
  1491. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1492. }
  1493. if endTime > 0 {
  1494. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1495. }
  1496. db = db.Count(&total)
  1497. offset := (page - 1) * limit
  1498. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1499. return
  1500. }
  1501. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1502. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1503. return list, err
  1504. }
  1505. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1506. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1507. return list, err
  1508. }
  1509. type DrugCancelStockInfo struct {
  1510. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1511. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1512. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1513. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1514. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1515. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1516. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1517. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1518. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1519. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1520. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1521. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1522. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1523. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1524. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1525. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1526. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1527. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1528. Number string `gorm:"column:number" json:"number" form:"number"`
  1529. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1530. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1531. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1532. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1533. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1534. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1535. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1536. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1537. }
  1538. func (DrugCancelStockInfo) TableName() string {
  1539. return "xt_drug_cancel_stock_info"
  1540. }
  1541. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1542. 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
  1543. return list, err
  1544. }
  1545. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1546. db := readDb.Model(&models.WarehousingInfo{})
  1547. db = db.Where("org_id = ? AND status = 1", orgId)
  1548. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1549. if startTime > 0 {
  1550. db = db.Where("ctime >=?", startTime)
  1551. }
  1552. if endTime > 0 {
  1553. db = db.Where("ctime<= ?", endTime)
  1554. }
  1555. db = db.Count(&total)
  1556. offset := (page - 1) * limit
  1557. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1558. return list, total, err
  1559. }
  1560. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1561. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1562. return total, err
  1563. }
  1564. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1565. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1566. return total, err
  1567. }
  1568. func FindAllSelfDrugWarehouseOut(org_id int64, tx *gorm.DB) (total int64, err error) {
  1569. err = tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1570. if err != gorm.ErrRecordNotFound {
  1571. if err != nil {
  1572. tx.Rollback()
  1573. return
  1574. }
  1575. }
  1576. return total, err
  1577. }
  1578. func AddSigleWarehouseOutOne(warehouseOut *models.WarehouseOut) error {
  1579. err := XTWriteDB().Create(&warehouseOut).Error
  1580. return err
  1581. }
  1582. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1583. err := tx.Create(&warehouseOut).Error
  1584. if err != nil {
  1585. tx.Rollback()
  1586. return err
  1587. }
  1588. return err
  1589. }
  1590. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1591. err := tx.Create(&warehouseOut).Error
  1592. if err != nil {
  1593. tx.Rollback()
  1594. return err
  1595. }
  1596. return err
  1597. }
  1598. func UpdateSingleWarehoseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1599. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1600. if err != nil {
  1601. tx.Rollback()
  1602. return err
  1603. }
  1604. return err
  1605. }
  1606. func GetDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string) (models.DrugWarehouseOut, error) {
  1607. drugWarehouseOut := models.DrugWarehouseOut{}
  1608. err := XTReadDB().Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).Find(&drugWarehouseOut).Error
  1609. return drugWarehouseOut, err
  1610. }
  1611. func GetNewDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  1612. drugWarehouseOut := models.DrugWarehouseOut{}
  1613. err := tx.Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).Find(&drugWarehouseOut).Error
  1614. if err != gorm.ErrRecordNotFound {
  1615. if err != nil {
  1616. tx.Rollback()
  1617. return drugWarehouseOut, err
  1618. }
  1619. }
  1620. return drugWarehouseOut, err
  1621. }
  1622. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1623. err := writeDb.Create(&warehouseOut).Error
  1624. return err
  1625. }
  1626. func GetSigleDrugWarehouseOutByPatientId(warehousing_time int64, patient_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouse, error) {
  1627. selfDrugWarehouse := models.XtSelfDrugWarehouse{}
  1628. err := tx.Where("warehousing_time = ? and patient_id = ? and is_sys =1 and status=1", warehousing_time, patient_id).Find(&selfDrugWarehouse).Error
  1629. if err != gorm.ErrRecordNotFound {
  1630. if err != nil {
  1631. tx.Rollback()
  1632. return selfDrugWarehouse, err
  1633. }
  1634. }
  1635. return selfDrugWarehouse, err
  1636. }
  1637. func AddSelfSigleDrugWarehouseOut(warehouseOut *models.XtSelfDrugWarehouseOut, tx *gorm.DB) error {
  1638. err := tx.Create(&warehouseOut).Error
  1639. if err != nil {
  1640. tx.Rollback()
  1641. return err
  1642. }
  1643. return err
  1644. }
  1645. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1646. err := tx.Create(&warehouseOut).Error
  1647. if err != nil {
  1648. tx.Rollback()
  1649. return err
  1650. }
  1651. return err
  1652. }
  1653. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1654. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1655. return err
  1656. }
  1657. func UpdateNewSingleDrugWarehouseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1658. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1659. if err != nil {
  1660. tx.Rollback()
  1661. return err
  1662. }
  1663. return err
  1664. }
  1665. func UpdateSelfSingleDrugWarehouseOut(id int64, patient_id int64, tx *gorm.DB) error {
  1666. err := tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"patient_id": patient_id}).Error
  1667. return err
  1668. }
  1669. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1670. if len(warehouseOutInfo) > 0 {
  1671. utx := writeDb.Begin()
  1672. if len(warehouseOutInfo) > 0 {
  1673. 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 "
  1674. insertParams := make([]string, 0)
  1675. insertData := make([]interface{}, 0)
  1676. for _, info := range warehouseOutInfo {
  1677. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1678. insertData = append(insertData, info.WarehouseOutId)
  1679. insertData = append(insertData, info.GoodId)
  1680. insertData = append(insertData, info.GoodTypeId)
  1681. insertData = append(insertData, info.ProductDate)
  1682. insertData = append(insertData, info.ExpiryDate)
  1683. insertData = append(insertData, info.Count)
  1684. insertData = append(insertData, info.Price)
  1685. insertData = append(insertData, info.TotalPrice)
  1686. insertData = append(insertData, info.Remark)
  1687. insertData = append(insertData, info.Ctime)
  1688. insertData = append(insertData, info.Status)
  1689. insertData = append(insertData, info.OrgId)
  1690. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1691. insertData = append(insertData, info.Type)
  1692. insertData = append(insertData, info.Dealer)
  1693. insertData = append(insertData, info.Manufacturer)
  1694. insertData = append(insertData, info.Number)
  1695. insertData = append(insertData, info.LicenseNumber)
  1696. }
  1697. thisSQL += strings.Join(insertParams, ", ")
  1698. err = utx.Exec(thisSQL, insertData...).Error
  1699. if err != nil {
  1700. utx.Rollback()
  1701. return
  1702. }
  1703. }
  1704. utx.Commit()
  1705. }
  1706. return
  1707. }
  1708. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1709. if len(warehouseOutInfo) > 0 {
  1710. utx := writeDb.Begin()
  1711. if len(warehouseOutInfo) > 0 {
  1712. 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 "
  1713. insertParams := make([]string, 0)
  1714. insertData := make([]interface{}, 0)
  1715. for _, info := range warehouseOutInfo {
  1716. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1717. insertData = append(insertData, info.WarehouseOutId)
  1718. insertData = append(insertData, info.DrugId)
  1719. insertData = append(insertData, info.ProductDate)
  1720. insertData = append(insertData, info.ExpiryDate)
  1721. insertData = append(insertData, info.Count)
  1722. insertData = append(insertData, info.Price)
  1723. insertData = append(insertData, info.TotalPrice)
  1724. insertData = append(insertData, info.Remark)
  1725. insertData = append(insertData, info.Ctime)
  1726. insertData = append(insertData, info.Status)
  1727. insertData = append(insertData, info.OrgId)
  1728. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1729. insertData = append(insertData, info.Type)
  1730. insertData = append(insertData, info.Dealer)
  1731. insertData = append(insertData, info.Manufacturer)
  1732. insertData = append(insertData, info.RetailPrice)
  1733. insertData = append(insertData, info.RetailTotalPrice)
  1734. insertData = append(insertData, info.Number)
  1735. insertData = append(insertData, info.BatchNumber)
  1736. insertData = append(insertData, info.CountUnit)
  1737. }
  1738. thisSQL += strings.Join(insertParams, ", ")
  1739. err = utx.Exec(thisSQL, insertData...).Error
  1740. if err != nil {
  1741. utx.Rollback()
  1742. return
  1743. }
  1744. }
  1745. utx.Commit()
  1746. }
  1747. return
  1748. }
  1749. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1750. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1751. return info, err
  1752. }
  1753. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1754. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1755. return info, err
  1756. }
  1757. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1758. db := readDb.Model(&models.WarehouseOut{})
  1759. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1760. if len(keywords) > 0 {
  1761. likeKey := "%" + keywords + "%"
  1762. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1763. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1764. 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")
  1765. }
  1766. if startTime > 0 {
  1767. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1768. }
  1769. if endTime > 0 {
  1770. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1771. }
  1772. db = db.Count(&total)
  1773. offset := (page - 1) * limit
  1774. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1775. return list, total, err
  1776. }
  1777. 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) {
  1778. db := readDb.Model(&models.WarehouseOut{})
  1779. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1780. if len(keywords) > 0 {
  1781. likeKey := "%" + keywords + "%"
  1782. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1783. 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")
  1784. }
  1785. if startTime > 0 {
  1786. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1787. }
  1788. if endTime > 0 {
  1789. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1790. }
  1791. if storehouse_id > 0 {
  1792. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1793. }
  1794. if way_type == 0 {
  1795. 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")
  1796. }
  1797. if way_type == 1 {
  1798. db = db.Where("xt_warehouse_out.is_sys = 0")
  1799. }
  1800. if way_type == 2 {
  1801. db = db.Where("xt_warehouse_out.is_sys = 1")
  1802. }
  1803. if way_type == 3 {
  1804. db = db.Where("xt_warehouse_out.is_sys = 12")
  1805. }
  1806. if way_type == 4 {
  1807. db = db.Where("xt_warehouse_out.is_sys = 5")
  1808. }
  1809. if check_type > 0 {
  1810. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1811. }
  1812. if check_type == 0 {
  1813. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1814. }
  1815. db = db.Count(&total)
  1816. offset := (page - 1) * limit
  1817. if orgId == 10191 {
  1818. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1819. } else {
  1820. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1821. }
  1822. return list, total, err
  1823. }
  1824. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1825. db := readDb.Model(&models.DrugWarehouseOut{})
  1826. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1827. if len(keywords) > 0 {
  1828. likeKey := "%" + keywords + "%"
  1829. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1830. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1831. 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")
  1832. }
  1833. if startTime > 0 {
  1834. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1835. }
  1836. if endTime > 0 {
  1837. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1838. }
  1839. db = db.Count(&total)
  1840. offset := (page - 1) * limit
  1841. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1842. return list, total, err
  1843. }
  1844. 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) {
  1845. db := readDb.Model(&models.DrugWarehouseOut{})
  1846. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1847. if len(keywords) > 0 {
  1848. likeKey := "%" + keywords + "%"
  1849. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1850. 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")
  1851. }
  1852. if startTime > 0 {
  1853. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1854. }
  1855. if endTime > 0 {
  1856. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1857. }
  1858. if storehouse_id > 0 {
  1859. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1860. }
  1861. if check_type > 0 {
  1862. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1863. }
  1864. if way_type == 0 {
  1865. 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")
  1866. }
  1867. if way_type == 1 {
  1868. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1869. }
  1870. if way_type == 2 {
  1871. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1872. }
  1873. if way_type == 3 {
  1874. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1875. }
  1876. if way_type == 4 {
  1877. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1878. }
  1879. db = db.Count(&total)
  1880. offset := (page - 1) * limit
  1881. if orgId == 10191 {
  1882. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1883. } else {
  1884. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1885. }
  1886. return list, total, err
  1887. }
  1888. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1889. 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
  1890. return list, err
  1891. }
  1892. type DrugWarehouseOutInfo struct {
  1893. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1894. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1895. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1896. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1897. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1898. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1899. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1900. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1901. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1902. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1903. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1904. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1905. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1906. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1907. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1908. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1909. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1910. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1911. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1912. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1913. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1914. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1915. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1916. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1917. Number string `gorm:"column:number" json:"number" form:"number"`
  1918. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1919. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1920. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1921. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1922. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1923. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1924. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1925. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1926. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1927. ClassType string `gorm:"column:class_type" json:"class_type" form:"class_type"`
  1928. }
  1929. func (DrugWarehouseOutInfo) TableName() string {
  1930. return "xt_drug_warehouse_out_info"
  1931. }
  1932. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1933. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1934. return list, err
  1935. }
  1936. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1937. info := &models.WarehousingInfo{}
  1938. type WarehouseInfoCountAmount struct {
  1939. Total int64
  1940. }
  1941. var result WarehouseInfoCountAmount
  1942. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1943. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1944. return result.Total, info
  1945. }
  1946. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1947. type SalesReturnCountAmount struct {
  1948. Total int64
  1949. }
  1950. var result SalesReturnCountAmount
  1951. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1952. return result.Total, err
  1953. }
  1954. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1955. type WarehouseOutInfoCountAmount struct {
  1956. Total int64
  1957. }
  1958. var result WarehouseOutInfoCountAmount
  1959. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1960. return result.Total, err
  1961. }
  1962. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1963. type CancleStockoCountAmount struct {
  1964. Total int64
  1965. }
  1966. var result CancleStockoCountAmount
  1967. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1968. return result.Total, err
  1969. }
  1970. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1971. info := models.WarehousingInfo{}
  1972. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  1973. return info, err
  1974. }
  1975. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  1976. stock := models.CancelStock{}
  1977. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  1978. if err == gorm.ErrRecordNotFound {
  1979. return nil, err
  1980. }
  1981. if err != nil {
  1982. return nil, err
  1983. }
  1984. return &stock, nil
  1985. }
  1986. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  1987. stock := models.CancelStock{}
  1988. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1989. if err == gorm.ErrRecordNotFound {
  1990. return nil, err
  1991. }
  1992. if err != nil {
  1993. return nil, err
  1994. }
  1995. return &stock, nil
  1996. }
  1997. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  1998. stock := models.CancelStock{}
  1999. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2000. if err != gorm.ErrRecordNotFound {
  2001. if err != nil {
  2002. tx.Rollback()
  2003. return &stock, err
  2004. }
  2005. }
  2006. return &stock, err
  2007. }
  2008. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  2009. err := writeDb.Create(&cancelStock).Error
  2010. return err
  2011. }
  2012. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  2013. err := tx.Create(&cancelStock).Error
  2014. if err != nil {
  2015. tx.Rollback()
  2016. return err
  2017. }
  2018. return err
  2019. }
  2020. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  2021. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2022. return err
  2023. }
  2024. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  2025. stock := models.CancelStock{}
  2026. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2027. return stock, err
  2028. }
  2029. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  2030. stock := models.CancelStock{}
  2031. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2032. if err != gorm.ErrRecordNotFound {
  2033. if err != nil {
  2034. tx.Rollback()
  2035. return stock, err
  2036. }
  2037. }
  2038. return stock, err
  2039. }
  2040. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  2041. stock := models.DrugCancelStock{}
  2042. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2043. return stock, err
  2044. }
  2045. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  2046. stock := models.DrugCancelStock{}
  2047. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2048. if err != gorm.ErrRecordNotFound {
  2049. if err != nil {
  2050. tx.Rollback()
  2051. return stock, err
  2052. }
  2053. }
  2054. return stock, err
  2055. }
  2056. func GetManufactureById(id int64) (models.Manufacturer, error) {
  2057. manufacturer := models.Manufacturer{}
  2058. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2059. return manufacturer, err
  2060. }
  2061. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  2062. manufacturer := models.Manufacturer{}
  2063. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2064. if err != gorm.ErrRecordNotFound {
  2065. if err != nil {
  2066. tx.Rollback()
  2067. return manufacturer, err
  2068. }
  2069. }
  2070. return manufacturer, err
  2071. }
  2072. func GetDealerById(id int64) (models.Dealer, error) {
  2073. dealer := models.Dealer{}
  2074. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2075. return dealer, err
  2076. }
  2077. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  2078. dealer := models.Dealer{}
  2079. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2080. if err != gorm.ErrRecordNotFound {
  2081. if err != nil {
  2082. tx.Rollback()
  2083. return dealer, err
  2084. }
  2085. }
  2086. return dealer, err
  2087. }
  2088. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  2089. err := writeDb.Create(&cancelStock).Error
  2090. return err
  2091. }
  2092. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  2093. err := tx.Create(&cancelStock).Error
  2094. if err != nil {
  2095. tx.Rollback()
  2096. return err
  2097. }
  2098. return err
  2099. }
  2100. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  2101. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2102. return err
  2103. }
  2104. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  2105. // err := tx.Create(&cancelStock).Error
  2106. // return err
  2107. //}
  2108. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  2109. if len(cancelStockInfo) > 0 {
  2110. utx := writeDb.Begin()
  2111. if len(cancelStockInfo) > 0 {
  2112. 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 "
  2113. insertParams := make([]string, 0)
  2114. insertData := make([]interface{}, 0)
  2115. for _, info := range cancelStockInfo {
  2116. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2117. insertData = append(insertData, info.GoodId)
  2118. insertData = append(insertData, info.CancelStockId)
  2119. insertData = append(insertData, info.GoodTypeId)
  2120. insertData = append(insertData, info.Count)
  2121. insertData = append(insertData, info.Price)
  2122. insertData = append(insertData, info.Total)
  2123. insertData = append(insertData, info.Ctime)
  2124. insertData = append(insertData, info.Status)
  2125. insertData = append(insertData, info.OrgId)
  2126. insertData = append(insertData, info.OrderNumber)
  2127. insertData = append(insertData, info.Type)
  2128. insertData = append(insertData, info.Dealer)
  2129. insertData = append(insertData, info.Manufacturer)
  2130. insertData = append(insertData, info.Number)
  2131. insertData = append(insertData, info.RegisterAccount)
  2132. insertData = append(insertData, info.Remark)
  2133. insertData = append(insertData, info.ProductDate)
  2134. insertData = append(insertData, info.ExpiryDate)
  2135. insertData = append(insertData, info.WarehouseInfoId)
  2136. insertData = append(insertData, info.WarehouseInfoId)
  2137. }
  2138. thisSQL += strings.Join(insertParams, ", ")
  2139. err = utx.Exec(thisSQL, insertData...).Error
  2140. if err != nil {
  2141. utx.Rollback()
  2142. return
  2143. }
  2144. }
  2145. utx.Commit()
  2146. }
  2147. return
  2148. }
  2149. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  2150. err := XTWriteDB().Create(&info).Error
  2151. return err
  2152. }
  2153. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  2154. // err := tx.Create(&info).Error
  2155. // return err
  2156. //}
  2157. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  2158. info := models.DrugCancelStockInfo{}
  2159. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2160. return info, err
  2161. }
  2162. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  2163. // info := models.DrugCancelStockInfo{}
  2164. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2165. // return info, err
  2166. //}
  2167. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  2168. err := XTWriteDB().Create(&info).Error
  2169. return err
  2170. }
  2171. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  2172. err := XTWriteDB().Create(&info).Error
  2173. if err != nil {
  2174. tx.Rollback()
  2175. return err
  2176. }
  2177. return err
  2178. }
  2179. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  2180. info := models.CancelStockInfo{}
  2181. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2182. return info, err
  2183. }
  2184. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  2185. info := models.CancelStockInfo{}
  2186. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2187. if err != gorm.ErrRecordNotFound {
  2188. if err != nil {
  2189. tx.Rollback()
  2190. return info, err
  2191. }
  2192. }
  2193. return info, err
  2194. }
  2195. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  2196. info := models.DrugCancelStockInfo{}
  2197. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  2198. return info, err
  2199. }
  2200. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  2201. if len(cancelStockInfo) > 0 {
  2202. utx := writeDb.Begin()
  2203. if len(cancelStockInfo) > 0 {
  2204. 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 "
  2205. insertParams := make([]string, 0)
  2206. insertData := make([]interface{}, 0)
  2207. for _, info := range cancelStockInfo {
  2208. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2209. insertData = append(insertData, info.DrugId)
  2210. insertData = append(insertData, info.CancelStockId)
  2211. insertData = append(insertData, info.Count)
  2212. insertData = append(insertData, info.Price)
  2213. insertData = append(insertData, info.Total)
  2214. insertData = append(insertData, info.Ctime)
  2215. insertData = append(insertData, info.Status)
  2216. insertData = append(insertData, info.OrgId)
  2217. insertData = append(insertData, info.OrderNumber)
  2218. insertData = append(insertData, info.Type)
  2219. insertData = append(insertData, info.Dealer)
  2220. insertData = append(insertData, info.Manufacturer)
  2221. insertData = append(insertData, info.RetailPrice)
  2222. insertData = append(insertData, info.RetailTotalPrice)
  2223. insertData = append(insertData, info.RegisterAccount)
  2224. insertData = append(insertData, info.BatchNumber)
  2225. insertData = append(insertData, info.Remark)
  2226. insertData = append(insertData, info.MaxUnit)
  2227. insertData = append(insertData, info.ProductDate)
  2228. insertData = append(insertData, info.ExpiryDate)
  2229. insertData = append(insertData, info.BatchNumberId)
  2230. }
  2231. thisSQL += strings.Join(insertParams, ", ")
  2232. err = utx.Exec(thisSQL, insertData...).Error
  2233. if err != nil {
  2234. utx.Rollback()
  2235. return
  2236. }
  2237. }
  2238. utx.Commit()
  2239. }
  2240. return
  2241. }
  2242. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  2243. db := readDb.Model(&models.CancelStock{})
  2244. db = db.Where("org_id = ? AND status = 1", orgId)
  2245. err = db.Order("ctime desc").Find(&list).Error
  2246. return
  2247. }
  2248. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2249. db := readDb.Model(&models.WarehousingInfo{})
  2250. db = db.Where("org_id = ? AND status = 1", orgId)
  2251. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2252. if startTime > 0 {
  2253. db = db.Where("ctime >=?", startTime)
  2254. }
  2255. if endTime > 0 {
  2256. db = db.Where("ctime<= ?", endTime)
  2257. }
  2258. db = db.Count(&total)
  2259. offset := (page - 1) * limit
  2260. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2261. return
  2262. }
  2263. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  2264. db := XTReadDB().Model(&models.StockInfo{})
  2265. if startime > 0 {
  2266. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2267. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  2268. })
  2269. }
  2270. if startime == 0 || endtime == 0 {
  2271. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2272. return db.Where("org_id = ? AND status = 1", orgId)
  2273. })
  2274. }
  2275. if endtime > 0 {
  2276. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2277. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  2278. })
  2279. }
  2280. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  2281. db = db.Group("xt_good_information.id")
  2282. if startime == 0 || endtime == 0 {
  2283. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2284. return db.Where("org_id = ? AND status = 1", orgId)
  2285. })
  2286. }
  2287. if startime > 0 {
  2288. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2289. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  2290. })
  2291. }
  2292. if endtime > 0 {
  2293. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2294. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  2295. })
  2296. }
  2297. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  2298. if startime == 0 || endtime == 0 {
  2299. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  2300. }
  2301. if startime > 0 {
  2302. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  2303. }
  2304. if endtime > 0 {
  2305. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2306. }
  2307. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2308. if len(keyword) > 0 {
  2309. likeKey := "%" + keyword + "%"
  2310. 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)
  2311. 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")
  2312. }
  2313. if type_name > 0 {
  2314. 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)
  2315. }
  2316. db = db.Count(&total)
  2317. offset := (page - 1) * limit
  2318. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2319. return
  2320. }
  2321. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2322. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2323. return
  2324. }
  2325. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2326. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2327. return
  2328. }
  2329. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2330. 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
  2331. return
  2332. }
  2333. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2334. 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
  2335. return
  2336. }
  2337. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2338. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2339. "good_id": warehouseInfo.GoodId,
  2340. "number": warehouseInfo.Number,
  2341. "product_date": warehouseInfo.ProductDate,
  2342. "expiry_date": warehouseInfo.ExpiryDate,
  2343. "warehousing_count": warehouseInfo.WarehousingCount,
  2344. "price": warehouseInfo.Price,
  2345. "total_price": warehouseInfo.TotalPrice,
  2346. "mtime": warehouseInfo.Mtime,
  2347. "remark": warehouseInfo.Remark,
  2348. }).Error
  2349. return
  2350. }
  2351. func UpDateWarehouseStatus(id int64) (err error) {
  2352. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2353. return
  2354. }
  2355. func UpdatedStockFlowById(id int64) (err error) {
  2356. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2357. return
  2358. }
  2359. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2360. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2361. return
  2362. }
  2363. func UpdateDrugFlowById(id int64) (err error) {
  2364. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2365. return
  2366. }
  2367. func DeleteWarehouse(ids []string) (err error) {
  2368. ut := writeDb.Begin()
  2369. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2370. if err != nil {
  2371. ut.Rollback()
  2372. return
  2373. }
  2374. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2375. if err != nil {
  2376. ut.Rollback()
  2377. return
  2378. }
  2379. ut.Commit()
  2380. return
  2381. }
  2382. func DeleteStockFlow(ids []string) (err error) {
  2383. ut := writeDb.Begin()
  2384. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2385. if err != nil {
  2386. ut.Rollback()
  2387. return
  2388. }
  2389. ut.Commit()
  2390. return
  2391. }
  2392. func DeleteDrugWarehouse(ids []string) (err error) {
  2393. ut := writeDb.Begin()
  2394. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2395. if err != nil {
  2396. ut.Rollback()
  2397. return
  2398. }
  2399. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2400. if err != nil {
  2401. ut.Rollback()
  2402. return
  2403. }
  2404. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2405. if err != nil {
  2406. ut.Rollback()
  2407. return
  2408. }
  2409. ut.Commit()
  2410. return
  2411. }
  2412. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2413. warehouse := &models.Warehousing{}
  2414. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2415. return warehouse, err
  2416. }
  2417. func FindWareHouseOutByIdOne(id int64) (*models.WarehouseOut, error) {
  2418. warehouseOut := &models.WarehouseOut{}
  2419. err := XTReadDB().Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2420. return warehouseOut, err
  2421. }
  2422. func FindWareHouseOutById(id int64, tx *gorm.DB) (*models.WarehouseOut, error) {
  2423. warehouseOut := &models.WarehouseOut{}
  2424. err := tx.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2425. if err != gorm.ErrRecordNotFound {
  2426. if err != nil {
  2427. tx.Rollback()
  2428. return warehouseOut, err
  2429. }
  2430. }
  2431. return warehouseOut, err
  2432. }
  2433. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2434. warehouseOut := &models.DrugWarehouseOut{}
  2435. 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
  2436. return warehouseOut, err
  2437. }
  2438. func FindNewDrugWareHouseOutById(id int64, org_id int64, tx *gorm.DB) (*models.DrugWarehouseOut, error) {
  2439. warehouseOut := &models.DrugWarehouseOut{}
  2440. err := tx.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
  2441. if err != nil {
  2442. tx.Rollback()
  2443. return warehouseOut, err
  2444. }
  2445. return warehouseOut, err
  2446. }
  2447. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2448. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2449. return
  2450. }
  2451. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2452. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2453. return
  2454. }
  2455. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2456. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2457. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2458. return
  2459. }
  2460. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2461. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2462. return
  2463. }
  2464. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2465. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2466. if manufacturer_id > 0 {
  2467. db = db.Where("manufacturer = ?", manufacturer_id)
  2468. }
  2469. if dealer_id > 0 {
  2470. db = db.Where("dealer = ?", dealer_id)
  2471. }
  2472. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2473. return goodInfo, err
  2474. }
  2475. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2476. db := readDb.Model(&models.Warehousing{})
  2477. likekey := "%" + keywords + "%"
  2478. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2479. db = db.Where("warehousing_order LIKE ? ", likekey)
  2480. db = db.Count(&total)
  2481. offset := (page - 1) * limit
  2482. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2483. return
  2484. }
  2485. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2486. info := models.WarehousingInfo{}
  2487. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2488. return info, err
  2489. }
  2490. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2491. info := models.WarehousingInfo{}
  2492. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2493. return info, err
  2494. }
  2495. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2496. err = writeDb.Save(&info).Error
  2497. return err
  2498. }
  2499. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2500. err = writeDb.Save(&info).Error
  2501. return err
  2502. }
  2503. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2504. 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
  2505. return err
  2506. }
  2507. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2508. 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
  2509. return err
  2510. }
  2511. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2512. info := models.DrugWarehouseInfo{}
  2513. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2514. return info, err
  2515. }
  2516. func EditWarehousing(warehouse models.Warehousing) {
  2517. 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
  2518. }
  2519. func EditNewWarehousing(warehouse models.Warehousing, tx *gorm.DB) {
  2520. err = tx.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
  2521. if err != nil {
  2522. tx.Rollback()
  2523. return
  2524. }
  2525. return
  2526. }
  2527. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2528. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2529. 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)
  2530. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2531. return list, err
  2532. }
  2533. func DeleteSalesReturn(ids []string) (err error) {
  2534. ut := writeDb.Begin()
  2535. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2536. if err != nil {
  2537. ut.Rollback()
  2538. return
  2539. }
  2540. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2541. if err != nil {
  2542. ut.Rollback()
  2543. return
  2544. }
  2545. ut.Commit()
  2546. return
  2547. }
  2548. func DeleteDrugSalesReturn(ids []string) (err error) {
  2549. ut := writeDb.Begin()
  2550. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2551. if err != nil {
  2552. ut.Rollback()
  2553. return
  2554. }
  2555. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2556. if err != nil {
  2557. ut.Rollback()
  2558. return
  2559. }
  2560. ut.Commit()
  2561. return
  2562. }
  2563. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2564. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2565. return
  2566. }
  2567. type DrugSalesReturnInfo struct {
  2568. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2569. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2570. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2571. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2572. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2573. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2574. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2575. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2576. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2577. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2578. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2579. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2580. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2581. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2582. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2583. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2584. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2585. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2586. Number string `gorm:"column:number" json:"number" form:"number"`
  2587. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2588. }
  2589. func (DrugSalesReturnInfo) TableName() string {
  2590. return "xt_drug_sales_return_info"
  2591. }
  2592. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2593. 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
  2594. return
  2595. }
  2596. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2597. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2598. return
  2599. }
  2600. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2601. 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
  2602. return
  2603. }
  2604. func UpDateSaleReturnStatus(id int64) (err error) {
  2605. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2606. return
  2607. }
  2608. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2609. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2610. return
  2611. }
  2612. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2613. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2614. return salesReturn, err
  2615. }
  2616. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2617. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2618. return salesReturn, err
  2619. }
  2620. func EditSaleReturn(sales models.SalesReturn) {
  2621. 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
  2622. }
  2623. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2624. 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
  2625. }
  2626. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2627. err = writeDb.Save(&info).Error
  2628. return err
  2629. }
  2630. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2631. err = writeDb.Save(&info).Error
  2632. return err
  2633. }
  2634. func DeleteWarehouseOut(ids []string) (err error) {
  2635. ut := writeDb.Begin()
  2636. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2637. if err != nil {
  2638. ut.Rollback()
  2639. return
  2640. }
  2641. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2642. if err != nil {
  2643. ut.Rollback()
  2644. return
  2645. }
  2646. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2647. if err != nil {
  2648. ut.Rollback()
  2649. return
  2650. }
  2651. ut.Commit()
  2652. return
  2653. }
  2654. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2655. ut := writeDb.Begin()
  2656. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2657. if err != nil {
  2658. ut.Rollback()
  2659. return
  2660. }
  2661. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2662. if err != nil {
  2663. ut.Rollback()
  2664. return
  2665. }
  2666. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2667. if err != nil {
  2668. ut.Rollback()
  2669. return
  2670. }
  2671. ut.Commit()
  2672. return
  2673. }
  2674. func UpDateWarehouseOutStatus(id int64) (err error) {
  2675. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2676. return
  2677. }
  2678. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2679. ut := writeDb.Begin()
  2680. err = ut.Save(&info).Error
  2681. if err != nil {
  2682. ut.Rollback()
  2683. return err
  2684. }
  2685. ut.Commit()
  2686. return err
  2687. }
  2688. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2689. err = tx.Save(&info).Error
  2690. if err != nil {
  2691. tx.Rollback()
  2692. return err
  2693. }
  2694. return err
  2695. }
  2696. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2697. ut := writeDb.Begin()
  2698. err = ut.Save(&info).Error
  2699. if err != nil {
  2700. ut.Rollback()
  2701. return err
  2702. }
  2703. ut.Commit()
  2704. return err
  2705. }
  2706. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2707. err = tx.Save(&info).Error
  2708. if err != nil {
  2709. tx.Rollback()
  2710. return err
  2711. }
  2712. return err
  2713. }
  2714. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2715. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2716. return
  2717. }
  2718. func EditWarehouseOut(warehouseOut models.WarehouseOut, tx *gorm.DB) {
  2719. err = tx.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
  2720. if err != nil {
  2721. tx.Rollback()
  2722. return
  2723. }
  2724. return
  2725. }
  2726. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2727. 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
  2728. }
  2729. func EditNewDrugWarehouseOut(warehouseOut models.DrugWarehouseOut, tx *gorm.DB) {
  2730. err = tx.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
  2731. if err != nil {
  2732. tx.Rollback()
  2733. return
  2734. }
  2735. return
  2736. }
  2737. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) (err error) {
  2738. err = tx.Save(&info).Error
  2739. if err != nil {
  2740. tx.Rollback()
  2741. return err
  2742. }
  2743. return err
  2744. }
  2745. 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) {
  2746. db := readDb.Model(&models.CancelStock{})
  2747. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2748. if len(keywords) > 0 {
  2749. likeKey := "%" + keywords + "%"
  2750. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2751. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2752. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2753. }
  2754. if startTime > 0 {
  2755. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2756. }
  2757. if endTime > 0 {
  2758. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2759. }
  2760. if storehouse_id > 0 {
  2761. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2762. }
  2763. if check_type > 0 {
  2764. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2765. }
  2766. db = db.Count(&total)
  2767. offset := (page - 1) * limit
  2768. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2769. return
  2770. }
  2771. 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) {
  2772. db := readDb.Model(&models.DrugCancelStock{})
  2773. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2774. if len(keywords) > 0 {
  2775. likeKey := "%" + keywords + "%"
  2776. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2777. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2778. 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")
  2779. }
  2780. if startTime > 0 {
  2781. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2782. }
  2783. if endTime > 0 {
  2784. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2785. }
  2786. if storehoue_id > 0 {
  2787. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2788. }
  2789. if check_type > 0 {
  2790. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2791. }
  2792. db = db.Count(&total)
  2793. offset := (page - 1) * limit
  2794. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2795. return
  2796. }
  2797. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2798. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2799. return info, err
  2800. }
  2801. func DeleteCancelStock(ids []string) (err error) {
  2802. ut := writeDb.Begin()
  2803. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2804. if err != nil {
  2805. ut.Rollback()
  2806. return
  2807. }
  2808. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2809. if err != nil {
  2810. ut.Rollback()
  2811. return
  2812. }
  2813. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2814. if err != nil {
  2815. ut.Rollback()
  2816. return
  2817. }
  2818. ut.Commit()
  2819. return
  2820. }
  2821. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2822. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2823. return info, err
  2824. }
  2825. func DeleteDrugCancelStock(ids []string) (err error) {
  2826. ut := writeDb.Begin()
  2827. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2828. if err != nil {
  2829. ut.Rollback()
  2830. return
  2831. }
  2832. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2833. if err != nil {
  2834. ut.Rollback()
  2835. return
  2836. }
  2837. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2838. if err != nil {
  2839. ut.Rollback()
  2840. return
  2841. }
  2842. ut.Commit()
  2843. return
  2844. }
  2845. func UpDateCancleStockStatus(id int64) (err error) {
  2846. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2847. return
  2848. }
  2849. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2850. info := models.CancelStockInfo{}
  2851. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2852. return info, err
  2853. }
  2854. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2855. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2856. return err
  2857. }
  2858. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2859. 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
  2860. return
  2861. }
  2862. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2863. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2864. return
  2865. }
  2866. func UpDateDrugCancelStockById(id int64) (err error) {
  2867. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2868. return
  2869. }
  2870. func EditCancelStock(sales models.CancelStock) {
  2871. 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
  2872. }
  2873. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2874. 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
  2875. }
  2876. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2877. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2878. return cancelStock, err
  2879. }
  2880. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2881. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2882. return cancelStock, err
  2883. }
  2884. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2885. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2886. return cancelStock, err
  2887. }
  2888. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2889. err = writeDb.Save(&info).Error
  2890. return err
  2891. }
  2892. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2893. err = writeDb.Save(&info).Error
  2894. return err
  2895. }
  2896. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2897. 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
  2898. return list, err
  2899. }
  2900. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2901. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2902. 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)
  2903. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2904. return list, err
  2905. }
  2906. type StockDetailModel struct {
  2907. GoodId int64 `gorm:"good_id" json:"good_id"`
  2908. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2909. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2910. Price float64 `gorm:"price" json:"price"`
  2911. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2912. Type int `gorm:"type" json:"type"`
  2913. Creater int64 `gorm:"creater" json:"creater"`
  2914. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2915. Types int64 `gorm:"types" json:"types"`
  2916. }
  2917. 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) {
  2918. db := readDb.Model(&models.WarehousingInfo{})
  2919. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2920. if len(keywords) > 0 {
  2921. likeKey := "%" + keywords + "%"
  2922. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2923. 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)
  2924. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2925. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2926. 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")
  2927. } else {
  2928. 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)
  2929. }
  2930. if manufacturer > 0 {
  2931. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2932. }
  2933. if storehouse_id > 0 {
  2934. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2935. }
  2936. if order_type > 0 {
  2937. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2938. }
  2939. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2940. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2941. db = db.Count(&total)
  2942. offset := (page - 1) * limit
  2943. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2944. return
  2945. }
  2946. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  2947. db := readDb.Model(&models.WarehousingInfo{})
  2948. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2949. if len(keywords) > 0 {
  2950. likeKey := "%" + keywords + "%"
  2951. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2952. 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)
  2953. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2954. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2955. 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")
  2956. } else {
  2957. 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)
  2958. }
  2959. if manufacturer > 0 {
  2960. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2961. }
  2962. if order_type > 0 {
  2963. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2964. }
  2965. if storehouse_id > 0 {
  2966. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2967. }
  2968. type PriceAmount struct {
  2969. Total float64
  2970. }
  2971. var result []PriceAmount
  2972. err = db.Select("total_price as total").Scan(&result).Error
  2973. var total float64
  2974. for _, value := range result {
  2975. total = total + value.Total
  2976. }
  2977. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2978. return err, total
  2979. }
  2980. func FloatRound(f float64, n int) float64 {
  2981. format := "%." + strconv.Itoa(n) + "f"
  2982. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  2983. return res
  2984. }
  2985. 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) {
  2986. if orgId == 9671 {
  2987. db := readDb.Model(&models.WarehouseOutInfo{})
  2988. 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)
  2989. if len(keywords) > 0 {
  2990. likeKey := "%" + keywords + "%"
  2991. 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)
  2992. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2993. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2994. 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")
  2995. } else {
  2996. 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)
  2997. }
  2998. if manufacturer > 0 {
  2999. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3000. }
  3001. if order_type > 0 {
  3002. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3003. }
  3004. if storehouse_id > 0 {
  3005. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3006. }
  3007. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  3008. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  3009. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3010. db = db.Count(&total)
  3011. offset := (page - 1) * limit
  3012. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3013. return
  3014. } else {
  3015. db := readDb.Model(&models.WarehouseOutInfo{})
  3016. 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.count <> 0 ", orgId)
  3017. if len(keywords) > 0 {
  3018. likeKey := "%" + keywords + "%"
  3019. 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)
  3020. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3021. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3022. 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")
  3023. } else {
  3024. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id and xt_warehouse_out.org_id = xt_warehouse_out_info.org_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)
  3025. }
  3026. if manufacturer > 0 {
  3027. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3028. }
  3029. if order_type > 0 {
  3030. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3031. }
  3032. if storehouse_id > 0 {
  3033. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3034. }
  3035. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ? and is_check =1", orgId)
  3036. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ? and is_check =1", orgId)
  3037. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3038. db = db.Count(&total)
  3039. offset := (page - 1) * limit
  3040. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3041. return
  3042. }
  3043. }
  3044. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64) (err error, totalPrice float64) {
  3045. db := readDb.Model(&models.WarehouseOutInfo{})
  3046. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.count > 0", orgId)
  3047. if len(keywords) > 0 {
  3048. likeKey := "%" + keywords + "%"
  3049. 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)
  3050. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3051. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3052. 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")
  3053. } else {
  3054. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id and xt_warehouse_out.org_id = xt_warehouse_out_info.org_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)
  3055. }
  3056. if manufacturer > 0 {
  3057. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3058. }
  3059. if order_type > 0 {
  3060. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3061. }
  3062. if storehouse_id > 0 {
  3063. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3064. }
  3065. type PriceAmount struct {
  3066. Total float64
  3067. }
  3068. var result []PriceAmount
  3069. err = db.Select("(count * price) as total").Scan(&result).Error
  3070. var total float64
  3071. for _, value := range result {
  3072. total = total + value.Total
  3073. }
  3074. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3075. fmt.Println(total)
  3076. return err, total
  3077. }
  3078. 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) {
  3079. db := readDb.Model(&models.SalesReturnInfo{})
  3080. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  3081. if len(keywords) > 0 {
  3082. likeKey := "%" + keywords + "%"
  3083. 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)
  3084. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  3085. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  3086. 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")
  3087. } else {
  3088. 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)
  3089. }
  3090. if manufacturer > 0 {
  3091. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  3092. }
  3093. if order_type > 0 {
  3094. db = db.Where("xt_sales_return_info.type = ?", order_type)
  3095. }
  3096. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  3097. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3098. offset := (page - 1) * limit
  3099. db = db.Count(&total)
  3100. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  3101. return
  3102. }
  3103. 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) {
  3104. db := readDb.Model(&models.CancelStockInfo{})
  3105. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3106. if len(keywords) > 0 {
  3107. likeKey := "%" + keywords + "%"
  3108. 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)
  3109. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3110. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3111. 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")
  3112. } else {
  3113. 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)
  3114. }
  3115. if manufacturer > 0 {
  3116. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3117. }
  3118. if order_type > 0 {
  3119. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3120. }
  3121. if storehouse_id > 0 {
  3122. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  3123. }
  3124. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3125. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3126. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  3127. return
  3128. }
  3129. 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) {
  3130. db := readDb.Model(&models.CancelStockInfo{})
  3131. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3132. if len(keywords) > 0 {
  3133. likeKey := "%" + keywords + "%"
  3134. 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)
  3135. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3136. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3137. 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")
  3138. } else {
  3139. 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)
  3140. }
  3141. if manufacturer > 0 {
  3142. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3143. }
  3144. if order_type > 0 {
  3145. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3146. }
  3147. if storehouse_id > 0 {
  3148. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  3149. }
  3150. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3151. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3152. db = db.Count(&total)
  3153. offset := (page - 1) * limit
  3154. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  3155. return
  3156. }
  3157. func FindStockInByDealerId(id int64) (total int64, err error) {
  3158. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  3159. return
  3160. }
  3161. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  3162. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  3163. return
  3164. }
  3165. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  3166. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  3167. return
  3168. }
  3169. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  3170. 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)
  3171. return
  3172. }
  3173. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  3174. 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)
  3175. return
  3176. }
  3177. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  3178. 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)
  3179. return
  3180. }
  3181. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  3182. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  3183. return goodType, err
  3184. }
  3185. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  3186. 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
  3187. return info, err
  3188. }
  3189. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  3190. 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
  3191. return
  3192. }
  3193. func FindStockOutByIsSysOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (out models.WarehouseOut, err error) {
  3194. err = tx.Model(&models.WarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).First(&out).Error
  3195. if err != gorm.ErrRecordNotFound {
  3196. if err != nil {
  3197. tx.Rollback()
  3198. return
  3199. }
  3200. }
  3201. return
  3202. }
  3203. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  3204. 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
  3205. return
  3206. }
  3207. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3208. 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
  3209. return
  3210. }
  3211. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  3212. 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
  3213. if err != gorm.ErrRecordNotFound {
  3214. if err != nil {
  3215. tx.Rollback()
  3216. return
  3217. }
  3218. }
  3219. return
  3220. }
  3221. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3222. 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
  3223. return
  3224. }
  3225. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  3226. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  3227. return
  3228. }
  3229. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  3230. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  3231. return
  3232. }
  3233. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3234. info := models.WarehouseOutInfo{}
  3235. 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
  3236. if err == gorm.ErrRecordNotFound {
  3237. return nil, err
  3238. }
  3239. if err != nil {
  3240. return nil, err
  3241. }
  3242. return &info, nil
  3243. }
  3244. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3245. info := models.WarehouseOutInfo{}
  3246. 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
  3247. if err == gorm.ErrRecordNotFound {
  3248. return nil, err
  3249. }
  3250. if err != nil {
  3251. return nil, err
  3252. }
  3253. return &info, nil
  3254. }
  3255. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3256. 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
  3257. return err
  3258. }
  3259. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3260. 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
  3261. return err
  3262. }
  3263. func GetGoodsIsExsit(good_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  3264. outInfo := models.WarehouseOutInfo{}
  3265. err := tx.Where("good_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", good_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3266. if err != gorm.ErrRecordNotFound {
  3267. if err != nil {
  3268. tx.Rollback()
  3269. return outInfo, err
  3270. }
  3271. }
  3272. return outInfo, err
  3273. }
  3274. func GetDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64) (models.XtDrugWarehouseOutInfo, error) {
  3275. outInfo := models.XtDrugWarehouseOutInfo{}
  3276. err := XTReadDB().Where("drug_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", drug_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3277. return outInfo, err
  3278. }
  3279. func GetNewDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.XtDrugWarehouseOutInfo, error) {
  3280. outInfo := models.XtDrugWarehouseOutInfo{}
  3281. err := tx.Where("drug_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", drug_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3282. if err != gorm.ErrRecordNotFound {
  3283. if err != nil {
  3284. tx.Rollback()
  3285. return outInfo, err
  3286. }
  3287. }
  3288. return outInfo, err
  3289. }
  3290. func AddSigleWarehouseOutInfoOne(info *models.WarehouseOutInfo) error {
  3291. err := XTWriteDB().Create(&info).Error
  3292. return err
  3293. }
  3294. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3295. err := tx.Create(&info).Error
  3296. if err != nil {
  3297. tx.Rollback()
  3298. return err
  3299. }
  3300. return err
  3301. }
  3302. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3303. err := tx.Create(&info).Error
  3304. if err != nil {
  3305. tx.Rollback()
  3306. return err
  3307. }
  3308. return err
  3309. }
  3310. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  3311. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3312. return err
  3313. }
  3314. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  3315. err := writeDb.Create(&info).Error
  3316. return err
  3317. }
  3318. func AddSelfSigleDrugWarehouseOutInfo(info *models.XtSelfDrugWarehouseOutInfo, tx *gorm.DB) error {
  3319. err := tx.Create(&info).Error
  3320. if err != nil {
  3321. tx.Rollback()
  3322. return err
  3323. }
  3324. return err
  3325. }
  3326. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  3327. err := tx.Create(&info).Error
  3328. if err != nil {
  3329. tx.Rollback()
  3330. return err
  3331. }
  3332. return err
  3333. }
  3334. func UpdateSigleDrugWarehouseOutInfoDetail(id int64, count int64, tx *gorm.DB) error {
  3335. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3336. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("over_count", gorm.Expr("over_count - ?", count)).Error
  3337. if err != nil {
  3338. tx.Rollback()
  3339. return err
  3340. }
  3341. return err
  3342. }
  3343. func UpdateSigleDrugWarehouseOutInfoDetailOne(id int64, count int64, tx *gorm.DB) error {
  3344. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"count": count}).Error
  3345. return err
  3346. }
  3347. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  3348. ut := writeDb.Begin()
  3349. err := ut.Create(&detail).Error
  3350. if err != nil {
  3351. ut.Rollback()
  3352. return err
  3353. }
  3354. ut.Commit()
  3355. return err
  3356. }
  3357. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  3358. err := writeDb.Create(&detail).Error
  3359. return err
  3360. }
  3361. func GetDrugAutoMaticReduceDetail(drug_id int64, patient_id int64, record_time int64, org_id int64, adivce_id int64, warehouse_info_id int64, tx *gorm.DB) (models.DrugAutomaticReduceDetail, error) {
  3362. detail := models.DrugAutomaticReduceDetail{}
  3363. err := tx.Where("drug_id = ? and patient_id =? and record_time =? and org_id = ? and advice_id = ? and warehouse_info_id =?", drug_id, patient_id, record_time, org_id, adivce_id, warehouse_info_id).Find(&detail).Error
  3364. if err != gorm.ErrRecordNotFound {
  3365. if err != nil {
  3366. tx.Rollback()
  3367. return detail, err
  3368. }
  3369. }
  3370. return detail, err
  3371. }
  3372. func UpdateNewSingleDrugAutoReduceRecordInfo(id int64, count int64, tx *gorm.DB) error {
  3373. err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3374. if err != nil {
  3375. tx.Rollback()
  3376. return err
  3377. }
  3378. return err
  3379. }
  3380. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  3381. err := tx.Create(&detail).Error
  3382. if err != nil {
  3383. tx.Rollback()
  3384. return err
  3385. }
  3386. return err
  3387. }
  3388. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  3389. 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
  3390. return
  3391. }
  3392. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  3393. 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
  3394. return
  3395. }
  3396. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  3397. 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
  3398. return
  3399. }
  3400. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  3401. info := models.WarehouseOutInfo{}
  3402. 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
  3403. return info, err
  3404. }
  3405. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  3406. 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
  3407. return
  3408. }
  3409. func UpdateStockOutInfoCount(org_id int64, id int64) {
  3410. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  3411. }
  3412. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3413. db := readDb.Model(&models.AutomaticReduceDetail{})
  3414. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3415. db = db.Preload("GoodsType", "status = 1")
  3416. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3417. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  3418. db = db.Count(&total)
  3419. err = db.Find(&user).Error
  3420. return
  3421. }
  3422. type DrugAutomaticReduceDetail struct {
  3423. ID int64 `gorm:"column:id" json:"id" form:"id"`
  3424. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  3425. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  3426. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  3427. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  3428. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3429. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3430. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3431. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3432. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3433. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3434. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3435. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3436. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3437. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3438. }
  3439. func (DrugAutomaticReduceDetail) TableName() string {
  3440. return "xt_drug_automatic_reduce_detail"
  3441. }
  3442. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3443. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3444. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3445. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3446. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3447. db = db.Count(&total)
  3448. err = db.Find(&user).Error
  3449. return
  3450. }
  3451. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3452. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3453. }
  3454. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3455. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3456. }
  3457. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3458. 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
  3459. return
  3460. }
  3461. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3462. 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
  3463. return
  3464. }
  3465. type VMWarehousingInfo struct {
  3466. ID int64 `gorm:"column:id" json:"id"`
  3467. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3468. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3469. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3470. Status int64 `gorm:"column:status" json:"status"`
  3471. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3472. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3473. }
  3474. func (VMWarehousingInfo) TableName() string {
  3475. return "xt_warehouse_info"
  3476. }
  3477. type GoodInfo struct {
  3478. ID int64 `gorm:"column:id" json:"id"`
  3479. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3480. Status int64 `gorm:"column:status" json:"status"`
  3481. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3482. }
  3483. func (GoodInfo) TableName() string {
  3484. return "xt_good_information"
  3485. }
  3486. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3487. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3488. return
  3489. }
  3490. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3491. 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
  3492. return
  3493. }
  3494. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3495. likeKey := "%" + keyword + "%"
  3496. db := XTReadDB().Model(&goods)
  3497. if len(keyword) > 0 {
  3498. db = db.Where("type_name like ?", likeKey)
  3499. }
  3500. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3501. return goods, err
  3502. }
  3503. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3504. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3505. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3506. return goodstype, err
  3507. }
  3508. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3509. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3510. if orgid > 0 {
  3511. db = db.Where("x.user_org_id = ?", orgid)
  3512. }
  3513. if recordtime > 0 {
  3514. db = db.Where("x.record_date = ?", recordtime)
  3515. }
  3516. if patientId > 0 {
  3517. db = db.Where("x.patient_id = ?", patientId)
  3518. }
  3519. 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
  3520. return prepare, err
  3521. }
  3522. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3523. detail := models.XtAutomaticReduceDetail{}
  3524. 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
  3525. if err == gorm.ErrRecordNotFound {
  3526. return nil, err
  3527. }
  3528. if err != nil {
  3529. return nil, err
  3530. }
  3531. return &detail, nil
  3532. }
  3533. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3534. 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
  3535. return autoMatic, err
  3536. }
  3537. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3538. 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
  3539. return autoMatic, err
  3540. }
  3541. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3542. var manufacturer models.Manufacturer
  3543. var err error
  3544. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3545. if err == gorm.ErrRecordNotFound {
  3546. return nil, err
  3547. }
  3548. if err != nil {
  3549. return nil, err
  3550. }
  3551. return &manufacturer, nil
  3552. }
  3553. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3554. err := XTWriteDB().Create(manufacturer).Error
  3555. return err
  3556. }
  3557. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3558. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3559. return err
  3560. }
  3561. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3562. var good models.GoodInfo
  3563. var err error
  3564. 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
  3565. if err == gorm.ErrRecordNotFound {
  3566. return nil, err
  3567. }
  3568. if err != nil {
  3569. return nil, err
  3570. }
  3571. return &good, nil
  3572. }
  3573. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3574. err := XTWriteDB().Create(&info).Error
  3575. return err
  3576. }
  3577. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3578. err := XTWriteDB().Model(&info).Where("good_name = ? and good_type_id = ? and specification_name = ? and org_id = ? and status = 1", goodName, goodType, specificationName, orgid).Updates(map[string]interface{}{"good_name": info.GoodName, "good_type_id": info.GoodTypeId, "medical_insurance_level": info.MedicalInsuranceLevel, "good_kind": info.GoodKind, "specification_name": info.SpecificationName, "manufacturer": info.Manufacturer, "good_unit": info.GoodUnit, "retail_price": info.RetailPrice, "stock_warn_count": info.StockWarnCount, "dealer": info.Dealer, "pinyin": info.Pinyin, "wubi": info.Wubi, "buy_price": info.BuyPrice, "medical_insurance_number": info.MedicalInsuranceNumber, "is_special_diseases": info.IsSpecialDiseases, "is_record": info.IsRecord, "statistics_category": info.StatisticsCategory, "good_status": info.GoodStatus, "social_security_directory_code": info.SocialSecurityDirectoryCode, "production_type": info.ProductionType, "special_medical": info.SpecialMedical, "remark": info.Remark, "number": info.Number, "register_number": info.RegisterNumber}).Error
  3579. return err
  3580. }
  3581. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3582. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3583. 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
  3584. return houseOutInfo, err
  3585. }
  3586. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3587. key := "%" + keyword + "%"
  3588. if len(keyword) == 0 {
  3589. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3590. } else {
  3591. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3592. }
  3593. return goodInfo, err
  3594. }
  3595. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3596. var drug models.BaseDrugLib
  3597. var err error
  3598. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3599. if err == gorm.ErrRecordNotFound {
  3600. return nil, err
  3601. }
  3602. if err != nil {
  3603. return nil, err
  3604. }
  3605. return &drug, nil
  3606. }
  3607. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3608. var drug models.BaseDrugLib
  3609. var err error
  3610. 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
  3611. if err == gorm.ErrRecordNotFound {
  3612. return nil, err
  3613. }
  3614. if err != nil {
  3615. return nil, err
  3616. }
  3617. return &drug, nil
  3618. }
  3619. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3620. err := XTWriteDB().Create(&info).Error
  3621. return err
  3622. }
  3623. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3624. err := XTWriteDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and org_id = ? and status = 1", drugName, drugSpec, orgid).Updates(map[string]interface{}{"drug_name": drug.DrugName, "drug_alias": drug.DrugAlias, "drug_spec": drug.DrugSpec, "drug_type": drug.DrugType, "drug_stock_limit": drug.DrugStockLimit, "drug_origin_place": drug.DrugOriginPlace, "drug_dosage_form": drug.DrugDosageForm, "medical_insurance_level": drug.MedicalInsuranceLevel, "max_unit": drug.MaxUnit, "min_unit": drug.MinUnit, "unit_matrixing": drug.UnitMatrixing, "retail_price": drug.RetailPrice, "last_price": drug.LastPrice, "drug_classify": drug.DrugClassify, "manufacturer": drug.Manufacturer, "dealer": drug.Dealer, "pinyin": drug.Pinyin, "wubi": drug.Wubi, "drug_alias_pinyin": drug.DrugAliasPinyin, "drug_alias_wubi": drug.DrugAliasWubi, "drug_category": drug.DrugCategory, "drug_control": drug.DrugControl, "number": drug.Number, "hosp_appr_flag": drug.HospApprFlag, "medical_insurance_number": drug.MedicalInsuranceNumber, "pharmacology_category": drug.PharmacologyCategory, "statistics_category": drug.StatisticsCategory, "code": drug.Code, "is_special_diseases": drug.IsSpecialDiseases, "is_record": drug.IsRecord, "prescription_mark": drug.PrescriptionMark, "social_security_directory_code": drug.SocialSecurityDirectoryCode, "record_date": drug.RecordDate, "drug_remark": drug.DrugRemark, "drug_status": drug.DrugStatus, "limit_remark": drug.LimitRemark, "drug_dose": drug.DrugDose, "single_dose": drug.DrugDose, "drug_dose_unit": drug.DrugDoseUnit, "lmt_used_flag": drug.LmtUsedFlag, "delivery_way": drug.DeliveryWay, "execution_frequency": drug.ExecutionFrequency, "drug_day": drug.DrugDay}).Error
  3625. return err
  3626. }
  3627. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3628. var projoect models.HisProject
  3629. var err error
  3630. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3631. if err == gorm.ErrRecordNotFound {
  3632. return nil, err
  3633. }
  3634. if err != nil {
  3635. return nil, err
  3636. }
  3637. return &projoect, nil
  3638. }
  3639. func CreateProjectInfomation(info *models.HisProject) error {
  3640. err := XTWriteDB().Create(&info).Error
  3641. return err
  3642. }
  3643. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3644. 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
  3645. return err
  3646. }
  3647. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3648. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3649. return warehouseinfo, err
  3650. }
  3651. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3652. 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
  3653. return err
  3654. }
  3655. func EditNewDrugWarehousingOne(warehouse models.DrugWarehouse, id int64, tx *gorm.DB) error {
  3656. err := tx.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
  3657. if err != nil {
  3658. tx.Rollback()
  3659. return err
  3660. }
  3661. return err
  3662. }
  3663. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3664. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3665. return list, err
  3666. }
  3667. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3668. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3669. return list, err
  3670. }
  3671. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3672. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3673. return list, err
  3674. }
  3675. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3676. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3677. // return list, err
  3678. // }
  3679. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3680. var dealer models.Dealer
  3681. var err error
  3682. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3683. if err == gorm.ErrRecordNotFound {
  3684. return nil, err
  3685. }
  3686. if err != nil {
  3687. return nil, err
  3688. }
  3689. return &dealer, nil
  3690. }
  3691. func CreateDealer(dealer *models.Dealer) error {
  3692. err := XTWriteDB().Create(&dealer).Error
  3693. return err
  3694. }
  3695. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3696. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3697. return err
  3698. }
  3699. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3700. dataconfig := models.DictDataconfig{}
  3701. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3702. return dataconfig, err
  3703. }
  3704. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3705. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3706. return dataconfig, err
  3707. }
  3708. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3709. var dataconfig models.DictDataconfig
  3710. var err error
  3711. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3712. if err == gorm.ErrRecordNotFound {
  3713. return nil, err
  3714. }
  3715. if err != nil {
  3716. return nil, err
  3717. }
  3718. return &dataconfig, nil
  3719. }
  3720. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3721. dataconfig := models.DictDataconfig{}
  3722. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3723. return dataconfig, err
  3724. }
  3725. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3726. err := XTWriteDB().Create(&dataconfig).Error
  3727. return err
  3728. }
  3729. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3730. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3731. return err
  3732. }
  3733. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3734. var goodType models.GoodsType
  3735. var err error
  3736. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3737. if err == gorm.ErrRecordNotFound {
  3738. return nil, err
  3739. }
  3740. if err != nil {
  3741. return nil, err
  3742. }
  3743. return &goodType, nil
  3744. }
  3745. func CreatedGoodType(goodsType *models.GoodsType) error {
  3746. err := XTWriteDB().Create(&goodsType).Error
  3747. return err
  3748. }
  3749. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3750. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3751. return goodsType, err
  3752. }
  3753. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3754. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3755. return goodsType, err
  3756. }
  3757. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3758. var department models.XtHisDepartment
  3759. var err error
  3760. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3761. if err == gorm.ErrRecordNotFound {
  3762. return nil, err
  3763. }
  3764. if err != nil {
  3765. return nil, err
  3766. }
  3767. return &department, nil
  3768. }
  3769. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3770. dataconfig := models.Dataconfig{}
  3771. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3772. return dataconfig, err
  3773. }
  3774. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3775. var dataconfig models.Dataconfig
  3776. var err error
  3777. 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
  3778. if err == gorm.ErrRecordNotFound {
  3779. return nil, err
  3780. }
  3781. if err != nil {
  3782. return nil, err
  3783. }
  3784. return &dataconfig, nil
  3785. }
  3786. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3787. dataconfig := models.Dataconfig{}
  3788. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3789. return dataconfig, err
  3790. }
  3791. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3792. err := XTWriteDB().Create(&dataconfig).Error
  3793. return err
  3794. }
  3795. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3796. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3797. return dataconfig, err
  3798. }
  3799. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3800. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3801. return list, err
  3802. }
  3803. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3804. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3805. if len(keywords) > 0 {
  3806. likekey := "%" + keywords + "%"
  3807. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3808. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3809. }).Find(&drug).Error
  3810. } else {
  3811. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3812. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3813. }).Find(&drug).Error
  3814. }
  3815. return
  3816. }
  3817. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3818. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3819. if len(keywords) > 0 {
  3820. likekey := "%" + keywords + "%"
  3821. 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 {
  3822. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3823. }).Find(&drug).Error
  3824. } else {
  3825. 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 {
  3826. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3827. }).Find(&drug).Error
  3828. }
  3829. return
  3830. }
  3831. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3832. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3833. if len(keywords) > 0 {
  3834. likekey := "%" + keywords + "%"
  3835. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3836. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3837. }).Find(&good).Error
  3838. } else {
  3839. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3840. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3841. }).Find(&good).Error
  3842. }
  3843. return
  3844. }
  3845. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3846. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3847. if len(keywords) > 0 {
  3848. likekey := "%" + keywords + "%"
  3849. 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 {
  3850. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3851. }).Find(&good).Error
  3852. } else {
  3853. 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 {
  3854. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3855. }).Find(&good).Error
  3856. }
  3857. return
  3858. }
  3859. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3860. db := writeDb.Begin()
  3861. err := db.Create(&detail).Error
  3862. if err != nil {
  3863. db.Rollback()
  3864. return err
  3865. }
  3866. db.Commit()
  3867. return err
  3868. }
  3869. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3870. var autoreduece models.BloodAutomaticReduceDetail
  3871. var err error
  3872. 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
  3873. if err == gorm.ErrRecordNotFound {
  3874. return nil, err
  3875. }
  3876. if err != nil {
  3877. return nil, err
  3878. }
  3879. return &autoreduece, nil
  3880. }
  3881. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3882. auto := models.BloodAutomaticReduceDetail{}
  3883. 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
  3884. return auto, err
  3885. }
  3886. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3887. detail := models.BloodAutomaticReduceDetail{}
  3888. 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
  3889. return err
  3890. }
  3891. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3892. 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
  3893. return stockflow, err
  3894. }
  3895. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3896. 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
  3897. 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
  3898. 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
  3899. return err
  3900. }
  3901. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3902. detail := models.BloodAutomaticReduceDetail{}
  3903. 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
  3904. info := models.WarehouseOutInfo{}
  3905. 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
  3906. prepare := models.DialysisBeforePrepare{}
  3907. 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
  3908. flow := models.VmStockFlow{}
  3909. 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
  3910. return err
  3911. }
  3912. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3913. detail := models.BloodAutomaticReduceDetail{}
  3914. 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
  3915. info := models.WarehouseOutInfo{}
  3916. 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
  3917. prepare := models.DialysisBeforePrepare{}
  3918. 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
  3919. flow := models.VmStockFlow{}
  3920. 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
  3921. return err
  3922. }
  3923. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  3924. detail := models.BloodAutomaticReduceDetail{}
  3925. 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
  3926. info := models.WarehouseOutInfo{}
  3927. 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
  3928. prepare := models.DialysisBeforePrepare{}
  3929. 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
  3930. flow := models.VmStockFlow{}
  3931. 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
  3932. return err
  3933. }
  3934. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  3935. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3936. //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
  3937. return err
  3938. }
  3939. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3940. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  3941. return info, err
  3942. }
  3943. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  3944. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  3945. return list, err
  3946. }
  3947. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  3948. 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
  3949. return err
  3950. }
  3951. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  3952. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  3953. return err
  3954. }
  3955. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  3956. var autoreduece models.BloodAutomaticReduceDetail
  3957. var err error
  3958. 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
  3959. if err == gorm.ErrRecordNotFound {
  3960. return nil, err
  3961. }
  3962. if err != nil {
  3963. return nil, err
  3964. }
  3965. return &autoreduece, nil
  3966. }
  3967. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  3968. detail := models.WarehouseOutInfoSeven{}
  3969. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3970. return detail, err
  3971. }
  3972. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  3973. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3974. return detail, err
  3975. }
  3976. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  3977. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  3978. return project, err
  3979. }
  3980. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  3981. project := models.HisPrescriptionProject{}
  3982. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  3983. return project, err
  3984. }
  3985. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  3986. 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
  3987. return auto, err
  3988. }
  3989. func ModefyWarehouseInfo(count int64, id int64) error {
  3990. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3991. return err
  3992. }
  3993. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  3994. 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
  3995. 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
  3996. 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
  3997. //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
  3998. return err
  3999. }
  4000. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  4001. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4002. return
  4003. }
  4004. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  4005. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4006. return
  4007. }
  4008. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  4009. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4010. return info, err
  4011. }
  4012. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4013. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4014. return info, err
  4015. }
  4016. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4017. 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
  4018. return info, err
  4019. }
  4020. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  4021. offset := (page - 1) * limit
  4022. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  4023. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4024. fmt.Println(table)
  4025. if startime > 0 {
  4026. db = db.Where("x.ctime>=?", startime)
  4027. }
  4028. if endtime > 0 {
  4029. db = db.Where("x.ctime<=?", endtime)
  4030. }
  4031. likeKey := "%" + keyword + "%"
  4032. if len(likeKey) > 0 {
  4033. db = db.Where("t.good_name like ?", likeKey)
  4034. }
  4035. if orgid > 0 {
  4036. db = db.Where("x.org_id = ?", orgid)
  4037. }
  4038. if good_type > 0 {
  4039. db = db.Where("t.good_type_id = ?", good_type)
  4040. }
  4041. 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
  4042. return info, total, err
  4043. }
  4044. 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) {
  4045. offset := (page - 1) * limit
  4046. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  4047. likeKey := "%" + keyword + "%"
  4048. if len(likeKey) > 0 {
  4049. db = db.Where("x.good_name like ?", likeKey)
  4050. }
  4051. if orgid > 0 {
  4052. db = db.Where("x.org_id = ?", orgid)
  4053. }
  4054. if good_type > 0 {
  4055. db = db.Where("x.good_type_id = ?", good_type)
  4056. }
  4057. if good_id > 0 {
  4058. db = db.Where("x.id = ?", good_id)
  4059. }
  4060. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4061. if startime > 0 {
  4062. db = db.Where("ctime>=?", startime)
  4063. }
  4064. if endtime > 0 {
  4065. db = db.Where("ctime<=?", endtime)
  4066. }
  4067. if good_id > 0 {
  4068. db = db.Where("good_id = ?", good_id)
  4069. }
  4070. if storehouse_id > 0 {
  4071. db = db.Where("storehouse_id = ?", storehouse_id)
  4072. }
  4073. return db.Where("status = 1 and is_check = 1")
  4074. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4075. if startime > 0 {
  4076. db = db.Where("ctime>=?", startime)
  4077. }
  4078. if endtime > 0 {
  4079. db = db.Where("ctime<=?", endtime)
  4080. }
  4081. if good_id > 0 {
  4082. db = db.Where("good_id = ?", good_id)
  4083. }
  4084. if storehouse_id > 0 {
  4085. db = db.Where("storehouse_id = ?", storehouse_id)
  4086. }
  4087. return db.Where("status = 1").Group("good_id,storehouse_id")
  4088. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4089. if startime > 0 {
  4090. db = db.Where("ctime>=?", startime)
  4091. }
  4092. if endtime > 0 {
  4093. db = db.Where("ctime<=?", endtime)
  4094. }
  4095. return db.Where("status = 1")
  4096. }).Find(&info).Error
  4097. return info, total, err
  4098. }
  4099. 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) {
  4100. offset := (page - 1) * limit
  4101. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  4102. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4103. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  4104. fmt.Println(table, tables)
  4105. if startime > 0 {
  4106. db = db.Where("x.ctime>=?", startime)
  4107. }
  4108. if endtime > 0 {
  4109. db = db.Where("x.ctime<=?", endtime)
  4110. }
  4111. if startfirsttime > 0 {
  4112. db = db.Where("x.expiry_date >=?", startfirsttime)
  4113. }
  4114. if endfirsttime > 0 {
  4115. db = db.Where("x.expiry_date <=?", endfirsttime)
  4116. }
  4117. if orgid > 0 {
  4118. db = db.Where("x.org_id = ?", orgid)
  4119. }
  4120. if good_id > 0 {
  4121. db = db.Where("x.good_id = ?", good_id)
  4122. }
  4123. 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
  4124. return info, total, err
  4125. }
  4126. 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) {
  4127. offset := (page - 1) * limit
  4128. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  4129. if startime > 0 {
  4130. db = db.Where("x.ctime>=?", startime)
  4131. }
  4132. if endtime > 0 {
  4133. db = db.Where("x.ctime<=?", endtime)
  4134. }
  4135. if orgid > 0 {
  4136. db = db.Where("x.org_id = ?", orgid)
  4137. }
  4138. if good_id > 0 {
  4139. db = db.Where("x.good_id = ?", good_id)
  4140. }
  4141. if is_sys == 2 {
  4142. db = db.Where("x.is_sys = 1")
  4143. }
  4144. if is_sys == 3 {
  4145. db = db.Where("x.is_sys = 0")
  4146. }
  4147. 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
  4148. return info, total, err
  4149. }
  4150. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4151. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4152. if startime > 0 {
  4153. db = db.Where("x.ctime >=?", startime)
  4154. }
  4155. if endtime > 0 {
  4156. db = db.Where("x.ctime<=?", endtime)
  4157. }
  4158. if orgid > 0 {
  4159. db = db.Where("x.org_id = ?", orgid)
  4160. }
  4161. 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
  4162. return info, err
  4163. }
  4164. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4165. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4166. if startime > 0 {
  4167. db = db.Where("x.ctime >=?", startime)
  4168. }
  4169. if endtime > 0 {
  4170. db = db.Where("x.ctime<=?", endtime)
  4171. }
  4172. if orgid > 0 {
  4173. db = db.Where("x.org_id = ?", orgid)
  4174. }
  4175. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  4176. return info, err
  4177. }
  4178. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  4179. if endtime > 0 {
  4180. 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
  4181. }
  4182. if endtime == 0 {
  4183. 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
  4184. }
  4185. return autoMatic, err
  4186. }
  4187. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  4188. out := models.WarehouseOut{}
  4189. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  4190. return out, err
  4191. }
  4192. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4193. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4194. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4195. fmt.Println(table)
  4196. if id > 0 {
  4197. db = db.Where("x.warehouse_out_id = ?", id)
  4198. }
  4199. if orgid > 0 {
  4200. db = db.Where("x.org_id = ?", orgid)
  4201. }
  4202. 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
  4203. return out, err
  4204. }
  4205. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  4206. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  4207. if len(id) > 0 {
  4208. db = db.Where("x.id in(?)", id)
  4209. }
  4210. if orgid > 0 {
  4211. db = db.Where("x.org_id= ?", orgid)
  4212. }
  4213. 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
  4214. return out, err
  4215. }
  4216. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4217. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4218. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4219. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  4220. fmt.Println(table, tableOne)
  4221. if len(id) > 0 {
  4222. db = db.Where("x.warehouse_out_id in (?)", id)
  4223. }
  4224. if orgid > 0 {
  4225. db = db.Where("x.org_id = ?", orgid)
  4226. }
  4227. err = db.Select("x.id,x.warehouse_out_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,x.manufacturer,t.social_security_directory_code,x.is_sys,x.sys_record_time,n.number,x.remark,x.license_number,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.warehouse_info_id,x.remark,x.patient_id,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit,t.packing_price,x.register_number,x.class_type").Joins("left join xt_warehouse_info as n on n.id=x.warehouse_info_id").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.warehouse_out_id,x.good_id").Order("x.ctime desc").Scan(&out).Error
  4228. return out, err
  4229. }
  4230. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4231. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4232. if len(id) > 0 {
  4233. db = db.Where("warehouse_out_id in (?)", id)
  4234. }
  4235. if orgid > 0 {
  4236. db = db.Where("user_org_id = ?", orgid)
  4237. }
  4238. err = db.Group("warehousing_id").Find(&list).Error
  4239. return list, err
  4240. }
  4241. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4242. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4243. if len(id) > 0 {
  4244. db = db.Where("warehouse_out_id in (?)", id)
  4245. }
  4246. if orgid > 0 {
  4247. db = db.Where("user_org_id = ?", orgid)
  4248. }
  4249. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4250. return list, err
  4251. }
  4252. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4253. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  4254. if len(id) > 0 {
  4255. db = db.Where("warehouse_out_id in (?)", id)
  4256. }
  4257. if orgid > 0 {
  4258. db = db.Where("user_org_id = ?", orgid)
  4259. }
  4260. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4261. return list, err
  4262. }
  4263. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  4264. db := XTReadDB().Model(&info)
  4265. db = db.Preload("GoodInfo", "status = 1")
  4266. 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
  4267. return info, err
  4268. }
  4269. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  4270. db := readDb.Model(&models.AutomaticReduceDetail{})
  4271. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  4272. db = db.Preload("GoodsType", "status = 1")
  4273. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  4274. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  4275. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  4276. db = db.Count(&total)
  4277. err = db.Find(&user).Error
  4278. return
  4279. }
  4280. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  4281. info := models.GoodInfo{}
  4282. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4283. return info, err
  4284. }
  4285. func GetNewGoodInformationByGoodId(good_id int64, tx *gorm.DB) (models.GoodInfo, error) {
  4286. info := models.GoodInfo{}
  4287. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4288. if err != gorm.ErrRecordNotFound {
  4289. if err != nil {
  4290. tx.Rollback()
  4291. return info, err
  4292. }
  4293. }
  4294. return info, err
  4295. }
  4296. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  4297. info := models.GoodInformationThirty{}
  4298. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4299. return info, err
  4300. }
  4301. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  4302. info := models.GoodInformationThirty{}
  4303. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4304. if err != gorm.ErrRecordNotFound {
  4305. if err != nil {
  4306. tx.Rollback()
  4307. return info, err
  4308. }
  4309. }
  4310. return info, err
  4311. }
  4312. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  4313. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  4314. return err
  4315. }
  4316. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  4317. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4318. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4319. fmt.Println(table)
  4320. if orgid > 0 {
  4321. db = db.Where("x.org_id = ?", orgid)
  4322. }
  4323. if len(orderid) > 0 {
  4324. db = db.Where("x.warehousing_id in(?)", orderid)
  4325. }
  4326. if startime > 0 {
  4327. db = db.Where("x.ctime>=?", startime)
  4328. }
  4329. if endtime > 0 {
  4330. db = db.Where("x.ctime<=?", endtime)
  4331. }
  4332. 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
  4333. return info, err
  4334. }
  4335. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  4336. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4337. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4338. fmt.Println(table)
  4339. if orgid > 0 {
  4340. db = db.Where("x.org_id = ?", orgid)
  4341. }
  4342. if len(orderid) > 0 {
  4343. db = db.Where("x.warehousing_id in(?)", orderid)
  4344. }
  4345. if startime > 0 {
  4346. db = db.Where("x.ctime>=?", startime)
  4347. }
  4348. if endtime > 0 {
  4349. db = db.Where("x.ctime<=?", endtime)
  4350. }
  4351. if storehouse_id > 0 {
  4352. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4353. }
  4354. 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
  4355. return info, err
  4356. }
  4357. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4358. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4359. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  4360. fmt.Println(table)
  4361. if startime > 0 {
  4362. db = db.Where("x.ctime>=?", startime)
  4363. }
  4364. if endtime > 0 {
  4365. db = db.Where("x.ctime<=?", endtime)
  4366. }
  4367. if orgid > 0 {
  4368. db = db.Where("x.org_id = ?", orgid)
  4369. }
  4370. 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
  4371. return info, err
  4372. }
  4373. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  4374. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  4375. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4376. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  4377. fmt.Println(table, tableOne)
  4378. if orgid > 0 {
  4379. db = db.Where("x.org_id = ?", orgid)
  4380. }
  4381. if orgid > 0 {
  4382. db = db.Where("t.org_id = ?", orgid)
  4383. }
  4384. if orgid > 0 {
  4385. db = db.Where("p.org_id = ?", orgid)
  4386. }
  4387. if len(ids) > 0 {
  4388. db = db.Where("x.warehouse_out_id in (?)", ids)
  4389. }
  4390. if startime > 0 {
  4391. db = db.Where("x.ctime>=?", startime)
  4392. }
  4393. if endtime > 0 {
  4394. db = db.Where("x.ctime<=?", endtime)
  4395. }
  4396. err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.number,x.license_number,x.warehouse_out_order_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,x.manufacturer,x.dealer,p.warehouse_out_time").Joins("left join xt_warehouse_out as p on p.id =x.warehouse_out_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&out).Error
  4397. return out, err
  4398. }
  4399. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  4400. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  4401. if len(ids) > 0 {
  4402. db = db.Where("x.id in(?)", ids)
  4403. }
  4404. if orgid > 0 {
  4405. db = db.Where("x.org_id = ?", orgid)
  4406. }
  4407. 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
  4408. return info, err
  4409. }
  4410. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4411. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  4412. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  4413. fmt.Println(table)
  4414. if len(ids) > 0 {
  4415. db = db.Where("x.warehouse_out_id in(?)", ids)
  4416. }
  4417. if orgid > 0 {
  4418. db = db.Where("x.org_id = ?", orgid)
  4419. }
  4420. 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
  4421. return info, err
  4422. }
  4423. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  4424. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  4425. likeKey := "%" + keyword + "%"
  4426. if len(keyword) > 0 {
  4427. db = db.Where("x.drug_name like ?", likeKey)
  4428. }
  4429. if orgid > 0 {
  4430. db = db.Where("x.org_id = ?", orgid)
  4431. }
  4432. err = db.Find(&basedrug).Error
  4433. return basedrug, err
  4434. }
  4435. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4436. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4437. if len(ids) > 0 {
  4438. db = db.Where("x.drug_id in(?)", ids)
  4439. }
  4440. if orgid > 0 {
  4441. db = db.Where("x.org_id = ?", orgid)
  4442. }
  4443. if startime > 0 {
  4444. db = db.Where("x.ctime>=?", startime)
  4445. }
  4446. if endtime > 0 {
  4447. db = db.Where("x.ctime<=?", endtime)
  4448. }
  4449. if storehouse_id > 0 {
  4450. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4451. }
  4452. err = db.Find(&info).Error
  4453. return info, err
  4454. }
  4455. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4456. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4457. if len(ids) > 0 {
  4458. db = db.Where("x.drug_id in(?)", ids)
  4459. }
  4460. if orgid > 0 {
  4461. db = db.Where("x.org_id = ?", orgid)
  4462. }
  4463. if startime > 0 {
  4464. db = db.Where("x.ctime>=?", startime)
  4465. }
  4466. if endtime > 0 {
  4467. db = db.Where("x.ctime<=?", endtime)
  4468. }
  4469. if storehouse_id > 0 {
  4470. db = db.Where("storehouse_id = ?", storehouse_id)
  4471. }
  4472. err = db.Find(&info).Error
  4473. return info, err
  4474. }
  4475. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4476. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4477. if drugid > 0 {
  4478. db = db.Where("x.drug_id = ?", drugid)
  4479. }
  4480. if orgid > 0 {
  4481. db = db.Where("x.org_id = ?", orgid)
  4482. }
  4483. if storehouse_id > 0 {
  4484. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4485. }
  4486. 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
  4487. return info, err
  4488. }
  4489. func GetDrugTotalCountTwoTwety(drugid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info models.VmDrugWarehouseInfo, err error) {
  4490. db := tx.Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4491. if drugid > 0 {
  4492. db = db.Where("x.drug_id = ?", drugid)
  4493. }
  4494. if orgid > 0 {
  4495. db = db.Where("x.org_id = ?", orgid)
  4496. }
  4497. if storehouse_id > 0 {
  4498. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4499. }
  4500. 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
  4501. if err != gorm.ErrRecordNotFound {
  4502. if err != nil {
  4503. tx.Rollback()
  4504. return info, err
  4505. }
  4506. }
  4507. return info, err
  4508. }
  4509. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4510. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4511. if drugid > 0 {
  4512. db = db.Where("x.drug_id = ?", drugid)
  4513. }
  4514. if orgid > 0 {
  4515. db = db.Where("x.org_id = ?", orgid)
  4516. }
  4517. if storehouse_id > 0 {
  4518. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4519. }
  4520. 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
  4521. return info, err
  4522. }
  4523. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4524. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4525. if id > 0 {
  4526. db = db.Where("x.id = ?", id)
  4527. }
  4528. if orgid > 0 {
  4529. db = db.Where("x.org_id = ?", orgid)
  4530. }
  4531. 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
  4532. return info, err
  4533. }
  4534. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4535. info := models.VmDrugWarehouseInfo{}
  4536. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4537. if id == 0 {
  4538. db = db.Where("id = ?", id)
  4539. }
  4540. if drugid > 0 {
  4541. db = db.Where("x.drug_id = ?", drugid)
  4542. }
  4543. if orgid > 0 {
  4544. db = db.Where("x.org_id = ?", orgid)
  4545. }
  4546. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4547. return info, err
  4548. }
  4549. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4550. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4551. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4552. fmt.Println(table)
  4553. if drugid > 0 {
  4554. db = db.Where("x.drug_id = ?", drugid)
  4555. }
  4556. if storehouse_id > 0 {
  4557. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4558. }
  4559. 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
  4560. return list, err
  4561. }
  4562. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4563. lib := models.BaseDrugLib{}
  4564. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4565. return lib, err
  4566. }
  4567. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4568. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4569. if goodid > 0 {
  4570. db = db.Where("x.good_id = ?", goodid)
  4571. }
  4572. if orgid > 0 {
  4573. db = db.Where("x.org_id = ?", orgid)
  4574. }
  4575. 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
  4576. return info, err
  4577. }
  4578. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4579. 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
  4580. return err
  4581. }
  4582. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4583. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4584. return info, err
  4585. }
  4586. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4587. //遍历循环入库数据
  4588. for i, item := range list {
  4589. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4590. if (item.WarehousingCount - item.StockCount) >= count {
  4591. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4592. info := models.WarehousingInfo{
  4593. GoodId: item.GoodId,
  4594. OrgId: item.OrgId,
  4595. WarehousingId: item.WarehousingId,
  4596. StockCount: item.StockCount + count,
  4597. }
  4598. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4599. fmt.Println("2323233232323232323232323232323", err2)
  4600. return err
  4601. }
  4602. if (item.WarehousingCount - item.StockCount) < count {
  4603. //如果该批次小于退库数量,则先出库该相差数量
  4604. var countOne = item.WarehousingCount - item.StockCount
  4605. infoOne := models.WarehousingInfo{
  4606. GoodId: item.GoodId,
  4607. OrgId: item.OrgId,
  4608. WarehousingId: item.WarehousingId,
  4609. StockCount: countOne + item.StockCount,
  4610. }
  4611. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4612. if err == nil {
  4613. //剩余的出库到第二个批次
  4614. var countTwo = count - countOne
  4615. infos := append(list, list[i+1])
  4616. var idArray []*models.WarehousingInfo
  4617. for _, it := range infos {
  4618. if it.WarehousingId != item.WarehousingId {
  4619. idArray = append(idArray, it)
  4620. }
  4621. }
  4622. //查询更新后的库存
  4623. UpdateCancelOut(idArray, countTwo)
  4624. return err
  4625. } else {
  4626. return err
  4627. }
  4628. }
  4629. }
  4630. return err
  4631. }
  4632. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4633. if len(idstr) > 0 {
  4634. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4635. } else {
  4636. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4637. }
  4638. return info, err
  4639. }
  4640. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4641. if len(idstr) > 0 {
  4642. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4643. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4644. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4645. }).Find(&info).Error
  4646. }
  4647. return info, err
  4648. }
  4649. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4650. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4651. if startime > 0 {
  4652. db = db.Where("x.ctime >=?", startime)
  4653. }
  4654. if endtime > 0 {
  4655. db = db.Where("x.ctime <= ?", endtime)
  4656. }
  4657. if orgid > 0 {
  4658. db = db.Where("x.org_id = ?", orgid)
  4659. }
  4660. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4661. return info, err
  4662. }
  4663. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4664. info := models.WarehouseOutInfo{}
  4665. var err error
  4666. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4667. if err == gorm.ErrRecordNotFound {
  4668. return nil, err
  4669. }
  4670. if err != nil {
  4671. return nil, err
  4672. }
  4673. return &info, nil
  4674. }
  4675. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4676. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4677. if goodid > 0 {
  4678. db = db.Where("x.good_id = ?", goodid)
  4679. }
  4680. if orgid > 0 {
  4681. db = db.Where("x.org_id = ?", orgid)
  4682. }
  4683. if id > 0 {
  4684. db = db.Where("x.warehouse_info_id = ?", id)
  4685. }
  4686. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4687. return info, err
  4688. }
  4689. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4690. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4691. if goodid > 0 {
  4692. db = db.Where("x.good_id = ?", goodid)
  4693. }
  4694. if orgid > 0 {
  4695. db = db.Where("x.user_org_id = ?", orgid)
  4696. }
  4697. if id > 0 {
  4698. db = db.Where("x.warehousing_id = ?", id)
  4699. }
  4700. if storehouse_id > 0 {
  4701. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4702. }
  4703. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4704. return info, err
  4705. }
  4706. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4707. stock := models.CancelStock{}
  4708. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4709. return stock, err
  4710. }
  4711. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4712. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4713. return info, err
  4714. }
  4715. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4716. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4717. return info, err
  4718. }
  4719. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4720. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4721. return stock, err
  4722. }
  4723. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4724. 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)")
  4725. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4726. fmt.Println(table)
  4727. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4728. fmt.Println(tableone)
  4729. if id > 0 {
  4730. db = db.Where("x.warehouse_out_id = ?", id)
  4731. }
  4732. if goodid > 0 {
  4733. db = db.Where("x.good_id = ?", goodid)
  4734. }
  4735. 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
  4736. return stock, err
  4737. }
  4738. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4739. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4740. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4741. fmt.Println(table)
  4742. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4743. fmt.Println(tableone)
  4744. if id > 0 {
  4745. db = db.Where("x.warehouse_out_id = ?", id)
  4746. }
  4747. if goodid > 0 {
  4748. db = db.Where("x.good_id = ?", goodid)
  4749. }
  4750. 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
  4751. return stock, err
  4752. }
  4753. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4754. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4755. return info, err
  4756. }
  4757. func ExportDrugList(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseInfo, err error) {
  4758. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1 AND x.org_id=?", org_id)
  4759. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4760. tableOne := XTReadDB().Table("xt_drug_warehouse as p").Where("p.status=1 and p.org_id=?", org_id)
  4761. fmt.Println("table", table)
  4762. fmt.Println("tableOne", tableOne)
  4763. if len(warehouse_out_id) > 0 {
  4764. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4765. }
  4766. 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
  4767. return out, err
  4768. }
  4769. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4770. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4771. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4772. fmt.Println("table", table)
  4773. if len(warehouse_out_id) > 0 {
  4774. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4775. }
  4776. 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
  4777. return out, err
  4778. }
  4779. func GetExportOutOrderDrugListOne(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseOutInfo, err error) {
  4780. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1 and x.user_org_id =?", org_id)
  4781. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4782. fmt.Println("table", table)
  4783. if len(warehouse_out_id) > 0 {
  4784. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4785. }
  4786. 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
  4787. return out, err
  4788. }
  4789. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4790. db := XTReadDB().Model(&info).Where("status = 1")
  4791. if len(warehouse_out_id) > 0 {
  4792. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4793. }
  4794. if orgid > 0 {
  4795. db = db.Where("org_id = ?", orgid)
  4796. }
  4797. err = db.Find(&info).Error
  4798. return info, err
  4799. }
  4800. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4801. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4802. return out, err
  4803. }
  4804. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4805. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4806. 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)
  4807. }).Find(&out).Error
  4808. return out, err
  4809. }
  4810. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4811. info := models.WarehouseOutInfo{}
  4812. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4813. return info, err
  4814. }
  4815. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4816. info := models.WarehousingInfo{}
  4817. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4818. if goodid > 0 {
  4819. db = db.Where("x.good_id = ?", goodid)
  4820. }
  4821. if storehouse_id > 0 {
  4822. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4823. }
  4824. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4825. return info, err
  4826. }
  4827. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4828. info := models.WarehousingInfo{}
  4829. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4830. return info, err
  4831. }
  4832. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4833. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4834. return err
  4835. }
  4836. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4837. if storehouse_id == 0 {
  4838. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4839. }
  4840. if storehouse_id > 0 {
  4841. 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
  4842. }
  4843. return info, err
  4844. }
  4845. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4846. if len(warehousingInfo) > 0 {
  4847. utx := writeDb.Begin()
  4848. if len(warehousingInfo) > 0 {
  4849. 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 "
  4850. insertParams := make([]string, 0)
  4851. insertData := make([]interface{}, 0)
  4852. for _, info := range warehousingInfo {
  4853. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4854. insertData = append(insertData, info.WarehousingId)
  4855. insertData = append(insertData, info.GoodId)
  4856. insertData = append(insertData, info.Number)
  4857. insertData = append(insertData, info.LicenseNumber)
  4858. insertData = append(insertData, info.Count)
  4859. insertData = append(insertData, info.UserOrgId)
  4860. insertData = append(insertData, info.PatientId)
  4861. insertData = append(insertData, info.SystemTime)
  4862. insertData = append(insertData, info.ConsumableType)
  4863. insertData = append(insertData, info.IsSys)
  4864. insertData = append(insertData, info.WarehousingOrder)
  4865. insertData = append(insertData, info.WarehouseOutId)
  4866. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4867. insertData = append(insertData, info.IsEdit)
  4868. insertData = append(insertData, info.CancelStockId)
  4869. insertData = append(insertData, info.CancelOrderNumber)
  4870. insertData = append(insertData, info.Manufacturer)
  4871. insertData = append(insertData, info.Dealer)
  4872. insertData = append(insertData, info.Creator)
  4873. insertData = append(insertData, info.UpdateCreator)
  4874. insertData = append(insertData, info.Status)
  4875. insertData = append(insertData, info.Ctime)
  4876. insertData = append(insertData, info.Mtime)
  4877. insertData = append(insertData, info.Price)
  4878. insertData = append(insertData, info.WarehousingDetailId)
  4879. insertData = append(insertData, info.WarehouseOutDetailId)
  4880. insertData = append(insertData, info.CancelOutDetailId)
  4881. insertData = append(insertData, info.ExpireDate)
  4882. insertData = append(insertData, info.ProductDate)
  4883. }
  4884. thisSQL += strings.Join(insertParams, ", ")
  4885. err = utx.Exec(thisSQL, insertData...).Error
  4886. if err != nil {
  4887. utx.Rollback()
  4888. return
  4889. }
  4890. }
  4891. utx.Commit()
  4892. }
  4893. return
  4894. }
  4895. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4896. utx := XTWriteDB().Begin()
  4897. err := utx.Create(&flow).Error
  4898. if err != nil {
  4899. utx.Rollback()
  4900. return err
  4901. }
  4902. utx.Commit()
  4903. return err
  4904. }
  4905. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4906. err := tx.Create(&flow).Error
  4907. if err != nil {
  4908. tx.Rollback()
  4909. return err
  4910. }
  4911. return err
  4912. }
  4913. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4914. err := XTWriteDB().Create(&flow).Error
  4915. return err
  4916. }
  4917. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4918. err := XTWriteDB().Save(&flow).Error
  4919. return err
  4920. }
  4921. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4922. 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
  4923. return err
  4924. }
  4925. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  4926. 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
  4927. return err
  4928. }
  4929. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  4930. stock := models.VmStockFlow{}
  4931. var err error
  4932. 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
  4933. if err == gorm.ErrRecordNotFound {
  4934. return nil, err
  4935. }
  4936. if err != nil {
  4937. return nil, err
  4938. }
  4939. return &stock, nil
  4940. }
  4941. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4942. 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
  4943. return flow, nil
  4944. }
  4945. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4946. 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
  4947. return flow, nil
  4948. }
  4949. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  4950. 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
  4951. if err != gorm.ErrRecordNotFound {
  4952. if err != nil {
  4953. tx.Rollback()
  4954. return flow, nil
  4955. }
  4956. }
  4957. return flow, nil
  4958. }
  4959. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4960. 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
  4961. return flow, nil
  4962. }
  4963. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  4964. 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
  4965. if err != gorm.ErrRecordNotFound {
  4966. if err != nil {
  4967. tx.Rollback()
  4968. return flow, nil
  4969. }
  4970. }
  4971. return flow, nil
  4972. }
  4973. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  4974. 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
  4975. return flow, nil
  4976. }
  4977. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  4978. 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
  4979. if err != gorm.ErrRecordNotFound {
  4980. if err != nil {
  4981. tx.Rollback()
  4982. return flow, err
  4983. }
  4984. }
  4985. return flow, err
  4986. }
  4987. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  4988. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  4989. return flow, nil
  4990. }
  4991. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  4992. 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
  4993. return flow, nil
  4994. }
  4995. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  4996. 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
  4997. return flow, nil
  4998. }
  4999. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5000. 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
  5001. if err != gorm.ErrRecordNotFound {
  5002. if err != nil {
  5003. tx.Rollback()
  5004. return flow, nil
  5005. }
  5006. }
  5007. return flow, nil
  5008. }
  5009. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5010. 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
  5011. return flow, nil
  5012. }
  5013. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  5014. info := models.WarehouseOutInfo{}
  5015. var err error
  5016. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5017. if err == gorm.ErrRecordNotFound {
  5018. return nil, err
  5019. }
  5020. if err != nil {
  5021. return nil, err
  5022. }
  5023. return &info, nil
  5024. }
  5025. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  5026. info := models.WarehouseOutInfo{}
  5027. var err error
  5028. 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
  5029. if err == gorm.ErrRecordNotFound {
  5030. return nil, err
  5031. }
  5032. if err != nil {
  5033. return nil, err
  5034. }
  5035. return &info, nil
  5036. }
  5037. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  5038. info := models.WarehouseOutInfo{}
  5039. 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
  5040. return info, err
  5041. }
  5042. 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) {
  5043. offset := (page - 1) * limit
  5044. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  5045. if consumable_type > 0 {
  5046. //入库
  5047. if consumable_type == 1 {
  5048. db = db.Where("consumable_type = ?", consumable_type)
  5049. }
  5050. //2 手动出库
  5051. if consumable_type == 2 {
  5052. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  5053. }
  5054. //3.自动出库
  5055. if consumable_type == 3 {
  5056. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  5057. }
  5058. //4.手动退库
  5059. if consumable_type == 4 {
  5060. db = db.Where(" consumable_type = ?", consumable_type)
  5061. }
  5062. //自动退库
  5063. if consumable_type == 7 {
  5064. db = db.Where(" consumable_type = ?", consumable_type)
  5065. }
  5066. if consumable_type == 10 {
  5067. db = db.Where(" consumable_type = ?", consumable_type)
  5068. }
  5069. if consumable_type == 11 {
  5070. db = db.Where(" consumable_type = ?", consumable_type)
  5071. }
  5072. if consumable_type == 12 {
  5073. db = db.Where(" consumable_type = ?", consumable_type)
  5074. }
  5075. if consumable_type == 13 {
  5076. db = db.Where(" consumable_type = ?", consumable_type)
  5077. }
  5078. if consumable_type == 15 {
  5079. db = db.Where(" consumable_type = ?", consumable_type)
  5080. }
  5081. }
  5082. if orgId > 0 {
  5083. db = db.Where("user_org_id = ?", orgId)
  5084. }
  5085. if startTime > 0 {
  5086. db = db.Where("ctime >= ?", startTime)
  5087. }
  5088. if endTime > 0 {
  5089. db = db.Where("ctime<=?", endTime)
  5090. }
  5091. if good_id > 0 {
  5092. db = db.Where("good_id = ?", good_id)
  5093. }
  5094. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  5095. return flow, total, err
  5096. }
  5097. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  5098. detail := models.AutomaticReduceDetail{}
  5099. 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
  5100. return detail, err
  5101. }
  5102. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  5103. detail := models.WarehouseOutInfo{}
  5104. 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
  5105. return detail, err
  5106. }
  5107. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  5108. outInfo := models.WarehouseOutInfo{}
  5109. 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
  5110. return err
  5111. }
  5112. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  5113. outInfo := models.WarehouseOutInfo{}
  5114. 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
  5115. if err != nil {
  5116. tx.Rollback()
  5117. return err
  5118. }
  5119. return err
  5120. }
  5121. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  5122. outInfo := models.WarehouseOutInfo{}
  5123. 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
  5124. return err
  5125. }
  5126. 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 {
  5127. outInfo := models.WarehouseOutInfo{}
  5128. 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
  5129. if err != nil {
  5130. tx.Rollback()
  5131. return err
  5132. }
  5133. return err
  5134. }
  5135. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  5136. info := models.WarehouseOutInfo{}
  5137. 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
  5138. return info, err
  5139. }
  5140. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  5141. info := models.WarehousingInfo{}
  5142. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5143. return info, err
  5144. }
  5145. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  5146. info := models.WarehousingInfo{}
  5147. db := XTReadDB().Model(&info)
  5148. if good_id > 0 {
  5149. db = db.Where("good_id = ? and status = 1", good_id)
  5150. }
  5151. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  5152. return info, err
  5153. }
  5154. 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) {
  5155. offset := (page - 1) * limit
  5156. db := XTReadDB().Model(drug_flow).Where("status = 1")
  5157. if drugid > 0 {
  5158. db = db.Where("drug_id = ?", drugid)
  5159. }
  5160. if orgid > 0 {
  5161. db = db.Where("user_org_id = ?", orgid)
  5162. }
  5163. if startTime > 0 {
  5164. db = db.Where("ctime >=? ", startTime)
  5165. }
  5166. if endTime > 0 {
  5167. db = db.Where("ctime<=?", endTime)
  5168. }
  5169. if stock_type > 0 {
  5170. //手动入库
  5171. if stock_type == 1 {
  5172. db = db.Where("consumable_type = 1")
  5173. }
  5174. //手动出库
  5175. if stock_type == 2 {
  5176. db = db.Where("consumable_type = 2 and is_sys = 0")
  5177. }
  5178. //自动出库
  5179. if stock_type == 3 {
  5180. db = db.Where("consumable_type = 3 and is_sys = 1")
  5181. }
  5182. if stock_type == 4 {
  5183. db = db.Where("consumable_type = 4")
  5184. }
  5185. if stock_type == 10 {
  5186. db = db.Where("consumable_type = 10")
  5187. }
  5188. if stock_type == 11 {
  5189. db = db.Where("consumable_type = 11")
  5190. }
  5191. if stock_type == 12 {
  5192. db = db.Where("consumable_type = 12")
  5193. }
  5194. if stock_type == 13 {
  5195. db = db.Where("consumable_type = 13")
  5196. }
  5197. if stock_type == 15 {
  5198. db = db.Where("consumable_type = 15")
  5199. }
  5200. }
  5201. 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
  5202. return drug_flow, total, err
  5203. }
  5204. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  5205. outInfo := models.DrugWarehouseOutInfo{}
  5206. var err error
  5207. 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
  5208. if err == gorm.ErrRecordNotFound {
  5209. return nil, err
  5210. }
  5211. if err != nil {
  5212. return nil, err
  5213. }
  5214. return &outInfo, nil
  5215. }
  5216. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  5217. 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
  5218. return outInfo, err
  5219. }
  5220. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  5221. outInfo := models.DrugWarehouseOutInfo{}
  5222. 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
  5223. return err
  5224. }
  5225. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  5226. db := XTReadDB().Model(&cancle).Where("status = 1")
  5227. if startime > 0 {
  5228. db = db.Where("ctime >=?", startime)
  5229. }
  5230. if endtime > 0 {
  5231. db = db.Where("ctime <=?", endtime)
  5232. }
  5233. if len(ids) > 0 {
  5234. db = db.Where("cancel_stock_id in(?)", ids)
  5235. }
  5236. if orgid > 0 {
  5237. db = db.Where("org_id = ?", orgid)
  5238. }
  5239. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  5240. err = db.Find(&cancle).Error
  5241. return cancle, err
  5242. }
  5243. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  5244. db := XTReadDB().Model(&info)
  5245. if id > 0 {
  5246. db = db.Where("cancel_stock_id = ? and status = 1", id)
  5247. }
  5248. db = db.Preload("BaseDrugLib")
  5249. err = db.Find(&info).Error
  5250. return info, err
  5251. }
  5252. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  5253. info := models.XtDrugWarehouseInfo{}
  5254. db := XTReadDB().Model(&info).Where("status = 1")
  5255. if id > 0 {
  5256. db = db.Where("id = ?", id)
  5257. }
  5258. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5259. return info, err
  5260. }
  5261. func GetDrugWarehouseInfoOne(id int64) (models.XtDrugWarehouseInfo, error) {
  5262. info := models.XtDrugWarehouseInfo{}
  5263. err := XTReadDB().Where("status = 1 and id=?", id).Find(&info).Error
  5264. return info, err
  5265. }
  5266. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  5267. // info := models.XtDrugWarehouseInfo{}
  5268. // db := tx.Model(&info).Where("status = 1")
  5269. // if id > 0 {
  5270. // db = db.Where("id = ?", id)
  5271. // }
  5272. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5273. // return info, err
  5274. //}
  5275. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5276. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  5277. return info, err
  5278. }
  5279. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  5280. 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
  5281. return info, err
  5282. }
  5283. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  5284. info := models.HisDoctorAdviceInfo{}
  5285. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  5286. return info, err
  5287. }
  5288. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  5289. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5290. return info, err
  5291. }
  5292. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  5293. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5294. if err != gorm.ErrRecordNotFound {
  5295. if err != nil {
  5296. tx.Rollback()
  5297. return info, err
  5298. }
  5299. }
  5300. return info, err
  5301. }
  5302. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  5303. adviceInfo := models.HisDoctorAdviceInfo{}
  5304. 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
  5305. if err != gorm.ErrRecordNotFound {
  5306. if err != nil {
  5307. tx.Rollback()
  5308. return adviceInfo, err
  5309. }
  5310. }
  5311. return adviceInfo, err
  5312. }
  5313. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5314. info := models.HisDoctorAdviceInfo{}
  5315. 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
  5316. return info, err
  5317. }
  5318. func GetHisAdviceListByDrugIdNight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5319. info := models.HisDoctorAdviceInfo{}
  5320. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1 and id =?", drugid, patient_id, advice_date, advice_id).Find(&info).Error
  5321. return info, err
  5322. }
  5323. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  5324. detail := models.DrugAutomaticReduceDetail{}
  5325. 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
  5326. return detail, err
  5327. }
  5328. func GetWarehouseOutListExport(drug_id int64, patient_id int64, record_date int64, advice_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  5329. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and advice_id =? and status =1", drug_id, patient_id, record_date, advice_id).Find(&out).Error
  5330. return out, err
  5331. }
  5332. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  5333. 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
  5334. return out, err
  5335. }
  5336. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  5337. 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
  5338. if err != gorm.ErrRecordNotFound {
  5339. if err != nil {
  5340. tx.Rollback()
  5341. return out, err
  5342. }
  5343. }
  5344. return out, err
  5345. }
  5346. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  5347. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5348. return err
  5349. }
  5350. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  5351. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5352. return err
  5353. }
  5354. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  5355. 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
  5356. 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
  5357. 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
  5358. return err
  5359. }
  5360. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5361. 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
  5362. 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
  5363. return err
  5364. }
  5365. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5366. 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
  5367. 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
  5368. return err
  5369. }
  5370. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  5371. info := models.XtDrugWarehouseInfo{}
  5372. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  5373. return info, err
  5374. }
  5375. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  5376. info := models.XtDrugWarehouseInfo{}
  5377. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5378. return info, err
  5379. }
  5380. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  5381. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5382. return err
  5383. }
  5384. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5385. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5386. // return err
  5387. //}
  5388. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  5389. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  5390. //return err
  5391. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5392. return err
  5393. }
  5394. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5395. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5396. // return err
  5397. //}
  5398. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  5399. info := models.DrugCancelStockInfo{}
  5400. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5401. return info, err
  5402. }
  5403. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  5404. info := models.DrugCancelStockInfo{}
  5405. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  5406. return info, err
  5407. }
  5408. func UpdatedDrugInfo(id int64, count int64) (err error) {
  5409. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5410. return err
  5411. }
  5412. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  5413. 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
  5414. return err
  5415. }
  5416. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  5417. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5418. return err
  5419. }
  5420. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5421. 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
  5422. return err
  5423. }
  5424. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  5425. 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
  5426. return err
  5427. }
  5428. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5429. 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
  5430. return err
  5431. }
  5432. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  5433. 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
  5434. return err
  5435. }
  5436. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  5437. info := models.WarehousingInfo{}
  5438. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5439. return info, err
  5440. }
  5441. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5442. info := models.WarehousingInfo{}
  5443. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  5444. return info, err
  5445. }
  5446. func GetLastWarehouseInfoTwo(goodid int64, storehouse_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  5447. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=? and org_id = ? and stock_count > 0", goodid, storehouse_id, user_org_id).Order("ctime").First(&info).Error
  5448. return info, err
  5449. }
  5450. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5451. info := models.WarehousingInfo{}
  5452. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  5453. return info, err
  5454. }
  5455. func CreateWarehouseOut(out models.WarehouseOut) error {
  5456. err := XTWriteDB().Create(&out).Error
  5457. return err
  5458. }
  5459. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  5460. out := models.WarehouseOut{}
  5461. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  5462. return out, err
  5463. }
  5464. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  5465. err := XTWriteDB().Create(&info).Error
  5466. return err
  5467. }
  5468. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  5469. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5470. return err
  5471. }
  5472. func UpdateWareInfoById(id int64, count int64) error {
  5473. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5474. return err
  5475. }
  5476. func UpdateWareInfoByIdSix(id int64, count int64) error {
  5477. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  5478. return err
  5479. }
  5480. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  5481. info := models.CancelStockInfo{}
  5482. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5483. return info, err
  5484. }
  5485. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  5486. info := models.CancelStockInfo{}
  5487. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5488. return info, err
  5489. }
  5490. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5491. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5492. return info, err
  5493. }
  5494. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5495. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5496. return info, err
  5497. }
  5498. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5499. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5500. return err
  5501. }
  5502. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5503. drugFlow := models.DrugFlow{}
  5504. 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
  5505. return err
  5506. }
  5507. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5508. drugFlow := models.DrugFlow{}
  5509. 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
  5510. return err
  5511. }
  5512. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5513. stock := models.DrugCancelStock{}
  5514. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5515. return stock, err
  5516. }
  5517. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5518. // stock := models.DrugCancelStock{}
  5519. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5520. // return stock, err
  5521. //}
  5522. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5523. db := XTReadDB().Model(&info)
  5524. if warehouse_info_id > 0 {
  5525. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5526. }
  5527. if drug_id > 0 {
  5528. db = db.Where("drug_id = ? and status =1", drug_id)
  5529. }
  5530. err = db.Find(&info).Error
  5531. return info, err
  5532. }
  5533. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5534. // db := tx.Model(&info)
  5535. // if warehouse_info_id > 0 {
  5536. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5537. // }
  5538. // if drug_id > 0 {
  5539. // db = db.Where("drug_id = ? and status =1", drug_id)
  5540. // }
  5541. // err = db.Find(&info).Error
  5542. // return info, err
  5543. //}
  5544. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5545. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5546. return info, err
  5547. }
  5548. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5549. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5550. // return info, err
  5551. //}
  5552. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5553. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5554. return config, err
  5555. }
  5556. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5557. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5558. return config, err
  5559. }
  5560. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5561. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5562. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5563. fmt.Println(table)
  5564. likeKey := "%" + keyword + "%"
  5565. if orgid > 0 {
  5566. db = db.Where("x.org_id = ?", orgid)
  5567. }
  5568. if storehouse_id > 0 {
  5569. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5570. }
  5571. 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
  5572. return info, err
  5573. }
  5574. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5575. err := XTWriteDB().Create(&adjustPrice).Error
  5576. return err
  5577. }
  5578. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5579. likeKey := "%" + keyword + "%"
  5580. offset := (page - 1) * limit
  5581. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5582. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5583. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5584. fmt.Println(table, tab)
  5585. if startime > 0 {
  5586. db = db.Where("x.start_time>=?", startime)
  5587. }
  5588. if endtime > 0 {
  5589. db = db.Where("x.start_time<=?", endtime)
  5590. }
  5591. if len(keyword) > 0 {
  5592. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5593. }
  5594. if orgid > 0 {
  5595. db = db.Where("x.user_org_id = ?", orgid)
  5596. }
  5597. 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
  5598. return list, total, err
  5599. }
  5600. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5601. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5602. return err
  5603. }
  5604. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5605. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5606. return err
  5607. }
  5608. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5609. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5610. return info, err
  5611. }
  5612. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5613. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5614. return adjust, err
  5615. }
  5616. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5617. err := XTWriteDB().Create(&price).Error
  5618. return err
  5619. }
  5620. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5621. likeKey := "%" + keyword + "%"
  5622. offset := (page - 1) * limit
  5623. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5624. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5625. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5626. fmt.Println(table, tab)
  5627. if startime > 0 {
  5628. db = db.Where("x.ctime>=?", startime)
  5629. }
  5630. if endtime > 0 {
  5631. db = db.Where("x.ctime<=?", endtime)
  5632. }
  5633. if len(keyword) > 0 {
  5634. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5635. }
  5636. if orgid > 0 {
  5637. db = db.Where("x.user_org_id = ?", orgid)
  5638. }
  5639. 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
  5640. return adjust, total, err
  5641. }
  5642. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5643. likeKey := "%" + keyword + "%"
  5644. offset := (page - 1) * limit
  5645. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5646. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5647. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5648. fmt.Println(table, tab)
  5649. if len(keyword) > 0 {
  5650. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5651. }
  5652. if orgid > 0 {
  5653. db = db.Where("x.user_org_id = ?", orgid)
  5654. }
  5655. if startime > 0 {
  5656. db = db.Where("x.ctime >=?", startime)
  5657. }
  5658. if endtime > 0 {
  5659. db = db.Where("x.ctime <=?", endtime)
  5660. }
  5661. if storehouse_id > 0 {
  5662. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5663. }
  5664. 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
  5665. return adjust, total, err
  5666. }
  5667. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5668. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5669. if inventory_type == 0 {
  5670. db = db.Where("x.inventory_type = 4")
  5671. }
  5672. if inventory_type > 0 {
  5673. db = db.Where("x.inventory_type = ?", inventory_type)
  5674. }
  5675. if len(keyword) > 0 {
  5676. likeKey := "%" + keyword + "%"
  5677. db = db.Where("x.warehousing_order like ?", likeKey)
  5678. }
  5679. if orgid > 0 {
  5680. db = db.Where("x.user_org_id = ?", orgid)
  5681. }
  5682. 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
  5683. return adjust, err
  5684. }
  5685. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5686. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5687. if orgid > 0 {
  5688. db = db.Where("x.user_org_id = ?", orgid)
  5689. }
  5690. 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
  5691. return adjust, err
  5692. }
  5693. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5694. 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
  5695. return err
  5696. }
  5697. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5698. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5699. return report, err
  5700. }
  5701. func CreateInentory(inventory models.XtStockInventory) error {
  5702. err = XTWriteDB().Create(&inventory).Error
  5703. return err
  5704. }
  5705. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5706. likeKey := "%" + keyword + "%"
  5707. offset := (page - 1) * limit
  5708. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5709. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5710. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5711. fmt.Println(table, tab)
  5712. if startime > 0 {
  5713. db = db.Where("x.ctime>=?", startime)
  5714. }
  5715. if endtime > 0 {
  5716. db = db.Where("x.ctime<=?", endtime)
  5717. }
  5718. if len(keyword) > 0 {
  5719. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5720. }
  5721. if orgid > 0 {
  5722. db = db.Where("x.user_org_id = ?", orgid)
  5723. }
  5724. if inventory_status > 0 {
  5725. db = db.Where("checker_status = ?", inventory_status)
  5726. }
  5727. 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
  5728. return adjust, total, err
  5729. }
  5730. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5731. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5732. return err
  5733. }
  5734. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5735. info := models.XtStockAdjustPrice{}
  5736. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5737. return info, err
  5738. }
  5739. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5740. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5741. return list, err
  5742. }
  5743. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5744. info := models.WarehousingInfo{}
  5745. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5746. return info, err
  5747. }
  5748. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5749. info := models.WarehousingInfo{}
  5750. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5751. //return info, err
  5752. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5753. if id > 0 {
  5754. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5755. }
  5756. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5757. return info, err
  5758. }
  5759. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5760. info := models.WarehousingInfo{}
  5761. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5762. if id > 0 {
  5763. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5764. }
  5765. if storehouse_id > 0 {
  5766. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5767. }
  5768. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5769. return info, err
  5770. }
  5771. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5772. 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
  5773. return err
  5774. }
  5775. func DeleteStockPrice(id int64) error {
  5776. info := models.XtStockAdjustPrice{}
  5777. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5778. return err
  5779. }
  5780. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5781. report := models.XtStockReportPrice{}
  5782. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5783. return report, err
  5784. }
  5785. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5786. 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
  5787. return err
  5788. }
  5789. func DeleteDamage(id int64) error {
  5790. adjust := models.XtStockReportPrice{}
  5791. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5792. return err
  5793. }
  5794. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5795. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5796. return adjust, err
  5797. }
  5798. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5799. inventory := models.XtStockInventory{}
  5800. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5801. return inventory, err
  5802. }
  5803. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5804. 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
  5805. return err
  5806. }
  5807. func DeleteInventory(id int64) error {
  5808. inventory := models.XtStockInventory{}
  5809. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5810. return err
  5811. }
  5812. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5813. likeKey := "%" + keyword + "%"
  5814. offset := (page - 1) * limit
  5815. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5816. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5817. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5818. fmt.Println(table, tab)
  5819. if len(keyword) > 0 {
  5820. db = db.Where("x.good_name like ?", likeKey)
  5821. }
  5822. if orgid > 0 {
  5823. db = db.Where("x.user_org_id = ?", orgid)
  5824. }
  5825. if storehouse_id > 0 {
  5826. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5827. }
  5828. 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
  5829. return list, total, err
  5830. }
  5831. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5832. likeKey := "%" + keyword + "%"
  5833. offset := (page - 1) * limit
  5834. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5835. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5836. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5837. fmt.Println(table, tab)
  5838. if len(keyword) > 0 {
  5839. db = db.Where("x.good_name like ?", likeKey)
  5840. }
  5841. if orgid > 0 {
  5842. db = db.Where("x.user_org_id = ?", orgid)
  5843. }
  5844. if storehouse_id > 0 {
  5845. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5846. }
  5847. if len(ids) > 0 {
  5848. db = db.Where("x.id in(?)", ids)
  5849. }
  5850. 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
  5851. return list, total, err
  5852. }
  5853. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5854. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5855. return list, err
  5856. }
  5857. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5858. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5859. return list, err
  5860. }
  5861. func DeleteDrugPrice(id int64) error {
  5862. info := models.XtDrugAdjustPrice{}
  5863. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5864. return err
  5865. }
  5866. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5867. info := models.WarehousingInfo{}
  5868. db := XTReadDB().Model(&info).Where("status = 1")
  5869. if goodid > 0 {
  5870. db = db.Where("good_id = ?", goodid)
  5871. }
  5872. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5873. return info, err
  5874. }
  5875. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5876. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5877. return err
  5878. }
  5879. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5880. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5881. return err
  5882. }
  5883. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5884. err := XTWriteDB().Create(&record).Error
  5885. return err
  5886. }
  5887. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5888. info := models.WarehousingInfo{}
  5889. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5890. //return info, err
  5891. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5892. if id > 0 {
  5893. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5894. }
  5895. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5896. return info, err
  5897. }
  5898. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5899. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5900. return info, err
  5901. }
  5902. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  5903. info := models.DrugWarehouseInfo{}
  5904. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5905. return info, err
  5906. }
  5907. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  5908. 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
  5909. return err
  5910. }
  5911. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  5912. 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
  5913. return err
  5914. }
  5915. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  5916. 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
  5917. return info, err
  5918. }
  5919. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  5920. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5921. return err
  5922. }
  5923. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  5924. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  5925. return err
  5926. }
  5927. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  5928. db := XTReadDB().Model(&list).Where(" status = 1")
  5929. if good_id > 0 {
  5930. db = db.Where("good_id = ?", good_id)
  5931. }
  5932. if len(keyword) > 0 {
  5933. likeKey := "%" + keyword + "%"
  5934. db = db.Where("warehousing_order like ?", likeKey)
  5935. }
  5936. if inventory_type > 0 {
  5937. db = db.Where("inventory_type = ?", inventory_type)
  5938. }
  5939. if inventory_type == 0 {
  5940. db = db.Where("inventory_type = 4")
  5941. }
  5942. err = db.Find(&list).Error
  5943. return list, err
  5944. }
  5945. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  5946. flow := models.DrugFlow{}
  5947. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  5948. if err == gorm.ErrRecordNotFound {
  5949. return nil, err
  5950. }
  5951. if err != nil {
  5952. return nil, err
  5953. }
  5954. return &flow, nil
  5955. }
  5956. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  5957. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  5958. return info, err
  5959. }
  5960. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  5961. info := models.XtDrugWarehouseInfo{}
  5962. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  5963. return info, err
  5964. }
  5965. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5966. info := models.WarehousingInfo{}
  5967. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  5968. return info, err
  5969. }
  5970. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  5971. flow := models.VmStockFlow{}
  5972. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  5973. if err == gorm.ErrRecordNotFound {
  5974. return nil, err
  5975. }
  5976. if err != nil {
  5977. return nil, err
  5978. }
  5979. return &flow, nil
  5980. }
  5981. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  5982. info := models.GoodInfo{}
  5983. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  5984. return info, err
  5985. }
  5986. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  5987. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  5988. return err
  5989. }
  5990. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  5991. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  5992. return err
  5993. }
  5994. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  5995. 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, "good_code": info.GoodCode}).Error
  5996. return err
  5997. }
  5998. func UpdateNewWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  5999. err := tx.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, "good_code": info.GoodCode}).Error
  6000. if err != nil {
  6001. tx.Rollback()
  6002. return err
  6003. }
  6004. return err
  6005. }
  6006. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6007. 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
  6008. return err
  6009. }
  6010. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6011. 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
  6012. return err
  6013. }
  6014. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  6015. 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
  6016. return err
  6017. }
  6018. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  6019. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  6020. return err
  6021. }
  6022. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  6023. if orgid == 10653 {
  6024. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0 and x.stock_count>0")
  6025. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6026. fmt.Println(table)
  6027. if orgid > 0 {
  6028. db = db.Where("x.org_id = ?", orgid)
  6029. }
  6030. 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
  6031. return info, err
  6032. } else {
  6033. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0")
  6034. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6035. fmt.Println(table)
  6036. if orgid > 0 {
  6037. db = db.Where("x.org_id = ?", orgid)
  6038. }
  6039. 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
  6040. return info, err
  6041. }
  6042. }
  6043. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  6044. warehouseInfo := models.XtDrugWarehouseInfo{}
  6045. 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
  6046. return err
  6047. }
  6048. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  6049. err := XTWriteDB().Create(&info).Error
  6050. return err
  6051. }
  6052. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  6053. err := tx.Create(&info).Error
  6054. if err != nil {
  6055. tx.Rollback()
  6056. return err
  6057. }
  6058. return err
  6059. }
  6060. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  6061. flow := models.VmStockFlow{}
  6062. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  6063. if err == gorm.ErrRecordNotFound {
  6064. return nil, err
  6065. }
  6066. if err != nil {
  6067. return nil, err
  6068. }
  6069. return &flow, nil
  6070. }
  6071. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  6072. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  6073. return err
  6074. }
  6075. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  6076. 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
  6077. return err
  6078. }
  6079. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  6080. flow := models.DrugWarehouseOutInfo{}
  6081. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6082. return flow, err
  6083. }
  6084. func GetLastNewWarehouseOutInfoById(drugid int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
  6085. flow := models.DrugWarehouseOutInfo{}
  6086. err := tx.Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6087. if err != gorm.ErrRecordNotFound {
  6088. if err != nil {
  6089. tx.Rollback()
  6090. return flow, err
  6091. }
  6092. }
  6093. return flow, err
  6094. }
  6095. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  6096. info := models.WarehouseOutInfo{}
  6097. 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
  6098. return info, err
  6099. }
  6100. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  6101. 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
  6102. return err
  6103. }
  6104. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  6105. 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
  6106. return err
  6107. }
  6108. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  6109. info := models.DrugWarehouseOutInfo{}
  6110. 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
  6111. if err == gorm.ErrRecordNotFound {
  6112. return nil, err
  6113. }
  6114. if err != nil {
  6115. return nil, err
  6116. }
  6117. return &info, nil
  6118. }
  6119. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  6120. drugFlow := models.DrugFlow{}
  6121. 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
  6122. if err == gorm.ErrRecordNotFound {
  6123. return nil, err
  6124. }
  6125. if err != nil {
  6126. return nil, err
  6127. }
  6128. return &drugFlow, nil
  6129. }
  6130. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  6131. 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
  6132. return err
  6133. }
  6134. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  6135. 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
  6136. return err
  6137. }
  6138. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  6139. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6140. return detail, err
  6141. }
  6142. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  6143. detail := models.DrugAutomaticReduceDetail{}
  6144. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6145. if err == gorm.ErrRecordNotFound {
  6146. return nil, err
  6147. }
  6148. if err != nil {
  6149. return nil, err
  6150. }
  6151. return &detail, nil
  6152. }
  6153. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  6154. 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
  6155. return err
  6156. }
  6157. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  6158. 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
  6159. return err
  6160. }
  6161. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  6162. info := models.DrugWarehouseInfo{}
  6163. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6164. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6165. return info, err
  6166. }
  6167. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  6168. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6169. return err
  6170. }
  6171. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  6172. info := models.DrugWarehouseInfo{}
  6173. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6174. return err
  6175. }
  6176. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  6177. info := models.DrugCancelStockInfo{}
  6178. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6179. return info, err
  6180. }
  6181. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  6182. info := models.DrugWarehouseInfo{}
  6183. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  6184. return err
  6185. }
  6186. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  6187. info := models.DrugWarehouseInfo{}
  6188. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  6189. return err
  6190. }
  6191. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  6192. 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
  6193. return advice, err
  6194. }
  6195. func GetAllHisDoctorAdviceByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (advice []*models.HisDoctorAdviceInfo, err error) {
  6196. err = tx.Where("drug_id = ? and patient_id = ? and advice_date = ? and execution_state = 1 and status = 1 and user_org_id = ?", drugid, patient_id, advicedate, orgid).Find(&advice).Error
  6197. if err != gorm.ErrRecordNotFound {
  6198. if err != nil {
  6199. tx.Rollback()
  6200. return advice, err
  6201. }
  6202. }
  6203. return advice, err
  6204. }
  6205. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6206. 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
  6207. return info, err
  6208. }
  6209. func GetAllDrugFlowByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  6210. err = tx.Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", drugid, patient_id, advicedate, orgid).Find(&info).Error
  6211. if err != gorm.ErrRecordNotFound {
  6212. if err != nil {
  6213. tx.Rollback()
  6214. return info, err
  6215. }
  6216. }
  6217. return info, err
  6218. }
  6219. // 耗材加库存
  6220. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6221. info := models.GoodInfo{}
  6222. 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
  6223. return info, err
  6224. }
  6225. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6226. info := models.GoodInfo{}
  6227. 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
  6228. 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
  6229. return info, err
  6230. }
  6231. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6232. info := models.GoodInfo{}
  6233. 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
  6234. return info, err
  6235. }
  6236. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6237. info := models.GoodInfo{}
  6238. 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
  6239. 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
  6240. return info, err
  6241. }
  6242. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  6243. info := models.GoodInfo{}
  6244. 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
  6245. return info, err
  6246. }
  6247. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6248. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6249. return info, err
  6250. }
  6251. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6252. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6253. return info, err
  6254. }
  6255. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  6256. info := models.WarehousingInfo{}
  6257. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  6258. return info, err
  6259. }
  6260. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  6261. info := models.WarehousingInfo{}
  6262. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  6263. return info, err
  6264. }
  6265. // 药品加库存
  6266. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6267. info := models.BaseDrugLib{}
  6268. 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
  6269. return info, err
  6270. }
  6271. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6272. info := models.BaseDrugLib{}
  6273. 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
  6274. 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
  6275. return info, err
  6276. }
  6277. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6278. info := models.BaseDrugLib{}
  6279. 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
  6280. return info, err
  6281. }
  6282. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6283. info := models.BaseDrugLib{}
  6284. 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
  6285. 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
  6286. return info, err
  6287. }
  6288. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  6289. info := models.BaseDrugLib{}
  6290. 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
  6291. 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
  6292. return info, err
  6293. }
  6294. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6295. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6296. return info, err
  6297. }
  6298. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6299. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6300. return info, err
  6301. }
  6302. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  6303. info := models.WarehouseOutInfo{}
  6304. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6305. return info, err
  6306. }
  6307. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  6308. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  6309. return out, err
  6310. }
  6311. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  6312. info := models.WarehousingInfo{}
  6313. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  6314. return info, err
  6315. }
  6316. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  6317. err = XTReadDB().Where("status = 1").Find(&template).Error
  6318. return template, err
  6319. }
  6320. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  6321. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  6322. return good, err
  6323. }
  6324. func GetMobileGoodList(org_id int64) (list []*models.GoodInformationThirty, err error) {
  6325. err = XTReadDB().Where("org_id = ? and status =1", org_id).Find(&list).Error
  6326. return list, err
  6327. }
  6328. func GetAllMobileProjectList(orgid int64) (project []*models.HisProject, err error) {
  6329. err = XTReadDB().Where("user_org_id =? and status =1", orgid).Find(&project).Error
  6330. return project, err
  6331. }
  6332. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6333. info := models.GoodInfo{}
  6334. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6335. return info, err
  6336. }
  6337. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  6338. db := XTReadDB().Where("status = 1")
  6339. if good_id > 0 {
  6340. db = db.Where("good_id = ?", good_id)
  6341. }
  6342. err = db.Find(&info).Error
  6343. return info, err
  6344. }
  6345. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6346. db := XTReadDB().Model(&info).Where("status = 1")
  6347. if drug_id > 0 {
  6348. db = db.Where("drug_id = ?", drug_id)
  6349. }
  6350. err = db.Find(&info).Error
  6351. return info, err
  6352. }
  6353. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6354. lib := models.BaseDrugLib{}
  6355. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6356. return lib, err
  6357. }
  6358. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  6359. info := models.Warehousing{}
  6360. 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
  6361. if err == gorm.ErrRecordNotFound {
  6362. return nil, err
  6363. }
  6364. if err != nil {
  6365. return nil, err
  6366. }
  6367. return &info, nil
  6368. }
  6369. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  6370. info := models.Warehousing{}
  6371. 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
  6372. return info, err
  6373. }
  6374. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  6375. out := models.DrugWarehouseOut{}
  6376. 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
  6377. if err == gorm.ErrRecordNotFound {
  6378. return nil, err
  6379. }
  6380. if err != nil {
  6381. return nil, err
  6382. }
  6383. return &out, nil
  6384. }
  6385. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  6386. info := models.DrugWarehouseOut{}
  6387. 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
  6388. return info, err
  6389. }
  6390. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  6391. info := models.DrugWarehouse{}
  6392. 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
  6393. if err == gorm.ErrRecordNotFound {
  6394. return nil, err
  6395. }
  6396. if err != nil {
  6397. return nil, err
  6398. }
  6399. return &info, nil
  6400. }
  6401. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  6402. err := XTWriteDB().Create(&warehouse).Error
  6403. return err
  6404. }
  6405. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  6406. info := models.DrugWarehouse{}
  6407. 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
  6408. return info, err
  6409. }
  6410. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6411. info := models.GoodInfo{}
  6412. 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
  6413. 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
  6414. return info, err
  6415. }
  6416. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6417. info := models.GoodInfo{}
  6418. 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
  6419. 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
  6420. return info, err
  6421. }
  6422. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6423. tx := XTWriteDB().Begin()
  6424. 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
  6425. if err != nil {
  6426. tx.Rollback()
  6427. return err
  6428. }
  6429. tx.Commit()
  6430. return err
  6431. }
  6432. func UpdateNewMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64, tx *gorm.DB) error {
  6433. 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
  6434. if err != nil {
  6435. tx.Rollback()
  6436. return err
  6437. }
  6438. return err
  6439. }
  6440. func UpdateMedicalSumCountOne(drug_id int64, sum_count int64, orgid int64) error {
  6441. tx := XTWriteDB().Begin()
  6442. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6443. if err != nil {
  6444. tx.Rollback()
  6445. return err
  6446. }
  6447. tx.Commit()
  6448. return err
  6449. }
  6450. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  6451. // 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
  6452. // return err
  6453. //}
  6454. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6455. 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
  6456. return err
  6457. }
  6458. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6459. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6460. table := XTReadDB().Table("xt_base_drug as x d")
  6461. fmt.Println(table)
  6462. if startime > 0 {
  6463. db = db.Where("x.advice_date >= ?", startime)
  6464. }
  6465. if endtime > 0 {
  6466. db = db.Where("x.advice_date <= ?", endtime)
  6467. }
  6468. if orgid > 0 {
  6469. db = db.Where("x.user_org_id = ?", orgid)
  6470. }
  6471. if len(keyword) > 0 {
  6472. likeKey := "%" + keyword + "%"
  6473. 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)
  6474. }
  6475. offset := (page - 1) * limit
  6476. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  6477. return adviceinfo, total, err
  6478. }
  6479. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6480. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6481. table := XTReadDB().Table("xt_base_drug as x d")
  6482. fmt.Println(table)
  6483. if startime > 0 {
  6484. db = db.Where("x.advice_date >= ?", startime)
  6485. }
  6486. if endtime > 0 {
  6487. db = db.Where("x.advice_date <= ?", endtime)
  6488. }
  6489. if orgid > 0 {
  6490. db = db.Where("x.user_org_id = ?", orgid)
  6491. }
  6492. if len(keyword) > 0 {
  6493. likeKey := "%" + keyword + "%"
  6494. 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)
  6495. }
  6496. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  6497. return adviceinfo, total, err
  6498. }
  6499. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  6500. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  6501. return drug, err
  6502. }
  6503. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6504. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6505. table := XTReadDB().Table("xt_good_information as d")
  6506. fmt.Println(table)
  6507. if startime > 0 {
  6508. db = db.Where("x.record_date >= ?", startime)
  6509. }
  6510. if endtime > 0 {
  6511. db = db.Where("x.record_date <= ?", endtime)
  6512. }
  6513. if orgid > 0 {
  6514. db = db.Where("x.user_org_id = ?", orgid)
  6515. }
  6516. if len(keyword) > 0 {
  6517. likeKey := "%" + keyword + "%"
  6518. 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")
  6519. }
  6520. offset := (page - 1) * limit
  6521. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  6522. return project, total, err
  6523. }
  6524. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6525. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6526. table := XTReadDB().Table("xt_good_information as d")
  6527. fmt.Println(table)
  6528. if startime > 0 {
  6529. db = db.Where("x.record_date >= ?", startime)
  6530. }
  6531. if endtime > 0 {
  6532. db = db.Where("x.record_date <= ?", endtime)
  6533. }
  6534. if orgid > 0 {
  6535. db = db.Where("x.user_org_id = ?", orgid)
  6536. }
  6537. if len(keyword) > 0 {
  6538. likeKey := "%" + keyword + "%"
  6539. 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")
  6540. }
  6541. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  6542. return project, total, err
  6543. }
  6544. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  6545. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  6546. return good, err
  6547. }
  6548. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  6549. setting := models.XtStockSetting{}
  6550. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6551. if err == gorm.ErrRecordNotFound {
  6552. return nil, err
  6553. }
  6554. if err != nil {
  6555. return nil, err
  6556. }
  6557. return &setting, nil
  6558. }
  6559. func CreateStockSetting(setting models.XtStockSetting) error {
  6560. err := XTWriteDB().Create(&setting).Error
  6561. return err
  6562. }
  6563. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6564. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6565. return err
  6566. }
  6567. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6568. setting := models.XtStockSetting{}
  6569. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6570. return setting, err
  6571. }
  6572. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6573. detail := models.BloodAutomaticReduceDetail{}
  6574. 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
  6575. return detail, err
  6576. }
  6577. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6578. detail := models.WarehouseOutInfoNight{}
  6579. 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
  6580. return detail, err
  6581. }
  6582. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6583. setting := models.XtAdviceSetting{}
  6584. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6585. if err == gorm.ErrRecordNotFound {
  6586. return nil, err
  6587. }
  6588. if err != nil {
  6589. return nil, err
  6590. }
  6591. return &setting, nil
  6592. }
  6593. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6594. err := XTWriteDB().Create(&setting).Error
  6595. return err
  6596. }
  6597. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6598. 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
  6599. return err
  6600. }
  6601. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6602. setting := models.XtAdviceSetting{}
  6603. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6604. return setting, err
  6605. }
  6606. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6607. config := models.XtPrescriptionConfig{}
  6608. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6609. if err == gorm.ErrRecordNotFound {
  6610. return nil, err
  6611. }
  6612. if err != nil {
  6613. return nil, err
  6614. }
  6615. return &config, nil
  6616. }
  6617. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6618. err := XTWriteDB().Create(&prescriptionConfig).Error
  6619. return err
  6620. }
  6621. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6622. 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
  6623. return err
  6624. }
  6625. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6626. config := models.XtPrescriptionConfig{}
  6627. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6628. return config, err
  6629. }
  6630. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6631. var deliver_number int64 = 0
  6632. var stock_number int64 = 0
  6633. deliver_number = maxNumber
  6634. // 根据先进先出原则,查询最先入库的批次,进行出库
  6635. // 如果没有对应的库存,则报错
  6636. //开启事物
  6637. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6638. if err != nil {
  6639. return err
  6640. }
  6641. // 将该批次的剩余库存数量转换为拆零数量
  6642. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6643. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6644. if stock_number >= deliver_number {
  6645. var maxNumber int64 = 0
  6646. var minNumber int64 = 0
  6647. var stock_max_number int64 = 0
  6648. stock_max_number = warehouse.StockMaxNumber
  6649. maxNumber = deliver_number / drup.MinNumber
  6650. minNumber = deliver_number % drup.MinNumber
  6651. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6652. minNumber = maxNumber
  6653. }
  6654. if drup.MaxUnit != drup.MinUnit {
  6655. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6656. return errors.New("库存数量不足")
  6657. }
  6658. }
  6659. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6660. if warehouse.StockMaxNumber < 0 {
  6661. warehouse.StockMaxNumber = 0
  6662. }
  6663. if warehouse.StockMinNumber < 0 {
  6664. warehouse.StockMinNumber = 0
  6665. }
  6666. warehouse.Mtime = time.Now().Unix()
  6667. if warehouse.StockMinNumber < minNumber {
  6668. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6669. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6670. } else {
  6671. if minNumber > 0 {
  6672. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6673. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6674. } else {
  6675. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6676. }
  6677. }
  6678. if minNumber == 0 && maxNumber != 1 {
  6679. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6680. }
  6681. }
  6682. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6683. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6684. if warehouse.StockMinNumber == 0 {
  6685. warehouse.StockMaxNumber = stock_max_number
  6686. }
  6687. }
  6688. if drup.MaxUnit != drup.MinUnit {
  6689. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6690. return errors.New("库存数量不足")
  6691. }
  6692. }
  6693. if warehouse.StockMinNumber <= 0 {
  6694. warehouse.StockMinNumber = 0
  6695. }
  6696. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6697. fmt.Println(errThree)
  6698. //查询默认仓库
  6699. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6700. //查询默认仓库剩余多少库存
  6701. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6702. var sum_count int64
  6703. var sum_in_count int64
  6704. for _, it := range list {
  6705. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6706. if it.MaxUnit == baseDrug.MaxUnit {
  6707. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6708. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6709. sum_count += it.StockMaxNumber + it.StockMinNumber
  6710. sum_in_count += it.WarehousingCount
  6711. }
  6712. }
  6713. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6714. return nil
  6715. } else {
  6716. warehouse.StockMaxNumber = 0
  6717. warehouse.StockMinNumber = 0
  6718. warehouse.Mtime = time.Now().Unix()
  6719. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6720. fmt.Println(errThree)
  6721. //查询默认仓库
  6722. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6723. //查询默认仓库剩余多少库存
  6724. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6725. var sum_count int64
  6726. var sum_in_count int64
  6727. for _, it := range list {
  6728. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6729. if it.MaxUnit == baseDrug.MaxUnit {
  6730. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6731. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6732. sum_count += it.StockMaxNumber + it.StockMinNumber
  6733. sum_in_count += it.WarehousingCount
  6734. }
  6735. }
  6736. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6737. if errThree != nil {
  6738. return errThree
  6739. }
  6740. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6741. prescribingNumber_two_temp := deliver_number - stock_number
  6742. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6743. }
  6744. return
  6745. }
  6746. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6747. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6748. return err
  6749. }
  6750. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6751. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6752. return err
  6753. }
  6754. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6755. config := models.DrugOutConfig{}
  6756. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6757. if err == gorm.ErrRecordNotFound {
  6758. return nil, err
  6759. }
  6760. if err != nil {
  6761. return nil, err
  6762. }
  6763. return &config, nil
  6764. }
  6765. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6766. config := models.DrugOutConfig{}
  6767. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6768. return config, err
  6769. }
  6770. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6771. config := models.XtHisConfig{}
  6772. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6773. return config, err
  6774. }
  6775. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6776. err := XTWriteDB().Create(&config).Error
  6777. return err
  6778. }
  6779. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6780. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6781. return err
  6782. }
  6783. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6784. config := models.DrugOutConfig{}
  6785. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6786. return config, err
  6787. }
  6788. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6789. config := models.HisSettleStockConfig{}
  6790. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6791. return config, err
  6792. }
  6793. func UpdatedWarehouseOut(id int64) error {
  6794. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6795. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6796. return err
  6797. }
  6798. func UpdatedNewWarehouseOut(id int64, tx *gorm.DB) error {
  6799. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6800. if err != nil {
  6801. tx.Rollback()
  6802. return err
  6803. }
  6804. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6805. if err != nil {
  6806. tx.Rollback()
  6807. return err
  6808. }
  6809. return err
  6810. }
  6811. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6812. tx := XTWriteDB().Begin()
  6813. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6814. if err != nil {
  6815. tx.Rollback()
  6816. return err
  6817. }
  6818. tx.Commit()
  6819. return err
  6820. }
  6821. func UpdatedNewSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6822. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6823. if err != nil {
  6824. tx.Rollback()
  6825. return err
  6826. }
  6827. return err
  6828. }
  6829. func GetLastGoodWarehouseOutInfo(orgid int64, id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  6830. err = tx.Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6831. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6832. }).Find(&info).Error
  6833. if err != nil {
  6834. tx.Rollback()
  6835. return info, err
  6836. }
  6837. return info, err
  6838. }
  6839. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6840. info := models.WarehouseOutInfo{}
  6841. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6842. return info, err
  6843. }
  6844. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6845. err := tx.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
  6846. if err != nil {
  6847. tx.Rollback()
  6848. return err
  6849. }
  6850. return err
  6851. }
  6852. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6853. 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
  6854. return err
  6855. }
  6856. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6857. out := models.WarehouseOut{}
  6858. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6859. return out, err
  6860. }
  6861. func GetLastGoodWarehouseOutTwentyOne(orgid int64, tx *gorm.DB) (models.WarehouseOut, error) {
  6862. out := models.WarehouseOut{}
  6863. err := tx.Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6864. if err != gorm.ErrRecordNotFound {
  6865. if err != nil {
  6866. tx.Rollback()
  6867. return out, err
  6868. }
  6869. }
  6870. return out, err
  6871. }
  6872. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6873. ut := writeDb.Begin()
  6874. 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
  6875. if err != nil {
  6876. ut.Rollback()
  6877. return err
  6878. }
  6879. ut.Commit()
  6880. return err
  6881. }
  6882. func UpdateaNewGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64, tx *gorm.DB) error {
  6883. err := tx.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
  6884. if err != nil {
  6885. tx.Rollback()
  6886. return err
  6887. }
  6888. return err
  6889. }
  6890. func ModifyGoodWarehouseOut(id int64) error {
  6891. ut := writeDb.Begin()
  6892. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6893. if err != nil {
  6894. ut.Rollback()
  6895. return err
  6896. }
  6897. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6898. if err != nil {
  6899. ut.Rollback()
  6900. return err
  6901. }
  6902. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6903. if err != nil {
  6904. ut.Rollback()
  6905. return err
  6906. }
  6907. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  6908. if err != nil {
  6909. ut.Rollback()
  6910. return err
  6911. }
  6912. ut.Commit()
  6913. return err
  6914. }
  6915. func ModifyNewGoodWarehouseOut(id int64, tx *gorm.DB) error {
  6916. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6917. if err != nil {
  6918. tx.Rollback()
  6919. return err
  6920. }
  6921. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6922. if err != nil {
  6923. tx.Rollback()
  6924. return err
  6925. }
  6926. err = tx.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6927. if err != nil {
  6928. tx.Rollback()
  6929. return err
  6930. }
  6931. err = tx.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  6932. if err != nil {
  6933. tx.Rollback()
  6934. return err
  6935. }
  6936. return err
  6937. }
  6938. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  6939. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6940. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6941. }).Find(&info).Error
  6942. return info, err
  6943. }
  6944. func GetNewGoodWarehouseInfoById(waresing_id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  6945. err = tx.Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6946. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6947. }).Find(&info).Error
  6948. if err != gorm.ErrRecordNotFound {
  6949. if err != nil {
  6950. tx.Rollback()
  6951. return info, err
  6952. }
  6953. }
  6954. return info, err
  6955. }
  6956. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  6957. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  6958. return info, err
  6959. }
  6960. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  6961. 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, "good_code": info.GoodCode}).Error
  6962. return err
  6963. }
  6964. func UpdateNewWarehouseDetail(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  6965. err := tx.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, "good_code": info.GoodCode}).Error
  6966. if err != nil {
  6967. tx.Rollback()
  6968. return err
  6969. }
  6970. return err
  6971. }
  6972. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  6973. tx := XTWriteDB().Begin()
  6974. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6975. if err != nil {
  6976. tx.Rollback()
  6977. return err
  6978. }
  6979. tx.Commit()
  6980. return err
  6981. }
  6982. func UpdateNewCheckWarehouseInfo(id int64, checker int64, tx *gorm.DB) error {
  6983. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  6984. if err != nil {
  6985. tx.Rollback()
  6986. return err
  6987. }
  6988. return err
  6989. }
  6990. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  6991. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6992. return info, err
  6993. }
  6994. func GetNewWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  6995. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  6996. if err != gorm.ErrRecordNotFound {
  6997. if err != nil {
  6998. tx.Rollback()
  6999. return info, err
  7000. }
  7001. }
  7002. return info, err
  7003. }
  7004. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  7005. tx := XTWriteDB().Begin()
  7006. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7007. if err != nil {
  7008. tx.Rollback()
  7009. return err
  7010. }
  7011. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7012. if err != nil {
  7013. tx.Rollback()
  7014. return err
  7015. }
  7016. tx.Commit()
  7017. return err
  7018. }
  7019. func UpdateNewWarehouseInfoByIdListThree(count int64, id int64, tx *gorm.DB) error {
  7020. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7021. if err != nil {
  7022. tx.Rollback()
  7023. return err
  7024. }
  7025. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7026. if err != nil {
  7027. tx.Rollback()
  7028. return err
  7029. }
  7030. return err
  7031. }
  7032. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  7033. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7034. return info, err
  7035. }
  7036. func FindNewLastWarehousingSix(org_id int64, tx *gorm.DB) (info models.Warehousing, err error) {
  7037. err = tx.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7038. if err != gorm.ErrRecordNotFound {
  7039. if err != nil {
  7040. tx.Rollback()
  7041. return info, err
  7042. }
  7043. }
  7044. return info, err
  7045. }
  7046. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  7047. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7048. return info, err
  7049. }
  7050. func FindNewLastWarehousingSeven(org_id int64, tx *gorm.DB) (info models.DrugWarehouse, err error) {
  7051. err = tx.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7052. if err != gorm.ErrRecordNotFound {
  7053. if err != nil {
  7054. tx.Rollback()
  7055. return info, err
  7056. }
  7057. }
  7058. return info, err
  7059. }
  7060. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7061. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7062. return info, err
  7063. }
  7064. func FindNewDrugWarehouseInfoListById(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7065. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7066. if err != gorm.ErrRecordNotFound {
  7067. if err != nil {
  7068. tx.Rollback()
  7069. return info, err
  7070. }
  7071. }
  7072. return info, err
  7073. }
  7074. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  7075. err := XTWriteDB().Create(&info).Error
  7076. return err
  7077. }
  7078. func CreatedNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7079. err := tx.Create(&info).Error
  7080. if err != nil {
  7081. tx.Rollback()
  7082. return err
  7083. }
  7084. return err
  7085. }
  7086. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  7087. err := XTWriteDB().Save(&info).Error
  7088. return err
  7089. }
  7090. func UpdateNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7091. err := tx.Save(&info).Error
  7092. if err != nil {
  7093. tx.Rollback()
  7094. return err
  7095. }
  7096. return err
  7097. }
  7098. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  7099. tx := XTWriteDB().Begin()
  7100. 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
  7101. if err != nil {
  7102. tx.Rollback()
  7103. return err
  7104. }
  7105. tx.Commit()
  7106. return err
  7107. }
  7108. func CheckNewWarehousingInfo(id int64, orgid int64, checker int64, tx *gorm.DB) error {
  7109. 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
  7110. if err != nil {
  7111. tx.Rollback()
  7112. return err
  7113. }
  7114. return err
  7115. }
  7116. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  7117. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7118. return info, err
  7119. }
  7120. func GetNewWarehousingInfoByList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7121. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7122. if err != gorm.ErrRecordNotFound {
  7123. if err != nil {
  7124. tx.Rollback()
  7125. return info, err
  7126. }
  7127. }
  7128. return info, err
  7129. }
  7130. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  7131. tx := XTWriteDB().Begin()
  7132. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7133. if err != nil {
  7134. tx.Rollback()
  7135. return err
  7136. }
  7137. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7138. if err != nil {
  7139. tx.Rollback()
  7140. return err
  7141. }
  7142. tx.Commit()
  7143. return err
  7144. }
  7145. func AddNewDrugWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7146. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7147. if err != nil {
  7148. tx.Rollback()
  7149. return err
  7150. }
  7151. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7152. if err != nil {
  7153. tx.Rollback()
  7154. return err
  7155. }
  7156. return err
  7157. }
  7158. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  7159. tx := XTWriteDB().Begin()
  7160. 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
  7161. if err != nil {
  7162. tx.Rollback()
  7163. return err
  7164. }
  7165. tx.Commit()
  7166. return err
  7167. }
  7168. func UpdateNewWarehousingInfoFlow(id int64, orgid int64, tx *gorm.DB) error {
  7169. 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
  7170. if err != nil {
  7171. tx.Rollback()
  7172. return err
  7173. }
  7174. return err
  7175. }
  7176. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  7177. tx := XTWriteDB().Begin()
  7178. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7179. if err != nil {
  7180. tx.Rollback()
  7181. return err
  7182. }
  7183. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7184. tx.Commit()
  7185. return err
  7186. }
  7187. func AddNewDrugWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7188. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7189. if err != nil {
  7190. tx.Rollback()
  7191. return err
  7192. }
  7193. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7194. if err != nil {
  7195. tx.Rollback()
  7196. return err
  7197. }
  7198. return err
  7199. }
  7200. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  7201. tx := XTWriteDB().Begin()
  7202. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7203. if err != nil {
  7204. tx.Rollback()
  7205. return err
  7206. }
  7207. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7208. if err != nil {
  7209. tx.Rollback()
  7210. return err
  7211. }
  7212. tx.Commit()
  7213. return err
  7214. }
  7215. func ReturnNewCheckWarehouseingInfo(id int64, orgid int64, tx *gorm.DB) error {
  7216. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7217. if err != nil {
  7218. tx.Rollback()
  7219. return err
  7220. }
  7221. err = tx.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7222. if err != nil {
  7223. tx.Rollback()
  7224. return err
  7225. }
  7226. return err
  7227. }
  7228. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  7229. tx := XTWriteDB().Begin()
  7230. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7231. if err != nil {
  7232. tx.Rollback()
  7233. return err
  7234. }
  7235. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7236. if err != nil {
  7237. tx.Rollback()
  7238. return err
  7239. }
  7240. tx.Commit()
  7241. return err
  7242. }
  7243. func AddNewDrugReturnWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7244. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7245. if err != nil {
  7246. tx.Rollback()
  7247. return err
  7248. }
  7249. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7250. if err != nil {
  7251. tx.Rollback()
  7252. return err
  7253. }
  7254. return err
  7255. }
  7256. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  7257. tx := XTWriteDB().Begin()
  7258. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7259. if err != nil {
  7260. tx.Rollback()
  7261. return err
  7262. }
  7263. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7264. if err != nil {
  7265. tx.Rollback()
  7266. return err
  7267. }
  7268. tx.Commit()
  7269. return err
  7270. }
  7271. func AddNewDrugReturnWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7272. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7273. if err != nil {
  7274. tx.Rollback()
  7275. return err
  7276. }
  7277. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7278. if err != nil {
  7279. tx.Rollback()
  7280. return err
  7281. }
  7282. return err
  7283. }
  7284. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  7285. 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, "drug_code": info.DrugCode, "batch_number": info.BatchNumber}).Error
  7286. return err
  7287. }
  7288. func UpdateNewDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7289. err := tx.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, "drug_code": info.DrugCode, "batch_number": info.BatchNumber}).Error
  7290. if err != nil {
  7291. tx.Rollback()
  7292. return err
  7293. }
  7294. return err
  7295. }
  7296. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  7297. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7298. return err
  7299. }
  7300. func UpdatedNewDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7301. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7302. if err != nil {
  7303. tx.Rollback()
  7304. return err
  7305. }
  7306. return err
  7307. }
  7308. func UpdatedSelfDrugWarehouseInfo(info *models.XtSelfDrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7309. err := tx.Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7310. if err != gorm.ErrRecordNotFound {
  7311. return err
  7312. }
  7313. return err
  7314. }
  7315. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  7316. out := models.DrugWarehouseOut{}
  7317. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7318. return out, err
  7319. }
  7320. func GetNewDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7321. out := models.DrugWarehouseOut{}
  7322. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7323. if err != gorm.ErrRecordNotFound {
  7324. if err != nil {
  7325. tx.Rollback()
  7326. return out, err
  7327. }
  7328. }
  7329. return out, err
  7330. }
  7331. func GetSelfDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7332. out := models.XtSelfDrugWarehouseOut{}
  7333. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7334. if err != gorm.ErrRecordNotFound {
  7335. if err != nil {
  7336. tx.Rollback()
  7337. return out, err
  7338. }
  7339. }
  7340. return out, err
  7341. }
  7342. func GetSelfDrugLastWarehouseOutOne(id int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7343. out := models.XtSelfDrugWarehouseOut{}
  7344. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7345. if err != gorm.ErrRecordNotFound {
  7346. if err != nil {
  7347. tx.Rollback()
  7348. return out, err
  7349. }
  7350. }
  7351. return out, err
  7352. }
  7353. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7354. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7355. return info, err
  7356. }
  7357. func GetNewDrugWarehouseOutListById(warehouse_out_id int64, orgId int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  7358. err = tx.Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7359. if err != gorm.ErrRecordNotFound {
  7360. if err != nil {
  7361. tx.Rollback()
  7362. return info, err
  7363. }
  7364. }
  7365. return info, err
  7366. }
  7367. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  7368. tx := XTWriteDB().Begin()
  7369. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7370. if err != nil {
  7371. tx.Rollback()
  7372. return err
  7373. }
  7374. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7375. if err != nil {
  7376. tx.Rollback()
  7377. return err
  7378. }
  7379. tx.Commit()
  7380. return err
  7381. }
  7382. func UpdateNewCheckDrugOut(out models.DrugWarehouseOut, id int64, tx *gorm.DB) error {
  7383. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7384. if err != nil {
  7385. tx.Rollback()
  7386. return err
  7387. }
  7388. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7389. if err != nil {
  7390. tx.Rollback()
  7391. return err
  7392. }
  7393. tx.Commit()
  7394. return err
  7395. }
  7396. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  7397. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7398. return err
  7399. }
  7400. func UpdateNewCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64, tx *gorm.DB) error {
  7401. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7402. if err != nil {
  7403. tx.Rollback()
  7404. return err
  7405. }
  7406. return err
  7407. }
  7408. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  7409. out := models.DrugWarehouseOut{}
  7410. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  7411. return out, err
  7412. }
  7413. func GetNewDrugWarehouseOutByLastId(id int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7414. out := models.DrugWarehouseOut{}
  7415. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7416. if err != gorm.ErrRecordNotFound {
  7417. if err != nil {
  7418. tx.Rollback()
  7419. return out, err
  7420. }
  7421. }
  7422. return out, err
  7423. }
  7424. func UpdateDrugMaxNumber(count int64, id int64) error {
  7425. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7426. return err
  7427. }
  7428. func UpdateNewDrugMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7429. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7430. if err != gorm.ErrRecordNotFound {
  7431. if err != nil {
  7432. tx.Rollback()
  7433. return err
  7434. }
  7435. }
  7436. return err
  7437. }
  7438. func UpdateDrugMinNumber(count int64, id int64) error {
  7439. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7440. return err
  7441. }
  7442. func UpdateNewDrugMinNumber(count int64, id int64, tx *gorm.DB) error {
  7443. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7444. if err != nil {
  7445. tx.Rollback()
  7446. return err
  7447. }
  7448. return err
  7449. }
  7450. func DeleteDrugOutFlow(id int64, orgid int64) error {
  7451. 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
  7452. return err
  7453. }
  7454. func DeleteNewDrugOutFlow(id int64, orgid int64, tx *gorm.DB) error {
  7455. err := tx.Model(models.DrugFlow{}).Where("warehouse_out_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  7456. if err != nil {
  7457. tx.Rollback()
  7458. return err
  7459. }
  7460. return err
  7461. }
  7462. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  7463. 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
  7464. return outInfo, err
  7465. }
  7466. func GetNewWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.WarehouseOutInfo, err error) {
  7467. err = tx.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
  7468. if err != gorm.ErrRecordNotFound {
  7469. if err != nil {
  7470. tx.Rollback()
  7471. return outInfo, err
  7472. }
  7473. }
  7474. return outInfo, err
  7475. }
  7476. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7477. 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
  7478. return outInfo, err
  7479. }
  7480. func GetNewDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7481. err = tx.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
  7482. if err != gorm.ErrRecordNotFound {
  7483. if err != nil {
  7484. tx.Rollback()
  7485. return outInfo, err
  7486. }
  7487. }
  7488. return outInfo, err
  7489. }
  7490. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7491. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7492. return info, err
  7493. }
  7494. func GetNewAllStockInList(id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7495. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7496. if err != gorm.ErrRecordNotFound {
  7497. if err != nil {
  7498. tx.Rollback()
  7499. return info, err
  7500. }
  7501. }
  7502. return info, err
  7503. }
  7504. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7505. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7506. return info, err
  7507. }
  7508. func GetNewDrugWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7509. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7510. if err != gorm.ErrRecordNotFound {
  7511. if err != nil {
  7512. tx.Rollback()
  7513. return info, err
  7514. }
  7515. }
  7516. return info, err
  7517. }
  7518. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  7519. 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
  7520. return info, err
  7521. }
  7522. func GetNewStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7523. err = tx.Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", goodid, storehouse_id, orgId).Find(&info).Error
  7524. if err != gorm.ErrRecordNotFound {
  7525. if err != nil {
  7526. tx.Rollback()
  7527. return info, err
  7528. }
  7529. }
  7530. return info, err
  7531. }
  7532. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  7533. 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
  7534. 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
  7535. return err
  7536. }
  7537. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  7538. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7539. return err
  7540. }
  7541. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  7542. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7543. return err
  7544. }
  7545. func UpdateNewGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7546. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7547. if err != gorm.ErrRecordNotFound {
  7548. if err != nil {
  7549. tx.Rollback()
  7550. return err
  7551. }
  7552. }
  7553. return err
  7554. }
  7555. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  7556. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7557. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7558. return err
  7559. }
  7560. func UpdateNewGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7561. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7562. if err != nil {
  7563. tx.Rollback()
  7564. return err
  7565. }
  7566. 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
  7567. if err != nil {
  7568. tx.Rollback()
  7569. return err
  7570. }
  7571. return err
  7572. }
  7573. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  7574. 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
  7575. return stock, err
  7576. }
  7577. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  7578. 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
  7579. return err
  7580. }
  7581. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7582. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7583. return info, err
  7584. }
  7585. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  7586. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7587. return info, err
  7588. }
  7589. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  7590. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7591. return info, err
  7592. }
  7593. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  7594. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  7595. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  7596. return err
  7597. }
  7598. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7599. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  7600. return info, err
  7601. }
  7602. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  7603. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and status = 1", warehouse_info_id, orgid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", stock_count)).Error
  7604. return err
  7605. }
  7606. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  7607. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  7608. return err
  7609. }
  7610. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  7611. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7612. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7613. return err
  7614. }
  7615. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  7616. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("cancel_out_detail_id = ? and user_org_id = ? and status = 1 and good_id =?", id, orgid, goodid).Update(map[string]interface{}{"status": 0}).Error
  7617. return err
  7618. }
  7619. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  7620. 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
  7621. 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
  7622. return err
  7623. }
  7624. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  7625. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  7626. 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
  7627. return err
  7628. }
  7629. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  7630. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  7631. return info, err
  7632. }
  7633. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  7634. 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
  7635. 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
  7636. return err
  7637. }
  7638. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  7639. 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
  7640. return info, err
  7641. }
  7642. func GetSelfDrugAllStockInfoOne(patient_id int64, orgid int64, drugid int64) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  7643. err = XTReadDB().Where("patient_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", patient_id, orgid, drugid).Find(&info).Error
  7644. return info, err
  7645. }
  7646. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7647. 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
  7648. if err != gorm.ErrRecordNotFound {
  7649. if err != nil {
  7650. tx.Rollback()
  7651. return info, err
  7652. }
  7653. }
  7654. return info, err
  7655. }
  7656. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  7657. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7658. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7659. return err
  7660. }
  7661. func UpdateNewBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7662. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7663. if err != gorm.ErrRecordNotFound {
  7664. if err != nil {
  7665. tx.Rollback()
  7666. return err
  7667. }
  7668. }
  7669. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7670. if err != gorm.ErrRecordNotFound {
  7671. if err != nil {
  7672. tx.Rollback()
  7673. return err
  7674. }
  7675. }
  7676. return err
  7677. }
  7678. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  7679. err = writeDb.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7680. return err
  7681. }
  7682. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7683. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7684. if err != nil {
  7685. tx.Rollback()
  7686. return err
  7687. }
  7688. return err
  7689. }
  7690. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7691. 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
  7692. return info, err
  7693. }
  7694. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7695. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7696. return info, err
  7697. }
  7698. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  7699. 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
  7700. 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
  7701. return err
  7702. }
  7703. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  7704. stock := models.DrugCancelStock{}
  7705. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  7706. return stock, err
  7707. }
  7708. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7709. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  7710. return stock, err
  7711. }
  7712. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7713. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  7714. return stock, err
  7715. }
  7716. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7717. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  7718. return info, err
  7719. }
  7720. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  7721. 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
  7722. return err
  7723. }
  7724. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7725. 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
  7726. if err != nil {
  7727. tx.Rollback()
  7728. return err
  7729. }
  7730. return err
  7731. }
  7732. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  7733. 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
  7734. return err
  7735. }
  7736. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7737. 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
  7738. if err != nil {
  7739. tx.Rollback()
  7740. return err
  7741. }
  7742. return err
  7743. }
  7744. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  7745. 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
  7746. return err
  7747. }
  7748. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  7749. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7750. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7751. return err
  7752. }
  7753. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7754. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  7755. return info, err
  7756. }
  7757. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  7758. 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
  7759. return err
  7760. }
  7761. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7762. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7763. return err
  7764. }
  7765. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7766. 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
  7767. return err
  7768. }
  7769. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  7770. info := models.DrugWarehouseInfo{}
  7771. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7772. return info, err
  7773. }
  7774. func GetSelfDrugInfoSix(id int64, orgid int64) (models.XtSelfDrugWarehouseInfo, error) {
  7775. info := models.XtSelfDrugWarehouseInfo{}
  7776. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7777. return info, err
  7778. }
  7779. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  7780. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  7781. if storehouse_id > 0 {
  7782. db = db.Where("storehouse_id = ?", storehouse_id)
  7783. }
  7784. if orgid > 0 {
  7785. db = db.Where("user_org_id = ?", orgid)
  7786. }
  7787. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  7788. return storeinventory, err
  7789. }
  7790. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  7791. detail := models.AutomaticReduceDetail{}
  7792. 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
  7793. return detail, err
  7794. }
  7795. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  7796. detail := models.WarehouseOutInfo{}
  7797. 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
  7798. return detail, err
  7799. }
  7800. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  7801. stock := models.XtGoodStockCount{}
  7802. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  7803. return stock, err
  7804. }
  7805. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  7806. err := XTWriteDB().Create(&stock).Error
  7807. return err
  7808. }
  7809. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7810. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error
  7811. return err
  7812. }
  7813. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7814. ut := XTWriteDB().Begin()
  7815. 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
  7816. if err != nil {
  7817. ut.Rollback()
  7818. return err
  7819. }
  7820. ut.Commit()
  7821. return err
  7822. }
  7823. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  7824. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7825. return err
  7826. }
  7827. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  7828. 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
  7829. if err != nil {
  7830. tx.Rollback()
  7831. return err
  7832. }
  7833. return err
  7834. }
  7835. func CreateDrugStock(stock models.XtDrugStockCount) error {
  7836. err := XTWriteDB().Create(&stock).Error
  7837. return err
  7838. }
  7839. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  7840. ut := XTWriteDB().Begin()
  7841. 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
  7842. if err != nil {
  7843. ut.Rollback()
  7844. return err
  7845. }
  7846. 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
  7847. if err != nil {
  7848. ut.Rollback()
  7849. return err
  7850. }
  7851. ut.Commit()
  7852. return err
  7853. }
  7854. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  7855. ut := XTWriteDB().Begin()
  7856. 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
  7857. if err != nil {
  7858. ut.Rollback()
  7859. return err
  7860. }
  7861. ut.Commit()
  7862. return err
  7863. }
  7864. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  7865. ut := XTWriteDB().Begin()
  7866. 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
  7867. if err != nil {
  7868. ut.Rollback()
  7869. return err
  7870. }
  7871. 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
  7872. if err != nil {
  7873. ut.Rollback()
  7874. return err
  7875. }
  7876. ut.Commit()
  7877. return err
  7878. }
  7879. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  7880. ut := XTWriteDB().Begin()
  7881. 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
  7882. if err != nil {
  7883. ut.Rollback()
  7884. return err
  7885. }
  7886. ut.Commit()
  7887. return err
  7888. }
  7889. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7890. ut := XTWriteDB().Begin()
  7891. 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
  7892. if err != nil {
  7893. ut.Rollback()
  7894. return err
  7895. }
  7896. 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
  7897. if err != nil {
  7898. ut.Rollback()
  7899. return err
  7900. }
  7901. ut.Commit()
  7902. return err
  7903. }
  7904. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7905. ut := XTWriteDB().Begin()
  7906. 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
  7907. if err != nil {
  7908. ut.Rollback()
  7909. return err
  7910. }
  7911. 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
  7912. if err != nil {
  7913. ut.Rollback()
  7914. return err
  7915. }
  7916. ut.Commit()
  7917. return err
  7918. }
  7919. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  7920. if orgid == 10191 || orgid == 10138 || orgid == 10278 {
  7921. 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
  7922. return info, err
  7923. } else {
  7924. 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
  7925. return info, err
  7926. }
  7927. }
  7928. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  7929. config := models.HisSettleStockConfig{}
  7930. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7931. if err == gorm.ErrRecordNotFound {
  7932. return nil, err
  7933. }
  7934. if err != nil {
  7935. return nil, err
  7936. }
  7937. return &config, nil
  7938. }
  7939. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  7940. err := XTWriteDB().Create(&config).Error
  7941. return err
  7942. }
  7943. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  7944. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  7945. return err
  7946. }
  7947. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  7948. config := models.DrugStockConfig{}
  7949. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7950. return config, err
  7951. }
  7952. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  7953. stockConfig := models.PharmacyConfig{}
  7954. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  7955. return stockConfig, err
  7956. }
  7957. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  7958. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7959. return err
  7960. }
  7961. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  7962. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  7963. return err
  7964. }
  7965. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  7966. config := models.HisSettleStockConfig{}
  7967. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  7968. return config, nil
  7969. }
  7970. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  7971. info := models.GoodInformationFourty{}
  7972. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  7973. return info, err
  7974. }
  7975. func GetAllWarehouseInfo(good_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  7976. err = XTReadDB().Where("good_id = ? and user_org_id =? and status=1 and is_check =1", good_id, user_org_id).Find(&info).Error
  7977. return info, err
  7978. }
  7979. func GetHisPrescriptionByGoodId(good_id int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  7980. err = XTReadDB().Where("project_id = ? and user_org_id =? and status=1 and type =3", good_id, user_org_id).Find(&project).Error
  7981. return project, err
  7982. }
  7983. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  7984. advice := models.HisDoctorAdvice{}
  7985. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7986. return advice, err
  7987. }
  7988. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  7989. advice := models.DoctorAdvice{}
  7990. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  7991. return advice, err
  7992. }
  7993. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  7994. out := models.WarehouseOut{}
  7995. 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
  7996. return out, err
  7997. }
  7998. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  7999. config := models.XtGoodOutConfig{}
  8000. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8001. if err == gorm.ErrRecordNotFound {
  8002. return nil, err
  8003. }
  8004. if err != nil {
  8005. return nil, err
  8006. }
  8007. return &config, nil
  8008. }
  8009. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  8010. err := XTWriteDB().Create(&good).Error
  8011. return err
  8012. }
  8013. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  8014. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  8015. return err
  8016. }
  8017. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  8018. config := models.XtGoodOutConfig{}
  8019. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8020. return config, err
  8021. }
  8022. func UpdateGoodAutoConfig(user_org_id int64) error {
  8023. gobalConfig := models.GobalConfig{}
  8024. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8025. return err
  8026. }
  8027. func UpdateGoodOutConfig(user_org_id int64) error {
  8028. config := models.XtGoodOutConfig{}
  8029. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8030. return err
  8031. }
  8032. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  8033. 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
  8034. return info, err
  8035. }
  8036. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  8037. 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
  8038. return good, err
  8039. }
  8040. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  8041. 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
  8042. return err
  8043. }
  8044. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8045. 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
  8046. return err
  8047. }
  8048. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8049. 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
  8050. return err
  8051. }
  8052. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8053. out := models.WarehouseOut{}
  8054. 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
  8055. return out, err
  8056. }
  8057. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8058. out := models.WarehouseOut{}
  8059. 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
  8060. if err != gorm.ErrRecordNotFound {
  8061. if err != nil {
  8062. tx.Rollback()
  8063. return out, err
  8064. }
  8065. }
  8066. return out, err
  8067. }
  8068. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8069. out := models.WarehouseOut{}
  8070. 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
  8071. return out, err
  8072. }
  8073. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8074. out := models.WarehouseOut{}
  8075. 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
  8076. if err != gorm.ErrRecordNotFound {
  8077. if err != nil {
  8078. tx.Rollback()
  8079. return out, err
  8080. }
  8081. }
  8082. return out, err
  8083. }
  8084. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  8085. goodsType := models.GoodsType{}
  8086. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  8087. return goodsType, err
  8088. }
  8089. func GetAllGoodTypeName(good_type_id int64, org_id int64) (good_type_name string) {
  8090. goodsTypeList, _ := GetAllGoodType(org_id)
  8091. for _, item := range goodsTypeList {
  8092. if good_type_id == item.ID {
  8093. good_type_name = item.TypeName
  8094. }
  8095. }
  8096. return good_type_name
  8097. }
  8098. func UpdateGoodTypeName(good_id int64, good_type_name string) (models.GoodInformation, error) {
  8099. information := models.GoodInformation{}
  8100. err := XTWriteDB().Model(&information).Where("id = ? and status =1", good_id).Updates(map[string]interface{}{"good_type_name": good_type_name}).Error
  8101. return information, err
  8102. }
  8103. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  8104. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  8105. return info, err
  8106. }
  8107. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  8108. ut := writeDb.Begin()
  8109. 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
  8110. if err != nil {
  8111. ut.Rollback()
  8112. return err
  8113. }
  8114. ut.Commit()
  8115. return err
  8116. }
  8117. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  8118. 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
  8119. if err != nil {
  8120. tx.Rollback()
  8121. return err
  8122. }
  8123. return err
  8124. }
  8125. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  8126. info := models.WarehouseOutInfo{}
  8127. var err error
  8128. 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
  8129. if err == gorm.ErrRecordNotFound {
  8130. return nil, err
  8131. }
  8132. if err != nil {
  8133. return nil, err
  8134. }
  8135. return &info, nil
  8136. }
  8137. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  8138. info := models.WarehouseOutInfo{}
  8139. 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
  8140. return info, err
  8141. }
  8142. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8143. info := models.WarehouseOutInfo{}
  8144. 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
  8145. if err != gorm.ErrRecordNotFound {
  8146. if err != nil {
  8147. tx.Rollback()
  8148. return info, err
  8149. }
  8150. }
  8151. return info, err
  8152. }
  8153. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  8154. info := models.WarehouseOutInfo{}
  8155. 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
  8156. return info, err
  8157. }
  8158. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8159. info := models.WarehouseOutInfo{}
  8160. 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
  8161. if err != gorm.ErrRecordNotFound {
  8162. if err != nil {
  8163. tx.Rollback()
  8164. return info, err
  8165. }
  8166. }
  8167. return info, err
  8168. }
  8169. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  8170. info := models.WarehouseOutInfo{}
  8171. var err error
  8172. 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
  8173. if err == gorm.ErrRecordNotFound {
  8174. return nil, err
  8175. }
  8176. if err != nil {
  8177. return nil, err
  8178. }
  8179. return &info, nil
  8180. }
  8181. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  8182. info := models.WarehouseOutInfo{}
  8183. var err error
  8184. 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
  8185. if err != gorm.ErrRecordNotFound {
  8186. if err != nil {
  8187. tx.Rollback()
  8188. return &info, err
  8189. }
  8190. }
  8191. return &info, err
  8192. }
  8193. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  8194. db := XTReadDB().Model(&info).Where("status = 1")
  8195. if startime > 0 {
  8196. db = db.Where("ctime>=?", startime)
  8197. }
  8198. if endtime > 0 {
  8199. db = db.Where("ctime<=?", endtime)
  8200. }
  8201. if orgid > 0 {
  8202. db = db.Where("org_id = ?", orgid)
  8203. }
  8204. if len(good_id) > 0 {
  8205. db = db.Where("good_id in(?)", good_id)
  8206. }
  8207. if storehouse_id > 0 {
  8208. db = db.Where("storehouse_id = ?", storehouse_id)
  8209. }
  8210. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  8211. return info, err
  8212. }
  8213. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  8214. db := XTReadDB().Model(&info).Where("status= 1")
  8215. if start_time > 0 {
  8216. db = db.Where("ctime>=?", start_time)
  8217. }
  8218. if end_time > 0 {
  8219. db = db.Where("ctime <=?", end_time)
  8220. }
  8221. if orgid > 0 {
  8222. db = db.Where("org_id = ?", orgid)
  8223. }
  8224. if len(good_id) > 0 {
  8225. db = db.Where("good_id in(?)", good_id)
  8226. }
  8227. if storehouse_id > 0 {
  8228. db = db.Where("storehouse_id = ?", storehouse_id)
  8229. }
  8230. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  8231. return info, err
  8232. }
  8233. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  8234. config := models.XtGoodOutConfig{}
  8235. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8236. return config, err
  8237. }
  8238. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  8239. 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
  8240. 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
  8241. return err
  8242. }
  8243. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  8244. tx := XTWriteDB().Begin()
  8245. for _, item := range list {
  8246. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8247. if err != nil {
  8248. tx.Rollback()
  8249. return err
  8250. }
  8251. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8252. if err != nil {
  8253. tx.Rollback()
  8254. return err
  8255. }
  8256. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8257. if err != nil {
  8258. tx.Rollback()
  8259. return err
  8260. }
  8261. var goodList []*models.WarehousingInfo
  8262. 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
  8263. var flush_count int64
  8264. for _, it := range goodList {
  8265. flush_count += it.StockCount
  8266. }
  8267. 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
  8268. if err != nil {
  8269. tx.Rollback()
  8270. return err
  8271. }
  8272. 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
  8273. if err != nil {
  8274. tx.Rollback()
  8275. return err
  8276. }
  8277. storeHouseConfig := models.XtStorehouseConfig{}
  8278. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8279. var stockList []*models.WarehousingInfo
  8280. //查询该耗材剩余多少库存
  8281. 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
  8282. var sum_count int64
  8283. var sum_in_count int64
  8284. for _, it := range stockList {
  8285. sum_count += it.StockCount
  8286. sum_in_count += it.WarehousingCount
  8287. }
  8288. 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
  8289. if err != nil {
  8290. tx.Rollback()
  8291. return err
  8292. }
  8293. 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
  8294. if err != nil {
  8295. tx.Rollback()
  8296. return err
  8297. }
  8298. }
  8299. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8300. if err != nil {
  8301. tx.Rollback()
  8302. return err
  8303. }
  8304. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8305. tx.Commit()
  8306. return err
  8307. }
  8308. func UpdateNewWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64, tx *gorm.DB) error {
  8309. for _, item := range list {
  8310. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8311. if err != nil {
  8312. tx.Rollback()
  8313. return err
  8314. }
  8315. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8316. if err != nil {
  8317. tx.Rollback()
  8318. return err
  8319. }
  8320. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8321. if err != nil {
  8322. tx.Rollback()
  8323. return err
  8324. }
  8325. var goodList []*models.WarehousingInfo
  8326. 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
  8327. var flush_count int64
  8328. for _, it := range goodList {
  8329. flush_count += it.StockCount
  8330. }
  8331. 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
  8332. if err != nil {
  8333. tx.Rollback()
  8334. return err
  8335. }
  8336. 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
  8337. if err != nil {
  8338. tx.Rollback()
  8339. return err
  8340. }
  8341. storeHouseConfig := models.XtStorehouseConfig{}
  8342. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8343. var stockList []*models.WarehousingInfo
  8344. //查询该耗材剩余多少库存
  8345. 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
  8346. var sum_count int64
  8347. var sum_in_count int64
  8348. for _, it := range stockList {
  8349. sum_count += it.StockCount
  8350. sum_in_count += it.WarehousingCount
  8351. }
  8352. 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
  8353. if err != nil {
  8354. tx.Rollback()
  8355. return err
  8356. }
  8357. 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
  8358. if err != nil {
  8359. tx.Rollback()
  8360. return err
  8361. }
  8362. }
  8363. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8364. if err != nil {
  8365. tx.Rollback()
  8366. return err
  8367. }
  8368. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8369. return err
  8370. }
  8371. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8372. if storehouse_id == 0 {
  8373. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  8374. }
  8375. if storehouse_id > 0 {
  8376. 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
  8377. }
  8378. return info, err
  8379. }
  8380. func FindFirstWarehousingInfoByStockFour(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8381. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").Last(&info).Error
  8382. return info, err
  8383. }
  8384. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  8385. 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
  8386. return list, err
  8387. }
  8388. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  8389. 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
  8390. return infor, err
  8391. }
  8392. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  8393. info := models.WarehouseOutInfo{}
  8394. 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
  8395. return info, err
  8396. }
  8397. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  8398. info := models.WarehousingInfo{}
  8399. 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
  8400. return info, err
  8401. }
  8402. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8403. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8404. return list, err
  8405. }
  8406. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8407. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8408. return list, err
  8409. }
  8410. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  8411. templateL := models.HisPrescriptionProjectTemplateL{}
  8412. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  8413. return templateL, err
  8414. }
  8415. func ModifyNewGoodWarehouseInfoStockMinNumber(count int64, goodid int64, orgid int64, id int64, tx *gorm.DB) error {
  8416. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and good_id = ? and status = 1", id, orgid, goodid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  8417. if err != nil {
  8418. tx.Rollback()
  8419. return err
  8420. }
  8421. return err
  8422. }
  8423. func GetDrugInvetoryByDrugId(user_org_id int64, drug_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  8424. err = XTReadDB().Where("org_id = ? and drug_id = ? and status=1", user_org_id, drug_id).Preload("XtBaseDrug", "org_id = ? and status=1", user_org_id).Order("id desc").Find(&info).Error
  8425. return info, err
  8426. }
  8427. func UpdateNewDrugWarehouseInfoById(id int64, last_stock_max_number int64, last_stock_min_number int64) error {
  8428. err := XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"stock_max_number": last_stock_max_number, "stock_min_number": last_stock_min_number}).Error
  8429. return err
  8430. }
  8431. func GeteSelfPrescriptionOpen(user_org_id int64) (models.XtSelfDrugInfoConfig, error) {
  8432. drugInfoConfig := models.XtSelfDrugInfoConfig{}
  8433. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugInfoConfig).Error
  8434. return drugInfoConfig, err
  8435. }
  8436. func CreaterSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8437. err := XTWriteDB().Create(&infoConfig).Error
  8438. return err
  8439. }
  8440. func SaveSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8441. err := XTWriteDB().Save(&infoConfig).Error
  8442. return err
  8443. }
  8444. func GetSelfPrescriptionOutOpen(user_org_id int64) (models.XtSelfDrugOutConfig, error) {
  8445. drugOutConfig := models.XtSelfDrugOutConfig{}
  8446. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugOutConfig).Error
  8447. return drugOutConfig, err
  8448. }
  8449. func CreateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8450. err := XTWriteDB().Create(&config).Error
  8451. return err
  8452. }
  8453. func UpdateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8454. err := XTWriteDB().Save(&config).Error
  8455. return err
  8456. }
  8457. func CreateSelfDrugFlowOne(flow models.XtSelfDrugFlow, tx *gorm.DB) error {
  8458. err := tx.Create(&flow).Error
  8459. if err != nil {
  8460. tx.Rollback()
  8461. return err
  8462. }
  8463. return err
  8464. }
  8465. func UpdateWarehouseOutInfoByPrice(good_id int64, packing_price float64) error {
  8466. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("good_id =? and status =1 and is_check=1", good_id).Updates(map[string]interface{}{"packing_price": packing_price}).Error
  8467. return err
  8468. }
  8469. func GetWarehosueOutInfoByPrice(good_id int64, org_id int64) (out []*models.WarehouseOutInfo, err error) {
  8470. err = XTReadDB().Where("good_id = ? and org_id = ? and status=1 and price =0", good_id, org_id).Find(&out).Error
  8471. return out, err
  8472. }
  8473. func UpdateWarehouseOut(good_id int64, packing_price float64) error {
  8474. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  8475. return err
  8476. }
  8477. func UpdateStockFlowByList(good_id int64, packing_price float64) error {
  8478. err := XTWriteDB().Model(models.VmStockFlow{}).Where("good_id = ? and user_org_id = 10633 and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  8479. return err
  8480. }
  8481. func GetGoodStockInventoryByRecordate(good_id int64, record_date int64, user_org_id int64) (models.XtGoodStockInventoryList, error) {
  8482. goodStockInventoryList := models.XtGoodStockInventoryList{}
  8483. err := XTReadDB().Where("good_id = ? and record_date = ? and user_org_id = ? and status=1", good_id, record_date, user_org_id).Find(&goodStockInventoryList).Error
  8484. return goodStockInventoryList, err
  8485. }
  8486. func CreateGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8487. err := XTWriteDB().Create(&list).Error
  8488. return err
  8489. }
  8490. func SaveGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8491. err := XTWriteDB().Save(&list).Error
  8492. return err
  8493. }
  8494. func GetGoodStockInventoryList(keyword string, limit int64, page int64, storehouse_id int64, startTime int64, endTime int64, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8495. likeKey := "%" + keyword + "%"
  8496. offset := (page - 1) * limit
  8497. db := XTReadDB().Model(&models.XtGoodStockInventoryList{}).Where("status=1")
  8498. if storehouse_id > 0 {
  8499. db = db.Where("storehouse_id = ?", storehouse_id)
  8500. }
  8501. if startTime > 0 {
  8502. db = db.Where("record_date >=?", startTime)
  8503. }
  8504. if endTime > 0 {
  8505. db = db.Where("record_date<=?", endTime)
  8506. }
  8507. if len(keyword) > 0 {
  8508. db = db.Where("good_name like ? or specification_name like ?", likeKey, likeKey)
  8509. }
  8510. if orgId > 0 {
  8511. db = db.Where("user_org_id = ?", orgId)
  8512. }
  8513. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8514. return list, total, err
  8515. }
  8516. func GetDrugStockInventoryList(drug_id int64, record_date int64, user_org_id int64) (models.XtDrugStockInventoryList, error) {
  8517. drugStockInventoryList := models.XtDrugStockInventoryList{}
  8518. err := XTReadDB().Where("drug_id = ? and record_date = ? and user_org_id = ? and status=1", drug_id, record_date, user_org_id).Find(&drugStockInventoryList).Error
  8519. return drugStockInventoryList, err
  8520. }
  8521. func CreatedDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8522. err := XTWriteDB().Create(&list).Error
  8523. return err
  8524. }
  8525. func SaveDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8526. err := XTWriteDB().Save(&list).Error
  8527. return err
  8528. }
  8529. func GetStockInventoryPrintList(storehouse_id int64, keyword string, limit int64, page int64, ids []string, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8530. db := XTReadDB().Model(&list).Where("status=1")
  8531. likeKey := "%" + keyword + "%"
  8532. offset := (page - 1) * limit
  8533. if storehouse_id > 0 {
  8534. db = db.Where("storehouse_id = ?", storehouse_id)
  8535. }
  8536. if len(keyword) > 0 {
  8537. db = db.Where("good_name like ?", likeKey)
  8538. }
  8539. if orgId > 0 {
  8540. db = db.Where("user_org_id = ?", orgId)
  8541. }
  8542. if len(ids) > 0 {
  8543. db = db.Where("id in(?)", ids)
  8544. }
  8545. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8546. return list, total, err
  8547. }
  8548. func GetGoodWarehouseInfoList(startTime int64, endTime int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  8549. db := XTReadDB().Model(&info).Where("status=1 and is_check=1")
  8550. if startTime > 0 {
  8551. db = db.Where("ctime>=?", startTime)
  8552. }
  8553. if endTime > 0 {
  8554. db = db.Where("ctime<=?", endTime)
  8555. }
  8556. if orgId > 0 {
  8557. db = db.Where("org_id = ?", orgId)
  8558. }
  8559. err = db.Find(&info).Error
  8560. return info, err
  8561. }
  8562. func GetGoodWarhouseOutList(startTime int64, endTime int64, orgId int64) (outinfo []*models.WarehouseOutInfo, err error) {
  8563. db := XTReadDB().Model(&outinfo).Where("status=1 and is_check =1")
  8564. if startTime > 0 {
  8565. db = db.Where("ctime>=?", startTime)
  8566. }
  8567. if endTime > 0 {
  8568. db = db.Where("ctime<=?", endTime)
  8569. }
  8570. if orgId > 0 {
  8571. db = db.Where("org_id = ?", orgId)
  8572. }
  8573. err = db.Find(&outinfo).Error
  8574. return outinfo, err
  8575. }
  8576. func GetGoodStockFlowProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8577. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 10")
  8578. if startTime > 0 {
  8579. db = db.Where("ctime>=?", startTime)
  8580. }
  8581. if endTime > 0 {
  8582. db = db.Where("ctime<=?", endTime)
  8583. }
  8584. if orgId > 0 {
  8585. db = db.Where("user_org_id = ?", orgId)
  8586. }
  8587. err = db.Find(&flow).Error
  8588. return flow, err
  8589. }
  8590. func GetGoodStockFlowNoProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8591. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 11")
  8592. if startTime > 0 {
  8593. db = db.Where("ctime>=?", startTime)
  8594. }
  8595. if endTime > 0 {
  8596. db = db.Where("ctime<=?", endTime)
  8597. }
  8598. if orgId > 0 {
  8599. db = db.Where("user_org_id = ?", orgId)
  8600. }
  8601. err = db.Find(&flow).Error
  8602. return flow, err
  8603. }
  8604. func GetCancelStockFlowList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8605. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 7")
  8606. if startTime > 0 {
  8607. db = db.Where("ctime>=?", startTime)
  8608. }
  8609. if endTime > 0 {
  8610. db = db.Where("ctime<=?", endTime)
  8611. }
  8612. if orgId > 0 {
  8613. db = db.Where("user_org_id = ?", orgId)
  8614. }
  8615. err = db.Find(&flow).Error
  8616. return flow, err
  8617. }
  8618. func GetLastMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8619. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 1 or consumable_type = 10)")
  8620. if startTime > 0 {
  8621. db = db.Where("ctime <?", startTime)
  8622. }
  8623. if orgId > 0 {
  8624. db = db.Where("user_org_id = ?", orgId)
  8625. }
  8626. err = db.Find(&flow).Error
  8627. return flow, err
  8628. }
  8629. func GetLastMonthOverCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8630. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 2 or consumable_type=3 or consumable_type=11)")
  8631. if startTime > 0 {
  8632. db = db.Where("ctime <?", startTime)
  8633. }
  8634. if orgId > 0 {
  8635. db = db.Where("user_org_id = ?", orgId)
  8636. }
  8637. err = db.Find(&flow).Error
  8638. return flow, err
  8639. }
  8640. func GetLastCancelMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8641. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 7)")
  8642. if startTime > 0 {
  8643. db = db.Where("ctime <?", startTime)
  8644. }
  8645. if orgId > 0 {
  8646. db = db.Where("user_org_id = ?", orgId)
  8647. }
  8648. err = db.Find(&flow).Error
  8649. return flow, err
  8650. }
  8651. func GetDrugCodeOpen(user_org_id int64) (models.XtDrugCodeOpen, error) {
  8652. drugCodeOpen := models.XtDrugCodeOpen{}
  8653. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugCodeOpen).Error
  8654. return drugCodeOpen, err
  8655. }
  8656. func CreateDrugOpen(open models.XtDrugCodeOpen) error {
  8657. err := XTWriteDB().Create(&open).Error
  8658. return err
  8659. }
  8660. func SaveDrugOpen(open models.XtDrugCodeOpen) error {
  8661. err := XTWriteDB().Save(&open).Error
  8662. return err
  8663. }
  8664. func GetHisDrugCodeQuery(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64) (list []*models.HisDoctorAdviceInfo, total int64, err error) {
  8665. db := XTReadDB().Model(&models.HisDoctorAdviceInfo{}).Where("status=1")
  8666. if orgId > 0 {
  8667. db = db.Where("user_org_id=?", orgId)
  8668. }
  8669. if start_time > 0 {
  8670. db = db.Where("advice_date >= ?", start_time)
  8671. }
  8672. if end_time > 0 {
  8673. db = db.Where("advice_date<=?", end_time)
  8674. }
  8675. offset := (page - 1) * limit
  8676. if is_sale == 1 {
  8677. db = db.Where("is_upload = 1")
  8678. }
  8679. if is_sale == 2 {
  8680. db = db.Where("is_upload = 2 or is_upload =0")
  8681. }
  8682. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8683. fmt.Println("totla====================", total)
  8684. return list, total, err
  8685. }
  8686. func GetDrugWarehouseOutInfoByDrugCode(drug_id int64, patient_id int64, advice_date int64, user_org_id int64) (outinfo []*models.XtDrugWarehouseOutInfo, err error) {
  8687. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time =? and org_id = ? and status =1", drug_id, patient_id, advice_date, user_org_id).Find(&outinfo).Error
  8688. return outinfo, err
  8689. }