stock_service.go 249KB

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