stock_service.go 458KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "github.com/jinzhu/gorm"
  10. )
  11. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  12. err := writeDb.Create(&dealer).Error
  13. return err, dealer
  14. }
  15. func ModifyDealer(dealer *models.Dealer) error {
  16. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  17. "dealer_name": dealer.DealerName,
  18. "contact": dealer.Contact,
  19. "contact_phone": dealer.ContactPhone,
  20. "platform_number": dealer.PlatformNumber,
  21. "email": dealer.Email,
  22. "contact_address": dealer.ContactAddress,
  23. "modifier": dealer.Modifier,
  24. "mtime": time.Now().Unix(),
  25. "remark": dealer.Remark,
  26. "wubi": dealer.WuBi,
  27. "pinyin": dealer.PinYin,
  28. }).Error
  29. return err
  30. }
  31. func FindAllDealerTotal(orgId int64) (total int64) {
  32. db := readDb.Model(&models.Dealer{})
  33. db = db.Where("org_id = ? ", orgId).Count(&total)
  34. return
  35. }
  36. // 获取经销商的名称
  37. func FindAllDealerName(orgId int64) (tmp []*models.Dealer, err error) {
  38. err = readDb.Model(&models.Dealer{}).Where("org_id = ? and status = 1", orgId).Find(&tmp).Error
  39. return
  40. }
  41. func FindAllDealerList(orgId int64, page int64, limit, key int64) (list []*models.Dealer, total int64, err error) {
  42. offset := (page - 1) * limit
  43. db := readDb.Model(&models.Dealer{})
  44. db = db.Where("org_id = ? AND status = 1", orgId)
  45. if key != 0 {
  46. db = db.Where("id = ? ", key)
  47. offset = 0
  48. limit = 10
  49. }
  50. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  51. return
  52. }
  53. func DeleteDealerById(id int64) error {
  54. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  55. return err
  56. }
  57. func FindDealerById(id int64) (*models.Dealer, error) {
  58. dealer := &models.Dealer{}
  59. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  60. return dealer, err
  61. }
  62. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  63. err := writeDb.Create(&manufacturer).Error
  64. return err, manufacturer
  65. }
  66. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  67. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  68. "manufacturer_name": manufacturer.ManufacturerName,
  69. "contact": manufacturer.Contact,
  70. "contact_phone": manufacturer.ContactPhone,
  71. "platform_number": manufacturer.PlatformNumber,
  72. "email": manufacturer.Email,
  73. "contact_address": manufacturer.ContactAddress,
  74. "modifier": manufacturer.Modifier,
  75. "mtime": time.Now().Unix(),
  76. "remark": manufacturer.Remark,
  77. "wubi": manufacturer.WuBi,
  78. "pinyin": manufacturer.PinYin,
  79. }).Error
  80. return err
  81. }
  82. // 获取生产厂商名称
  83. func FindAllManufacturerName(orgid int64) (tmp []*models.Manufacturer, err error) {
  84. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? and status = 1", orgid).Find(&tmp).Error
  85. return
  86. }
  87. func FindAllManufacturerList(orgId int64, page int64, limit, key int64) (list []*models.Manufacturer, total int64, err error) {
  88. offset := (page - 1) * limit
  89. db := readDb.Model(&models.Manufacturer{})
  90. db = db.Where("org_id = ? AND status = 1", orgId)
  91. if key != 0 {
  92. db = db.Where("id = ?", key)
  93. offset = 0
  94. limit = 10
  95. }
  96. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  97. return
  98. }
  99. func DeleteManufacturerById(id int64) error {
  100. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  101. return err
  102. }
  103. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  104. dealer := &models.Manufacturer{}
  105. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  106. return dealer, err
  107. }
  108. func FindManufacturerTotal(orgId int64) (total int64) {
  109. db := readDb.Model(&models.Manufacturer{})
  110. db = db.Where("org_id = ?", orgId).Count(&total)
  111. return
  112. }
  113. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  114. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  115. return dealers, err
  116. }
  117. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  118. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  119. return manufacturer, err
  120. }
  121. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  122. db := readDb.Model(&models.GoodsType{})
  123. db = db.Where("org_id = ?", orgId).Count(&total)
  124. return
  125. }
  126. func GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  127. goodsType := models.GoodsType{}
  128. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  129. if err == gorm.ErrRecordNotFound {
  130. return nil, err
  131. }
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &goodsType, nil
  136. }
  137. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  138. goodsType := models.GoodsType{}
  139. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  140. if err == gorm.ErrRecordNotFound {
  141. return nil, err
  142. }
  143. if err != nil {
  144. return nil, err
  145. }
  146. return &goodsType, nil
  147. }
  148. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  149. err := writeDb.Create(&goodType).Error
  150. return err, goodType
  151. }
  152. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  153. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  154. "modifier": goodType.Modifier,
  155. "mtime": time.Now().Unix(),
  156. "remark": goodType.Remark,
  157. "type_name": goodType.TypeName,
  158. "out_stock": goodType.OutStock,
  159. "stock_attribute": goodType.StockAttribute,
  160. }).Error
  161. return err, goodType
  162. }
  163. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  164. offset := (page - 1) * limit
  165. db := readDb.Model(&models.GoodsType{})
  166. db = db.Where("org_id=? AND status = 1", orgId)
  167. if len(keyword) > 0 {
  168. likeKey := "%" + keyword + "%"
  169. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  170. }
  171. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  172. return
  173. }
  174. func DeleteGoodTypeById(id int64, operateId int64) error {
  175. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  176. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  177. return err
  178. }
  179. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  180. goodType := &models.GoodsType{}
  181. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  182. return goodType, err
  183. }
  184. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  185. db := readDb.Model(&models.GoodInfo{})
  186. db = db.Where("org_id = ?", orgId).Count(&total)
  187. return
  188. }
  189. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  190. err := writeDb.Create(&goodInfo).Error
  191. return err, goodInfo
  192. }
  193. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  194. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  195. "good_type_id": goodInfo.GoodTypeId,
  196. "modifier": goodInfo.Modifier,
  197. "mtime": time.Now().Unix(),
  198. "remark": goodInfo.Remark,
  199. "specification_name": goodInfo.SpecificationName,
  200. "good_unit": goodInfo.GoodUnit,
  201. "buy_price": goodInfo.BuyPrice,
  202. "sell_price": goodInfo.SellPrice,
  203. "manufacturer": goodInfo.Manufacturer,
  204. "dealer": goodInfo.Dealer,
  205. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  206. "stock_warn_count": goodInfo.StockWarnCount,
  207. "is_reuse": goodInfo.IsReuse,
  208. "good_name": goodInfo.GoodName,
  209. "pinyin": goodInfo.Pinyin,
  210. "wubi": goodInfo.Wubi,
  211. "good_kind": goodInfo.GoodKind,
  212. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  213. "retail_price": goodInfo.PackingPrice,
  214. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  215. "is_special_diseases": goodInfo.IsSpecialDiseases,
  216. "is_record": goodInfo.IsRecord,
  217. "statistics_category": goodInfo.StatisticsCategory,
  218. "good_status": goodInfo.GoodStatus,
  219. "default_count": goodInfo.DefaultCount,
  220. "sign": goodInfo.Sign,
  221. "is_default": goodInfo.IsDefault,
  222. "is_charge_use": goodInfo.IsChargeUse,
  223. "is_charge_predict": goodInfo.IsChargePredict,
  224. "is_statistics_work": goodInfo.IsStatisticsWork,
  225. "sort": goodInfo.Sort,
  226. "is_doctor_use": goodInfo.IsDoctorUse,
  227. "agent": goodInfo.Agent,
  228. "good_number": goodInfo.GoodNumber,
  229. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  230. "special_medical": goodInfo.SpecialMedical,
  231. "production_type": goodInfo.ProductionType,
  232. "min_number": goodInfo.MinNumber,
  233. "packing_unit": goodInfo.PackingUnit,
  234. "packing_price": goodInfo.PackingPrice,
  235. "default_count_unit": goodInfo.DefaultCountUnit,
  236. "min_unit": goodInfo.MinUnit,
  237. "register_number": goodInfo.RegisterNumber,
  238. "provinces_code": goodInfo.ProvincesCode,
  239. "is_user": goodInfo.IsUser,
  240. "number": goodInfo.Number,
  241. "is_warehouse": goodInfo.IsWarehouse,
  242. "bbx01": goodInfo.Bbx01,
  243. "bby01": goodInfo.Bby01,
  244. "zuobiao_id": goodInfo.Bbx01,
  245. "first_letter": goodInfo.FirstLetter,
  246. "is_show": goodInfo.IsShow,
  247. "is_zero_flag": goodInfo.IsZeroFlag,
  248. "split_of_number": goodInfo.SplitOfNumber,
  249. }).Error
  250. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  251. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  252. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  253. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  254. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  255. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  256. return err, goodInfo
  257. }
  258. 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) {
  259. offset := (page - 1) * limit
  260. db := readDb.Model(&models.GoodInfo{})
  261. db = db.Where("org_id = ? AND status = 1", orgId)
  262. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  263. if len(keyword) > 0 {
  264. likeKey := "%" + keyword + "%"
  265. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  266. if is_use > 0 {
  267. if is_use == 2 {
  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 is_charge > 0 {
  274. if is_charge == 1 {
  275. db = db.Where("find_in_set('收费',good_status) > 0")
  276. } else {
  277. db = db.Where("find_in_set('收费',good_status) = 0")
  278. }
  279. }
  280. if good_kind > 0 {
  281. db = db.Where(" good_kind = ?", good_kind)
  282. }
  283. if is_mark > 0 {
  284. db = db.Where("is_mark = ?", is_mark)
  285. }
  286. if manufacturer > 0 {
  287. db = db.Where("manufacturer = ?", manufacturer)
  288. }
  289. if is_type > 0 {
  290. db = db.Where("good_type_id = ?", is_type)
  291. }
  292. } else {
  293. if is_use > 0 {
  294. if is_use == 2 {
  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 is_charge > 0 {
  301. if is_charge == 1 {
  302. db = db.Where("find_in_set('收费',good_status) > 0")
  303. } else {
  304. db = db.Where("find_in_set('收费',good_status) = 0")
  305. }
  306. }
  307. if good_kind > 0 {
  308. db = db.Where(" good_kind = ?", good_kind)
  309. }
  310. if is_mark > 0 {
  311. db = db.Where("is_mark = ?", is_mark)
  312. }
  313. if manufacturer > 0 {
  314. db = db.Where("manufacturer = ?", manufacturer)
  315. }
  316. if is_type > 0 {
  317. db = db.Where("good_type_id = ?", is_type)
  318. }
  319. }
  320. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  321. return
  322. }
  323. func DeleteGoodInfoById(id int64, operateId int64) error {
  324. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  325. return err
  326. }
  327. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  328. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  329. return list, err
  330. }
  331. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  332. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  333. return list, err
  334. }
  335. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  336. goodInfo := &models.GoodInfo{}
  337. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  338. return goodInfo, err
  339. }
  340. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  341. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  342. return goodType, err
  343. }
  344. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  345. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  346. return goodType, err
  347. }
  348. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  349. 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
  350. return goodInfo, err
  351. }
  352. func FindAllGoodInfoTwo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  353. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  354. return goodInfo, err
  355. }
  356. func FindAllGoodInfoThree(org_id int64) (goodInfo []*models.XtGoodInformationFourty, err error) {
  357. err = XTReadDB().Model(&models.XtGoodInformationFourty{}).Where("org_id = ? AND status = 1 and find_in_set('停用',good_status) = 0", org_id).Find(&goodInfo).Error
  358. return goodInfo, err
  359. }
  360. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  361. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  362. return total, err
  363. }
  364. func FindNewAllWarehouseTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  365. err = tx.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  366. if err != gorm.ErrRecordNotFound {
  367. if err != nil {
  368. tx.Rollback()
  369. return total, err
  370. }
  371. }
  372. return total, err
  373. }
  374. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  375. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  376. return total, err
  377. }
  378. func FindNewAllWarehouseTotalOne(org_id int64, tx *gorm.DB) (total int64, err error) {
  379. err = tx.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  380. if err != gorm.ErrRecordNotFound {
  381. if err != nil {
  382. tx.Rollback()
  383. return total, err
  384. }
  385. }
  386. return total, err
  387. }
  388. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  389. err := writeDb.Create(&warehouse).Error
  390. return err
  391. }
  392. func AddNewSigleWarehouse(warehouse *models.Warehousing, tx *gorm.DB) error {
  393. err := writeDb.Create(&warehouse).Error
  394. if err != nil {
  395. tx.Rollback()
  396. return err
  397. }
  398. return err
  399. }
  400. func UpdateWarehouse(id int64, storehouse_id int64) error {
  401. err := XTWriteDB().Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  402. return err
  403. }
  404. func UpdateNewWarehouse(id int64, storehouse_id int64, tx *gorm.DB) error {
  405. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  406. if err != nil {
  407. tx.Rollback()
  408. return err
  409. }
  410. return err
  411. }
  412. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  413. err := writeDb.Create(&warehouse).Error
  414. return err
  415. }
  416. func AddNewSigleDrugWarehouse(warehouse *models.DrugWarehouse, tx *gorm.DB) error {
  417. err := tx.Create(&warehouse).Error
  418. if err != nil {
  419. tx.Rollback()
  420. return err
  421. }
  422. return err
  423. }
  424. func UpdateSigleDrugWarehouse(id int64, storehosue_id int64) error {
  425. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  426. return err
  427. }
  428. func UpdateNewSigleDrugWarehouse(id int64, storehosue_id int64, tx *gorm.DB) error {
  429. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehosue_id}).Error
  430. if err != nil {
  431. tx.Rollback()
  432. return err
  433. }
  434. return err
  435. }
  436. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  437. err := writeDb.Create(&salesReturn).Error
  438. return err
  439. }
  440. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  441. err := writeDb.Create(&salesReturn).Error
  442. return err
  443. }
  444. func UpDateGoodReturnStatus(ids []string) {
  445. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  446. }
  447. func UpDateCanCelStockStatus(ids []string) {
  448. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  449. }
  450. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  451. if len(warehousingInfo) > 0 {
  452. utx := writeDb.Begin()
  453. if len(warehousingInfo) > 0 {
  454. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number,packing_price,storehouse_id,good_code) VALUES "
  455. insertParams := make([]string, 0)
  456. insertData := make([]interface{}, 0)
  457. for _, info := range warehousingInfo {
  458. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  459. insertData = append(insertData, info.WarehousingId)
  460. insertData = append(insertData, info.GoodId)
  461. insertData = append(insertData, info.GoodTypeId)
  462. insertData = append(insertData, info.Number)
  463. insertData = append(insertData, info.ProductDate)
  464. insertData = append(insertData, info.ExpiryDate)
  465. insertData = append(insertData, info.WarehousingCount)
  466. insertData = append(insertData, info.Price)
  467. insertData = append(insertData, info.TotalPrice)
  468. insertData = append(insertData, info.Dealer)
  469. insertData = append(insertData, info.Manufacturer)
  470. insertData = append(insertData, info.Remark)
  471. insertData = append(insertData, info.Ctime)
  472. insertData = append(insertData, info.Mtime)
  473. insertData = append(insertData, info.Status)
  474. insertData = append(insertData, info.OrgId)
  475. insertData = append(insertData, info.WarehousingOrder)
  476. insertData = append(insertData, info.Type)
  477. insertData = append(insertData, info.WarehousingCount)
  478. insertData = append(insertData, info.LicenseNumber)
  479. insertData = append(insertData, info.PackingPrice)
  480. insertData = append(insertData, info.StorehouseId)
  481. insertData = append(insertData, info.GoodCode)
  482. }
  483. thisSQL += strings.Join(insertParams, ", ")
  484. err = utx.Exec(thisSQL, insertData...).Error
  485. if err != nil {
  486. utx.Rollback()
  487. return
  488. }
  489. }
  490. utx.Commit()
  491. }
  492. return
  493. }
  494. func CreateNewWarehousingInfo(warehousingInfo []*models.WarehousingInfo, tx *gorm.DB) (err error) {
  495. if len(warehousingInfo) > 0 {
  496. if len(warehousingInfo) > 0 {
  497. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type,stock_count,license_number,packing_price,storehouse_id,good_code) VALUES "
  498. insertParams := make([]string, 0)
  499. insertData := make([]interface{}, 0)
  500. for _, info := range warehousingInfo {
  501. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  502. insertData = append(insertData, info.WarehousingId)
  503. insertData = append(insertData, info.GoodId)
  504. insertData = append(insertData, info.GoodTypeId)
  505. insertData = append(insertData, info.Number)
  506. insertData = append(insertData, info.ProductDate)
  507. insertData = append(insertData, info.ExpiryDate)
  508. insertData = append(insertData, info.WarehousingCount)
  509. insertData = append(insertData, info.Price)
  510. insertData = append(insertData, info.TotalPrice)
  511. insertData = append(insertData, info.Dealer)
  512. insertData = append(insertData, info.Manufacturer)
  513. insertData = append(insertData, info.Remark)
  514. insertData = append(insertData, info.Ctime)
  515. insertData = append(insertData, info.Mtime)
  516. insertData = append(insertData, info.Status)
  517. insertData = append(insertData, info.OrgId)
  518. insertData = append(insertData, info.WarehousingOrder)
  519. insertData = append(insertData, info.Type)
  520. insertData = append(insertData, info.WarehousingCount)
  521. insertData = append(insertData, info.LicenseNumber)
  522. insertData = append(insertData, info.PackingPrice)
  523. insertData = append(insertData, info.StorehouseId)
  524. insertData = append(insertData, info.GoodCode)
  525. }
  526. thisSQL += strings.Join(insertParams, ", ")
  527. err = tx.Exec(thisSQL, insertData...).Error
  528. if err != nil {
  529. tx.Rollback()
  530. return
  531. }
  532. }
  533. }
  534. return
  535. }
  536. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  537. if len(warehousingInfo) > 0 {
  538. utx := writeDb.Begin()
  539. if len(warehousingInfo) > 0 {
  540. 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 "
  541. insertParams := make([]string, 0)
  542. insertData := make([]interface{}, 0)
  543. for _, info := range warehousingInfo {
  544. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  545. insertData = append(insertData, info.WarehousingId)
  546. insertData = append(insertData, info.DrugId)
  547. insertData = append(insertData, info.Number)
  548. insertData = append(insertData, info.ProductDate)
  549. insertData = append(insertData, info.ExpiryDate)
  550. insertData = append(insertData, info.WarehousingCount)
  551. insertData = append(insertData, info.Price)
  552. insertData = append(insertData, info.TotalPrice)
  553. insertData = append(insertData, info.Dealer)
  554. insertData = append(insertData, info.Manufacturer)
  555. insertData = append(insertData, info.Remark)
  556. insertData = append(insertData, info.Ctime)
  557. insertData = append(insertData, info.Mtime)
  558. insertData = append(insertData, info.Status)
  559. insertData = append(insertData, info.OrgId)
  560. insertData = append(insertData, info.WarehousingOrder)
  561. insertData = append(insertData, info.Type)
  562. insertData = append(insertData, info.RetailPrice)
  563. insertData = append(insertData, info.RetailTotalPrice)
  564. insertData = append(insertData, info.StockMaxNumber)
  565. insertData = append(insertData, info.MaxUnit)
  566. insertData = append(insertData, info.MinUnit)
  567. insertData = append(insertData, info.BatchNumber)
  568. insertData = append(insertData, info.StockMinNumber)
  569. insertData = append(insertData, info.StorehouseId)
  570. }
  571. thisSQL += strings.Join(insertParams, ", ")
  572. err = utx.Exec(thisSQL, insertData...).Error
  573. if err != nil {
  574. utx.Rollback()
  575. return
  576. }
  577. }
  578. utx.Commit()
  579. }
  580. return
  581. }
  582. func CreateNewDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo, tx *gorm.DB) (err error) {
  583. if len(warehousingInfo) > 0 {
  584. if len(warehousingInfo) > 0 {
  585. 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,drug_code) VALUES "
  586. insertParams := make([]string, 0)
  587. insertData := make([]interface{}, 0)
  588. for _, info := range warehousingInfo {
  589. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  590. insertData = append(insertData, info.WarehousingId)
  591. insertData = append(insertData, info.DrugId)
  592. insertData = append(insertData, info.Number)
  593. insertData = append(insertData, info.ProductDate)
  594. insertData = append(insertData, info.ExpiryDate)
  595. insertData = append(insertData, info.WarehousingCount)
  596. insertData = append(insertData, info.Price)
  597. insertData = append(insertData, info.TotalPrice)
  598. insertData = append(insertData, info.Dealer)
  599. insertData = append(insertData, info.Manufacturer)
  600. insertData = append(insertData, info.Remark)
  601. insertData = append(insertData, info.Ctime)
  602. insertData = append(insertData, info.Mtime)
  603. insertData = append(insertData, info.Status)
  604. insertData = append(insertData, info.OrgId)
  605. insertData = append(insertData, info.WarehousingOrder)
  606. insertData = append(insertData, info.Type)
  607. insertData = append(insertData, info.RetailPrice)
  608. insertData = append(insertData, info.RetailTotalPrice)
  609. insertData = append(insertData, info.StockMaxNumber)
  610. insertData = append(insertData, info.MaxUnit)
  611. insertData = append(insertData, info.MinUnit)
  612. insertData = append(insertData, info.BatchNumber)
  613. insertData = append(insertData, info.StockMinNumber)
  614. insertData = append(insertData, info.StorehouseId)
  615. insertData = append(insertData, info.DrugCode)
  616. }
  617. thisSQL += strings.Join(insertParams, ", ")
  618. err = tx.Exec(thisSQL, insertData...).Error
  619. if err != nil {
  620. tx.Rollback()
  621. return
  622. }
  623. }
  624. }
  625. return
  626. }
  627. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  628. if len(warehousingInfo) > 0 {
  629. utx := writeDb.Begin()
  630. if len(warehousingInfo) > 0 {
  631. 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 "
  632. insertParams := make([]string, 0)
  633. insertData := make([]interface{}, 0)
  634. for _, info := range warehousingInfo {
  635. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  636. insertData = append(insertData, info.WarehousingId)
  637. insertData = append(insertData, info.DrugId)
  638. insertData = append(insertData, info.Number)
  639. insertData = append(insertData, info.ProductDate)
  640. insertData = append(insertData, info.ExpiryDate)
  641. insertData = append(insertData, info.WarehousingCount)
  642. insertData = append(insertData, info.Price)
  643. insertData = append(insertData, info.TotalPrice)
  644. insertData = append(insertData, info.Dealer)
  645. insertData = append(insertData, info.Manufacturer)
  646. insertData = append(insertData, info.Remark)
  647. insertData = append(insertData, info.Ctime)
  648. insertData = append(insertData, info.Mtime)
  649. insertData = append(insertData, info.Status)
  650. insertData = append(insertData, info.OrgId)
  651. insertData = append(insertData, info.WarehousingOrder)
  652. insertData = append(insertData, info.Type)
  653. insertData = append(insertData, info.RetailPrice)
  654. insertData = append(insertData, info.RetailTotalPrice)
  655. insertData = append(insertData, info.StockMaxNumber)
  656. insertData = append(insertData, info.MaxUnit)
  657. insertData = append(insertData, info.MinUnit)
  658. insertData = append(insertData, info.BatchNumber)
  659. insertData = append(insertData, info.StockMinNumber)
  660. insertData = append(insertData, info.SupplyWarehouseId)
  661. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  662. insertData = append(insertData, info.StorehouseId)
  663. insertData = append(insertData, info.IsCheck)
  664. }
  665. thisSQL += strings.Join(insertParams, ", ")
  666. err = utx.Exec(thisSQL, insertData...).Error
  667. if err != nil {
  668. utx.Rollback()
  669. return
  670. }
  671. }
  672. utx.Commit()
  673. }
  674. return
  675. }
  676. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  677. var total float64
  678. var min_number int64
  679. for _, item := range warehousingInfo {
  680. //查询刚入库耗材的库存
  681. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  682. //更新药品库的总量
  683. info, _ := FindeDrugInfo(item.DrugId)
  684. min_number = count.StockMaxNumber * info.MinNumber
  685. str := strconv.FormatInt(min_number, 10)
  686. minFloat, _ := strconv.ParseFloat(str, 64)
  687. total = info.Total + minFloat
  688. lib := models.BaseDrugLib{
  689. Total: total,
  690. }
  691. UpdateBaseDrug(&lib, item.ID)
  692. }
  693. return err
  694. }
  695. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  696. info := models.XtDrugWarehouseInfo{}
  697. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  698. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  699. return info, err
  700. }
  701. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  702. lib := models.BaseDrugLib{}
  703. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  704. return lib, err
  705. }
  706. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  707. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  708. return err
  709. }
  710. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  711. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  712. return err
  713. }
  714. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  715. info := models.DrugWarehouseInfo{}
  716. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  717. return info, err
  718. }
  719. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  720. out := models.DrugWarehouseOut{}
  721. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  722. return out, err
  723. }
  724. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  725. if len(drugflow) > 0 {
  726. utx := writeDb.Begin()
  727. if len(drugflow) > 0 {
  728. 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 "
  729. insertParams := make([]string, 0)
  730. insertData := make([]interface{}, 0)
  731. for _, info := range drugflow {
  732. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  733. insertData = append(insertData, info.WarehousingId)
  734. insertData = append(insertData, info.DrugId)
  735. insertData = append(insertData, info.Number)
  736. insertData = append(insertData, info.BatchNumber)
  737. insertData = append(insertData, info.Count)
  738. insertData = append(insertData, info.UserOrgId)
  739. insertData = append(insertData, info.PatientId)
  740. insertData = append(insertData, info.SystemTime)
  741. insertData = append(insertData, info.ConsumableType)
  742. insertData = append(insertData, info.IsSys)
  743. insertData = append(insertData, info.WarehousingOrder)
  744. insertData = append(insertData, info.WarehouseOutId)
  745. insertData = append(insertData, info.WarehouseOutOrderNumber)
  746. insertData = append(insertData, info.IsEdit)
  747. insertData = append(insertData, info.CancelStockId)
  748. insertData = append(insertData, info.CancelOrderNumber)
  749. insertData = append(insertData, info.Manufacturer)
  750. insertData = append(insertData, info.Dealer)
  751. insertData = append(insertData, info.Creator)
  752. insertData = append(insertData, info.UpdateCreator)
  753. insertData = append(insertData, info.Status)
  754. insertData = append(insertData, info.Ctime)
  755. insertData = append(insertData, info.Mtime)
  756. insertData = append(insertData, info.Price)
  757. insertData = append(insertData, info.WarehousingDetailId)
  758. insertData = append(insertData, info.WarehouseOutDetailId)
  759. insertData = append(insertData, info.CancelOutDetailId)
  760. insertData = append(insertData, info.ExpireDate)
  761. insertData = append(insertData, info.ProductDate)
  762. insertData = append(insertData, info.MaxUnit)
  763. insertData = append(insertData, info.MinUnit)
  764. insertData = append(insertData, info.StorehouseId)
  765. }
  766. thisSQL += strings.Join(insertParams, ", ")
  767. err = utx.Exec(thisSQL, insertData...).Error
  768. if err != nil {
  769. utx.Rollback()
  770. return
  771. }
  772. }
  773. utx.Commit()
  774. }
  775. return
  776. }
  777. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  778. info := models.DrugFlow{}
  779. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  780. if err == gorm.ErrRecordNotFound {
  781. return nil, err
  782. }
  783. if err != nil {
  784. return nil, err
  785. }
  786. return &info, nil
  787. }
  788. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  789. info := models.XtDrugWarehouseInfo{}
  790. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  791. return info, err
  792. }
  793. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  794. warehouseInfo := models.XtDrugWarehouseInfo{}
  795. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  796. return err
  797. }
  798. func CreateDrugFlowOne(flow models.DrugFlow) error {
  799. ut := writeDb.Begin()
  800. err := ut.Create(&flow).Error
  801. if err != nil {
  802. ut.Rollback()
  803. return err
  804. }
  805. ut.Commit()
  806. return err
  807. }
  808. func CreateSelfDrugFlowTwo(flow models.XtSelfDrugFlow) error {
  809. ut := writeDb.Begin()
  810. err := ut.Create(&flow).Error
  811. if err != nil {
  812. ut.Rollback()
  813. return err
  814. }
  815. ut.Commit()
  816. return err
  817. }
  818. func CreateNewDrugFlowOne(flow models.DrugFlow, tx *gorm.DB) error {
  819. err := tx.Create(&flow).Error
  820. if err != nil {
  821. tx.Rollback()
  822. return err
  823. }
  824. return err
  825. }
  826. func CreateDrugOutInfoLog(log models.XtDrugWarehouseOutInfoLog, tx *gorm.DB) error {
  827. err := tx.Create(&log).Error
  828. if err != nil {
  829. tx.Rollback()
  830. return err
  831. }
  832. return err
  833. }
  834. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.DrugAutomaticReduceDetail, error) {
  835. detail := models.DrugAutomaticReduceDetail{}
  836. 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
  837. 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
  838. return detail, err
  839. }
  840. func UpdateSelfDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64, id int64) (models.XtSelfDrugWarehouseOutInfo, error) {
  841. detail := models.XtSelfDrugWarehouseOutInfo{}
  842. err = XTWriteDB().Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("patient_id =? and sys_record_time =? and drug_id = ? and status = 1 and advice_id =?", patientid, recorddate, drugid, advice_id).Updates(map[string]interface{}{"status": 0}).Error
  843. return detail, err
  844. }
  845. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  846. flow := models.DrugFlow{}
  847. 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
  848. return flow, err
  849. }
  850. func UpdateDrugFlowTensOne(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.XtSelfDrugFlow, error) {
  851. flow := models.XtSelfDrugFlow{}
  852. 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
  853. return flow, err
  854. }
  855. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  856. tx := XTWriteDB().Begin()
  857. err := tx.Create(&flow).Error
  858. if err != nil {
  859. tx.Rollback()
  860. return err
  861. }
  862. tx.Commit()
  863. return err
  864. }
  865. func CreateNewDrugFlowTwo(flow *models.DrugFlow, tx *gorm.DB) error {
  866. err := tx.Create(&flow).Error
  867. if err != nil {
  868. tx.Rollback()
  869. return err
  870. }
  871. return err
  872. }
  873. func CreateInfoLog(log models.XtDrugWarehouseInfoLog, tx *gorm.DB) error {
  874. err := tx.Create(&log).Error
  875. if err != nil {
  876. tx.Rollback()
  877. return err
  878. }
  879. return err
  880. }
  881. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  882. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  883. return info, err
  884. }
  885. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  886. info := models.WarehousingInfo{}
  887. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  888. return info, err
  889. }
  890. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  891. err := XTWriteDB().Create(&info).Error
  892. return err
  893. }
  894. func CreatedNewWarehouseingDetail(info *models.WarehousingInfo, tx *gorm.DB) error {
  895. err := tx.Create(&info).Error
  896. if err != nil {
  897. tx.Rollback()
  898. return err
  899. }
  900. return err
  901. }
  902. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  903. if storehouse_id == 0 {
  904. 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
  905. }
  906. if storehouse_id > 0 {
  907. 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
  908. }
  909. return info, err
  910. }
  911. func FindNewFirstWarehousingInfoByStock(good_id int64, good_type_id int64, storehouse_id int64, tx *gorm.DB) (info models.WarehousingInfo, err error) {
  912. if storehouse_id == 0 {
  913. err = tx.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0", good_id, good_type_id).Order("ctime").First(&info).Error
  914. if err != gorm.ErrRecordNotFound {
  915. if err != nil {
  916. tx.Rollback()
  917. return info, err
  918. }
  919. }
  920. }
  921. if storehouse_id > 0 {
  922. err = tx.Model(&models.WarehousingInfo{}).Where("good_id = ? AND good_type_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, good_type_id, storehouse_id).Order("ctime").First(&info).Error
  923. if err != gorm.ErrRecordNotFound {
  924. if err != nil {
  925. tx.Rollback()
  926. return info, err
  927. }
  928. }
  929. }
  930. return info, err
  931. }
  932. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64, storehouse_id int64) (info models.VmWarehousingInfo, err error) {
  933. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  934. if good_id > 0 {
  935. db = db.Where("x.good_id = ?", good_id)
  936. }
  937. if good_type_id > 0 {
  938. db = db.Where("x.good_type_id = ?", good_type_id)
  939. }
  940. if storehouse_id > 0 {
  941. db = db.Where("x.storehouse_id = ?", storehouse_id)
  942. }
  943. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  944. return info, err
  945. }
  946. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  947. info := models.WarehousingInfo{}
  948. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  949. return info, err
  950. }
  951. func FindWarehousingInfoTen(goodId int64, storehouse_id int64) (models.WarehousingInfo, error) {
  952. info := models.WarehousingInfo{}
  953. var err error
  954. if storehouse_id == 0 {
  955. 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
  956. }
  957. if storehouse_id > 0 {
  958. 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
  959. }
  960. return info, err
  961. }
  962. func FindNewWarehousingInfoTen(goodId int64, storehouse_id int64, tx *gorm.DB) (models.WarehousingInfo, error) {
  963. info := models.WarehousingInfo{}
  964. var err error
  965. if storehouse_id == 0 {
  966. err = tx.Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and is_check = 1", goodId).Find(&info).Error
  967. if err != gorm.ErrRecordNotFound {
  968. if err != nil {
  969. tx.Rollback()
  970. return info, err
  971. }
  972. }
  973. }
  974. if storehouse_id > 0 {
  975. err := tx.Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1 and storehouse_id = ? and is_check = 1", goodId, storehouse_id).Find(&info).Error
  976. if err != gorm.ErrRecordNotFound {
  977. if err != nil {
  978. tx.Rollback()
  979. return info, err
  980. }
  981. }
  982. }
  983. return info, err
  984. }
  985. //药品先进先出,找出最先入库的批次
  986. func FindLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  987. 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
  988. return info, err
  989. }
  990. func FindLastDrugWarehousingInfoByIDTwo(drug_id int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  991. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").Last(&info).Error
  992. return info, err
  993. }
  994. func FindNewLastDrugWarehousingInfoByID(drug_id int64, storehouse_id int64, tx *gorm.DB) (info models.XtDrugWarehouseInfo, err error) {
  995. err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) and storehouse_id = ? and is_check = 1", drug_id, storehouse_id).Order("ctime").First(&info).Error
  996. if err != gorm.ErrRecordNotFound {
  997. if err != nil {
  998. tx.Rollback()
  999. return info, err
  1000. }
  1001. }
  1002. return info, err
  1003. }
  1004. func FindLastDrugWarehousingInfoByIDSix(drug_id int64, warehouse_out_id int64, is_source int64, storehouse_id int64) (info models.XtDrugWarehouseInfo, err error) {
  1005. if is_source == 1 {
  1006. 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
  1007. }
  1008. if is_source == 2 || is_source == 0 {
  1009. 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
  1010. }
  1011. return info, err
  1012. }
  1013. // 出库时先进先出,找出最先入库的批次
  1014. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  1015. 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
  1016. return info, err
  1017. }
  1018. //
  1019. //// 出库时先进先出,找出最先入库的批次
  1020. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  1021. // 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
  1022. // return info, err
  1023. //}
  1024. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  1025. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  1026. return info, err
  1027. }
  1028. 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) {
  1029. offset := (page - 1) * limit
  1030. db := readDb.Model(&models.Warehousing{})
  1031. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  1032. if len(keywords) > 0 {
  1033. likeKey := "%" + keywords + "%"
  1034. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  1035. 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")
  1036. }
  1037. if startTime > 0 {
  1038. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  1039. }
  1040. if endTime > 0 {
  1041. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  1042. }
  1043. if storehouse_id > 0 {
  1044. db = db.Where("xt_warehouse.storehouse_id = ?", storehouse_id)
  1045. }
  1046. if check_type == 0 {
  1047. db = db.Where("xt_warehouse.is_check = 0 or xt_warehouse.is_check = 1 or xt_warehouse.is_check = 2")
  1048. }
  1049. if check_type > 0 {
  1050. db = db.Where("xt_warehouse.is_check = ?", check_type)
  1051. }
  1052. err = db.Count(&total).Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  1053. return
  1054. }
  1055. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  1056. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  1057. if orgid > 0 {
  1058. db = db.Where("org_id = ?", orgid)
  1059. }
  1060. if len(keyword) > 0 {
  1061. likeKey := "%" + keyword + "%"
  1062. db = db.Where("good_name like ?", likeKey)
  1063. }
  1064. err = db.Find(&goodInfo).Error
  1065. return goodInfo, err
  1066. }
  1067. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  1068. db := XTReadDB().Model(&info).Where("status =1")
  1069. if len(good_id) > 0 {
  1070. db = db.Where("good_id in (?)", good_id)
  1071. }
  1072. if startime > 0 {
  1073. db = db.Where("ctime >=?", startime)
  1074. }
  1075. if endtime > 0 {
  1076. db = db.Where("ctime<=?", endtime)
  1077. }
  1078. if orgid > 0 {
  1079. db = db.Where("org_id = ?", orgid)
  1080. }
  1081. if storehouse_id > 0 {
  1082. db = db.Where("storehouse_id = ?", storehouse_id)
  1083. }
  1084. err = db.Find(&info).Error
  1085. return info, err
  1086. }
  1087. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64, storehouse_id int64) (info []*models.WarehouseOutInfo, err error) {
  1088. db := XTReadDB().Model(&info).Where("status =1")
  1089. if len(good_id) > 0 {
  1090. db = db.Where("good_id in (?)", good_id)
  1091. }
  1092. if startime > 0 {
  1093. db = db.Where("ctime >=?", startime)
  1094. }
  1095. if endtime > 0 {
  1096. db = db.Where("ctime<=?", endtime)
  1097. }
  1098. if orgid > 0 {
  1099. db = db.Where("org_id = ?", orgid)
  1100. }
  1101. if storehouse_id > 0 {
  1102. db = db.Where("storehouse_id = ?", storehouse_id)
  1103. }
  1104. err = db.Find(&info).Error
  1105. return info, err
  1106. }
  1107. type VMDrugWarehouse struct {
  1108. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1109. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1110. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  1111. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1112. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  1113. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1114. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  1115. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1116. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1117. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  1118. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1119. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1120. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1121. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  1122. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  1123. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1124. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1125. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1126. IsCheck int64 `gorm:"column:is_check" json:"is_check" form:"is_check"`
  1127. Checker int64 `gorm:"column:checker" json:"checker" form:"checker"`
  1128. }
  1129. func (VMDrugWarehouse) TableName() string {
  1130. return "xt_drug_warehouse"
  1131. }
  1132. type VMDrugWarehouseInfo struct {
  1133. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1134. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  1135. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1136. Number string `gorm:"column:number" json:"number" form:"number"`
  1137. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1138. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1139. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  1140. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1141. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1142. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1143. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1144. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1145. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1146. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1147. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1148. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1149. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  1150. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  1151. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1152. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1153. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1154. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1155. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1156. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1157. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1158. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1159. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  1160. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1161. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1162. }
  1163. func (VMDrugWarehouseInfo) TableName() string {
  1164. return "xt_drug_warehouse_info"
  1165. }
  1166. type Drug struct {
  1167. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1168. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  1169. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  1170. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  1171. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  1172. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  1173. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  1174. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  1175. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  1176. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  1177. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  1178. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  1179. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  1180. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  1181. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1182. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  1183. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  1184. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1185. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1186. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  1187. Number string `gorm:"column:number" json:"number" form:"number"`
  1188. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  1189. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  1190. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  1191. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  1192. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  1193. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1194. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  1195. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  1196. Code string `gorm:"column:code" json:"code" form:"code"`
  1197. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  1198. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  1199. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  1200. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  1201. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  1202. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  1203. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  1204. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  1205. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  1206. Label int64 `gorm:"column:label" json:"label" form:"label"`
  1207. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  1208. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  1209. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  1210. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  1211. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  1212. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  1213. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1214. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1215. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1216. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1217. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  1218. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1219. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  1220. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  1221. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  1222. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  1223. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  1224. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  1225. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  1226. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  1227. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1228. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  1229. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  1230. Dose string `gorm:"column:dose" json:"dose" form:"dose"`
  1231. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  1232. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  1233. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  1234. IsZeroFlag int64 `gorm:"column:is_zero_flag" json:"is_zero_flag" form:"is_zero_flag"`
  1235. }
  1236. func (Drug) TableName() string {
  1237. return "xt_base_drug"
  1238. }
  1239. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  1240. db := readDb.Model(&VMDrugWarehouse{})
  1241. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1242. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1243. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1244. if len(keywords) > 0 {
  1245. likeKey := "%" + keywords + "%"
  1246. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1247. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  1248. 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")
  1249. }
  1250. if startTime > 0 {
  1251. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1252. }
  1253. if endTime > 0 {
  1254. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1255. }
  1256. db = db.Count(&total)
  1257. offset := (page - 1) * limit
  1258. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1259. return
  1260. }
  1261. 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) {
  1262. db := readDb.Model(&VMDrugWarehouse{})
  1263. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  1264. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  1265. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  1266. if len(keywords) > 0 {
  1267. likeKey := "%" + keywords + "%"
  1268. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  1269. 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")
  1270. }
  1271. if startTime > 0 {
  1272. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  1273. }
  1274. if endTime > 0 {
  1275. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  1276. }
  1277. if storehouse_id > 0 {
  1278. db = db.Where("xt_drug_warehouse.storehouse_id = ?", storehouse_id)
  1279. }
  1280. if check_type > 0 {
  1281. db = db.Where("xt_drug_warehouse.is_check = ?", check_type)
  1282. }
  1283. db = db.Count(&total)
  1284. offset := (page - 1) * limit
  1285. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  1286. return
  1287. }
  1288. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  1289. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1290. return list, err
  1291. }
  1292. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  1293. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  1294. return list, err
  1295. }
  1296. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1297. 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
  1298. return list, err
  1299. }
  1300. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  1301. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  1302. return list, err
  1303. }
  1304. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  1305. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  1306. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  1307. fmt.Println(table)
  1308. if len(id) > 0 {
  1309. db = db.Where("x.warehousing_id in(?)", id)
  1310. }
  1311. if org_id > 0 {
  1312. db = db.Where("x.org_id = ?", org_id)
  1313. }
  1314. 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
  1315. return list, err
  1316. }
  1317. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  1318. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1319. return warehousing, err
  1320. }
  1321. func FindNewWarehousingById(id int64, tx *gorm.DB) (warehousing models.Warehousing, err error) {
  1322. err = tx.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1323. if err != gorm.ErrRecordNotFound {
  1324. if err != nil {
  1325. tx.Rollback()
  1326. return warehousing, err
  1327. }
  1328. }
  1329. return warehousing, err
  1330. }
  1331. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  1332. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  1333. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1334. return warehousing, err
  1335. }
  1336. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  1337. 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
  1338. return warehousing, err
  1339. }
  1340. func FindNewDrugWarehousingById(id int64, org_id int64, tx *gorm.DB) (warehousing VMDrugWarehouse, err error) {
  1341. err = tx.Model(&VMDrugWarehouse{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1342. if err != gorm.ErrRecordNotFound {
  1343. if err != nil {
  1344. tx.Rollback()
  1345. return warehousing, err
  1346. }
  1347. }
  1348. return warehousing, err
  1349. }
  1350. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  1351. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  1352. if len(id) > 0 {
  1353. db = db.Where("x.id in(?)", id)
  1354. }
  1355. if org_id > 0 {
  1356. db = db.Where("x.org_id = ?", org_id)
  1357. }
  1358. 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
  1359. return warehousing, err
  1360. }
  1361. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  1362. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  1363. // return warehousing, err
  1364. //
  1365. //}
  1366. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  1367. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1368. return
  1369. }
  1370. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1371. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  1372. return
  1373. }
  1374. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  1375. if len(salesReturnInfo) > 0 {
  1376. utx := writeDb.Begin()
  1377. if len(salesReturnInfo) > 0 {
  1378. 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 "
  1379. insertParams := make([]string, 0)
  1380. insertData := make([]interface{}, 0)
  1381. for _, info := range salesReturnInfo {
  1382. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1383. insertData = append(insertData, info.GoodId)
  1384. insertData = append(insertData, info.SalesReturnId)
  1385. insertData = append(insertData, info.GoodTypeId)
  1386. insertData = append(insertData, info.Count)
  1387. insertData = append(insertData, info.Price)
  1388. insertData = append(insertData, info.Total)
  1389. insertData = append(insertData, info.Ctime)
  1390. insertData = append(insertData, info.Status)
  1391. insertData = append(insertData, info.OrgId)
  1392. insertData = append(insertData, info.OrderNumber)
  1393. insertData = append(insertData, info.Type)
  1394. insertData = append(insertData, info.Dealer)
  1395. insertData = append(insertData, info.Manufacturer)
  1396. }
  1397. thisSQL += strings.Join(insertParams, ", ")
  1398. err = utx.Exec(thisSQL, insertData...).Error
  1399. if err != nil {
  1400. utx.Rollback()
  1401. return
  1402. }
  1403. }
  1404. utx.Commit()
  1405. }
  1406. return
  1407. }
  1408. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1409. if len(salesReturnInfo) > 0 {
  1410. utx := writeDb.Begin()
  1411. if len(salesReturnInfo) > 0 {
  1412. 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 "
  1413. insertParams := make([]string, 0)
  1414. insertData := make([]interface{}, 0)
  1415. for _, info := range salesReturnInfo {
  1416. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1417. insertData = append(insertData, info.DrugId)
  1418. insertData = append(insertData, info.SalesReturnId)
  1419. insertData = append(insertData, info.Count)
  1420. insertData = append(insertData, info.Price)
  1421. insertData = append(insertData, info.Total)
  1422. insertData = append(insertData, info.Ctime)
  1423. insertData = append(insertData, info.Status)
  1424. insertData = append(insertData, info.OrgId)
  1425. insertData = append(insertData, info.OrderNumber)
  1426. insertData = append(insertData, info.Type)
  1427. insertData = append(insertData, info.Dealer)
  1428. insertData = append(insertData, info.Manufacturer)
  1429. insertData = append(insertData, info.RetailPrice)
  1430. insertData = append(insertData, info.RetailTotalPrice)
  1431. }
  1432. thisSQL += strings.Join(insertParams, ", ")
  1433. err = utx.Exec(thisSQL, insertData...).Error
  1434. if err != nil {
  1435. utx.Rollback()
  1436. return
  1437. }
  1438. }
  1439. utx.Commit()
  1440. }
  1441. return
  1442. }
  1443. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1444. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1445. return total, err
  1446. }
  1447. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1448. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1449. return total, err
  1450. }
  1451. func FindNewAllCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1452. err = tx.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1453. if err != gorm.ErrRecordNotFound {
  1454. if err != nil {
  1455. tx.Rollback()
  1456. return total, err
  1457. }
  1458. }
  1459. return total, err
  1460. }
  1461. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1462. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1463. return total, err
  1464. }
  1465. func FindNewAllDrugCancelStockTotal(org_id int64, tx *gorm.DB) (total int64, err error) {
  1466. err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1467. if err != gorm.ErrRecordNotFound {
  1468. if err != nil {
  1469. tx.Rollback()
  1470. return total, err
  1471. }
  1472. }
  1473. return total, err
  1474. }
  1475. //func FindAllDrugCancelStockTotalTX(org_id int64,tx *gorm.DB) (total int64, err error) {
  1476. // err = tx.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1477. // return total, err
  1478. //}
  1479. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1480. db := readDb.Model(&models.SalesReturn{})
  1481. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1482. if len(keywords) > 0 {
  1483. likeKey := "%" + keywords + "%"
  1484. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1485. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1486. 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")
  1487. }
  1488. if startTime > 0 {
  1489. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1490. }
  1491. if endTime > 0 {
  1492. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1493. }
  1494. db = db.Count(&total)
  1495. offset := (page - 1) * limit
  1496. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1497. return
  1498. }
  1499. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1500. db := readDb.Model(&models.DrugSalesReturn{})
  1501. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1502. if len(keywords) > 0 {
  1503. likeKey := "%" + keywords + "%"
  1504. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1505. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1506. 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")
  1507. }
  1508. if startTime > 0 {
  1509. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1510. }
  1511. if endTime > 0 {
  1512. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1513. }
  1514. db = db.Count(&total)
  1515. offset := (page - 1) * limit
  1516. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1517. return
  1518. }
  1519. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1520. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1521. return list, err
  1522. }
  1523. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1524. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1525. return list, err
  1526. }
  1527. type DrugCancelStockInfo struct {
  1528. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1529. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1530. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1531. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1532. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1533. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1534. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1535. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1536. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1537. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1538. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1539. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1540. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1541. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1542. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1543. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1544. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1545. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1546. Number string `gorm:"column:number" json:"number" form:"number"`
  1547. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1548. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1549. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1550. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1551. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1552. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1553. StorehouseId int64 `gorm:"column:storehouse_id" json:"storehouse_id" form:"storehouse_id"`
  1554. DrugWarehouseInfo []*models.XtDrugWarehouseInfo `gorm:"ForeignKey:DrugId;AssociationForeignKey:DrugId" json:"drug_warehouse_info"`
  1555. }
  1556. func (DrugCancelStockInfo) TableName() string {
  1557. return "xt_drug_cancel_stock_info"
  1558. }
  1559. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1560. 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
  1561. return list, err
  1562. }
  1563. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1564. db := readDb.Model(&models.WarehousingInfo{})
  1565. db = db.Where("org_id = ? AND status = 1", orgId)
  1566. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1567. if startTime > 0 {
  1568. db = db.Where("ctime >=?", startTime)
  1569. }
  1570. if endTime > 0 {
  1571. db = db.Where("ctime<= ?", endTime)
  1572. }
  1573. db = db.Count(&total)
  1574. offset := (page - 1) * limit
  1575. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1576. return list, total, err
  1577. }
  1578. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1579. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1580. return total, err
  1581. }
  1582. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1583. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1584. return total, err
  1585. }
  1586. func FindAllSelfDrugWarehouseOut(org_id int64, tx *gorm.DB) (total int64, err error) {
  1587. err = tx.Model(&models.XtDrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1588. if err != gorm.ErrRecordNotFound {
  1589. if err != nil {
  1590. tx.Rollback()
  1591. return
  1592. }
  1593. }
  1594. return total, err
  1595. }
  1596. func AddSigleWarehouseOutOne(warehouseOut *models.WarehouseOut) error {
  1597. err := XTWriteDB().Create(&warehouseOut).Error
  1598. return err
  1599. }
  1600. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1601. err := tx.Create(&warehouseOut).Error
  1602. if err != nil {
  1603. tx.Rollback()
  1604. return err
  1605. }
  1606. return err
  1607. }
  1608. func AddNewSigleWarehouseOut(warehouseOut *models.WarehouseOut, tx *gorm.DB) error {
  1609. err := tx.Create(&warehouseOut).Error
  1610. if err != nil {
  1611. tx.Rollback()
  1612. return err
  1613. }
  1614. return err
  1615. }
  1616. func UpdateSingleWarehoseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1617. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1618. if err != nil {
  1619. tx.Rollback()
  1620. return err
  1621. }
  1622. return err
  1623. }
  1624. func GetDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string) (models.DrugWarehouseOut, error) {
  1625. drugWarehouseOut := models.DrugWarehouseOut{}
  1626. err := XTReadDB().Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).Find(&drugWarehouseOut).Error
  1627. return drugWarehouseOut, err
  1628. }
  1629. func GetNewDrugWarehouseOutIsExit(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  1630. drugWarehouseOut := models.DrugWarehouseOut{}
  1631. err := tx.Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).Find(&drugWarehouseOut).Error
  1632. if err != gorm.ErrRecordNotFound {
  1633. if err != nil {
  1634. tx.Rollback()
  1635. return drugWarehouseOut, err
  1636. }
  1637. }
  1638. return drugWarehouseOut, err
  1639. }
  1640. func GetNewDrugWarehouseOutIsExitOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB, id int64) (models.DrugWarehouseOut, error) {
  1641. drugWarehouseOut := models.DrugWarehouseOut{}
  1642. err := tx.Where("is_sys = ? AND status = 1 AND org_id = ? AND id = ?", is_sys, org_id, id).Find(&drugWarehouseOut).Error
  1643. if err != gorm.ErrRecordNotFound {
  1644. if err != nil {
  1645. tx.Rollback()
  1646. return drugWarehouseOut, err
  1647. }
  1648. }
  1649. return drugWarehouseOut, err
  1650. }
  1651. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1652. err := writeDb.Create(&warehouseOut).Error
  1653. return err
  1654. }
  1655. func GetSigleDrugWarehouseOutByPatientId(warehousing_time int64, patient_id int64, tx *gorm.DB) (models.XtSelfDrugWarehouse, error) {
  1656. selfDrugWarehouse := models.XtSelfDrugWarehouse{}
  1657. err := tx.Where("warehousing_time = ? and patient_id = ? and is_sys =1 and status=1", warehousing_time, patient_id).Find(&selfDrugWarehouse).Error
  1658. if err != gorm.ErrRecordNotFound {
  1659. if err != nil {
  1660. tx.Rollback()
  1661. return selfDrugWarehouse, err
  1662. }
  1663. }
  1664. return selfDrugWarehouse, err
  1665. }
  1666. func AddSelfSigleDrugWarehouseOut(warehouseOut *models.XtSelfDrugWarehouseOut, tx *gorm.DB) error {
  1667. err := tx.Create(&warehouseOut).Error
  1668. if err != nil {
  1669. tx.Rollback()
  1670. return err
  1671. }
  1672. return err
  1673. }
  1674. func AddNewSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut, tx *gorm.DB) error {
  1675. err := tx.Create(&warehouseOut).Error
  1676. if err != nil {
  1677. tx.Rollback()
  1678. return err
  1679. }
  1680. return err
  1681. }
  1682. func UpdateSingleDrugWarehouseOut(id int64, storehouse_id int64) error {
  1683. err := XTWriteDB().Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1684. return err
  1685. }
  1686. func UpdateNewSingleDrugWarehouseOut(id int64, storehouse_id int64, tx *gorm.DB) error {
  1687. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  1688. if err != nil {
  1689. tx.Rollback()
  1690. return err
  1691. }
  1692. return err
  1693. }
  1694. func UpdateSelfSingleDrugWarehouseOut(id int64, patient_id int64, tx *gorm.DB) error {
  1695. err := tx.Model(&models.XtSelfDrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"patient_id": patient_id}).Error
  1696. return err
  1697. }
  1698. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1699. if len(warehouseOutInfo) > 0 {
  1700. utx := writeDb.Begin()
  1701. if len(warehouseOutInfo) > 0 {
  1702. 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 "
  1703. insertParams := make([]string, 0)
  1704. insertData := make([]interface{}, 0)
  1705. for _, info := range warehouseOutInfo {
  1706. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1707. insertData = append(insertData, info.WarehouseOutId)
  1708. insertData = append(insertData, info.GoodId)
  1709. insertData = append(insertData, info.GoodTypeId)
  1710. insertData = append(insertData, info.ProductDate)
  1711. insertData = append(insertData, info.ExpiryDate)
  1712. insertData = append(insertData, info.Count)
  1713. insertData = append(insertData, info.Price)
  1714. insertData = append(insertData, info.TotalPrice)
  1715. insertData = append(insertData, info.Remark)
  1716. insertData = append(insertData, info.Ctime)
  1717. insertData = append(insertData, info.Status)
  1718. insertData = append(insertData, info.OrgId)
  1719. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1720. insertData = append(insertData, info.Type)
  1721. insertData = append(insertData, info.Dealer)
  1722. insertData = append(insertData, info.Manufacturer)
  1723. insertData = append(insertData, info.Number)
  1724. insertData = append(insertData, info.LicenseNumber)
  1725. }
  1726. thisSQL += strings.Join(insertParams, ", ")
  1727. err = utx.Exec(thisSQL, insertData...).Error
  1728. if err != nil {
  1729. utx.Rollback()
  1730. return
  1731. }
  1732. }
  1733. utx.Commit()
  1734. }
  1735. return
  1736. }
  1737. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1738. if len(warehouseOutInfo) > 0 {
  1739. utx := writeDb.Begin()
  1740. if len(warehouseOutInfo) > 0 {
  1741. 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 "
  1742. insertParams := make([]string, 0)
  1743. insertData := make([]interface{}, 0)
  1744. for _, info := range warehouseOutInfo {
  1745. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1746. insertData = append(insertData, info.WarehouseOutId)
  1747. insertData = append(insertData, info.DrugId)
  1748. insertData = append(insertData, info.ProductDate)
  1749. insertData = append(insertData, info.ExpiryDate)
  1750. insertData = append(insertData, info.Count)
  1751. insertData = append(insertData, info.Price)
  1752. insertData = append(insertData, info.TotalPrice)
  1753. insertData = append(insertData, info.Remark)
  1754. insertData = append(insertData, info.Ctime)
  1755. insertData = append(insertData, info.Status)
  1756. insertData = append(insertData, info.OrgId)
  1757. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1758. insertData = append(insertData, info.Type)
  1759. insertData = append(insertData, info.Dealer)
  1760. insertData = append(insertData, info.Manufacturer)
  1761. insertData = append(insertData, info.RetailPrice)
  1762. insertData = append(insertData, info.RetailTotalPrice)
  1763. insertData = append(insertData, info.Number)
  1764. insertData = append(insertData, info.BatchNumber)
  1765. insertData = append(insertData, info.CountUnit)
  1766. }
  1767. thisSQL += strings.Join(insertParams, ", ")
  1768. err = utx.Exec(thisSQL, insertData...).Error
  1769. if err != nil {
  1770. utx.Rollback()
  1771. return
  1772. }
  1773. }
  1774. utx.Commit()
  1775. }
  1776. return
  1777. }
  1778. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1779. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1780. return info, err
  1781. }
  1782. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1783. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1784. return info, err
  1785. }
  1786. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1787. db := readDb.Model(&models.WarehouseOut{})
  1788. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1789. if len(keywords) > 0 {
  1790. likeKey := "%" + keywords + "%"
  1791. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1792. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1793. 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")
  1794. }
  1795. if startTime > 0 {
  1796. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1797. }
  1798. if endTime > 0 {
  1799. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1800. }
  1801. db = db.Count(&total)
  1802. offset := (page - 1) * limit
  1803. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1804. return list, total, err
  1805. }
  1806. 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) {
  1807. db := readDb.Model(&models.WarehouseOut{})
  1808. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1", orgId)
  1809. if len(keywords) > 0 {
  1810. likeKey := "%" + keywords + "%"
  1811. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1812. 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")
  1813. }
  1814. if startTime > 0 {
  1815. db = db.Where("xt_warehouse_out.warehouse_out_time >=?", startTime)
  1816. }
  1817. if endTime > 0 {
  1818. db = db.Where("xt_warehouse_out.warehouse_out_time<= ?", endTime)
  1819. }
  1820. if storehouse_id > 0 {
  1821. db = db.Where("xt_warehouse_out.storehouse_id = ?", storehouse_id)
  1822. }
  1823. if way_type == 0 {
  1824. 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")
  1825. }
  1826. if way_type == 1 {
  1827. db = db.Where("xt_warehouse_out.is_sys = 0")
  1828. }
  1829. if way_type == 2 {
  1830. db = db.Where("xt_warehouse_out.is_sys = 1")
  1831. }
  1832. if way_type == 3 {
  1833. db = db.Where("xt_warehouse_out.is_sys = 12")
  1834. }
  1835. if way_type == 4 {
  1836. db = db.Where("xt_warehouse_out.is_sys = 5")
  1837. }
  1838. if check_type > 0 {
  1839. db = db.Where("xt_warehouse_out.is_check = ?", check_type)
  1840. }
  1841. if check_type == 0 {
  1842. db = db.Where("xt_warehouse_out.is_check = 1 or xt_warehouse_out.is_check = 2")
  1843. }
  1844. db = db.Count(&total)
  1845. offset := (page - 1) * limit
  1846. if orgId == 10191 {
  1847. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1848. } else {
  1849. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1850. }
  1851. return list, total, err
  1852. }
  1853. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1854. db := readDb.Model(&models.DrugWarehouseOut{})
  1855. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1856. if len(keywords) > 0 {
  1857. likeKey := "%" + keywords + "%"
  1858. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1859. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1860. 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")
  1861. }
  1862. if startTime > 0 {
  1863. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1864. }
  1865. if endTime > 0 {
  1866. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1867. }
  1868. db = db.Count(&total)
  1869. offset := (page - 1) * limit
  1870. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1871. return list, total, err
  1872. }
  1873. 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) {
  1874. db := readDb.Model(&models.DrugWarehouseOut{})
  1875. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1876. if len(keywords) > 0 {
  1877. likeKey := "%" + keywords + "%"
  1878. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1879. 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")
  1880. }
  1881. if startTime > 0 {
  1882. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1883. }
  1884. if endTime > 0 {
  1885. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1886. }
  1887. if storehouse_id > 0 {
  1888. db = db.Where("xt_drug_warehouse_out.storehouse_id = ?", storehouse_id)
  1889. }
  1890. if check_type > 0 {
  1891. db = db.Where("xt_drug_warehouse_out.is_check = ?", check_type)
  1892. }
  1893. if way_type == 0 {
  1894. 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")
  1895. }
  1896. if way_type == 1 {
  1897. db = db.Where("xt_drug_warehouse_out.is_sys = 0")
  1898. }
  1899. if way_type == 2 {
  1900. db = db.Where("xt_drug_warehouse_out.is_sys = 1")
  1901. }
  1902. if way_type == 3 {
  1903. db = db.Where("xt_drug_warehouse_out.is_sys = 12")
  1904. }
  1905. if way_type == 4 {
  1906. db = db.Where("xt_drug_warehouse_out.is_sys = 15")
  1907. }
  1908. db = db.Count(&total)
  1909. offset := (page - 1) * limit
  1910. if orgId == 10191 {
  1911. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1912. } else {
  1913. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1914. }
  1915. return list, total, err
  1916. }
  1917. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1918. 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
  1919. return list, err
  1920. }
  1921. type DrugWarehouseOutInfo struct {
  1922. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1923. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1924. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1925. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1926. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1927. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1928. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1929. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1930. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1931. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1932. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1933. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1934. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1935. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1936. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1937. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1938. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1939. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1940. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1941. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1942. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1943. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1944. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1945. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1946. Number string `gorm:"column:number" json:"number" form:"number"`
  1947. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1948. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1949. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1950. SupplyCancelOutId int64 `gorm:"column:supply_cancel_out_id" json:"supply_cancel_out_id" form:"supply_cancel_out_id"`
  1951. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  1952. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1953. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  1954. StockCount string `gorm:"column:stock_count" json:"stock_count" form:"stock_count"`
  1955. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1956. ClassType string `gorm:"column:class_type" json:"class_type" form:"class_type"`
  1957. }
  1958. func (DrugWarehouseOutInfo) TableName() string {
  1959. return "xt_drug_warehouse_out_info"
  1960. }
  1961. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1962. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1963. return list, err
  1964. }
  1965. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1966. info := &models.WarehousingInfo{}
  1967. type WarehouseInfoCountAmount struct {
  1968. Total int64
  1969. }
  1970. var result WarehouseInfoCountAmount
  1971. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1972. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1973. return result.Total, info
  1974. }
  1975. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1976. type SalesReturnCountAmount struct {
  1977. Total int64
  1978. }
  1979. var result SalesReturnCountAmount
  1980. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1981. return result.Total, err
  1982. }
  1983. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1984. type WarehouseOutInfoCountAmount struct {
  1985. Total int64
  1986. }
  1987. var result WarehouseOutInfoCountAmount
  1988. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1989. return result.Total, err
  1990. }
  1991. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1992. type CancleStockoCountAmount struct {
  1993. Total int64
  1994. }
  1995. var result CancleStockoCountAmount
  1996. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1997. return result.Total, err
  1998. }
  1999. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  2000. info := models.WarehousingInfo{}
  2001. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  2002. return info, err
  2003. }
  2004. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  2005. stock := models.CancelStock{}
  2006. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  2007. if err == gorm.ErrRecordNotFound {
  2008. return nil, err
  2009. }
  2010. if err != nil {
  2011. return nil, err
  2012. }
  2013. return &stock, nil
  2014. }
  2015. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  2016. stock := models.CancelStock{}
  2017. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2018. if err == gorm.ErrRecordNotFound {
  2019. return nil, err
  2020. }
  2021. if err != nil {
  2022. return nil, err
  2023. }
  2024. return &stock, nil
  2025. }
  2026. func GetNewCancelStockDetailByOrderNumberOne(return_time int64, orgid int64, tx *gorm.DB) (*models.CancelStock, error) {
  2027. stock := models.CancelStock{}
  2028. err := tx.Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  2029. if err != gorm.ErrRecordNotFound {
  2030. if err != nil {
  2031. tx.Rollback()
  2032. return &stock, err
  2033. }
  2034. }
  2035. return &stock, err
  2036. }
  2037. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  2038. err := writeDb.Create(&cancelStock).Error
  2039. return err
  2040. }
  2041. func AddNewSigleCancelStock(cancelStock *models.CancelStock, tx *gorm.DB) error {
  2042. err := tx.Create(&cancelStock).Error
  2043. if err != nil {
  2044. tx.Rollback()
  2045. return err
  2046. }
  2047. return err
  2048. }
  2049. func UpdateSigleCancelStock(id int64, storehouse_id int64) error {
  2050. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2051. return err
  2052. }
  2053. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  2054. stock := models.CancelStock{}
  2055. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2056. return stock, err
  2057. }
  2058. func GetNewLastCancelStockById(orgid int64, tx *gorm.DB) (models.CancelStock, error) {
  2059. stock := models.CancelStock{}
  2060. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2061. if err != gorm.ErrRecordNotFound {
  2062. if err != nil {
  2063. tx.Rollback()
  2064. return stock, err
  2065. }
  2066. }
  2067. return stock, err
  2068. }
  2069. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  2070. stock := models.DrugCancelStock{}
  2071. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2072. return stock, err
  2073. }
  2074. func GetNewLastDrugCancelStockById(orgid int64, tx *gorm.DB) (models.DrugCancelStock, error) {
  2075. stock := models.DrugCancelStock{}
  2076. err := tx.Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  2077. if err != gorm.ErrRecordNotFound {
  2078. if err != nil {
  2079. tx.Rollback()
  2080. return stock, err
  2081. }
  2082. }
  2083. return stock, err
  2084. }
  2085. func GetManufactureById(id int64) (models.Manufacturer, error) {
  2086. manufacturer := models.Manufacturer{}
  2087. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2088. return manufacturer, err
  2089. }
  2090. func GetNewManufactureById(id int64, tx *gorm.DB) (models.Manufacturer, error) {
  2091. manufacturer := models.Manufacturer{}
  2092. err := tx.Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  2093. if err != gorm.ErrRecordNotFound {
  2094. if err != nil {
  2095. tx.Rollback()
  2096. return manufacturer, err
  2097. }
  2098. }
  2099. return manufacturer, err
  2100. }
  2101. func GetDealerById(id int64) (models.Dealer, error) {
  2102. dealer := models.Dealer{}
  2103. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2104. return dealer, err
  2105. }
  2106. func GetNewDealerById(id int64, tx *gorm.DB) (models.Dealer, error) {
  2107. dealer := models.Dealer{}
  2108. err := tx.Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  2109. if err != gorm.ErrRecordNotFound {
  2110. if err != nil {
  2111. tx.Rollback()
  2112. return dealer, err
  2113. }
  2114. }
  2115. return dealer, err
  2116. }
  2117. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  2118. err := writeDb.Create(&cancelStock).Error
  2119. return err
  2120. }
  2121. func AddNewSigleDrugCancelStock(cancelStock *models.DrugCancelStock, tx *gorm.DB) error {
  2122. err := tx.Create(&cancelStock).Error
  2123. if err != nil {
  2124. tx.Rollback()
  2125. return err
  2126. }
  2127. return err
  2128. }
  2129. func UpdateSingleDrugCancelStock(id int64, storehouse_id int64) error {
  2130. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"storehouse_id": storehouse_id}).Error
  2131. return err
  2132. }
  2133. //func AddSigleDrugCancelStockTX(cancelStock *models.DrugCancelStock,tx *gorm.DB) error {
  2134. // err := tx.Create(&cancelStock).Error
  2135. // return err
  2136. //}
  2137. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  2138. if len(cancelStockInfo) > 0 {
  2139. utx := writeDb.Begin()
  2140. if len(cancelStockInfo) > 0 {
  2141. 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 "
  2142. insertParams := make([]string, 0)
  2143. insertData := make([]interface{}, 0)
  2144. for _, info := range cancelStockInfo {
  2145. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2146. insertData = append(insertData, info.GoodId)
  2147. insertData = append(insertData, info.CancelStockId)
  2148. insertData = append(insertData, info.GoodTypeId)
  2149. insertData = append(insertData, info.Count)
  2150. insertData = append(insertData, info.Price)
  2151. insertData = append(insertData, info.Total)
  2152. insertData = append(insertData, info.Ctime)
  2153. insertData = append(insertData, info.Status)
  2154. insertData = append(insertData, info.OrgId)
  2155. insertData = append(insertData, info.OrderNumber)
  2156. insertData = append(insertData, info.Type)
  2157. insertData = append(insertData, info.Dealer)
  2158. insertData = append(insertData, info.Manufacturer)
  2159. insertData = append(insertData, info.Number)
  2160. insertData = append(insertData, info.RegisterAccount)
  2161. insertData = append(insertData, info.Remark)
  2162. insertData = append(insertData, info.ProductDate)
  2163. insertData = append(insertData, info.ExpiryDate)
  2164. insertData = append(insertData, info.WarehouseInfoId)
  2165. insertData = append(insertData, info.WarehouseInfoId)
  2166. }
  2167. thisSQL += strings.Join(insertParams, ", ")
  2168. err = utx.Exec(thisSQL, insertData...).Error
  2169. if err != nil {
  2170. utx.Rollback()
  2171. return
  2172. }
  2173. }
  2174. utx.Commit()
  2175. }
  2176. return
  2177. }
  2178. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  2179. err := XTWriteDB().Create(&info).Error
  2180. return err
  2181. }
  2182. //func CreateCancelStockInfoTwoTX(info *models.DrugCancelStockInfo,tx *gorm.DB) error {
  2183. // err := tx.Create(&info).Error
  2184. // return err
  2185. //}
  2186. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  2187. info := models.DrugCancelStockInfo{}
  2188. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2189. return info, err
  2190. }
  2191. //func GetLastStockInfoThreeTX(drugid int64,tx *gorm.DB) (models.DrugCancelStockInfo, error) {
  2192. // info := models.DrugCancelStockInfo{}
  2193. // err := tx.Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  2194. // return info, err
  2195. //}
  2196. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  2197. err := XTWriteDB().Create(&info).Error
  2198. return err
  2199. }
  2200. func CreateNewCancelStockInfoOne(info *models.CancelStockInfo, tx *gorm.DB) error {
  2201. err := XTWriteDB().Create(&info).Error
  2202. if err != nil {
  2203. tx.Rollback()
  2204. return err
  2205. }
  2206. return err
  2207. }
  2208. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  2209. info := models.CancelStockInfo{}
  2210. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2211. return info, err
  2212. }
  2213. func GetLastNewCancelStockInfoByGoodId(goodId int64, tx *gorm.DB) (models.CancelStockInfo, error) {
  2214. info := models.CancelStockInfo{}
  2215. err := tx.Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  2216. if err != gorm.ErrRecordNotFound {
  2217. if err != nil {
  2218. tx.Rollback()
  2219. return info, err
  2220. }
  2221. }
  2222. return info, err
  2223. }
  2224. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  2225. info := models.DrugCancelStockInfo{}
  2226. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  2227. return info, err
  2228. }
  2229. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  2230. if len(cancelStockInfo) > 0 {
  2231. utx := writeDb.Begin()
  2232. if len(cancelStockInfo) > 0 {
  2233. 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 "
  2234. insertParams := make([]string, 0)
  2235. insertData := make([]interface{}, 0)
  2236. for _, info := range cancelStockInfo {
  2237. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  2238. insertData = append(insertData, info.DrugId)
  2239. insertData = append(insertData, info.CancelStockId)
  2240. insertData = append(insertData, info.Count)
  2241. insertData = append(insertData, info.Price)
  2242. insertData = append(insertData, info.Total)
  2243. insertData = append(insertData, info.Ctime)
  2244. insertData = append(insertData, info.Status)
  2245. insertData = append(insertData, info.OrgId)
  2246. insertData = append(insertData, info.OrderNumber)
  2247. insertData = append(insertData, info.Type)
  2248. insertData = append(insertData, info.Dealer)
  2249. insertData = append(insertData, info.Manufacturer)
  2250. insertData = append(insertData, info.RetailPrice)
  2251. insertData = append(insertData, info.RetailTotalPrice)
  2252. insertData = append(insertData, info.RegisterAccount)
  2253. insertData = append(insertData, info.BatchNumber)
  2254. insertData = append(insertData, info.Remark)
  2255. insertData = append(insertData, info.MaxUnit)
  2256. insertData = append(insertData, info.ProductDate)
  2257. insertData = append(insertData, info.ExpiryDate)
  2258. insertData = append(insertData, info.BatchNumberId)
  2259. }
  2260. thisSQL += strings.Join(insertParams, ", ")
  2261. err = utx.Exec(thisSQL, insertData...).Error
  2262. if err != nil {
  2263. utx.Rollback()
  2264. return
  2265. }
  2266. }
  2267. utx.Commit()
  2268. }
  2269. return
  2270. }
  2271. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  2272. db := readDb.Model(&models.CancelStock{})
  2273. db = db.Where("org_id = ? AND status = 1", orgId)
  2274. err = db.Order("ctime desc").Find(&list).Error
  2275. return
  2276. }
  2277. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2278. db := readDb.Model(&models.WarehousingInfo{})
  2279. db = db.Where("org_id = ? AND status = 1", orgId)
  2280. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2281. if startTime > 0 {
  2282. db = db.Where("ctime >=?", startTime)
  2283. }
  2284. if endTime > 0 {
  2285. db = db.Where("ctime<= ?", endTime)
  2286. }
  2287. db = db.Count(&total)
  2288. offset := (page - 1) * limit
  2289. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2290. return
  2291. }
  2292. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  2293. db := XTReadDB().Model(&models.StockInfo{})
  2294. if startime > 0 {
  2295. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2296. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  2297. })
  2298. }
  2299. if startime == 0 || endtime == 0 {
  2300. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2301. return db.Where("org_id = ? AND status = 1", orgId)
  2302. })
  2303. }
  2304. if endtime > 0 {
  2305. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2306. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  2307. })
  2308. }
  2309. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  2310. db = db.Group("xt_good_information.id")
  2311. if startime == 0 || endtime == 0 {
  2312. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2313. return db.Where("org_id = ? AND status = 1", orgId)
  2314. })
  2315. }
  2316. if startime > 0 {
  2317. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2318. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  2319. })
  2320. }
  2321. if endtime > 0 {
  2322. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2323. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  2324. })
  2325. }
  2326. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  2327. if startime == 0 || endtime == 0 {
  2328. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  2329. }
  2330. if startime > 0 {
  2331. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  2332. }
  2333. if endtime > 0 {
  2334. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  2335. }
  2336. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  2337. if len(keyword) > 0 {
  2338. likeKey := "%" + keyword + "%"
  2339. 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)
  2340. 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")
  2341. }
  2342. if type_name > 0 {
  2343. 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)
  2344. }
  2345. db = db.Count(&total)
  2346. offset := (page - 1) * limit
  2347. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2348. return
  2349. }
  2350. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2351. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2352. return
  2353. }
  2354. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  2355. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  2356. return
  2357. }
  2358. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  2359. 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
  2360. return
  2361. }
  2362. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  2363. 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
  2364. return
  2365. }
  2366. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  2367. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  2368. "good_id": warehouseInfo.GoodId,
  2369. "number": warehouseInfo.Number,
  2370. "product_date": warehouseInfo.ProductDate,
  2371. "expiry_date": warehouseInfo.ExpiryDate,
  2372. "warehousing_count": warehouseInfo.WarehousingCount,
  2373. "price": warehouseInfo.Price,
  2374. "total_price": warehouseInfo.TotalPrice,
  2375. "mtime": warehouseInfo.Mtime,
  2376. "remark": warehouseInfo.Remark,
  2377. }).Error
  2378. return
  2379. }
  2380. func UpDateWarehouseStatus(id int64) (err error) {
  2381. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2382. return
  2383. }
  2384. func UpdatedStockFlowById(id int64) (err error) {
  2385. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2386. return
  2387. }
  2388. func UpDateDrugWarehouseStatus(id int64) (err error) {
  2389. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2390. return
  2391. }
  2392. func UpdateDrugFlowById(id int64) (err error) {
  2393. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2394. return
  2395. }
  2396. func DeleteWarehouse(ids []string) (err error) {
  2397. ut := writeDb.Begin()
  2398. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2399. if err != nil {
  2400. ut.Rollback()
  2401. return
  2402. }
  2403. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2404. if err != nil {
  2405. ut.Rollback()
  2406. return
  2407. }
  2408. ut.Commit()
  2409. return
  2410. }
  2411. func DeleteStockFlow(ids []string) (err error) {
  2412. ut := writeDb.Begin()
  2413. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2414. if err != nil {
  2415. ut.Rollback()
  2416. return
  2417. }
  2418. ut.Commit()
  2419. return
  2420. }
  2421. func DeleteDrugWarehouse(ids []string) (err error) {
  2422. ut := writeDb.Begin()
  2423. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2424. if err != nil {
  2425. ut.Rollback()
  2426. return
  2427. }
  2428. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2429. if err != nil {
  2430. ut.Rollback()
  2431. return
  2432. }
  2433. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2434. if err != nil {
  2435. ut.Rollback()
  2436. return
  2437. }
  2438. ut.Commit()
  2439. return
  2440. }
  2441. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  2442. warehouse := &models.Warehousing{}
  2443. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  2444. return warehouse, err
  2445. }
  2446. func FindWareHouseOutByIdOne(id int64) (*models.WarehouseOut, error) {
  2447. warehouseOut := &models.WarehouseOut{}
  2448. err := XTReadDB().Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2449. return warehouseOut, err
  2450. }
  2451. func FindWareHouseOutById(id int64, tx *gorm.DB) (*models.WarehouseOut, error) {
  2452. warehouseOut := &models.WarehouseOut{}
  2453. err := tx.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2454. if err != gorm.ErrRecordNotFound {
  2455. if err != nil {
  2456. tx.Rollback()
  2457. return warehouseOut, err
  2458. }
  2459. }
  2460. return warehouseOut, err
  2461. }
  2462. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  2463. warehouseOut := &models.DrugWarehouseOut{}
  2464. 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
  2465. return warehouseOut, err
  2466. }
  2467. func FindNewDrugWareHouseOutById(id int64, org_id int64, tx *gorm.DB) (*models.DrugWarehouseOut, error) {
  2468. warehouseOut := &models.DrugWarehouseOut{}
  2469. err := tx.Model(&models.DrugWarehouseOut{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ? ", org_id).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  2470. if err != nil {
  2471. tx.Rollback()
  2472. return warehouseOut, err
  2473. }
  2474. return warehouseOut, err
  2475. }
  2476. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  2477. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  2478. return
  2479. }
  2480. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  2481. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  2482. return
  2483. }
  2484. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  2485. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2486. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2487. return
  2488. }
  2489. func DeleteStockInRecordByReocordId(id int64) (err error) {
  2490. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2491. return
  2492. }
  2493. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  2494. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  2495. if manufacturer_id > 0 {
  2496. db = db.Where("manufacturer = ?", manufacturer_id)
  2497. }
  2498. if dealer_id > 0 {
  2499. db = db.Where("dealer = ?", dealer_id)
  2500. }
  2501. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  2502. return goodInfo, err
  2503. }
  2504. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  2505. db := readDb.Model(&models.Warehousing{})
  2506. likekey := "%" + keywords + "%"
  2507. db = db.Where("org_id = ? AND status = 1 ", orgId)
  2508. db = db.Where("warehousing_order LIKE ? ", likekey)
  2509. db = db.Count(&total)
  2510. offset := (page - 1) * limit
  2511. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  2512. return
  2513. }
  2514. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2515. info := models.WarehousingInfo{}
  2516. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2517. return info, err
  2518. }
  2519. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  2520. info := models.WarehousingInfo{}
  2521. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  2522. return info, err
  2523. }
  2524. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  2525. err = writeDb.Save(&info).Error
  2526. return err
  2527. }
  2528. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  2529. err = writeDb.Save(&info).Error
  2530. return err
  2531. }
  2532. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  2533. 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
  2534. return err
  2535. }
  2536. func UpDateDrugWarehousingInfoSix(id int64, info *models.DrugWarehouseInfo) (err error) {
  2537. 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
  2538. return err
  2539. }
  2540. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  2541. info := models.DrugWarehouseInfo{}
  2542. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  2543. return info, err
  2544. }
  2545. func EditWarehousing(warehouse models.Warehousing) {
  2546. 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
  2547. }
  2548. func EditNewWarehousing(warehouse models.Warehousing, tx *gorm.DB) {
  2549. err = tx.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", warehouse.WarehousingOrder).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehousing_time": warehouse.WarehousingTime, "modifier": warehouse.Modifier, "dealer": warehouse.Dealer, "manufacturer": warehouse.Manufacturer, "storehouse_id": warehouse.StorehouseId}).Error
  2550. if err != nil {
  2551. tx.Rollback()
  2552. return
  2553. }
  2554. return
  2555. }
  2556. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  2557. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2558. 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)
  2559. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2560. return list, err
  2561. }
  2562. func DeleteSalesReturn(ids []string) (err error) {
  2563. ut := writeDb.Begin()
  2564. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2565. if err != nil {
  2566. ut.Rollback()
  2567. return
  2568. }
  2569. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2570. if err != nil {
  2571. ut.Rollback()
  2572. return
  2573. }
  2574. ut.Commit()
  2575. return
  2576. }
  2577. func DeleteDrugSalesReturn(ids []string) (err error) {
  2578. ut := writeDb.Begin()
  2579. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2580. if err != nil {
  2581. ut.Rollback()
  2582. return
  2583. }
  2584. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2585. if err != nil {
  2586. ut.Rollback()
  2587. return
  2588. }
  2589. ut.Commit()
  2590. return
  2591. }
  2592. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  2593. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  2594. return
  2595. }
  2596. type DrugSalesReturnInfo struct {
  2597. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2598. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2599. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  2600. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2601. Price float64 `gorm:"column:price" json:"price" form:"price"`
  2602. Total float64 `gorm:"column:total" json:"total" form:"total"`
  2603. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  2604. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  2605. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2606. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2607. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2608. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2609. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  2610. Type int64 `gorm:"column:type" json:"type" form:"type"`
  2611. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  2612. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  2613. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  2614. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  2615. Number string `gorm:"column:number" json:"number" form:"number"`
  2616. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2617. }
  2618. func (DrugSalesReturnInfo) TableName() string {
  2619. return "xt_drug_sales_return_info"
  2620. }
  2621. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  2622. 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
  2623. return
  2624. }
  2625. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2626. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2627. return
  2628. }
  2629. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  2630. 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
  2631. return
  2632. }
  2633. func UpDateSaleReturnStatus(id int64) (err error) {
  2634. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2635. return
  2636. }
  2637. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  2638. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2639. return
  2640. }
  2641. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  2642. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2643. return salesReturn, err
  2644. }
  2645. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  2646. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  2647. return salesReturn, err
  2648. }
  2649. func EditSaleReturn(sales models.SalesReturn) {
  2650. 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
  2651. }
  2652. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  2653. 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
  2654. }
  2655. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  2656. err = writeDb.Save(&info).Error
  2657. return err
  2658. }
  2659. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  2660. err = writeDb.Save(&info).Error
  2661. return err
  2662. }
  2663. func DeleteWarehouseOut(ids []string) (err error) {
  2664. ut := writeDb.Begin()
  2665. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2666. if err != nil {
  2667. ut.Rollback()
  2668. return
  2669. }
  2670. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2671. if err != nil {
  2672. ut.Rollback()
  2673. return
  2674. }
  2675. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2676. if err != nil {
  2677. ut.Rollback()
  2678. return
  2679. }
  2680. ut.Commit()
  2681. return
  2682. }
  2683. func DeleteDrugWarehouseOut(ids []string) (err error) {
  2684. ut := writeDb.Begin()
  2685. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2686. if err != nil {
  2687. ut.Rollback()
  2688. return
  2689. }
  2690. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2691. if err != nil {
  2692. ut.Rollback()
  2693. return
  2694. }
  2695. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2696. if err != nil {
  2697. ut.Rollback()
  2698. return
  2699. }
  2700. ut.Commit()
  2701. return
  2702. }
  2703. func UpDateWarehouseOutStatus(id int64) (err error) {
  2704. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2705. return
  2706. }
  2707. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2708. ut := writeDb.Begin()
  2709. err = ut.Save(&info).Error
  2710. if err != nil {
  2711. ut.Rollback()
  2712. return err
  2713. }
  2714. ut.Commit()
  2715. return err
  2716. }
  2717. func UpDateNewDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo, tx *gorm.DB) (err error) {
  2718. err = tx.Save(&info).Error
  2719. if err != nil {
  2720. tx.Rollback()
  2721. return err
  2722. }
  2723. return err
  2724. }
  2725. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2726. ut := writeDb.Begin()
  2727. err = ut.Save(&info).Error
  2728. if err != nil {
  2729. ut.Rollback()
  2730. return err
  2731. }
  2732. ut.Commit()
  2733. return err
  2734. }
  2735. func UpDateNewWarehouseInfoByStock(info *models.WarehousingInfo, tx *gorm.DB) (err error) {
  2736. err = tx.Save(&info).Error
  2737. if err != nil {
  2738. tx.Rollback()
  2739. return err
  2740. }
  2741. return err
  2742. }
  2743. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2744. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2745. return
  2746. }
  2747. func EditWarehouseOut(warehouseOut models.WarehouseOut, tx *gorm.DB) {
  2748. err = tx.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", warehouseOut.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehouse_out_time": warehouseOut.WarehouseOutTime, "dealer": warehouseOut.Dealer, "manufacturer": warehouseOut.Manufacturer, "storehouse_id": warehouseOut.StorehouseId}).Error
  2749. if err != nil {
  2750. tx.Rollback()
  2751. return
  2752. }
  2753. return
  2754. }
  2755. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2756. 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
  2757. }
  2758. func EditNewDrugWarehouseOut(warehouseOut models.DrugWarehouseOut, tx *gorm.DB) {
  2759. err = tx.Model(&models.DrugWarehouseOut{}).Where("id = ? AND status = 1", warehouseOut.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehouse_out_time": warehouseOut.WarehouseOutTime, "dealer": warehouseOut.Dealer, "manufacturer": warehouseOut.Manufacturer, "storehouse_id": warehouseOut.StorehouseId}).Error
  2760. if err != nil {
  2761. tx.Rollback()
  2762. return
  2763. }
  2764. return
  2765. }
  2766. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) (err error) {
  2767. err = tx.Save(&info).Error
  2768. if err != nil {
  2769. tx.Rollback()
  2770. return err
  2771. }
  2772. return err
  2773. }
  2774. 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) {
  2775. db := readDb.Model(&models.CancelStock{})
  2776. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2777. if len(keywords) > 0 {
  2778. likeKey := "%" + keywords + "%"
  2779. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2780. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2781. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey).Group("xt_cancel_stock.id")
  2782. }
  2783. if startTime > 0 {
  2784. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2785. }
  2786. if endTime > 0 {
  2787. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2788. }
  2789. if storehouse_id > 0 {
  2790. db = db.Where("xt_cancel_stock.storehouse_id = ?", storehouse_id)
  2791. }
  2792. if check_type > 0 {
  2793. db = db.Where("xt_cancel_stock.is_check = ?", check_type)
  2794. }
  2795. db = db.Count(&total)
  2796. offset := (page - 1) * limit
  2797. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2798. return
  2799. }
  2800. 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) {
  2801. db := readDb.Model(&models.DrugCancelStock{})
  2802. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2803. if len(keywords) > 0 {
  2804. likeKey := "%" + keywords + "%"
  2805. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2806. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2807. 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")
  2808. }
  2809. if startTime > 0 {
  2810. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2811. }
  2812. if endTime > 0 {
  2813. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2814. }
  2815. if storehoue_id > 0 {
  2816. db = db.Where("xt_drug_cancel_stock.storehouse_id = ?", storehoue_id)
  2817. }
  2818. if check_type > 0 {
  2819. db = db.Where("xt_drug_cancel_stock.is_check = ?", check_type)
  2820. }
  2821. db = db.Count(&total)
  2822. offset := (page - 1) * limit
  2823. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2824. return
  2825. }
  2826. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2827. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2828. return info, err
  2829. }
  2830. func DeleteCancelStock(ids []string) (err error) {
  2831. ut := writeDb.Begin()
  2832. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2833. if err != nil {
  2834. ut.Rollback()
  2835. return
  2836. }
  2837. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2838. if err != nil {
  2839. ut.Rollback()
  2840. return
  2841. }
  2842. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2843. if err != nil {
  2844. ut.Rollback()
  2845. return
  2846. }
  2847. ut.Commit()
  2848. return
  2849. }
  2850. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2851. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2852. return info, err
  2853. }
  2854. func DeleteDrugCancelStock(ids []string) (err error) {
  2855. ut := writeDb.Begin()
  2856. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2857. if err != nil {
  2858. ut.Rollback()
  2859. return
  2860. }
  2861. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2862. if err != nil {
  2863. ut.Rollback()
  2864. return
  2865. }
  2866. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2867. if err != nil {
  2868. ut.Rollback()
  2869. return
  2870. }
  2871. ut.Commit()
  2872. return
  2873. }
  2874. func UpDateCancleStockStatus(id int64) (err error) {
  2875. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2876. return
  2877. }
  2878. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2879. info := models.CancelStockInfo{}
  2880. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2881. return info, err
  2882. }
  2883. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2884. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2885. return err
  2886. }
  2887. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2888. 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
  2889. return
  2890. }
  2891. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2892. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2893. return
  2894. }
  2895. func UpDateDrugCancelStockById(id int64) (err error) {
  2896. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2897. return
  2898. }
  2899. func EditCancelStock(sales models.CancelStock) {
  2900. 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
  2901. }
  2902. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2903. 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
  2904. }
  2905. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2906. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2907. return cancelStock, err
  2908. }
  2909. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2910. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2911. return cancelStock, err
  2912. }
  2913. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2914. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2915. return cancelStock, err
  2916. }
  2917. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2918. err = writeDb.Save(&info).Error
  2919. return err
  2920. }
  2921. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2922. err = writeDb.Save(&info).Error
  2923. return err
  2924. }
  2925. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2926. 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
  2927. return list, err
  2928. }
  2929. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2930. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2931. 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)
  2932. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2933. return list, err
  2934. }
  2935. type StockDetailModel struct {
  2936. GoodId int64 `gorm:"good_id" json:"good_id"`
  2937. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2938. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2939. Price float64 `gorm:"price" json:"price"`
  2940. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2941. Type int `gorm:"type" json:"type"`
  2942. Creater int64 `gorm:"creater" json:"creater"`
  2943. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2944. Types int64 `gorm:"types" json:"types"`
  2945. }
  2946. func FindStockInDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (list []*models.WarehousingInfo, total int64, err error) {
  2947. db := readDb.Model(&models.WarehousingInfo{})
  2948. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1 AND xt_warehouse_info.is_check = 1", orgId)
  2949. if good_id > 0 {
  2950. db = db.Where("xt_warehouse_info.good_id = ?", good_id)
  2951. }
  2952. if len(keywords) > 0 {
  2953. likeKey := "%" + keywords + "%"
  2954. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2955. 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)
  2956. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2957. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2958. 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")
  2959. } else {
  2960. 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)
  2961. }
  2962. if manufacturer > 0 {
  2963. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2964. }
  2965. if storehouse_id > 0 {
  2966. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  2967. }
  2968. if order_type > 0 {
  2969. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2970. }
  2971. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2972. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2973. db = db.Count(&total)
  2974. offset := (page - 1) * limit
  2975. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.id desc").Find(&list).Error
  2976. return
  2977. }
  2978. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (err error, totalPrice float64) {
  2979. db := readDb.Model(&models.WarehousingInfo{})
  2980. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2981. if good_id > 0 {
  2982. db = db.Where("good_id = ?", good_id)
  2983. }
  2984. if len(keywords) > 0 {
  2985. likeKey := "%" + keywords + "%"
  2986. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2987. 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)
  2988. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2989. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2990. 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")
  2991. } else {
  2992. 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)
  2993. }
  2994. if manufacturer > 0 {
  2995. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2996. }
  2997. if order_type > 0 {
  2998. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2999. }
  3000. if storehouse_id > 0 {
  3001. db = db.Where("xt_warehouse_info.storehouse_id = ?", storehouse_id)
  3002. }
  3003. type PriceAmount struct {
  3004. Total float64
  3005. }
  3006. var result []PriceAmount
  3007. err = db.Select("total_price as total").Scan(&result).Error
  3008. var total float64
  3009. for _, value := range result {
  3010. total = total + value.Total
  3011. }
  3012. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3013. return err, total
  3014. }
  3015. func FloatRound(f float64, n int) float64 {
  3016. format := "%." + strconv.Itoa(n) + "f"
  3017. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  3018. return res
  3019. }
  3020. func FindStockOutDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64, order_way int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  3021. if orgId == 9671 {
  3022. db := readDb.Model(&models.WarehouseOutInfo{})
  3023. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.is_check = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 AND xt_warehouse_out_info.sys_record_time>=? AND xt_warehouse_out_info.sys_record_time<=?", orgId, startTime, endTime)
  3024. if len(keywords) > 0 {
  3025. likeKey := "%" + keywords + "%"
  3026. 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)
  3027. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3028. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3029. 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")
  3030. } else {
  3031. 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)
  3032. }
  3033. if manufacturer > 0 {
  3034. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3035. }
  3036. if order_type > 0 {
  3037. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3038. }
  3039. if storehouse_id > 0 {
  3040. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3041. }
  3042. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  3043. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ?", orgId)
  3044. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3045. db = db.Count(&total)
  3046. offset := (page - 1) * limit
  3047. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3048. return
  3049. } else {
  3050. db := readDb.Model(&models.WarehouseOutInfo{})
  3051. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.is_check = 1 AND xt_warehouse_out_info.count <> 0 ", orgId)
  3052. if good_id > 0 {
  3053. db = db.Where("good_id = ?", good_id)
  3054. }
  3055. if order_way == 1 {
  3056. db = db.Where("xt_warehouse_out_info.is_sys =0")
  3057. }
  3058. if order_way == 2 {
  3059. db = db.Where("xt_warehouse_out_info.is_sys !=0")
  3060. }
  3061. if len(keywords) > 0 {
  3062. likeKey := "%" + keywords + "%"
  3063. 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)
  3064. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3065. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3066. 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")
  3067. } else {
  3068. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id and xt_warehouse_out.org_id = xt_warehouse_out_info.org_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  3069. }
  3070. if manufacturer > 0 {
  3071. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3072. }
  3073. if order_type > 0 {
  3074. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3075. }
  3076. if storehouse_id > 0 {
  3077. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3078. }
  3079. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ? and is_check =1", orgId)
  3080. db = db.Preload("WarehousingInfoNight", "status=1 and org_id = ? and is_check =1", orgId)
  3081. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3082. db = db.Count(&total)
  3083. offset := (page - 1) * limit
  3084. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.id desc").Find(&list).Error
  3085. return
  3086. }
  3087. }
  3088. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64, order_way int64) (err error, totalPrice float64) {
  3089. db := readDb.Model(&models.WarehouseOutInfo{})
  3090. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.count > 0", orgId)
  3091. if len(keywords) > 0 {
  3092. likeKey := "%" + keywords + "%"
  3093. 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)
  3094. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  3095. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  3096. 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")
  3097. } else {
  3098. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id and xt_warehouse_out.org_id = xt_warehouse_out_info.org_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  3099. }
  3100. if good_id > 0 {
  3101. db = db.Where("xt_warehouse_out_info.good_id = ?", good_id)
  3102. }
  3103. if order_way == 1 {
  3104. db = db.Where("xt_warehouse_out_info.is_sys = ?", 0)
  3105. }
  3106. if order_way == 2 {
  3107. db = db.Where("xt_warehouse_out_info.is_sys != ?", 0)
  3108. }
  3109. if manufacturer > 0 {
  3110. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  3111. }
  3112. if order_type > 0 {
  3113. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  3114. }
  3115. if storehouse_id > 0 {
  3116. db = db.Where("xt_warehouse_out_info.storehouse_id = ?", storehouse_id)
  3117. }
  3118. type PriceAmount struct {
  3119. Total float64
  3120. }
  3121. var result []PriceAmount
  3122. err = db.Select("(count * price) as total").Scan(&result).Error
  3123. var total float64
  3124. for _, value := range result {
  3125. total = total + value.Total
  3126. }
  3127. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  3128. fmt.Println(total)
  3129. return err, total
  3130. }
  3131. 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) {
  3132. db := readDb.Model(&models.SalesReturnInfo{})
  3133. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  3134. if len(keywords) > 0 {
  3135. likeKey := "%" + keywords + "%"
  3136. 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)
  3137. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  3138. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  3139. 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")
  3140. } else {
  3141. 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)
  3142. }
  3143. if manufacturer > 0 {
  3144. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  3145. }
  3146. if order_type > 0 {
  3147. db = db.Where("xt_sales_return_info.type = ?", order_type)
  3148. }
  3149. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  3150. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3151. offset := (page - 1) * limit
  3152. db = db.Count(&total)
  3153. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  3154. return
  3155. }
  3156. func FindeCancelGroup(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (list []*models.CancelStockInfo, err error) {
  3157. db := readDb.Model(&models.CancelStockInfo{})
  3158. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3159. if len(keywords) > 0 {
  3160. likeKey := "%" + keywords + "%"
  3161. 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)
  3162. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3163. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3164. 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")
  3165. } else {
  3166. 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)
  3167. }
  3168. if manufacturer > 0 {
  3169. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3170. }
  3171. if order_type > 0 {
  3172. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3173. }
  3174. if storehouse_id > 0 {
  3175. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", order_type)
  3176. }
  3177. if good_id > 0 {
  3178. db = db.Where("xt_cancel_stock_info.good_id = ?", good_id)
  3179. }
  3180. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3181. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3182. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  3183. return
  3184. }
  3185. func FindCancelDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64, storehouse_id int64, good_id int64) (list []*models.CancelStockInfo, total int64, err error) {
  3186. db := readDb.Model(&models.CancelStockInfo{})
  3187. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  3188. if len(keywords) > 0 {
  3189. likeKey := "%" + keywords + "%"
  3190. 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)
  3191. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  3192. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  3193. 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")
  3194. } else {
  3195. 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)
  3196. }
  3197. if manufacturer > 0 {
  3198. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  3199. }
  3200. if order_type > 0 {
  3201. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  3202. }
  3203. if storehouse_id > 0 {
  3204. db = db.Where("xt_cancel_stock_info.storehouse_id = ?", storehouse_id)
  3205. }
  3206. if good_id > 0 {
  3207. db = db.Where("xt_cancel_stock_info.good_id =?", good_id)
  3208. }
  3209. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  3210. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  3211. db = db.Count(&total)
  3212. offset := (page - 1) * limit
  3213. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.id desc").Find(&list).Error
  3214. return
  3215. }
  3216. func FindStockInByDealerId(id int64) (total int64, err error) {
  3217. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  3218. return
  3219. }
  3220. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  3221. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  3222. return
  3223. }
  3224. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  3225. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  3226. return
  3227. }
  3228. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  3229. 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)
  3230. return
  3231. }
  3232. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  3233. 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)
  3234. return
  3235. }
  3236. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  3237. 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)
  3238. return
  3239. }
  3240. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  3241. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  3242. return goodType, err
  3243. }
  3244. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  3245. 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
  3246. return info, err
  3247. }
  3248. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  3249. 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
  3250. return
  3251. }
  3252. func FindStockOutByIsSysOne(org_id int64, is_sys int, record_time int64, warehousing_out_order string, tx *gorm.DB) (out models.WarehouseOut, err error) {
  3253. err = tx.Model(&models.WarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ? and warehouse_out_order_number = ?", is_sys, org_id, record_time, warehousing_out_order).First(&out).Error
  3254. if err != gorm.ErrRecordNotFound {
  3255. if err != nil {
  3256. tx.Rollback()
  3257. return
  3258. }
  3259. }
  3260. return
  3261. }
  3262. func FindStockOutByIsSysSix(org_id int64, is_sys int, record_time int64, second_warehouse_id int64) (out models.WarehouseOut, err error) {
  3263. 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
  3264. return
  3265. }
  3266. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3267. 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
  3268. return
  3269. }
  3270. func FindNewDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64, tx *gorm.DB) (out models.DrugWarehouseOut, err error) {
  3271. err = tx.Model(&models.DrugWarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ?", is_sys, org_id, record_time).First(&out).Error
  3272. if err != gorm.ErrRecordNotFound {
  3273. if err != nil {
  3274. tx.Rollback()
  3275. return
  3276. }
  3277. }
  3278. return
  3279. }
  3280. func FindDrugSettleStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  3281. 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
  3282. return
  3283. }
  3284. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  3285. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  3286. return
  3287. }
  3288. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  3289. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  3290. return
  3291. }
  3292. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3293. info := models.WarehouseOutInfo{}
  3294. 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
  3295. if err == gorm.ErrRecordNotFound {
  3296. return nil, err
  3297. }
  3298. if err != nil {
  3299. return nil, err
  3300. }
  3301. return &info, nil
  3302. }
  3303. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3304. info := models.WarehouseOutInfo{}
  3305. 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
  3306. if err == gorm.ErrRecordNotFound {
  3307. return nil, err
  3308. }
  3309. if err != nil {
  3310. return nil, err
  3311. }
  3312. return &info, nil
  3313. }
  3314. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3315. 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
  3316. return err
  3317. }
  3318. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  3319. 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
  3320. return err
  3321. }
  3322. func GetGoodsIsExsit(good_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  3323. outInfo := models.WarehouseOutInfo{}
  3324. err := tx.Where("good_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", good_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3325. if err != gorm.ErrRecordNotFound {
  3326. if err != nil {
  3327. tx.Rollback()
  3328. return outInfo, err
  3329. }
  3330. }
  3331. return outInfo, err
  3332. }
  3333. func GetDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64) (models.XtDrugWarehouseOutInfo, error) {
  3334. outInfo := models.XtDrugWarehouseOutInfo{}
  3335. err := XTReadDB().Where("drug_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", drug_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3336. return outInfo, err
  3337. }
  3338. func GetNewDrugsIsExsit(drug_id int64, sys_record_time int64, org_id int64, warehouse_out_id int64, tx *gorm.DB) (models.XtDrugWarehouseOutInfo, error) {
  3339. outInfo := models.XtDrugWarehouseOutInfo{}
  3340. err := tx.Where("drug_id = ? and sys_record_time = ? and org_id = ? and is_sys =0 and status = 1 and warehouse_out_id = ?", drug_id, sys_record_time, org_id, warehouse_out_id).Find(&outInfo).Error
  3341. if err != gorm.ErrRecordNotFound {
  3342. if err != nil {
  3343. tx.Rollback()
  3344. return outInfo, err
  3345. }
  3346. }
  3347. return outInfo, err
  3348. }
  3349. func AddSigleWarehouseOutInfoOne(info *models.WarehouseOutInfo) error {
  3350. err := XTWriteDB().Create(&info).Error
  3351. return err
  3352. }
  3353. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3354. err := tx.Create(&info).Error
  3355. if err != nil {
  3356. tx.Rollback()
  3357. return err
  3358. }
  3359. return err
  3360. }
  3361. func AddNewSigleWarehouseOutInfo(info *models.WarehouseOutInfo, tx *gorm.DB) error {
  3362. err := tx.Create(&info).Error
  3363. if err != nil {
  3364. tx.Rollback()
  3365. return err
  3366. }
  3367. return err
  3368. }
  3369. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  3370. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3371. return err
  3372. }
  3373. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  3374. err := writeDb.Create(&info).Error
  3375. return err
  3376. }
  3377. func AddSelfSigleDrugWarehouseOutInfo(info *models.XtSelfDrugWarehouseOutInfo, tx *gorm.DB) error {
  3378. err := tx.Create(&info).Error
  3379. if err != nil {
  3380. tx.Rollback()
  3381. return err
  3382. }
  3383. return err
  3384. }
  3385. func AddNewSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, tx *gorm.DB) error {
  3386. err := tx.Create(&info).Error
  3387. if err != nil {
  3388. tx.Rollback()
  3389. return err
  3390. }
  3391. return err
  3392. }
  3393. func UpdateSigleDrugWarehouseOutInfoDetail(id int64, count int64, tx *gorm.DB) error {
  3394. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3395. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).UpdateColumn("over_count", gorm.Expr("over_count - ?", count)).Error
  3396. if err != nil {
  3397. tx.Rollback()
  3398. return err
  3399. }
  3400. return err
  3401. }
  3402. func UpdateSigleDrugWarehouseOutInfoDetailOne(id int64, count int64, tx *gorm.DB) error {
  3403. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"count": count}).Error
  3404. return err
  3405. }
  3406. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  3407. ut := writeDb.Begin()
  3408. err := ut.Create(&detail).Error
  3409. if err != nil {
  3410. ut.Rollback()
  3411. return err
  3412. }
  3413. ut.Commit()
  3414. return err
  3415. }
  3416. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  3417. err := writeDb.Create(&detail).Error
  3418. return err
  3419. }
  3420. func GetDrugAutoMaticReduceDetail(drug_id int64, patient_id int64, record_time int64, org_id int64, adivce_id int64, warehouse_info_id int64, tx *gorm.DB) (models.DrugAutomaticReduceDetail, error) {
  3421. detail := models.DrugAutomaticReduceDetail{}
  3422. err := tx.Where("drug_id = ? and patient_id =? and record_time =? and org_id = ? and advice_id = ? and warehouse_info_id =?", drug_id, patient_id, record_time, org_id, adivce_id, warehouse_info_id).Find(&detail).Error
  3423. if err != gorm.ErrRecordNotFound {
  3424. if err != nil {
  3425. tx.Rollback()
  3426. return detail, err
  3427. }
  3428. }
  3429. return detail, err
  3430. }
  3431. func UpdateNewSingleDrugAutoReduceRecordInfo(id int64, count int64, tx *gorm.DB) error {
  3432. err := tx.Model(models.DrugAutomaticReduceDetail{}).Where("id = ? and status =1", id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  3433. if err != nil {
  3434. tx.Rollback()
  3435. return err
  3436. }
  3437. return err
  3438. }
  3439. func AddNewSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail, tx *gorm.DB) error {
  3440. err := tx.Create(&detail).Error
  3441. if err != nil {
  3442. tx.Rollback()
  3443. return err
  3444. }
  3445. return err
  3446. }
  3447. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  3448. 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
  3449. return
  3450. }
  3451. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  3452. 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
  3453. return
  3454. }
  3455. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  3456. 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
  3457. return
  3458. }
  3459. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  3460. info := models.WarehouseOutInfo{}
  3461. 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
  3462. return info, err
  3463. }
  3464. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  3465. 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
  3466. return
  3467. }
  3468. func UpdateStockOutInfoCount(org_id int64, id int64) {
  3469. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  3470. }
  3471. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3472. db := readDb.Model(&models.AutomaticReduceDetail{})
  3473. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3474. db = db.Preload("GoodsType", "status = 1")
  3475. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3476. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  3477. db = db.Count(&total)
  3478. err = db.Find(&user).Error
  3479. return
  3480. }
  3481. type DrugAutomaticReduceDetail struct {
  3482. ID int64 `gorm:"column:id" json:"id" form:"id"`
  3483. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  3484. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  3485. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  3486. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  3487. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  3488. Status int64 `gorm:"column:status" json:"status" form:"status"`
  3489. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  3490. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  3491. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  3492. Count int64 `gorm:"column:count" json:"count" form:"count"`
  3493. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  3494. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  3495. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  3496. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  3497. }
  3498. func (DrugAutomaticReduceDetail) TableName() string {
  3499. return "xt_drug_automatic_reduce_detail"
  3500. }
  3501. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  3502. db := readDb.Model(&DrugAutomaticReduceDetail{})
  3503. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  3504. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3505. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  3506. db = db.Count(&total)
  3507. err = db.Find(&user).Error
  3508. return
  3509. }
  3510. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  3511. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3512. }
  3513. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  3514. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  3515. }
  3516. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  3517. 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
  3518. return
  3519. }
  3520. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  3521. 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
  3522. return
  3523. }
  3524. type VMWarehousingInfo struct {
  3525. ID int64 `gorm:"column:id" json:"id"`
  3526. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  3527. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  3528. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  3529. Status int64 `gorm:"column:status" json:"status"`
  3530. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3531. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  3532. }
  3533. func (VMWarehousingInfo) TableName() string {
  3534. return "xt_warehouse_info"
  3535. }
  3536. type GoodInfo struct {
  3537. ID int64 `gorm:"column:id" json:"id"`
  3538. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  3539. Status int64 `gorm:"column:status" json:"status"`
  3540. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  3541. }
  3542. func (GoodInfo) TableName() string {
  3543. return "xt_good_information"
  3544. }
  3545. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  3546. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  3547. return
  3548. }
  3549. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  3550. 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
  3551. return
  3552. }
  3553. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  3554. likeKey := "%" + keyword + "%"
  3555. db := XTReadDB().Model(&goods)
  3556. if len(keyword) > 0 {
  3557. db = db.Where("type_name like ?", likeKey)
  3558. }
  3559. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  3560. return goods, err
  3561. }
  3562. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  3563. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3 or stock_attribute = 4)", orgid).
  3564. Preload("GoodInfo", "org_id = ? and status = 1 and find_in_set('停用',good_status) = 0", orgid).Find(&goodstype).Error
  3565. return goodstype, err
  3566. }
  3567. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  3568. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  3569. if orgid > 0 {
  3570. db = db.Where("x.user_org_id = ?", orgid)
  3571. }
  3572. if recordtime > 0 {
  3573. db = db.Where("x.record_date = ?", recordtime)
  3574. }
  3575. if patientId > 0 {
  3576. db = db.Where("x.patient_id = ?", patientId)
  3577. }
  3578. 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
  3579. return prepare, err
  3580. }
  3581. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  3582. detail := models.XtAutomaticReduceDetail{}
  3583. 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
  3584. if err == gorm.ErrRecordNotFound {
  3585. return nil, err
  3586. }
  3587. if err != nil {
  3588. return nil, err
  3589. }
  3590. return &detail, nil
  3591. }
  3592. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3593. 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
  3594. return autoMatic, err
  3595. }
  3596. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3597. 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
  3598. return autoMatic, err
  3599. }
  3600. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  3601. var manufacturer models.Manufacturer
  3602. var err error
  3603. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  3604. if err == gorm.ErrRecordNotFound {
  3605. return nil, err
  3606. }
  3607. if err != nil {
  3608. return nil, err
  3609. }
  3610. return &manufacturer, nil
  3611. }
  3612. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  3613. err := XTWriteDB().Create(manufacturer).Error
  3614. return err
  3615. }
  3616. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  3617. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  3618. return err
  3619. }
  3620. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  3621. var good models.GoodInfo
  3622. var err error
  3623. 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
  3624. if err == gorm.ErrRecordNotFound {
  3625. return nil, err
  3626. }
  3627. if err != nil {
  3628. return nil, err
  3629. }
  3630. return &good, nil
  3631. }
  3632. func CreateGoodsInfomation(info *models.GoodInfo) error {
  3633. err := XTWriteDB().Create(&info).Error
  3634. return err
  3635. }
  3636. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  3637. err := XTWriteDB().Model(&info).Where("good_name = ? and good_type_id = ? and specification_name = ? and org_id = ? and status = 1", goodName, goodType, specificationName, orgid).Updates(map[string]interface{}{"good_name": info.GoodName, "good_type_id": info.GoodTypeId, "medical_insurance_level": info.MedicalInsuranceLevel, "good_kind": info.GoodKind, "specification_name": info.SpecificationName, "manufacturer": info.Manufacturer, "good_unit": info.GoodUnit, "retail_price": info.RetailPrice, "stock_warn_count": info.StockWarnCount, "dealer": info.Dealer, "pinyin": info.Pinyin, "wubi": info.Wubi, "buy_price": info.BuyPrice, "medical_insurance_number": info.MedicalInsuranceNumber, "is_special_diseases": info.IsSpecialDiseases, "is_record": info.IsRecord, "statistics_category": info.StatisticsCategory, "good_status": info.GoodStatus, "social_security_directory_code": info.SocialSecurityDirectoryCode, "production_type": info.ProductionType, "special_medical": info.SpecialMedical, "remark": info.Remark, "number": info.Number, "register_number": info.RegisterNumber}).Error
  3638. return err
  3639. }
  3640. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  3641. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3642. 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
  3643. return houseOutInfo, err
  3644. }
  3645. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  3646. key := "%" + keyword + "%"
  3647. if len(keyword) == 0 {
  3648. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  3649. } else {
  3650. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  3651. }
  3652. return goodInfo, err
  3653. }
  3654. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  3655. var drug models.BaseDrugLib
  3656. var err error
  3657. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  3658. if err == gorm.ErrRecordNotFound {
  3659. return nil, err
  3660. }
  3661. if err != nil {
  3662. return nil, err
  3663. }
  3664. return &drug, nil
  3665. }
  3666. func IsExistDrugByNameOne(drugname string, dosage string, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  3667. var drug models.BaseDrugLib
  3668. var err error
  3669. 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
  3670. if err == gorm.ErrRecordNotFound {
  3671. return nil, err
  3672. }
  3673. if err != nil {
  3674. return nil, err
  3675. }
  3676. return &drug, nil
  3677. }
  3678. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  3679. err := XTWriteDB().Create(&info).Error
  3680. return err
  3681. }
  3682. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  3683. err := XTWriteDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and org_id = ? and status = 1", drugName, drugSpec, orgid).Updates(map[string]interface{}{"drug_name": drug.DrugName, "drug_alias": drug.DrugAlias, "drug_spec": drug.DrugSpec, "drug_type": drug.DrugType, "drug_stock_limit": drug.DrugStockLimit, "drug_origin_place": drug.DrugOriginPlace, "drug_dosage_form": drug.DrugDosageForm, "medical_insurance_level": drug.MedicalInsuranceLevel, "max_unit": drug.MaxUnit, "min_unit": drug.MinUnit, "unit_matrixing": drug.UnitMatrixing, "retail_price": drug.RetailPrice, "last_price": drug.LastPrice, "drug_classify": drug.DrugClassify, "manufacturer": drug.Manufacturer, "dealer": drug.Dealer, "pinyin": drug.Pinyin, "wubi": drug.Wubi, "drug_alias_pinyin": drug.DrugAliasPinyin, "drug_alias_wubi": drug.DrugAliasWubi, "drug_category": drug.DrugCategory, "drug_control": drug.DrugControl, "number": drug.Number, "hosp_appr_flag": drug.HospApprFlag, "medical_insurance_number": drug.MedicalInsuranceNumber, "pharmacology_category": drug.PharmacologyCategory, "statistics_category": drug.StatisticsCategory, "code": drug.Code, "is_special_diseases": drug.IsSpecialDiseases, "is_record": drug.IsRecord, "prescription_mark": drug.PrescriptionMark, "social_security_directory_code": drug.SocialSecurityDirectoryCode, "record_date": drug.RecordDate, "drug_remark": drug.DrugRemark, "drug_status": drug.DrugStatus, "limit_remark": drug.LimitRemark, "drug_dose": drug.DrugDose, "single_dose": drug.DrugDose, "drug_dose_unit": drug.DrugDoseUnit, "lmt_used_flag": drug.LmtUsedFlag, "delivery_way": drug.DeliveryWay, "execution_frequency": drug.ExecutionFrequency, "drug_day": drug.DrugDay}).Error
  3684. return err
  3685. }
  3686. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  3687. var projoect models.HisProject
  3688. var err error
  3689. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  3690. if err == gorm.ErrRecordNotFound {
  3691. return nil, err
  3692. }
  3693. if err != nil {
  3694. return nil, err
  3695. }
  3696. return &projoect, nil
  3697. }
  3698. func CreateProjectInfomation(info *models.HisProject) error {
  3699. err := XTWriteDB().Create(&info).Error
  3700. return err
  3701. }
  3702. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  3703. 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
  3704. return err
  3705. }
  3706. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  3707. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  3708. return warehouseinfo, err
  3709. }
  3710. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) error {
  3711. 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
  3712. return err
  3713. }
  3714. func EditNewDrugWarehousingOne(warehouse models.DrugWarehouse, id int64, tx *gorm.DB) error {
  3715. err := tx.Model(&models.DrugWarehouse{}).Where("warehousing_order = ? AND status = 1 and id = ?", warehouse.WarehousingOrder, id).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehousing_time": warehouse.WarehousingTime, "modifier": warehouse.Modifier, "dealer": warehouse.Dealer, "manufacturer": warehouse.Manufacturer, "storehouse_id": warehouse.StorehouseId}).Error
  3716. if err != nil {
  3717. tx.Rollback()
  3718. return err
  3719. }
  3720. return err
  3721. }
  3722. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  3723. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3724. return list, err
  3725. }
  3726. func GetNewAllManufacturerList(orgid int64) (list []*models.BloodManufacturer, err error) {
  3727. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3728. return list, err
  3729. }
  3730. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  3731. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3732. return list, err
  3733. }
  3734. // func GetAllDealerListTX(orgid int64,tx *gorm.DB) (list []*models.Dealer, err error) {
  3735. // err = tx.Where("org_id = ? and status = 1", orgid).Find(&list).Error
  3736. // return list, err
  3737. // }
  3738. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  3739. var dealer models.Dealer
  3740. var err error
  3741. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  3742. if err == gorm.ErrRecordNotFound {
  3743. return nil, err
  3744. }
  3745. if err != nil {
  3746. return nil, err
  3747. }
  3748. return &dealer, nil
  3749. }
  3750. func CreateDealer(dealer *models.Dealer) error {
  3751. err := XTWriteDB().Create(&dealer).Error
  3752. return err
  3753. }
  3754. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  3755. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  3756. return err
  3757. }
  3758. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  3759. dataconfig := models.DictDataconfig{}
  3760. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  3761. return dataconfig, err
  3762. }
  3763. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  3764. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  3765. return dataconfig, err
  3766. }
  3767. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  3768. var dataconfig models.DictDataconfig
  3769. var err error
  3770. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  3771. if err == gorm.ErrRecordNotFound {
  3772. return nil, err
  3773. }
  3774. if err != nil {
  3775. return nil, err
  3776. }
  3777. return &dataconfig, nil
  3778. }
  3779. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  3780. dataconfig := models.DictDataconfig{}
  3781. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  3782. return dataconfig, err
  3783. }
  3784. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  3785. err := XTWriteDB().Create(&dataconfig).Error
  3786. return err
  3787. }
  3788. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  3789. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  3790. return err
  3791. }
  3792. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  3793. var goodType models.GoodsType
  3794. var err error
  3795. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  3796. if err == gorm.ErrRecordNotFound {
  3797. return nil, err
  3798. }
  3799. if err != nil {
  3800. return nil, err
  3801. }
  3802. return &goodType, nil
  3803. }
  3804. func CreatedGoodType(goodsType *models.GoodsType) error {
  3805. err := XTWriteDB().Create(&goodsType).Error
  3806. return err
  3807. }
  3808. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  3809. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3810. return goodsType, err
  3811. }
  3812. func GetAllNewGoodType(orgid int64) (goodsType []*models.NewGoodsType, err error) {
  3813. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  3814. return goodsType, err
  3815. }
  3816. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  3817. var department models.XtHisDepartment
  3818. var err error
  3819. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  3820. if err == gorm.ErrRecordNotFound {
  3821. return nil, err
  3822. }
  3823. if err != nil {
  3824. return nil, err
  3825. }
  3826. return &department, nil
  3827. }
  3828. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  3829. dataconfig := models.Dataconfig{}
  3830. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  3831. return dataconfig, err
  3832. }
  3833. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  3834. var dataconfig models.Dataconfig
  3835. var err error
  3836. 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
  3837. if err == gorm.ErrRecordNotFound {
  3838. return nil, err
  3839. }
  3840. if err != nil {
  3841. return nil, err
  3842. }
  3843. return &dataconfig, nil
  3844. }
  3845. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  3846. dataconfig := models.Dataconfig{}
  3847. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  3848. return dataconfig, err
  3849. }
  3850. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  3851. err := XTWriteDB().Create(&dataconfig).Error
  3852. return err
  3853. }
  3854. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  3855. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  3856. return dataconfig, err
  3857. }
  3858. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  3859. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  3860. return list, err
  3861. }
  3862. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  3863. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3864. if len(keywords) > 0 {
  3865. likekey := "%" + keywords + "%"
  3866. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3867. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3868. }).Find(&drug).Error
  3869. } else {
  3870. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3871. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  3872. }).Find(&drug).Error
  3873. }
  3874. return
  3875. }
  3876. func GetSearchDrugListSix(keywords string, orgid int64, storehouse_id int64) (drug []*models.BaseDrugLib, err error) {
  3877. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and find_in_set('停用',drug_status) = 0", orgid)
  3878. if len(keywords) > 0 {
  3879. likekey := "%" + keywords + "%"
  3880. err = db.Where("(drug_name LIKE ?)", likekey).Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3881. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3882. }).Find(&drug).Error
  3883. } else {
  3884. err = db.Preload("LastDrugWarehouseInfo", "org_id=? and status = 1 and (stock_max_number > 0 or stock_min_number>0) and storehouse_id = ?", orgid, storehouse_id).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  3885. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
  3886. }).Find(&drug).Error
  3887. }
  3888. return
  3889. }
  3890. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  3891. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  3892. if len(keywords) > 0 {
  3893. likekey := "%" + keywords + "%"
  3894. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3895. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3896. }).Find(&good).Error
  3897. } else {
  3898. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3899. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  3900. }).Find(&good).Error
  3901. }
  3902. return
  3903. }
  3904. func GetSearchGoodListSix(keywords string, orgid int64, storehouse_id int64) (good []*models.GoodInfoSevenThirty, err error) {
  3905. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 AND find_in_set('停用',good_status) = 0", orgid)
  3906. if len(keywords) > 0 {
  3907. likekey := "%" + keywords + "%"
  3908. err = db.Where("(good_name Like ?)", likekey).Preload("LaStWarehousingInfo", "org_id=? and status = 1 and stock_count <> 0 and storehouse_id =?", orgid, storehouse_id).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3909. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id =?", orgid, storehouse_id)
  3910. }).Find(&good).Error
  3911. } else {
  3912. err = db.Preload("LaStWarehousingInfo", "org_id=? and status = 1 and stock_count <> 0 and storehouse_id =?", orgid, storehouse_id).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3913. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id)
  3914. }).Find(&good).Error
  3915. }
  3916. return
  3917. }
  3918. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  3919. db := writeDb.Begin()
  3920. err := db.Create(&detail).Error
  3921. if err != nil {
  3922. db.Rollback()
  3923. return err
  3924. }
  3925. db.Commit()
  3926. return err
  3927. }
  3928. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  3929. var autoreduece models.BloodAutomaticReduceDetail
  3930. var err error
  3931. 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
  3932. if err == gorm.ErrRecordNotFound {
  3933. return nil, err
  3934. }
  3935. if err != nil {
  3936. return nil, err
  3937. }
  3938. return &autoreduece, nil
  3939. }
  3940. func GetAutoMaticReduceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (models.BloodAutomaticReduceDetail, error) {
  3941. auto := models.BloodAutomaticReduceDetail{}
  3942. 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
  3943. return auto, err
  3944. }
  3945. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  3946. detail := models.BloodAutomaticReduceDetail{}
  3947. 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
  3948. return err
  3949. }
  3950. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  3951. 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
  3952. return stockflow, err
  3953. }
  3954. func UpdateAutoGood(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64, count int64) (err error) {
  3955. 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
  3956. 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
  3957. 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
  3958. return err
  3959. }
  3960. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3961. detail := models.BloodAutomaticReduceDetail{}
  3962. 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
  3963. info := models.WarehouseOutInfo{}
  3964. 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
  3965. prepare := models.DialysisBeforePrepare{}
  3966. 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
  3967. flow := models.VmStockFlow{}
  3968. 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
  3969. return err
  3970. }
  3971. func DeleteAutoRedeceDetailTwenty(orgid int64, patient_id int64, recordtime int64, goodid int64, project_id int64) error {
  3972. detail := models.BloodAutomaticReduceDetail{}
  3973. 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
  3974. info := models.WarehouseOutInfo{}
  3975. 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
  3976. prepare := models.DialysisBeforePrepare{}
  3977. 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
  3978. flow := models.VmStockFlow{}
  3979. 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
  3980. return err
  3981. }
  3982. func DeleteAutoRedeceDetailTenOne(orgid int64, patient_id int64, recordtime int64, goodid int64, order_id int64) error {
  3983. detail := models.BloodAutomaticReduceDetail{}
  3984. 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
  3985. info := models.WarehouseOutInfo{}
  3986. 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
  3987. prepare := models.DialysisBeforePrepare{}
  3988. 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
  3989. flow := models.VmStockFlow{}
  3990. 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
  3991. return err
  3992. }
  3993. func UpDateWarehouseInfoByStockDelete(id int64, count int64, patient_id int64, record_time int64, goodid int64) (err error) {
  3994. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3995. //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
  3996. return err
  3997. }
  3998. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3999. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  4000. return info, err
  4001. }
  4002. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  4003. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  4004. return list, err
  4005. }
  4006. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  4007. 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
  4008. return err
  4009. }
  4010. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  4011. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  4012. return err
  4013. }
  4014. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  4015. var autoreduece models.BloodAutomaticReduceDetail
  4016. var err error
  4017. 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
  4018. if err == gorm.ErrRecordNotFound {
  4019. return nil, err
  4020. }
  4021. if err != nil {
  4022. return nil, err
  4023. }
  4024. return &autoreduece, nil
  4025. }
  4026. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  4027. detail := models.WarehouseOutInfoSeven{}
  4028. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  4029. return detail, err
  4030. }
  4031. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  4032. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  4033. return detail, err
  4034. }
  4035. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  4036. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  4037. return project, err
  4038. }
  4039. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  4040. project := models.HisPrescriptionProject{}
  4041. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  4042. return project, err
  4043. }
  4044. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  4045. 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
  4046. return auto, err
  4047. }
  4048. func ModefyWarehouseInfo(count int64, id int64) error {
  4049. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4050. return err
  4051. }
  4052. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  4053. 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
  4054. 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
  4055. 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
  4056. //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
  4057. return err
  4058. }
  4059. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  4060. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4061. return
  4062. }
  4063. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  4064. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  4065. return
  4066. }
  4067. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  4068. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4069. return info, err
  4070. }
  4071. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4072. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  4073. return info, err
  4074. }
  4075. func GetGoodWarehouseInfoSevenTen(good_id int64, storehouse_id int64) (info []*models.WarehousingInfoSeven, err error) {
  4076. 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
  4077. return info, err
  4078. }
  4079. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  4080. offset := (page - 1) * limit
  4081. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  4082. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4083. fmt.Println(table)
  4084. if startime > 0 {
  4085. db = db.Where("x.ctime>=?", startime)
  4086. }
  4087. if endtime > 0 {
  4088. db = db.Where("x.ctime<=?", endtime)
  4089. }
  4090. likeKey := "%" + keyword + "%"
  4091. if len(likeKey) > 0 {
  4092. db = db.Where("t.good_name like ?", likeKey)
  4093. }
  4094. if orgid > 0 {
  4095. db = db.Where("x.org_id = ?", orgid)
  4096. }
  4097. if good_type > 0 {
  4098. db = db.Where("t.good_type_id = ?", good_type)
  4099. }
  4100. 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
  4101. return info, total, err
  4102. }
  4103. 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) {
  4104. offset := (page - 1) * limit
  4105. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  4106. likeKey := "%" + keyword + "%"
  4107. if len(likeKey) > 0 {
  4108. db = db.Where("x.good_name like ?", likeKey)
  4109. }
  4110. if orgid > 0 {
  4111. db = db.Where("x.org_id = ?", orgid)
  4112. }
  4113. if good_type > 0 {
  4114. db = db.Where("x.good_type_id = ?", good_type)
  4115. }
  4116. if good_id > 0 {
  4117. db = db.Where("x.id = ?", good_id)
  4118. }
  4119. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4120. if startime > 0 {
  4121. db = db.Where("ctime>=?", startime)
  4122. }
  4123. if endtime > 0 {
  4124. db = db.Where("ctime<=?", endtime)
  4125. }
  4126. if good_id > 0 {
  4127. db = db.Where("good_id = ?", good_id)
  4128. }
  4129. if storehouse_id > 0 {
  4130. db = db.Where("storehouse_id = ?", storehouse_id)
  4131. }
  4132. return db.Where("status = 1 and is_check = 1")
  4133. }).Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB {
  4134. if startime > 0 {
  4135. db = db.Where("ctime>=?", startime)
  4136. }
  4137. if endtime > 0 {
  4138. db = db.Where("ctime<=?", endtime)
  4139. }
  4140. if good_id > 0 {
  4141. db = db.Where("good_id = ?", good_id)
  4142. }
  4143. if storehouse_id > 0 {
  4144. db = db.Where("storehouse_id = ?", storehouse_id)
  4145. }
  4146. return db.Where("status = 1").Group("good_id,storehouse_id")
  4147. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4148. if startime > 0 {
  4149. db = db.Where("ctime>=?", startime)
  4150. }
  4151. if endtime > 0 {
  4152. db = db.Where("ctime<=?", endtime)
  4153. }
  4154. return db.Where("status = 1")
  4155. }).Find(&info).Error
  4156. return info, total, err
  4157. }
  4158. 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) {
  4159. offset := (page - 1) * limit
  4160. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1 and x.is_check = 1")
  4161. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4162. tables := XTReadDB().Table("xt_stock_flow as f").Where("f.status = 1 and f.consumable_type = 1 and f.user_org_id = ?", orgid)
  4163. fmt.Println(table, tables)
  4164. if startime > 0 {
  4165. db = db.Where("x.ctime>=?", startime)
  4166. }
  4167. if endtime > 0 {
  4168. db = db.Where("x.ctime<=?", endtime)
  4169. }
  4170. if startfirsttime > 0 {
  4171. db = db.Where("x.expiry_date >=?", startfirsttime)
  4172. }
  4173. if endfirsttime > 0 {
  4174. db = db.Where("x.expiry_date <=?", endfirsttime)
  4175. }
  4176. if orgid > 0 {
  4177. db = db.Where("x.org_id = ?", orgid)
  4178. }
  4179. if good_id > 0 {
  4180. db = db.Where("x.good_id = ?", good_id)
  4181. }
  4182. 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
  4183. return info, total, err
  4184. }
  4185. 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) {
  4186. offset := (page - 1) * limit
  4187. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  4188. if startime > 0 {
  4189. db = db.Where("x.ctime>=?", startime)
  4190. }
  4191. if endtime > 0 {
  4192. db = db.Where("x.ctime<=?", endtime)
  4193. }
  4194. if orgid > 0 {
  4195. db = db.Where("x.org_id = ?", orgid)
  4196. }
  4197. if good_id > 0 {
  4198. db = db.Where("x.good_id = ?", good_id)
  4199. }
  4200. if is_sys == 2 {
  4201. db = db.Where("x.is_sys = 1")
  4202. }
  4203. if is_sys == 3 {
  4204. db = db.Where("x.is_sys = 0")
  4205. }
  4206. 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
  4207. return info, total, err
  4208. }
  4209. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4210. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4211. if startime > 0 {
  4212. db = db.Where("x.ctime >=?", startime)
  4213. }
  4214. if endtime > 0 {
  4215. db = db.Where("x.ctime<=?", endtime)
  4216. }
  4217. if orgid > 0 {
  4218. db = db.Where("x.org_id = ?", orgid)
  4219. }
  4220. 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
  4221. return info, err
  4222. }
  4223. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  4224. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4225. if startime > 0 {
  4226. db = db.Where("x.ctime >=?", startime)
  4227. }
  4228. if endtime > 0 {
  4229. db = db.Where("x.ctime<=?", endtime)
  4230. }
  4231. if orgid > 0 {
  4232. db = db.Where("x.org_id = ?", orgid)
  4233. }
  4234. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  4235. return info, err
  4236. }
  4237. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  4238. if endtime > 0 {
  4239. 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
  4240. }
  4241. if endtime == 0 {
  4242. 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
  4243. }
  4244. return autoMatic, err
  4245. }
  4246. func GetGoodWarehouseOutById(id int64, orgid int64) (models.WarehouseOut, error) {
  4247. out := models.WarehouseOut{}
  4248. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", id, orgid).Find(&out).Error
  4249. return out, err
  4250. }
  4251. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4252. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4253. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4254. fmt.Println(table)
  4255. if id > 0 {
  4256. db = db.Where("x.warehouse_out_id = ?", id)
  4257. }
  4258. if orgid > 0 {
  4259. db = db.Where("x.org_id = ?", orgid)
  4260. }
  4261. err = db.Select("x.id,x.warehouse_out_id,x.warehouse_info_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,x.patient_id,t.manufacturer,x.is_sys,x.sys_record_time,x.number,x.remark,x.license_number,x.supply_cancel_out_id,x.supply_warehouse_id,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.register_number,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.good_id,x.warehouse_info_id").Order("x.ctime desc").Scan(&out).Error
  4262. return out, err
  4263. }
  4264. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  4265. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  4266. if len(id) > 0 {
  4267. db = db.Where("x.id in(?)", id)
  4268. }
  4269. if orgid > 0 {
  4270. db = db.Where("x.org_id= ?", orgid)
  4271. }
  4272. 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
  4273. return out, err
  4274. }
  4275. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  4276. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  4277. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4278. tableOne := XTReadDB().Table("xt_warehouse_info as n").Where("n.status = 1")
  4279. fmt.Println(table, tableOne)
  4280. if len(id) > 0 {
  4281. db = db.Where("x.warehouse_out_id in (?)", id)
  4282. }
  4283. if orgid > 0 {
  4284. db = db.Where("x.org_id = ?", orgid)
  4285. }
  4286. err = db.Select("x.id,x.warehouse_out_id,x.good_id,sum(x.count) as count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.warehouse_out_order_number,x.type,x.dealer,x.manufacturer,t.social_security_directory_code,x.is_sys,x.sys_record_time,n.number,x.remark,x.license_number,x.storehouse_id,x.admin_user_id,x.buy_price,x.stock_count,x.warehouse_info_id,x.remark,x.patient_id,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit,t.packing_price,x.register_number,x.class_type").Joins("left join xt_warehouse_info as n on n.id=x.warehouse_info_id").Joins("left join xt_good_information as t on t.id=x.good_id").Group("x.warehouse_out_id,x.good_id").Order("x.ctime desc").Scan(&out).Error
  4287. return out, err
  4288. }
  4289. func GetOrderDetailStockFlowByStorehouseById(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4290. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4291. if len(id) > 0 {
  4292. db = db.Where("warehouse_out_id in (?)", id)
  4293. }
  4294. if orgid > 0 {
  4295. db = db.Where("user_org_id = ?", orgid)
  4296. }
  4297. err = db.Group("warehousing_id").Find(&list).Error
  4298. return list, err
  4299. }
  4300. func GetOrderDetailStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4301. db := XTReadDB().Model(&list).Where("status = 1 and (consumable_type = 2 or consumable_type = 3 or consumable_type = 15 or consumable_type = 12)")
  4302. if len(id) > 0 {
  4303. db = db.Where("warehouse_out_id in (?)", id)
  4304. }
  4305. if orgid > 0 {
  4306. db = db.Where("user_org_id = ?", orgid)
  4307. }
  4308. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4309. return list, err
  4310. }
  4311. func GetOrderStockFlow(id []string, orgid int64) (list []*models.VmStockFlow, err error) {
  4312. db := XTReadDB().Model(&list).Where("status = 1 and consumable_type = 7 and patient_id > 0 and count>0")
  4313. if len(id) > 0 {
  4314. db = db.Where("warehouse_out_id in (?)", id)
  4315. }
  4316. if orgid > 0 {
  4317. db = db.Where("user_org_id = ?", orgid)
  4318. }
  4319. err = db.Preload("WarehousingInfo", "org_id = ? and status = 1", orgid).Find(&list).Error
  4320. return list, err
  4321. }
  4322. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  4323. db := XTReadDB().Model(&info)
  4324. db = db.Preload("GoodInfo", "status = 1")
  4325. 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
  4326. return info, err
  4327. }
  4328. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  4329. db := readDb.Model(&models.AutomaticReduceDetail{})
  4330. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  4331. db = db.Preload("GoodsType", "status = 1")
  4332. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  4333. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  4334. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  4335. db = db.Count(&total)
  4336. err = db.Find(&user).Error
  4337. return
  4338. }
  4339. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  4340. info := models.GoodInfo{}
  4341. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4342. return info, err
  4343. }
  4344. func GetNewGoodInformationByGoodId(good_id int64, tx *gorm.DB) (models.GoodInfo, error) {
  4345. info := models.GoodInfo{}
  4346. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4347. if err != gorm.ErrRecordNotFound {
  4348. if err != nil {
  4349. tx.Rollback()
  4350. return info, err
  4351. }
  4352. }
  4353. return info, err
  4354. }
  4355. func GetGoodInformationByGoodIdThirty(good_id int64) (models.GoodInformationThirty, error) {
  4356. info := models.GoodInformationThirty{}
  4357. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  4358. return info, err
  4359. }
  4360. func GetNewGoodInformationByGoodIdThirty(good_id int64, tx *gorm.DB) (models.GoodInformationThirty, error) {
  4361. info := models.GoodInformationThirty{}
  4362. err := tx.Where("id = ? and status = 1", good_id).Find(&info).Error
  4363. if err != gorm.ErrRecordNotFound {
  4364. if err != nil {
  4365. tx.Rollback()
  4366. return info, err
  4367. }
  4368. }
  4369. return info, err
  4370. }
  4371. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  4372. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  4373. return err
  4374. }
  4375. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  4376. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4377. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4378. fmt.Println(table)
  4379. if orgid > 0 {
  4380. db = db.Where("x.org_id = ?", orgid)
  4381. }
  4382. if len(orderid) > 0 {
  4383. db = db.Where("x.warehousing_id in(?)", orderid)
  4384. }
  4385. if startime > 0 {
  4386. db = db.Where("x.ctime>=?", startime)
  4387. }
  4388. if endtime > 0 {
  4389. db = db.Where("x.ctime<=?", endtime)
  4390. }
  4391. 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
  4392. return info, err
  4393. }
  4394. func GetExprotStockListTwenty(orgid int64, orderid []string, startime int64, endtime int64, storehouse_id int64) (info []*models.StWarehousingInfo, err error) {
  4395. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4396. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4397. fmt.Println(table)
  4398. if orgid > 0 {
  4399. db = db.Where("x.org_id = ?", orgid)
  4400. }
  4401. if len(orderid) > 0 {
  4402. db = db.Where("x.warehousing_id in(?)", orderid)
  4403. }
  4404. if startime > 0 {
  4405. db = db.Where("x.ctime>=?", startime)
  4406. }
  4407. if endtime > 0 {
  4408. db = db.Where("x.ctime<=?", endtime)
  4409. }
  4410. if storehouse_id > 0 {
  4411. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4412. }
  4413. err = db.Select("x.id,x.good_id,x.good_type_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.price,x.dealer,x.manufacturer,x.remark,x.license_number,t.good_name,t.packing_unit,t.specification_name,p.warehousing_time").Joins("left join xt_warehouse as p on p.id =x.warehousing_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&info).Error
  4414. return info, err
  4415. }
  4416. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4417. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4418. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  4419. fmt.Println(table)
  4420. if startime > 0 {
  4421. db = db.Where("x.ctime>=?", startime)
  4422. }
  4423. if endtime > 0 {
  4424. db = db.Where("x.ctime<=?", endtime)
  4425. }
  4426. if orgid > 0 {
  4427. db = db.Where("x.org_id = ?", orgid)
  4428. }
  4429. 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
  4430. return info, err
  4431. }
  4432. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  4433. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  4434. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4435. tableOne := XTReadDB().Table("xt_warehouse_out as p").Where("p.status=1")
  4436. fmt.Println(table, tableOne)
  4437. if orgid > 0 {
  4438. db = db.Where("x.org_id = ?", orgid)
  4439. }
  4440. if orgid > 0 {
  4441. db = db.Where("t.org_id = ?", orgid)
  4442. }
  4443. if orgid > 0 {
  4444. db = db.Where("p.org_id = ?", orgid)
  4445. }
  4446. if len(ids) > 0 {
  4447. db = db.Where("x.warehouse_out_id in (?)", ids)
  4448. }
  4449. if startime > 0 {
  4450. db = db.Where("x.ctime>=?", startime)
  4451. }
  4452. if endtime > 0 {
  4453. db = db.Where("x.ctime<=?", endtime)
  4454. }
  4455. err = db.Select("x.id,x.warehouse_out_id,x.good_id,x.good_type_id,x.warehousing_out_target,x.count,x.price,x.total_price,x.product_date,x.expiry_date,x.ctime,x.org_id,x.remark,x.number,x.license_number,x.warehouse_out_order_number,t.good_name,t.specification_name,t.min_number,t.packing_price,t.min_unit,t.packing_unit,x.manufacturer,x.dealer,p.warehouse_out_time").Joins("left join xt_warehouse_out as p on p.id =x.warehouse_out_id").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&out).Error
  4456. return out, err
  4457. }
  4458. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  4459. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  4460. if len(ids) > 0 {
  4461. db = db.Where("x.id in(?)", ids)
  4462. }
  4463. if orgid > 0 {
  4464. db = db.Where("x.org_id = ?", orgid)
  4465. }
  4466. 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
  4467. return info, err
  4468. }
  4469. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  4470. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  4471. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  4472. fmt.Println(table)
  4473. if len(ids) > 0 {
  4474. db = db.Where("x.warehouse_out_id in(?)", ids)
  4475. }
  4476. if orgid > 0 {
  4477. db = db.Where("x.org_id = ?", orgid)
  4478. }
  4479. 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
  4480. return info, err
  4481. }
  4482. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  4483. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  4484. likeKey := "%" + keyword + "%"
  4485. if len(keyword) > 0 {
  4486. db = db.Where("x.drug_name like ?", likeKey)
  4487. }
  4488. if orgid > 0 {
  4489. db = db.Where("x.org_id = ?", orgid)
  4490. }
  4491. err = db.Find(&basedrug).Error
  4492. return basedrug, err
  4493. }
  4494. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4495. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4496. if len(ids) > 0 {
  4497. db = db.Where("x.drug_id in(?)", ids)
  4498. }
  4499. if orgid > 0 {
  4500. db = db.Where("x.org_id = ?", orgid)
  4501. }
  4502. if startime > 0 {
  4503. db = db.Where("x.ctime>=?", startime)
  4504. }
  4505. if endtime > 0 {
  4506. db = db.Where("x.ctime<=?", endtime)
  4507. }
  4508. if storehouse_id > 0 {
  4509. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4510. }
  4511. err = db.Find(&info).Error
  4512. return info, err
  4513. }
  4514. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64, storehouse_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  4515. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4516. if len(ids) > 0 {
  4517. db = db.Where("x.drug_id in(?)", ids)
  4518. }
  4519. if orgid > 0 {
  4520. db = db.Where("x.org_id = ?", orgid)
  4521. }
  4522. if startime > 0 {
  4523. db = db.Where("x.ctime>=?", startime)
  4524. }
  4525. if endtime > 0 {
  4526. db = db.Where("x.ctime<=?", endtime)
  4527. }
  4528. if storehouse_id > 0 {
  4529. db = db.Where("storehouse_id = ?", storehouse_id)
  4530. }
  4531. err = db.Find(&info).Error
  4532. return info, err
  4533. }
  4534. func GetDrugTotalCount(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4535. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4536. if drugid > 0 {
  4537. db = db.Where("x.drug_id = ?", drugid)
  4538. }
  4539. if orgid > 0 {
  4540. db = db.Where("x.org_id = ?", orgid)
  4541. }
  4542. if storehouse_id > 0 {
  4543. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4544. }
  4545. 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
  4546. return info, err
  4547. }
  4548. func GetDrugTotalCountTwoTwety(drugid int64, orgid int64, storehouse_id int64, tx *gorm.DB) (info models.VmDrugWarehouseInfo, err error) {
  4549. db := tx.Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  4550. if drugid > 0 {
  4551. db = db.Where("x.drug_id = ?", drugid)
  4552. }
  4553. if orgid > 0 {
  4554. db = db.Where("x.org_id = ?", orgid)
  4555. }
  4556. if storehouse_id > 0 {
  4557. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4558. }
  4559. 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
  4560. if err != gorm.ErrRecordNotFound {
  4561. if err != nil {
  4562. tx.Rollback()
  4563. return info, err
  4564. }
  4565. }
  4566. return info, err
  4567. }
  4568. func GetDrugTotalCountTwenty(drugid int64, orgid int64, storehouse_id int64) (info models.VmDrugWarehouseInfo, err error) {
  4569. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1 and x.is_check = 1 ")
  4570. if drugid > 0 {
  4571. db = db.Where("x.drug_id = ?", drugid)
  4572. }
  4573. if orgid > 0 {
  4574. db = db.Where("x.org_id = ?", orgid)
  4575. }
  4576. if storehouse_id > 0 {
  4577. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4578. }
  4579. 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
  4580. return info, err
  4581. }
  4582. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  4583. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4584. if id > 0 {
  4585. db = db.Where("x.id = ?", id)
  4586. }
  4587. if orgid > 0 {
  4588. db = db.Where("x.org_id = ?", orgid)
  4589. }
  4590. 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
  4591. return info, err
  4592. }
  4593. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  4594. info := models.VmDrugWarehouseInfo{}
  4595. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4596. if id == 0 {
  4597. db = db.Where("id = ?", id)
  4598. }
  4599. if drugid > 0 {
  4600. db = db.Where("x.drug_id = ?", drugid)
  4601. }
  4602. if orgid > 0 {
  4603. db = db.Where("x.org_id = ?", orgid)
  4604. }
  4605. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  4606. return info, err
  4607. }
  4608. func GetDrugTotalCountTwo(drugid int64, storehouse_id int64) (list []*models.VmDrugWarehouseInfo, err error) {
  4609. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  4610. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  4611. fmt.Println(table)
  4612. if drugid > 0 {
  4613. db = db.Where("x.drug_id = ?", drugid)
  4614. }
  4615. if storehouse_id > 0 {
  4616. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4617. }
  4618. 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
  4619. return list, err
  4620. }
  4621. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  4622. lib := models.BaseDrugLib{}
  4623. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  4624. return lib, err
  4625. }
  4626. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4627. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4628. if goodid > 0 {
  4629. db = db.Where("x.good_id = ?", goodid)
  4630. }
  4631. if orgid > 0 {
  4632. db = db.Where("x.org_id = ?", orgid)
  4633. }
  4634. 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
  4635. return info, err
  4636. }
  4637. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  4638. 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
  4639. return err
  4640. }
  4641. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  4642. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  4643. return info, err
  4644. }
  4645. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  4646. //遍历循环入库数据
  4647. for i, item := range list {
  4648. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  4649. if (item.WarehousingCount - item.StockCount) >= count {
  4650. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  4651. info := models.WarehousingInfo{
  4652. GoodId: item.GoodId,
  4653. OrgId: item.OrgId,
  4654. WarehousingId: item.WarehousingId,
  4655. StockCount: item.StockCount + count,
  4656. }
  4657. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  4658. fmt.Println("2323233232323232323232323232323", err2)
  4659. return err
  4660. }
  4661. if (item.WarehousingCount - item.StockCount) < count {
  4662. //如果该批次小于退库数量,则先出库该相差数量
  4663. var countOne = item.WarehousingCount - item.StockCount
  4664. infoOne := models.WarehousingInfo{
  4665. GoodId: item.GoodId,
  4666. OrgId: item.OrgId,
  4667. WarehousingId: item.WarehousingId,
  4668. StockCount: countOne + item.StockCount,
  4669. }
  4670. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  4671. if err == nil {
  4672. //剩余的出库到第二个批次
  4673. var countTwo = count - countOne
  4674. infos := append(list, list[i+1])
  4675. var idArray []*models.WarehousingInfo
  4676. for _, it := range infos {
  4677. if it.WarehousingId != item.WarehousingId {
  4678. idArray = append(idArray, it)
  4679. }
  4680. }
  4681. //查询更新后的库存
  4682. UpdateCancelOut(idArray, countTwo)
  4683. return err
  4684. } else {
  4685. return err
  4686. }
  4687. }
  4688. }
  4689. return err
  4690. }
  4691. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  4692. if len(idstr) > 0 {
  4693. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  4694. } else {
  4695. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  4696. }
  4697. return info, err
  4698. }
  4699. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  4700. if len(idstr) > 0 {
  4701. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  4702. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  4703. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  4704. }).Find(&info).Error
  4705. }
  4706. return info, err
  4707. }
  4708. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  4709. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  4710. if startime > 0 {
  4711. db = db.Where("x.ctime >=?", startime)
  4712. }
  4713. if endtime > 0 {
  4714. db = db.Where("x.ctime <= ?", endtime)
  4715. }
  4716. if orgid > 0 {
  4717. db = db.Where("x.org_id = ?", orgid)
  4718. }
  4719. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  4720. return info, err
  4721. }
  4722. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  4723. info := models.WarehouseOutInfo{}
  4724. var err error
  4725. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  4726. if err == gorm.ErrRecordNotFound {
  4727. return nil, err
  4728. }
  4729. if err != nil {
  4730. return nil, err
  4731. }
  4732. return &info, nil
  4733. }
  4734. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  4735. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  4736. if goodid > 0 {
  4737. db = db.Where("x.good_id = ?", goodid)
  4738. }
  4739. if orgid > 0 {
  4740. db = db.Where("x.org_id = ?", orgid)
  4741. }
  4742. if id > 0 {
  4743. db = db.Where("x.warehouse_info_id = ?", id)
  4744. }
  4745. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4746. return info, err
  4747. }
  4748. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64, storehouse_id int64) (info models.VmStockFlow, err error) {
  4749. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  4750. if goodid > 0 {
  4751. db = db.Where("x.good_id = ?", goodid)
  4752. }
  4753. if orgid > 0 {
  4754. db = db.Where("x.user_org_id = ?", orgid)
  4755. }
  4756. if id > 0 {
  4757. db = db.Where("x.warehousing_id = ?", id)
  4758. }
  4759. if storehouse_id > 0 {
  4760. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4761. }
  4762. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  4763. return info, err
  4764. }
  4765. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  4766. stock := models.CancelStock{}
  4767. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4768. return stock, err
  4769. }
  4770. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  4771. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  4772. return info, err
  4773. }
  4774. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  4775. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4776. return info, err
  4777. }
  4778. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  4779. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  4780. return stock, err
  4781. }
  4782. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4783. 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)")
  4784. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4785. fmt.Println(table)
  4786. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4787. fmt.Println(tableone)
  4788. if id > 0 {
  4789. db = db.Where("x.warehouse_out_id = ?", id)
  4790. }
  4791. if goodid > 0 {
  4792. db = db.Where("x.good_id = ?", goodid)
  4793. }
  4794. 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
  4795. return stock, err
  4796. }
  4797. func GetStockFlowCancelInfo(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  4798. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and x.count > 0 and x.consumable_type = 7")
  4799. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  4800. fmt.Println(table)
  4801. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  4802. fmt.Println(tableone)
  4803. if id > 0 {
  4804. db = db.Where("x.warehouse_out_id = ?", id)
  4805. }
  4806. if goodid > 0 {
  4807. db = db.Where("x.good_id = ?", goodid)
  4808. }
  4809. 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
  4810. return stock, err
  4811. }
  4812. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  4813. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  4814. return info, err
  4815. }
  4816. func ExportDrugList(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseInfo, err error) {
  4817. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1 AND x.org_id=?", org_id)
  4818. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4819. tableOne := XTReadDB().Table("xt_drug_warehouse as p").Where("p.status=1 and p.org_id=?", org_id)
  4820. fmt.Println("table", table)
  4821. fmt.Println("tableOne", tableOne)
  4822. if len(warehouse_out_id) > 0 {
  4823. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  4824. }
  4825. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.price,x.dealer,x.manufacturer,x.warehousing_order,x.batch_number,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type,p.warehousing_time").Joins("left join xt_drug_warehouse as p on p.id =x.warehousing_id").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  4826. return out, err
  4827. }
  4828. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4829. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  4830. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  4831. fmt.Println("table", table)
  4832. if len(warehouse_out_id) > 0 {
  4833. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4834. }
  4835. 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
  4836. return out, err
  4837. }
  4838. func GetExportOutOrderDrugListOne(warehouse_out_id []string, org_id int64) (out []*models.StDrugWarehouseOutInfo, err error) {
  4839. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1 and x.user_org_id =?", org_id)
  4840. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1 and b.org_id = ?", org_id)
  4841. fmt.Println("table", table)
  4842. if len(warehouse_out_id) > 0 {
  4843. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  4844. }
  4845. 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
  4846. return out, err
  4847. }
  4848. func GetDrugOutListFlow(warehouse_out_id []string, orgid int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  4849. db := XTReadDB().Model(&info).Where("status = 1")
  4850. if len(warehouse_out_id) > 0 {
  4851. db = db.Where("warehouse_out_id in(?)", warehouse_out_id)
  4852. }
  4853. if orgid > 0 {
  4854. db = db.Where("org_id = ?", orgid)
  4855. }
  4856. err = db.Find(&info).Error
  4857. return info, err
  4858. }
  4859. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  4860. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  4861. return out, err
  4862. }
  4863. func GetDrugWarehouseOutTenty(warehouse_out_id []string, orgid int64) (out []*models.DrugWarehouseOutTwenty, err error) {
  4864. err = XTReadDB().Where("id in(?)", warehouse_out_id).Preload("DrugWarehouseOutInfoTenty", func(db *gorm.DB) *gorm.DB {
  4865. 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)
  4866. }).Find(&out).Error
  4867. return out, err
  4868. }
  4869. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  4870. info := models.WarehouseOutInfo{}
  4871. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  4872. return info, err
  4873. }
  4874. func GetTotalCountByGoodId(goodid int64, storehouse_id int64) (models.WarehousingInfo, error) {
  4875. info := models.WarehousingInfo{}
  4876. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4877. if goodid > 0 {
  4878. db = db.Where("x.good_id = ?", goodid)
  4879. }
  4880. if storehouse_id > 0 {
  4881. db = db.Where("x.storehouse_id = ?", storehouse_id)
  4882. }
  4883. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  4884. return info, err
  4885. }
  4886. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  4887. info := models.WarehousingInfo{}
  4888. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  4889. return info, err
  4890. }
  4891. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4892. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4893. return err
  4894. }
  4895. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  4896. if storehouse_id == 0 {
  4897. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  4898. }
  4899. if storehouse_id > 0 {
  4900. 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
  4901. }
  4902. return info, err
  4903. }
  4904. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  4905. if len(warehousingInfo) > 0 {
  4906. utx := writeDb.Begin()
  4907. if len(warehousingInfo) > 0 {
  4908. 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 "
  4909. insertParams := make([]string, 0)
  4910. insertData := make([]interface{}, 0)
  4911. for _, info := range warehousingInfo {
  4912. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  4913. insertData = append(insertData, info.WarehousingId)
  4914. insertData = append(insertData, info.GoodId)
  4915. insertData = append(insertData, info.Number)
  4916. insertData = append(insertData, info.LicenseNumber)
  4917. insertData = append(insertData, info.Count)
  4918. insertData = append(insertData, info.UserOrgId)
  4919. insertData = append(insertData, info.PatientId)
  4920. insertData = append(insertData, info.SystemTime)
  4921. insertData = append(insertData, info.ConsumableType)
  4922. insertData = append(insertData, info.IsSys)
  4923. insertData = append(insertData, info.WarehousingOrder)
  4924. insertData = append(insertData, info.WarehouseOutId)
  4925. insertData = append(insertData, info.WarehouseOutOrderNumber)
  4926. insertData = append(insertData, info.IsEdit)
  4927. insertData = append(insertData, info.CancelStockId)
  4928. insertData = append(insertData, info.CancelOrderNumber)
  4929. insertData = append(insertData, info.Manufacturer)
  4930. insertData = append(insertData, info.Dealer)
  4931. insertData = append(insertData, info.Creator)
  4932. insertData = append(insertData, info.UpdateCreator)
  4933. insertData = append(insertData, info.Status)
  4934. insertData = append(insertData, info.Ctime)
  4935. insertData = append(insertData, info.Mtime)
  4936. insertData = append(insertData, info.Price)
  4937. insertData = append(insertData, info.WarehousingDetailId)
  4938. insertData = append(insertData, info.WarehouseOutDetailId)
  4939. insertData = append(insertData, info.CancelOutDetailId)
  4940. insertData = append(insertData, info.ExpireDate)
  4941. insertData = append(insertData, info.ProductDate)
  4942. }
  4943. thisSQL += strings.Join(insertParams, ", ")
  4944. err = utx.Exec(thisSQL, insertData...).Error
  4945. if err != nil {
  4946. utx.Rollback()
  4947. return
  4948. }
  4949. }
  4950. utx.Commit()
  4951. }
  4952. return
  4953. }
  4954. func CreateStockFlowOne(flow models.VmStockFlow) error {
  4955. utx := XTWriteDB().Begin()
  4956. err := utx.Create(&flow).Error
  4957. if err != nil {
  4958. utx.Rollback()
  4959. return err
  4960. }
  4961. utx.Commit()
  4962. return err
  4963. }
  4964. func CreateNewStockFlowOne(flow models.VmStockFlow, tx *gorm.DB) error {
  4965. err := tx.Create(&flow).Error
  4966. if err != nil {
  4967. tx.Rollback()
  4968. return err
  4969. }
  4970. return err
  4971. }
  4972. func CreateOutInfoLog(log models.XtGoodWarehouseOutInfoLog, tx *gorm.DB) error {
  4973. err := tx.Create(&log).Error
  4974. if err != nil {
  4975. tx.Rollback()
  4976. return err
  4977. }
  4978. return err
  4979. }
  4980. func CreateGoodWarehouseInfoLog(log models.XtGoodWarehouseInfoLog, tx *gorm.DB) error {
  4981. err := tx.Create(&log).Error
  4982. if err != nil {
  4983. tx.Rollback()
  4984. return err
  4985. }
  4986. return err
  4987. }
  4988. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  4989. err := XTWriteDB().Create(&flow).Error
  4990. return err
  4991. }
  4992. func UpdatedStockFlow(flow models.VmStockFlow) error {
  4993. err := XTWriteDB().Save(&flow).Error
  4994. return err
  4995. }
  4996. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  4997. 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
  4998. return err
  4999. }
  5000. func UpdatedStockFlowFive(flow models.VmStockFlow, warehouse_out_id int64, patient_id int64, record_time int64, good_id int64) error {
  5001. 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
  5002. return err
  5003. }
  5004. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  5005. stock := models.VmStockFlow{}
  5006. var err error
  5007. 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
  5008. if err == gorm.ErrRecordNotFound {
  5009. return nil, err
  5010. }
  5011. if err != nil {
  5012. return nil, err
  5013. }
  5014. return &stock, nil
  5015. }
  5016. func GetStockFlowIsBatchNumber(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  5017. 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
  5018. return flow, nil
  5019. }
  5020. func GetStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5021. 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
  5022. return flow, nil
  5023. }
  5024. func GetNewStockFlowIsBatchNumberSix(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5025. err = tx.Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type = 3 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  5026. if err != gorm.ErrRecordNotFound {
  5027. if err != nil {
  5028. tx.Rollback()
  5029. return flow, nil
  5030. }
  5031. }
  5032. return flow, nil
  5033. }
  5034. func GetStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.WarehouseOutInfo, err error) {
  5035. 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
  5036. return flow, nil
  5037. }
  5038. func GetNewStockFlowIsBatchNumberSixty(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.WarehouseOutInfo, err error) {
  5039. err = tx.Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  5040. if err != gorm.ErrRecordNotFound {
  5041. if err != nil {
  5042. tx.Rollback()
  5043. return flow, nil
  5044. }
  5045. }
  5046. return flow, nil
  5047. }
  5048. func GetStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.AutomaticReduceDetail, err error) {
  5049. 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
  5050. return flow, nil
  5051. }
  5052. func GetNewStockFlowIsBatchNumberSeventy(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.AutomaticReduceDetail, err error) {
  5053. err = tx.Where("patient_id = ? and record_time = ? and good_id = ? and status = 1 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  5054. if err != gorm.ErrRecordNotFound {
  5055. if err != nil {
  5056. tx.Rollback()
  5057. return flow, err
  5058. }
  5059. }
  5060. return flow, err
  5061. }
  5062. func GetStockFlowIsBatchNumberTwo(patient_id int64, record_time int64, good_id int64) (flow []*models.WarehouseOutInfo, err error) {
  5063. err = XTReadDB().Where("patient_id = ? and sys_record_time = ? and good_id = ? and status = 1 ", patient_id, record_time, good_id).Find(&flow).Error
  5064. return flow, nil
  5065. }
  5066. func GetStockFlowIsBatchNumberThree(patient_id int64, record_time int64, good_id int64) (flow []*models.VmStockFlow, err error) {
  5067. 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
  5068. return flow, nil
  5069. }
  5070. func GetStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5071. 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
  5072. return flow, nil
  5073. }
  5074. func GetNewStockFlowIsBatchNumberFour(patient_id int64, record_time int64, good_id int64, project_id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  5075. err = tx.Where("patient_id = ? and system_time = ? and good_id = ? and status = 1 and consumable_type =7 and project_id = ?", patient_id, record_time, good_id, project_id).Find(&flow).Error
  5076. if err != gorm.ErrRecordNotFound {
  5077. if err != nil {
  5078. tx.Rollback()
  5079. return flow, nil
  5080. }
  5081. }
  5082. return flow, nil
  5083. }
  5084. func GetStockFlowIsBatchNumberFourty(patient_id int64, record_time int64, good_id int64, project_id int64) (flow []*models.VmStockFlow, err error) {
  5085. 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
  5086. return flow, nil
  5087. }
  5088. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  5089. info := models.WarehouseOutInfo{}
  5090. var err error
  5091. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5092. if err == gorm.ErrRecordNotFound {
  5093. return nil, err
  5094. }
  5095. if err != nil {
  5096. return nil, err
  5097. }
  5098. return &info, nil
  5099. }
  5100. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  5101. info := models.WarehouseOutInfo{}
  5102. var err error
  5103. err = XTReadDB().Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and is_sys=1", good_id, record_time, patient_id).Find(&info).Error
  5104. if err == gorm.ErrRecordNotFound {
  5105. return nil, err
  5106. }
  5107. if err != nil {
  5108. return nil, err
  5109. }
  5110. return &info, nil
  5111. }
  5112. func GetWarehouseOutInfoIsExistTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  5113. info := models.WarehouseOutInfo{}
  5114. err := XTReadDB().Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and is_sys=1", good_id, record_time, patient_id).Find(&info).Error
  5115. return info, err
  5116. }
  5117. 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) {
  5118. offset := (page - 1) * limit
  5119. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  5120. if consumable_type > 0 {
  5121. //入库
  5122. if consumable_type == 1 {
  5123. db = db.Where("consumable_type = ?", consumable_type)
  5124. }
  5125. //2 手动出库
  5126. if consumable_type == 2 {
  5127. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  5128. }
  5129. //3.自动出库
  5130. if consumable_type == 3 {
  5131. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  5132. }
  5133. //4.手动退库
  5134. if consumable_type == 4 {
  5135. db = db.Where(" consumable_type = ?", consumable_type)
  5136. }
  5137. //自动退库
  5138. if consumable_type == 7 {
  5139. db = db.Where(" consumable_type = ?", consumable_type)
  5140. }
  5141. if consumable_type == 10 {
  5142. db = db.Where(" consumable_type = ?", consumable_type)
  5143. }
  5144. if consumable_type == 11 {
  5145. db = db.Where(" consumable_type = ?", consumable_type)
  5146. }
  5147. if consumable_type == 12 {
  5148. db = db.Where(" consumable_type = ?", consumable_type)
  5149. }
  5150. if consumable_type == 13 {
  5151. db = db.Where(" consumable_type = ?", consumable_type)
  5152. }
  5153. if consumable_type == 15 {
  5154. db = db.Where(" consumable_type = ?", consumable_type)
  5155. }
  5156. }
  5157. if orgId > 0 {
  5158. db = db.Where("user_org_id = ?", orgId)
  5159. }
  5160. if startTime > 0 {
  5161. db = db.Where("ctime >= ?", startTime)
  5162. }
  5163. if endTime > 0 {
  5164. db = db.Where("ctime<=?", endTime)
  5165. }
  5166. if good_id > 0 {
  5167. db = db.Where("good_id = ?", good_id)
  5168. }
  5169. err = db.Count(&total).Offset(offset).Limit(limit).Order("id desc").Find(&flow).Error
  5170. return flow, total, err
  5171. }
  5172. func GetHidsDoctorAdvice(good_id int64, user_org_id int64, patient_id int64, record_date int64) (models.HisPrescriptionProject, error) {
  5173. project := models.HisPrescriptionProject{}
  5174. err = XTReadDB().Where("project_id = ? and type =3 and user_org_id = ? and record_date =? and patient_id = ?", good_id, user_org_id, record_date, patient_id).Find(&project).Error
  5175. return project, err
  5176. }
  5177. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  5178. detail := models.AutomaticReduceDetail{}
  5179. 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
  5180. return detail, err
  5181. }
  5182. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  5183. detail := models.WarehouseOutInfo{}
  5184. 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
  5185. return detail, err
  5186. }
  5187. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64) error {
  5188. outInfo := models.WarehouseOutInfo{}
  5189. 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
  5190. return err
  5191. }
  5192. func UpdatedNewWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) error {
  5193. outInfo := models.WarehouseOutInfo{}
  5194. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1", good_id, patient_id, record_time).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "over_count": info.OverCount}).Error
  5195. if err != nil {
  5196. tx.Rollback()
  5197. return err
  5198. }
  5199. return err
  5200. }
  5201. func UpdatedWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) error {
  5202. outInfo := models.WarehouseOutInfo{}
  5203. 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
  5204. return err
  5205. }
  5206. func UpdatedNewWarehouseOutInfoSix(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) error {
  5207. outInfo := models.WarehouseOutInfo{}
  5208. err := tx.Model(&outInfo).Where("good_id = ? and patient_id = ? and sys_record_time =? and status = 1 and project_id = ? and warehouse_info_id =? ", good_id, patient_id, record_time, project_id, warehouse_info_id).Updates(map[string]interface{}{"warehouse_out_id": info.WarehouseOutId, "WarehouseOutOrderNumber": info.WarehouseOutOrderNumber, "sys_record_time": info.SysRecordTime, "good_type_id": info.GoodTypeId, "patient_id": info.PatientId, "consumable_type": info.ConsumableType, "count": info.Count, "price": info.Price, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "over_count": info.OverCount}).Error
  5209. if err != nil {
  5210. tx.Rollback()
  5211. return err
  5212. }
  5213. return err
  5214. }
  5215. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  5216. info := models.WarehouseOutInfo{}
  5217. 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
  5218. return info, err
  5219. }
  5220. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  5221. info := models.WarehousingInfo{}
  5222. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5223. return info, err
  5224. }
  5225. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  5226. info := models.WarehousingInfo{}
  5227. db := XTReadDB().Model(&info)
  5228. if good_id > 0 {
  5229. db = db.Where("good_id = ? and status = 1", good_id)
  5230. }
  5231. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  5232. return info, err
  5233. }
  5234. 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) {
  5235. offset := (page - 1) * limit
  5236. db := XTReadDB().Model(drug_flow).Where("status = 1")
  5237. if drugid > 0 {
  5238. db = db.Where("drug_id = ?", drugid)
  5239. }
  5240. if orgid > 0 {
  5241. db = db.Where("user_org_id = ?", orgid)
  5242. }
  5243. if startTime > 0 {
  5244. db = db.Where("ctime >=? ", startTime)
  5245. }
  5246. if endTime > 0 {
  5247. db = db.Where("ctime<=?", endTime)
  5248. }
  5249. if stock_type > 0 {
  5250. //手动入库
  5251. if stock_type == 1 {
  5252. db = db.Where("consumable_type = 1")
  5253. }
  5254. //手动出库
  5255. if stock_type == 2 {
  5256. db = db.Where("consumable_type = 2 and is_sys = 0")
  5257. }
  5258. //自动出库
  5259. if stock_type == 3 {
  5260. db = db.Where("consumable_type = 3 and is_sys = 1")
  5261. }
  5262. if stock_type == 4 {
  5263. db = db.Where("consumable_type = 4")
  5264. }
  5265. if stock_type == 10 {
  5266. db = db.Where("consumable_type = 10")
  5267. }
  5268. if stock_type == 11 {
  5269. db = db.Where("consumable_type = 11")
  5270. }
  5271. if stock_type == 12 {
  5272. db = db.Where("consumable_type = 12")
  5273. }
  5274. if stock_type == 13 {
  5275. db = db.Where("consumable_type = 13")
  5276. }
  5277. if stock_type == 15 {
  5278. db = db.Where("consumable_type = 15")
  5279. }
  5280. }
  5281. 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
  5282. return drug_flow, total, err
  5283. }
  5284. func HisDoctorAdviceMap(patient_id int64, advice_date int64, drug_id int64, user_org_id int64) (models.HisDoctorAdviceInfo, error) {
  5285. advicelist := models.HisDoctorAdviceInfo{}
  5286. err = XTReadDB().Where("patient_id = ? and drug_id = ? and user_org_id = ? and advice_date = ? and status=1", patient_id, drug_id, user_org_id, advice_date).Find(&advicelist).Error
  5287. return advicelist, err
  5288. }
  5289. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  5290. outInfo := models.DrugWarehouseOutInfo{}
  5291. var err error
  5292. 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
  5293. if err == gorm.ErrRecordNotFound {
  5294. return nil, err
  5295. }
  5296. if err != nil {
  5297. return nil, err
  5298. }
  5299. return &outInfo, nil
  5300. }
  5301. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  5302. 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
  5303. return outInfo, err
  5304. }
  5305. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  5306. outInfo := models.DrugWarehouseOutInfo{}
  5307. 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
  5308. return err
  5309. }
  5310. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  5311. db := XTReadDB().Model(&cancle).Where("status = 1")
  5312. if startime > 0 {
  5313. db = db.Where("ctime >=?", startime)
  5314. }
  5315. if endtime > 0 {
  5316. db = db.Where("ctime <=?", endtime)
  5317. }
  5318. if len(ids) > 0 {
  5319. db = db.Where("cancel_stock_id in(?)", ids)
  5320. }
  5321. if orgid > 0 {
  5322. db = db.Where("org_id = ?", orgid)
  5323. }
  5324. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  5325. err = db.Find(&cancle).Error
  5326. return cancle, err
  5327. }
  5328. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  5329. db := XTReadDB().Model(&info)
  5330. if id > 0 {
  5331. db = db.Where("cancel_stock_id = ? and status = 1", id)
  5332. }
  5333. db = db.Preload("BaseDrugLib")
  5334. err = db.Find(&info).Error
  5335. return info, err
  5336. }
  5337. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  5338. info := models.XtDrugWarehouseInfo{}
  5339. db := XTReadDB().Model(&info).Where("status = 1")
  5340. if id > 0 {
  5341. db = db.Where("id = ?", id)
  5342. }
  5343. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5344. return info, err
  5345. }
  5346. func GetDrugWarehouseInfoOne(id int64) (models.XtDrugWarehouseInfo, error) {
  5347. info := models.XtDrugWarehouseInfo{}
  5348. err := XTReadDB().Where("status = 1 and id=?", id).Find(&info).Error
  5349. return info, err
  5350. }
  5351. //func GetDrugWarehouseInfoTX(id int64,tx *gorm.DB) (models.XtDrugWarehouseInfo, error) {
  5352. // info := models.XtDrugWarehouseInfo{}
  5353. // db := tx.Model(&info).Where("status = 1")
  5354. // if id > 0 {
  5355. // db = db.Where("id = ?", id)
  5356. // }
  5357. // err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  5358. // return info, err
  5359. //}
  5360. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  5361. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  5362. return info, err
  5363. }
  5364. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  5365. 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
  5366. return info, err
  5367. }
  5368. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  5369. info := models.HisDoctorAdviceInfo{}
  5370. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  5371. return info, err
  5372. }
  5373. func GetHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64) (info []*models.HisDoctorAdviceInfo, err error) {
  5374. err = XTReadDB().Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5375. return info, err
  5376. }
  5377. func GetNewHisAdviceListByDrugIdTwo(patient_id int64, advice_date int64, orgid int64, tx *gorm.DB) (info []*models.HisDoctorAdviceInfo, err error) {
  5378. err = tx.Where("patient_id = ? and advice_date = ? and status = 1 and user_org_id = ?", patient_id, advice_date, orgid).Find(&info).Error
  5379. if err != gorm.ErrRecordNotFound {
  5380. if err != nil {
  5381. tx.Rollback()
  5382. return info, err
  5383. }
  5384. }
  5385. return info, err
  5386. }
  5387. func GetLastHisAdvicesList(patient_id int64, advice_date int64, user_org_id int64, tx *gorm.DB) (models.HisDoctorAdviceInfo, error) {
  5388. adviceInfo := models.HisDoctorAdviceInfo{}
  5389. err := tx.Where("patient_id = ? and advice_date= ? and user_org_id = ? and status=1", patient_id, advice_date, user_org_id).Last(&adviceInfo).Error
  5390. if err != gorm.ErrRecordNotFound {
  5391. if err != nil {
  5392. tx.Rollback()
  5393. return adviceInfo, err
  5394. }
  5395. }
  5396. return adviceInfo, err
  5397. }
  5398. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5399. info := models.HisDoctorAdviceInfo{}
  5400. 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
  5401. return info, err
  5402. }
  5403. func GetHisAdviceListByDrugIdNight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  5404. info := models.HisDoctorAdviceInfo{}
  5405. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1 and id =?", drugid, patient_id, advice_date, advice_id).Find(&info).Error
  5406. return info, err
  5407. }
  5408. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  5409. detail := models.DrugAutomaticReduceDetail{}
  5410. 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
  5411. return detail, err
  5412. }
  5413. func GetWarehouseOutListExport(drug_id int64, patient_id int64, record_date int64, advice_id int64) (out []*models.XtDrugWarehouseOutInfo, err error) {
  5414. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time = ? and advice_id =? and status =1", drug_id, patient_id, record_date, advice_id).Find(&out).Error
  5415. return out, err
  5416. }
  5417. func GetLastDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64) (out []*models.DrugWarehouseOutInfo, err error) {
  5418. 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
  5419. return out, err
  5420. }
  5421. func GetLastNewDrugWarehouseOutByDrugIdTwenty(drugid int64, patient_id int64, advice_date int64, advice_id int64, tx *gorm.DB) (out []*models.DrugWarehouseOutInfo, err error) {
  5422. err = tx.Where("advice_id = ? and drug_id = ? and patient_id = ? and sys_record_time = ? and status = 1 and is_check = 1", advice_id, drugid, patient_id, advice_date).Find(&out).Error
  5423. if err != gorm.ErrRecordNotFound {
  5424. if err != nil {
  5425. tx.Rollback()
  5426. return out, err
  5427. }
  5428. }
  5429. return out, err
  5430. }
  5431. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  5432. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  5433. return err
  5434. }
  5435. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  5436. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  5437. return err
  5438. }
  5439. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  5440. 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
  5441. 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
  5442. 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
  5443. return err
  5444. }
  5445. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5446. 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
  5447. 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
  5448. return err
  5449. }
  5450. func DeleteNewDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  5451. 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
  5452. 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
  5453. return err
  5454. }
  5455. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  5456. info := models.XtDrugWarehouseInfo{}
  5457. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  5458. return info, err
  5459. }
  5460. func GetDrugWarehouseInfoByIdTwenty(id int64) (models.XtDrugWarehouseInfo, error) {
  5461. info := models.XtDrugWarehouseInfo{}
  5462. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5463. return info, err
  5464. }
  5465. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  5466. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5467. return err
  5468. }
  5469. //func UpdateDrugWarehouseInfoTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5470. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  5471. // return err
  5472. //}
  5473. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  5474. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  5475. //return err
  5476. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5477. return err
  5478. }
  5479. //func UpdateDrugWarehouseInfoOneTX(info *models.XtDrugWarehouseInfo, id int64,tx *gorm.DB) error {
  5480. // err = tx.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  5481. // return err
  5482. //}
  5483. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  5484. info := models.DrugCancelStockInfo{}
  5485. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5486. return info, err
  5487. }
  5488. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  5489. info := models.DrugCancelStockInfo{}
  5490. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  5491. return info, err
  5492. }
  5493. func UpdatedDrugInfo(id int64, count int64) (err error) {
  5494. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  5495. return err
  5496. }
  5497. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  5498. 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
  5499. return err
  5500. }
  5501. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  5502. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  5503. return err
  5504. }
  5505. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5506. 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
  5507. return err
  5508. }
  5509. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  5510. 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
  5511. return err
  5512. }
  5513. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64, warehouse_info_id int64) error {
  5514. 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
  5515. return err
  5516. }
  5517. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  5518. 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
  5519. return err
  5520. }
  5521. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  5522. info := models.WarehousingInfo{}
  5523. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5524. return info, err
  5525. }
  5526. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5527. info := models.WarehousingInfo{}
  5528. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  5529. return info, err
  5530. }
  5531. func GetLastWarehouseInfoTwo(goodid int64, storehouse_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  5532. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=? and org_id = ? and stock_count > 0", goodid, storehouse_id, user_org_id).Order("ctime").First(&info).Error
  5533. return info, err
  5534. }
  5535. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  5536. info := models.WarehousingInfo{}
  5537. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  5538. return info, err
  5539. }
  5540. func CreateWarehouseOut(out models.WarehouseOut) error {
  5541. err := XTWriteDB().Create(&out).Error
  5542. return err
  5543. }
  5544. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  5545. out := models.WarehouseOut{}
  5546. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  5547. return out, err
  5548. }
  5549. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  5550. err := XTWriteDB().Create(&info).Error
  5551. return err
  5552. }
  5553. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  5554. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5555. return err
  5556. }
  5557. func UpdateWareInfoById(id int64, count int64) error {
  5558. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  5559. return err
  5560. }
  5561. func UpdateWareInfoByIdSix(id int64, count int64) error {
  5562. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  5563. return err
  5564. }
  5565. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  5566. info := models.CancelStockInfo{}
  5567. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5568. return info, err
  5569. }
  5570. func GetLastCancelInfoSix(id int64, goodid int64, storehouse_id int64) (models.CancelStockInfo, error) {
  5571. info := models.CancelStockInfo{}
  5572. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1 and storehouse_id = ?", id, goodid, storehouse_id).Find(&info).Error
  5573. return info, err
  5574. }
  5575. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  5576. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5577. return info, err
  5578. }
  5579. func GetAllCancelInfoByIdSix(id int64, goodid int64, storehouse_id int64) (info []*models.CancelStockInfo, err error) {
  5580. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  5581. return info, err
  5582. }
  5583. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  5584. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  5585. return err
  5586. }
  5587. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5588. drugFlow := models.DrugFlow{}
  5589. 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
  5590. return err
  5591. }
  5592. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  5593. drugFlow := models.DrugFlow{}
  5594. 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
  5595. return err
  5596. }
  5597. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  5598. stock := models.DrugCancelStock{}
  5599. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5600. return stock, err
  5601. }
  5602. //func UpdateDrugCancelTX(id int64,tx *gorm.DB) (models.DrugCancelStock, error) {
  5603. // stock := models.DrugCancelStock{}
  5604. // err := tx.Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5605. // return stock, err
  5606. //}
  5607. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  5608. db := XTReadDB().Model(&info)
  5609. if warehouse_info_id > 0 {
  5610. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5611. }
  5612. if drug_id > 0 {
  5613. db = db.Where("drug_id = ? and status =1", drug_id)
  5614. }
  5615. err = db.Find(&info).Error
  5616. return info, err
  5617. }
  5618. //func GetDrugWarehouseOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  5619. // db := tx.Model(&info)
  5620. // if warehouse_info_id > 0 {
  5621. // db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  5622. // }
  5623. // if drug_id > 0 {
  5624. // db = db.Where("drug_id = ? and status =1", drug_id)
  5625. // }
  5626. // err = db.Find(&info).Error
  5627. // return info, err
  5628. //}
  5629. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  5630. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5631. return info, err
  5632. }
  5633. //func GetCancelDrugStockOutInfoTX(warehouse_info_id int64, drug_id int64,tx *gorm.DB) (info []*models.DrugCancelStockInfo, err error) {
  5634. // err = tx.Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  5635. // return info, err
  5636. //}
  5637. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  5638. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5639. return config, err
  5640. }
  5641. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  5642. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  5643. return config, err
  5644. }
  5645. func GetSearchGoodWarehouseList(keyword string, orgid int64, storehouse_id int64) (info []*models.BloodWarehouseInfo, err error) {
  5646. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.is_check = 1")
  5647. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  5648. fmt.Println(table)
  5649. likeKey := "%" + keyword + "%"
  5650. if orgid > 0 {
  5651. db = db.Where("x.org_id = ?", orgid)
  5652. }
  5653. if storehouse_id > 0 {
  5654. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5655. }
  5656. 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
  5657. return info, err
  5658. }
  5659. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  5660. err := XTWriteDB().Create(&adjustPrice).Error
  5661. return err
  5662. }
  5663. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  5664. likeKey := "%" + keyword + "%"
  5665. offset := (page - 1) * limit
  5666. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  5667. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5668. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5669. fmt.Println(table, tab)
  5670. if startime > 0 {
  5671. db = db.Where("x.start_time>=?", startime)
  5672. }
  5673. if endtime > 0 {
  5674. db = db.Where("x.start_time<=?", endtime)
  5675. }
  5676. if len(keyword) > 0 {
  5677. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  5678. }
  5679. if orgid > 0 {
  5680. db = db.Where("x.user_org_id = ?", orgid)
  5681. }
  5682. 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
  5683. return list, total, err
  5684. }
  5685. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  5686. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5687. return err
  5688. }
  5689. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  5690. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  5691. return err
  5692. }
  5693. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  5694. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  5695. return info, err
  5696. }
  5697. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  5698. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5699. return adjust, err
  5700. }
  5701. func CreateReportPrice(price *models.XtStockReportPrice) error {
  5702. err := XTWriteDB().Create(&price).Error
  5703. return err
  5704. }
  5705. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5706. likeKey := "%" + keyword + "%"
  5707. offset := (page - 1) * limit
  5708. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  5709. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5710. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5711. fmt.Println(table, tab)
  5712. if startime > 0 {
  5713. db = db.Where("x.ctime>=?", startime)
  5714. }
  5715. if endtime > 0 {
  5716. db = db.Where("x.ctime<=?", endtime)
  5717. }
  5718. if len(keyword) > 0 {
  5719. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5720. }
  5721. if orgid > 0 {
  5722. db = db.Where("x.user_org_id = ?", orgid)
  5723. }
  5724. 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
  5725. return adjust, total, err
  5726. }
  5727. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64, storehouse_id int64) (adjust []*models.VmStockInventory, total int64, err error) {
  5728. likeKey := "%" + keyword + "%"
  5729. offset := (page - 1) * limit
  5730. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  5731. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5732. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5733. fmt.Println(table, tab)
  5734. if len(keyword) > 0 {
  5735. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5736. }
  5737. if orgid > 0 {
  5738. db = db.Where("x.user_org_id = ?", orgid)
  5739. }
  5740. if startime > 0 {
  5741. db = db.Where("x.ctime >=?", startime)
  5742. }
  5743. if endtime > 0 {
  5744. db = db.Where("x.ctime <=?", endtime)
  5745. }
  5746. if storehouse_id > 0 {
  5747. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5748. }
  5749. err = db.Group("x.good_id").Select("x.ctime,x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,x.storehouse_id,t.packing_unit,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&adjust).Error
  5750. return adjust, total, err
  5751. }
  5752. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  5753. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5754. if inventory_type == 0 {
  5755. db = db.Where("x.inventory_type = 4")
  5756. }
  5757. if inventory_type > 0 {
  5758. db = db.Where("x.inventory_type = ?", inventory_type)
  5759. }
  5760. if len(keyword) > 0 {
  5761. likeKey := "%" + keyword + "%"
  5762. db = db.Where("x.warehousing_order like ?", likeKey)
  5763. }
  5764. if orgid > 0 {
  5765. db = db.Where("x.user_org_id = ?", orgid)
  5766. }
  5767. 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
  5768. return adjust, err
  5769. }
  5770. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  5771. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  5772. if orgid > 0 {
  5773. db = db.Where("x.user_org_id = ?", orgid)
  5774. }
  5775. 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
  5776. return adjust, err
  5777. }
  5778. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  5779. 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
  5780. return err
  5781. }
  5782. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  5783. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  5784. return report, err
  5785. }
  5786. func CreateInentory(inventory models.XtStockInventory) error {
  5787. err = XTWriteDB().Create(&inventory).Error
  5788. return err
  5789. }
  5790. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  5791. likeKey := "%" + keyword + "%"
  5792. offset := (page - 1) * limit
  5793. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  5794. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5795. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5796. fmt.Println(table, tab)
  5797. if startime > 0 {
  5798. db = db.Where("x.ctime>=?", startime)
  5799. }
  5800. if endtime > 0 {
  5801. db = db.Where("x.ctime<=?", endtime)
  5802. }
  5803. if len(keyword) > 0 {
  5804. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  5805. }
  5806. if orgid > 0 {
  5807. db = db.Where("x.user_org_id = ?", orgid)
  5808. }
  5809. if inventory_status > 0 {
  5810. db = db.Where("checker_status = ?", inventory_status)
  5811. }
  5812. 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
  5813. return adjust, total, err
  5814. }
  5815. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  5816. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  5817. return err
  5818. }
  5819. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  5820. info := models.XtStockAdjustPrice{}
  5821. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  5822. return info, err
  5823. }
  5824. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  5825. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  5826. return list, err
  5827. }
  5828. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  5829. info := models.WarehousingInfo{}
  5830. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  5831. return info, err
  5832. }
  5833. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  5834. info := models.WarehousingInfo{}
  5835. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  5836. //return info, err
  5837. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5838. if id > 0 {
  5839. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5840. }
  5841. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5842. return info, err
  5843. }
  5844. func GetWarehouseInfoByIdTen(id int64, storehouse_id int64) (models.WarehousingInfo, error) {
  5845. info := models.WarehousingInfo{}
  5846. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5847. if id > 0 {
  5848. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5849. }
  5850. if storehouse_id > 0 {
  5851. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5852. }
  5853. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  5854. return info, err
  5855. }
  5856. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  5857. 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
  5858. return err
  5859. }
  5860. func DeleteStockPrice(id int64) error {
  5861. info := models.XtStockAdjustPrice{}
  5862. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5863. return err
  5864. }
  5865. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  5866. report := models.XtStockReportPrice{}
  5867. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  5868. return report, err
  5869. }
  5870. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  5871. 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
  5872. return err
  5873. }
  5874. func DeleteDamage(id int64) error {
  5875. adjust := models.XtStockReportPrice{}
  5876. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5877. return err
  5878. }
  5879. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  5880. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  5881. return adjust, err
  5882. }
  5883. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  5884. inventory := models.XtStockInventory{}
  5885. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  5886. return inventory, err
  5887. }
  5888. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  5889. 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
  5890. return err
  5891. }
  5892. func DeleteInventory(id int64) error {
  5893. inventory := models.XtStockInventory{}
  5894. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5895. return err
  5896. }
  5897. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64) (list []*models.VmStockInventory, total int64, err error) {
  5898. likeKey := "%" + keyword + "%"
  5899. offset := (page - 1) * limit
  5900. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5901. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5902. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5903. fmt.Println(table, tab)
  5904. if len(keyword) > 0 {
  5905. db = db.Where("x.good_name like ?", likeKey)
  5906. }
  5907. if orgid > 0 {
  5908. db = db.Where("x.user_org_id = ?", orgid)
  5909. }
  5910. if storehouse_id > 0 {
  5911. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5912. }
  5913. err = db.Select("x.id,x.good_name,x.specification_name,x.warehousing_unit,x.count,x.buy_price,x.packing_price,x.new_price,x.manufacturer,x.dealer,x.remark,x.good_id,x.warehousing_order,x.license_number,x.start_time,x.creater,x.checker,x.checker_status,x.checker_time,x.total,x.number,x.expire_date,x.product_date,t.packing_unit,x.last_stock_count,x.type,x.inventory_type,x.stock_count,x.storehouse_id,x.ctime").Joins("left join sgj_users.sgj_user_admin_role as r on r.id = x.creater").Joins("left join xt_good_information as t on t.id =x.good_id").Count(&total).Offset(offset).Limit(limit).Order("x.id desc").Scan(&list).Error
  5914. return list, total, err
  5915. }
  5916. func GetInventoryDetailPrintMapList(keyword string, limit int64, page int64, orgid int64, storehouse_id int64, ids []string) (list []*models.VmStockInventory, total int64, err error) {
  5917. likeKey := "%" + keyword + "%"
  5918. offset := (page - 1) * limit
  5919. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  5920. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  5921. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  5922. fmt.Println(table, tab)
  5923. if len(keyword) > 0 {
  5924. db = db.Where("x.good_name like ?", likeKey)
  5925. }
  5926. if orgid > 0 {
  5927. db = db.Where("x.user_org_id = ?", orgid)
  5928. }
  5929. if storehouse_id > 0 {
  5930. db = db.Where("x.storehouse_id = ?", storehouse_id)
  5931. }
  5932. if len(ids) > 0 {
  5933. db = db.Where("x.id in(?)", ids)
  5934. }
  5935. 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
  5936. return list, total, err
  5937. }
  5938. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5939. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5940. return list, err
  5941. }
  5942. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  5943. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  5944. return list, err
  5945. }
  5946. func DeleteDrugPrice(id int64) error {
  5947. info := models.XtDrugAdjustPrice{}
  5948. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  5949. return err
  5950. }
  5951. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  5952. info := models.WarehousingInfo{}
  5953. db := XTReadDB().Model(&info).Where("status = 1")
  5954. if goodid > 0 {
  5955. db = db.Where("good_id = ?", goodid)
  5956. }
  5957. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  5958. return info, err
  5959. }
  5960. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  5961. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  5962. return err
  5963. }
  5964. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  5965. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  5966. return err
  5967. }
  5968. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  5969. err := XTWriteDB().Create(&record).Error
  5970. return err
  5971. }
  5972. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  5973. info := models.WarehousingInfo{}
  5974. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  5975. //return info, err
  5976. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  5977. if id > 0 {
  5978. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  5979. }
  5980. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  5981. return info, err
  5982. }
  5983. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  5984. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  5985. return info, err
  5986. }
  5987. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  5988. info := models.DrugWarehouseInfo{}
  5989. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  5990. return info, err
  5991. }
  5992. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  5993. 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
  5994. return err
  5995. }
  5996. func ModifyDrugWarehouseInfoSix(info *models.DrugWarehouseInfo, id int64) error {
  5997. 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
  5998. return err
  5999. }
  6000. func GetGoodInventoryWarehouseList(id int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  6001. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1 and storehouse_id=? and is_check= 1 and stock_count>0", id, storehouse_id).Preload("GoodInfo", "status = 1").Find(&info).Error
  6002. return info, err
  6003. }
  6004. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  6005. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  6006. return err
  6007. }
  6008. func UpdatedWarehousingInfoSix(info models.WarehousingInfo, id int64) error {
  6009. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount, "warehousing_count": info.WarehousingCount}).Error
  6010. return err
  6011. }
  6012. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  6013. db := XTReadDB().Model(&list).Where(" status = 1")
  6014. if good_id > 0 {
  6015. db = db.Where("good_id = ?", good_id)
  6016. }
  6017. if len(keyword) > 0 {
  6018. likeKey := "%" + keyword + "%"
  6019. db = db.Where("warehousing_order like ?", likeKey)
  6020. }
  6021. if inventory_type > 0 {
  6022. db = db.Where("inventory_type = ?", inventory_type)
  6023. }
  6024. if inventory_type == 0 {
  6025. db = db.Where("inventory_type = 4")
  6026. }
  6027. err = db.Find(&list).Error
  6028. return list, err
  6029. }
  6030. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  6031. flow := models.DrugFlow{}
  6032. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  6033. if err == gorm.ErrRecordNotFound {
  6034. return nil, err
  6035. }
  6036. if err != nil {
  6037. return nil, err
  6038. }
  6039. return &flow, nil
  6040. }
  6041. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  6042. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  6043. return info, err
  6044. }
  6045. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  6046. info := models.XtDrugWarehouseInfo{}
  6047. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  6048. return info, err
  6049. }
  6050. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  6051. info := models.WarehousingInfo{}
  6052. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  6053. return info, err
  6054. }
  6055. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  6056. flow := models.VmStockFlow{}
  6057. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  6058. if err == gorm.ErrRecordNotFound {
  6059. return nil, err
  6060. }
  6061. if err != nil {
  6062. return nil, err
  6063. }
  6064. return &flow, nil
  6065. }
  6066. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  6067. info := models.GoodInfo{}
  6068. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  6069. return info, err
  6070. }
  6071. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  6072. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  6073. return err
  6074. }
  6075. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  6076. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  6077. return err
  6078. }
  6079. func UpdateWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64) error {
  6080. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount, "price": info.Price, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "license_number": info.LicenseNumber, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "remark": info.Remark, "packing_price": info.PackingPrice, "storehouse_id": info.StorehouseId, "register_number": info.RegisterNumber, "good_id": info.GoodId, "good_code": info.GoodCode}).Error
  6081. return err
  6082. }
  6083. func UpdateNewWarehouseInfoByGoodIdOne(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  6084. err := tx.Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount, "price": info.Price, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "license_number": info.LicenseNumber, "manufacturer": info.Manufacturer, "dealer": info.Dealer, "remark": info.Remark, "packing_price": info.PackingPrice, "storehouse_id": info.StorehouseId, "register_number": info.RegisterNumber, "good_id": info.GoodId, "good_code": info.GoodCode}).Error
  6085. if err != nil {
  6086. tx.Rollback()
  6087. return err
  6088. }
  6089. return err
  6090. }
  6091. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6092. 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
  6093. return err
  6094. }
  6095. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  6096. 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
  6097. return err
  6098. }
  6099. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  6100. 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
  6101. return err
  6102. }
  6103. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  6104. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  6105. return err
  6106. }
  6107. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  6108. if orgid == 10653 {
  6109. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0 and x.stock_count>0")
  6110. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6111. fmt.Println(table)
  6112. if orgid > 0 {
  6113. db = db.Where("x.org_id = ?", orgid)
  6114. }
  6115. 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,x.stock_count,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
  6116. return info, err
  6117. } else {
  6118. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1 and x.warehousing_count>0")
  6119. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  6120. fmt.Println(table)
  6121. if orgid > 0 {
  6122. db = db.Where("x.org_id = ?", orgid)
  6123. }
  6124. 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,x.stock_count,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
  6125. return info, err
  6126. }
  6127. }
  6128. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  6129. warehouseInfo := models.XtDrugWarehouseInfo{}
  6130. 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
  6131. return err
  6132. }
  6133. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  6134. err := XTWriteDB().Create(&info).Error
  6135. return err
  6136. }
  6137. func CreatedNewCancelStock(info models.DrugCancelStockInfo, tx *gorm.DB) error {
  6138. err := tx.Create(&info).Error
  6139. if err != nil {
  6140. tx.Rollback()
  6141. return err
  6142. }
  6143. return err
  6144. }
  6145. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  6146. flow := models.VmStockFlow{}
  6147. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  6148. if err == gorm.ErrRecordNotFound {
  6149. return nil, err
  6150. }
  6151. if err != nil {
  6152. return nil, err
  6153. }
  6154. return &flow, nil
  6155. }
  6156. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  6157. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  6158. return err
  6159. }
  6160. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  6161. 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
  6162. return err
  6163. }
  6164. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  6165. flow := models.DrugWarehouseOutInfo{}
  6166. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6167. return flow, err
  6168. }
  6169. func GetLastNewWarehouseOutInfoById(drugid int64, tx *gorm.DB) (models.DrugWarehouseOutInfo, error) {
  6170. flow := models.DrugWarehouseOutInfo{}
  6171. err := tx.Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  6172. if err != gorm.ErrRecordNotFound {
  6173. if err != nil {
  6174. tx.Rollback()
  6175. return flow, err
  6176. }
  6177. }
  6178. return flow, err
  6179. }
  6180. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  6181. info := models.WarehouseOutInfo{}
  6182. 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
  6183. return info, err
  6184. }
  6185. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  6186. 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
  6187. return err
  6188. }
  6189. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  6190. 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
  6191. return err
  6192. }
  6193. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  6194. info := models.DrugWarehouseOutInfo{}
  6195. 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
  6196. if err == gorm.ErrRecordNotFound {
  6197. return nil, err
  6198. }
  6199. if err != nil {
  6200. return nil, err
  6201. }
  6202. return &info, nil
  6203. }
  6204. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  6205. drugFlow := models.DrugFlow{}
  6206. 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
  6207. if err == gorm.ErrRecordNotFound {
  6208. return nil, err
  6209. }
  6210. if err != nil {
  6211. return nil, err
  6212. }
  6213. return &drugFlow, nil
  6214. }
  6215. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  6216. 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
  6217. return err
  6218. }
  6219. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  6220. 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
  6221. return err
  6222. }
  6223. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  6224. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6225. return detail, err
  6226. }
  6227. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  6228. detail := models.DrugAutomaticReduceDetail{}
  6229. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  6230. if err == gorm.ErrRecordNotFound {
  6231. return nil, err
  6232. }
  6233. if err != nil {
  6234. return nil, err
  6235. }
  6236. return &detail, nil
  6237. }
  6238. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  6239. 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
  6240. return err
  6241. }
  6242. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  6243. 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
  6244. return err
  6245. }
  6246. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  6247. info := models.DrugWarehouseInfo{}
  6248. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6249. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6250. return info, err
  6251. }
  6252. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  6253. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  6254. return err
  6255. }
  6256. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  6257. info := models.DrugWarehouseInfo{}
  6258. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  6259. return err
  6260. }
  6261. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  6262. info := models.DrugCancelStockInfo{}
  6263. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6264. return info, err
  6265. }
  6266. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  6267. info := models.DrugWarehouseInfo{}
  6268. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  6269. return err
  6270. }
  6271. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  6272. info := models.DrugWarehouseInfo{}
  6273. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  6274. return err
  6275. }
  6276. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  6277. 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
  6278. return advice, err
  6279. }
  6280. func GetAllHisDoctorAdviceByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (advice []*models.HisDoctorAdviceInfo, err error) {
  6281. err = tx.Where("drug_id = ? and patient_id = ? and advice_date = ? and execution_state = 1 and status = 1 and user_org_id = ?", drugid, patient_id, advicedate, orgid).Find(&advice).Error
  6282. if err != gorm.ErrRecordNotFound {
  6283. if err != nil {
  6284. tx.Rollback()
  6285. return advice, err
  6286. }
  6287. }
  6288. return advice, err
  6289. }
  6290. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  6291. 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
  6292. return info, err
  6293. }
  6294. func GetAllDrugFlowByIdOne(drugid int64, patient_id int64, advicedate int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  6295. err = tx.Where("drug_id = ? and patient_id = ? and sys_record_time = ? and org_id = ? and status = 1", drugid, patient_id, advicedate, orgid).Find(&info).Error
  6296. if err != gorm.ErrRecordNotFound {
  6297. if err != nil {
  6298. tx.Rollback()
  6299. return info, err
  6300. }
  6301. }
  6302. return info, err
  6303. }
  6304. // 耗材加库存
  6305. func ModifyGoodAddInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6306. info := models.GoodInfo{}
  6307. 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
  6308. return info, err
  6309. }
  6310. func ModifyGoodAddInformationSix(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6311. info := models.GoodInfo{}
  6312. 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
  6313. 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
  6314. return info, err
  6315. }
  6316. func ModifyGoodReduceInformation(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6317. info := models.GoodInfo{}
  6318. 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
  6319. return info, err
  6320. }
  6321. func ModifyGoodReduceInformationTwo(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6322. info := models.GoodInfo{}
  6323. 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
  6324. 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
  6325. return info, err
  6326. }
  6327. func ModifyGoodReduceInformationSix(good_id int64, sum_count int64, orgid int64, retail_price float64) (models.GoodInfo, error) {
  6328. info := models.GoodInfo{}
  6329. 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
  6330. return info, err
  6331. }
  6332. func GetWarehousingByIdList(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6333. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6334. return info, err
  6335. }
  6336. func GetWarehousingByIdListTwenty(ids []string, orgid int64) (info []*models.WarehousingInfo, err error) {
  6337. err = XTReadDB().Model(&info).Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6338. return info, err
  6339. }
  6340. func GetWarehouseInfoByIdSix(id int64, orgid int64) (models.WarehousingInfo, error) {
  6341. info := models.WarehousingInfo{}
  6342. err := XTReadDB().Model(&info).Where("id = ? and status= 1 and org_id = ?", id, orgid).Find(&info).Error
  6343. return info, err
  6344. }
  6345. func GetWarehouseInfoByIdTwenty(id int64, orgid int64) (models.WarehousingInfo, error) {
  6346. info := models.WarehousingInfo{}
  6347. err := XTReadDB().Model(&info).Where("id = ? and org_id = ?", id, orgid).Find(&info).Error
  6348. return info, err
  6349. }
  6350. // 药品加库存
  6351. func ModifyDrugAddInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6352. info := models.BaseDrugLib{}
  6353. 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
  6354. return info, err
  6355. }
  6356. func ModifyDrugAddInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6357. info := models.BaseDrugLib{}
  6358. 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
  6359. 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
  6360. return info, err
  6361. }
  6362. func ModifyDrugReduceInformation(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6363. info := models.BaseDrugLib{}
  6364. 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
  6365. return info, err
  6366. }
  6367. func ModifyDrugReduceInformationTwo(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6368. info := models.BaseDrugLib{}
  6369. 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
  6370. 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
  6371. return info, err
  6372. }
  6373. func ModifyDrugReduceInformationSix(drug_id int64, sum_count int64, orgid int64, batch_retai_price float64) (models.BaseDrugLib, error) {
  6374. info := models.BaseDrugLib{}
  6375. 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
  6376. 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
  6377. return info, err
  6378. }
  6379. func GetLastDrugwarehouseInfoById(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6380. err = XTReadDB().Where("warehousing_id in(?) and org_id = ? and status = 1", ids, orgid).Find(&info).Error
  6381. return info, err
  6382. }
  6383. func GetLastDrugwarehouseInfoByIdTwenty(ids []string, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  6384. err = XTReadDB().Where("warehousing_id in(?) and org_id = ?", ids, orgid).Find(&info).Error
  6385. return info, err
  6386. }
  6387. func GetWarhouseOutById(id int64) (models.WarehouseOutInfo, error) {
  6388. info := models.WarehouseOutInfo{}
  6389. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  6390. return info, err
  6391. }
  6392. func FindeGoodWareHouseOutInfo(id int64, storehouse_id int64, orgid int64) (out []*models.WarehouseOutInfo, err error) {
  6393. err = XTReadDB().Where("warehouse_out_id = ? and storehouse_id =? and org_id = ?", id, storehouse_id, orgid).Find(&out).Error
  6394. return out, err
  6395. }
  6396. func ModifyWarehouseInfo(id int64, count int64) (models.WarehousingInfo, error) {
  6397. info := models.WarehousingInfo{}
  6398. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  6399. return info, err
  6400. }
  6401. func GetOrgIdList() (template []*models.VMGobalTemplate, err error) {
  6402. err = XTReadDB().Where("status = 1").Find(&template).Error
  6403. return template, err
  6404. }
  6405. func GetAllGoodList(orgid int64) (good []*models.GoodInfo, err error) {
  6406. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&good).Error
  6407. return good, err
  6408. }
  6409. func GetMobileGoodList(org_id int64) (list []*models.GoodInformationThirty, err error) {
  6410. err = XTReadDB().Where("org_id = ? and status =1", org_id).Find(&list).Error
  6411. return list, err
  6412. }
  6413. func GetAllMobileProjectList(orgid int64) (project []*models.HisProject, err error) {
  6414. err = XTReadDB().Where("user_org_id =? and status =1", orgid).Find(&project).Error
  6415. return project, err
  6416. }
  6417. func ModifyGoodAddInformationSeven(good_id int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6418. info := models.GoodInfo{}
  6419. err = XTWriteDB().Model(&info).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6420. return info, err
  6421. }
  6422. func GetStockCountWarehouseInfoById(good_id int64) (info []*models.WarehousingInfo, err error) {
  6423. db := XTReadDB().Where("status = 1")
  6424. if good_id > 0 {
  6425. db = db.Where("good_id = ?", good_id)
  6426. }
  6427. err = db.Find(&info).Error
  6428. return info, err
  6429. }
  6430. func GetDrugStockCountWarehoseInfoById(drug_id int64) (info []*models.DrugWarehouseInfo, err error) {
  6431. db := XTReadDB().Model(&info).Where("status = 1")
  6432. if drug_id > 0 {
  6433. db = db.Where("drug_id = ?", drug_id)
  6434. }
  6435. err = db.Find(&info).Error
  6436. return info, err
  6437. }
  6438. func UpdateModifyDrugInfoById(drug_id int64, sum_count int64, orgid int64) (models.BaseDrugLib, error) {
  6439. lib := models.BaseDrugLib{}
  6440. err := XTWriteDB().Model(&lib).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6441. return lib, err
  6442. }
  6443. func GetWarehouseBySecondWarehouseId(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (*models.Warehousing, error) {
  6444. info := models.Warehousing{}
  6445. 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
  6446. if err == gorm.ErrRecordNotFound {
  6447. return nil, err
  6448. }
  6449. if err != nil {
  6450. return nil, err
  6451. }
  6452. return &info, nil
  6453. }
  6454. func GetLastWarehouseById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.Warehousing, error) {
  6455. info := models.Warehousing{}
  6456. 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
  6457. return info, err
  6458. }
  6459. func GetDrugWarehouseBySyS(orgid int64, is_sys int64, sencond_wahouse_id int64, record_date int64) (*models.DrugWarehouseOut, error) {
  6460. out := models.DrugWarehouseOut{}
  6461. 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
  6462. if err == gorm.ErrRecordNotFound {
  6463. return nil, err
  6464. }
  6465. if err != nil {
  6466. return nil, err
  6467. }
  6468. return &out, nil
  6469. }
  6470. func GetLastDrugWarehouseOutById(is_sys int64, sencond_wahouse_id int64, orgid int64, record_date int64) (models.DrugWarehouseOut, error) {
  6471. info := models.DrugWarehouseOut{}
  6472. 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
  6473. return info, err
  6474. }
  6475. func GetDrugWarehouseingBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (*models.DrugWarehouse, error) {
  6476. info := models.DrugWarehouse{}
  6477. 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
  6478. if err == gorm.ErrRecordNotFound {
  6479. return nil, err
  6480. }
  6481. if err != nil {
  6482. return nil, err
  6483. }
  6484. return &info, nil
  6485. }
  6486. func CreateDrugWaresing(warehouse models.DrugWarehouse) error {
  6487. err := XTWriteDB().Create(&warehouse).Error
  6488. return err
  6489. }
  6490. func GetLastDrugWarehouseBySys(is_sys int64, second_warehouse_id int64, orgid int64, record_date int64) (models.DrugWarehouse, error) {
  6491. info := models.DrugWarehouse{}
  6492. 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
  6493. return info, err
  6494. }
  6495. func ModifyGoodReduceInformationTwenty(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6496. info := models.GoodInfo{}
  6497. 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
  6498. 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
  6499. return info, err
  6500. }
  6501. func ModifyGoodReduceInformationTwentyOne(good_id int64, waresing_count int64, sum_count int64, orgid int64) (models.GoodInfo, error) {
  6502. info := models.GoodInfo{}
  6503. 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
  6504. 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
  6505. return info, err
  6506. }
  6507. func UpdateMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6508. tx := XTWriteDB().Begin()
  6509. 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
  6510. if err != nil {
  6511. tx.Rollback()
  6512. return err
  6513. }
  6514. tx.Commit()
  6515. return err
  6516. }
  6517. func UpdateNewMedicalSumCount(drug_id int64, sum_count int64, sum_in_count int64, orgid int64, tx *gorm.DB) error {
  6518. 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
  6519. if err != nil {
  6520. tx.Rollback()
  6521. return err
  6522. }
  6523. return err
  6524. }
  6525. func UpdateMedicalSumCountOne(drug_id int64, sum_count int64, orgid int64) error {
  6526. tx := XTWriteDB().Begin()
  6527. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drug_id, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  6528. if err != nil {
  6529. tx.Rollback()
  6530. return err
  6531. }
  6532. tx.Commit()
  6533. return err
  6534. }
  6535. //func UpdateMedicalSumCountTX(drug_id int64, sum_count int64, sum_in_count int64, orgid int64,tx *gorm.DB) error {
  6536. // 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
  6537. // return err
  6538. //}
  6539. func UpdateGoodByGoodId(good_id int64, sum_count int64, sum_in_count int64, orgid int64) error {
  6540. 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
  6541. return err
  6542. }
  6543. func GetDrugDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6544. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6545. table := XTReadDB().Table("xt_base_drug as x d")
  6546. fmt.Println(table)
  6547. if startime > 0 {
  6548. db = db.Where("x.advice_date >= ?", startime)
  6549. }
  6550. if endtime > 0 {
  6551. db = db.Where("x.advice_date <= ?", endtime)
  6552. }
  6553. if orgid > 0 {
  6554. db = db.Where("x.user_org_id = ?", orgid)
  6555. }
  6556. if len(keyword) > 0 {
  6557. likeKey := "%" + keyword + "%"
  6558. 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)
  6559. }
  6560. offset := (page - 1) * limit
  6561. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.created_time desc").Group("x.drug_id").Scan(&adviceinfo).Error
  6562. return adviceinfo, total, err
  6563. }
  6564. func GetDrugDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (adviceinfo []*models.BloodHisDoctorAdviceInfo, total int64, err error) {
  6565. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  6566. table := XTReadDB().Table("xt_base_drug as x d")
  6567. fmt.Println(table)
  6568. if startime > 0 {
  6569. db = db.Where("x.advice_date >= ?", startime)
  6570. }
  6571. if endtime > 0 {
  6572. db = db.Where("x.advice_date <= ?", endtime)
  6573. }
  6574. if orgid > 0 {
  6575. db = db.Where("x.user_org_id = ?", orgid)
  6576. }
  6577. if len(keyword) > 0 {
  6578. likeKey := "%" + keyword + "%"
  6579. 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)
  6580. }
  6581. err = db.Order("x.created_time desc").Scan(&adviceinfo).Error
  6582. return adviceinfo, total, err
  6583. }
  6584. func GetAllDrugSummary(orgid int64) (drug []*models.BaseDrugLib, err error) {
  6585. err = XTReadDB().Where("org_id = ? and status = 1 and is_user = 1", orgid).Find(&drug).Error
  6586. return drug, err
  6587. }
  6588. func GetGoodDetailSummary(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6589. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6590. table := XTReadDB().Table("xt_good_information as d")
  6591. fmt.Println(table)
  6592. if startime > 0 {
  6593. db = db.Where("x.record_date >= ?", startime)
  6594. }
  6595. if endtime > 0 {
  6596. db = db.Where("x.record_date <= ?", endtime)
  6597. }
  6598. if orgid > 0 {
  6599. db = db.Where("x.user_org_id = ?", orgid)
  6600. }
  6601. if len(keyword) > 0 {
  6602. likeKey := "%" + keyword + "%"
  6603. 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")
  6604. }
  6605. offset := (page - 1) * limit
  6606. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Group("x.project_id").Scan(&project).Error
  6607. return project, total, err
  6608. }
  6609. func GetGoodDetailSummaryOne(startime int64, endtime int64, keyword string, limit int64, page int64, orgid int64) (project []*models.BloodHisPrescriptionProject, total int64, err error) {
  6610. db := XTReadDB().Table("his_prescription_project as x").Where("x.status = 1 and x.type = 3")
  6611. table := XTReadDB().Table("xt_good_information as d")
  6612. fmt.Println(table)
  6613. if startime > 0 {
  6614. db = db.Where("x.record_date >= ?", startime)
  6615. }
  6616. if endtime > 0 {
  6617. db = db.Where("x.record_date <= ?", endtime)
  6618. }
  6619. if orgid > 0 {
  6620. db = db.Where("x.user_org_id = ?", orgid)
  6621. }
  6622. if len(keyword) > 0 {
  6623. likeKey := "%" + keyword + "%"
  6624. 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")
  6625. }
  6626. err = db.Order("x.ctime desc").Group("x.id").Scan(&project).Error
  6627. return project, total, err
  6628. }
  6629. func GeAllGoodInformation(orgid int64) (good []*models.GoodInfo, err error) {
  6630. err = XTReadDB().Where("org_id =? and status = 1 and is_user = 1", orgid).Find(&good).Error
  6631. return good, err
  6632. }
  6633. func GetStockSettingIsExsit(orgid int64) (*models.XtStockSetting, error) {
  6634. setting := models.XtStockSetting{}
  6635. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6636. if err == gorm.ErrRecordNotFound {
  6637. return nil, err
  6638. }
  6639. if err != nil {
  6640. return nil, err
  6641. }
  6642. return &setting, nil
  6643. }
  6644. func CreateStockSetting(setting models.XtStockSetting) error {
  6645. err := XTWriteDB().Create(&setting).Error
  6646. return err
  6647. }
  6648. func UpdateStockSetting(orgid int64, setting models.XtStockSetting) error {
  6649. err := XTWriteDB().Model(&models.XtStockSetting{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_type": setting.IsType}).Error
  6650. return err
  6651. }
  6652. func FindStockSettingById(orgid int64) (models.XtStockSetting, error) {
  6653. setting := models.XtStockSetting{}
  6654. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6655. return setting, err
  6656. }
  6657. func GetAutoStockOutById(good_id int64, record_date int64, orgid int64, patient_id int64) (models.BloodAutomaticReduceDetail, error) {
  6658. detail := models.BloodAutomaticReduceDetail{}
  6659. 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
  6660. return detail, err
  6661. }
  6662. func GetAutoWarehouseOutList(good_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfoNight, error) {
  6663. detail := models.WarehouseOutInfoNight{}
  6664. 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
  6665. return detail, err
  6666. }
  6667. func GetAdviceConfig(orgid int64) (*models.XtAdviceSetting, error) {
  6668. setting := models.XtAdviceSetting{}
  6669. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6670. if err == gorm.ErrRecordNotFound {
  6671. return nil, err
  6672. }
  6673. if err != nil {
  6674. return nil, err
  6675. }
  6676. return &setting, nil
  6677. }
  6678. func CreateAdviceSetting(setting models.XtAdviceSetting) error {
  6679. err := XTWriteDB().Create(&setting).Error
  6680. return err
  6681. }
  6682. func UpdateAdviceSetting(orgid int64, setting models.XtAdviceSetting) error {
  6683. 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
  6684. return err
  6685. }
  6686. func FindAdviceSettingById(orgid int64) (models.XtAdviceSetting, error) {
  6687. setting := models.XtAdviceSetting{}
  6688. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&setting).Error
  6689. return setting, err
  6690. }
  6691. func GetPrescriptionConfig(orgid int64) (*models.XtPrescriptionConfig, error) {
  6692. config := models.XtPrescriptionConfig{}
  6693. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6694. if err == gorm.ErrRecordNotFound {
  6695. return nil, err
  6696. }
  6697. if err != nil {
  6698. return nil, err
  6699. }
  6700. return &config, nil
  6701. }
  6702. func CreatePrescriptionConfig(prescriptionConfig models.XtPrescriptionConfig) error {
  6703. err := XTWriteDB().Create(&prescriptionConfig).Error
  6704. return err
  6705. }
  6706. func UpdatePrescriptionConfig(orgid int64, config models.XtPrescriptionConfig) error {
  6707. 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
  6708. return err
  6709. }
  6710. func FindPrescriptionConfigById(orgid int64) (models.XtPrescriptionConfig, error) {
  6711. config := models.XtPrescriptionConfig{}
  6712. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6713. return config, err
  6714. }
  6715. func UpdateWarehouseInfoTwentyFive(maxNumber int64, drugid int64, orgID int64, storehouse_id int64, drup *models.XtBaseDrug) (err error) {
  6716. var deliver_number int64 = 0
  6717. var stock_number int64 = 0
  6718. deliver_number = maxNumber
  6719. // 根据先进先出原则,查询最先入库的批次,进行出库
  6720. // 如果没有对应的库存,则报错
  6721. //开启事物
  6722. warehouse, err := FindLastDrugWarehousingInfoByID(drugid, storehouse_id)
  6723. if err != nil {
  6724. return err
  6725. }
  6726. // 将该批次的剩余库存数量转换为拆零数量
  6727. stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
  6728. // 当库存数量大于或等于出库数量的话,则正常出库该批次
  6729. if stock_number >= deliver_number {
  6730. var maxNumber int64 = 0
  6731. var minNumber int64 = 0
  6732. var stock_max_number int64 = 0
  6733. stock_max_number = warehouse.StockMaxNumber
  6734. maxNumber = deliver_number / drup.MinNumber
  6735. minNumber = deliver_number % drup.MinNumber
  6736. if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
  6737. minNumber = maxNumber
  6738. }
  6739. if drup.MaxUnit != drup.MinUnit {
  6740. if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
  6741. return errors.New("库存数量不足")
  6742. }
  6743. }
  6744. warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
  6745. if warehouse.StockMaxNumber < 0 {
  6746. warehouse.StockMaxNumber = 0
  6747. }
  6748. if warehouse.StockMinNumber < 0 {
  6749. warehouse.StockMinNumber = 0
  6750. }
  6751. warehouse.Mtime = time.Now().Unix()
  6752. if warehouse.StockMinNumber < minNumber {
  6753. warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
  6754. warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
  6755. } else {
  6756. if minNumber > 0 {
  6757. if minNumber == 1 && maxNumber == 1 && drup.MaxUnit != drup.MinUnit {
  6758. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6759. } else {
  6760. warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
  6761. }
  6762. }
  6763. if minNumber == 0 && maxNumber != 1 {
  6764. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6765. }
  6766. }
  6767. if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
  6768. warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
  6769. if warehouse.StockMinNumber == 0 {
  6770. warehouse.StockMaxNumber = stock_max_number
  6771. }
  6772. }
  6773. if drup.MaxUnit != drup.MinUnit {
  6774. if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
  6775. return errors.New("库存数量不足")
  6776. }
  6777. }
  6778. if warehouse.StockMinNumber <= 0 {
  6779. warehouse.StockMinNumber = 0
  6780. }
  6781. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6782. fmt.Println(errThree)
  6783. //查询默认仓库
  6784. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6785. //查询默认仓库剩余多少库存
  6786. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6787. var sum_count int64
  6788. var sum_in_count int64
  6789. for _, it := range list {
  6790. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6791. if it.MaxUnit == baseDrug.MaxUnit {
  6792. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6793. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6794. sum_count += it.StockMaxNumber + it.StockMinNumber
  6795. sum_in_count += it.WarehousingCount
  6796. }
  6797. }
  6798. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6799. return nil
  6800. } else {
  6801. warehouse.StockMaxNumber = 0
  6802. warehouse.StockMinNumber = 0
  6803. warehouse.Mtime = time.Now().Unix()
  6804. errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
  6805. fmt.Println(errThree)
  6806. //查询默认仓库
  6807. houseConfig, _ := GetAllStoreHouseConfig(orgID)
  6808. //查询默认仓库剩余多少库存
  6809. list, _ := GetDrugSumCountByStorehouseId(houseConfig.DrugStorehouseOut, orgID, warehouse.DrugId)
  6810. var sum_count int64
  6811. var sum_in_count int64
  6812. for _, it := range list {
  6813. baseDrug, _ := GetBaseDrugMedical(it.DrugId)
  6814. if it.MaxUnit == baseDrug.MaxUnit {
  6815. it.StockMaxNumber = it.StockMaxNumber * baseDrug.MinNumber
  6816. it.WarehousingCount = it.WarehousingCount * baseDrug.MinNumber
  6817. sum_count += it.StockMaxNumber + it.StockMinNumber
  6818. sum_in_count += it.WarehousingCount
  6819. }
  6820. }
  6821. UpdateMedicalSumCount(warehouse.DrugId, sum_count, sum_in_count, orgID)
  6822. if errThree != nil {
  6823. return errThree
  6824. }
  6825. // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
  6826. prescribingNumber_two_temp := deliver_number - stock_number
  6827. UpdateWarehouseInfoTwentyFive(prescribingNumber_two_temp, drugid, orgID, storehouse_id, drup)
  6828. }
  6829. return
  6830. }
  6831. func UpdateWarehouseInfoTentyFour(count int64, id int64) error {
  6832. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6833. return err
  6834. }
  6835. func UpdateWarehouseInfoTentyThree(count int64, id int64) error {
  6836. err := writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  6837. return err
  6838. }
  6839. func GetDrugOpenConfig(orgid int64) (*models.DrugOutConfig, error) {
  6840. config := models.DrugOutConfig{}
  6841. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6842. if err == gorm.ErrRecordNotFound {
  6843. return nil, err
  6844. }
  6845. if err != nil {
  6846. return nil, err
  6847. }
  6848. return &config, nil
  6849. }
  6850. func GetDrugOpenConfigOne(orgid int64) (models.DrugOutConfig, error) {
  6851. config := models.DrugOutConfig{}
  6852. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).First(&config).Error
  6853. return config, err
  6854. }
  6855. func FindXTHisRecordByOrgIdSix(org_id int64) (models.XtHisConfig, error) {
  6856. config := models.XtHisConfig{}
  6857. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).Find(&config).Error
  6858. return config, err
  6859. }
  6860. func CreateDrugOutConfig(config models.DrugOutConfig) error {
  6861. err := XTWriteDB().Create(&config).Error
  6862. return err
  6863. }
  6864. func UpdateDrugOutConfig(orgid int64, drug_out_open int64) error {
  6865. err := XTWriteDB().Model(&models.DrugOutConfig{}).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": drug_out_open}).Error
  6866. return err
  6867. }
  6868. func FindDrugOutConfigById(orgid int64) (models.DrugOutConfig, error) {
  6869. config := models.DrugOutConfig{}
  6870. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6871. return config, err
  6872. }
  6873. func FindeDrugSettleConfigById(orgid int64) (models.HisSettleStockConfig, error) {
  6874. config := models.HisSettleStockConfig{}
  6875. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
  6876. return config, err
  6877. }
  6878. func UpdatedWarehouseOut(id int64) error {
  6879. err := XTWriteDB().Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6880. err = XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6881. return err
  6882. }
  6883. func UpdatedNewWarehouseOut(id int64, tx *gorm.DB) error {
  6884. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6885. if err != nil {
  6886. tx.Rollback()
  6887. return err
  6888. }
  6889. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  6890. if err != nil {
  6891. tx.Rollback()
  6892. return err
  6893. }
  6894. return err
  6895. }
  6896. func UpdatedSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo) error {
  6897. tx := XTWriteDB().Begin()
  6898. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6899. if err != nil {
  6900. tx.Rollback()
  6901. return err
  6902. }
  6903. tx.Commit()
  6904. return err
  6905. }
  6906. func UpdatedNewSigleWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6907. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"warehouse_info_id": info.WarehouseInfotId}).Error
  6908. if err != nil {
  6909. tx.Rollback()
  6910. return err
  6911. }
  6912. return err
  6913. }
  6914. func GetLastGoodWarehouseOutInfo(orgid int64, id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  6915. err = tx.Where("org_id = ? and status = 1 and warehouse_out_id = ?", orgid, id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  6916. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  6917. }).Find(&info).Error
  6918. if err != nil {
  6919. tx.Rollback()
  6920. return info, err
  6921. }
  6922. return info, err
  6923. }
  6924. func GetLastGoodWarehouseOutInfoById(orgid int64) (models.WarehouseOutInfo, error) {
  6925. info := models.WarehouseOutInfo{}
  6926. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&info).Error
  6927. return info, err
  6928. }
  6929. func ModifyWarehouseOutInfo(id int64, info *models.WarehouseOutInfo, tx *gorm.DB) error {
  6930. err := tx.Model(&models.WarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "count": info.Count, "price": info.Price, "total_price": info.TotalPrice, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "number": info.Number, "license_number": info.LicenseNumber, "storehouse_id": info.StorehouseId, "admin_user_id": info.AdminUserId, "buy_price": info.BuyPrice, "stock_count": info.StockCount, "register_number": info.RegisterNumber}).Error
  6931. if err != nil {
  6932. tx.Rollback()
  6933. return err
  6934. }
  6935. return err
  6936. }
  6937. func UpdateWarehouseOutFlow(info models.VmStockFlow, id int64) error {
  6938. 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
  6939. return err
  6940. }
  6941. func GetLastGoodWarehouseOutTwenty(orgid int64) (models.WarehouseOut, error) {
  6942. out := models.WarehouseOut{}
  6943. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6944. return out, err
  6945. }
  6946. func GetLastGoodWarehouseOutTwentyOne(orgid int64, tx *gorm.DB) (models.WarehouseOut, error) {
  6947. out := models.WarehouseOut{}
  6948. err := tx.Where("org_id = ? and status = 1", orgid).Last(&out).Error
  6949. if err != gorm.ErrRecordNotFound {
  6950. if err != nil {
  6951. tx.Rollback()
  6952. return out, err
  6953. }
  6954. }
  6955. return out, err
  6956. }
  6957. func UpdateaGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64) error {
  6958. ut := writeDb.Begin()
  6959. 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
  6960. if err != nil {
  6961. ut.Rollback()
  6962. return err
  6963. }
  6964. ut.Commit()
  6965. return err
  6966. }
  6967. func ModifyGoodOutInfoLog(good_id int64, id int64, tx *gorm.DB) (models.XtGoodWarehouseOutInfoLog, error) {
  6968. outInfoLog := models.XtGoodWarehouseOutInfoLog{}
  6969. err := tx.Model(&outInfoLog).Where("good_id = ? and warehouse_out_info_id = ?", good_id, id).Updates(map[string]interface{}{"status": 0}).Error
  6970. if err != nil {
  6971. tx.Rollback()
  6972. return outInfoLog, err
  6973. }
  6974. return outInfoLog, err
  6975. }
  6976. func UpdateaNewGoodWarehouseInfo(count int64, good_id int64, orgid int64, warehouse_info_id int64, tx *gorm.DB) error {
  6977. err := tx.Model(&models.WarehousingInfo{}).Where("good_id = ? and org_id = ? and status = 1 and id = ?", good_id, orgid, warehouse_info_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  6978. if err != nil {
  6979. tx.Rollback()
  6980. return err
  6981. }
  6982. return err
  6983. }
  6984. func ModifyGoodWarehouseOut(id int64) error {
  6985. ut := writeDb.Begin()
  6986. err := ut.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6987. if err != nil {
  6988. ut.Rollback()
  6989. return err
  6990. }
  6991. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  6992. if err != nil {
  6993. ut.Rollback()
  6994. return err
  6995. }
  6996. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  6997. if err != nil {
  6998. ut.Rollback()
  6999. return err
  7000. }
  7001. err = ut.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  7002. if err != nil {
  7003. ut.Rollback()
  7004. return err
  7005. }
  7006. ut.Commit()
  7007. return err
  7008. }
  7009. func ModifyNewGoodWarehouseOut(id int64, tx *gorm.DB) error {
  7010. err := tx.Model(&models.WarehouseOut{}).Where("id = ? and status =1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7011. if err != nil {
  7012. tx.Rollback()
  7013. return err
  7014. }
  7015. err = tx.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7016. if err != nil {
  7017. tx.Rollback()
  7018. return err
  7019. }
  7020. err = tx.Model(&models.VmStockFlow{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  7021. if err != nil {
  7022. tx.Rollback()
  7023. return err
  7024. }
  7025. err = tx.Model(&models.XtWarehouseFlushInfo{}).Where("warehouse_out_id in(?) and status = 1", id).Update(map[string]interface{}{"status": 0}).Error
  7026. if err != nil {
  7027. tx.Rollback()
  7028. return err
  7029. }
  7030. return err
  7031. }
  7032. func GetGoodWarehouseInfoById(waresing_id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7033. err = XTReadDB().Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  7034. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  7035. }).Find(&info).Error
  7036. return info, err
  7037. }
  7038. func GetNewGoodWarehouseInfoById(waresing_id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7039. err = tx.Where("org_id = ? and status = 1 and warehousing_id = ?", orgid, waresing_id).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  7040. return tx.Where("org_id=? and status = 1", orgid).Preload("GoodsType", "status = 1")
  7041. }).Find(&info).Error
  7042. if err != gorm.ErrRecordNotFound {
  7043. if err != nil {
  7044. tx.Rollback()
  7045. return info, err
  7046. }
  7047. }
  7048. return info, err
  7049. }
  7050. func FindLastWarehousing(order string) (info models.Warehousing, err error) {
  7051. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  7052. return info, err
  7053. }
  7054. func UpdateWarehouseDetail(info *models.WarehousingInfo, id int64) error {
  7055. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "warehousing_unit": info.WarehousingUnit, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "license_number": info.LicenseNumber, "packing_price": info.PackingPrice, "register_number": info.RegisterNumber, "storehouse_id": info.StorehouseId, "good_code": info.GoodCode}).Error
  7056. return err
  7057. }
  7058. func UpdateNewWarehouseDetail(info *models.WarehousingInfo, id int64, tx *gorm.DB) error {
  7059. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"good_id": info.GoodId, "good_type_id": info.GoodTypeId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "warehousing_unit": info.WarehousingUnit, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "license_number": info.LicenseNumber, "packing_price": info.PackingPrice, "register_number": info.RegisterNumber, "storehouse_id": info.StorehouseId, "good_code": info.GoodCode}).Error
  7060. if err != nil {
  7061. tx.Rollback()
  7062. return err
  7063. }
  7064. return err
  7065. }
  7066. func UpdateCheckWarehouseInfo(id int64, checker int64) error {
  7067. tx := XTWriteDB().Begin()
  7068. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7069. if err != nil {
  7070. tx.Rollback()
  7071. return err
  7072. }
  7073. tx.Commit()
  7074. return err
  7075. }
  7076. func UpdateNewCheckWarehouseInfo(id int64, checker int64, tx *gorm.DB) error {
  7077. err := tx.Model(&models.Warehousing{}).Where("id =? and status = 1", id).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7078. if err != nil {
  7079. tx.Rollback()
  7080. return err
  7081. }
  7082. return err
  7083. }
  7084. func GetWarehouseInfoList(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  7085. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7086. return info, err
  7087. }
  7088. func GetNewWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7089. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7090. if err != gorm.ErrRecordNotFound {
  7091. if err != nil {
  7092. tx.Rollback()
  7093. return info, err
  7094. }
  7095. }
  7096. return info, err
  7097. }
  7098. func UpdateWarehouseInfoByIdListThree(count int64, id int64) error {
  7099. tx := XTWriteDB().Begin()
  7100. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7101. if err != nil {
  7102. tx.Rollback()
  7103. return err
  7104. }
  7105. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7106. if err != nil {
  7107. tx.Rollback()
  7108. return err
  7109. }
  7110. tx.Commit()
  7111. return err
  7112. }
  7113. func UpdateNewWarehouseInfoByIdListThree(count int64, id int64, tx *gorm.DB) error {
  7114. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"stock_count": count}).Error
  7115. if err != nil {
  7116. tx.Rollback()
  7117. return err
  7118. }
  7119. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7120. if err != nil {
  7121. tx.Rollback()
  7122. return err
  7123. }
  7124. return err
  7125. }
  7126. func FindLastWarehousingSix(org_id int64) (info models.Warehousing, err error) {
  7127. err = readDb.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7128. return info, err
  7129. }
  7130. func FindNewLastWarehousingSix(org_id int64, tx *gorm.DB) (info models.Warehousing, err error) {
  7131. err = tx.Model(&models.Warehousing{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7132. if err != gorm.ErrRecordNotFound {
  7133. if err != nil {
  7134. tx.Rollback()
  7135. return info, err
  7136. }
  7137. }
  7138. return info, err
  7139. }
  7140. func FindLastWarehousingSeven(org_id int64) (info models.DrugWarehouse, err error) {
  7141. err = readDb.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7142. return info, err
  7143. }
  7144. func FindNewLastWarehousingSeven(org_id int64, tx *gorm.DB) (info models.DrugWarehouse, err error) {
  7145. err = tx.Model(&models.DrugWarehouse{}).Where("org_id = ? AND status = 1", org_id).Last(&info).Error
  7146. if err != gorm.ErrRecordNotFound {
  7147. if err != nil {
  7148. tx.Rollback()
  7149. return info, err
  7150. }
  7151. }
  7152. return info, err
  7153. }
  7154. func FindDrugWarehouseInfoListById(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7155. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7156. return info, err
  7157. }
  7158. func FindNewDrugWarehouseInfoListById(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7159. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "status = 1 and org_id =?", orgid).Find(&info).Error
  7160. if err != gorm.ErrRecordNotFound {
  7161. if err != nil {
  7162. tx.Rollback()
  7163. return info, err
  7164. }
  7165. }
  7166. return info, err
  7167. }
  7168. func CreatedWarehouseing(info *models.DrugWarehouseInfo) error {
  7169. err := XTWriteDB().Create(&info).Error
  7170. return err
  7171. }
  7172. func CreatedNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7173. err := tx.Create(&info).Error
  7174. if err != nil {
  7175. tx.Rollback()
  7176. return err
  7177. }
  7178. return err
  7179. }
  7180. func UpdateWarehouseing(info *models.DrugWarehouseInfo) error {
  7181. err := XTWriteDB().Save(&info).Error
  7182. return err
  7183. }
  7184. func UpdateNewWarehouseing(info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7185. err := tx.Save(&info).Error
  7186. if err != nil {
  7187. tx.Rollback()
  7188. return err
  7189. }
  7190. return err
  7191. }
  7192. func CheckWarehousingInfo(id int64, orgid int64, checker int64) error {
  7193. tx := XTWriteDB().Begin()
  7194. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7195. if err != nil {
  7196. tx.Rollback()
  7197. return err
  7198. }
  7199. tx.Commit()
  7200. return err
  7201. }
  7202. func CheckNewWarehousingInfo(id int64, orgid int64, checker int64, tx *gorm.DB) error {
  7203. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"is_check": 1, "checker": checker}).Error
  7204. if err != nil {
  7205. tx.Rollback()
  7206. return err
  7207. }
  7208. return err
  7209. }
  7210. func GetWarehousingInfoByList(id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  7211. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7212. return info, err
  7213. }
  7214. func GetNewWarehousingInfoByList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7215. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Find(&info).Error
  7216. if err != gorm.ErrRecordNotFound {
  7217. if err != nil {
  7218. tx.Rollback()
  7219. return info, err
  7220. }
  7221. }
  7222. return info, err
  7223. }
  7224. func AddDrugWarehouseStockMaxNumber(count int64, id int64) error {
  7225. tx := XTWriteDB().Begin()
  7226. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7227. if err != nil {
  7228. tx.Rollback()
  7229. return err
  7230. }
  7231. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7232. if err != nil {
  7233. tx.Rollback()
  7234. return err
  7235. }
  7236. tx.Commit()
  7237. return err
  7238. }
  7239. func AddNewDrugWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7240. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7241. if err != nil {
  7242. tx.Rollback()
  7243. return err
  7244. }
  7245. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7246. if err != nil {
  7247. tx.Rollback()
  7248. return err
  7249. }
  7250. return err
  7251. }
  7252. func UpdateWarehousingInfoFlow(id int64, orgid int64) error {
  7253. tx := XTWriteDB().Begin()
  7254. 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
  7255. if err != nil {
  7256. tx.Rollback()
  7257. return err
  7258. }
  7259. tx.Commit()
  7260. return err
  7261. }
  7262. func UpdateNewWarehousingInfoFlow(id int64, orgid int64, tx *gorm.DB) error {
  7263. 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
  7264. if err != nil {
  7265. tx.Rollback()
  7266. return err
  7267. }
  7268. return err
  7269. }
  7270. func AddDrugWarehouseStockMinNumber(count int64, id int64) error {
  7271. tx := XTWriteDB().Begin()
  7272. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7273. if err != nil {
  7274. tx.Rollback()
  7275. return err
  7276. }
  7277. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7278. tx.Commit()
  7279. return err
  7280. }
  7281. func AddNewDrugWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7282. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7283. if err != nil {
  7284. tx.Rollback()
  7285. return err
  7286. }
  7287. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7288. if err != nil {
  7289. tx.Rollback()
  7290. return err
  7291. }
  7292. return err
  7293. }
  7294. func ReturnCheckWarehouseingInfo(id int64, orgid int64) error {
  7295. tx := XTWriteDB().Begin()
  7296. err := XTWriteDB().Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7297. if err != nil {
  7298. tx.Rollback()
  7299. return err
  7300. }
  7301. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7302. if err != nil {
  7303. tx.Rollback()
  7304. return err
  7305. }
  7306. tx.Commit()
  7307. return err
  7308. }
  7309. func ReturnNewCheckWarehouseingInfo(id int64, orgid int64, tx *gorm.DB) error {
  7310. err := tx.Model(&models.DrugWarehouse{}).Where("id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7311. if err != nil {
  7312. tx.Rollback()
  7313. return err
  7314. }
  7315. err = tx.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id = ? and status = 1 and org_id = ?", id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7316. if err != nil {
  7317. tx.Rollback()
  7318. return err
  7319. }
  7320. err = tx.Model(models.XtDrugWarehouseInfoLog{}).Where("warehouse_info_id = ? and status=1", id).Update(map[string]interface{}{"status": 0}).Error
  7321. if err != nil {
  7322. tx.Rollback()
  7323. return err
  7324. }
  7325. return err
  7326. }
  7327. func AddDrugReturnWarehouseStockMaxNumber(count int64, id int64) error {
  7328. tx := XTWriteDB().Begin()
  7329. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7330. if err != nil {
  7331. tx.Rollback()
  7332. return err
  7333. }
  7334. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7335. if err != nil {
  7336. tx.Rollback()
  7337. return err
  7338. }
  7339. tx.Commit()
  7340. return err
  7341. }
  7342. func AddNewDrugReturnWarehouseStockMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7343. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7344. if err != nil {
  7345. tx.Rollback()
  7346. return err
  7347. }
  7348. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7349. if err != nil {
  7350. tx.Rollback()
  7351. return err
  7352. }
  7353. return err
  7354. }
  7355. func AddDrugReturnWarehouseStockMinNumber(count int64, id int64) error {
  7356. tx := XTWriteDB().Begin()
  7357. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7358. if err != nil {
  7359. tx.Rollback()
  7360. return err
  7361. }
  7362. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7363. if err != nil {
  7364. tx.Rollback()
  7365. return err
  7366. }
  7367. tx.Commit()
  7368. return err
  7369. }
  7370. func AddNewDrugReturnWarehouseStockMinNumber(count int64, id int64, tx *gorm.DB) error {
  7371. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", count)).Error
  7372. if err != nil {
  7373. tx.Rollback()
  7374. return err
  7375. }
  7376. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 2}).Error
  7377. if err != nil {
  7378. tx.Rollback()
  7379. return err
  7380. }
  7381. return err
  7382. }
  7383. func UpdateDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo) error {
  7384. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "retail_price": info.RetailPrice, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "drug_code": info.DrugCode, "batch_number": info.BatchNumber}).Error
  7385. return err
  7386. }
  7387. func UpdateNewDrugWarehouseInfoTwenty(id int64, info *models.DrugWarehouseInfo, tx *gorm.DB) error {
  7388. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "number": info.Number, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "warehousing_count": info.WarehousingCount, "price": info.Price, "total_price": info.TotalPrice, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "remark": info.Remark, "retail_price": info.RetailPrice, "max_unit": info.MaxUnit, "min_unit": info.MinUnit, "drug_code": info.DrugCode, "batch_number": info.BatchNumber}).Error
  7389. if err != nil {
  7390. tx.Rollback()
  7391. return err
  7392. }
  7393. return err
  7394. }
  7395. func UpdatedDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64) error {
  7396. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7397. return err
  7398. }
  7399. func UpdatedNewDrugWarehouseInfo(info *models.DrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7400. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7401. if err != nil {
  7402. tx.Rollback()
  7403. return err
  7404. }
  7405. return err
  7406. }
  7407. func UpdatedSelfDrugWarehouseInfo(info *models.XtSelfDrugWarehouseOutInfo, id int64, tx *gorm.DB) error {
  7408. err := tx.Model(&models.XtSelfDrugWarehouseOutInfo{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"drug_id": info.DrugId, "count": info.Count, "count_unit": info.CountUnit, "price": info.Price, "product_date": info.ProductDate, "expiry_date": info.ExpiryDate, "remark": info.Remark, "dealer": info.Dealer, "manufacturer": info.Manufacturer, "retail_price": info.RetailPrice, "retail_total_price": info.RetailTotalPrice, "number": info.Number, "batch_number": info.BatchNumber, "admin_user_id": info.AdminUserId, "last_price": info.LastPrice, "stock_count": info.StockCount, "storehouse_id": info.StorehouseId, "patient_id": info.PatientId, "class_type": info.ClassType}).Error
  7409. if err != gorm.ErrRecordNotFound {
  7410. return err
  7411. }
  7412. return err
  7413. }
  7414. func GetDrugLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  7415. out := models.DrugWarehouseOut{}
  7416. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7417. return out, err
  7418. }
  7419. func GetNewDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7420. out := models.DrugWarehouseOut{}
  7421. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7422. if err != gorm.ErrRecordNotFound {
  7423. if err != nil {
  7424. tx.Rollback()
  7425. return out, err
  7426. }
  7427. }
  7428. return out, err
  7429. }
  7430. func GetSelfDrugLastWarehouseOut(orgid int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7431. out := models.XtSelfDrugWarehouseOut{}
  7432. err := tx.Where("org_id = ? and status = 1", orgid).Find(&out).Error
  7433. if err != gorm.ErrRecordNotFound {
  7434. if err != nil {
  7435. tx.Rollback()
  7436. return out, err
  7437. }
  7438. }
  7439. return out, err
  7440. }
  7441. func GetSelfDrugLastWarehouseOutOne(id int64, tx *gorm.DB) (models.XtSelfDrugWarehouseOut, error) {
  7442. out := models.XtSelfDrugWarehouseOut{}
  7443. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7444. if err != gorm.ErrRecordNotFound {
  7445. if err != nil {
  7446. tx.Rollback()
  7447. return out, err
  7448. }
  7449. }
  7450. return out, err
  7451. }
  7452. func GetDrugWarehouseOutListById(warehouse_out_id int64, orgId int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7453. err = XTReadDB().Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7454. return info, err
  7455. }
  7456. func GetNewDrugWarehouseOutListById(warehouse_out_id int64, orgId int64, tx *gorm.DB) (info []*models.DrugWarehouseOutInfo, err error) {
  7457. err = tx.Where("warehouse_out_id = ? and org_id =? and status = 1", warehouse_out_id, orgId).Preload("BaseDrugLib", "status=1").Find(&info).Error
  7458. if err != gorm.ErrRecordNotFound {
  7459. if err != nil {
  7460. tx.Rollback()
  7461. return info, err
  7462. }
  7463. }
  7464. return info, err
  7465. }
  7466. func UpdateCheckDrugOut(out models.DrugWarehouseOut, id int64) error {
  7467. tx := XTWriteDB().Begin()
  7468. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7469. if err != nil {
  7470. tx.Rollback()
  7471. return err
  7472. }
  7473. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7474. if err != nil {
  7475. tx.Rollback()
  7476. return err
  7477. }
  7478. tx.Commit()
  7479. return err
  7480. }
  7481. func UpdateNewCheckDrugOut(out models.DrugWarehouseOut, id int64, tx *gorm.DB) error {
  7482. err := tx.Model(&models.DrugWarehouseOut{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_check": out.IsCheck}).Error
  7483. if err != nil {
  7484. tx.Rollback()
  7485. return err
  7486. }
  7487. err = tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", id).Update(map[string]interface{}{"is_check": 1}).Error
  7488. if err != nil {
  7489. tx.Rollback()
  7490. return err
  7491. }
  7492. return err
  7493. }
  7494. func UpdateCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64) error {
  7495. err := XTWriteDB().Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7496. return err
  7497. }
  7498. func UpdateNewCheckDrugOutInfoList(info models.DrugWarehouseOutInfo, warehouse_out_id int64, tx *gorm.DB) error {
  7499. err := tx.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id = ? and status = 1", warehouse_out_id).Update(map[string]interface{}{"is_check": info.IsCheck}).Error
  7500. if err != nil {
  7501. tx.Rollback()
  7502. return err
  7503. }
  7504. return err
  7505. }
  7506. func GetDrugWarehouseOutByLastId(id int64) (models.DrugWarehouseOut, error) {
  7507. out := models.DrugWarehouseOut{}
  7508. err := XTReadDB().Where("id = ? and status = 1", id).Find(&out).Error
  7509. return out, err
  7510. }
  7511. func GetNewDrugWarehouseOutByLastId(id int64, tx *gorm.DB) (models.DrugWarehouseOut, error) {
  7512. out := models.DrugWarehouseOut{}
  7513. err := tx.Where("id = ? and status = 1", id).Find(&out).Error
  7514. if err != gorm.ErrRecordNotFound {
  7515. if err != nil {
  7516. tx.Rollback()
  7517. return out, err
  7518. }
  7519. }
  7520. return out, err
  7521. }
  7522. func UpdateDrugMaxNumber(count int64, id int64) error {
  7523. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7524. return err
  7525. }
  7526. func UpdateNewDrugMaxNumber(count int64, id int64, tx *gorm.DB) error {
  7527. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7528. if err != gorm.ErrRecordNotFound {
  7529. if err != nil {
  7530. tx.Rollback()
  7531. return err
  7532. }
  7533. }
  7534. return err
  7535. }
  7536. func UpdateNewDrugMaxNumberOne(count int64, id int64, tx *gorm.DB) error {
  7537. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7538. if err != gorm.ErrRecordNotFound {
  7539. if err != nil {
  7540. tx.Rollback()
  7541. return err
  7542. }
  7543. }
  7544. return err
  7545. }
  7546. func UpdateDrugMinNumber(count int64, id int64) error {
  7547. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7548. return err
  7549. }
  7550. func UpdateNewDrugMinNumber(count int64, id int64, tx *gorm.DB) error {
  7551. err := tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7552. if err != nil {
  7553. tx.Rollback()
  7554. return err
  7555. }
  7556. return err
  7557. }
  7558. func DeleteDrugOutFlow(id int64, orgid int64) error {
  7559. 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
  7560. return err
  7561. }
  7562. func DeleteNewDrugOutFlow(id int64, orgid int64, tx *gorm.DB) error {
  7563. err := tx.Model(models.DrugFlow{}).Where("warehouse_out_detail_id = ? and user_org_id = ? and status = 1", id, orgid).Update(map[string]interface{}{"status": 0}).Error
  7564. if err != nil {
  7565. tx.Rollback()
  7566. return err
  7567. }
  7568. return err
  7569. }
  7570. func GetWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64) (outInfo []*models.WarehouseOutInfo, err error) {
  7571. 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
  7572. return outInfo, err
  7573. }
  7574. func GetNewWarehouseOutByWarehouseInfoId(warehouse_info_id int64, good_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.WarehouseOutInfo, err error) {
  7575. err = tx.Where("warehouse_info_id = ? and good_id = ? and org_id = ? and is_check = 1 and status = 1", warehouse_info_id, good_id, orgid).Find(&outInfo).Error
  7576. if err != gorm.ErrRecordNotFound {
  7577. if err != nil {
  7578. tx.Rollback()
  7579. return outInfo, err
  7580. }
  7581. }
  7582. return outInfo, err
  7583. }
  7584. func GetDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7585. 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
  7586. return outInfo, err
  7587. }
  7588. func GetNewDrugWarehouseOutByWarehouseInfoId(warehouse_info_id int64, drug_id int64, orgid int64, tx *gorm.DB) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  7589. err = tx.Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and is_check = 1 and status = 1", warehouse_info_id, drug_id, orgid).Find(&outInfo).Error
  7590. if err != gorm.ErrRecordNotFound {
  7591. if err != nil {
  7592. tx.Rollback()
  7593. return outInfo, err
  7594. }
  7595. }
  7596. return outInfo, err
  7597. }
  7598. func GetAllStockInList(id int64, orgid int64) (info []*models.VmWarehousingInfoSix, err error) {
  7599. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7600. return info, err
  7601. }
  7602. func GetNewAllStockInList(id int64, orgid int64, tx *gorm.DB) (info []*models.VmWarehousingInfoSix, err error) {
  7603. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("GoodInfo", "status = 1 and org_id =?", orgid).Find(&info).Error
  7604. if err != gorm.ErrRecordNotFound {
  7605. if err != nil {
  7606. tx.Rollback()
  7607. return info, err
  7608. }
  7609. }
  7610. return info, err
  7611. }
  7612. func GetDrugWarehouseInfoList(id int64, orgid int64) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7613. err = XTReadDB().Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7614. return info, err
  7615. }
  7616. func GetNewDrugWarehouseInfoList(id int64, orgid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfoTwenty, err error) {
  7617. err = tx.Where("warehousing_id = ? and org_id = ? and status = 1", id, orgid).Preload("BaseDrugLib", "org_id = ? and status = 1", orgid).Find(&info).Error
  7618. if err != gorm.ErrRecordNotFound {
  7619. if err != nil {
  7620. tx.Rollback()
  7621. return info, err
  7622. }
  7623. }
  7624. return info, err
  7625. }
  7626. func GetStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  7627. 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
  7628. return info, err
  7629. }
  7630. func GetNewStockCountByGoodId(goodid int64, storehouse_id int64, orgId int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) {
  7631. err = tx.Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", goodid, storehouse_id, orgId).Find(&info).Error
  7632. if err != gorm.ErrRecordNotFound {
  7633. if err != nil {
  7634. tx.Rollback()
  7635. return info, err
  7636. }
  7637. }
  7638. return info, err
  7639. }
  7640. func UpdateGoodInfoSumCount(goodid int64, sum_count int64, orgid int64) error {
  7641. 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
  7642. 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
  7643. return err
  7644. }
  7645. func UpdateGoodInfoSumCountSix(goodid int64, sum_count int64, orgid int64) error {
  7646. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7647. return err
  7648. }
  7649. func UpdateGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64) error {
  7650. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7651. return err
  7652. }
  7653. func UpdateNewGoodInfoReduceSumCount(goodid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7654. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7655. if err != gorm.ErrRecordNotFound {
  7656. if err != nil {
  7657. tx.Rollback()
  7658. return err
  7659. }
  7660. }
  7661. return err
  7662. }
  7663. func GetGoodFlowByWarehouseOutId(good_id int64, user_org_id int64, id int64, tx *gorm.DB) (flow []*models.VmStockFlow, err error) {
  7664. err = tx.Where("good_id = ? and user_org_id = ? and warehouse_out_detail_id = ? and status=1", good_id, user_org_id, id).Find(&flow).Error
  7665. if err != gorm.ErrRecordNotFound {
  7666. if err != nil {
  7667. tx.Rollback()
  7668. return flow, err
  7669. }
  7670. }
  7671. return
  7672. }
  7673. func UpdateGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64) error {
  7674. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7675. err = XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7676. return err
  7677. }
  7678. func UpdateNewGoodInfoAddSumCount(goodid int64, sum_count int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7679. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", goodid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7680. if err != nil {
  7681. tx.Rollback()
  7682. return err
  7683. }
  7684. 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
  7685. if err != nil {
  7686. tx.Rollback()
  7687. return err
  7688. }
  7689. return err
  7690. }
  7691. func GetCancelStockInfo(id int64, orgid int64) (stock []*models.CancelStockInfo, err error) {
  7692. 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
  7693. return stock, err
  7694. }
  7695. func ModifyCancelStockInfo(id int64, info *models.CancelStockInfo) error {
  7696. 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
  7697. return err
  7698. }
  7699. func GetCancelStockInfoListById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7700. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7701. return info, err
  7702. }
  7703. func GetWarehouseOutList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.WarehouseOutInfo, err error) {
  7704. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7705. return info, err
  7706. }
  7707. func GetCancelWarehouseList(orgId int64, goodId int64, warehouseInfoId int64) (info []*models.CancelStockInfo, err error) {
  7708. err = XTReadDB().Where("org_id = ? and good_id = ? and warehouse_info_id = ? and status = 1", orgId, goodId, warehouseInfoId).Find(&info).Error
  7709. return info, err
  7710. }
  7711. func CheckCancelStock(stock models.CancelStock, cancel_stock_id int64, orgid int64) error {
  7712. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  7713. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and status = 1 and org_id = ?", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 1}).Error
  7714. return err
  7715. }
  7716. func GetCancelStockInfoById(cancel_stock_id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  7717. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&info).Error
  7718. return info, err
  7719. }
  7720. func UpdateCancelStockNumber(stock_count int64, warehouse_info_id int64, orgid int64) error {
  7721. err := XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and status = 1", warehouse_info_id, orgid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", stock_count)).Error
  7722. return err
  7723. }
  7724. func UpdateGoodSumCount(sum_count int64, good_id int64, orgid int64) error {
  7725. err := XTWriteDB().Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  7726. return err
  7727. }
  7728. func UpdateStockWarehouseInfo(cancel_stock_id int64, orgid int64, stock models.CancelStock) error {
  7729. err := XTWriteDB().Model(&models.CancelStock{}).Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7730. err = XTWriteDB().Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Update(map[string]interface{}{"is_check": 2}).Error
  7731. return err
  7732. }
  7733. func UpdateStockWarehouseInfoStockFlow(id int64, orgid int64, goodid int64) error {
  7734. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("cancel_out_detail_id = ? and user_org_id = ? and status = 1 and good_id =?", id, orgid, goodid).Update(map[string]interface{}{"status": 0}).Error
  7735. return err
  7736. }
  7737. func UpdateStockNumberWarehouseInfo(warehouse_info_id int64, good_id int64, org_id int64, stock_count int64) error {
  7738. 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
  7739. 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
  7740. return err
  7741. }
  7742. func UpdateDrugSumCount(drugid int64, sum_count int64, sum_in_count int64, orgid int64) error {
  7743. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ?", drugid, orgid).UpdateColumn("sum_count", gorm.Expr("sum_count + ?", sum_count)).Error
  7744. 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
  7745. return err
  7746. }
  7747. func GetDrugStockWarehouseInfo(id int64, orgid int64, storehouse_id int64) (info []*models.DrugWarehouseInfo, err error) {
  7748. err = XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1 and storehouse_id = ?", id, orgid, storehouse_id).Find(&info).Error
  7749. return info, err
  7750. }
  7751. func UpdateBaseDrugSum(drugid int64, orgid int64, sum_count int64, sum_in_count int64) error {
  7752. 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
  7753. 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
  7754. return err
  7755. }
  7756. func GetDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64) (info []*models.DrugWarehouseInfo, err error) {
  7757. 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
  7758. return info, err
  7759. }
  7760. func GetSelfDrugAllStockInfoOne(patient_id int64, orgid int64, drugid int64) (info []*models.XtSelfDrugWarehouseInfo, err error) {
  7761. err = XTReadDB().Where("patient_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", patient_id, orgid, drugid).Find(&info).Error
  7762. return info, err
  7763. }
  7764. func GetNewDrugAllStockInfo(storehouse_id int64, orgid int64, drugid int64, tx *gorm.DB) (info []*models.DrugWarehouseInfo, err error) {
  7765. err = tx.Where("storehouse_id = ? and org_id = ? and status = 1 and drug_id = ? and is_check = 1", storehouse_id, orgid, drugid).Find(&info).Error
  7766. if err != gorm.ErrRecordNotFound {
  7767. if err != nil {
  7768. tx.Rollback()
  7769. return info, err
  7770. }
  7771. }
  7772. return info, err
  7773. }
  7774. func UpdateBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64) error {
  7775. err := XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7776. err = XTWriteDB().Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7777. return err
  7778. }
  7779. func UpdateNewBaseDrugSumInfo(sum_count int64, drugid int64, orgid int64, sum_in_count int64, tx *gorm.DB) error {
  7780. err := tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7781. if err != gorm.ErrRecordNotFound {
  7782. if err != nil {
  7783. tx.Rollback()
  7784. return err
  7785. }
  7786. }
  7787. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and status = 1 and org_id = ?", drugid, orgid).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  7788. if err != gorm.ErrRecordNotFound {
  7789. if err != nil {
  7790. tx.Rollback()
  7791. return err
  7792. }
  7793. }
  7794. return err
  7795. }
  7796. func UpdateBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64) error {
  7797. err = writeDb.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7798. return err
  7799. }
  7800. func UpdateNewBaseDrugSumTwo(drugid int64, sum_count int64, orgid int64, tx *gorm.DB) error {
  7801. err = tx.Model(&models.BaseDrugLib{}).Where("id = ? and org_id = ? and status = 1", drugid, orgid).Update(map[string]interface{}{"sum_count": sum_count}).Error
  7802. if err != nil {
  7803. tx.Rollback()
  7804. return err
  7805. }
  7806. return err
  7807. }
  7808. func GetLastCancelStockByList(id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7809. 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
  7810. return info, err
  7811. }
  7812. func GetCancelDrugStockList(cancel_stock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7813. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&info).Error
  7814. return info, err
  7815. }
  7816. func ModifyCancelStock(cancel_stock_id int64, stock models.DrugCancelStock, orgid int64) error {
  7817. 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
  7818. 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
  7819. return err
  7820. }
  7821. func GetCancelDrugStock(cancel_stock_id int64, orgid int64) (models.DrugCancelStock, error) {
  7822. stock := models.DrugCancelStock{}
  7823. err := XTReadDB().Where("id = ? and org_id = ? and status = 1", cancel_stock_id, orgid).Find(&stock).Error
  7824. return stock, err
  7825. }
  7826. func GetDrugCancelCheckById(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7827. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 and is_check = 1", cancel_stock_id, orgid).Find(&stock).Error
  7828. return stock, err
  7829. }
  7830. func GetDrugCancelCheckByIdTwenty(cancel_stock_id int64, orgid int64) (stock []*models.DrugCancelStockInfo, err error) {
  7831. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1 ", cancel_stock_id, orgid).Find(&stock).Error
  7832. return stock, err
  7833. }
  7834. func GetDrugWarehouseOutList(batch_number_id int64, drugId int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  7835. err = XTReadDB().Where("warehouse_info_id = ? and drug_id = ? and org_id = ? and status = 1", batch_number_id, drugId, orgid).Find(&info).Error
  7836. return info, err
  7837. }
  7838. func ModifyDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64) error {
  7839. 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
  7840. return err
  7841. }
  7842. func ModifyNewDrugWarehouseInfoStockMaxNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7843. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  7844. if err != nil {
  7845. tx.Rollback()
  7846. return err
  7847. }
  7848. return err
  7849. }
  7850. func ModifyDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64) error {
  7851. 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
  7852. return err
  7853. }
  7854. func ModifyNewDrugWarehouseInfoStockMinNumber(count int64, drugid int64, orgid int64, id int64, tx *gorm.DB) error {
  7855. err = tx.Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and drug_id = ? and status = 1", id, orgid, drugid).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  7856. if err != nil {
  7857. tx.Rollback()
  7858. return err
  7859. }
  7860. return err
  7861. }
  7862. func ModifyDrugCancelStockInfo(id int64, info *models.DrugCancelStockInfo) error {
  7863. 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
  7864. return err
  7865. }
  7866. func ReturnDrugCancelStock(cancelstock_id int64, stock models.DrugCancelStock) error {
  7867. err := XTWriteDB().Model(&models.DrugCancelStock{}).Where("id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7868. err = XTWriteDB().Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id = ? and status = 1", cancelstock_id).Update(map[string]interface{}{"is_check": 2}).Error
  7869. return err
  7870. }
  7871. func GetDrugCancelStockInfo(cancelstock_id int64, orgid int64) (info []*models.DrugCancelStockInfo, err error) {
  7872. err = XTReadDB().Where("cancel_stock_id = ? and org_id = ? and status = 1", cancelstock_id, orgid).Find(&info).Error
  7873. return info, err
  7874. }
  7875. func ModifyDrugFlowByCancelId(cancelstock_id int64, drugId int64, orgid int64) error {
  7876. 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
  7877. return err
  7878. }
  7879. func ModifyDrugMaxNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7880. err := XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and org_id = ? and status = 1", id, orgid).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", count)).Error
  7881. return err
  7882. }
  7883. func ModifyDrugMinNumberWarehouseInfo(id int64, count int64, orgid int64) error {
  7884. 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
  7885. return err
  7886. }
  7887. func GetDrugInfoSix(id int64, orgid int64) (models.DrugWarehouseInfo, error) {
  7888. info := models.DrugWarehouseInfo{}
  7889. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7890. return info, err
  7891. }
  7892. func GetSelfDrugInfoSix(id int64, orgid int64) (models.XtSelfDrugWarehouseInfo, error) {
  7893. info := models.XtSelfDrugWarehouseInfo{}
  7894. err := XTReadDB().Where("id = ? and org_id = ? and status = 1 and is_check = 1", id, orgid).Find(&info).Error
  7895. return info, err
  7896. }
  7897. func GetPrintList(storehouse_id int64, good_name string, orgid int64, limit int64, page int64) (storeinventory []*models.XtStockInventory, err error) {
  7898. db := XTReadDB().Model(&storeinventory).Where("status = 1")
  7899. if storehouse_id > 0 {
  7900. db = db.Where("storehouse_id = ?", storehouse_id)
  7901. }
  7902. if orgid > 0 {
  7903. db = db.Where("user_org_id = ?", orgid)
  7904. }
  7905. err = XTReadDB().Preload("GoodInfo", "status = 1 and org_id = ?", orgid).Preload("WarehousingInfo", "status = 1 and org_id=?", orgid).Find(&storeinventory).Error
  7906. return storeinventory, err
  7907. }
  7908. func GetGoodDialysisOutInfo(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.AutomaticReduceDetail, error) {
  7909. detail := models.AutomaticReduceDetail{}
  7910. 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
  7911. return detail, err
  7912. }
  7913. func GetGoodDialysisOutInfoSix(orgid int64, patient_id int64, sys_record_time int64, good_id int64) (models.WarehouseOutInfo, error) {
  7914. detail := models.WarehouseOutInfo{}
  7915. 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
  7916. return detail, err
  7917. }
  7918. func GetGoodSumCount(storehouse_id int64, good_id int64, orgid int64) (models.XtGoodStockCount, error) {
  7919. stock := models.XtGoodStockCount{}
  7920. err := XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and status = 1", storehouse_id, good_id, orgid).Find(&stock).Error
  7921. return stock, err
  7922. }
  7923. func CreateGoodStockCount(stock models.XtGoodStockCount) error {
  7924. err := XTWriteDB().Create(&stock).Error
  7925. return err
  7926. }
  7927. func AddGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7928. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error
  7929. return err
  7930. }
  7931. func ReduceGoodSumInCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  7932. ut := XTWriteDB().Begin()
  7933. 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
  7934. if err != nil {
  7935. ut.Rollback()
  7936. return err
  7937. }
  7938. ut.Commit()
  7939. return err
  7940. }
  7941. func UpdateGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64) error {
  7942. err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7943. return err
  7944. }
  7945. func UpdateNewGoodFlushCount(storehouse_id int64, good_id int64, user_org_id int64, flush_count int64, tx *gorm.DB) error {
  7946. err := tx.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error
  7947. if err != nil {
  7948. tx.Rollback()
  7949. return err
  7950. }
  7951. return err
  7952. }
  7953. func CreateDrugStock(stock models.XtDrugStockCount) error {
  7954. err := XTWriteDB().Create(&stock).Error
  7955. return err
  7956. }
  7957. func UpdateDrugCount(storehouse_id int64, user_org_id int64, drug_id int64, sum_in_count int64, flush_count int64) error {
  7958. ut := XTWriteDB().Begin()
  7959. 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
  7960. if err != nil {
  7961. ut.Rollback()
  7962. return err
  7963. }
  7964. 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
  7965. if err != nil {
  7966. ut.Rollback()
  7967. return err
  7968. }
  7969. ut.Commit()
  7970. return err
  7971. }
  7972. func UpdateDrugCountOne(storehouse_id int64, user_org_id int64, drug_id int64, flush_count int64) error {
  7973. ut := XTWriteDB().Begin()
  7974. 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
  7975. if err != nil {
  7976. ut.Rollback()
  7977. return err
  7978. }
  7979. ut.Commit()
  7980. return err
  7981. }
  7982. func AddDrugSumOutCount(storehouse_id int64, sum_out_count int64, user_org_id int64, drug_id int64) error {
  7983. ut := XTWriteDB().Begin()
  7984. 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
  7985. if err != nil {
  7986. ut.Rollback()
  7987. return err
  7988. }
  7989. 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
  7990. if err != nil {
  7991. ut.Rollback()
  7992. return err
  7993. }
  7994. ut.Commit()
  7995. return err
  7996. }
  7997. func ReduceDrugSumOut(storehouse_id int64, user_org_id int64, drug_id int64, sum_out_count int64) error {
  7998. ut := XTWriteDB().Begin()
  7999. 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
  8000. if err != nil {
  8001. ut.Rollback()
  8002. return err
  8003. }
  8004. ut.Commit()
  8005. return err
  8006. }
  8007. func AddGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  8008. ut := XTWriteDB().Begin()
  8009. 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
  8010. if err != nil {
  8011. ut.Rollback()
  8012. return err
  8013. }
  8014. 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
  8015. if err != nil {
  8016. ut.Rollback()
  8017. return err
  8018. }
  8019. ut.Commit()
  8020. return err
  8021. }
  8022. func ReduceGoodSumOutCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error {
  8023. ut := XTWriteDB().Begin()
  8024. 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
  8025. if err != nil {
  8026. ut.Rollback()
  8027. return err
  8028. }
  8029. 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
  8030. if err != nil {
  8031. ut.Rollback()
  8032. return err
  8033. }
  8034. ut.Commit()
  8035. return err
  8036. }
  8037. func GetInvetoryWarehouseInfoList(orgid int64, storehouse_id int64) (info []*models.WarehousingInfo, err error) {
  8038. if orgid == 10191 || orgid == 10138 || orgid == 10278 {
  8039. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id=? and is_check= 1 and org_id = ?", storehouse_id, orgid).Preload("GoodInfo", "status = 1").Order("good_id").Find(&info).Error
  8040. return info, err
  8041. } else {
  8042. err = XTReadDB().Model(&info).Where("status = 1 and storehouse_id=? and is_check= 1 and org_id = ? and stock_count >0", storehouse_id, orgid).Preload("GoodInfo", "status = 1").Order("good_id").Find(&info).Error
  8043. return info, err
  8044. }
  8045. }
  8046. func GetSettleOpenConfig(user_org_id int64) (*models.HisSettleStockConfig, error) {
  8047. config := models.HisSettleStockConfig{}
  8048. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8049. if err == gorm.ErrRecordNotFound {
  8050. return nil, err
  8051. }
  8052. if err != nil {
  8053. return nil, err
  8054. }
  8055. return &config, nil
  8056. }
  8057. func CreateSettleOpenConfig(config models.HisSettleStockConfig) error {
  8058. err := XTWriteDB().Create(&config).Error
  8059. return err
  8060. }
  8061. func UpdateSettleOpenConfig(id int64, config models.HisSettleStockConfig) error {
  8062. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"is_open": config.IsOpen}).Error
  8063. return err
  8064. }
  8065. func UpdateDrugStockAutomaticReduceRecordOne(orgid int64, is_open int64) (models.DrugStockConfig, error) {
  8066. config := models.DrugStockConfig{}
  8067. err := XTWriteDB().Model(&config).Where("org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  8068. return config, err
  8069. }
  8070. func UpdatePharyConfig(orgid int64, is_open int64) (models.PharmacyConfig, error) {
  8071. stockConfig := models.PharmacyConfig{}
  8072. err := XTWriteDB().Model(&stockConfig).Where("user_org_id = ? and status = 1", orgid).Update(map[string]interface{}{"is_open": is_open}).Error
  8073. return stockConfig, err
  8074. }
  8075. func UpdateSettleOpenConfigOne(id int64, is_open int64) error {
  8076. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  8077. return err
  8078. }
  8079. func UpdateSettleOpenConfigTwo(id int64, is_open int64) error {
  8080. err := XTWriteDB().Model(&models.HisSettleStockConfig{}).Where("user_org_id = ? and status = 1", id).Update(map[string]interface{}{"is_open": is_open}).Error
  8081. return err
  8082. }
  8083. func GetSettleOpenConfigByOrgId(user_org_id int64) (models.HisSettleStockConfig, error) {
  8084. config := models.HisSettleStockConfig{}
  8085. err = XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8086. return config, nil
  8087. }
  8088. func GetGoodWareHouseOutInfoByGood(good_id int64, sys_record_time int64, user_org_id int64) (info []*models.WarehouseOutInfo, err error) {
  8089. err = XTReadDB().Where("good_id= ? and sys_record_time = ? and org_id = ? and status=1", good_id, sys_record_time, user_org_id).Find(&info).Error
  8090. return info, err
  8091. }
  8092. func GetGoodInformationByGoodIdOne(good_id int64) (models.GoodInformationFourty, error) {
  8093. info := models.GoodInformationFourty{}
  8094. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  8095. return info, err
  8096. }
  8097. func GetGoodWarehouseInfoByGoodList(good_id int64) (info []*models.WarehousingInfo, err error) {
  8098. err = XTReadDB().Where("good_id = ? and status =1 and stock_count>0 and is_check = 1", good_id).Find(&info).Error
  8099. return info, err
  8100. }
  8101. func GetAllWarehouseInfo(good_id int64, user_org_id int64) (info []*models.WarehousingInfo, err error) {
  8102. err = XTReadDB().Where("good_id = ? and user_org_id =? and status=1 and is_check =1", good_id, user_org_id).Find(&info).Error
  8103. return info, err
  8104. }
  8105. func GetHisPrescriptionByGoodId(good_id int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  8106. err = XTReadDB().Where("project_id = ? and user_org_id =? and status=1 and type =3", good_id, user_org_id).Find(&project).Error
  8107. return project, err
  8108. }
  8109. func GetHisDocById(id int64, orgid int64) (models.HisDoctorAdvice, error) {
  8110. advice := models.HisDoctorAdvice{}
  8111. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8112. return advice, err
  8113. }
  8114. func GetBloodDocById(id int64, orgid int64) (models.DoctorAdvice, error) {
  8115. advice := models.DoctorAdvice{}
  8116. err := XTReadDB().Where("id = ? and user_org_id= ? and status = 1", id, orgid).Find(&advice).Error
  8117. return advice, err
  8118. }
  8119. func FindAllSettleWarehouseOut(org_id int64, warehouse_out_time int64, is_sys int64) (models.WarehouseOut, error) {
  8120. out := models.WarehouseOut{}
  8121. 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
  8122. return out, err
  8123. }
  8124. func GetGoodOutOpenConfig(user_org_id int64) (*models.XtGoodOutConfig, error) {
  8125. config := models.XtGoodOutConfig{}
  8126. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8127. if err == gorm.ErrRecordNotFound {
  8128. return nil, err
  8129. }
  8130. if err != nil {
  8131. return nil, err
  8132. }
  8133. return &config, nil
  8134. }
  8135. func CreateGoodOutOpenConfig(good models.XtGoodOutConfig) error {
  8136. err := XTWriteDB().Create(&good).Error
  8137. return err
  8138. }
  8139. func UpdateGoodOutOpenConfig(id int64, good models.XtGoodOutConfig) error {
  8140. err := XTWriteDB().Model(&good).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"is_open": good.IsOpen}).Error
  8141. return err
  8142. }
  8143. func FindGoodOutConfigById(user_org_id int64) (models.XtGoodOutConfig, error) {
  8144. config := models.XtGoodOutConfig{}
  8145. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8146. return config, err
  8147. }
  8148. func UpdateGoodAutoConfig(user_org_id int64) error {
  8149. gobalConfig := models.GobalConfig{}
  8150. err := XTWriteDB().Model(&gobalConfig).Where("org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8151. return err
  8152. }
  8153. func UpdateGoodOutConfig(user_org_id int64) error {
  8154. config := models.XtGoodOutConfig{}
  8155. err := XTWriteDB().Model(&config).Where("user_org_id = ? and status = 1", user_org_id).Updates(map[string]interface{}{"is_open": 2}).Error
  8156. return err
  8157. }
  8158. func GetHisGoodList(patient_id int64, record_date int64, orgid int64) (info []*models.GoodHisPrescriptionProject, err error) {
  8159. 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
  8160. return info, err
  8161. }
  8162. func GetLastGoodWarehouseOutInfoByProjectId(good_id int64, patient_id int64, sys_record_time int64, project_id int64) (good []*models.WarehouseOutInfo, err error) {
  8163. 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
  8164. return good, err
  8165. }
  8166. func ModifyGoodWarehouseInfo(good_id int64, warehouse_info_id int64, orgid int64, count int64) error {
  8167. 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
  8168. return err
  8169. }
  8170. func DeleteGoodWarehouseOutInfo(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8171. 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
  8172. return err
  8173. }
  8174. func DeleteGoodFlow(good_id int64, sys_record_time int64, org_id int64, project_id int64) error {
  8175. 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
  8176. return err
  8177. }
  8178. func FindPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8179. out := models.WarehouseOut{}
  8180. 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
  8181. return out, err
  8182. }
  8183. func FindNewPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8184. out := models.WarehouseOut{}
  8185. err = tx.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Find(&out).Error
  8186. if err != gorm.ErrRecordNotFound {
  8187. if err != nil {
  8188. tx.Rollback()
  8189. return out, err
  8190. }
  8191. }
  8192. return out, err
  8193. }
  8194. func FindLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64) (models.WarehouseOut, error) {
  8195. out := models.WarehouseOut{}
  8196. 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
  8197. return out, err
  8198. }
  8199. func FindNewLastPrescriptionWarehouseOut(org_id int64, warehouse_out_time int64, tx *gorm.DB) (models.WarehouseOut, error) {
  8200. out := models.WarehouseOut{}
  8201. err = tx.Model(&models.WarehouseOut{}).Where("org_id = ? and status = 1 and warehouse_out_time = ? and is_sys=1", org_id, warehouse_out_time).Last(&out).Error
  8202. if err != gorm.ErrRecordNotFound {
  8203. if err != nil {
  8204. tx.Rollback()
  8205. return out, err
  8206. }
  8207. }
  8208. return out, err
  8209. }
  8210. func GetGoodTypeObj(goodid int64, org_id int64) (models.GoodsType, error) {
  8211. goodsType := models.GoodsType{}
  8212. err := XTReadDB().Where("good_id = ? and org_id = ? and status = 1", goodid, org_id).Find(&goodsType).Error
  8213. return goodsType, err
  8214. }
  8215. func GetAllGoodTypeName(good_type_id int64, org_id int64) (good_type_name string) {
  8216. goodsTypeList, _ := GetAllGoodType(org_id)
  8217. for _, item := range goodsTypeList {
  8218. if good_type_id == item.ID {
  8219. good_type_name = item.TypeName
  8220. }
  8221. }
  8222. return good_type_name
  8223. }
  8224. func UpdateGoodTypeName(good_id int64, good_type_name string) (models.GoodInformation, error) {
  8225. information := models.GoodInformation{}
  8226. err := XTWriteDB().Model(&information).Where("id = ? and status =1", good_id).Updates(map[string]interface{}{"good_type_name": good_type_name}).Error
  8227. return information, err
  8228. }
  8229. func GetSencondeGoodWarehouseInfo(good_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  8230. err = XTReadDB().Where("good_id = ? and org_id= ? and status= 1 and is_check = 1", good_id, org_id).Find(&info).Error
  8231. return info, err
  8232. }
  8233. func UpdateGoodSumCountSeven(sum_count int64, good_id int64, orgid int64) error {
  8234. ut := writeDb.Begin()
  8235. 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
  8236. if err != nil {
  8237. ut.Rollback()
  8238. return err
  8239. }
  8240. ut.Commit()
  8241. return err
  8242. }
  8243. func UpdateNewGoodSumCountSeven(sum_count int64, good_id int64, orgid int64, tx *gorm.DB) error {
  8244. err := tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", good_id, orgid).Updates(map[string]interface{}{"sum_count": sum_count}).Error
  8245. if err != nil {
  8246. tx.Rollback()
  8247. return err
  8248. }
  8249. return err
  8250. }
  8251. func GetWarehouseOutInfoIsExistOTwo(good_id int64, patient_id int64, record_time int64, project_id int64) (*models.WarehouseOutInfo, error) {
  8252. info := models.WarehouseOutInfo{}
  8253. var err error
  8254. 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
  8255. if err == gorm.ErrRecordNotFound {
  8256. return nil, err
  8257. }
  8258. if err != nil {
  8259. return nil, err
  8260. }
  8261. return &info, nil
  8262. }
  8263. func GetWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64) (models.WarehouseOutInfo, error) {
  8264. info := models.WarehouseOutInfo{}
  8265. 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
  8266. return info, err
  8267. }
  8268. func GetNewWarehouseOutInfoIsExistThree(good_id int64, patient_id int64, record_time int64, project_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8269. info := models.WarehouseOutInfo{}
  8270. err := tx.Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ?", good_id, record_time, patient_id, project_id).Find(&info).Error
  8271. if err != gorm.ErrRecordNotFound {
  8272. if err != nil {
  8273. tx.Rollback()
  8274. return info, err
  8275. }
  8276. }
  8277. return info, err
  8278. }
  8279. func GetWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (models.WarehouseOutInfo, error) {
  8280. info := models.WarehouseOutInfo{}
  8281. 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
  8282. return info, err
  8283. }
  8284. func GetNewWarehouseOutInfoIsExistTen(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (models.WarehouseOutInfo, error) {
  8285. info := models.WarehouseOutInfo{}
  8286. err := tx.Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  8287. if err != gorm.ErrRecordNotFound {
  8288. if err != nil {
  8289. tx.Rollback()
  8290. return info, err
  8291. }
  8292. }
  8293. return info, err
  8294. }
  8295. func GetNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64) (*models.WarehouseOutInfo, error) {
  8296. info := models.WarehouseOutInfo{}
  8297. var err error
  8298. 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
  8299. if err == gorm.ErrRecordNotFound {
  8300. return nil, err
  8301. }
  8302. if err != nil {
  8303. return nil, err
  8304. }
  8305. return &info, nil
  8306. }
  8307. func GetNewNewWarehouseOutInfoIsExistSix(good_id int64, patient_id int64, record_time int64, project_id int64, warehouse_info_id int64, tx *gorm.DB) (*models.WarehouseOutInfo, error) {
  8308. info := models.WarehouseOutInfo{}
  8309. var err error
  8310. err = tx.Model(&info).Where("good_id = ? and sys_record_time = ? and status = 1 and patient_id = ? and project_id = ? and warehouse_info_id = ?", good_id, record_time, patient_id, project_id, warehouse_info_id).Find(&info).Error
  8311. if err != gorm.ErrRecordNotFound {
  8312. if err != nil {
  8313. tx.Rollback()
  8314. return &info, err
  8315. }
  8316. }
  8317. return &info, err
  8318. }
  8319. func GetNewGoodWarehouseInfo(startime int64, endtime int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.GoodWarehousingInfo, err error) {
  8320. db := XTReadDB().Model(&info).Where("status = 1")
  8321. if startime > 0 {
  8322. db = db.Where("ctime>=?", startime)
  8323. }
  8324. if endtime > 0 {
  8325. db = db.Where("ctime<=?", endtime)
  8326. }
  8327. if orgid > 0 {
  8328. db = db.Where("org_id = ?", orgid)
  8329. }
  8330. if len(good_id) > 0 {
  8331. db = db.Where("good_id in(?)", good_id)
  8332. }
  8333. if storehouse_id > 0 {
  8334. db = db.Where("storehouse_id = ?", storehouse_id)
  8335. }
  8336. err = db.Preload("GoodInfo", "org_id = ? and status= 1", orgid).Find(&info).Error
  8337. return info, err
  8338. }
  8339. func GetNewWarehouseOutInfoList(start_time int64, end_time int64, orgid int64, good_id []int64, storehouse_id int64) (info []*models.WarehouseOutInfoNight, err error) {
  8340. db := XTReadDB().Model(&info).Where("status= 1")
  8341. if start_time > 0 {
  8342. db = db.Where("ctime>=?", start_time)
  8343. }
  8344. if end_time > 0 {
  8345. db = db.Where("ctime <=?", end_time)
  8346. }
  8347. if orgid > 0 {
  8348. db = db.Where("org_id = ?", orgid)
  8349. }
  8350. if len(good_id) > 0 {
  8351. db = db.Where("good_id in(?)", good_id)
  8352. }
  8353. if storehouse_id > 0 {
  8354. db = db.Where("storehouse_id = ?", storehouse_id)
  8355. }
  8356. err = db.Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&info).Error
  8357. return info, err
  8358. }
  8359. func GetGoodOutOpenConfigOne(user_org_id int64) (models.XtGoodOutConfig, error) {
  8360. config := models.XtGoodOutConfig{}
  8361. err := XTReadDB().Where("user_org_id = ? and status = 1", user_org_id).Find(&config).Error
  8362. return config, err
  8363. }
  8364. func UpdateDeleteAutoGood(good_id int64, record_date int64, patient_id int64) error {
  8365. 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
  8366. 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
  8367. return err
  8368. }
  8369. func UpdateWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64) error {
  8370. tx := XTWriteDB().Begin()
  8371. for _, item := range list {
  8372. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8373. if err != nil {
  8374. tx.Rollback()
  8375. return err
  8376. }
  8377. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8378. if err != nil {
  8379. tx.Rollback()
  8380. return err
  8381. }
  8382. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8383. if err != nil {
  8384. tx.Rollback()
  8385. return err
  8386. }
  8387. var goodList []*models.WarehousingInfo
  8388. err = tx.Where("org_id = ? and storehouse_id = ? and good_id = ? and status =1 and is_check = 1", orgId, item.StorehouseId, item.GoodId).Find(&goodList).Error
  8389. var flush_count int64
  8390. for _, it := range goodList {
  8391. flush_count += it.StockCount
  8392. }
  8393. err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", item.OrgId, item.GoodId, item.StorehouseId).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", item.WarehousingCount)).Error
  8394. if err != nil {
  8395. tx.Rollback()
  8396. return err
  8397. }
  8398. err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", item.OrgId, item.GoodId, item.StorehouseId).Update(map[string]interface{}{"flush_count": flush_count}).Error
  8399. if err != nil {
  8400. tx.Rollback()
  8401. return err
  8402. }
  8403. storeHouseConfig := models.XtStorehouseConfig{}
  8404. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8405. var stockList []*models.WarehousingInfo
  8406. //查询该耗材剩余多少库存
  8407. err = tx.Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", item.GoodId, storeHouseConfig.StorehouseOutInfo, orgId).Find(&stockList).Error
  8408. var sum_count int64
  8409. var sum_in_count int64
  8410. for _, it := range stockList {
  8411. sum_count += it.StockCount
  8412. sum_in_count += it.WarehousingCount
  8413. }
  8414. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", item.GoodId, orgId).Update(map[string]interface{}{"sum_count": sum_count}).Error
  8415. if err != nil {
  8416. tx.Rollback()
  8417. return err
  8418. }
  8419. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", item.GoodId, orgId).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  8420. if err != nil {
  8421. tx.Rollback()
  8422. return err
  8423. }
  8424. }
  8425. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8426. if err != nil {
  8427. tx.Rollback()
  8428. return err
  8429. }
  8430. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8431. tx.Commit()
  8432. return err
  8433. }
  8434. func UpdateNewWarehouseInfoMap(list []*models.WarehousingInfo, warehousing_info_id int64, orgId int64, tx *gorm.DB) error {
  8435. for _, item := range list {
  8436. err := tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", item.WarehousingCount)).Error
  8437. if err != nil {
  8438. tx.Rollback()
  8439. return err
  8440. }
  8441. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", item.ID).Update(map[string]interface{}{"is_check": 2}).Error
  8442. if err != nil {
  8443. tx.Rollback()
  8444. return err
  8445. }
  8446. err = tx.Model(models.VmStockFlow{}).Where("warehousing_detail_id = ?", item.ID).Update(map[string]interface{}{"status": 0, "is_check": 2}).Error
  8447. if err != nil {
  8448. tx.Rollback()
  8449. return err
  8450. }
  8451. var goodList []*models.WarehousingInfo
  8452. err = tx.Where("org_id = ? and storehouse_id = ? and good_id = ? and status =1 and is_check = 1", orgId, item.StorehouseId, item.GoodId).Find(&goodList).Error
  8453. var flush_count int64
  8454. for _, it := range goodList {
  8455. flush_count += it.StockCount
  8456. }
  8457. err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", item.OrgId, item.GoodId, item.StorehouseId).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", item.WarehousingCount)).Error
  8458. if err != nil {
  8459. tx.Rollback()
  8460. return err
  8461. }
  8462. err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", item.OrgId, item.GoodId, item.StorehouseId).Update(map[string]interface{}{"flush_count": flush_count}).Error
  8463. if err != nil {
  8464. tx.Rollback()
  8465. return err
  8466. }
  8467. storeHouseConfig := models.XtStorehouseConfig{}
  8468. err = tx.Where("user_org_id = ? and status = 1", orgId).First(&storeHouseConfig).Error
  8469. var stockList []*models.WarehousingInfo
  8470. //查询该耗材剩余多少库存
  8471. err = tx.Where("good_id = ? and storehouse_id = ? and org_id = ? and status = 1 and is_check = 1", item.GoodId, storeHouseConfig.StorehouseOutInfo, orgId).Find(&stockList).Error
  8472. var sum_count int64
  8473. var sum_in_count int64
  8474. for _, it := range stockList {
  8475. sum_count += it.StockCount
  8476. sum_in_count += it.WarehousingCount
  8477. }
  8478. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", item.GoodId, orgId).Update(map[string]interface{}{"sum_count": sum_count}).Error
  8479. if err != nil {
  8480. tx.Rollback()
  8481. return err
  8482. }
  8483. err = tx.Model(&models.GoodInfo{}).Where("id = ? and org_id = ? and status = 1", item.GoodId, orgId).Update(map[string]interface{}{"sum_in_count": sum_in_count}).Error
  8484. if err != nil {
  8485. tx.Rollback()
  8486. return err
  8487. }
  8488. err = tx.Model(models.XtGoodWarehouseInfoLog{}).Where("warehouse_info_id = ? and status=1", item.ID).Update(map[string]interface{}{"status": 0}).Error
  8489. if err != nil {
  8490. tx.Rollback()
  8491. return err
  8492. }
  8493. }
  8494. err := tx.Model(&models.Warehousing{}).Where("id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8495. if err != nil {
  8496. tx.Rollback()
  8497. return err
  8498. }
  8499. err = tx.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? and status = 1", warehousing_info_id).Update(map[string]interface{}{"is_check": 2}).Error
  8500. return err
  8501. }
  8502. func FindFirstWarehousingInfoByStockThree(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8503. if storehouse_id == 0 {
  8504. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime").First(&info).Error
  8505. }
  8506. if storehouse_id > 0 {
  8507. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and stock_count > 0 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").First(&info).Error
  8508. }
  8509. return info, err
  8510. }
  8511. func FindFirstWarehousingInfoByStockFour(good_id int64, storehouse_id int64) (info models.WarehousingInfo, err error) {
  8512. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1 and storehouse_id =? ", good_id, storehouse_id).Order("ctime").Last(&info).Error
  8513. return info, err
  8514. }
  8515. func GetHisPrescriptionNumberList(project_id int64, record_date int64, orgid int64, patient_id int64) (list []*models.HisPrescriptionProject, err error) {
  8516. err = readDb.Where("project_id = ? and record_date =? and user_org_id =? and status =1 and patient_id = ?", project_id, record_date, orgid, patient_id).Find(&list).Error
  8517. return list, err
  8518. }
  8519. func GetWarehouseOutnfoByProjectId(project_id int64, record_date int64, orgid int64, patient_id int64) (infor []*models.WarehouseOutInfo, err error) {
  8520. err = readDb.Where("good_id =? and sys_record_time = ? and org_id = ? and patient_id = ? and status=1", project_id, record_date, orgid, patient_id).Find(&infor).Error
  8521. return infor, err
  8522. }
  8523. func UpdateWarehouseInfoByPatientId(project_id int64, record_date int64, orgid int64, patient_id int64) (models.WarehouseOutInfo, error) {
  8524. info := models.WarehouseOutInfo{}
  8525. err := XTWriteDB().Model(&info).Where("good_id = ? and sys_record_time = ? and org_id = ? and status =1 and patient_id = ?", project_id, record_date, orgid, patient_id).Updates(map[string]interface{}{"status": 0}).Error
  8526. return info, err
  8527. }
  8528. func UpdateWarehouseInfoByProject(good_id int64, id int64, user_org_id int64, count int64) (models.WarehousingInfo, error) {
  8529. info := models.WarehousingInfo{}
  8530. err := XTWriteDB().Where("id = ? and good_id = ? and org_id = ? and status= 1", id, good_id, user_org_id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  8531. return info, err
  8532. }
  8533. func GetHisPrescripionProjectList(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8534. err = XTReadDB().Where("project_id=? and status=1 and type =3 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8535. return list, err
  8536. }
  8537. func GetHisPrescripionProjectListOne(good_id int64, user_org_id int64) (list []*models.HisPrescriptionProjectTemplate, err error) {
  8538. err = XTReadDB().Where("project_id=? and status=1 and type =2 and user_org_id=?", good_id, user_org_id).Find(&list).Error
  8539. return list, err
  8540. }
  8541. func UpdateHisPrescriptionProjectList(id int64, price float64) (models.HisPrescriptionProjectTemplateL, error) {
  8542. templateL := models.HisPrescriptionProjectTemplateL{}
  8543. err := XTWriteDB().Model(&templateL).Where("id =? and status=1", id).Updates(map[string]interface{}{"price": price}).Error
  8544. return templateL, err
  8545. }
  8546. func ModifyNewGoodWarehouseInfoStockMinNumber(count int64, goodid int64, orgid int64, id int64, tx *gorm.DB) error {
  8547. err = tx.Model(&models.WarehousingInfo{}).Where("id = ? and org_id = ? and good_id = ? and status = 1", id, orgid, goodid).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  8548. if err != nil {
  8549. tx.Rollback()
  8550. return err
  8551. }
  8552. return err
  8553. }
  8554. func GetDrugInvetoryByDrugId(user_org_id int64, drug_id int64) (info []*models.XtDrugWarehouseInfo, err error) {
  8555. err = XTReadDB().Where("org_id = ? and drug_id = ? and status=1", user_org_id, drug_id).Preload("XtBaseDrug", "org_id = ? and status=1", user_org_id).Order("id desc").Find(&info).Error
  8556. return info, err
  8557. }
  8558. func UpdateNewDrugWarehouseInfoById(id int64, last_stock_max_number int64, last_stock_min_number int64) error {
  8559. err := XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"stock_max_number": last_stock_max_number, "stock_min_number": last_stock_min_number}).Error
  8560. return err
  8561. }
  8562. func GeteSelfPrescriptionOpen(user_org_id int64) (models.XtSelfDrugInfoConfig, error) {
  8563. drugInfoConfig := models.XtSelfDrugInfoConfig{}
  8564. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugInfoConfig).Error
  8565. return drugInfoConfig, err
  8566. }
  8567. func CreaterSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8568. err := XTWriteDB().Create(&infoConfig).Error
  8569. return err
  8570. }
  8571. func SaveSelfConfig(infoConfig models.XtSelfDrugInfoConfig) error {
  8572. err := XTWriteDB().Save(&infoConfig).Error
  8573. return err
  8574. }
  8575. func GetSelfPrescriptionOutOpen(user_org_id int64) (models.XtSelfDrugOutConfig, error) {
  8576. drugOutConfig := models.XtSelfDrugOutConfig{}
  8577. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugOutConfig).Error
  8578. return drugOutConfig, err
  8579. }
  8580. func CreateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8581. err := XTWriteDB().Create(&config).Error
  8582. return err
  8583. }
  8584. func UpdateSelfDrugOutConfig(config models.XtSelfDrugOutConfig) error {
  8585. err := XTWriteDB().Save(&config).Error
  8586. return err
  8587. }
  8588. func CreateSelfDrugFlowOne(flow models.XtSelfDrugFlow, tx *gorm.DB) error {
  8589. err := tx.Create(&flow).Error
  8590. if err != nil {
  8591. tx.Rollback()
  8592. return err
  8593. }
  8594. return err
  8595. }
  8596. func UpdateWarehouseOutInfoByPrice(good_id int64, packing_price float64) error {
  8597. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("good_id =? and status =1 and is_check=1", good_id).Updates(map[string]interface{}{"packing_price": packing_price}).Error
  8598. return err
  8599. }
  8600. func GetWarehosueOutInfoByPrice(good_id int64, org_id int64) (out []*models.WarehouseOutInfo, err error) {
  8601. err = XTReadDB().Where("good_id = ? and org_id = ? and status=1 and price =0", good_id, org_id).Find(&out).Error
  8602. return out, err
  8603. }
  8604. func UpdateWarehouseOut(good_id int64, packing_price float64) error {
  8605. err := XTWriteDB().Model(&models.WarehouseOutInfo{}).Where("id = ? and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  8606. return err
  8607. }
  8608. func UpdateStockFlowByList(good_id int64, packing_price float64) error {
  8609. err := XTWriteDB().Model(models.VmStockFlow{}).Where("good_id = ? and user_org_id = 10633 and status=1", good_id).Updates(map[string]interface{}{"price": packing_price}).Error
  8610. return err
  8611. }
  8612. func GetGoodStockInventoryByRecordate(good_id int64, record_date int64, user_org_id int64) (models.XtGoodStockInventoryList, error) {
  8613. goodStockInventoryList := models.XtGoodStockInventoryList{}
  8614. err := XTReadDB().Where("good_id = ? and record_date = ? and user_org_id = ? and status=1", good_id, record_date, user_org_id).Find(&goodStockInventoryList).Error
  8615. return goodStockInventoryList, err
  8616. }
  8617. func CreateGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8618. err := XTWriteDB().Create(&list).Error
  8619. return err
  8620. }
  8621. func SaveGoodStockInventoryList(list models.XtGoodStockInventoryList) error {
  8622. err := XTWriteDB().Save(&list).Error
  8623. return err
  8624. }
  8625. func GetGoodStockInventoryList(keyword string, limit int64, page int64, storehouse_id int64, startTime int64, endTime int64, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8626. likeKey := "%" + keyword + "%"
  8627. offset := (page - 1) * limit
  8628. db := XTReadDB().Model(&models.XtGoodStockInventoryList{}).Where("status=1")
  8629. if storehouse_id > 0 {
  8630. db = db.Where("storehouse_id = ?", storehouse_id)
  8631. }
  8632. if startTime > 0 {
  8633. db = db.Where("record_date >=?", startTime)
  8634. }
  8635. if endTime > 0 {
  8636. db = db.Where("record_date<=?", endTime)
  8637. }
  8638. if len(keyword) > 0 {
  8639. db = db.Where("good_name like ? or specification_name like ?", likeKey, likeKey)
  8640. }
  8641. if orgId > 0 {
  8642. db = db.Where("user_org_id = ?", orgId)
  8643. }
  8644. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8645. return list, total, err
  8646. }
  8647. func GetDrugStockInventoryList(drug_id int64, record_date int64, user_org_id int64) (models.XtDrugStockInventoryList, error) {
  8648. drugStockInventoryList := models.XtDrugStockInventoryList{}
  8649. err := XTReadDB().Where("drug_id = ? and record_date = ? and user_org_id = ? and status=1", drug_id, record_date, user_org_id).Find(&drugStockInventoryList).Error
  8650. return drugStockInventoryList, err
  8651. }
  8652. func CreatedDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8653. err := XTWriteDB().Create(&list).Error
  8654. return err
  8655. }
  8656. func SaveDrugStockInventory(list models.XtDrugStockInventoryList) error {
  8657. err := XTWriteDB().Save(&list).Error
  8658. return err
  8659. }
  8660. func GetStockInventoryPrintList(storehouse_id int64, keyword string, limit int64, page int64, ids []string, orgId int64) (list []*models.XtGoodStockInventoryList, total int64, err error) {
  8661. db := XTReadDB().Model(&list).Where("status=1")
  8662. likeKey := "%" + keyword + "%"
  8663. offset := (page - 1) * limit
  8664. if storehouse_id > 0 {
  8665. db = db.Where("storehouse_id = ?", storehouse_id)
  8666. }
  8667. if len(keyword) > 0 {
  8668. db = db.Where("good_name like ?", likeKey)
  8669. }
  8670. if orgId > 0 {
  8671. db = db.Where("user_org_id = ?", orgId)
  8672. }
  8673. if len(ids) > 0 {
  8674. db = db.Where("id in(?)", ids)
  8675. }
  8676. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8677. return list, total, err
  8678. }
  8679. func GetGoodWarehouseInfoList(startTime int64, endTime int64, orgId int64) (info []*models.WarehousingInfo, err error) {
  8680. db := XTReadDB().Model(&info).Where("status=1 and is_check=1")
  8681. if startTime > 0 {
  8682. db = db.Where("ctime>=?", startTime)
  8683. }
  8684. if endTime > 0 {
  8685. db = db.Where("ctime<=?", endTime)
  8686. }
  8687. if orgId > 0 {
  8688. db = db.Where("org_id = ?", orgId)
  8689. }
  8690. err = db.Find(&info).Error
  8691. return info, err
  8692. }
  8693. func GetGoodWarhouseOutList(startTime int64, endTime int64, orgId int64) (outinfo []*models.WarehouseOutInfo, err error) {
  8694. db := XTReadDB().Model(&outinfo).Where("status=1 and is_check =1")
  8695. if startTime > 0 {
  8696. db = db.Where("ctime>=?", startTime)
  8697. }
  8698. if endTime > 0 {
  8699. db = db.Where("ctime<=?", endTime)
  8700. }
  8701. if orgId > 0 {
  8702. db = db.Where("org_id = ?", orgId)
  8703. }
  8704. err = db.Find(&outinfo).Error
  8705. return outinfo, err
  8706. }
  8707. func GetGoodStockFlowProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8708. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 10")
  8709. if startTime > 0 {
  8710. db = db.Where("ctime>=?", startTime)
  8711. }
  8712. if endTime > 0 {
  8713. db = db.Where("ctime<=?", endTime)
  8714. }
  8715. if orgId > 0 {
  8716. db = db.Where("user_org_id = ?", orgId)
  8717. }
  8718. err = db.Find(&flow).Error
  8719. return flow, err
  8720. }
  8721. func GetGoodStockFlowNoProList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8722. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 11")
  8723. if startTime > 0 {
  8724. db = db.Where("ctime>=?", startTime)
  8725. }
  8726. if endTime > 0 {
  8727. db = db.Where("ctime<=?", endTime)
  8728. }
  8729. if orgId > 0 {
  8730. db = db.Where("user_org_id = ?", orgId)
  8731. }
  8732. err = db.Find(&flow).Error
  8733. return flow, err
  8734. }
  8735. func GetCancelStockFlowList(startTime int64, endTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8736. db := XTReadDB().Model(&flow).Where("status=1 and consumable_type = 7")
  8737. if startTime > 0 {
  8738. db = db.Where("ctime>=?", startTime)
  8739. }
  8740. if endTime > 0 {
  8741. db = db.Where("ctime<=?", endTime)
  8742. }
  8743. if orgId > 0 {
  8744. db = db.Where("user_org_id = ?", orgId)
  8745. }
  8746. err = db.Find(&flow).Error
  8747. return flow, err
  8748. }
  8749. func GetLastMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8750. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 1 or consumable_type = 10)")
  8751. if startTime > 0 {
  8752. db = db.Where("ctime <?", startTime)
  8753. }
  8754. if orgId > 0 {
  8755. db = db.Where("user_org_id = ?", orgId)
  8756. }
  8757. err = db.Find(&flow).Error
  8758. return flow, err
  8759. }
  8760. func GetLastMonthOverCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8761. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 2 or consumable_type=3 or consumable_type=11)")
  8762. if startTime > 0 {
  8763. db = db.Where("ctime <?", startTime)
  8764. }
  8765. if orgId > 0 {
  8766. db = db.Where("user_org_id = ?", orgId)
  8767. }
  8768. err = db.Find(&flow).Error
  8769. return flow, err
  8770. }
  8771. func GetLastCancelMonthCount(startTime int64, orgId int64) (flow []*models.VmStockFlow, err error) {
  8772. db := XTReadDB().Model(&flow).Where("status=1 and (consumable_type = 7)")
  8773. if startTime > 0 {
  8774. db = db.Where("ctime <?", startTime)
  8775. }
  8776. if orgId > 0 {
  8777. db = db.Where("user_org_id = ?", orgId)
  8778. }
  8779. err = db.Find(&flow).Error
  8780. return flow, err
  8781. }
  8782. func GetDrugCodeOpen(user_org_id int64) (models.XtDrugCodeOpen, error) {
  8783. drugCodeOpen := models.XtDrugCodeOpen{}
  8784. err := XTReadDB().Where("user_org_id = ? and status=1", user_org_id).Find(&drugCodeOpen).Error
  8785. return drugCodeOpen, err
  8786. }
  8787. func CreateDrugOpen(open models.XtDrugCodeOpen) error {
  8788. err := XTWriteDB().Create(&open).Error
  8789. return err
  8790. }
  8791. func SaveDrugOpen(open models.XtDrugCodeOpen) error {
  8792. err := XTWriteDB().Save(&open).Error
  8793. return err
  8794. }
  8795. func GetHisDrugCodeQuery(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64, is_settle int64, keywords string, is_code int64) (list []*models.NewHisDoctorAdviceInfo, total int64, err error) {
  8796. db := XTReadDB().Model(&models.NewHisDoctorAdviceInfo{}).Preload("NewHisPrescription", "status = 1").Where("his_doctor_advice_info.status=1")
  8797. if is_settle == 2 {
  8798. db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8799. } else if is_settle == 1 {
  8800. db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status <> 2 and his_prescription.status = 1")
  8801. }
  8802. if is_code == 2 {
  8803. //db = db.Joins("join his_prescription on his_prescription.id = his_doctor_advice_info.prescription_id and his_prescription.order_status = 2 and his_prescription.status = 1")
  8804. db = db.Where("his_doctor_advice_info.drug_code <> ''")
  8805. } else if is_code == 1 {
  8806. db = db.Where("length(his_doctor_advice_info.drug_code) = 0")
  8807. }
  8808. if len(keywords) > 0 {
  8809. keywords = "%" + keywords + "%"
  8810. db = db.Joins("join xt_patients on xt_patients.id = his_doctor_advice_info.patient_id and xt_patients.user_org_id = ? and xt_patients.name like ?", orgId, keywords)
  8811. }
  8812. if orgId > 0 {
  8813. db = db.Where("his_doctor_advice_info.user_org_id=?", orgId)
  8814. }
  8815. if start_time > 0 {
  8816. db = db.Where("his_doctor_advice_info.advice_date >= ?", start_time)
  8817. }
  8818. if end_time > 0 {
  8819. db = db.Where("his_doctor_advice_info.advice_date<=?", end_time)
  8820. }
  8821. offset := (page - 1) * limit
  8822. if is_sale == 1 {
  8823. db = db.Where("his_doctor_advice_info.is_upload = 1")
  8824. }
  8825. if is_sale == 2 {
  8826. db = db.Where("his_doctor_advice_info.is_upload = 2 or his_doctor_advice_info.is_upload =0")
  8827. }
  8828. err = db.Count(&total).Offset(offset).Limit(limit).Order("his_doctor_advice_info.record_date desc").Find(&list).Error
  8829. return list, total, err
  8830. }
  8831. func GetBloodDrugCode(orgId int64, start_time int64, end_time int64, limit int64, page int64, is_sale int64) (advice []*models.DoctorAdvice, total int64, err error) {
  8832. db := XTReadDB().Model(&advice).Where("status=1 and advice_type !=1")
  8833. if orgId > 0 {
  8834. db = db.Where("user_org_id = ?", orgId)
  8835. }
  8836. if start_time > 0 {
  8837. db = db.Where("advice_date>=?", start_time)
  8838. }
  8839. if end_time > 0 {
  8840. db = db.Where("advice_date<=?", end_time)
  8841. }
  8842. if is_sale > 0 {
  8843. db = db.Where("is_upload = ?", is_sale)
  8844. }
  8845. offset := (page - 1) * limit
  8846. err = db.Count(&total).Offset(offset).Limit(limit).Order("advice_date desc").Find(&advice).Error
  8847. return advice, total, err
  8848. }
  8849. func GetDoctorAdviceInfoList(page int64, limit int64, orgId int64, start_time int64, end_time int64, is_sale int64) ([]models.HisDoctorAdviceInfo, int64, error) {
  8850. var list []models.HisDoctorAdviceInfo
  8851. var total int64
  8852. // 计算偏移量
  8853. offset := (page - 1) * limit
  8854. // 初始化数据库查询
  8855. db := XTReadDB().Model(&models.HisDoctorAdviceInfo{})
  8856. // 构建 WHERE 子句
  8857. query := db.Where("status=1")
  8858. if orgId > 0 {
  8859. query = query.Where("user_org_id=?", orgId)
  8860. }
  8861. if start_time > 0 {
  8862. query = query.Where("advice_date >= ?", start_time)
  8863. }
  8864. if end_time > 0 {
  8865. query = query.Where("advice_date <= ?", end_time)
  8866. }
  8867. if is_sale == 1 {
  8868. query = query.Where("is_upload = 1")
  8869. }
  8870. if is_sale == 2 {
  8871. query = query.Where("is_upload IN (?, ?)", 2, 0) // 使用 IN 替代 OR,通常性能更好
  8872. }
  8873. // 获取总记录数
  8874. err := query.Count(&total).Error
  8875. if err != nil {
  8876. return nil, 0, err
  8877. }
  8878. // 获取分页后的数据
  8879. err = query.Offset(offset).Limit(limit).Order("record_date desc").Find(&list).Error
  8880. if err != nil {
  8881. return nil, 0, err
  8882. }
  8883. return list, total, nil
  8884. }
  8885. func GetDrugWarehouseOutInfoByDrugCode(drug_id int64, patient_id int64, advice_date int64, user_org_id int64) (outinfo []*models.XtDrugWarehouseOutInfo, err error) {
  8886. err = XTReadDB().Where("drug_id = ? and patient_id = ? and sys_record_time =? and org_id = ? and status =1", drug_id, patient_id, advice_date, user_org_id).Find(&outinfo).Error
  8887. return outinfo, err
  8888. }
  8889. func GetNewGoodWarehouseOutInfo(good_id int64, user_org_id int64, warehouse_out_id int64, tx *gorm.DB) (info []*models.WarehouseOutInfo, err error) {
  8890. err = tx.Where("good_id = ? and org_id = ? and warehouse_out_id = ? and status= 1", good_id, user_org_id, warehouse_out_id).Find(&info).Error
  8891. if err != gorm.ErrRecordNotFound {
  8892. if err != nil {
  8893. tx.Rollback()
  8894. return info, err
  8895. }
  8896. }
  8897. return info, err
  8898. }
  8899. func GetInitGoodFlow(user_org_id int64, good_id int64, warehousing_detail_id int64, tx *gorm.DB) (list []*models.VmStockFlow, err error) {
  8900. err = tx.Where("user_org_id = ? and good_id = ? and warehousing_detail_id = ? and status=1", user_org_id, good_id, warehousing_detail_id).Find(&list).Error
  8901. if err != gorm.ErrRecordNotFound {
  8902. if err != nil {
  8903. tx.Rollback()
  8904. return list, err
  8905. }
  8906. }
  8907. return list, err
  8908. }
  8909. func GetNewDrugFlowList(drug_id int64, user_org_id int64, warehousing_detail_id int64, tx *gorm.DB) (flow []*models.DrugFlow, err error) {
  8910. err = tx.Where("drug_id = ? and user_org_id =? and warehousing_detail_id = ? and status=1", drug_id, user_org_id, warehousing_detail_id).Find(&flow).Error
  8911. if err != gorm.ErrRecordNotFound {
  8912. if err != nil {
  8913. tx.Rollback()
  8914. return flow, err
  8915. }
  8916. }
  8917. return flow, err
  8918. }
  8919. func GetNewPatientAdviceList(patient_id int64, start_time int64, end_time int64, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  8920. db := XTReadDB().Model(&advice).Where("status=1 and advice_type =2")
  8921. if patient_id > 0 {
  8922. db = db.Where("patient_id = ?", patient_id)
  8923. }
  8924. if start_time > 0 {
  8925. db = db.Where("advice_date>=?", start_time)
  8926. }
  8927. if end_time > 0 {
  8928. db = db.Where("advice_date<=?", end_time)
  8929. }
  8930. if user_org_id > 0 {
  8931. db = db.Where("user_org_id = ?", user_org_id)
  8932. }
  8933. err = db.Order("drug_id desc").Find(&advice).Error
  8934. return advice, err
  8935. }
  8936. func GetSelfDrugWarehouseInfoByPatientId(patient_id int64, user_org_id int64) (drugInfo []*models.XtSelfDrugWarehouseInfo, err error) {
  8937. err = XTReadDB().Where("patient_id = ? and org_id = ? and status =1 and is_check =1", patient_id, user_org_id).Find(&drugInfo).Error
  8938. return drugInfo, err
  8939. }
  8940. func GetPatientAdviceListDetail(patient_id int64, start_time int64, end_time int64, user_org_id int64, advice_name string) (advice []*models.DoctorAdvice, err error) {
  8941. db := XTReadDB().Model(&advice).Where("status = 1")
  8942. if patient_id > 0 {
  8943. db = db.Where("patient_id = ?", patient_id)
  8944. }
  8945. if start_time > 0 {
  8946. db = db.Where("advice_date>=?", start_time)
  8947. }
  8948. if end_time > 0 {
  8949. db = db.Where("advice_date<=?", end_time)
  8950. }
  8951. if user_org_id > 0 {
  8952. db = db.Where("user_org_id = ?", user_org_id)
  8953. }
  8954. if len(advice_name) > 0 {
  8955. db = db.Where("advice_name =?", advice_name)
  8956. }
  8957. err = db.Order("advice_date asc").Find(&advice).Error
  8958. return advice, err
  8959. }
  8960. func GetGoodWarehosueInfoPrintList(good_id int64, start_time int64, end_time int64, user_org_id int64) (info []*models.PrintWarehousingInfo, err error) {
  8961. db := XTReadDB().Model(&info).Where("status=1")
  8962. if good_id > 0 {
  8963. db = db.Where("good_id = ?", good_id)
  8964. }
  8965. if start_time > 0 {
  8966. db = db.Where("ctime>=?", start_time)
  8967. }
  8968. if end_time > 0 {
  8969. db = db.Where("ctime<=?", end_time)
  8970. }
  8971. if user_org_id > 0 {
  8972. db = db.Where("org_id = ?", user_org_id)
  8973. }
  8974. err = db.Find(&info).Error
  8975. return info, err
  8976. }
  8977. func GetDrugWarehouseInfoPrintList(drug_id int64, user_org_id int64, start_time int64, end_time int64) (drugInfo []*models.PrintDrugWarehouseInfo, err error) {
  8978. db := XTReadDB().Model(&drug_id).Where("status=1")
  8979. if drug_id > 0 {
  8980. db = db.Where("drug_id = ?", drug_id)
  8981. }
  8982. if start_time > 0 {
  8983. db = db.Where("ctime>=?", start_time)
  8984. }
  8985. if end_time > 0 {
  8986. db = db.Where("ctime<=?", end_time)
  8987. }
  8988. if user_org_id > 0 {
  8989. db = db.Where("org_id = ?", user_org_id)
  8990. }
  8991. err = db.Find(&drugInfo).Error
  8992. return drugInfo, err
  8993. }