stock_service.go 376KB

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