stock_service.go 252KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  11. err := writeDb.Create(&dealer).Error
  12. return err, dealer
  13. }
  14. func ModifyDealer(dealer *models.Dealer) error {
  15. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  16. "dealer_name": dealer.DealerName,
  17. "contact": dealer.Contact,
  18. "contact_phone": dealer.ContactPhone,
  19. "platform_number": dealer.PlatformNumber,
  20. "email": dealer.Email,
  21. "contact_address": dealer.ContactAddress,
  22. "modifier": dealer.Modifier,
  23. "mtime": time.Now().Unix(),
  24. "remark": dealer.Remark,
  25. "wubi": dealer.WuBi,
  26. "pinyin": dealer.PinYin,
  27. }).Error
  28. return err
  29. }
  30. func FindAllDealerTotal(orgId int64) (total int64) {
  31. db := readDb.Model(&models.Dealer{})
  32. db = db.Where("org_id = ? ", orgId).Count(&total)
  33. return
  34. }
  35. func FindAllDealerList(orgId int64, page int64, limit int64) (list []*models.Dealer, total int64, err error) {
  36. offset := (page - 1) * limit
  37. db := readDb.Model(&models.Dealer{})
  38. db = db.Where("org_id = ? AND status = 1", orgId)
  39. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  40. return
  41. }
  42. func DeleteDealerById(id int64) error {
  43. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  44. return err
  45. }
  46. func FindDealerById(id int64) (*models.Dealer, error) {
  47. dealer := &models.Dealer{}
  48. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  49. return dealer, err
  50. }
  51. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  52. err := writeDb.Create(&manufacturer).Error
  53. return err, manufacturer
  54. }
  55. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  56. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  57. "manufacturer_name": manufacturer.ManufacturerName,
  58. "contact": manufacturer.Contact,
  59. "contact_phone": manufacturer.ContactPhone,
  60. "platform_number": manufacturer.PlatformNumber,
  61. "email": manufacturer.Email,
  62. "contact_address": manufacturer.ContactAddress,
  63. "modifier": manufacturer.Modifier,
  64. "mtime": time.Now().Unix(),
  65. "remark": manufacturer.Remark,
  66. "wubi": manufacturer.WuBi,
  67. "pinyin": manufacturer.PinYin,
  68. }).Error
  69. return err
  70. }
  71. func FindAllManufacturerList(orgId int64, page int64, limit int64) (list []*models.Manufacturer, total int64, err error) {
  72. offset := (page - 1) * limit
  73. db := readDb.Model(&models.Manufacturer{})
  74. db = db.Where("org_id = ? AND status = 1", orgId)
  75. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  76. return
  77. }
  78. func DeleteManufacturerById(id int64) error {
  79. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  80. return err
  81. }
  82. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  83. dealer := &models.Manufacturer{}
  84. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  85. return dealer, err
  86. }
  87. func FindManufacturerTotal(orgId int64) (total int64) {
  88. db := readDb.Model(&models.Manufacturer{})
  89. db = db.Where("org_id = ?", orgId).Count(&total)
  90. return
  91. }
  92. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  93. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  94. return dealers, err
  95. }
  96. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  97. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  98. return manufacturer, err
  99. }
  100. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  101. db := readDb.Model(&models.GoodsType{})
  102. db = db.Where("org_id = ?", orgId).Count(&total)
  103. return
  104. }
  105. func GetGoodsTypeIsExist(orgid int64) (*models.GoodsType, error) {
  106. goodsType := models.GoodsType{}
  107. err := XTReadDB().Where("org_id = ? and stock_attribute = 2 and status =1", orgid).Find(&goodsType).Error
  108. if err == gorm.ErrRecordNotFound {
  109. return nil, err
  110. }
  111. if err != nil {
  112. return nil, err
  113. }
  114. return &goodsType, nil
  115. }
  116. func GetGoodsTypeIsExistOne(orgid int64) (*models.GoodsType, error) {
  117. goodsType := models.GoodsType{}
  118. err := XTReadDB().Where("org_id = ? and stock_attribute = 3 and status = 1", orgid).Find(&goodsType).Error
  119. if err == gorm.ErrRecordNotFound {
  120. return nil, err
  121. }
  122. if err != nil {
  123. return nil, err
  124. }
  125. return &goodsType, nil
  126. }
  127. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  128. err := writeDb.Create(&goodType).Error
  129. return err, goodType
  130. }
  131. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  132. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  133. "modifier": goodType.Modifier,
  134. "mtime": time.Now().Unix(),
  135. "remark": goodType.Remark,
  136. "type_name": goodType.TypeName,
  137. "out_stock": goodType.OutStock,
  138. "stock_attribute": goodType.StockAttribute,
  139. }).Error
  140. return err, goodType
  141. }
  142. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  143. offset := (page - 1) * limit
  144. db := readDb.Model(&models.GoodsType{})
  145. db = db.Where("org_id=? AND status = 1", orgId)
  146. if len(keyword) > 0 {
  147. likeKey := "%" + keyword + "%"
  148. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  149. }
  150. err = db.Count(&total).Offset(offset).Limit(limit).Order("type desc, number desc,ctime desc").Find(&list).Error
  151. return
  152. }
  153. func DeleteGoodTypeById(id int64, operateId int64) error {
  154. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  155. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  156. return err
  157. }
  158. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  159. goodType := &models.GoodsType{}
  160. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  161. return goodType, err
  162. }
  163. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  164. db := readDb.Model(&models.GoodInfo{})
  165. db = db.Where("org_id = ?", orgId).Count(&total)
  166. return
  167. }
  168. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  169. err := writeDb.Create(&goodInfo).Error
  170. return err, goodInfo
  171. }
  172. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  173. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  174. "good_type_id": goodInfo.GoodTypeId,
  175. "modifier": goodInfo.Modifier,
  176. "mtime": time.Now().Unix(),
  177. "remark": goodInfo.Remark,
  178. "specification_name": goodInfo.SpecificationName,
  179. "good_unit": goodInfo.GoodUnit,
  180. "buy_price": goodInfo.BuyPrice,
  181. "sell_price": goodInfo.SellPrice,
  182. "manufacturer": goodInfo.Manufacturer,
  183. "dealer": goodInfo.Dealer,
  184. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  185. "stock_warn_count": goodInfo.StockWarnCount,
  186. "is_reuse": goodInfo.IsReuse,
  187. "good_name": goodInfo.GoodName,
  188. "pinyin": goodInfo.Pinyin,
  189. "wubi": goodInfo.Wubi,
  190. "good_kind": goodInfo.GoodKind,
  191. "medical_insurance_level": goodInfo.MedicalInsuranceLevel,
  192. "retail_price": goodInfo.PackingPrice,
  193. "medical_insurance_number": goodInfo.MedicalInsuranceNumber,
  194. "is_special_diseases": goodInfo.IsSpecialDiseases,
  195. "is_record": goodInfo.IsRecord,
  196. "statistics_category": goodInfo.StatisticsCategory,
  197. "good_status": goodInfo.GoodStatus,
  198. "default_count": goodInfo.DefaultCount,
  199. "sign": goodInfo.Sign,
  200. "is_default": goodInfo.IsDefault,
  201. "is_charge_use": goodInfo.IsChargeUse,
  202. "is_charge_predict": goodInfo.IsChargePredict,
  203. "is_statistics_work": goodInfo.IsStatisticsWork,
  204. "sort": goodInfo.Sort,
  205. "is_doctor_use": goodInfo.IsDoctorUse,
  206. "agent": goodInfo.Agent,
  207. "good_number": goodInfo.GoodNumber,
  208. "social_security_directory_code": goodInfo.SocialSecurityDirectoryCode,
  209. "special_medical": goodInfo.SpecialMedical,
  210. "production_type": goodInfo.ProductionType,
  211. "min_number": goodInfo.MinNumber,
  212. "packing_unit": goodInfo.PackingUnit,
  213. "packing_price": goodInfo.PackingPrice,
  214. "default_count_unit": goodInfo.DefaultCountUnit,
  215. "min_unit": goodInfo.MinUnit,
  216. "register_number": goodInfo.RegisterNumber,
  217. "provinces_code": goodInfo.ProvincesCode,
  218. "is_user": goodInfo.IsUser,
  219. "number": goodInfo.Number,
  220. "is_warehouse": goodInfo.IsWarehouse,
  221. }).Error
  222. writeDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  223. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  224. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  225. writeDb.Model(&models.DialysisBeforePrepare{}).Where("good_id = ? AND user_org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  226. writeDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  227. writeDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND org_id = ?", goodInfo.ID, goodInfo.OrgId).Updates(map[string]interface{}{"good_type_id": goodInfo.GoodTypeId})
  228. return err, goodInfo
  229. }
  230. 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) {
  231. offset := (page - 1) * limit
  232. db := readDb.Model(&models.GoodInfo{})
  233. db = db.Where("org_id = ? AND status = 1", orgId)
  234. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  235. if len(keyword) > 0 {
  236. likeKey := "%" + keyword + "%"
  237. db = db.Where("good_code LIKE ? OR good_name LIKE ?", likeKey, likeKey)
  238. if is_use > 0 {
  239. if is_use == 2 {
  240. db = db.Where("find_in_set('停用',good_status) = 0")
  241. } else {
  242. db = db.Where("find_in_set('停用',good_status) > 0")
  243. }
  244. }
  245. if is_charge > 0 {
  246. if is_charge == 1 {
  247. db = db.Where("find_in_set('收费',good_status) > 0")
  248. } else {
  249. db = db.Where("find_in_set('收费',good_status) = 0")
  250. }
  251. }
  252. if good_kind > 0 {
  253. db = db.Where(" good_kind = ?", good_kind)
  254. }
  255. if is_mark > 0 {
  256. db = db.Where("is_mark = ?", is_mark)
  257. }
  258. if manufacturer > 0 {
  259. db = db.Where("manufacturer = ?", manufacturer)
  260. }
  261. if is_type > 0 {
  262. db = db.Where("good_type_id = ?", is_type)
  263. }
  264. } else {
  265. if is_use > 0 {
  266. if is_use == 2 {
  267. db = db.Where("find_in_set('停用',good_status) = 0")
  268. } else {
  269. db = db.Where("find_in_set('停用',good_status) > 0")
  270. }
  271. }
  272. if is_charge > 0 {
  273. if is_charge == 1 {
  274. db = db.Where("find_in_set('收费',good_status) > 0")
  275. } else {
  276. db = db.Where("find_in_set('收费',good_status) = 0")
  277. }
  278. }
  279. if good_kind > 0 {
  280. db = db.Where(" good_kind = ?", good_kind)
  281. }
  282. if is_mark > 0 {
  283. db = db.Where("is_mark = ?", is_mark)
  284. }
  285. if manufacturer > 0 {
  286. db = db.Where("manufacturer = ?", manufacturer)
  287. }
  288. if is_type > 0 {
  289. db = db.Where("good_type_id = ?", is_type)
  290. }
  291. }
  292. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  293. return
  294. }
  295. func DeleteGoodInfoById(id int64, operateId int64) error {
  296. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  297. return err
  298. }
  299. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  300. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  301. return list, err
  302. }
  303. func FindStockOutDetailGoodInfoByGoodId(id int64) (list []*models.AutomaticReduceDetail, err error) {
  304. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  305. return list, err
  306. }
  307. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  308. goodInfo := &models.GoodInfo{}
  309. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  310. return goodInfo, err
  311. }
  312. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  313. err = XTReadDB().Model(&models.GoodsType{}).Where("org_id = ? AND status = 1", org_id).Find(&goodType).Error
  314. return goodType, err
  315. }
  316. func FindAllGoodTypeOne(org_id int64) (goodType []*models.GoodsType, err error) {
  317. err = readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 and out_stock = 1", org_id).Find(&goodType).Error
  318. return goodType, err
  319. }
  320. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  321. err = XTReadDB().Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 and is_warehouse = 1", org_id).Find(&goodInfo).Error
  322. return goodInfo, err
  323. }
  324. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  325. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  326. return total, err
  327. }
  328. func FindAllWarehouseTotalOne(org_id int64) (total int64, err error) {
  329. err = readDb.Model(&models.XtDrugWarehouse{}).Where("org_id = ?", org_id).Count(&total).Error
  330. return total, err
  331. }
  332. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  333. err := writeDb.Create(&warehouse).Error
  334. return err
  335. }
  336. func AddSigleDrugWarehouse(warehouse *models.DrugWarehouse) error {
  337. err := writeDb.Create(&warehouse).Error
  338. return err
  339. }
  340. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  341. err := writeDb.Create(&salesReturn).Error
  342. return err
  343. }
  344. func AddSigleDrugSalesReturn(salesReturn *models.DrugSalesReturn) error {
  345. err := writeDb.Create(&salesReturn).Error
  346. return err
  347. }
  348. func UpDateGoodReturnStatus(ids []string) {
  349. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  350. }
  351. func UpDateCanCelStockStatus(ids []string) {
  352. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  353. }
  354. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  355. if len(warehousingInfo) > 0 {
  356. utx := writeDb.Begin()
  357. if len(warehousingInfo) > 0 {
  358. 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) VALUES "
  359. insertParams := make([]string, 0)
  360. insertData := make([]interface{}, 0)
  361. for _, info := range warehousingInfo {
  362. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  363. insertData = append(insertData, info.WarehousingId)
  364. insertData = append(insertData, info.GoodId)
  365. insertData = append(insertData, info.GoodTypeId)
  366. insertData = append(insertData, info.Number)
  367. insertData = append(insertData, info.ProductDate)
  368. insertData = append(insertData, info.ExpiryDate)
  369. insertData = append(insertData, info.WarehousingCount)
  370. insertData = append(insertData, info.Price)
  371. insertData = append(insertData, info.TotalPrice)
  372. insertData = append(insertData, info.Dealer)
  373. insertData = append(insertData, info.Manufacturer)
  374. insertData = append(insertData, info.Remark)
  375. insertData = append(insertData, info.Ctime)
  376. insertData = append(insertData, info.Mtime)
  377. insertData = append(insertData, info.Status)
  378. insertData = append(insertData, info.OrgId)
  379. insertData = append(insertData, info.WarehousingOrder)
  380. insertData = append(insertData, info.Type)
  381. insertData = append(insertData, info.WarehousingCount)
  382. insertData = append(insertData, info.LicenseNumber)
  383. insertData = append(insertData, info.PackingPrice)
  384. }
  385. thisSQL += strings.Join(insertParams, ", ")
  386. err = utx.Exec(thisSQL, insertData...).Error
  387. if err != nil {
  388. utx.Rollback()
  389. return
  390. }
  391. }
  392. utx.Commit()
  393. }
  394. return
  395. }
  396. func CreateDrugWarehousingInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  397. if len(warehousingInfo) > 0 {
  398. utx := writeDb.Begin()
  399. if len(warehousingInfo) > 0 {
  400. 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) VALUES "
  401. insertParams := make([]string, 0)
  402. insertData := make([]interface{}, 0)
  403. for _, info := range warehousingInfo {
  404. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  405. insertData = append(insertData, info.WarehousingId)
  406. insertData = append(insertData, info.DrugId)
  407. insertData = append(insertData, info.Number)
  408. insertData = append(insertData, info.ProductDate)
  409. insertData = append(insertData, info.ExpiryDate)
  410. insertData = append(insertData, info.WarehousingCount)
  411. insertData = append(insertData, info.Price)
  412. insertData = append(insertData, info.TotalPrice)
  413. insertData = append(insertData, info.Dealer)
  414. insertData = append(insertData, info.Manufacturer)
  415. insertData = append(insertData, info.Remark)
  416. insertData = append(insertData, info.Ctime)
  417. insertData = append(insertData, info.Mtime)
  418. insertData = append(insertData, info.Status)
  419. insertData = append(insertData, info.OrgId)
  420. insertData = append(insertData, info.WarehousingOrder)
  421. insertData = append(insertData, info.Type)
  422. insertData = append(insertData, info.RetailPrice)
  423. insertData = append(insertData, info.RetailTotalPrice)
  424. insertData = append(insertData, info.StockMaxNumber)
  425. insertData = append(insertData, info.MaxUnit)
  426. insertData = append(insertData, info.MinUnit)
  427. insertData = append(insertData, info.BatchNumber)
  428. insertData = append(insertData, info.StockMinNumber)
  429. }
  430. thisSQL += strings.Join(insertParams, ", ")
  431. err = utx.Exec(thisSQL, insertData...).Error
  432. if err != nil {
  433. utx.Rollback()
  434. return
  435. }
  436. }
  437. utx.Commit()
  438. }
  439. return
  440. }
  441. func CreateDrugWarehousingInfoSix(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  442. if len(warehousingInfo) > 0 {
  443. utx := writeDb.Begin()
  444. if len(warehousingInfo) > 0 {
  445. 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) VALUES "
  446. insertParams := make([]string, 0)
  447. insertData := make([]interface{}, 0)
  448. for _, info := range warehousingInfo {
  449. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  450. insertData = append(insertData, info.WarehousingId)
  451. insertData = append(insertData, info.DrugId)
  452. insertData = append(insertData, info.Number)
  453. insertData = append(insertData, info.ProductDate)
  454. insertData = append(insertData, info.ExpiryDate)
  455. insertData = append(insertData, info.WarehousingCount)
  456. insertData = append(insertData, info.Price)
  457. insertData = append(insertData, info.TotalPrice)
  458. insertData = append(insertData, info.Dealer)
  459. insertData = append(insertData, info.Manufacturer)
  460. insertData = append(insertData, info.Remark)
  461. insertData = append(insertData, info.Ctime)
  462. insertData = append(insertData, info.Mtime)
  463. insertData = append(insertData, info.Status)
  464. insertData = append(insertData, info.OrgId)
  465. insertData = append(insertData, info.WarehousingOrder)
  466. insertData = append(insertData, info.Type)
  467. insertData = append(insertData, info.RetailPrice)
  468. insertData = append(insertData, info.RetailTotalPrice)
  469. insertData = append(insertData, info.StockMaxNumber)
  470. insertData = append(insertData, info.MaxUnit)
  471. insertData = append(insertData, info.MinUnit)
  472. insertData = append(insertData, info.BatchNumber)
  473. insertData = append(insertData, info.StockMinNumber)
  474. insertData = append(insertData, info.SupplyWarehouseId)
  475. insertData = append(insertData, info.SupplyWarehouseDetailInfo)
  476. }
  477. thisSQL += strings.Join(insertParams, ", ")
  478. err = utx.Exec(thisSQL, insertData...).Error
  479. if err != nil {
  480. utx.Rollback()
  481. return
  482. }
  483. }
  484. utx.Commit()
  485. }
  486. return
  487. }
  488. func UpdateDrugInfo(warehousingInfo []*models.DrugWarehouseInfo) (err error) {
  489. var total float64
  490. var min_number int64
  491. for _, item := range warehousingInfo {
  492. //查询刚入库耗材的库存
  493. count, _ := FindDrugWaresingCount(item.DrugId, item.OrgId)
  494. //更新药品库的总量
  495. info, _ := FindeDrugInfo(item.DrugId)
  496. min_number = count.StockMaxNumber * info.MinNumber
  497. str := strconv.FormatInt(min_number, 10)
  498. minFloat, _ := strconv.ParseFloat(str, 64)
  499. total = info.Total + minFloat
  500. lib := models.BaseDrugLib{
  501. Total: total,
  502. }
  503. UpdateBaseDrug(&lib, item.ID)
  504. }
  505. return err
  506. }
  507. func FindDrugWaresingCount(drug_id int64, orgid int64) (models.XtDrugWarehouseInfo, error) {
  508. info := models.XtDrugWarehouseInfo{}
  509. db := XTReadDB().Table("xt_drug_warehouse_info").Where("status = 1")
  510. err = db.Select("stock_max_number,drug_id").Where("drug_id = ? and org_id = ? and status =1", drug_id, orgid).Last(&info).Error
  511. return info, err
  512. }
  513. func FindeDrugInfo(drug_id int64) (models.BaseDrugLib, error) {
  514. lib := models.BaseDrugLib{}
  515. err := XTReadDB().Model(&lib).Where("id = ?", drug_id).Find(&lib).Error
  516. return lib, err
  517. }
  518. func UpdateBaseDrug(lib *models.BaseDrugLib, id int64) error {
  519. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"total": lib.Total}).Error
  520. return err
  521. }
  522. func UpdateBaseDrugOne(lib models.BaseDrugLib, id int64) error {
  523. err := XTWriteDB().Model(&lib).Where("id = ?", id).Updates(map[string]interface{}{"retail_price": lib.RetailPrice, "last_price": lib.LastPrice}).Error
  524. return err
  525. }
  526. func FindeLastWarehouseInfo(id int64) (models.DrugWarehouseInfo, error) {
  527. info := models.DrugWarehouseInfo{}
  528. err := XTReadDB().Model(&info).Where("id=? and status = 1", id).Find(&info).Error
  529. return info, err
  530. }
  531. func GetLastWarehouseOut(orgid int64) (models.DrugWarehouseOut, error) {
  532. out := models.DrugWarehouseOut{}
  533. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&out).Error
  534. return out, err
  535. }
  536. func CreateDrugFlow(drugflow []*models.DrugFlow) (err error) {
  537. if len(drugflow) > 0 {
  538. utx := writeDb.Begin()
  539. if len(drugflow) > 0 {
  540. 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) VALUES "
  541. insertParams := make([]string, 0)
  542. insertData := make([]interface{}, 0)
  543. for _, info := range drugflow {
  544. fmt.Println("hhhhhh", info.WarehouseOutId)
  545. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  546. insertData = append(insertData, info.WarehousingId)
  547. insertData = append(insertData, info.DrugId)
  548. insertData = append(insertData, info.Number)
  549. insertData = append(insertData, info.BatchNumber)
  550. insertData = append(insertData, info.Count)
  551. insertData = append(insertData, info.UserOrgId)
  552. insertData = append(insertData, info.PatientId)
  553. insertData = append(insertData, info.SystemTime)
  554. insertData = append(insertData, info.ConsumableType)
  555. insertData = append(insertData, info.IsSys)
  556. insertData = append(insertData, info.WarehousingOrder)
  557. insertData = append(insertData, info.WarehouseOutId)
  558. insertData = append(insertData, info.WarehouseOutOrderNumber)
  559. insertData = append(insertData, info.IsEdit)
  560. insertData = append(insertData, info.CancelStockId)
  561. insertData = append(insertData, info.CancelOrderNumber)
  562. insertData = append(insertData, info.Manufacturer)
  563. insertData = append(insertData, info.Dealer)
  564. insertData = append(insertData, info.Creator)
  565. insertData = append(insertData, info.UpdateCreator)
  566. insertData = append(insertData, info.Status)
  567. insertData = append(insertData, info.Ctime)
  568. insertData = append(insertData, info.Mtime)
  569. insertData = append(insertData, info.Price)
  570. insertData = append(insertData, info.WarehousingDetailId)
  571. insertData = append(insertData, info.WarehouseOutDetailId)
  572. insertData = append(insertData, info.CancelOutDetailId)
  573. insertData = append(insertData, info.ExpireDate)
  574. insertData = append(insertData, info.ProductDate)
  575. insertData = append(insertData, info.MaxUnit)
  576. insertData = append(insertData, info.MinUnit)
  577. }
  578. thisSQL += strings.Join(insertParams, ", ")
  579. err = utx.Exec(thisSQL, insertData...).Error
  580. if err != nil {
  581. utx.Rollback()
  582. return
  583. }
  584. }
  585. utx.Commit()
  586. }
  587. return
  588. }
  589. func GetDrugFlowIsExist(warhouseing int64, drug_id int64) (*models.DrugFlow, error) {
  590. info := models.DrugFlow{}
  591. err = XTReadDB().Where("warehousing_id = ? and drug_id = ? and status = 1", warhouseing, drug_id).Find(&info).Error
  592. if err == gorm.ErrRecordNotFound {
  593. return nil, err
  594. }
  595. if err != nil {
  596. return nil, err
  597. }
  598. return &info, nil
  599. }
  600. func GetDrugByWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  601. info := models.XtDrugWarehouseInfo{}
  602. err := XTReadDB().Where("id=? and status = 1", id).Find(&info).Error
  603. return info, err
  604. }
  605. func UpdateDrugWarehouseingInfo(id int64, info models.XtDrugWarehouseInfo) error {
  606. warehouseInfo := models.XtDrugWarehouseInfo{}
  607. err := XTWriteDB().Model(&warehouseInfo).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber}).Error
  608. return err
  609. }
  610. func CreateDrugFlowOne(flow models.DrugFlow) error {
  611. err := XTWriteDB().Create(&flow).Error
  612. return err
  613. }
  614. func UpdateDrugFlowSeven(patientid int64, recorddate int64, drugid int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  615. detail := models.DrugAutomaticReduceDetail{}
  616. 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
  617. 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
  618. return detail, err
  619. }
  620. func UpdateDrugFlowTens(patientid int64, warehouse_out_number string, drug_id int64, advice_id int64) (models.DrugFlow, error) {
  621. flow := models.DrugFlow{}
  622. 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{}{"status": 0}).Error
  623. return flow, err
  624. }
  625. func CreateDrugFlowTwo(flow *models.DrugFlow) error {
  626. err := XTWriteDB().Create(&flow).Error
  627. return err
  628. }
  629. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  630. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  631. return info, err
  632. }
  633. func GetLastWarehousingInfo(goodid int64) (models.WarehousingInfo, error) {
  634. info := models.WarehousingInfo{}
  635. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  636. return info, err
  637. }
  638. func CreatedWarehouseingDetail(info *models.WarehousingInfo) error {
  639. err := XTWriteDB().Create(&info).Error
  640. return err
  641. }
  642. func FindFirstWarehousingInfoByStock(good_id int64, good_type_id int64) (info models.WarehousingInfo, err error) {
  643. 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
  644. return info, err
  645. }
  646. func FindFirstWarehousingInfoByStockTwo(good_id int64, good_type_id int64) (info models.VmWarehousingInfo, err error) {
  647. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  648. if good_id > 0 {
  649. db = db.Where("x.good_id = ?", good_id)
  650. }
  651. if good_type_id > 0 {
  652. db = db.Where("x.good_type_id = ?", good_type_id)
  653. }
  654. err = db.Select("sum(x.stock_count) as count").Scan(&info).Error
  655. return info, err
  656. }
  657. func FindWarehousingInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  658. info := models.WarehousingInfo{}
  659. err := XTReadDB().Model(&info).Where("id= ? and status = 1", id).Find(&info).Error
  660. return info, err
  661. }
  662. func FindWarehousingInfoTen(goodId int64) (models.WarehousingInfo, error) {
  663. info := models.WarehousingInfo{}
  664. err := XTReadDB().Select(" good_id,sum(stock_count) as stock_count").Where("good_id = ? and status = 1", goodId).Find(&info).Error
  665. return info, err
  666. }
  667. //药品先进先出,找出最先入库的批次
  668. func FindLastDrugWarehousingInfoByID(drug_id int64) (info models.XtDrugWarehouseInfo, err error) {
  669. err = readDb.Model(&models.XtDrugWarehouseInfo{}).Where("drug_id = ? AND status = 1 AND (stock_max_number > 0 or stock_min_number > 0) ", drug_id).Order("ctime").First(&info).Error
  670. return info, err
  671. }
  672. // 出库时先进先出,找出最先入库的批次
  673. func FindLastWarehousingInfoByID(drug_id int64) (info models.WarehousingInfo, err error) {
  674. 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
  675. return info, err
  676. }
  677. //
  678. //// 出库时先进先出,找出最先入库的批次
  679. //func (drug_id int64) (info models.WarehousingInfo, err error) {
  680. // 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
  681. // return info, err
  682. //}
  683. func FindLastDrugWarehousingInfo(order string) (info models.DrugWarehouseInfo, err error) {
  684. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  685. return info, err
  686. }
  687. func FindAllWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64) (list []*models.Warehousing, total int64, err error) {
  688. db := readDb.Model(&models.Warehousing{})
  689. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  690. if len(keywords) > 0 {
  691. likeKey := "%" + keywords + "%"
  692. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  693. 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")
  694. }
  695. if startTime > 0 {
  696. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  697. }
  698. if endTime > 0 {
  699. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  700. }
  701. db = db.Count(&total)
  702. offset := (page - 1) * limit
  703. err = db.Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  704. return
  705. }
  706. func GetGoodInforByGoodName(keyword string, orgid int64) (goodInfo []*models.GoodInfo, err error) {
  707. db := XTReadDB().Model(&goodInfo).Where("status = 1")
  708. if orgid > 0 {
  709. db = db.Where("org_id = ?", orgid)
  710. }
  711. if len(keyword) > 0 {
  712. likeKey := "%" + keyword + "%"
  713. db = db.Where("good_name like ?", likeKey)
  714. }
  715. err = db.Find(&goodInfo).Error
  716. return goodInfo, err
  717. }
  718. func GetWarehoureOrderInfoByGoodId(good_id []int64, startime int64, endtime int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  719. db := XTReadDB().Model(&info).Where("status =1")
  720. if len(good_id) > 0 {
  721. db = db.Where("good_id in (?)", good_id)
  722. }
  723. if startime > 0 {
  724. db = db.Where("ctime >=?", startime)
  725. }
  726. if endtime > 0 {
  727. db = db.Where("ctime<=?", endtime)
  728. }
  729. if orgid > 0 {
  730. db = db.Where("org_id = ?", orgid)
  731. }
  732. err = db.Find(&info).Error
  733. return info, err
  734. }
  735. func GetWarehoureOrderOutByGoodId(good_id []int64, startime int64, endtime int64, orgid int64) (info []*models.WarehouseOutInfo, err error) {
  736. db := XTReadDB().Model(&info).Where("status =1")
  737. if len(good_id) > 0 {
  738. db = db.Where("good_id in (?)", good_id)
  739. }
  740. if startime > 0 {
  741. db = db.Where("ctime >=?", startime)
  742. }
  743. if endtime > 0 {
  744. db = db.Where("ctime<=?", endtime)
  745. }
  746. if orgid > 0 {
  747. db = db.Where("org_id = ?", orgid)
  748. }
  749. err = db.Find(&info).Error
  750. return info, err
  751. }
  752. type VMDrugWarehouse struct {
  753. ID int64 `gorm:"column:id" json:"id" form:"id"`
  754. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  755. OperationTime int64 `gorm:"column:operation_time" json:"operation_time" form:"operation_time"`
  756. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  757. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  758. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  759. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  760. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  761. Status int64 `gorm:"column:status" json:"status" form:"status"`
  762. WarehousingTime int64 `gorm:"column:warehousing_time" json:"warehousing_time" form:"warehousing_time"`
  763. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  764. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  765. Type int64 `gorm:"column:type" json:"type" form:"type"`
  766. Manufacturers *models.Manufacturer `gorm:"ForeignKey:Manufacturer;AssociationForeignKey:ID" json:"manufacturers"`
  767. Dealers *models.Dealer `gorm:"ForeignKey:Dealer;AssociationForeignKey:ID" json:"dealers"`
  768. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  769. }
  770. func (VMDrugWarehouse) TableName() string {
  771. return "xt_drug_warehouse"
  772. }
  773. type VMDrugWarehouseInfo struct {
  774. ID int64 `gorm:"column:id" json:"id" form:"id"`
  775. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id" form:"warehousing_id"`
  776. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  777. Number string `gorm:"column:number" json:"number" form:"number"`
  778. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  779. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  780. WarehousingCount int64 `gorm:"column:warehousing_count" json:"warehousing_count" form:"warehousing_count"`
  781. Price float64 `gorm:"column:price" json:"price" form:"price"`
  782. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  783. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  784. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  785. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  786. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  787. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  788. Status int64 `gorm:"column:status" json:"status" form:"status"`
  789. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  790. IsReturn int64 `gorm:"column:is_return" json:"is_return" form:"is_return"`
  791. WarehousingOrder string `gorm:"column:warehousing_order" json:"warehousing_order" form:"warehousing_order"`
  792. Type int64 `gorm:"column:type" json:"type" form:"type"`
  793. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  794. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  795. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  796. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  797. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  798. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  799. SupplyWarehouseId int64 `gorm:"column:supply_warehouse_id" json:"supply_warehouse_id" form:"supply_warehouse_id"`
  800. SupplyWarehouseDetailInfo int64 `gorm:"column:supply_warehouse_detail_info" json:"supply_warehouse_detail_info" form:"supply_warehouse_detail_info"`
  801. }
  802. func (VMDrugWarehouseInfo) TableName() string {
  803. return "xt_drug_warehouse_info"
  804. }
  805. type Drug struct {
  806. ID int64 `gorm:"column:id" json:"id" form:"id"`
  807. DrugName string `gorm:"column:drug_name" json:"drug_name" form:"drug_name"`
  808. Pinyin string `gorm:"column:pinyin" json:"pinyin" form:"pinyin"`
  809. Wubi string `gorm:"column:wubi" json:"wubi" form:"wubi"`
  810. DrugAlias string `gorm:"column:drug_alias" json:"drug_alias" form:"drug_alias"`
  811. DrugAliasPinyin string `gorm:"column:drug_alias_pinyin" json:"drug_alias_pinyin" form:"drug_alias_pinyin"`
  812. DrugAliasWubi string `gorm:"column:drug_alias_wubi" json:"drug_alias_wubi" form:"drug_alias_wubi"`
  813. DrugCategory int64 `gorm:"column:drug_category" json:"drug_category" form:"drug_category"`
  814. DrugSpec string `gorm:"column:drug_spec" json:"drug_spec" form:"drug_spec"`
  815. DrugType int64 `gorm:"column:drug_type" json:"drug_type" form:"drug_type"`
  816. DrugStockLimit string `gorm:"column:drug_stock_limit" json:"drug_stock_limit" form:"drug_stock_limit"`
  817. DrugOriginPlace string `gorm:"column:drug_origin_place" json:"drug_origin_place" form:"drug_origin_place"`
  818. DrugDosageForm int64 `gorm:"column:drug_dosage_form" json:"drug_dosage_form" form:"drug_dosage_form"`
  819. MedicalInsuranceLevel int64 `gorm:"column:medical_insurance_level" json:"medical_insurance_level" form:"medical_insurance_level"`
  820. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  821. MinUnit string `gorm:"column:min_unit" json:"min_unit" form:"min_unit"`
  822. UnitMatrixing string `gorm:"column:unit_matrixing" json:"unit_matrixing" form:"unit_matrixing"`
  823. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  824. LastPrice float64 `gorm:"column:last_price" json:"last_price" form:"last_price"`
  825. DrugControl int64 `gorm:"column:drug_control" json:"drug_control" form:"drug_control"`
  826. Number string `gorm:"column:number" json:"number" form:"number"`
  827. DrugClassify string `gorm:"column:drug_classify" json:"drug_classify" form:"drug_classify"`
  828. DrugDose float64 `gorm:"column:drug_dose" json:"drug_dose" form:"drug_dose"`
  829. DrugDoseUnit int64 `gorm:"column:drug_dose_unit" json:"drug_dose_unit" form:"drug_dose_unit"`
  830. MedicalInsuranceNumber string `gorm:"column:medical_insurance_number" json:"medical_insurance_number" form:"medical_insurance_number"`
  831. ProvincesCode string `gorm:"column:provinces_code" json:"provinces_code" form:"provinces_code"`
  832. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  833. PharmacologyCategory int64 `gorm:"column:pharmacology_category" json:"pharmacology_category" form:"pharmacology_category"`
  834. StatisticsCategory int64 `gorm:"column:statistics_category" json:"statistics_category" form:"statistics_category"`
  835. Code string `gorm:"column:code" json:"code" form:"code"`
  836. IsSpecialDiseases int64 `gorm:"column:is_special_diseases" json:"is_special_diseases" form:"is_special_diseases"`
  837. IsRecord int64 `gorm:"column:is_record" json:"is_record" form:"is_record"`
  838. Agent string `gorm:"column:agent" json:"agent" form:"agent"`
  839. DrugStatus string `gorm:"column:drug_status" json:"drug_status" form:"drug_status"`
  840. LimitRemark string `gorm:"column:limit_remark" json:"limit_remark" form:"limit_remark"`
  841. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way" form:"delivery_way"`
  842. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency" form:"execution_frequency"`
  843. SingleDose float64 `gorm:"column:single_dose" json:"single_dose" form:"single_dose"`
  844. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number" form:"prescribing_number"`
  845. Label int64 `gorm:"column:label" json:"label" form:"label"`
  846. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  847. IsUseDoctorAdvice int64 `gorm:"column:is_use_doctor_advice" json:"is_use_doctor_advice" form:"is_use_doctor_advice"`
  848. IsDefault int64 `gorm:"column:is_default" json:"is_default" form:"is_default"`
  849. IsChargePredict int64 `gorm:"column:is_charge_predict" json:"is_charge_predict" form:"is_charge_predict"`
  850. IsStatisticsWork int64 `gorm:"column:is_statistics_work" json:"is_statistics_work" form:"is_statistics_work"`
  851. IsChargeUse int64 `gorm:"column:is_charge_use" json:"is_charge_use" form:"is_charge_use"`
  852. Status int64 `gorm:"column:status" json:"status" form:"status"`
  853. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  854. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  855. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  856. DrugCode string `gorm:"column:drug_code" json:"drug_code" form:"drug_code"`
  857. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  858. DoseCode string `gorm:"column:dose_code" json:"dose_code" form:"dose_code"`
  859. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  860. DrugRemark string `gorm:"column:drug_remark" json:"drug_remark" form:"drug_remark"`
  861. SocialSecurityDirectoryCode string `gorm:"column:social_security_directory_code" json:"social_security_directory_code" form:"social_security_directory_code"`
  862. IsMark int64 `gorm:"column:is_mark" json:"is_mark" form:"is_mark"`
  863. PrescriptionMark int64 `gorm:"column:prescription_mark" json:"prescription_mark" form:"prescription_mark"`
  864. HospApprFlag int64 `gorm:"column:hosp_appr_flag" json:"hosp_appr_flag" form:"hosp_appr_flag"`
  865. LmtUsedFlag int64 `gorm:"column:lmt_used_flag" json:"lmt_used_flag" form:"lmt_used_flag"`
  866. Total float64 `gorm:"column:total" json:"total" form:"total"`
  867. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit" form:"prescribing_number_unit"`
  868. MinNumber int64 `gorm:"column:min_number" json:"min_number" form:"min_number"`
  869. Dose float64 `gorm:"column:dose" json:"dose" form:"dose"`
  870. DoseUnit string `gorm:"column:dose_unit" json:"dose_unit" form:"dose_unit"`
  871. DrugDay string `gorm:"column:drug_day" json:"drug_day" form:"drug_day"`
  872. MinPrice float64 `gorm:"column:min_price" json:"min_price" form:"min_price"`
  873. }
  874. func (Drug) TableName() string {
  875. return "xt_base_drug"
  876. }
  877. func FindAllDrugWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*VMDrugWarehouse, total int64, err error) {
  878. db := readDb.Model(&VMDrugWarehouse{})
  879. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  880. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  881. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  882. if len(keywords) > 0 {
  883. likeKey := "%" + keywords + "%"
  884. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  885. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse.manufacturer")
  886. 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")
  887. }
  888. if startTime > 0 {
  889. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  890. }
  891. if endTime > 0 {
  892. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  893. }
  894. db = db.Count(&total)
  895. offset := (page - 1) * limit
  896. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  897. return
  898. }
  899. func FindAllDrugWarehousingListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64) (list []*VMDrugWarehouse, total int64, err error) {
  900. db := readDb.Model(&VMDrugWarehouse{})
  901. db = db.Where("xt_drug_warehouse.org_id = ? AND xt_drug_warehouse.status = 1", orgId)
  902. db = db.Preload("Manufacturers", "status = 1 AND org_id = ?", orgId)
  903. db = db.Preload("Dealers", "status = 1 AND org_id = ?", orgId)
  904. if len(keywords) > 0 {
  905. likeKey := "%" + keywords + "%"
  906. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse.creater")
  907. 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")
  908. }
  909. if startTime > 0 {
  910. db = db.Where("xt_drug_warehouse.operation_time >=?", startTime)
  911. }
  912. if endTime > 0 {
  913. db = db.Where("xt_drug_warehouse.operation_time<= ?", endTime)
  914. }
  915. db = db.Count(&total)
  916. offset := (page - 1) * limit
  917. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse.ctime desc").Find(&list).Error
  918. return
  919. }
  920. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  921. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  922. return list, err
  923. }
  924. func FindWarehousingInfoByIdOne(id []int64) (list []*models.WarehousingInfo, err error) {
  925. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id in(?) AND status = 1", id).Preload("GoodInfo").Find(&list).Error
  926. return list, err
  927. }
  928. func FindDrugWarehousingInfoById(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  929. 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
  930. return list, err
  931. }
  932. func FindDrugWarehousingInfoByIdThree(id int64, org_id int64) (list []*VMDrugWarehouseInfo, err error) {
  933. err = readDb.Model(&VMDrugWarehouseInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  934. return list, err
  935. }
  936. func FindDrugWarehousingInfoByIdOne(id []string, org_id int64) (list []*models.StDrugWarehouseInfo, err error) {
  937. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  938. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  939. fmt.Println(table)
  940. if len(id) > 0 {
  941. db = db.Where("x.warehousing_id in(?)", id)
  942. }
  943. if org_id > 0 {
  944. db = db.Where("x.org_id = ?", org_id)
  945. }
  946. 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.stock_max_number,x.stock_min_number,x.price,x.remark,x.batch_number,x.batch_number,t.manufacturer,t.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
  947. return list, err
  948. }
  949. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  950. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  951. return warehousing, err
  952. }
  953. func FindWarehousingByIdOne(id []string) (warehousing []*models.Warehousing, err error) {
  954. err = readDb.Model(&models.Warehousing{}).Where("id in(?) and status = 1", id).Find(&warehousing).Error
  955. //err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  956. return warehousing, err
  957. }
  958. func FindDrugWarehousingById(id int64, org_id int64) (warehousing VMDrugWarehouse, err error) {
  959. 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
  960. return warehousing, err
  961. }
  962. func FindDrugWarehousingByIdOne(id []string, org_id int64) (warehousing []*models.DrugWarehouse, err error) {
  963. db := XTReadDB().Table("xt_drug_warehouse as x").Where("x.status = 1")
  964. if len(id) > 0 {
  965. db = db.Where("x.id in(?)", id)
  966. }
  967. if org_id > 0 {
  968. db = db.Where("x.org_id = ?", org_id)
  969. }
  970. 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
  971. return warehousing, err
  972. }
  973. //func FindDrugWarehousingById(id int64) (warehousing models.DrugWarehouse, err error) {
  974. // err = readDb.Model(&models.DrugWarehouse{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  975. // return warehousing, err
  976. //
  977. //}
  978. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  979. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  980. return
  981. }
  982. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  983. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  984. return
  985. }
  986. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  987. if len(salesReturnInfo) > 0 {
  988. utx := writeDb.Begin()
  989. if len(salesReturnInfo) > 0 {
  990. 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 "
  991. insertParams := make([]string, 0)
  992. insertData := make([]interface{}, 0)
  993. for _, info := range salesReturnInfo {
  994. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  995. insertData = append(insertData, info.GoodId)
  996. insertData = append(insertData, info.SalesReturnId)
  997. insertData = append(insertData, info.GoodTypeId)
  998. insertData = append(insertData, info.Count)
  999. insertData = append(insertData, info.Price)
  1000. insertData = append(insertData, info.Total)
  1001. insertData = append(insertData, info.Ctime)
  1002. insertData = append(insertData, info.Status)
  1003. insertData = append(insertData, info.OrgId)
  1004. insertData = append(insertData, info.OrderNumber)
  1005. insertData = append(insertData, info.Type)
  1006. insertData = append(insertData, info.Dealer)
  1007. insertData = append(insertData, info.Manufacturer)
  1008. }
  1009. thisSQL += strings.Join(insertParams, ", ")
  1010. err = utx.Exec(thisSQL, insertData...).Error
  1011. if err != nil {
  1012. utx.Rollback()
  1013. return
  1014. }
  1015. }
  1016. utx.Commit()
  1017. }
  1018. return
  1019. }
  1020. func CreateDrugSalesReturnInfo(salesReturnInfo []*models.DrugSalesReturnInfo) (err error) {
  1021. if len(salesReturnInfo) > 0 {
  1022. utx := writeDb.Begin()
  1023. if len(salesReturnInfo) > 0 {
  1024. 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 "
  1025. insertParams := make([]string, 0)
  1026. insertData := make([]interface{}, 0)
  1027. for _, info := range salesReturnInfo {
  1028. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1029. insertData = append(insertData, info.DrugId)
  1030. insertData = append(insertData, info.SalesReturnId)
  1031. insertData = append(insertData, info.Count)
  1032. insertData = append(insertData, info.Price)
  1033. insertData = append(insertData, info.Total)
  1034. insertData = append(insertData, info.Ctime)
  1035. insertData = append(insertData, info.Status)
  1036. insertData = append(insertData, info.OrgId)
  1037. insertData = append(insertData, info.OrderNumber)
  1038. insertData = append(insertData, info.Type)
  1039. insertData = append(insertData, info.Dealer)
  1040. insertData = append(insertData, info.Manufacturer)
  1041. insertData = append(insertData, info.RetailPrice)
  1042. insertData = append(insertData, info.RetailTotalPrice)
  1043. }
  1044. thisSQL += strings.Join(insertParams, ", ")
  1045. err = utx.Exec(thisSQL, insertData...).Error
  1046. if err != nil {
  1047. utx.Rollback()
  1048. return
  1049. }
  1050. }
  1051. utx.Commit()
  1052. }
  1053. return
  1054. }
  1055. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  1056. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  1057. return total, err
  1058. }
  1059. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  1060. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1061. return total, err
  1062. }
  1063. func FindAllDrugCancelStockTotal(org_id int64) (total int64, err error) {
  1064. err = readDb.Model(&models.DrugCancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  1065. return total, err
  1066. }
  1067. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  1068. db := readDb.Model(&models.SalesReturn{})
  1069. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  1070. if len(keywords) > 0 {
  1071. likeKey := "%" + keywords + "%"
  1072. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  1073. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  1074. 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")
  1075. }
  1076. if startTime > 0 {
  1077. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  1078. }
  1079. if endTime > 0 {
  1080. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  1081. }
  1082. db = db.Count(&total)
  1083. offset := (page - 1) * limit
  1084. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  1085. return
  1086. }
  1087. func FindAllDrugReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugSalesReturn, total int64, err error) {
  1088. db := readDb.Model(&models.DrugSalesReturn{})
  1089. db = db.Where("xt_drug_sales_return.org_id = ? AND xt_drug_sales_return.status = 1 ", orgId)
  1090. if len(keywords) > 0 {
  1091. likeKey := "%" + keywords + "%"
  1092. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_sales_return.creater")
  1093. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_sales_return.manufacturer")
  1094. 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")
  1095. }
  1096. if startTime > 0 {
  1097. db = db.Where("xt_drug_sales_return.opera_time >=?", startTime)
  1098. }
  1099. if endTime > 0 {
  1100. db = db.Where("xt_drug_sales_return.opera_time<= ?", endTime)
  1101. }
  1102. db = db.Count(&total)
  1103. offset := (page - 1) * limit
  1104. err = db.Offset(offset).Limit(limit).Order("xt_drug_sales_return.ctime desc").Find(&list).Error
  1105. return
  1106. }
  1107. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1108. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1109. return list, err
  1110. }
  1111. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  1112. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Preload("WarehousingInfo", "status = 1").Preload("GoodInfo", "status = 1").Find(&list).Error
  1113. return list, err
  1114. }
  1115. type DrugCancelStockInfo struct {
  1116. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1117. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1118. CancelStockId int64 `gorm:"column:cancel_stock_id" json:"cancel_stock_id" form:"cancel_stock_id"`
  1119. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1120. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1121. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1122. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1123. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1124. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1125. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1126. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1127. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1128. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1129. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1130. Dealer string `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1131. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1132. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1133. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1134. Number string `gorm:"column:number" json:"number" form:"number"`
  1135. RegisterAccount string `gorm:"column:register_account" json:"register_account" form:"register_account"`
  1136. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1137. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1138. MaxUnit string `gorm:"column:max_unit" json:"max_unit" form:"max_unit"`
  1139. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1140. BatchNumberId int64 `gorm:"column:batch_number_id" json:"batch_number_id" form:"batch_number_id"`
  1141. }
  1142. func (DrugCancelStockInfo) TableName() string {
  1143. return "xt_drug_cancel_stock_info"
  1144. }
  1145. func FindDrugCancelStockInfoById(id int64, org_id int64) (list []*DrugCancelStockInfo, err error) {
  1146. err = readDb.Model(&DrugCancelStockInfo{}).Preload("Drug", "status = 1 AND org_id = ? ", org_id).Where("cancel_stock_id = ? AND status = 1", id).Find(&list).Error
  1147. return list, err
  1148. }
  1149. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  1150. db := readDb.Model(&models.WarehousingInfo{})
  1151. db = db.Where("org_id = ? AND status = 1", orgId)
  1152. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  1153. if startTime > 0 {
  1154. db = db.Where("ctime >=?", startTime)
  1155. }
  1156. if endTime > 0 {
  1157. db = db.Where("ctime<= ?", endTime)
  1158. }
  1159. db = db.Count(&total)
  1160. offset := (page - 1) * limit
  1161. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1162. return list, total, err
  1163. }
  1164. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  1165. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1166. return total, err
  1167. }
  1168. func FindAllDrugWarehouseOut(org_id int64) (total int64, err error) {
  1169. err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  1170. return total, err
  1171. }
  1172. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut) error {
  1173. err := writeDb.Create(&warehouseOut).Error
  1174. return err
  1175. }
  1176. func AddSigleDrugWarehouseOut(warehouseOut *models.DrugWarehouseOut) error {
  1177. err := writeDb.Create(&warehouseOut).Error
  1178. return err
  1179. }
  1180. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  1181. if len(warehouseOutInfo) > 0 {
  1182. utx := writeDb.Begin()
  1183. if len(warehouseOutInfo) > 0 {
  1184. 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 "
  1185. insertParams := make([]string, 0)
  1186. insertData := make([]interface{}, 0)
  1187. for _, info := range warehouseOutInfo {
  1188. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1189. insertData = append(insertData, info.WarehouseOutId)
  1190. insertData = append(insertData, info.GoodId)
  1191. insertData = append(insertData, info.GoodTypeId)
  1192. insertData = append(insertData, info.ProductDate)
  1193. insertData = append(insertData, info.ExpiryDate)
  1194. insertData = append(insertData, info.Count)
  1195. insertData = append(insertData, info.Price)
  1196. insertData = append(insertData, info.TotalPrice)
  1197. insertData = append(insertData, info.Remark)
  1198. insertData = append(insertData, info.Ctime)
  1199. insertData = append(insertData, info.Status)
  1200. insertData = append(insertData, info.OrgId)
  1201. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1202. insertData = append(insertData, info.Type)
  1203. insertData = append(insertData, info.Dealer)
  1204. insertData = append(insertData, info.Manufacturer)
  1205. insertData = append(insertData, info.Number)
  1206. insertData = append(insertData, info.LicenseNumber)
  1207. }
  1208. thisSQL += strings.Join(insertParams, ", ")
  1209. err = utx.Exec(thisSQL, insertData...).Error
  1210. if err != nil {
  1211. utx.Rollback()
  1212. return
  1213. }
  1214. }
  1215. utx.Commit()
  1216. }
  1217. return
  1218. }
  1219. func CreateDrugWarehousingOutInfo(warehouseOutInfo []*models.DrugWarehouseOutInfo) (err error) {
  1220. if len(warehouseOutInfo) > 0 {
  1221. utx := writeDb.Begin()
  1222. if len(warehouseOutInfo) > 0 {
  1223. 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 "
  1224. insertParams := make([]string, 0)
  1225. insertData := make([]interface{}, 0)
  1226. for _, info := range warehouseOutInfo {
  1227. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1228. insertData = append(insertData, info.WarehouseOutId)
  1229. insertData = append(insertData, info.DrugId)
  1230. insertData = append(insertData, info.ProductDate)
  1231. insertData = append(insertData, info.ExpiryDate)
  1232. insertData = append(insertData, info.Count)
  1233. insertData = append(insertData, info.Price)
  1234. insertData = append(insertData, info.TotalPrice)
  1235. insertData = append(insertData, info.Remark)
  1236. insertData = append(insertData, info.Ctime)
  1237. insertData = append(insertData, info.Status)
  1238. insertData = append(insertData, info.OrgId)
  1239. insertData = append(insertData, info.WarehouseOutOrderNumber)
  1240. insertData = append(insertData, info.Type)
  1241. insertData = append(insertData, info.Dealer)
  1242. insertData = append(insertData, info.Manufacturer)
  1243. insertData = append(insertData, info.RetailPrice)
  1244. insertData = append(insertData, info.RetailTotalPrice)
  1245. insertData = append(insertData, info.Number)
  1246. insertData = append(insertData, info.BatchNumber)
  1247. insertData = append(insertData, info.CountUnit)
  1248. }
  1249. thisSQL += strings.Join(insertParams, ", ")
  1250. err = utx.Exec(thisSQL, insertData...).Error
  1251. if err != nil {
  1252. utx.Rollback()
  1253. return
  1254. }
  1255. }
  1256. utx.Commit()
  1257. }
  1258. return
  1259. }
  1260. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  1261. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1262. return info, err
  1263. }
  1264. func FindLastDrugWarehousingOutInfo(order string) (info models.DrugWarehouseOutInfo, err error) {
  1265. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  1266. return info, err
  1267. }
  1268. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  1269. db := readDb.Model(&models.WarehouseOut{})
  1270. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1271. if len(keywords) > 0 {
  1272. likeKey := "%" + keywords + "%"
  1273. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1274. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  1275. 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")
  1276. }
  1277. if startTime > 0 {
  1278. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1279. }
  1280. if endTime > 0 {
  1281. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1282. }
  1283. db = db.Count(&total)
  1284. offset := (page - 1) * limit
  1285. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1286. return list, total, err
  1287. }
  1288. func FindAllWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64) (list []*models.WarehouseOut, total int64, err error) {
  1289. db := readDb.Model(&models.WarehouseOut{})
  1290. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  1291. if len(keywords) > 0 {
  1292. likeKey := "%" + keywords + "%"
  1293. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  1294. 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")
  1295. }
  1296. if startTime > 0 {
  1297. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  1298. }
  1299. if endTime > 0 {
  1300. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  1301. }
  1302. db = db.Count(&total)
  1303. offset := (page - 1) * limit
  1304. if orgId == 10191 {
  1305. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1306. } else {
  1307. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  1308. }
  1309. return list, total, err
  1310. }
  1311. func FindAllDrugWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugWarehouseOut, total int64, err error) {
  1312. db := readDb.Model(&models.DrugWarehouseOut{})
  1313. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1314. if len(keywords) > 0 {
  1315. likeKey := "%" + keywords + "%"
  1316. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1317. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_warehouse_out.manufacturer")
  1318. 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")
  1319. }
  1320. if startTime > 0 {
  1321. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1322. }
  1323. if endTime > 0 {
  1324. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1325. }
  1326. db = db.Count(&total)
  1327. offset := (page - 1) * limit
  1328. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1329. return list, total, err
  1330. }
  1331. func FindAllDrugWarehouseOutListOne(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string, ids []int64) (list []*models.DrugWarehouseOut, total int64, err error) {
  1332. db := readDb.Model(&models.DrugWarehouseOut{})
  1333. db = db.Where("xt_drug_warehouse_out.org_id = ? AND xt_drug_warehouse_out.status = 1 ", orgId)
  1334. if len(keywords) > 0 {
  1335. likeKey := "%" + keywords + "%"
  1336. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_warehouse_out.creater")
  1337. 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")
  1338. }
  1339. if startTime > 0 {
  1340. db = db.Where("xt_drug_warehouse_out.ctime >=?", startTime)
  1341. }
  1342. if endTime > 0 {
  1343. db = db.Where("xt_drug_warehouse_out.ctime<= ?", endTime)
  1344. }
  1345. db = db.Count(&total)
  1346. offset := (page - 1) * limit
  1347. if orgId == 10191 {
  1348. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.warehouse_out_time desc").Find(&list).Error
  1349. } else {
  1350. err = db.Offset(offset).Limit(limit).Order("xt_drug_warehouse_out.ctime desc").Find(&list).Error
  1351. }
  1352. return list, total, err
  1353. }
  1354. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  1355. 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
  1356. return list, err
  1357. }
  1358. type DrugWarehouseOutInfo struct {
  1359. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1360. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  1361. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1362. WarehousingOutTarget int64 `gorm:"column:warehousing_out_target" json:"warehousing_out_target" form:"warehousing_out_target"`
  1363. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1364. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  1365. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1366. TotalPrice float64 `gorm:"column:total_price" json:"total_price" form:"total_price"`
  1367. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1368. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1369. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1370. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1371. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1372. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1373. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  1374. IsCancel int64 `gorm:"column:is_cancel" json:"is_cancel" form:"is_cancel"`
  1375. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  1376. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1377. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1378. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1379. IsSys int64 `gorm:"column:is_sys" json:"is_sys" form:"is_sys"`
  1380. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time" form:"sys_record_time"`
  1381. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1382. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1383. Number string `gorm:"column:number" json:"number" form:"number"`
  1384. BatchNumber string `gorm:"column:batch_number" json:"batch_number" form:"batch_number"`
  1385. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1386. WarehouseInfoId int64 `gorm:"column:warehouse_info_id" json:"warehouse_info_id" form:"warehouse_info_id"`
  1387. }
  1388. func (DrugWarehouseOutInfo) TableName() string {
  1389. return "xt_drug_warehouse_out_info"
  1390. }
  1391. func FindDrugWarehouseOutInfoById(id int64, org_id int64) (list []*DrugWarehouseOutInfo, err error) {
  1392. err = readDb.Model(&DrugWarehouseOutInfo{}).Preload("Drug", "status = 1 AND org_id = ?", org_id).Where("warehouse_out_id = ? AND status = 1 ", id).Find(&list).Error
  1393. return list, err
  1394. }
  1395. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  1396. info := &models.WarehousingInfo{}
  1397. type WarehouseInfoCountAmount struct {
  1398. Total int64
  1399. }
  1400. var result WarehouseInfoCountAmount
  1401. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  1402. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  1403. return result.Total, info
  1404. }
  1405. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  1406. type SalesReturnCountAmount struct {
  1407. Total int64
  1408. }
  1409. var result SalesReturnCountAmount
  1410. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1411. return result.Total, err
  1412. }
  1413. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  1414. type WarehouseOutInfoCountAmount struct {
  1415. Total int64
  1416. }
  1417. var result WarehouseOutInfoCountAmount
  1418. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1419. return result.Total, err
  1420. }
  1421. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  1422. type CancleStockoCountAmount struct {
  1423. Total int64
  1424. }
  1425. var result CancleStockoCountAmount
  1426. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  1427. return result.Total, err
  1428. }
  1429. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  1430. info := models.WarehousingInfo{}
  1431. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  1432. return info, err
  1433. }
  1434. func GetCancelStockDetailByOrderNumber(ordernumber string, orgid int64) (*models.CancelStock, error) {
  1435. stock := models.CancelStock{}
  1436. err := XTReadDB().Where("order_number = ? and org_id = ? and status = 1", ordernumber, orgid).Find(&stock).Error
  1437. if err == gorm.ErrRecordNotFound {
  1438. return nil, err
  1439. }
  1440. if err != nil {
  1441. return nil, err
  1442. }
  1443. return &stock, nil
  1444. }
  1445. func GetCancelStockDetailByOrderNumberOne(return_time int64, orgid int64) (*models.CancelStock, error) {
  1446. stock := models.CancelStock{}
  1447. err := XTReadDB().Where("return_time = ? and org_id = ? and status = 1", return_time, orgid).Find(&stock).Error
  1448. if err == gorm.ErrRecordNotFound {
  1449. return nil, err
  1450. }
  1451. if err != nil {
  1452. return nil, err
  1453. }
  1454. return &stock, nil
  1455. }
  1456. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  1457. err := writeDb.Create(&cancelStock).Error
  1458. return err
  1459. }
  1460. func GetLastCancelStockById(orgid int64) (models.CancelStock, error) {
  1461. stock := models.CancelStock{}
  1462. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1463. return stock, err
  1464. }
  1465. func GetLastDrugCancelStockById(orgid int64) (models.DrugCancelStock, error) {
  1466. stock := models.DrugCancelStock{}
  1467. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Last(&stock).Error
  1468. return stock, err
  1469. }
  1470. func GetManufactureById(id int64) (models.Manufacturer, error) {
  1471. manufacturer := models.Manufacturer{}
  1472. err := XTReadDB().Model(&manufacturer).Where("id = ? and status = 1", id).Find(&manufacturer).Error
  1473. return manufacturer, err
  1474. }
  1475. func GetDealerById(id int64) (models.Dealer, error) {
  1476. dealer := models.Dealer{}
  1477. err := XTReadDB().Model(&dealer).Where("id=? and status = 1", id).Find(&dealer).Error
  1478. return dealer, err
  1479. }
  1480. func AddSigleDrugCancelStock(cancelStock *models.DrugCancelStock) error {
  1481. err := writeDb.Create(&cancelStock).Error
  1482. return err
  1483. }
  1484. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  1485. if len(cancelStockInfo) > 0 {
  1486. utx := writeDb.Begin()
  1487. if len(cancelStockInfo) > 0 {
  1488. 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) VALUES "
  1489. insertParams := make([]string, 0)
  1490. insertData := make([]interface{}, 0)
  1491. for _, info := range cancelStockInfo {
  1492. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1493. insertData = append(insertData, info.GoodId)
  1494. insertData = append(insertData, info.CancelStockId)
  1495. insertData = append(insertData, info.GoodTypeId)
  1496. insertData = append(insertData, info.Count)
  1497. insertData = append(insertData, info.Price)
  1498. insertData = append(insertData, info.Total)
  1499. insertData = append(insertData, info.Ctime)
  1500. insertData = append(insertData, info.Status)
  1501. insertData = append(insertData, info.OrgId)
  1502. insertData = append(insertData, info.OrderNumber)
  1503. insertData = append(insertData, info.Type)
  1504. insertData = append(insertData, info.Dealer)
  1505. insertData = append(insertData, info.Manufacturer)
  1506. insertData = append(insertData, info.Number)
  1507. insertData = append(insertData, info.RegisterAccount)
  1508. insertData = append(insertData, info.Remark)
  1509. insertData = append(insertData, info.ProductDate)
  1510. insertData = append(insertData, info.ExpiryDate)
  1511. insertData = append(insertData, info.WarehouseInfoId)
  1512. }
  1513. thisSQL += strings.Join(insertParams, ", ")
  1514. err = utx.Exec(thisSQL, insertData...).Error
  1515. if err != nil {
  1516. utx.Rollback()
  1517. return
  1518. }
  1519. }
  1520. utx.Commit()
  1521. }
  1522. return
  1523. }
  1524. func CreateCancelStockInfoTwo(info *models.DrugCancelStockInfo) error {
  1525. err := XTWriteDB().Create(&info).Error
  1526. return err
  1527. }
  1528. func GetLastStockInfoThree(drugid int64) (models.DrugCancelStockInfo, error) {
  1529. info := models.DrugCancelStockInfo{}
  1530. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Find(&info).Error
  1531. return info, err
  1532. }
  1533. func CreateCancelStockInfoOne(info *models.CancelStockInfo) error {
  1534. err := XTWriteDB().Create(&info).Error
  1535. return err
  1536. }
  1537. func GetLastCancelStockInfoByGoodId(goodId int64) (models.CancelStockInfo, error) {
  1538. info := models.CancelStockInfo{}
  1539. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodId).Last(&info).Error
  1540. return info, err
  1541. }
  1542. func GetLastDrugCancelStockInfoById(drugid int64) (models.DrugCancelStockInfo, error) {
  1543. info := models.DrugCancelStockInfo{}
  1544. err := XTReadDB().Model(&info).Where("drug_id = ? and status = 1", drugid).Last(&info).Error
  1545. return info, err
  1546. }
  1547. func CreateDrugCancelStockInfo(cancelStockInfo []*models.DrugCancelStockInfo) (err error) {
  1548. if len(cancelStockInfo) > 0 {
  1549. utx := writeDb.Begin()
  1550. if len(cancelStockInfo) > 0 {
  1551. 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 "
  1552. insertParams := make([]string, 0)
  1553. insertData := make([]interface{}, 0)
  1554. for _, info := range cancelStockInfo {
  1555. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1556. insertData = append(insertData, info.DrugId)
  1557. insertData = append(insertData, info.CancelStockId)
  1558. insertData = append(insertData, info.Count)
  1559. insertData = append(insertData, info.Price)
  1560. insertData = append(insertData, info.Total)
  1561. insertData = append(insertData, info.Ctime)
  1562. insertData = append(insertData, info.Status)
  1563. insertData = append(insertData, info.OrgId)
  1564. insertData = append(insertData, info.OrderNumber)
  1565. insertData = append(insertData, info.Type)
  1566. insertData = append(insertData, info.Dealer)
  1567. insertData = append(insertData, info.Manufacturer)
  1568. insertData = append(insertData, info.RetailPrice)
  1569. insertData = append(insertData, info.RetailTotalPrice)
  1570. insertData = append(insertData, info.RegisterAccount)
  1571. insertData = append(insertData, info.BatchNumber)
  1572. insertData = append(insertData, info.Remark)
  1573. insertData = append(insertData, info.MaxUnit)
  1574. insertData = append(insertData, info.ProductDate)
  1575. insertData = append(insertData, info.ExpiryDate)
  1576. insertData = append(insertData, info.BatchNumberId)
  1577. }
  1578. thisSQL += strings.Join(insertParams, ", ")
  1579. err = utx.Exec(thisSQL, insertData...).Error
  1580. if err != nil {
  1581. utx.Rollback()
  1582. return
  1583. }
  1584. }
  1585. utx.Commit()
  1586. }
  1587. return
  1588. }
  1589. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  1590. db := readDb.Model(&models.CancelStock{})
  1591. db = db.Where("org_id = ? AND status = 1", orgId)
  1592. err = db.Order("ctime desc").Find(&list).Error
  1593. return
  1594. }
  1595. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  1596. db := readDb.Model(&models.WarehousingInfo{})
  1597. db = db.Where("org_id = ? AND status = 1", orgId)
  1598. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  1599. if startTime > 0 {
  1600. db = db.Where("ctime >=?", startTime)
  1601. }
  1602. if endTime > 0 {
  1603. db = db.Where("ctime<= ?", endTime)
  1604. }
  1605. db = db.Count(&total)
  1606. offset := (page - 1) * limit
  1607. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1608. return
  1609. }
  1610. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string, startime int64, endtime int64, type_name int64) (list []*models.StockInfo, total int64, err error) {
  1611. db := XTReadDB().Model(&models.StockInfo{})
  1612. if startime > 0 {
  1613. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1614. return db.Where("org_id = ? AND status = 1 and sys_record_time>=?", orgId, startime)
  1615. })
  1616. }
  1617. if startime == 0 || endtime == 0 {
  1618. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1619. return db.Where("org_id = ? AND status = 1", orgId)
  1620. })
  1621. }
  1622. if endtime > 0 {
  1623. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1624. return db.Where("org_id = ? AND status = 1 and sys_record_time >=? and sys_record_time<=?", orgId, startime, endtime)
  1625. })
  1626. }
  1627. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  1628. db = db.Group("xt_good_information.id")
  1629. if startime == 0 || endtime == 0 {
  1630. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1631. return db.Where("org_id = ? AND status = 1", orgId)
  1632. })
  1633. }
  1634. if startime > 0 {
  1635. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1636. return db.Where("org_id = ? AND status = 1 and ctime >= ?", orgId, startime)
  1637. })
  1638. }
  1639. if endtime > 0 {
  1640. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  1641. return db.Where("org_id = ? AND status = 1 and ctime>=? and ctime <= ?", orgId, startime, endtime)
  1642. })
  1643. }
  1644. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  1645. if startime == 0 || endtime == 0 {
  1646. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  1647. }
  1648. if startime > 0 {
  1649. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=?", orgId, startime)
  1650. }
  1651. if endtime > 0 {
  1652. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1 and ctime >=? and ctime<=?", orgId, startime, endtime)
  1653. }
  1654. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  1655. if len(keyword) > 0 {
  1656. likeKey := "%" + keyword + "%"
  1657. 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)
  1658. 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")
  1659. }
  1660. if type_name > 0 {
  1661. 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)
  1662. }
  1663. db = db.Count(&total)
  1664. offset := (page - 1) * limit
  1665. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1666. return
  1667. }
  1668. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  1669. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  1670. return
  1671. }
  1672. func FindWarehouseOutInfoTotalByGoodId(goodId int64) (total int64, err error) {
  1673. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  1674. return
  1675. }
  1676. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  1677. 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
  1678. return
  1679. }
  1680. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  1681. 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
  1682. return
  1683. }
  1684. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  1685. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  1686. "good_id": warehouseInfo.GoodId,
  1687. "number": warehouseInfo.Number,
  1688. "product_date": warehouseInfo.ProductDate,
  1689. "expiry_date": warehouseInfo.ExpiryDate,
  1690. "warehousing_count": warehouseInfo.WarehousingCount,
  1691. "price": warehouseInfo.Price,
  1692. "total_price": warehouseInfo.TotalPrice,
  1693. "mtime": warehouseInfo.Mtime,
  1694. "remark": warehouseInfo.Remark,
  1695. }).Error
  1696. return
  1697. }
  1698. func UpDateWarehouseStatus(id int64) (err error) {
  1699. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1700. return
  1701. }
  1702. func UpdatedStockFlowById(id int64) (err error) {
  1703. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehousing_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1704. return
  1705. }
  1706. func UpDateDrugWarehouseStatus(id int64) (err error) {
  1707. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1708. return
  1709. }
  1710. func UpdateDrugFlowById(id int64) (err error) {
  1711. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehousing_detail_id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1712. return
  1713. }
  1714. func DeleteWarehouse(ids []string) (err error) {
  1715. ut := writeDb.Begin()
  1716. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1717. if err != nil {
  1718. ut.Rollback()
  1719. return
  1720. }
  1721. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1722. if err != nil {
  1723. ut.Rollback()
  1724. return
  1725. }
  1726. ut.Commit()
  1727. return
  1728. }
  1729. func DeleteStockFlow(ids []string) (err error) {
  1730. ut := writeDb.Begin()
  1731. err = ut.Model(&models.VmStockFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1732. if err != nil {
  1733. ut.Rollback()
  1734. return
  1735. }
  1736. ut.Commit()
  1737. return
  1738. }
  1739. func DeleteDrugWarehouse(ids []string) (err error) {
  1740. ut := writeDb.Begin()
  1741. err = ut.Model(&models.DrugWarehouse{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1742. if err != nil {
  1743. ut.Rollback()
  1744. return
  1745. }
  1746. err = ut.Model(&models.DrugWarehouseInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1747. if err != nil {
  1748. ut.Rollback()
  1749. return
  1750. }
  1751. err = ut.Model(&models.DrugFlow{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1752. if err != nil {
  1753. ut.Rollback()
  1754. return
  1755. }
  1756. ut.Commit()
  1757. return
  1758. }
  1759. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  1760. warehouse := &models.Warehousing{}
  1761. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  1762. return warehouse, err
  1763. }
  1764. func FindWareHouseOutById(id int64) (*models.WarehouseOut, error) {
  1765. warehouseOut := &models.WarehouseOut{}
  1766. err := readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  1767. return warehouseOut, err
  1768. }
  1769. func FindDrugWareHouseOutById(id int64, org_id int64) (*models.DrugWarehouseOut, error) {
  1770. warehouseOut := &models.DrugWarehouseOut{}
  1771. 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
  1772. return warehouseOut, err
  1773. }
  1774. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  1775. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  1776. return
  1777. }
  1778. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  1779. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  1780. return
  1781. }
  1782. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  1783. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1784. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1785. return
  1786. }
  1787. func DeleteStockInRecordByReocordId(id int64) (err error) {
  1788. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1789. return
  1790. }
  1791. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  1792. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  1793. if manufacturer_id > 0 {
  1794. db = db.Where("manufacturer = ?", manufacturer_id)
  1795. }
  1796. if dealer_id > 0 {
  1797. db = db.Where("dealer = ?", dealer_id)
  1798. }
  1799. err = db.Preload("GoodsType", "status = 1 AND org_id = ?", org_id).Find(&goodInfo).Error
  1800. return goodInfo, err
  1801. }
  1802. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  1803. db := readDb.Model(&models.Warehousing{})
  1804. likekey := "%" + keywords + "%"
  1805. db = db.Where("org_id = ? AND status = 1 ", orgId)
  1806. db = db.Where("warehousing_order LIKE ? ", likekey)
  1807. db = db.Count(&total)
  1808. offset := (page - 1) * limit
  1809. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  1810. return
  1811. }
  1812. func GetLastWarehousingById(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  1813. info := models.WarehousingInfo{}
  1814. err = XTReadDB().Model(&info).Where("warehousing_id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  1815. return info, err
  1816. }
  1817. func GetLastWarehousingByIdOne(warehousing_id int64, orgid int64) (models.WarehousingInfo, error) {
  1818. info := models.WarehousingInfo{}
  1819. err = XTReadDB().Model(&info).Where("id = ? and good_id = ? and status = 1", warehousing_id, orgid).Find(&info).Error
  1820. return info, err
  1821. }
  1822. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  1823. err = writeDb.Save(&info).Error
  1824. return err
  1825. }
  1826. func UpDateDrugWarehousingInfo(info *models.DrugWarehouseInfo) (err error) {
  1827. err = writeDb.Save(&info).Error
  1828. return err
  1829. }
  1830. func UpDateDrugWarehousingInfoTwo(id int64, info *models.DrugWarehouseInfo) (err error) {
  1831. 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}).Error
  1832. return err
  1833. }
  1834. func GetDrugWarehouseOrderOne(id int64, drug_id int64) (models.DrugWarehouseInfo, error) {
  1835. info := models.DrugWarehouseInfo{}
  1836. err = XTReadDB().Model(&info).Where("warehousing_id = ? and drug_id = ?", id, drug_id).Find(&info).Error
  1837. return info, err
  1838. }
  1839. func EditWarehousing(warehouse models.Warehousing) {
  1840. 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}).Error
  1841. }
  1842. func EditDrugWarehousing(warehouse models.DrugWarehouse) {
  1843. 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
  1844. }
  1845. func FindAllWarehouse(org_id int64, types int64) (list []*models.Warehousing, err error) {
  1846. 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
  1847. return list, err
  1848. }
  1849. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  1850. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  1851. 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)
  1852. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  1853. return list, err
  1854. }
  1855. func DeleteSalesReturn(ids []string) (err error) {
  1856. ut := writeDb.Begin()
  1857. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1858. if err != nil {
  1859. ut.Rollback()
  1860. return
  1861. }
  1862. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1863. if err != nil {
  1864. ut.Rollback()
  1865. return
  1866. }
  1867. ut.Commit()
  1868. return
  1869. }
  1870. func DeleteDrugSalesReturn(ids []string) (err error) {
  1871. ut := writeDb.Begin()
  1872. err = ut.Model(&models.DrugSalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1873. if err != nil {
  1874. ut.Rollback()
  1875. return
  1876. }
  1877. err = ut.Model(&models.DrugSalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1878. if err != nil {
  1879. ut.Rollback()
  1880. return
  1881. }
  1882. ut.Commit()
  1883. return
  1884. }
  1885. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  1886. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  1887. return
  1888. }
  1889. type DrugSalesReturnInfo struct {
  1890. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1891. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  1892. SalesReturnId int64 `gorm:"column:sales_return_id" json:"sales_return_id" form:"sales_return_id"`
  1893. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1894. Price float64 `gorm:"column:price" json:"price" form:"price"`
  1895. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1896. ProductDate int64 `gorm:"column:product_date" json:"product_date" form:"product_date"`
  1897. ExpiryDate int64 `gorm:"column:expiry_date" json:"expiry_date" form:"expiry_date"`
  1898. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1899. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1900. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1901. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1902. OrderNumber string `gorm:"column:order_number" json:"order_number" form:"order_number"`
  1903. Type int64 `gorm:"column:type" json:"type" form:"type"`
  1904. Dealer int64 `gorm:"column:dealer" json:"dealer" form:"dealer"`
  1905. Manufacturer int64 `gorm:"column:manufacturer" json:"manufacturer" form:"manufacturer"`
  1906. RetailPrice float64 `gorm:"column:retail_price" json:"retail_price" form:"retail_price"`
  1907. RetailTotalPrice float64 `gorm:"column:retail_total_price" json:"retail_total_price" form:"retail_total_price"`
  1908. Number string `gorm:"column:number" json:"number" form:"number"`
  1909. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  1910. }
  1911. func (DrugSalesReturnInfo) TableName() string {
  1912. return "xt_drug_sales_return_info"
  1913. }
  1914. func FindAllDrugSalesReturnInfoById(id int64, org_id int64) (list []*DrugSalesReturnInfo, err error) {
  1915. 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
  1916. return
  1917. }
  1918. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  1919. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1920. return
  1921. }
  1922. func FindAllDrugSalesReturnById(id int64, org_id int64) (salesReturn models.DrugSalesReturn, err error) {
  1923. 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
  1924. return
  1925. }
  1926. func UpDateSaleReturnStatus(id int64) (err error) {
  1927. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1928. return
  1929. }
  1930. func UpDateDrugSaleReturnStatus(id int64) (err error) {
  1931. err = readDb.Model(&models.DrugSalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1932. return
  1933. }
  1934. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  1935. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1936. return salesReturn, err
  1937. }
  1938. func FindDrugSalesReturnById(id int64) (salesReturn models.DrugSalesReturn, err error) {
  1939. err = readDb.Model(&models.DrugSalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  1940. return salesReturn, err
  1941. }
  1942. func EditSaleReturn(sales models.SalesReturn) {
  1943. 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
  1944. }
  1945. func EditDrugSaleReturn(sales models.DrugSalesReturn) {
  1946. 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
  1947. }
  1948. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  1949. err = writeDb.Save(&info).Error
  1950. return err
  1951. }
  1952. func UpDateDrugSalesReturnInfo(info *models.DrugSalesReturnInfo) (err error) {
  1953. err = writeDb.Save(&info).Error
  1954. return err
  1955. }
  1956. func DeleteWarehouseOut(ids []string) (err error) {
  1957. ut := writeDb.Begin()
  1958. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1959. if err != nil {
  1960. ut.Rollback()
  1961. return
  1962. }
  1963. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1964. if err != nil {
  1965. ut.Rollback()
  1966. return
  1967. }
  1968. err = ut.Model(&models.VmStockFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1969. if err != nil {
  1970. ut.Rollback()
  1971. return
  1972. }
  1973. ut.Commit()
  1974. return
  1975. }
  1976. func GetWarehouseOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  1977. err = XTReadDB().Where("warehouse_out_id in(?) and status = 0", ids).Find(&info).Error
  1978. return info, err
  1979. }
  1980. func UpdateWarehouseInfoById(id int64, count int64) error {
  1981. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  1982. return err
  1983. }
  1984. func GetDrugWarhouseOutByIds(ids []string) (info []*models.DrugWarehouseOutInfo, err error) {
  1985. err = XTReadDB().Where("warehouse_out_id in(?) and status = 0", ids).Find(&info).Error
  1986. return info, err
  1987. }
  1988. func UpdateDrugInfoByIds(id int64, count int64) error {
  1989. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  1990. return err
  1991. }
  1992. func UpdateDrugInfoByIdsOne(id int64, count int64) error {
  1993. err = XTWriteDB().Model(&models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  1994. return err
  1995. }
  1996. func DeleteDrugWarehouseOut(ids []string) (err error) {
  1997. ut := writeDb.Begin()
  1998. err = ut.Model(&models.DrugWarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1999. if err != nil {
  2000. ut.Rollback()
  2001. return
  2002. }
  2003. err = ut.Model(&models.DrugWarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2004. if err != nil {
  2005. ut.Rollback()
  2006. return
  2007. }
  2008. err = ut.Model(&models.DrugFlow{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2009. if err != nil {
  2010. ut.Rollback()
  2011. return
  2012. }
  2013. ut.Commit()
  2014. return
  2015. }
  2016. func UpDateWarehouseOutStatus(id int64) (err error) {
  2017. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2018. return
  2019. }
  2020. func UpdateStockFlowWarehouseOutById(id int64) (err error) {
  2021. err = XTWriteDB().Model(&models.VmStockFlow{}).Where("warehouse_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2022. return err
  2023. }
  2024. func UpDateDrugWarehouseInfoByStock(info *models.XtDrugWarehouseInfo) (err error) {
  2025. ut := writeDb.Begin()
  2026. err = ut.Save(&info).Error
  2027. if err != nil {
  2028. ut.Rollback()
  2029. return err
  2030. }
  2031. ut.Commit()
  2032. return err
  2033. }
  2034. func UpDateWarehouseInfoByStock(info *models.WarehousingInfo) (err error) {
  2035. ut := writeDb.Begin()
  2036. err = ut.Save(&info).Error
  2037. if err != nil {
  2038. ut.Rollback()
  2039. return err
  2040. }
  2041. ut.Commit()
  2042. return err
  2043. }
  2044. func UpDateDrugWarehouseOutStatus(id int64) (err error) {
  2045. err = readDb.Model(&models.DrugWarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2046. return
  2047. }
  2048. func DeleteDrugWarehouseOutInfoById(id int64) (err error) {
  2049. err = XTWriteDB().Model(&models.DrugFlow{}).Where("warehouse_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2050. return
  2051. }
  2052. func GetDrugWarehosueOutInfo(id int64) (models.DrugWarehouseOutInfo, error) {
  2053. info := models.DrugWarehouseOutInfo{}
  2054. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  2055. return info, err
  2056. }
  2057. func EditWarehouseOut(warehouseOut models.WarehouseOut) {
  2058. 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}).Error
  2059. }
  2060. func EditDrugWarehouseOut(warehouseOut models.DrugWarehouseOut) {
  2061. 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}).Error
  2062. }
  2063. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo) (err error) {
  2064. err = writeDb.Save(&info).Error
  2065. return err
  2066. }
  2067. func UpDateDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) (err error) {
  2068. err = writeDb.Save(&info).Error
  2069. return err
  2070. }
  2071. func UpdateDrugFlowSix(warehousoutid int64, drugid int64, warehouse_out_order_number string, flow models.DrugFlow) error {
  2072. 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}).Error
  2073. return err
  2074. }
  2075. func FindAllCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.CancelStock, total int64, err error) {
  2076. db := readDb.Model(&models.CancelStock{})
  2077. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  2078. if len(keywords) > 0 {
  2079. likeKey := "%" + keywords + "%"
  2080. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2081. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  2082. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_cancel_stock.id")
  2083. }
  2084. if startTime > 0 {
  2085. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  2086. }
  2087. if endTime > 0 {
  2088. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  2089. }
  2090. db = db.Count(&total)
  2091. offset := (page - 1) * limit
  2092. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  2093. return
  2094. }
  2095. func FindAllDrugCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.DrugCancelStock, total int64, err error) {
  2096. db := readDb.Model(&models.DrugCancelStock{})
  2097. db = db.Where("xt_drug_cancel_stock.org_id = ? AND xt_drug_cancel_stock.status = 1", orgId)
  2098. if len(keywords) > 0 {
  2099. likeKey := "%" + keywords + "%"
  2100. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_drug_cancel_stock.creater")
  2101. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_drug_cancel_stock.manufacturer")
  2102. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_drug_cancel_stock.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_drug_cancel_stock.id")
  2103. }
  2104. if startTime > 0 {
  2105. db = db.Where("xt_drug_cancel_stock.opera_time >=?", startTime)
  2106. }
  2107. if endTime > 0 {
  2108. db = db.Where("xt_drug_cancel_stock.opera_time<= ?", endTime)
  2109. }
  2110. db = db.Count(&total)
  2111. offset := (page - 1) * limit
  2112. err = db.Offset(offset).Limit(limit).Order("xt_drug_cancel_stock.ctime desc").Find(&list).Error
  2113. return
  2114. }
  2115. func GetCancelWarehosueInfo(ids []string) (info []*models.CancelStockInfo, err error) {
  2116. err = XTReadDB().Where("id IN(?) and status = 1", ids).Find(&info).Error
  2117. return info, err
  2118. }
  2119. func DeleteCancelStock(ids []string) (err error) {
  2120. ut := writeDb.Begin()
  2121. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2122. if err != nil {
  2123. ut.Rollback()
  2124. return
  2125. }
  2126. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2127. if err != nil {
  2128. ut.Rollback()
  2129. return
  2130. }
  2131. err = ut.Model(&models.VmStockFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2132. if err != nil {
  2133. ut.Rollback()
  2134. return
  2135. }
  2136. ut.Commit()
  2137. return
  2138. }
  2139. func GetDrugWareInfoByCancelId(ids []string) (info []*models.DrugCancelStockInfo, err error) {
  2140. err = XTReadDB().Where("cancel_stock_id IN(?) and status = 1", ids).Find(&info).Error
  2141. return info, err
  2142. }
  2143. func DeleteDrugCancelStock(ids []string) (err error) {
  2144. ut := writeDb.Begin()
  2145. err = ut.Model(&models.DrugCancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2146. if err != nil {
  2147. ut.Rollback()
  2148. return
  2149. }
  2150. err = ut.Model(&models.DrugCancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2151. if err != nil {
  2152. ut.Rollback()
  2153. return
  2154. }
  2155. err = ut.Model(&models.DrugFlow{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2156. if err != nil {
  2157. ut.Rollback()
  2158. return
  2159. }
  2160. ut.Commit()
  2161. return
  2162. }
  2163. func UpDateCancleStockStatus(id int64) (err error) {
  2164. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2165. return
  2166. }
  2167. func GetCancelStockById(id int64) (models.CancelStockInfo, error) {
  2168. info := models.CancelStockInfo{}
  2169. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  2170. return info, err
  2171. }
  2172. func ModefyWarehouseInfoNight(info models.WarehousingInfo, id int64) error {
  2173. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", info.StockCount)).Error
  2174. return err
  2175. }
  2176. func UpdatedStockFlowByCancelId(id int64, good_id int64) (err error) {
  2177. 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
  2178. return
  2179. }
  2180. func UpDateDrugCancleStockStatus(id int64) (err error) {
  2181. err = readDb.Model(&models.DrugCancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  2182. return
  2183. }
  2184. func UpDateDrugCancelStockById(id int64) (err error) {
  2185. err = XTWriteDB().Model(&models.DrugFlow{}).Where("cancel_out_detail_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  2186. return
  2187. }
  2188. func EditCancelStock(sales models.CancelStock) {
  2189. 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}).Error
  2190. }
  2191. func EditDrugCancelStock(sales models.DrugCancelStock) {
  2192. 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}).Error
  2193. }
  2194. func FindCancelStockById(id int64, org_id int64) (cancelStock models.CancelStock, err error) {
  2195. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2196. return cancelStock, err
  2197. }
  2198. func FindCancelDrugById(id int64, org_id int64) (cancelStock models.DrugCancelStock, err error) {
  2199. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2200. return cancelStock, err
  2201. }
  2202. func FindDrugCancelStockById(id int64) (cancelStock models.DrugCancelStock, err error) {
  2203. err = readDb.Model(&models.DrugCancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  2204. return cancelStock, err
  2205. }
  2206. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  2207. err = writeDb.Save(&info).Error
  2208. return err
  2209. }
  2210. func UpDateDrugCancelStockInfo(info *models.DrugCancelStockInfo) (err error) {
  2211. err = writeDb.Save(&info).Error
  2212. return err
  2213. }
  2214. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  2215. 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
  2216. return list, err
  2217. }
  2218. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  2219. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  2220. 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)
  2221. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  2222. return list, err
  2223. }
  2224. type StockDetailModel struct {
  2225. GoodId int64 `gorm:"good_id" json:"good_id"`
  2226. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  2227. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  2228. Price float64 `gorm:"price" json:"price"`
  2229. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  2230. Type int `gorm:"type" json:"type"`
  2231. Creater int64 `gorm:"creater" json:"creater"`
  2232. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  2233. Types int64 `gorm:"types" json:"types"`
  2234. }
  2235. func FindStockInDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.WarehousingInfo, total int64, err error) {
  2236. db := readDb.Model(&models.WarehousingInfo{})
  2237. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2238. if len(keywords) > 0 {
  2239. likeKey := "%" + keywords + "%"
  2240. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2241. 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)
  2242. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2243. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2244. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse.manufacturer")
  2245. //db = db.Where(" sgj_user_admin_role.user_name LIKE ?", likeKey).Group("xt_warehouse_info.id")
  2246. 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")
  2247. } else {
  2248. 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)
  2249. }
  2250. if manufacturer > 0 {
  2251. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2252. }
  2253. if order_type > 0 {
  2254. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2255. }
  2256. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  2257. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2258. db = db.Count(&total)
  2259. offset := (page - 1) * limit
  2260. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.ctime desc").Find(&list).Error
  2261. return
  2262. }
  2263. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (err error, totalPrice float64) {
  2264. db := readDb.Model(&models.WarehousingInfo{})
  2265. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  2266. if len(keywords) > 0 {
  2267. likeKey := "%" + keywords + "%"
  2268. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  2269. 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)
  2270. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  2271. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  2272. 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")
  2273. } else {
  2274. 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)
  2275. }
  2276. if manufacturer > 0 {
  2277. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  2278. }
  2279. if order_type > 0 {
  2280. db = db.Where("xt_warehouse_info.type = ?", order_type)
  2281. }
  2282. type PriceAmount struct {
  2283. Total float64
  2284. }
  2285. var result []PriceAmount
  2286. err = db.Select("total_price as total").Scan(&result).Error
  2287. var total float64
  2288. for _, value := range result {
  2289. total = total + value.Total
  2290. }
  2291. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2292. return err, total
  2293. }
  2294. func FloatRound(f float64, n int) float64 {
  2295. format := "%." + strconv.Itoa(n) + "f"
  2296. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  2297. return res
  2298. }
  2299. func FindStockOutDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  2300. db := readDb.Model(&models.WarehouseOutInfo{})
  2301. 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)
  2302. if len(keywords) > 0 {
  2303. likeKey := "%" + keywords + "%"
  2304. 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)
  2305. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2306. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2307. 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")
  2308. } else {
  2309. 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)
  2310. }
  2311. if manufacturer > 0 {
  2312. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2313. }
  2314. if order_type > 0 {
  2315. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2316. }
  2317. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  2318. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2319. db = db.Count(&total)
  2320. offset := (page - 1) * limit
  2321. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.ctime desc").Find(&list).Error
  2322. return
  2323. }
  2324. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (err error, totalPrice float64) {
  2325. db := readDb.Model(&models.WarehouseOutInfo{})
  2326. 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)
  2327. if len(keywords) > 0 {
  2328. likeKey := "%" + keywords + "%"
  2329. 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)
  2330. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  2331. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  2332. 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")
  2333. } else {
  2334. 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)
  2335. }
  2336. if manufacturer > 0 {
  2337. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  2338. }
  2339. if order_type > 0 {
  2340. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  2341. }
  2342. type PriceAmount struct {
  2343. Total float64
  2344. }
  2345. var result []PriceAmount
  2346. err = db.Select("(count * price) as total").Scan(&result).Error
  2347. var total float64
  2348. for _, value := range result {
  2349. total = total + value.Total
  2350. }
  2351. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  2352. fmt.Println(total)
  2353. return err, total
  2354. }
  2355. 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) {
  2356. db := readDb.Model(&models.SalesReturnInfo{})
  2357. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  2358. if len(keywords) > 0 {
  2359. likeKey := "%" + keywords + "%"
  2360. 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)
  2361. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  2362. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  2363. 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")
  2364. } else {
  2365. 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)
  2366. }
  2367. if manufacturer > 0 {
  2368. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  2369. }
  2370. if order_type > 0 {
  2371. db = db.Where("xt_sales_return_info.type = ?", order_type)
  2372. }
  2373. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  2374. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2375. offset := (page - 1) * limit
  2376. db = db.Count(&total)
  2377. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  2378. return
  2379. }
  2380. func FindeCancelGroup(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.CancelStockInfo, err error) {
  2381. db := readDb.Model(&models.CancelStockInfo{})
  2382. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2383. if len(keywords) > 0 {
  2384. likeKey := "%" + keywords + "%"
  2385. 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)
  2386. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2387. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2388. 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")
  2389. } else {
  2390. 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)
  2391. }
  2392. if manufacturer > 0 {
  2393. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2394. }
  2395. if order_type > 0 {
  2396. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2397. }
  2398. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2399. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2400. err = db.Group("xt_cancel_stock_info.good_id").Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  2401. return
  2402. }
  2403. func FindCancelDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.CancelStockInfo, total int64, err error) {
  2404. db := readDb.Model(&models.CancelStockInfo{})
  2405. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  2406. if len(keywords) > 0 {
  2407. likeKey := "%" + keywords + "%"
  2408. 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)
  2409. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  2410. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  2411. 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")
  2412. } else {
  2413. 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)
  2414. }
  2415. if manufacturer > 0 {
  2416. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  2417. }
  2418. if order_type > 0 {
  2419. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  2420. }
  2421. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  2422. db = db.Preload("GoodInfo", "status = 1 AND org_id = ?", orgId)
  2423. db = db.Count(&total)
  2424. offset := (page - 1) * limit
  2425. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  2426. return
  2427. }
  2428. func FindStockInByDealerId(id int64) (total int64, err error) {
  2429. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  2430. return
  2431. }
  2432. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  2433. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  2434. return
  2435. }
  2436. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  2437. readDb.Model(&models.GoodsType{}).Where("org_id = ? AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  2438. return
  2439. }
  2440. func FindGoodInfoByName(name string, org_id int64, good_type_id int64) (total int64) {
  2441. 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)
  2442. return
  2443. }
  2444. func FindGoodInfoByNameTwo(name string, org_id int64, good_type_id int64, good_name string) (total int64) {
  2445. 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)
  2446. return
  2447. }
  2448. func FindGoodInfoByNameOne(name string, org_id int64, good_type_id int64, id int64, goodname string) (total int64) {
  2449. 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)
  2450. return
  2451. }
  2452. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  2453. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  2454. return goodType, err
  2455. }
  2456. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  2457. 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
  2458. return info, err
  2459. }
  2460. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  2461. 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
  2462. return
  2463. }
  2464. func FindDrugStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.DrugWarehouseOut, err error) {
  2465. 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
  2466. return
  2467. }
  2468. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  2469. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).First(&in).Error
  2470. return
  2471. }
  2472. func FindLastDrugStockInInfoRecord(drug_id int64, org_id int64) (in models.DrugWarehouseOutInfo, err error) {
  2473. err = readDb.Model(&models.DrugWarehouseInfo{}).Where("status = 1 AND org_id = ? AND drug_id = ?", org_id, drug_id).Last(&in).Error
  2474. return
  2475. }
  2476. func GetStockInRecoredByGoodId(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2477. info := models.WarehouseOutInfo{}
  2478. 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
  2479. if err == gorm.ErrRecordNotFound {
  2480. return nil, err
  2481. }
  2482. if err != nil {
  2483. return nil, err
  2484. }
  2485. return &info, nil
  2486. }
  2487. func GetStockInRecoredByGoodIdOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64) (*models.WarehouseOutInfo, error) {
  2488. info := models.WarehouseOutInfo{}
  2489. 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
  2490. if err == gorm.ErrRecordNotFound {
  2491. return nil, err
  2492. }
  2493. if err != nil {
  2494. return nil, err
  2495. }
  2496. return &info, nil
  2497. }
  2498. func UpdateAutoMaticReduceDetail(goodid int64, goodtypeid int64, recordtime int64, patientid int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2499. 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}).Error
  2500. return err
  2501. }
  2502. func UpdateAutoMaticReduceDetailOne(goodid int64, goodtypeid int64, recordtime int64, orgid int64, prepare *models.WarehouseOutInfo) error {
  2503. 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
  2504. return err
  2505. }
  2506. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo) error {
  2507. err := writeDb.Create(&info).Error
  2508. return err
  2509. }
  2510. func UpdateStockOut(warehouseinfoid int64, info models.WarehousingInfo) error {
  2511. err := XTWriteDB().Model(&info).Where("id=? and status = 1", warehouseinfoid).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  2512. return err
  2513. }
  2514. func AddSigleDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo) error {
  2515. ut := writeDb.Begin()
  2516. err := ut.Create(&info).Error
  2517. if err != nil {
  2518. ut.Rollback()
  2519. return err
  2520. }
  2521. ut.Commit()
  2522. return err
  2523. }
  2524. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  2525. ut := writeDb.Begin()
  2526. err := ut.Create(&detail).Error
  2527. if err != nil {
  2528. ut.Rollback()
  2529. return err
  2530. }
  2531. ut.Commit()
  2532. return err
  2533. }
  2534. func AddSigleDrugAutoReduceRecordInfo(detail *models.DrugAutomaticReduceDetail) error {
  2535. err := writeDb.Create(&detail).Error
  2536. return err
  2537. }
  2538. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, out_id int64, number string) (out models.WarehouseOutInfo, err error) {
  2539. 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
  2540. return
  2541. }
  2542. func FindStockOutInfoByStock(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (out []*models.WarehouseOutInfo, err error) {
  2543. 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
  2544. return
  2545. }
  2546. func FindStockOutInfoByStockOne(org_id int64, good_type_id int64, good_id int64, record_date int64) (out []*models.WarehouseOutInfo, err error) {
  2547. 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
  2548. return
  2549. }
  2550. func FindStockOutInfoByStockTwo(org_id int64, good_type_id int64, good_id int64, record_date int64, patient_id int64) (models.WarehouseOutInfo, error) {
  2551. info := models.WarehouseOutInfo{}
  2552. 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
  2553. return info, err
  2554. }
  2555. func FindDrugStockOutInfoByTypeId(org_id int64, drug_id int64, out_id int64, number string) (out models.DrugWarehouseOutInfo, err error) {
  2556. 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
  2557. return
  2558. }
  2559. func UpdateStockOutInfoCount(org_id int64, id int64) {
  2560. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  2561. }
  2562. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  2563. db := readDb.Model(&models.AutomaticReduceDetail{})
  2564. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  2565. db = db.Preload("GoodsType", "status = 1")
  2566. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  2567. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0 AND count <> 0 ", org_id, id)
  2568. db = db.Count(&total)
  2569. err = db.Find(&user).Error
  2570. return
  2571. }
  2572. type DrugAutomaticReduceDetail struct {
  2573. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2574. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id" form:"warehouse_out_id"`
  2575. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number" form:"warehouse_out_order_number"`
  2576. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  2577. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2578. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2579. Status int64 `gorm:"column:status" json:"status" form:"status"`
  2580. RecordTime int64 `gorm:"column:record_time" json:"record_time" form:"record_time"`
  2581. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  2582. DrugId int64 `gorm:"column:drug_id" json:"drug_id" form:"drug_id"`
  2583. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2584. CountUnit string `gorm:"column:count_unit" json:"count_unit" form:"count_unit"`
  2585. Drug *Drug `gorm:"ForeignKey:DrugId;AssociationForeignKey:ID" json:"drug"`
  2586. Patients *models.Patients `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"user"`
  2587. DrugWarehouseOutInfo []*models.DrugWarehouseOutInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"drugwarehouseoutinfo"`
  2588. }
  2589. func (DrugAutomaticReduceDetail) TableName() string {
  2590. return "xt_drug_automatic_reduce_detail"
  2591. }
  2592. func FindDrugStockUserDetailById(org_id int64, id int64) (user []*DrugAutomaticReduceDetail, err error, total int64) {
  2593. db := readDb.Model(&DrugAutomaticReduceDetail{})
  2594. db = db.Preload("Drug", "org_id = ? AND status = 1", org_id)
  2595. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  2596. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND drug_id != 0", org_id, id)
  2597. db = db.Count(&total)
  2598. err = db.Find(&user).Error
  2599. return
  2600. }
  2601. func UpdateStockOutInfoCount2(org_id int64, id int64, count int64) {
  2602. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  2603. }
  2604. func UpdateDrugStockOutInfoCount2(org_id int64, id int64, count int64) {
  2605. writeDb.Model(&models.DrugWarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", count))
  2606. }
  2607. func FindPatientAutomaticReduceRecord(org_id int64, record_time int64, good_id int64, good_type_id int64, patient_id int64) (count int64, err error) {
  2608. 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
  2609. return
  2610. }
  2611. func FindPatientDrugAutomaticReduceRecord(org_id int64, record_time int64, drug_id int64, patient_id int64) (count int64, err error) {
  2612. 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
  2613. return
  2614. }
  2615. type VMWarehousingInfo struct {
  2616. ID int64 `gorm:"column:id" json:"id"`
  2617. WarehousingId int64 `gorm:"column:warehousing_id" json:"warehousing_id"`
  2618. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  2619. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  2620. Status int64 `gorm:"column:status" json:"status"`
  2621. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2622. GoodInfo GoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  2623. }
  2624. func (VMWarehousingInfo) TableName() string {
  2625. return "xt_warehouse_info"
  2626. }
  2627. type GoodInfo struct {
  2628. ID int64 `gorm:"column:id" json:"id"`
  2629. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  2630. Status int64 `gorm:"column:status" json:"status"`
  2631. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2632. }
  2633. func (GoodInfo) TableName() string {
  2634. return "xt_good_information"
  2635. }
  2636. func GetStockInGoodInfo(orgId int64) (stockIn []*VMWarehousingInfo, err error) {
  2637. err = readDb.Model(&VMWarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", orgId).Where("org_id = ? AND status = 1", orgId).Find(&stockIn).Error
  2638. return
  2639. }
  2640. func GetAllStockOutUserDetail(patient_id int64, orgId int64, record_time int64) (goodUser []*models.DialysisBeforePrepare, err error) {
  2641. 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
  2642. return
  2643. }
  2644. func PostSearchStock(keyword string, orgid int64) (goods []*models.GoodsType, err error) {
  2645. likeKey := "%" + keyword + "%"
  2646. db := XTReadDB().Model(&goods)
  2647. if len(keyword) > 0 {
  2648. db = db.Where("type_name like ?", likeKey)
  2649. }
  2650. err = db.Where("org_id = ? and status =1", orgid).Find(&goods).Error
  2651. return goods, err
  2652. }
  2653. func GetStockType(orgid int64) (goodstype []*models.GoodsTypeOne, err error) {
  2654. err = XTReadDB().Where("org_id = ? and status = 1 and (stock_attribute = 2 or stock_attribute = 3)", orgid).
  2655. Preload("GoodInfo", "org_id = ? and status = 1", orgid).Find(&goodstype).Error
  2656. return goodstype, err
  2657. }
  2658. func GetDialyStockOut(orgid int64, recordtime int64, patientId int64) (prepare []*models.XtDialysisBeforePrepare, err error) {
  2659. db := XTReadDB().Table("dialysis_before_prepare as x").Where("x.status = 1")
  2660. if orgid > 0 {
  2661. db = db.Where("x.user_org_id = ?", orgid)
  2662. }
  2663. if recordtime > 0 {
  2664. db = db.Where("x.record_date = ?", recordtime)
  2665. }
  2666. if patientId > 0 {
  2667. db = db.Where("x.patient_id = ?", patientId)
  2668. }
  2669. 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
  2670. return prepare, err
  2671. }
  2672. func GetAutoReduceRecordInfoById(orgid int64, recordtime int64, goodid int64, goodtypeid int64) (*models.XtAutomaticReduceDetail, error) {
  2673. detail := models.XtAutomaticReduceDetail{}
  2674. 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
  2675. if err == gorm.ErrRecordNotFound {
  2676. return nil, err
  2677. }
  2678. if err != nil {
  2679. return nil, err
  2680. }
  2681. return &detail, nil
  2682. }
  2683. func GetOutStockTotalCount(wareouttime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  2684. 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
  2685. return autoMatic, err
  2686. }
  2687. func GetOutStockTotalCountOne(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  2688. 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
  2689. return autoMatic, err
  2690. }
  2691. func GetManufacturerName(orgid int64, name string) (*models.Manufacturer, error) {
  2692. var manufacturer models.Manufacturer
  2693. var err error
  2694. err = XTReadDB().Model(&manufacturer).Where("org_id = ? and manufacturer_name = ? and status = 1", orgid, name).Find(&manufacturer).Error
  2695. if err == gorm.ErrRecordNotFound {
  2696. return nil, err
  2697. }
  2698. if err != nil {
  2699. return nil, err
  2700. }
  2701. return &manufacturer, nil
  2702. }
  2703. func CreateManufacturer(manufacturer *models.Manufacturer) error {
  2704. err := XTWriteDB().Create(manufacturer).Error
  2705. return err
  2706. }
  2707. func UpdatedManufactur(manufacturer *models.Manufacturer, orgid int64) error {
  2708. err := XTWriteDB().Model(&manufacturer).Where("org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"manufacturer_name": manufacturer.ManufacturerName}).Error
  2709. return err
  2710. }
  2711. func GetGoodsInformaitonIsExist(goodName string, goodType int64, specificationName string, orgid int64) (*models.GoodInfo, error) {
  2712. var good models.GoodInfo
  2713. var err error
  2714. 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
  2715. if err == gorm.ErrRecordNotFound {
  2716. return nil, err
  2717. }
  2718. if err != nil {
  2719. return nil, err
  2720. }
  2721. return &good, nil
  2722. }
  2723. func CreateGoodsInfomation(info *models.GoodInfo) error {
  2724. err := XTWriteDB().Create(&info).Error
  2725. return err
  2726. }
  2727. func UpdateGoodsInformation(info *models.GoodInfo, goodName string, goodType int64, specificationName string, orgid int64) error {
  2728. 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
  2729. return err
  2730. }
  2731. func GetCoutWareseOutInfo(startime int64, endtime int64, orgid int64) (houseOutInfo []*models.VmWarehouseOutInfo, err error) {
  2732. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  2733. 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
  2734. return houseOutInfo, err
  2735. }
  2736. func GetGoodInfomationList(orgid int64, keyword string) (goodInfo []*models.GoodInfo, err error) {
  2737. key := "%" + keyword + "%"
  2738. if len(keyword) == 0 {
  2739. err = XTReadDB().Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodInfo).Error
  2740. } else {
  2741. err = XTReadDB().Where("org_id = ? and status = 1 AND good_name like ? AND find_in_set('停用',good_status) = 0", orgid, key).Find(&goodInfo).Error
  2742. }
  2743. return goodInfo, err
  2744. }
  2745. func IsExistDrugByName(drugname string, drugdesc string, orgid int64) (*models.BaseDrugLib, error) {
  2746. var drug models.BaseDrugLib
  2747. var err error
  2748. err = XTReadDB().Model(&drug).Where("drug_name = ? and drug_spec = ? and status =1 and org_id = ?", drugname, drugdesc, orgid).Find(&drug).Error
  2749. if err == gorm.ErrRecordNotFound {
  2750. return nil, err
  2751. }
  2752. if err != nil {
  2753. return nil, err
  2754. }
  2755. return &drug, nil
  2756. }
  2757. func IsExistDrugByNameOne(drugname string, dosage float64, doseunit string, minnumber int64, minunit string, packtunit string, orgid int64) (*models.BaseDrugLib, error) {
  2758. var drug models.BaseDrugLib
  2759. var err error
  2760. 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
  2761. if err == gorm.ErrRecordNotFound {
  2762. return nil, err
  2763. }
  2764. if err != nil {
  2765. return nil, err
  2766. }
  2767. return &drug, nil
  2768. }
  2769. func CreateDrugsInfomation(info *models.BaseDrugLib) error {
  2770. err := XTWriteDB().Create(&info).Error
  2771. return err
  2772. }
  2773. func UpdateDrugsInformation(drug *models.BaseDrugLib, drugName string, drugSpec string, orgid int64) error {
  2774. 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
  2775. return err
  2776. }
  2777. func GetProjectByNameIsExsit(projectName string, orgid int64) (*models.HisProject, error) {
  2778. var projoect models.HisProject
  2779. var err error
  2780. err = XTReadDB().Model(&projoect).Where("project_name = ? and user_org_id = ? and status =1", projectName, orgid).Find(&projoect).Error
  2781. if err == gorm.ErrRecordNotFound {
  2782. return nil, err
  2783. }
  2784. if err != nil {
  2785. return nil, err
  2786. }
  2787. return &projoect, nil
  2788. }
  2789. func CreateProjectInfomation(info *models.HisProject) error {
  2790. err := XTWriteDB().Create(&info).Error
  2791. return err
  2792. }
  2793. func UpdateProjectByName(project *models.HisProject, projectName string, orgid int64) error {
  2794. 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
  2795. return err
  2796. }
  2797. func GetWareOutInfoById(warehouseoutid int64, orgid int64) (warehouseinfo []*models.WarehouseOutInfo, err error) {
  2798. err = XTReadDB().Model(&warehouseinfo).Where("warehouse_out_id = ? and org_id = ? and status= 1 and is_sys = 0", warehouseoutid, orgid).Find(&warehouseinfo).Error
  2799. return warehouseinfo, err
  2800. }
  2801. func EditDrugWarehousingOne(warehouse models.DrugWarehouse, id int64) {
  2802. err = readDb.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}).Error
  2803. }
  2804. func GetAllManufacturerList(orgid int64) (list []*models.Manufacturer, err error) {
  2805. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  2806. return list, err
  2807. }
  2808. func GetAllDealerList(orgid int64) (list []*models.Dealer, err error) {
  2809. err = XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&list).Error
  2810. return list, err
  2811. }
  2812. func GetDealerByName(orgid int64, name string) (*models.Dealer, error) {
  2813. var dealer models.Dealer
  2814. var err error
  2815. err = XTReadDB().Model(&dealer).Where("org_id = ? and dealer_name = ? and status = 1", orgid, name).Find(&dealer).Error
  2816. if err == gorm.ErrRecordNotFound {
  2817. return nil, err
  2818. }
  2819. if err != nil {
  2820. return nil, err
  2821. }
  2822. return &dealer, nil
  2823. }
  2824. func CreateDealer(dealer *models.Dealer) error {
  2825. err := XTWriteDB().Create(&dealer).Error
  2826. return err
  2827. }
  2828. func UpdateDealer(dealer *models.Dealer, orgid int64) error {
  2829. err := XTWriteDB().Model(&dealer).Where("org_id = ? and status =1", orgid).Updates(map[string]interface{}{"dealer_name": dealer.DealerName}).Error
  2830. return err
  2831. }
  2832. func GetDrugDataConfig(orgid int64, name string) (models.DictDataconfig, error) {
  2833. dataconfig := models.DictDataconfig{}
  2834. err := XTReadDB().Where("org_id = ? and name = ?", orgid, name).Find(&dataconfig).Error
  2835. return dataconfig, err
  2836. }
  2837. func GetParentDataConfig(parentid int64, orgid int64) (dataconfig []*models.DictDataconfig, err error) {
  2838. err = XTReadDB().Where("parent_id = ? and status =1 and (org_id = ? or org_id = 0)", parentid, orgid).Find(&dataconfig).Error
  2839. return dataconfig, err
  2840. }
  2841. func IsExistDicConfig(parentid int64, goodKind string, orgid int64) (*models.DictDataconfig, error) {
  2842. var dataconfig models.DictDataconfig
  2843. var err error
  2844. err = XTReadDB().Where("parent_id = ? and status = 1 and name = ? and (org_id = ? or org_id = 0)", parentid, goodKind, orgid).Find(&dataconfig).Error
  2845. if err == gorm.ErrRecordNotFound {
  2846. return nil, err
  2847. }
  2848. if err != nil {
  2849. return nil, err
  2850. }
  2851. return &dataconfig, nil
  2852. }
  2853. func GetLastDicConfig(parentid int64, orgid int64) (models.DictDataconfig, error) {
  2854. dataconfig := models.DictDataconfig{}
  2855. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status = 1", parentid, orgid).Last(&dataconfig).Error
  2856. return dataconfig, err
  2857. }
  2858. func CreatedDicConfig(dataconfig *models.DictDataconfig) error {
  2859. err := XTWriteDB().Create(&dataconfig).Error
  2860. return err
  2861. }
  2862. func UpdatedDicConfig(parentid int64, dataconfig *models.DictDataconfig, orgid int64, goodKind string) error {
  2863. err := XTWriteDB().Model(&dataconfig).Where("parent_id = ? and org_id = ? and name = ?", parentid, orgid, goodKind).Updates(map[string]interface{}{"name": dataconfig.Name}).Error
  2864. return err
  2865. }
  2866. func GetGoodType(goodName string, orgid int64) (*models.GoodsType, error) {
  2867. var goodType models.GoodsType
  2868. var err error
  2869. err = XTReadDB().Where("type_name =? and org_id = ? and status = 1", goodName, orgid).Find(&goodType).Error
  2870. if err == gorm.ErrRecordNotFound {
  2871. return nil, err
  2872. }
  2873. if err != nil {
  2874. return nil, err
  2875. }
  2876. return &goodType, nil
  2877. }
  2878. func CreatedGoodType(goodsType *models.GoodsType) error {
  2879. err := XTWriteDB().Create(&goodsType).Error
  2880. return err
  2881. }
  2882. func GetAllGoodType(orgid int64) (goodsType []*models.GoodsType, err error) {
  2883. err = XTReadDB().Model(&goodsType).Where("org_id = ? and status = 1", orgid).Find(&goodsType).Error
  2884. return goodsType, err
  2885. }
  2886. func GetDepartMentByName(name string, orgid int64) (*models.XtHisDepartment, error) {
  2887. var department models.XtHisDepartment
  2888. var err error
  2889. err = XTReadDB().Where("name= ? and user_org_id = ? and status = 1", name, orgid).Find(&department).Error
  2890. if err == gorm.ErrRecordNotFound {
  2891. return nil, err
  2892. }
  2893. if err != nil {
  2894. return nil, err
  2895. }
  2896. return &department, nil
  2897. }
  2898. func GetDataConfigIsExist(orgid int64, name string) (models.Dataconfig, error) {
  2899. dataconfig := models.Dataconfig{}
  2900. err := XTReadDB().Model(&dataconfig).Where("org_id = ? and name = ? and status = 1", orgid, name).Find(&dataconfig).Error
  2901. return dataconfig, err
  2902. }
  2903. func GetChildeConfigIsExist(parentid int64, name string, orgid int64) (*models.Dataconfig, error) {
  2904. var dataconfig models.Dataconfig
  2905. var err error
  2906. 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
  2907. if err == gorm.ErrRecordNotFound {
  2908. return nil, err
  2909. }
  2910. if err != nil {
  2911. return nil, err
  2912. }
  2913. return &dataconfig, nil
  2914. }
  2915. func GetLastChildeConfig(parentid int64, orgid int64) (models.Dataconfig, error) {
  2916. dataconfig := models.Dataconfig{}
  2917. err := XTReadDB().Model(&dataconfig).Where("parent_id = ? and (org_id = ? or org_id = 0) and status =1", parentid, orgid).Last(&dataconfig).Error
  2918. return dataconfig, err
  2919. }
  2920. func CreateDataConfig(dataconfig *models.Dataconfig) error {
  2921. err := XTWriteDB().Create(&dataconfig).Error
  2922. return err
  2923. }
  2924. func FindAllDataConfigList(orgid int64, parentId int64) (dataconfig []*models.Dataconfig, err error) {
  2925. err = XTReadDB().Model(&dataconfig).Where("(org_id = ? or org_id = 0) and parent_id = ? and status = 1", orgid, parentId).Find(&dataconfig).Error
  2926. return dataconfig, err
  2927. }
  2928. func GetWarehouseOrderInfoList(orgid int64) (list []*models.WarehousingInfo, err error) {
  2929. err = XTReadDB().Model(&list).Where("org_id = ? and status = 1", orgid).Order("id asc").Group("good_id").Find(&list).Error
  2930. return list, err
  2931. }
  2932. func GetSearchDrugList(keywords string, orgid int64) (drug []*models.BaseDrugLib, err error) {
  2933. db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and drug_status<>'停用'", orgid)
  2934. if len(keywords) > 0 {
  2935. likekey := "%" + keywords + "%"
  2936. err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  2937. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  2938. }).Find(&drug).Error
  2939. } else {
  2940. err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
  2941. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid).Group("id")
  2942. }).Find(&drug).Error
  2943. }
  2944. return
  2945. }
  2946. func GetSearchGoodList(keywords string, orgid int64) (good []*models.GoodInfo, err error) {
  2947. db := XTReadDB().Model(&good).Where("org_id =? and status = 1 and good_status <> '停用'", orgid)
  2948. if len(keywords) > 0 {
  2949. likekey := "%" + keywords + "%"
  2950. err = db.Where("(good_name Like ?)", likekey).Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2951. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  2952. }).Find(&good).Error
  2953. } else {
  2954. err = db.Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  2955. return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0", orgid)
  2956. }).Find(&good).Error
  2957. }
  2958. return
  2959. }
  2960. func CreateAutoReduceRecord(detail *models.BloodAutomaticReduceDetail) error {
  2961. db := writeDb.Begin()
  2962. err := db.Create(&detail).Error
  2963. if err != nil {
  2964. db.Rollback()
  2965. return err
  2966. }
  2967. db.Commit()
  2968. return err
  2969. }
  2970. func GetAutoMaticReduceDetail(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) (*models.BloodAutomaticReduceDetail, error) {
  2971. var autoreduece models.BloodAutomaticReduceDetail
  2972. var err error
  2973. 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
  2974. if err == gorm.ErrRecordNotFound {
  2975. return nil, err
  2976. }
  2977. if err != nil {
  2978. return nil, err
  2979. }
  2980. return &autoreduece, nil
  2981. }
  2982. func DeleteAutoRedeceDetailTwo(orgid int64, patient_id int64, recordtime int64, goodid int64, goodtypeid int64) error {
  2983. detail := models.BloodAutomaticReduceDetail{}
  2984. 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
  2985. return err
  2986. }
  2987. func GetWarehosueOutByStockFlow(patient_id int64, recrod_date int64, goodid int64) (stockflow []*models.VmStockFlow, err error) {
  2988. 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
  2989. return stockflow, err
  2990. }
  2991. func DeleteAutoRedeceDetailTen(orgid int64, patient_id int64, recordtime int64, goodid int64) error {
  2992. detail := models.BloodAutomaticReduceDetail{}
  2993. 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
  2994. info := models.WarehouseOutInfo{}
  2995. err = XTWriteDB().Model(&info).Where("org_id = ? and patient_id = ? and sys_record_time = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  2996. prepare := models.DialysisBeforePrepare{}
  2997. 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
  2998. flow := models.VmStockFlow{}
  2999. err = XTWriteDB().Model(&flow).Where("user_org_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", orgid, patient_id, recordtime, goodid).Updates(map[string]interface{}{"status": 0, "count": 0}).Error
  3000. return err
  3001. }
  3002. func UpDateWarehouseInfoByStockDelete(id int64, count int64) (err error) {
  3003. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3004. return err
  3005. }
  3006. func GetCancelStockInfoByPatientId(patientid int64, goodid int64, record_date int64) (info []*models.CancelStockInfo, err error) {
  3007. err = XTReadDB().Where("patient_id = ? and good_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&info).Error
  3008. return info, err
  3009. }
  3010. func GetHisPrepscritionByPatientId(patientid int64, goodid int64, record_date int64) (list []*models.HisPrescriptionProject, err error) {
  3011. err = XTReadDB().Where("patient_id = ? and project_id = ? and record_date = ? and status = 1", patientid, goodid, record_date).Find(&list).Error
  3012. return list, err
  3013. }
  3014. func UpDateWarehouStockFlowByStockDelete(id int64, recordtime int64, good_id int64, count int64, patient_id int64) (err error) {
  3015. 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
  3016. return err
  3017. }
  3018. func UpDateWarehouseInfoByStockDeleteOne(id int64, count int64) (err error) {
  3019. err = writeDb.Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  3020. return err
  3021. }
  3022. func GetAutoDialysisBefor(goodid int64, goodtypeid int64, patientid int64, orgid int64, recorddate int64) (*models.BloodAutomaticReduceDetail, error) {
  3023. var autoreduece models.BloodAutomaticReduceDetail
  3024. var err error
  3025. 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
  3026. if err == gorm.ErrRecordNotFound {
  3027. return nil, err
  3028. }
  3029. if err != nil {
  3030. return nil, err
  3031. }
  3032. return &autoreduece, nil
  3033. }
  3034. func GetAutoRecordByGoodIdSeven(goodid int64, patientid int64, recorddate int64) (models.WarehouseOutInfoSeven, error) {
  3035. detail := models.WarehouseOutInfoSeven{}
  3036. err := XTReadDB().Select("id,count").Where(" good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3037. return detail, err
  3038. }
  3039. func GetAutoRecordByGoodIdSevenEight(goodid int64, patientid int64, recorddate int64) (detail []*models.WarehouseOutInfoSeven, err error) {
  3040. err = XTReadDB().Select("id,count").Where("good_id = ? and sys_record_time = ? and patient_id = ? and status = 1", goodid, recorddate, patientid).Find(&detail).Error
  3041. return detail, err
  3042. }
  3043. func GetHisProjectPrescriptionByPatientId(patientid int64, recorddate int64) (project []*models.HisPrescriptionProject, err error) {
  3044. err = XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status =1", patientid, recorddate).Find(&project).Error
  3045. return project, err
  3046. }
  3047. func GetHisProjectPrescriptionByPatientIdOne(id int64) (models.HisPrescriptionProject, error) {
  3048. project := models.HisPrescriptionProject{}
  3049. err = XTReadDB().Model(&project).Where("id = ? and status =1", id).Find(&project).Error
  3050. return project, err
  3051. }
  3052. func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.WarehouseOutInfoNight, err error) {
  3053. err = XTReadDB().Select("count,warehouse_info_id").Where(" patient_id = ? and sys_record_time = ? and status = 1 and is_sys = 1 and count>0", patientid, recorddate).Find(&auto).Error
  3054. return auto, err
  3055. }
  3056. //func GetAllAutoRecordByPatient(patientid int64, recorddate int64) (auto []*models.VmStockFlow, err error) {
  3057. //
  3058. // err = XTReadDB().Select("count,warehousing_id").Where(" patient_id = ? and system_time = ? and status = 1 and is_sys = 1 and count>0",patientid,recorddate).Find(&auto).Error
  3059. // return auto, err
  3060. //
  3061. //}
  3062. func ModefyWarehouseInfo(count int64, id int64) error {
  3063. err := XTWriteDB().Model(models.WarehousingInfo{}).Where(" id =? and status = 1", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  3064. return err
  3065. }
  3066. func DeleteAutoWarehouse(patient_id int64, record_time int64) error {
  3067. 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
  3068. 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
  3069. err = XTWriteDB().Model(models.VmStockFlow{}).Where("patient_id = ? and system_time = ? and status = 1", patient_id, record_time).Updates(map[string]interface{}{"status": 0}).Error
  3070. 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
  3071. fmt.Println("err2332332322323322323232323232332", err)
  3072. return err
  3073. }
  3074. func FindGoodInfoByIdTwo(id int64) (goodInfo models.GoodInfo, err error) {
  3075. err = readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3076. return
  3077. }
  3078. func FindGoodInfoByIdSeven(id int64) (goodInfo models.GoodInfoSeven, err error) {
  3079. err = readDb.Select("id,good_name").Where("id = ? AND status = 1", id).First(&goodInfo).Error
  3080. return
  3081. }
  3082. func GetGoodWarehouseInfo(good_id int64) (info []*models.WarehousingInfo, err error) {
  3083. err = readDb.Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3084. return info, err
  3085. }
  3086. func GetGoodWarehouseInfoSeven(good_id int64) (info []*models.WarehousingInfoSeven, err error) {
  3087. err = readDb.Select("stock_count").Where("good_id = ? and status = 1 and stock_count<>0", good_id).Find(&info).Error
  3088. return info, err
  3089. }
  3090. func GetAllStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3091. offset := (page - 1) * limit
  3092. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  3093. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3094. fmt.Println(table)
  3095. if startime > 0 {
  3096. db = db.Where("x.ctime>=?", startime)
  3097. }
  3098. if endtime > 0 {
  3099. db = db.Where("x.ctime<=?", endtime)
  3100. }
  3101. likeKey := "%" + keyword + "%"
  3102. if len(likeKey) > 0 {
  3103. db = db.Where("t.good_name like ?", likeKey)
  3104. }
  3105. if orgid > 0 {
  3106. db = db.Where("x.org_id = ?", orgid)
  3107. }
  3108. if good_type > 0 {
  3109. db = db.Where("t.good_type_id = ?", good_type)
  3110. }
  3111. 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
  3112. return info, total, err
  3113. }
  3114. func GetAllGoodInfoStockList(page int64, limit int64, startime int64, endtime int64, good_type int64, keyword string, orgid int64) (info []*models.GoodInfo, total int64, err error) {
  3115. offset := (page - 1) * limit
  3116. db := XTReadDB().Table("xt_good_information as x").Where("x.status= 1")
  3117. likeKey := "%" + keyword + "%"
  3118. if len(likeKey) > 0 {
  3119. db = db.Where("x.good_name like ?", likeKey)
  3120. }
  3121. if orgid > 0 {
  3122. db = db.Where("x.org_id = ?", orgid)
  3123. }
  3124. if good_type > 0 {
  3125. db = db.Where("x.good_type_id = ?", good_type)
  3126. }
  3127. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Preload("StWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  3128. if startime > 0 {
  3129. db = db.Where("ctime>=?", startime)
  3130. }
  3131. if endtime > 0 {
  3132. db = db.Where("ctime<=?", endtime)
  3133. }
  3134. return db.Where("status = 1")
  3135. }).Preload("CancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3136. if startime > 0 {
  3137. db = db.Where("ctime>=?", startime)
  3138. }
  3139. if endtime > 0 {
  3140. db = db.Where("ctime<=?", endtime)
  3141. }
  3142. return db.Where("status = 1")
  3143. }).Find(&info).Error
  3144. return info, total, err
  3145. }
  3146. func GetStockListById(good_id int64, orgid int64, limit int64, page int64, startime int64, endtime int64) (info []*models.VmWarehousingInfo, total int64, err error) {
  3147. offset := (page - 1) * limit
  3148. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status= 1")
  3149. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3150. fmt.Println(table)
  3151. if startime > 0 {
  3152. db = db.Where("x.ctime>=?", startime)
  3153. }
  3154. if endtime > 0 {
  3155. db = db.Where("x.ctime<=?", endtime)
  3156. }
  3157. if orgid > 0 {
  3158. db = db.Where("x.org_id = ?", orgid)
  3159. }
  3160. if good_id > 0 {
  3161. db = db.Where("x.good_id = ?", good_id)
  3162. }
  3163. 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,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.min_unit,t.buy_price").Joins("left join xt_good_information as t on t.id = x.good_id").Count(&total).Offset(offset).Limit(limit).Scan(&info).Error
  3164. return info, total, err
  3165. }
  3166. 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) {
  3167. offset := (page - 1) * limit
  3168. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status= 1")
  3169. if startime > 0 {
  3170. db = db.Where("x.ctime>=?", startime)
  3171. }
  3172. if endtime > 0 {
  3173. db = db.Where("x.ctime<=?", endtime)
  3174. }
  3175. if orgid > 0 {
  3176. db = db.Where("x.org_id = ?", orgid)
  3177. }
  3178. if good_id > 0 {
  3179. db = db.Where("x.good_id = ?", good_id)
  3180. }
  3181. if is_sys == 2 {
  3182. db = db.Where("x.is_sys = 1")
  3183. }
  3184. if is_sys == 3 {
  3185. db = db.Where("x.is_sys = 0")
  3186. }
  3187. 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
  3188. return info, total, err
  3189. }
  3190. func GetStockDrugCount(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3191. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3192. if startime > 0 {
  3193. db = db.Where("x.ctime >=?", startime)
  3194. }
  3195. if endtime > 0 {
  3196. db = db.Where("x.ctime<=?", endtime)
  3197. }
  3198. if orgid > 0 {
  3199. db = db.Where("x.org_id = ?", orgid)
  3200. }
  3201. 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
  3202. return info, err
  3203. }
  3204. func GetAutoDiallysisBefor(startime int64, endtime int64, orgid int64) (info []*models.VmWarehouseInfo, err error) {
  3205. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3206. if startime > 0 {
  3207. db = db.Where("x.ctime >=?", startime)
  3208. }
  3209. if endtime > 0 {
  3210. db = db.Where("x.ctime<=?", endtime)
  3211. }
  3212. if orgid > 0 {
  3213. db = db.Where("x.org_id = ?", orgid)
  3214. }
  3215. err = db.Select("sum(x.count) as count,x.good_id").Where("is_sys = 0").Group("x.good_id").Scan(&info).Error
  3216. return info, err
  3217. }
  3218. func GetOutStockTotalCountFour(startime int64, endtime int64, orgid int64) (autoMatic []*models.NewXtAutomaticReduceDetail, err error) {
  3219. if endtime > 0 {
  3220. 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
  3221. }
  3222. if endtime == 0 {
  3223. 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
  3224. }
  3225. return autoMatic, err
  3226. }
  3227. func GetOrderDetialByOrderId(id int64, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3228. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3229. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3230. fmt.Println(table)
  3231. if id > 0 {
  3232. db = db.Where("x.warehouse_out_id = ?", id)
  3233. }
  3234. if orgid > 0 {
  3235. db = db.Where("x.org_id = ?", orgid)
  3236. }
  3237. 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,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
  3238. return out, err
  3239. }
  3240. func GetWarehouseOutOrder(id []string, orgid int64) (out []*models.WarehouseOut, err error) {
  3241. db := XTReadDB().Table("xt_warehouse_out as x").Where("x.status = 1")
  3242. if len(id) > 0 {
  3243. db = db.Where("x.id in(?)", id)
  3244. }
  3245. if orgid > 0 {
  3246. db = db.Where("x.org_id= ?", orgid)
  3247. }
  3248. 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
  3249. return out, err
  3250. }
  3251. func GetOrderDetialByOrderIdOne(id []string, orgid int64) (out []*models.WarehouseOutInfoOne, err error) {
  3252. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status = 1")
  3253. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3254. fmt.Println(table)
  3255. if len(id) > 0 {
  3256. db = db.Where("x.warehouse_out_id in (?)", id)
  3257. }
  3258. if orgid > 0 {
  3259. db = db.Where("x.org_id = ?", orgid)
  3260. }
  3261. 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,t.manufacturer,t.social_security_directory_code,x.is_sys,x.sys_record_time,x.number,x.remark,x.license_number,t.good_name,t.good_type_id,t.specification_name,t.min_number,t.packing_unit,t.min_unit,t.packing_price").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
  3262. return out, err
  3263. }
  3264. func GetLastWarehouseOutInfo(ids []string, orgId int64) (info []*models.WarehouseOutInfoSix, err error) {
  3265. db := XTReadDB().Model(&info)
  3266. db = db.Preload("GoodInfo", "status = 1")
  3267. 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
  3268. return info, err
  3269. }
  3270. func FindUserDetailByIdOne(good_id int64, record_time int64, org_id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  3271. db := readDb.Model(&models.AutomaticReduceDetail{})
  3272. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  3273. db = db.Preload("GoodsType", "status = 1")
  3274. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  3275. db = db.Preload("WarehouseOutInfo", "org_id = ? and status = 1 and good_id = ? and sys_record_time = ?", org_id, good_id, record_time)
  3276. db = db.Where("status = 1 AND org_id = ? AND good_id = ? AND record_time = ? and count<> 0 ", org_id, good_id, record_time)
  3277. db = db.Count(&total)
  3278. err = db.Find(&user).Error
  3279. return
  3280. }
  3281. func GetGoodInformationByGoodId(good_id int64) (models.GoodInfo, error) {
  3282. info := models.GoodInfo{}
  3283. err := XTReadDB().Where("id = ? and status = 1", good_id).Find(&info).Error
  3284. return info, err
  3285. }
  3286. func UpdateGoodInfoList(info models.GoodInfo, id int64) error {
  3287. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"total": info.Total}).Error
  3288. return err
  3289. }
  3290. func GetExprotStockList(orgid int64, orderid []string, startime int64, endtime int64) (info []*models.StWarehousingInfo, err error) {
  3291. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3292. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3293. fmt.Println(table)
  3294. if orgid > 0 {
  3295. db = db.Where("x.org_id = ?", orgid)
  3296. }
  3297. if len(orderid) > 0 {
  3298. db = db.Where("x.warehousing_id in(?)", orderid)
  3299. }
  3300. if startime > 0 {
  3301. db = db.Where("x.ctime>=?", startime)
  3302. }
  3303. if endtime > 0 {
  3304. db = db.Where("x.ctime<=?", endtime)
  3305. }
  3306. 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
  3307. return info, err
  3308. }
  3309. func GetDrugWarehouOrderInfo(startime int64, endtime int64, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3310. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3311. table := XTReadDB().Table("xt_base_drug as t").Where("t.status = 1")
  3312. fmt.Println(table)
  3313. if startime > 0 {
  3314. db = db.Where("x.ctime>=?", startime)
  3315. }
  3316. if endtime > 0 {
  3317. db = db.Where("x.ctime<=?", endtime)
  3318. }
  3319. if orgid > 0 {
  3320. db = db.Where("x.org_id = ?", orgid)
  3321. }
  3322. 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
  3323. return info, err
  3324. }
  3325. func GetOutExprotList(orgid int64, ids []string, startime int64, endtime int64) (out []*models.WarehouseOutInfoOne, err error) {
  3326. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1 ")
  3327. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  3328. fmt.Println(table)
  3329. if orgid > 0 {
  3330. db = db.Where("x.org_id = ?", orgid)
  3331. }
  3332. if len(ids) > 0 {
  3333. db = db.Where("x.warehouse_out_id in (?)", ids)
  3334. }
  3335. if startime > 0 {
  3336. db = db.Where("x.ctime>=?", startime)
  3337. }
  3338. if endtime > 0 {
  3339. db = db.Where("x.ctime<=?", endtime)
  3340. }
  3341. 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,t.manufacturer,t.dealer").Joins("left join xt_good_information as t on t.id = x.good_id").Scan(&out).Error
  3342. return out, err
  3343. }
  3344. func FindeDrugWarehouseOutOrder(ids []string, orgid int64) (info []*models.DrugWarehouseOut, err error) {
  3345. db := XTReadDB().Table("xt_drug_warehouse_out as x").Where("x.status = 1")
  3346. if len(ids) > 0 {
  3347. db = db.Where("x.id in(?)", ids)
  3348. }
  3349. if orgid > 0 {
  3350. db = db.Where("x.org_id = ?", orgid)
  3351. }
  3352. 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
  3353. return info, err
  3354. }
  3355. func FindeDrugWarehouseOutDetail(ids []string, orgid int64) (info []*models.VmDrugWarehouseOutInfo, err error) {
  3356. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status =1 ")
  3357. table := XTReadDB().Table("x.base_drug as t").Where("t.status = 1")
  3358. fmt.Println(table)
  3359. if len(ids) > 0 {
  3360. db = db.Where("x.warehouse_out_id in(?)", ids)
  3361. }
  3362. if orgid > 0 {
  3363. db = db.Where("x.org_id = ?", orgid)
  3364. }
  3365. 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
  3366. return info, err
  3367. }
  3368. func GetDrugNameByKeyword(keyword string, orgid int64) (basedrug []*models.XtBaseDrug, err error) {
  3369. db := XTReadDB().Table("xt_base_drug as x").Where("x.status = 1")
  3370. likeKey := "%" + keyword + "%"
  3371. if len(keyword) > 0 {
  3372. db = db.Where("x.drug_name like ?", likeKey)
  3373. }
  3374. if orgid > 0 {
  3375. db = db.Where("x.org_id = ?", orgid)
  3376. }
  3377. err = db.Find(&basedrug).Error
  3378. return basedrug, err
  3379. }
  3380. func GetDrugWarehouseOrderDetail(ids []int64, orgid int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseOutInfo, err error) {
  3381. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3382. if len(ids) > 0 {
  3383. db = db.Where("x.drug_id in(?)", ids)
  3384. }
  3385. if orgid > 0 {
  3386. db = db.Where("x.org_id = ?", orgid)
  3387. }
  3388. if startime > 0 {
  3389. db = db.Where("x.ctime>=?", startime)
  3390. }
  3391. if endtime > 0 {
  3392. db = db.Where("x.ctime<=?", endtime)
  3393. }
  3394. err = db.Find(&info).Error
  3395. return info, err
  3396. }
  3397. func GetDrugWarehouseOrderDetailTwo(ids []int64, orgid int64, startime int64, endtime int64) (info []*models.XtDrugWarehouseInfo, err error) {
  3398. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3399. if len(ids) > 0 {
  3400. db = db.Where("x.drug_id in(?)", ids)
  3401. }
  3402. if orgid > 0 {
  3403. db = db.Where("x.org_id = ?", orgid)
  3404. }
  3405. if startime > 0 {
  3406. db = db.Where("x.ctime>=?", startime)
  3407. }
  3408. if endtime > 0 {
  3409. db = db.Where("x.ctime<=?", endtime)
  3410. }
  3411. err = db.Find(&info).Error
  3412. return info, err
  3413. }
  3414. func GetDrugTotalCount(drugid int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  3415. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3416. if drugid > 0 {
  3417. db = db.Where("x.drug_id = ?", drugid)
  3418. }
  3419. if orgid > 0 {
  3420. db = db.Where("x.org_id = ?", orgid)
  3421. }
  3422. 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
  3423. return info, err
  3424. }
  3425. func GetDrugTotalCountSwx(id int64, orgid int64) (info models.VmDrugWarehouseInfo, err error) {
  3426. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3427. if id > 0 {
  3428. db = db.Where("x.id = ?", id)
  3429. }
  3430. if orgid > 0 {
  3431. db = db.Where("x.org_id = ?", orgid)
  3432. }
  3433. 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
  3434. return info, err
  3435. }
  3436. func GetDrugTotalCountOne(drugid int64, orgid int64, id int64) (models.VmDrugWarehouseInfo, error) {
  3437. info := models.VmDrugWarehouseInfo{}
  3438. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3439. if id == 0 {
  3440. db = db.Where("id = ?", id)
  3441. }
  3442. if drugid > 0 {
  3443. db = db.Where("x.drug_id = ?", drugid)
  3444. }
  3445. if orgid > 0 {
  3446. db = db.Where("x.org_id = ?", orgid)
  3447. }
  3448. err = db.Select("x.drug_id,x.stock_max_number,x.stock_min_number,x.max_unit,x.min_unit").Find(&info).Error
  3449. return info, err
  3450. }
  3451. func GetDrugTotalCountTwo(drugid int64) (list []*models.VmDrugWarehouseInfo, err error) {
  3452. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status = 1")
  3453. table := XTReadDB().Table("xt_base_drug as d").Where("d.status = 1")
  3454. fmt.Println(table)
  3455. if drugid > 0 {
  3456. db = db.Where("x.drug_id = ?", drugid)
  3457. }
  3458. 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
  3459. return list, err
  3460. }
  3461. func GetDrugByGoodId(drugid int64) (models.BaseDrugLib, error) {
  3462. lib := models.BaseDrugLib{}
  3463. err := XTReadDB().Model(&lib).Where("id = ? and status = 1", drugid).Find(&lib).Error
  3464. return lib, err
  3465. }
  3466. func GetWarehoureOrderInfoList(goodid int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  3467. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3468. if goodid > 0 {
  3469. db = db.Where("x.good_id = ?", goodid)
  3470. }
  3471. if orgid > 0 {
  3472. db = db.Where("x.org_id = ?", orgid)
  3473. }
  3474. 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
  3475. return info, err
  3476. }
  3477. func UpdateWarehousingCount(info *models.WarehousingInfo, goodid int64, orgid int64, warehousingId int64) error {
  3478. 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
  3479. return err
  3480. }
  3481. func GetSingleCancelOrder(id int64, orgid int64) (info []*models.CancelStockInfo, err error) {
  3482. err = XTReadDB().Where("cancel_stock_id = ? and status = 1 and org_id = ?", id, orgid).Preload("GoodInfo", "status = 1").Find(&info).Error
  3483. return info, err
  3484. }
  3485. func UpdateCancelOut(list []*models.WarehousingInfo, count int64) error {
  3486. //遍历循环入库数据
  3487. for i, item := range list {
  3488. //计算每个批次剩余库存与入库数据的差 然后和退库数量比较
  3489. if (item.WarehousingCount - item.StockCount) >= count {
  3490. //如果差距大于退库数量,则直接把退库数量增加到该批次中
  3491. info := models.WarehousingInfo{
  3492. GoodId: item.GoodId,
  3493. OrgId: item.OrgId,
  3494. WarehousingId: item.WarehousingId,
  3495. StockCount: item.StockCount + count,
  3496. }
  3497. err2 := UpdateWarehousingCount(&info, item.GoodId, item.OrgId, item.WarehousingId)
  3498. fmt.Println("2323233232323232323232323232323", err2)
  3499. return err
  3500. }
  3501. if (item.WarehousingCount - item.StockCount) < count {
  3502. //如果该批次小于退库数量,则先出库该相差数量
  3503. var countOne = item.WarehousingCount - item.StockCount
  3504. infoOne := models.WarehousingInfo{
  3505. GoodId: item.GoodId,
  3506. OrgId: item.OrgId,
  3507. WarehousingId: item.WarehousingId,
  3508. StockCount: countOne + item.StockCount,
  3509. }
  3510. err := UpdateWarehousingCount(&infoOne, item.GoodId, item.OrgId, item.WarehousingId)
  3511. if err == nil {
  3512. //剩余的出库到第二个批次
  3513. var countTwo = count - countOne
  3514. infos := append(list, list[i+1])
  3515. var idArray []*models.WarehousingInfo
  3516. for _, it := range infos {
  3517. if it.WarehousingId != item.WarehousingId {
  3518. idArray = append(idArray, it)
  3519. }
  3520. }
  3521. //查询更新后的库存
  3522. UpdateCancelOut(idArray, countTwo)
  3523. return err
  3524. } else {
  3525. return err
  3526. }
  3527. }
  3528. }
  3529. return err
  3530. }
  3531. func GetCancelStockOrderPrint(idstr []string) (info []*models.CancelStockInfo, err error) {
  3532. if len(idstr) > 0 {
  3533. err = XTReadDB().Model(&info).Where("cancel_stock_id in(?)", idstr).Preload("GoodInfo", "status = 1").Find(&info).Error
  3534. } else {
  3535. err = XTReadDB().Model(&info).Preload("GoodInfo", "status = 1").Find(&info).Error
  3536. }
  3537. return info, err
  3538. }
  3539. func GetCancelStockOrderPrintOne(idstr []string, orgid int64) (info []*models.CancelStock, err error) {
  3540. if len(idstr) > 0 {
  3541. err = XTReadDB().Model(&info).Where("id in(?)", idstr).
  3542. Preload("XtCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  3543. return XTReadDB().Where("org_id=? and status = 1", orgid).Preload("GoodInfo", "status = 1 and org_id = ?", orgid)
  3544. }).Find(&info).Error
  3545. }
  3546. return info, err
  3547. }
  3548. func GetCancelOutTotalCount(startime int64, endtime int64, orgid int64) (info []*models.VmCancelStockInfo, err error) {
  3549. db := XTReadDB().Table("xt_cancel_stock_info as x").Where("x.status = 1")
  3550. if startime > 0 {
  3551. db = db.Where("x.ctime >=?", startime)
  3552. }
  3553. if endtime > 0 {
  3554. db = db.Where("x.ctime <= ?", endtime)
  3555. }
  3556. if orgid > 0 {
  3557. db = db.Where("x.org_id = ?", orgid)
  3558. }
  3559. err = db.Select("x.good_id,sum(x.count) as count").Group("x.good_id").Scan(&info).Error
  3560. return info, err
  3561. }
  3562. func GetWarehoureOutOrderInfo(goodid int64, orgid int64) (*models.WarehouseOutInfo, error) {
  3563. info := models.WarehouseOutInfo{}
  3564. var err error
  3565. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1", goodid, orgid).Find(&info).Error
  3566. if err == gorm.ErrRecordNotFound {
  3567. return nil, err
  3568. }
  3569. if err != nil {
  3570. return nil, err
  3571. }
  3572. return &info, nil
  3573. }
  3574. func GetWarehoureOrderInfoCount(goodid int64, orgid int64, id int64) (info models.WarehouseOutInfo, err error) {
  3575. db := XTReadDB().Table("xt_warehouse_out_info as x").Where("x.status =1")
  3576. if goodid > 0 {
  3577. db = db.Where("x.good_id = ?", goodid)
  3578. }
  3579. if orgid > 0 {
  3580. db = db.Where("x.org_id = ?", orgid)
  3581. }
  3582. if id > 0 {
  3583. db = db.Where("x.warehouse_info_id = ?", id)
  3584. }
  3585. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  3586. return info, err
  3587. }
  3588. func GetWarehoureOrderStockOutFlow(goodid int64, orgid int64, id int64) (info models.VmStockFlow, err error) {
  3589. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status =1 and x.consumable_type = 2 or x.consumable_type = 3")
  3590. if goodid > 0 {
  3591. db = db.Where("x.good_id = ?", goodid)
  3592. }
  3593. if orgid > 0 {
  3594. db = db.Where("x.user_org_id = ?", orgid)
  3595. }
  3596. if id > 0 {
  3597. db = db.Where("x.warehousing_id = ?", id)
  3598. }
  3599. err = db.Select("x.good_id,sum(x.count) as count").Scan(&info).Error
  3600. return info, err
  3601. }
  3602. func UpdateCancelInfo(id int64) (models.CancelStock, error) {
  3603. stock := models.CancelStock{}
  3604. err := XTWriteDB().Model(&stock).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  3605. return stock, err
  3606. }
  3607. func GetStockBatchNumber(id int64, orgid int64) (info []*models.WarehousingInfo, err error) {
  3608. err = XTReadDB().Model(&info).Where("good_id = ? and org_id = ? and status = 1 and number <> ''", id, orgid).Find(&info).Error
  3609. return info, err
  3610. }
  3611. func GetWarehouseOutInfoById(id int64, goodid int64) (info []*models.WarehouseOutInfo, err error) {
  3612. err = XTReadDB().Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  3613. return info, err
  3614. }
  3615. func GetStockFlowBatchNumber(id int64, goodid int64) (stock []*models.VmStockFlow, err error) {
  3616. err = XTReadDB().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Group("patient_id,number").Find(&stock).Error
  3617. return stock, err
  3618. }
  3619. func GetStockFlowBatchNumberOne(id int64, goodid int64) (stock []*models.VmStockFlowOne, err error) {
  3620. db := XTReadDB().Table("xt_stock_flow as x").Where("x.status = 1 and count > 0")
  3621. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  3622. fmt.Println(table)
  3623. tableone := XTReadDB().Table("xt_good_information as o").Where("o.status = 1")
  3624. fmt.Println(tableone)
  3625. if id > 0 {
  3626. db = db.Where("x.warehouse_out_id = ?", id)
  3627. }
  3628. if goodid > 0 {
  3629. db = db.Where("x.good_id = ?", goodid)
  3630. }
  3631. 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
  3632. //err =XTReadDB()()()()().Model(&stock).Where("warehouse_out_id = ? and good_id = ? and status = 1", id, goodid).Find(&stock).Error
  3633. return stock, err
  3634. }
  3635. func GetDrugBatchNumber(drug_id int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  3636. err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status = 1 and batch_number <>''", drug_id, orgid).Find(&info).Error
  3637. return info, err
  3638. }
  3639. func ExportDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseInfo, err error) {
  3640. db := XTReadDB().Table("xt_drug_warehouse_info as x").Where("x.status =1")
  3641. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  3642. fmt.Println("table", table)
  3643. if len(warehouse_out_id) > 0 {
  3644. db = db.Where("x.warehousing_id in(?)", warehouse_out_id)
  3645. }
  3646. err = db.Select("x.id,x.warehousing_id,x.drug_id,x.number,x.product_date,x.expiry_date,x.warehousing_count,x.warehouseing_unit,x.max_unit,x.min_unit,x.stock_max_number,x.stock_min_number,x.price,x.dealer,x.manufacturer,x.warehousing_order,x.batch_number,b.dose,b.dose_unit,b.min_number,b.min_unit,b.max_unit,b.drug_name,b.drug_type").Joins("left join xt_base_drug as b on b.id = x.drug_id").Scan(&out).Error
  3647. return out, err
  3648. }
  3649. func GetExportOutOrderDrugList(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  3650. db := XTReadDB().Table("xt_drug_warehouse_out_info as x").Where("x.status = 1")
  3651. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  3652. fmt.Println("table", table)
  3653. if len(warehouse_out_id) > 0 {
  3654. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  3655. }
  3656. 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
  3657. return out, err
  3658. }
  3659. func GetExportOutOrderDrugListOne(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  3660. db := XTReadDB().Table("xt_drug_flow as x").Where("x.status = 1")
  3661. table := XTReadDB().Table("xt_base_drug as b").Where("b.status = 1")
  3662. fmt.Println("table", table)
  3663. if len(warehouse_out_id) > 0 {
  3664. db = db.Where("x.warehouse_out_id in(?)", warehouse_out_id)
  3665. }
  3666. err = db.Select("x.id,x.warehouse_out_id,x.drug_id,x.count,x.max_unit as count_unit,x.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,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
  3667. return out, err
  3668. }
  3669. func GetDrugWarehouseOut(warehouse_out_id []string) (out []*models.StDrugWarehouseOutInfo, err error) {
  3670. err = XTReadDB().Where("warehouse_out_id in(?)", warehouse_out_id).Find(&out).Error
  3671. return out, err
  3672. }
  3673. func GetLastGoodInformationByGoodId(goodid int64, warehouse_info_id int64, orgid_id int64) (models.WarehouseOutInfo, error) {
  3674. info := models.WarehouseOutInfo{}
  3675. err := XTReadDB().Where("good_id = ? and warehouse_info_id= ? and status =1", goodid, warehouse_info_id).Last(&info).Error
  3676. return info, err
  3677. }
  3678. func GetTotalCountByGoodId(goodid int64) (models.WarehousingInfo, error) {
  3679. info := models.WarehousingInfo{}
  3680. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  3681. if goodid > 0 {
  3682. db = db.Where("x.good_id = ?", goodid)
  3683. }
  3684. err := db.Select("x.good_id,sum(x.stock_count) as stock_count").Scan(&info).Error
  3685. return info, err
  3686. }
  3687. func GetLastGoodCountById(goodid int64, warehouse_info_id int64) (models.WarehousingInfo, error) {
  3688. info := models.WarehousingInfo{}
  3689. err := XTReadDB().Where("good_id = ? and id= ? and status = 1", goodid, warehouse_info_id).First(&info).Error
  3690. return info, err
  3691. }
  3692. func UpdateWarehousingInfo(info models.WarehousingInfo, id int64) error {
  3693. err = XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  3694. return err
  3695. }
  3696. func FindFirstWarehousingInfoByGoodId(good_id int64, id int64) (info models.WarehousingInfo, err error) {
  3697. err = readDb.Where("good_id = ? AND status = 1 and stock_count > 0", good_id).Order("ctime asc").First(&info).Error
  3698. return info, err
  3699. }
  3700. func CreateStockFlow(warehousingInfo []*models.VmStockFlow) (err error) {
  3701. if len(warehousingInfo) > 0 {
  3702. utx := writeDb.Begin()
  3703. if len(warehousingInfo) > 0 {
  3704. 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 "
  3705. insertParams := make([]string, 0)
  3706. insertData := make([]interface{}, 0)
  3707. for _, info := range warehousingInfo {
  3708. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  3709. insertData = append(insertData, info.WarehousingId)
  3710. insertData = append(insertData, info.GoodId)
  3711. insertData = append(insertData, info.Number)
  3712. insertData = append(insertData, info.LicenseNumber)
  3713. insertData = append(insertData, info.Count)
  3714. insertData = append(insertData, info.UserOrgId)
  3715. insertData = append(insertData, info.PatientId)
  3716. insertData = append(insertData, info.SystemTime)
  3717. insertData = append(insertData, info.ConsumableType)
  3718. insertData = append(insertData, info.IsSys)
  3719. insertData = append(insertData, info.WarehousingOrder)
  3720. insertData = append(insertData, info.WarehouseOutId)
  3721. insertData = append(insertData, info.WarehouseOutOrderNumber)
  3722. insertData = append(insertData, info.IsEdit)
  3723. insertData = append(insertData, info.CancelStockId)
  3724. insertData = append(insertData, info.CancelOrderNumber)
  3725. insertData = append(insertData, info.Manufacturer)
  3726. insertData = append(insertData, info.Dealer)
  3727. insertData = append(insertData, info.Creator)
  3728. insertData = append(insertData, info.UpdateCreator)
  3729. insertData = append(insertData, info.Status)
  3730. insertData = append(insertData, info.Ctime)
  3731. insertData = append(insertData, info.Mtime)
  3732. insertData = append(insertData, info.Price)
  3733. insertData = append(insertData, info.WarehousingDetailId)
  3734. insertData = append(insertData, info.WarehouseOutDetailId)
  3735. insertData = append(insertData, info.CancelOutDetailId)
  3736. insertData = append(insertData, info.ExpireDate)
  3737. insertData = append(insertData, info.ProductDate)
  3738. }
  3739. thisSQL += strings.Join(insertParams, ", ")
  3740. err = utx.Exec(thisSQL, insertData...).Error
  3741. if err != nil {
  3742. utx.Rollback()
  3743. return
  3744. }
  3745. }
  3746. utx.Commit()
  3747. }
  3748. return
  3749. }
  3750. func CreateStockFlowOne(flow models.VmStockFlow) error {
  3751. err := XTWriteDB().Create(&flow).Error
  3752. return err
  3753. }
  3754. func CreateStockFlowSix(flow *models.VmStockFlow) error {
  3755. err := XTWriteDB().Create(&flow).Error
  3756. return err
  3757. }
  3758. func UpdatedStockFlow(flow models.VmStockFlow) error {
  3759. err := XTWriteDB().Save(&flow).Error
  3760. return err
  3761. }
  3762. func UpdatedStockFlowOne(flow models.VmStockFlow, warehousing_id int64, patient_id int64, record_time int64, good_id int64) error {
  3763. 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}).Error
  3764. return err
  3765. }
  3766. func GetStockFlowIsExsit(warehousing_id int64, patient_id int64, record_time int64, good_id int64) (*models.VmStockFlow, error) {
  3767. stock := models.VmStockFlow{}
  3768. var err error
  3769. err = XTReadDB().Model(&stock).Where("warehousing_id = ? and patient_id = ? and system_time = ? and good_id = ? and status = 1", warehousing_id, patient_id, record_time, good_id).Find(&stock).Error
  3770. if err == gorm.ErrRecordNotFound {
  3771. return nil, err
  3772. }
  3773. if err != nil {
  3774. return nil, err
  3775. }
  3776. return &stock, nil
  3777. }
  3778. func GetWarehouseOutInfoIsExist(id int64) (*models.WarehouseOutInfo, error) {
  3779. info := models.WarehouseOutInfo{}
  3780. var err error
  3781. err = XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  3782. if err == gorm.ErrRecordNotFound {
  3783. return nil, err
  3784. }
  3785. if err != nil {
  3786. return nil, err
  3787. }
  3788. return &info, nil
  3789. }
  3790. func GetWarehouseOutInfoIsExistOne(good_id int64, patient_id int64, record_time int64) (*models.WarehouseOutInfo, error) {
  3791. info := models.WarehouseOutInfo{}
  3792. var err error
  3793. 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
  3794. if err == gorm.ErrRecordNotFound {
  3795. return nil, err
  3796. }
  3797. if err != nil {
  3798. return nil, err
  3799. }
  3800. return &info, nil
  3801. }
  3802. 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) {
  3803. offset := (page - 1) * limit
  3804. db := XTReadDB().Model(&flow).Where("status = 1 and count > 0")
  3805. if consumable_type > 0 {
  3806. //入库
  3807. if consumable_type == 1 {
  3808. db = db.Where("consumable_type = ?", consumable_type)
  3809. }
  3810. //2 手动出库
  3811. if consumable_type == 2 {
  3812. db = db.Where(" consumable_type = ? and is_sys = 0", consumable_type)
  3813. }
  3814. //3.自动出库
  3815. if consumable_type == 3 {
  3816. db = db.Where(" consumable_type = ? and is_sys = 1", consumable_type)
  3817. }
  3818. //4.手动退库
  3819. if consumable_type == 4 {
  3820. db = db.Where(" consumable_type = ?", consumable_type)
  3821. }
  3822. if consumable_type == 10 {
  3823. db = db.Where(" consumable_type = ?", consumable_type)
  3824. }
  3825. if consumable_type == 11 {
  3826. db = db.Where(" consumable_type = ?", consumable_type)
  3827. }
  3828. }
  3829. if orgId > 0 {
  3830. db = db.Where("user_org_id = ?", orgId)
  3831. }
  3832. if startTime > 0 {
  3833. db = db.Where("ctime >= ?", startTime)
  3834. }
  3835. if endTime > 0 {
  3836. db = db.Where("ctime<=?", endTime)
  3837. }
  3838. if good_id > 0 {
  3839. db = db.Where("good_id = ?", good_id)
  3840. }
  3841. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&flow).Error
  3842. return flow, total, err
  3843. }
  3844. func GetLastGoodListByPatientId(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.AutomaticReduceDetail, error) {
  3845. detail := models.AutomaticReduceDetail{}
  3846. 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
  3847. return detail, err
  3848. }
  3849. func GetLastGoodListByPatientIdOne(recordtime int64, patientid int64, goodid int64, goodtypeid int64) (models.WarehouseOutInfo, error) {
  3850. detail := models.WarehouseOutInfo{}
  3851. 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
  3852. return detail, err
  3853. }
  3854. func UpdatedWarehouseOutInfo(info *models.WarehouseOutInfo, good_id int64, patient_id int64, record_time int64) error {
  3855. outInfo := models.WarehouseOutInfo{}
  3856. 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}).Error
  3857. return err
  3858. }
  3859. func GetLastWarehouOutInfoByPatientId(orgid int64, patient_id int64, record_time int64, goodid int64, good_type_id int64) (models.WarehouseOutInfo, error) {
  3860. info := models.WarehouseOutInfo{}
  3861. 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
  3862. return info, err
  3863. }
  3864. func GetLastStockOut(id int64) (models.WarehousingInfo, error) {
  3865. info := models.WarehousingInfo{}
  3866. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  3867. return info, err
  3868. }
  3869. func GetStockGoodCount(good_id int64) (models.WarehousingInfo, error) {
  3870. info := models.WarehousingInfo{}
  3871. db := XTReadDB().Model(&info)
  3872. if good_id > 0 {
  3873. db = db.Where("good_id = ? and status = 1", good_id)
  3874. }
  3875. err := db.Select("sum(stock_count) as stock_count").Find(&info).Error
  3876. return info, err
  3877. }
  3878. 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) {
  3879. offset := (page - 1) * limit
  3880. db := XTReadDB().Model(drug_flow).Where("status = 1")
  3881. if drugid > 0 {
  3882. db = db.Where("drug_id = ?", drugid)
  3883. }
  3884. if orgid > 0 {
  3885. db = db.Where("user_org_id = ?", orgid)
  3886. }
  3887. if startTime > 0 {
  3888. db = db.Where("ctime >=? ", startTime)
  3889. }
  3890. if endTime > 0 {
  3891. db = db.Where("ctime<=?", endTime)
  3892. }
  3893. if stock_type > 0 {
  3894. //手动入库
  3895. if stock_type == 1 {
  3896. db = db.Where("consumable_type = 1")
  3897. }
  3898. //手动出库
  3899. if stock_type == 2 {
  3900. db = db.Where("consumable_type = 2 and is_sys = 0")
  3901. }
  3902. //自动出库
  3903. if stock_type == 3 {
  3904. db = db.Where("consumable_type = 3 and is_sys = 1")
  3905. }
  3906. if stock_type == 4 {
  3907. db = db.Where("consumable_type = 4")
  3908. }
  3909. if stock_type == 10 {
  3910. db = db.Where("consumable_type = 10")
  3911. }
  3912. if stock_type == 11 {
  3913. db = db.Where("consumable_type = 11")
  3914. }
  3915. }
  3916. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Preload("BaseDrugLib", "status = 1").Preload("DrugWarehouseInfo", "status = 1").Find(&drug_flow).Error
  3917. return drug_flow, total, err
  3918. }
  3919. func GetDrugWarehouseOutInfoIsExit(drug_id int64, patient_id int64, recordtime int64) (*models.DrugWarehouseOutInfo, error) {
  3920. outInfo := models.DrugWarehouseOutInfo{}
  3921. var err error
  3922. 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
  3923. if err == gorm.ErrRecordNotFound {
  3924. return nil, err
  3925. }
  3926. if err != nil {
  3927. return nil, err
  3928. }
  3929. return &outInfo, nil
  3930. }
  3931. func GetDrugWarehouseOutInfoIsExitOne(drug_id int64, patient_id int64, recordtime int64) (outInfo []*models.DrugWarehouseOutInfo, err error) {
  3932. 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
  3933. return outInfo, err
  3934. }
  3935. func UpdatedDrugWarehouseOutInfo(info *models.DrugWarehouseOutInfo, drug_id int64, patient_id int64, record_date int64, orgid int64) error {
  3936. outInfo := models.DrugWarehouseOutInfo{}
  3937. 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
  3938. return err
  3939. }
  3940. func GetCancelExportList(startime int64, endtime int64, ids []string, orgid int64) (cancle []*models.CancelStockInfo, err error) {
  3941. db := XTReadDB().Model(&cancle).Where("status = 1")
  3942. if startime > 0 {
  3943. db = db.Where("ctime >=?", startime)
  3944. }
  3945. if endtime > 0 {
  3946. db = db.Where("ctime <=?", endtime)
  3947. }
  3948. if len(ids) > 0 {
  3949. db = db.Where("cancel_stock_id in(?)", ids)
  3950. }
  3951. if orgid > 0 {
  3952. db = db.Where("org_id = ?", orgid)
  3953. }
  3954. db = db.Preload("GoodInfo", "status =1 and org_id = ?", orgid)
  3955. err = db.Find(&cancle).Error
  3956. return cancle, err
  3957. }
  3958. func GetCancelStockDetail(id int64) (info []*models.DrugCancelStockInfo, err error) {
  3959. db := XTReadDB().Model(&info)
  3960. if id > 0 {
  3961. db = db.Where("cancel_stock_id = ?", id)
  3962. }
  3963. db = db.Preload("BaseDrugLib")
  3964. err = db.Find(&info).Error
  3965. return info, err
  3966. }
  3967. func GetDrugWarehouseInfo(id int64) (models.XtDrugWarehouseInfo, error) {
  3968. info := models.XtDrugWarehouseInfo{}
  3969. db := XTReadDB().Model(&info).Where("status = 1")
  3970. if id > 0 {
  3971. db = db.Where("id = ?", id)
  3972. }
  3973. err := db.Select("drug_id,warehousing_count,stock_max_number,stock_min_number").Find(&info).Error
  3974. return info, err
  3975. }
  3976. func GetDrugWarehouseInfoPrescription(drugid int64, orgid int64) (info []*models.DrugWarehouseInfo, err error) {
  3977. err = XTReadDB().Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  3978. return info, err
  3979. }
  3980. func GetDrugWarehouseInfoPrescriptionSeven(drugid int64, orgid int64) (info []*models.DrugWarehouseInfoSeven, err error) {
  3981. err = XTReadDB().Select("id,stock_max_number,stock_min_number").Where("drug_id = ? and org_id =? and status = 1", drugid, orgid).Find(&info).Error
  3982. return info, err
  3983. }
  3984. func GetHisAdviceListByDrugId(drugid int64, patient_id int64, advice_date int64) (models.HisDoctorAdviceInfo, error) {
  3985. info := models.HisDoctorAdviceInfo{}
  3986. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1", drugid, patient_id, advice_date).Find(&info).Error
  3987. return info, err
  3988. }
  3989. func GetHisAdviceListByDrugIdEight(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.HisDoctorAdviceInfo, error) {
  3990. info := models.HisDoctorAdviceInfo{}
  3991. err := XTReadDB().Model(&info).Where("drug_id = ? and patient_id = ? and advice_date = ? and status = 1 and id =?", drugid, patient_id, advice_date, advice_id).Find(&info).Error
  3992. return info, err
  3993. }
  3994. func GetLastDrugWarehouseOutByDrugId(drugid int64, patient_id int64, advice_date int64, advice_id int64) (models.DrugAutomaticReduceDetail, error) {
  3995. detail := models.DrugAutomaticReduceDetail{}
  3996. 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
  3997. return detail, err
  3998. }
  3999. func ModefyDrugByWarehouseInfo(id int64, stock_max_number int64) error {
  4000. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  4001. return err
  4002. }
  4003. func ModefyDrugByWarehouseInfoOne(id int64, stock_min_number int64) error {
  4004. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  4005. return err
  4006. }
  4007. func DeleteDrugAutoWarehouse(drugid int64, patient_id int64, record_time int64) error {
  4008. 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
  4009. 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
  4010. 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
  4011. return err
  4012. }
  4013. func DeleteDrugAutoWarehouseSeven(drugid int64, patient_id int64, record_time int64, advice_id int64) error {
  4014. 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
  4015. 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
  4016. 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
  4017. return err
  4018. }
  4019. func GetDrugWarehouseInfoById(id int64) (models.XtDrugWarehouseInfo, error) {
  4020. info := models.XtDrugWarehouseInfo{}
  4021. err := XTReadDB().Model(&info).Where("id=? and status =1", id).Find(&info).Error
  4022. return info, err
  4023. }
  4024. func UpdateDrugWarehouseInfo(info *models.XtDrugWarehouseInfo, id int64) error {
  4025. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", info.StockMaxNumber)).Error
  4026. return err
  4027. }
  4028. func UpdateDrugWarehouseInfoOne(info *models.XtDrugWarehouseInfo, id int64) error {
  4029. //err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_min_number": info.StockMaxNumber}).Error
  4030. //return err
  4031. err = writeDb.Model(&models.XtDrugWarehouseInfo{}).Where("id = ? and status = 1", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", info.StockMinNumber)).Error
  4032. return err
  4033. }
  4034. func GetLastCancelStockInfo(id int64) (models.DrugCancelStockInfo, error) {
  4035. info := models.DrugCancelStockInfo{}
  4036. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  4037. return info, err
  4038. }
  4039. func GetLastCancelStockInfoOne(id int64, drug_id int64) (models.DrugCancelStockInfo, error) {
  4040. info := models.DrugCancelStockInfo{}
  4041. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ? and status = 1", id, drug_id).Find(&info).Error
  4042. return info, err
  4043. }
  4044. func UpdatedDrugInfo(id int64, count int64) (err error) {
  4045. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", count)).Error
  4046. return err
  4047. }
  4048. func UpdatedDrugFlow(cancelstockid int64, drug_id int64, flow models.DrugFlow) error {
  4049. 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
  4050. return err
  4051. }
  4052. func UpdatedDrugInfoOne(id int64, count int64) (err error) {
  4053. err = XTWriteDB().Model(&models.XtDrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", count)).Error
  4054. return err
  4055. }
  4056. func UpdateWarehouseInfo(count int64, drugid int64, orgid int64) error {
  4057. 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
  4058. return err
  4059. }
  4060. func UpdateWarehouseInfoTwo(count int64, drugid int64, orgid int64) error {
  4061. 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
  4062. return err
  4063. }
  4064. func UpdateWarehouseInfoOne(count int64, drugid int64, orgid int64) error {
  4065. 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
  4066. return err
  4067. }
  4068. func UpdateWarehouseInfoFour(count int64, drugid int64, orgid int64) error {
  4069. 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
  4070. return err
  4071. }
  4072. func GetWarehouseInfoByIdSeven(id int64) (models.WarehousingInfo, error) {
  4073. info := models.WarehousingInfo{}
  4074. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4075. return info, err
  4076. }
  4077. func GetLastWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4078. info := models.WarehousingInfo{}
  4079. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).Last(&info).Error
  4080. return info, err
  4081. }
  4082. func GetFirstWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4083. info := models.WarehousingInfo{}
  4084. err := XTReadDB().Model(&info).Where("good_id = ? and status = 1", goodid).First(&info).Error
  4085. return info, err
  4086. }
  4087. func CreateWarehouseOut(out models.WarehouseOut) error {
  4088. err := XTWriteDB().Create(&out).Error
  4089. return err
  4090. }
  4091. func GetLastGoodWarehouseOut(orgid int64) (models.WarehouseOut, error) {
  4092. out := models.WarehouseOut{}
  4093. err := XTReadDB().Where("org_id = ? and status = 1", orgid).Find(&out).Error
  4094. return out, err
  4095. }
  4096. func CreateWarehouseInfo(info models.WarehousingInfo) error {
  4097. err := XTReadDB().Create(&info).Error
  4098. return err
  4099. }
  4100. func UpdateGoodWarehouseInfo(id int64, info models.WarehousingInfo) error {
  4101. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4102. return err
  4103. }
  4104. func UpdateWareInfoById(id int64, count int64) error {
  4105. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count + ?", count)).Error
  4106. return err
  4107. }
  4108. func UpdateWareInfoByIdSix(id int64, count int64) error {
  4109. err = XTWriteDB().Model(&models.WarehousingInfo{}).Where("id = ?", id).UpdateColumn("stock_count", gorm.Expr("stock_count - ?", count)).Error
  4110. return err
  4111. }
  4112. func GetLastCancelInfo(id int64, goodid int64) (models.CancelStockInfo, error) {
  4113. info := models.CancelStockInfo{}
  4114. err := XTReadDB().Model(&info).Where("warehouse_info_id = ? and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4115. return info, err
  4116. }
  4117. func GetAllCancelInfoById(id int64, goodid int64) (info []*models.CancelStockInfo, err error) {
  4118. err = XTReadDB().Model(&info).Where("warehouse_info_id in(?) and good_id = ? and status = 1", id, goodid).Find(&info).Error
  4119. return info, err
  4120. }
  4121. func UpdateStockFlow(flow models.VmStockFlow, goodid int64, warehousing_id int64) error {
  4122. err := XTWriteDB().Model(&flow).Where("good_id = ? and warehousing_id= ? and status = 1", goodid, warehousing_id).Updates(map[string]interface{}{"count": flow.Count}).Error
  4123. return err
  4124. }
  4125. func UpdateDrugFlow(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  4126. drugFlow := models.DrugFlow{}
  4127. 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
  4128. return err
  4129. }
  4130. func UpdateDrugFlowNight(flow models.DrugFlow, drugid int64, warehousing_id int64) error {
  4131. drugFlow := models.DrugFlow{}
  4132. 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
  4133. return err
  4134. }
  4135. func UpdateDrugCancel(id int64) (models.DrugCancelStock, error) {
  4136. stock := models.DrugCancelStock{}
  4137. err := XTWriteDB().Model(&stock).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4138. return stock, err
  4139. }
  4140. func GetDrugWarehouseOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugWarehouseOutInfo, err error) {
  4141. db := XTReadDB().Model(&info)
  4142. if warehouse_info_id > 0 {
  4143. db = db.Where("warehouse_info_id = ?", warehouse_info_id)
  4144. }
  4145. if drug_id > 0 {
  4146. db = db.Where("drug_id = ? and status =1", drug_id)
  4147. }
  4148. err = db.Find(&info).Error
  4149. return info, err
  4150. }
  4151. func GetCancelDrugStockOutInfo(warehouse_info_id int64, drug_id int64) (info []*models.DrugCancelStockInfo, err error) {
  4152. err = XTReadDB().Model(&info).Where("batch_number_id =? and drug_id = ? and status = 1", warehouse_info_id, drug_id).Find(&info).Error
  4153. return info, err
  4154. }
  4155. func GetTemplateSummary(orgid int64) (config []*models.Dataconfig, err error) {
  4156. err = XTReadDB().Model(&config).Where("module='template_summary' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  4157. return config, err
  4158. }
  4159. func GetTemplatePlan(orgid int64) (config []*models.Dataconfig, err error) {
  4160. err = XTReadDB().Model(&config).Where("module='template_plan' and parent_id=? and org_id in (?)", 0, orgid).Find(&config).Error
  4161. return config, err
  4162. }
  4163. func GetSearchGoodWarehouseList(keyword string, orgid int64) (info []*models.BloodWarehouseInfo, err error) {
  4164. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4165. table := XTReadDB().Table("xt_goood_information as t").Where("t.status")
  4166. fmt.Println(table)
  4167. likeKey := "%" + keyword + "%"
  4168. if orgid > 0 {
  4169. db = db.Where("x.org_id = ?", orgid)
  4170. }
  4171. 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,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
  4172. return info, err
  4173. }
  4174. func CreateAdjustPrice(adjustPrice *models.XtStockAdjustPrice) error {
  4175. err := XTWriteDB().Create(&adjustPrice).Error
  4176. return err
  4177. }
  4178. func GetAllStockPrice(orgid int64, startime int64, endtime int64, keyword string, limit int64, page int64) (list []*models.VmStockAdjustPrice, total int64, err error) {
  4179. likeKey := "%" + keyword + "%"
  4180. offset := (page - 1) * limit
  4181. db := XTReadDB().Table("xt_stock_adjust_price as x").Where("x.status = 1")
  4182. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4183. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4184. fmt.Println(table, tab)
  4185. if startime > 0 {
  4186. db = db.Where("x.start_time>=?", startime)
  4187. }
  4188. if endtime > 0 {
  4189. db = db.Where("x.start_time<=?", endtime)
  4190. }
  4191. if len(keyword) > 0 {
  4192. db = db.Where("x.warehousing_order = ? or r.user_name like ?", keyword, likeKey)
  4193. }
  4194. if orgid > 0 {
  4195. db = db.Where("x.user_org_id = ?", orgid)
  4196. }
  4197. 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
  4198. return list, total, err
  4199. }
  4200. func UpdateAdjustPrice(ids []string, info models.XtStockAdjustPrice) error {
  4201. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  4202. return err
  4203. }
  4204. func UpdateAdjustCheckPrice(info *models.GoodInfo, id int64) error {
  4205. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"packing_price": info.PackingPrice, "buy_price": info.BuyPrice}).Error
  4206. return err
  4207. }
  4208. func GetAdjustCheckPriceList(ids []string) (info []*models.XtStockAdjustPrice, err error) {
  4209. err = XTReadDB().Where("id in(?)", ids).Find(&info).Error
  4210. return info, err
  4211. }
  4212. func GetAdjustPricebyIdOne(ids []string) (adjust []*models.XtStockAdjustPrice, err error) {
  4213. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  4214. return adjust, err
  4215. }
  4216. func CreateReportPrice(price *models.XtStockReportPrice) error {
  4217. err := XTWriteDB().Create(&price).Error
  4218. return err
  4219. }
  4220. func GetReportStockList(startime int64, endtime int64, orgid int64, keyword string, page int64, limit int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  4221. likeKey := "%" + keyword + "%"
  4222. offset := (page - 1) * limit
  4223. db := XTReadDB().Table("xt_stock_report_price as x").Where("x.status = 1")
  4224. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4225. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4226. fmt.Println(table, tab)
  4227. if startime > 0 {
  4228. db = db.Where("x.ctime>=?", startime)
  4229. }
  4230. if endtime > 0 {
  4231. db = db.Where("x.ctime<=?", endtime)
  4232. }
  4233. if len(keyword) > 0 {
  4234. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  4235. }
  4236. if orgid > 0 {
  4237. db = db.Where("x.user_org_id = ?", orgid)
  4238. }
  4239. 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
  4240. return adjust, total, err
  4241. }
  4242. func GetStockDamagedList(orgid int64, keyword string, page int64, limit int64, startime int64, endtime int64) (adjust []*models.VmStockInventory, total int64, err error) {
  4243. likeKey := "%" + keyword + "%"
  4244. offset := (page - 1) * limit
  4245. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.inventory_type = 4")
  4246. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4247. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4248. fmt.Println(table, tab)
  4249. if len(keyword) > 0 {
  4250. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  4251. }
  4252. if orgid > 0 {
  4253. db = db.Where("x.user_org_id = ?", orgid)
  4254. }
  4255. if startime > 0 {
  4256. db = db.Where("x.ctime >=?", startime)
  4257. }
  4258. if endtime > 0 {
  4259. db = db.Where("x.ctime <=?", endtime)
  4260. }
  4261. 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,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
  4262. return adjust, total, err
  4263. }
  4264. func GetStockDamagedCountOne(orgid int64, keyword string, inventory_type int64) (adjust []*models.VmStockInventory, err error) {
  4265. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  4266. if inventory_type == 0 {
  4267. db = db.Where("x.inventory_type = 4")
  4268. }
  4269. if inventory_type > 0 {
  4270. db = db.Where("x.inventory_type = ?", inventory_type)
  4271. }
  4272. if len(keyword) > 0 {
  4273. likeKey := "%" + keyword + "%"
  4274. db = db.Where("x.warehousing_order like ?", likeKey)
  4275. }
  4276. if orgid > 0 {
  4277. db = db.Where("x.user_org_id = ?", orgid)
  4278. }
  4279. 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
  4280. return adjust, err
  4281. }
  4282. func GetStockDamagedCount(orgid int64) (adjust []*models.VmStockInventory, err error) {
  4283. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and inventory_type = 4 ")
  4284. if orgid > 0 {
  4285. db = db.Where("x.user_org_id = ?", orgid)
  4286. }
  4287. 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
  4288. return adjust, err
  4289. }
  4290. func UpdateCheckDamage(ids []string, reportPrice models.XtStockReportPrice) error {
  4291. 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
  4292. return err
  4293. }
  4294. func GetCheckDamageList(ids []string) (report []*models.XtStockReportPrice, err error) {
  4295. err = XTReadDB().Model(&report).Where("id in(?) and status= 1", ids).Find(&report).Error
  4296. return report, err
  4297. }
  4298. func CreateInentory(inventory models.XtStockInventory) error {
  4299. err = XTWriteDB().Create(&inventory).Error
  4300. return err
  4301. }
  4302. func GetInventorylist(startime int64, endtime int64, page int64, limit int64, inventory_status int64, keyword string, orgid int64) (adjust []*models.VmStockAdjustPrice, total int64, err error) {
  4303. likeKey := "%" + keyword + "%"
  4304. offset := (page - 1) * limit
  4305. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1")
  4306. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4307. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4308. fmt.Println(table, tab)
  4309. if startime > 0 {
  4310. db = db.Where("x.ctime>=?", startime)
  4311. }
  4312. if endtime > 0 {
  4313. db = db.Where("x.ctime<=?", endtime)
  4314. }
  4315. if len(keyword) > 0 {
  4316. db = db.Where("x.warehousing_order like ? or r.user_name like ?", likeKey, likeKey)
  4317. }
  4318. if orgid > 0 {
  4319. db = db.Where("x.user_org_id = ?", orgid)
  4320. }
  4321. if inventory_status > 0 {
  4322. db = db.Where("checker_status = ?", inventory_status)
  4323. }
  4324. 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
  4325. return adjust, total, err
  4326. }
  4327. func UpdateCheckInventory(ids []string, info models.XtStockInventory) error {
  4328. err := XTWriteDB().Model(&info).Where("id in(?)", ids).Updates(map[string]interface{}{"checker": info.Checker, "checker_status": info.CheckerStatus, "checker_time": info.CheckerTime}).Error
  4329. return err
  4330. }
  4331. func GetModifyPriceDetail(id int64) (models.XtStockAdjustPrice, error) {
  4332. info := models.XtStockAdjustPrice{}
  4333. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Find(&info).Error
  4334. return info, err
  4335. }
  4336. func GetInventoryDetailById(id []string) (list []*models.XtStockInventory, err error) {
  4337. err = XTReadDB().Model(&list).Where("id in(?) and status = 1", id).Find(&list).Error
  4338. return list, err
  4339. }
  4340. func GetWarehouseInfoByIdNight(id int64) (models.WarehousingInfo, error) {
  4341. info := models.WarehousingInfo{}
  4342. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  4343. return info, err
  4344. }
  4345. func GetWarehouseInfoById(id int64) (models.WarehousingInfo, error) {
  4346. info := models.WarehousingInfo{}
  4347. //err :=XTReadDB().Model(&info).Where("id = ? or warehouse_info_id and status = 1", id).Find(&info).Error
  4348. //return info, err
  4349. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4350. if id > 0 {
  4351. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  4352. }
  4353. err := db.Select("sum(x.warehousing_count) as warehousing_count,sum(x.stock_count) as stock_count").Scan(&info).Error
  4354. return info, err
  4355. }
  4356. func UpdateStockPrice(info models.XtStockAdjustPrice, id int64) error {
  4357. 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
  4358. return err
  4359. }
  4360. func DeleteStockPrice(id int64) error {
  4361. info := models.XtStockAdjustPrice{}
  4362. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4363. return err
  4364. }
  4365. func GetStockDamageDetail(id int64) (models.XtStockReportPrice, error) {
  4366. report := models.XtStockReportPrice{}
  4367. err := XTReadDB().Model(&report).Where("id=? and status = 1", id).Find(&report).Error
  4368. return report, err
  4369. }
  4370. func ModifyPrice(id int64, info models.XtStockReportPrice) error {
  4371. 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
  4372. return err
  4373. }
  4374. func DeleteDamage(id int64) error {
  4375. adjust := models.XtStockReportPrice{}
  4376. err := XTWriteDB().Model(&adjust).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4377. return err
  4378. }
  4379. func GetStockDamageById(ids []string) (adjust []*models.XtStockReportPrice, err error) {
  4380. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&adjust).Error
  4381. return adjust, err
  4382. }
  4383. func GetInventoryDetail(id int64) (models.XtStockInventory, error) {
  4384. inventory := models.XtStockInventory{}
  4385. err := XTReadDB().Model(&inventory).Where("id=? and status = 1", id).Find(&inventory).Error
  4386. return inventory, err
  4387. }
  4388. func ModifyInventory(id int64, inventory models.XtStockInventory) error {
  4389. 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
  4390. return err
  4391. }
  4392. func DeleteInventory(id int64) error {
  4393. inventory := models.XtStockInventory{}
  4394. err := XTWriteDB().Model(&inventory).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4395. return err
  4396. }
  4397. func GetInventoryDetailList(keyword string, limit int64, page int64, orgid int64) (list []*models.VmStockInventory, total int64, err error) {
  4398. likeKey := "%" + keyword + "%"
  4399. offset := (page - 1) * limit
  4400. db := XTReadDB().Table("xt_stock_inventory as x").Where("x.status = 1 and x.type = 10")
  4401. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4402. tab := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  4403. fmt.Println(table, tab)
  4404. if len(keyword) > 0 {
  4405. db = db.Where("x.good_name like ?", likeKey)
  4406. }
  4407. if orgid > 0 {
  4408. db = db.Where("x.user_org_id = ?", orgid)
  4409. }
  4410. 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").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
  4411. return list, total, err
  4412. }
  4413. func GetInventoryPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  4414. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  4415. return list, err
  4416. }
  4417. func GetInventoryDetailPrintList(ids []string) (list []*models.XtStockInventory, err error) {
  4418. err = XTReadDB().Where("id in(?) and status = 1", ids).Find(&list).Error
  4419. return list, err
  4420. }
  4421. func DeleteDrugPrice(id int64) error {
  4422. info := models.XtDrugAdjustPrice{}
  4423. err := XTReadDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  4424. return err
  4425. }
  4426. func GetGoodWarehouseList(goodid int64) (models.WarehousingInfo, error) {
  4427. info := models.WarehousingInfo{}
  4428. db := XTReadDB().Model(&info).Where("status = 1")
  4429. if goodid > 0 {
  4430. db = db.Where("good_id = ?", goodid)
  4431. }
  4432. err := db.Select("good_id,sum(stock_count) as stock_count").Find(&info).Error
  4433. return info, err
  4434. }
  4435. func UpdateProofInventory(id int64, info models.WarehousingInfo) error {
  4436. err := XTWriteDB().Model(&info).Where("id=? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4437. return err
  4438. }
  4439. func UpdateStockInventory(inventory models.XtStockInventory, id int64) error {
  4440. err := XTWriteDB().Model(&inventory).Where("id= ? and status = 1", id).Updates(map[string]interface{}{"total": inventory.Total}).Error
  4441. return err
  4442. }
  4443. func CeateStockCorrectRecord(record models.XtStockCorrectRecord) error {
  4444. err := XTWriteDB().Create(&record).Error
  4445. return err
  4446. }
  4447. func GetWarehouseTotal(id int64) (models.WarehousingInfo, error) {
  4448. info := models.WarehousingInfo{}
  4449. //err :=XTReadDB()()()()().Where("id=? and status = 1", id).Find(&info).Error
  4450. //return info, err
  4451. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4452. if id > 0 {
  4453. db = db.Where("x.id = ? or x.warehouse_info_id = ?", id, id)
  4454. }
  4455. err := db.Select("sum(x.stock_count) as stock_count").Scan(&info).Error
  4456. return info, err
  4457. }
  4458. func GetCurrentWarehosueInfo(id int64) (info []*models.DrugWarehouseInfo, err error) {
  4459. err = XTReadDB().Model(&info).Where("id=? or warehousing_info_id = ? ", id, id).Find(&info).Error
  4460. return info, err
  4461. }
  4462. func GetCurrentWarehosueInfoOne(id int64) (models.DrugWarehouseInfo, error) {
  4463. info := models.DrugWarehouseInfo{}
  4464. err := XTReadDB().Model(&info).Where("id=?", id).Find(&info).Error
  4465. return info, err
  4466. }
  4467. func ModifyDrugWarehouseInfo(info *models.DrugWarehouseInfo, id int64) error {
  4468. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_max_number": info.StockMaxNumber, "stock_min_number": info.StockMinNumber}).Error
  4469. return err
  4470. }
  4471. func GetGoodInventoryWarehouseList(id int64) (info []*models.WarehousingInfo, err error) {
  4472. err = XTReadDB().Model(&info).Where("good_id = ? and status = 1", id).Preload("GoodInfo", "status = 1").Find(&info).Error
  4473. return info, err
  4474. }
  4475. func UpdatedWarehousingInfo(info models.WarehousingInfo, id int64) error {
  4476. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"stock_count": info.StockCount}).Error
  4477. return err
  4478. }
  4479. func GetDamageDetailByGoodId(good_id int64, keyword string, inventory_type int64) (list []*models.XtStockInventory, err error) {
  4480. db := XTReadDB().Model(&list).Where(" status = 1")
  4481. if good_id > 0 {
  4482. db = db.Where("good_id = ?", good_id)
  4483. }
  4484. if len(keyword) > 0 {
  4485. likeKey := "%" + keyword + "%"
  4486. db = db.Where("warehousing_order like ?", likeKey)
  4487. }
  4488. if inventory_type > 0 {
  4489. db = db.Where("inventory_type = ?", inventory_type)
  4490. }
  4491. if inventory_type == 0 {
  4492. db = db.Where("inventory_type = 4")
  4493. }
  4494. err = db.Find(&list).Error
  4495. return list, err
  4496. }
  4497. func GetCancelStockId(cancelstock_id int64, drug_id int64) (*models.DrugFlow, error) {
  4498. flow := models.DrugFlow{}
  4499. err := XTReadDB().Where("cancel_stock_id = ? and drug_id = ?", cancelstock_id, drug_id).Find(&flow).Error
  4500. if err == gorm.ErrRecordNotFound {
  4501. return nil, err
  4502. }
  4503. if err != nil {
  4504. return nil, err
  4505. }
  4506. return &flow, nil
  4507. }
  4508. func GetTotalDrugCancelInfo(batchNumber_id int64, drug_id int64, id int64) (info []*models.DrugCancelStockInfo, err error) {
  4509. err = XTReadDB().Where("batch_number_id = ? and drug_id = ? and status = 1 and id<>?", batchNumber_id, drug_id, id).Find(&info).Error
  4510. return info, err
  4511. }
  4512. func GetLastDrugWarehouseInfo(drugid int64) (models.XtDrugWarehouseInfo, error) {
  4513. info := models.XtDrugWarehouseInfo{}
  4514. err := XTReadDB().Where("drug_id = ? and status =1", drugid).Last(&info).Error
  4515. return info, err
  4516. }
  4517. func GetLastGoodWarehouseInfo(goodid int64) (models.WarehousingInfo, error) {
  4518. info := models.WarehousingInfo{}
  4519. err := XTReadDB().Where("good_id =? and status = 1", goodid).Find(&info).Error
  4520. return info, err
  4521. }
  4522. func GetGoodStockFlowInfo(warehouseid int64, goodid int64) (*models.VmStockFlow, error) {
  4523. flow := models.VmStockFlow{}
  4524. err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and status = 1", warehouseid, goodid).Find(&flow).Error
  4525. if err == gorm.ErrRecordNotFound {
  4526. return nil, err
  4527. }
  4528. if err != nil {
  4529. return nil, err
  4530. }
  4531. return &flow, nil
  4532. }
  4533. func GetLastInfoMationById(id int64) (models.GoodInfo, error) {
  4534. info := models.GoodInfo{}
  4535. err := XTReadDB().Where("id =? and status = 1", id).Find(&info).Error
  4536. return info, err
  4537. }
  4538. func UpdateGoodInfoMation(goodid int64, info models.GoodInfo) error {
  4539. err := XTWriteDB().Model(&info).Where("id =? and status = 1", goodid).Updates(map[string]interface{}{"total": info.Total}).Error
  4540. return err
  4541. }
  4542. func UpdateWarehouseInfoByGoodId(info models.WarehousingInfo, id int64) error {
  4543. err := XTWriteDB().Model(&info).Where("id =? and status = 1", id).Updates(map[string]interface{}{"warehousing_count": info.WarehousingCount, "stock_count": info.StockCount}).Error
  4544. return err
  4545. }
  4546. func UpdateWarehouseInfoByGoodIdOne(info models.WarehousingInfo, id int64) error {
  4547. 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}).Error
  4548. return err
  4549. }
  4550. func UpdatedStockFlowByGoodId(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  4551. 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
  4552. return err
  4553. }
  4554. func UpdatedStockFlowByGoodIdOne(warehousing_id int64, good_id int64, flow models.VmStockFlow) error {
  4555. 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}).Error
  4556. return err
  4557. }
  4558. func UpdateStockFlowByDetailId(flow models.VmStockFlow, wareoutdetailid int64) error {
  4559. 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}).Error
  4560. return err
  4561. }
  4562. func UpdatedGoodInfo(info models.GoodInfo, goodid int64) error {
  4563. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", goodid).Updates(map[string]interface{}{"manufacturer": info.Manufacturer, "dealer": info.Dealer}).Error
  4564. return err
  4565. }
  4566. func GetInventoryExportList(orgid int64) (info []*models.PatientWarehouseInfo, err error) {
  4567. db := XTReadDB().Table("xt_warehouse_info as x").Where("x.status = 1")
  4568. table := XTReadDB().Table("xt_good_information as t").Where("t.status = 1")
  4569. fmt.Println(table)
  4570. if orgid > 0 {
  4571. db = db.Where("x.org_id = ?", orgid)
  4572. }
  4573. 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
  4574. return info, err
  4575. }
  4576. func UpdateDrugWarehouseingInfoSix(id int64, info models.XtDrugWarehouseInfo) error {
  4577. warehouseInfo := models.XtDrugWarehouseInfo{}
  4578. 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}).Error
  4579. return err
  4580. }
  4581. func CreatedCancelStock(info models.DrugCancelStockInfo) error {
  4582. err := XTWriteDB().Create(&info).Error
  4583. return err
  4584. }
  4585. func IsExsitStockFlow(goodid int64, cancel_order_number string) (*models.VmStockFlow, error) {
  4586. flow := models.VmStockFlow{}
  4587. err := XTReadDB().Where("good_id = ? and cancel_order_number = ? and status = 1", goodid, cancel_order_number).Find(&flow).Error
  4588. if err == gorm.ErrRecordNotFound {
  4589. return nil, err
  4590. }
  4591. if err != nil {
  4592. return nil, err
  4593. }
  4594. return &flow, nil
  4595. }
  4596. func UpdateStockFlowTwo(id int64, flow models.VmStockFlow) error {
  4597. err := XTWriteDB().Model(&flow).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"count": flow.Count}).Error
  4598. return err
  4599. }
  4600. func UpdateStockFlowThree(id int64, flow models.VmStockFlow) error {
  4601. 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}).Error
  4602. return err
  4603. }
  4604. func GetLastWarehouseOutInfoById(drugid int64) (models.DrugWarehouseOutInfo, error) {
  4605. flow := models.DrugWarehouseOutInfo{}
  4606. err := XTReadDB().Where("drug_id = ? and status = 1", drugid).Last(&flow).Error
  4607. return flow, err
  4608. }
  4609. func GetLastWarehoseByGoodId(goodid int64, patient_id int64, record_date int64) (models.WarehouseOutInfo, error) {
  4610. info := models.WarehouseOutInfo{}
  4611. 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
  4612. return info, err
  4613. }
  4614. func ModifyDrugWarehouseOutInfo(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64) error {
  4615. 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
  4616. return err
  4617. }
  4618. func ModifyDrugWarehouseOutInfoOne(info models.DrugWarehouseOutInfo, drugid int64, patientid int64, sys_record_time int64, warehouse_info_id int64) error {
  4619. 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
  4620. return err
  4621. }
  4622. func GetDrugWarehouseOutInfoByWarehosueId(drugid int64, patientid int64, recorddate int64, warehosueinfoid int64) (*models.DrugWarehouseOutInfo, error) {
  4623. info := models.DrugWarehouseOutInfo{}
  4624. 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
  4625. if err == gorm.ErrRecordNotFound {
  4626. return nil, err
  4627. }
  4628. if err != nil {
  4629. return nil, err
  4630. }
  4631. return &info, nil
  4632. }
  4633. func GetDrugFlowRecord(warehouse_out_order_number string, drugid int64, patientid int64) (*models.DrugFlow, error) {
  4634. drugFlow := models.DrugFlow{}
  4635. 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
  4636. if err == gorm.ErrRecordNotFound {
  4637. return nil, err
  4638. }
  4639. if err != nil {
  4640. return nil, err
  4641. }
  4642. return &drugFlow, nil
  4643. }
  4644. func ModifyDrugFlow(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64) error {
  4645. 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
  4646. return err
  4647. }
  4648. func ModifyDrugFlowOne(flow models.DrugFlow, warehouse_out_order_number string, drugid int64, patient_id int64, warehousing_detail_id int64) error {
  4649. 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
  4650. return err
  4651. }
  4652. func GetDrugAutoReduceRecordOne(drugid int64, patientid int64, recorddate int64) (detail []*models.DrugAutomaticReduceDetail, err error) {
  4653. err = XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  4654. return detail, err
  4655. }
  4656. func GetDrugAutoReduceRecord(drugid int64, patientid int64, recorddate int64) (*models.DrugAutomaticReduceDetail, error) {
  4657. detail := models.DrugAutomaticReduceDetail{}
  4658. err := XTReadDB().Model(&detail).Where("drug_id = ? and patient_id = ? and record_time = ? and status = 1", drugid, patientid, recorddate).Find(&detail).Error
  4659. if err == gorm.ErrRecordNotFound {
  4660. return nil, err
  4661. }
  4662. if err != nil {
  4663. return nil, err
  4664. }
  4665. return &detail, nil
  4666. }
  4667. func ModifyAutoMaticReduce(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64) error {
  4668. 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
  4669. return err
  4670. }
  4671. func ModifyAutoMaticReduceOne(auto models.DrugAutomaticReduceDetail, patientid int64, recordate int64, drugid int64, warehouse_info_id int64) error {
  4672. 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
  4673. return err
  4674. }
  4675. func ModifyDrugWarehouse(id int64, stock_max_number int64, stock_min_number int64) (models.DrugWarehouseInfo, error) {
  4676. info := models.DrugWarehouseInfo{}
  4677. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  4678. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  4679. return info, err
  4680. }
  4681. func ModeifyDrugWarehosueInfo(id int64, stock_max_number int64) error {
  4682. err := XTWriteDB().Model(models.DrugWarehouseInfo{}).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number + ?", stock_max_number)).Error
  4683. return err
  4684. }
  4685. func ModeFyStockMinById(id int64, stock_min_number int64) error {
  4686. info := models.DrugWarehouseInfo{}
  4687. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number + ?", stock_min_number)).Error
  4688. return err
  4689. }
  4690. func GetDrugCancelById(id int64) (models.DrugCancelStockInfo, error) {
  4691. info := models.DrugCancelStockInfo{}
  4692. err := XTReadDB().Where("id = ? and status = 1", id).Find(&info).Error
  4693. return info, err
  4694. }
  4695. func ModeFyDrugWareseInfo(id int64, stock_max_number int64) error {
  4696. info := models.DrugWarehouseInfo{}
  4697. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_max_number", gorm.Expr("stock_max_number - ?", stock_max_number)).Error
  4698. return err
  4699. }
  4700. func ModeFyDrugWarehouseMin(id int64, stock_min_number int64) error {
  4701. info := models.DrugWarehouseInfo{}
  4702. err = XTWriteDB().Model(&info).Where("id = ?", id).UpdateColumn("stock_min_number", gorm.Expr("stock_min_number - ?", stock_min_number)).Error
  4703. return err
  4704. }
  4705. func GetAllHisDoctorAdviceById(drugid int64, patient_id int64, advicedate int64, orgid int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  4706. 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
  4707. return advice, err
  4708. }
  4709. func GetAllDrugFlowById(drugid int64, patient_id int64, advicedate int64, orgid int64) (info []*models.DrugWarehouseOutInfo, err error) {
  4710. 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
  4711. return info, err
  4712. }