stock_service.go 366KB

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