Stock.go 172 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452
  1. package controllers
  2. import (
  3. "ERP_storage/Nats"
  4. "ERP_storage/Nats/NatsServer"
  5. "ERP_storage/conf"
  6. "ERP_storage/dto"
  7. "ERP_storage/logs"
  8. "ERP_storage/models/Account"
  9. "ERP_storage/models/Basic"
  10. "ERP_storage/models/Contract"
  11. "ERP_storage/models/Stock"
  12. "errors"
  13. "fmt"
  14. "math"
  15. "os"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "github.com/beego/beego/v2/adapter/orm"
  21. beego "github.com/beego/beego/v2/server/web"
  22. "github.com/robfig/cron/v3"
  23. "github.com/xuri/excelize/v2"
  24. natslibs "gogs.baozhida.cn/zoie/ERP_libs/Nats"
  25. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  26. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  27. )
  28. type StockController struct {
  29. beego.Controller
  30. User userlibs.User
  31. }
  32. func (c *StockController) Prepare() {
  33. c.User = *Account.User_r
  34. }
  35. func (c *StockController) Device_List() {
  36. // 分页参数 初始化
  37. page, _ := c.GetInt("page")
  38. if page < 1 {
  39. page = 1
  40. }
  41. page_z, _ := c.GetInt("page_z")
  42. if page_z < 1 {
  43. page_z = conf.Page_size
  44. }
  45. T_state, _ := c.GetInt("T_state")
  46. T_name := c.GetString("T_name")
  47. T_product_name := c.GetString("T_product_name") // 产品名称
  48. T_product_model := c.GetString("T_product_model") // 产品型号
  49. DeviceDao := Stock.NewDevice(orm.NewOrm())
  50. R_List, R_cnt := DeviceDao.Read_Device_List(T_name, T_product_name, T_product_model, T_state, page, page_z)
  51. var r_jsons lib.R_JSONS
  52. r_jsons.Num = R_cnt
  53. r_jsons.Data = R_List
  54. r_jsons.Page = page
  55. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  56. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  57. c.ServeJSON()
  58. return
  59. }
  60. func (c *StockController) Device_Excel() {
  61. T_state, _ := c.GetInt("T_state")
  62. T_name := c.GetString("T_name")
  63. T_product_name := c.GetString("T_product_name") // 产品名称
  64. T_product_model := c.GetString("T_product_model") // 产品型号
  65. DeviceDao := Stock.NewDevice(orm.NewOrm())
  66. R_List, _ := DeviceDao.Read_Device_List(T_name, T_product_name, T_product_model, T_state, 0, 9999)
  67. // 生成Excel文件名
  68. filename := fmt.Sprintf("设备列表(%s)", lib.GetRandstring(6, "0123456789", 0))
  69. // 创建Excel文件
  70. f := excelize.NewFile()
  71. // 设置样式
  72. Style1, _ := f.NewStyle(
  73. &excelize.Style{
  74. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  75. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  76. })
  77. Style2, _ := f.NewStyle(
  78. &excelize.Style{
  79. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  80. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  81. Border: []excelize.Border{
  82. {Type: "left", Color: "000000", Style: 1},
  83. {Type: "top", Color: "000000", Style: 1},
  84. {Type: "bottom", Color: "000000", Style: 1},
  85. {Type: "right", Color: "000000", Style: 1},
  86. },
  87. })
  88. Style3, _ := f.NewStyle(
  89. &excelize.Style{
  90. Font: &excelize.Font{Size: 10, Family: "宋体"},
  91. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  92. Border: []excelize.Border{
  93. {Type: "left", Color: "000000", Style: 1},
  94. {Type: "top", Color: "000000", Style: 1},
  95. {Type: "bottom", Color: "000000", Style: 1},
  96. {Type: "right", Color: "000000", Style: 1},
  97. },
  98. })
  99. // 设置工作表名称
  100. f.SetSheetName("Sheet1", "设备列表")
  101. // 设置标题
  102. f.MergeCell("设备列表", "A1", "M1")
  103. f.SetRowStyle("设备列表", 1, 1, Style1)
  104. f.SetCellValue("设备列表", "A1", "宝智达科技设备列表")
  105. f.SetRowHeight("设备列表", 1, 30)
  106. // 设置表头
  107. f.SetCellStyle("设备列表", "A2", "M2", Style2)
  108. f.SetRowHeight("设备列表", 2, 20)
  109. // 设置表头内容
  110. f.SetCellValue("设备列表", "A2", "序号")
  111. f.SetCellValue("设备列表", "B2", "合同编号")
  112. f.SetCellValue("设备列表", "C2", "出库单号")
  113. f.SetCellValue("设备列表", "D2", "关联项目")
  114. f.SetCellValue("设备列表", "E2", "产品名称")
  115. f.SetCellValue("设备列表", "F2", "产品分类")
  116. f.SetCellValue("设备列表", "G2", "产品型号")
  117. f.SetCellValue("设备列表", "H2", "产品规格")
  118. f.SetCellValue("设备列表", "I2", "设备SN")
  119. f.SetCellValue("设备列表", "J2", "模组imei")
  120. f.SetCellValue("设备列表", "K2", "物联网卡号")
  121. f.SetCellValue("设备列表", "L2", "设备编号")
  122. f.SetCellValue("设备列表", "M2", "状态")
  123. // 设置列宽
  124. f.SetColWidth("设备列表", "A", "A", 8) // 序号
  125. f.SetColWidth("设备列表", "B", "B", 15) // 合同编号
  126. f.SetColWidth("设备列表", "C", "C", 15) // 出库单号
  127. f.SetColWidth("设备列表", "D", "D", 20) // 关联项目
  128. f.SetColWidth("设备列表", "E", "E", 20) // 产品名称
  129. f.SetColWidth("设备列表", "F", "F", 15) // 产品分类
  130. f.SetColWidth("设备列表", "G", "G", 15) // 产品型号
  131. f.SetColWidth("设备列表", "H", "H", 15) // 产品规格
  132. f.SetColWidth("设备列表", "I", "I", 20) // 设备SN
  133. f.SetColWidth("设备列表", "J", "J", 15) // 模组imei
  134. f.SetColWidth("设备列表", "K", "K", 20) // 物联网卡号
  135. f.SetColWidth("设备列表", "L", "L", 18) // 设备编号
  136. f.SetColWidth("设备列表", "M", "M", 10) // 状态
  137. // 填充数据
  138. for i, device := range R_List {
  139. row := i + 3 // 从第3行开始填充数据
  140. // 状态转换
  141. stateText := "未出库"
  142. if device.T_State == 1 {
  143. stateText = "已出库"
  144. }
  145. f.SetCellStyle("设备列表", fmt.Sprintf("A%d", row), fmt.Sprintf("M%d", row), Style3)
  146. //f.SetRowHeight("设备列表", row, 20)
  147. f.SetCellValue("设备列表", fmt.Sprintf("A%d", row), i+1) // 序号
  148. f.SetCellValue("设备列表", fmt.Sprintf("B%d", row), device.T_contract_number) // 合同编号
  149. f.SetCellValue("设备列表", fmt.Sprintf("C%d", row), device.T_out_number) // 出库单号
  150. f.SetCellValue("设备列表", fmt.Sprintf("D%d", row), device.T_project) // 关联项目
  151. f.SetCellValue("设备列表", fmt.Sprintf("E%d", row), device.T_product_name) // 产品名称
  152. f.SetCellValue("设备列表", fmt.Sprintf("F%d", row), device.T_product_class_name) // 产品分类
  153. f.SetCellValue("设备列表", fmt.Sprintf("G%d", row), device.T_product_model) // 产品型号
  154. f.SetCellValue("设备列表", fmt.Sprintf("H%d", row), device.T_product_spec) // 产品规格
  155. f.SetCellValue("设备列表", fmt.Sprintf("I%d", row), device.T_sn) // 设备SN
  156. f.SetCellValue("设备列表", fmt.Sprintf("J%d", row), device.T_imei) // 模组imei
  157. f.SetCellValue("设备列表", fmt.Sprintf("K%d", row), device.T_iccid) // 物联网卡号
  158. f.SetCellValue("设备列表", fmt.Sprintf("L%d", row), device.T_device_number) // 设备编号
  159. f.SetCellValue("设备列表", fmt.Sprintf("M%d", row), stateText) // 状态
  160. }
  161. // 保存文件
  162. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  163. fmt.Println(err)
  164. }
  165. var url string
  166. //// 上传 OSS
  167. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  168. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  169. if !is {
  170. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  171. c.ServeJSON()
  172. return
  173. }
  174. //删除目录
  175. err := os.Remove("ofile/" + filename + ".xlsx")
  176. if err != nil {
  177. logs.Error(lib.FuncName(), err)
  178. }
  179. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  180. c.ServeJSON()
  181. return
  182. }
  183. func (c *StockController) Device_Check() {
  184. T_sn := c.GetString("T_sn")
  185. T_type, _ := c.GetInt("T_type") //1-出库 2-入库
  186. T_product_id, _ := c.GetInt("T_product_id")
  187. T_date := c.GetString("T_date")
  188. // 检查MQTT授权
  189. //mqtt := Stock.Read_MqttUser(T_sn)
  190. //if len(mqtt.Username) == 0 {
  191. // c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未授权,请先授权!", T_sn)}
  192. // c.ServeJSON()
  193. // return
  194. //}
  195. DeviceDao := Stock.NewDevice(orm.NewOrm())
  196. date, is := lib.DateStrToTime(T_date)
  197. if !is {
  198. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  199. c.ServeJSON()
  200. return
  201. }
  202. checkTime := date.Format("2006-01-02") + time.Now().Format(" 15:04:05")
  203. // T_State 1-已出库 2-未出库/入库
  204. // 查询checkTime时间前最后一条记录
  205. lastBeforeCheckTime, errBefore := DeviceDao.Read_Device_LastBeforeCheckTime(T_sn, checkTime)
  206. // 查询checkTime时间后第一条记录
  207. firstAfterCheckTime, errAfter := DeviceDao.Read_Device_FirstAfterCheckTime(T_sn, checkTime)
  208. // 如果是入库操作
  209. if T_type == 2 {
  210. // 根据表格逻辑判断:
  211. // 1. 时间前无数据,时间后有入库记录
  212. if (errBefore != nil || lastBeforeCheckTime.Id == 0) &&
  213. errAfter == nil && firstAfterCheckTime.T_State == 2 && len(firstAfterCheckTime.T_in_number) > 0 {
  214. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  215. c.ServeJSON()
  216. return
  217. }
  218. // 2. 时间前已入库
  219. if errBefore == nil && lastBeforeCheckTime.T_State == 2 && len(lastBeforeCheckTime.T_in_number) > 0 {
  220. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  221. c.ServeJSON()
  222. return
  223. }
  224. // 3. 时间前已出库,时间后有入库记录
  225. if errBefore == nil && lastBeforeCheckTime.T_State == 1 && len(lastBeforeCheckTime.T_out_number) > 0 &&
  226. errAfter == nil && firstAfterCheckTime.T_State == 2 && len(firstAfterCheckTime.T_in_number) > 0 {
  227. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  228. c.ServeJSON()
  229. return
  230. }
  231. }
  232. // 如果是出库操作
  233. if T_type == 1 {
  234. // 根据表格逻辑判断:
  235. // 1. 时间前无数据 -> 未入库
  236. if errBefore != nil || lastBeforeCheckTime.Id == 0 {
  237. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未入库,请先入库!", T_sn)}
  238. c.ServeJSON()
  239. return
  240. }
  241. // 2. 时间前已入库,时间后有出库记录
  242. if lastBeforeCheckTime.T_State == 2 && len(lastBeforeCheckTime.T_in_number) > 0 &&
  243. errAfter == nil && firstAfterCheckTime.T_State == 1 && len(firstAfterCheckTime.T_out_number) > 0 {
  244. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已出库,请勿重复提交!", T_sn)}
  245. c.ServeJSON()
  246. return
  247. }
  248. // 3. 时间前已出库
  249. if lastBeforeCheckTime.T_State == 1 && len(lastBeforeCheckTime.T_out_number) > 0 {
  250. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已出库,请勿重复提交!", T_sn)}
  251. c.ServeJSON()
  252. return
  253. }
  254. // 检查产品是否一致
  255. if lastBeforeCheckTime.T_product_id != T_product_id {
  256. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库产品与出库产品不一致,请检查!"}
  257. c.ServeJSON()
  258. return
  259. }
  260. }
  261. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  262. c.ServeJSON()
  263. return
  264. }
  265. func (c *StockController) Device_Take_Stock() {
  266. T_product_name := c.GetString("T_product_name") // 产品名称
  267. T_product_model := c.GetString("T_product_model") // 产品型号
  268. T_sn_list := c.GetString("T_sn_list") // sn列表
  269. DeviceDao := Stock.NewDevice(orm.NewOrm())
  270. // 获取库存中的所有设备(如果有产品名称和型号过滤)
  271. R_List, _ := DeviceDao.Read_Device_List("", T_product_name, T_product_model, 0, 0, 9999)
  272. // 创建设备SN到设备信息的映射,用于快速查找
  273. deviceMap := make(map[string]Stock.Device_R)
  274. // 保存在库设备列表
  275. var inStockDevices []Stock.Device_R
  276. for _, device := range R_List {
  277. deviceMap[device.T_sn] = device
  278. // 收集所有在库设备(状态为2)
  279. if device.T_State == 2 {
  280. inStockDevices = append(inStockDevices, device)
  281. }
  282. }
  283. // 创建结果数据结构
  284. type DeviceCheckResult struct {
  285. T_sn string `json:"t_sn"` // 设备SN
  286. T_exists bool `json:"t_exists"` // 是否存在于库存
  287. T_state int `json:"t_state"` // 设备状态:1-已出库 2-未出库/入库
  288. T_state_desc string `json:"t_state_desc"` // 状态描述
  289. T_product_id int `json:"t_product_id"` // 产品ID
  290. T_product_name string `json:"t_product_name"` // 产品名称
  291. T_product_model string `json:"t_product_model"` // 产品型号
  292. T_iccid string `json:"t_iccid"` // SIM卡号
  293. T_imei string `json:"t_imei"` // 模组IMEI
  294. T_in_number string `json:"t_in_number"` // 入库编号
  295. T_out_number string `json:"t_out_number"` // 出库编号
  296. T_from_input bool `json:"t_from_input"` // 是否来自输入列表
  297. }
  298. // 处理每个输入的SN
  299. var results []DeviceCheckResult
  300. var processedSNs = make(map[string]bool) // 用于跟踪已处理的SN
  301. // 解析输入的SN列表(如果有)
  302. var snList []string
  303. if len(T_sn_list) > 0 {
  304. snList = lib.SplitString(T_sn_list, ",")
  305. for _, sn := range snList {
  306. sn = strings.TrimSpace(sn)
  307. if len(sn) == 0 {
  308. continue
  309. }
  310. result := DeviceCheckResult{
  311. T_sn: sn,
  312. T_exists: false,
  313. T_from_input: true, // 标记为来自输入列表
  314. }
  315. // 检查设备是否存在于库存
  316. if device, exists := deviceMap[sn]; exists {
  317. result.T_exists = true
  318. result.T_state = device.T_State
  319. result.T_product_id = device.T_product_id
  320. result.T_product_name = device.T_product_name
  321. result.T_product_model = device.T_product_model
  322. result.T_iccid = device.T_iccid
  323. result.T_imei = device.T_imei
  324. result.T_in_number = device.T_in_number
  325. result.T_out_number = device.T_out_number
  326. // 设置状态描述
  327. if device.T_State == 1 {
  328. result.T_state_desc = "已出库"
  329. } else if device.T_State == 2 {
  330. result.T_state_desc = "在库"
  331. } else {
  332. result.T_state_desc = "未知状态"
  333. }
  334. } else {
  335. // 设备不存在于库存
  336. result.T_state_desc = "未入库"
  337. }
  338. results = append(results, result)
  339. processedSNs[sn] = true
  340. }
  341. }
  342. // 添加所有在库设备(如果还没有处理过)
  343. for _, device := range inStockDevices {
  344. // 如果这个SN已经处理过(来自输入列表),则跳过
  345. if _, processed := processedSNs[device.T_sn]; processed {
  346. continue
  347. }
  348. result := DeviceCheckResult{
  349. T_sn: device.T_sn,
  350. T_exists: true,
  351. T_state: device.T_State,
  352. T_state_desc: "在库",
  353. T_product_id: device.T_product_id,
  354. T_product_name: device.T_product_name,
  355. T_product_model: device.T_product_model,
  356. T_iccid: device.T_iccid,
  357. T_imei: device.T_imei,
  358. T_in_number: device.T_in_number,
  359. T_out_number: device.T_out_number,
  360. T_from_input: false, // 标记为不是来自输入列表
  361. }
  362. results = append(results, result)
  363. }
  364. // 统计结果
  365. totalInputCount := 0
  366. existsCount := 0
  367. inStockCount := 0
  368. outStockCount := 0
  369. notExistsCount := 0
  370. for _, result := range results {
  371. if result.T_from_input {
  372. totalInputCount++
  373. if result.T_exists {
  374. existsCount++
  375. if result.T_state == 2 {
  376. inStockCount++
  377. } else if result.T_state == 1 {
  378. outStockCount++
  379. }
  380. } else {
  381. notExistsCount++
  382. }
  383. }
  384. }
  385. // 组装返回数据
  386. r_jsons := map[string]interface{}{
  387. "devices": results,
  388. "stats": map[string]interface{}{
  389. "total_input": totalInputCount,
  390. "exists": existsCount,
  391. "in_stock": inStockCount,
  392. "out_stock": outStockCount,
  393. "not_exists": notExistsCount,
  394. "total_in_stock": len(inStockDevices),
  395. },
  396. }
  397. c.Data["json"] = lib.JSONS{Code: 200, Msg: "盘点完成", Data: r_jsons}
  398. c.ServeJSON()
  399. return
  400. }
  401. func (c *StockController) Stock_List() {
  402. // 分页参数 初始化
  403. page, _ := c.GetInt("page")
  404. if page < 1 {
  405. page = 1
  406. }
  407. page_z, _ := c.GetInt("page_z")
  408. if page_z < 1 {
  409. page_z = conf.Page_size
  410. }
  411. // 查询
  412. T_depot_id, _ := c.GetInt("T_depot_id")
  413. // 查询
  414. T_product_name := c.GetString("T_product_name")
  415. T_product_model := c.GetString("T_product_model")
  416. T_product_class, _ := c.GetInt("T_product_class")
  417. userList, _ := NatsServer.Read_User_List_All()
  418. Account.Read_User_All_Map(userList)
  419. Basic.Read_Depot_All_Map()
  420. StockDao := Stock.NewStock(orm.NewOrm())
  421. R_List, R_cnt := StockDao.Read_Stock_List(T_depot_id, T_product_class, T_product_name, T_product_model, page, page_z)
  422. var r_jsons lib.R_JSONS
  423. r_jsons.Num = R_cnt
  424. r_jsons.Data = R_List
  425. r_jsons.Page = page
  426. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  427. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  428. c.ServeJSON()
  429. return
  430. }
  431. // 修改库存排序
  432. func (c *StockController) Stock_Edit_Sort() {
  433. T_id, _ := c.GetInt("T_id")
  434. T_sort, _ := c.GetInt("T_sort")
  435. StockDao := Stock.NewStock(orm.NewOrm())
  436. Stock_r, err := StockDao.Read_Stock_ById(T_id)
  437. if err != nil {
  438. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  439. c.ServeJSON()
  440. return
  441. }
  442. Stock_r.T_sort = T_sort
  443. if err = StockDao.Update_Stock(Stock_r, "T_sort"); err != nil {
  444. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  445. c.ServeJSON()
  446. return
  447. }
  448. NatsServer.AddUserLogs(c.User.T_uuid, "库存", "修改", Stock_r)
  449. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  450. c.ServeJSON()
  451. return
  452. }
  453. func (c *StockController) Stock_Detail_List() {
  454. // 查询
  455. T_depot_id, _ := c.GetInt("T_depot_id")
  456. // 查询
  457. T_product_id, _ := c.GetInt("T_product_id")
  458. T_start_date := c.GetString("T_start_date")
  459. T_end_date := c.GetString("T_end_date")
  460. now := time.Now()
  461. if len(T_start_date) == 0 {
  462. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  463. } else {
  464. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  465. T_start_date = T_start_date_t.Format("2006-01")
  466. }
  467. if len(T_end_date) == 0 {
  468. T_end_date = now.Format("2006-01")
  469. } else {
  470. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  471. T_end_date = T_end_date_t.Format("2006-01")
  472. }
  473. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  474. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  475. R_List := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, 0, T_start_date, T_end_date)
  476. for i := 0; i < len(R_List); i++ {
  477. R_List[i].T_project = strings.Join(StockOutDao.Read_StockOut_T_project(T_depot_id, R_List[i].T_product_id, R_List[i].T_month), "|")
  478. }
  479. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_List}
  480. c.ServeJSON()
  481. return
  482. }
  483. func (c *StockController) Stock_Detail_Excel() {
  484. // 查询
  485. T_depot_id, _ := c.GetInt("T_depot_id")
  486. T_product_id, _ := c.GetInt("T_product_id")
  487. // 查询
  488. T_start_date := c.GetString("T_start_date")
  489. T_end_date := c.GetString("T_end_date")
  490. now := time.Now()
  491. if len(T_start_date) == 0 {
  492. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  493. } else {
  494. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  495. T_start_date = T_start_date_t.Format("2006-01")
  496. }
  497. if len(T_end_date) == 0 {
  498. T_end_date = now.Format("2006-01")
  499. } else {
  500. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  501. T_end_date = T_end_date_t.Format("2006-01")
  502. }
  503. var class_List []Basic.ProductClass_R
  504. filename := fmt.Sprintf("进销存(%s)", lib.GetRandstring(6, "0123456789", 0))
  505. if T_product_id > 0 {
  506. product, _ := Basic.Read_Product_ById(T_product_id)
  507. filename = fmt.Sprintf("进销存-%s明细(%s)", product.T_name, lib.GetRandstring(6, "0123456789", 0))
  508. class, _ := Basic.Read_ProductClass_ById(product.T_class)
  509. class_List = append(class_List, Basic.ProductClassToProductClass_R(class))
  510. } else {
  511. class_List, _ = Basic.Read_ProductClass_List("", 0, 0, 9999)
  512. }
  513. f := excelize.NewFile() // 设置单元格的值
  514. Style1, _ := f.NewStyle(
  515. &excelize.Style{
  516. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  517. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  518. })
  519. Style2, _ := f.NewStyle(
  520. &excelize.Style{
  521. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  522. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  523. Border: []excelize.Border{
  524. {Type: "left", Color: "000000", Style: 1},
  525. {Type: "top", Color: "000000", Style: 1},
  526. {Type: "bottom", Color: "000000", Style: 1},
  527. {Type: "right", Color: "000000", Style: 1},
  528. },
  529. })
  530. Style3, _ := f.NewStyle(
  531. &excelize.Style{
  532. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体", Color: "FF0000"},
  533. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  534. Border: []excelize.Border{
  535. {Type: "left", Color: "000000", Style: 1},
  536. {Type: "top", Color: "000000", Style: 1},
  537. {Type: "bottom", Color: "000000", Style: 1},
  538. {Type: "right", Color: "000000", Style: 1},
  539. },
  540. })
  541. Style4, _ := f.NewStyle(
  542. &excelize.Style{
  543. Font: &excelize.Font{Size: 10, Family: "宋体"},
  544. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  545. Border: []excelize.Border{
  546. {Type: "left", Color: "000000", Style: 1},
  547. {Type: "top", Color: "000000", Style: 1},
  548. {Type: "bottom", Color: "000000", Style: 1},
  549. {Type: "right", Color: "000000", Style: 1},
  550. },
  551. })
  552. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  553. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  554. var j = 0
  555. for _, r := range class_List {
  556. StockList := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, r.Id, T_start_date, T_end_date)
  557. if len(StockList) == 0 {
  558. continue
  559. }
  560. if j == 0 {
  561. f.SetSheetName("Sheet1", r.T_name)
  562. } else {
  563. f.NewSheet(r.T_name)
  564. }
  565. j += 1
  566. f.MergeCell(r.T_name, "A1", "K1")
  567. f.SetRowStyle(r.T_name, 1, 1, Style1)
  568. f.SetCellValue(r.T_name, "A1", fmt.Sprintf("宝智达科技产品进销存统计表"))
  569. f.SetRowHeight(r.T_name, 1, 30)
  570. // 这里设置表头
  571. f.SetCellStyle(r.T_name, "A2", "K2", Style2)
  572. f.SetRowHeight(r.T_name, 2, 20)
  573. f.SetCellValue(r.T_name, "A2", "序号")
  574. f.SetCellValue(r.T_name, "B2", "产品名称")
  575. f.SetCellValue(r.T_name, "C2", "型号")
  576. f.SetCellValue(r.T_name, "D2", "规格")
  577. f.SetCellValue(r.T_name, "E2", "月份")
  578. f.SetCellValue(r.T_name, "F2", "期初库存")
  579. f.SetCellValue(r.T_name, "G2", "当月入库(正常生产新购入库产品)")
  580. f.SetCellValue(r.T_name, "H2", "退货入库(退还重新入库产品)")
  581. f.SetCellValue(r.T_name, "I2", "当月出库")
  582. f.SetCellValue(r.T_name, "J2", "期末库存")
  583. f.SetCellValue(r.T_name, "K2", "出库项目")
  584. //f.SetCellValue(r.T_name, "K2", "备注")
  585. // 设置列宽
  586. f.SetColWidth(r.T_name, "A", "A", 10)
  587. f.SetColWidth(r.T_name, "B", "B", 10)
  588. f.SetColWidth(r.T_name, "C", "D", 10)
  589. f.SetColWidth(r.T_name, "D", "D", 10)
  590. f.SetColWidth(r.T_name, "E", "E", 10)
  591. f.SetColWidth(r.T_name, "F", "F", 10)
  592. f.SetColWidth(r.T_name, "G", "G", 10)
  593. f.SetColWidth(r.T_name, "H", "H", 10)
  594. f.SetColWidth(r.T_name, "I", "I", 10)
  595. f.SetColWidth(r.T_name, "J", "J", 10)
  596. f.SetColWidth(r.T_name, "K", "K", 70)
  597. //f.SetColWidth(r.T_name, "K", "K", 10)
  598. line := 2
  599. // 循环写入数据
  600. for _, v := range StockList {
  601. line++
  602. product, _ := Basic.Read_Product_ById(v.T_product_id)
  603. f.SetCellValue(r.T_name, fmt.Sprintf("A%d", line), line-2)
  604. f.SetCellValue(r.T_name, fmt.Sprintf("B%d", line), product.T_name)
  605. f.SetCellValue(r.T_name, fmt.Sprintf("C%d", line), product.T_model)
  606. f.SetCellValue(r.T_name, fmt.Sprintf("D%d", line), product.T_spec)
  607. f.SetCellValue(r.T_name, fmt.Sprintf("E%d", line), v.T_month)
  608. f.SetCellValue(r.T_name, fmt.Sprintf("F%d", line), v.T_beginning)
  609. f.SetCellValue(r.T_name, fmt.Sprintf("G%d", line), v.T_in)
  610. f.SetCellValue(r.T_name, fmt.Sprintf("H%d", line), v.T_return)
  611. f.SetCellValue(r.T_name, fmt.Sprintf("I%d", line), v.T_out)
  612. f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), v.T_ending)
  613. if product.T_early_warning > v.T_ending {
  614. f.SetCellStyle(r.T_name, fmt.Sprintf("J%d", line), fmt.Sprintf("J%d", line), Style3)
  615. } else {
  616. f.SetCellStyle(r.T_name, fmt.Sprintf("J%d", line), fmt.Sprintf("J%d", line), Style4)
  617. }
  618. T_project := StockOutDao.Read_StockOut_T_project(T_depot_id, product.Id, v.T_month)
  619. //f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), v.T_project)
  620. f.SetCellValue(r.T_name, fmt.Sprintf("K%d", line), strings.Join(T_project, "\r\n"))
  621. }
  622. f.SetCellStyle(r.T_name, "A2", fmt.Sprintf("I%d", line), Style4)
  623. Style5, _ := f.NewStyle(
  624. &excelize.Style{
  625. Font: &excelize.Font{Size: 10, Family: "宋体"},
  626. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  627. Border: []excelize.Border{
  628. {Type: "left", Color: "000000", Style: 1},
  629. {Type: "top", Color: "000000", Style: 1},
  630. {Type: "bottom", Color: "000000", Style: 1},
  631. {Type: "right", Color: "000000", Style: 1},
  632. },
  633. })
  634. f.SetCellStyle(r.T_name, "K2", fmt.Sprintf("K%d", line), Style5)
  635. }
  636. // 保存文件
  637. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  638. fmt.Println(err)
  639. }
  640. var url string
  641. //// 上传 OSS
  642. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  643. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  644. if !is {
  645. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  646. c.ServeJSON()
  647. return
  648. }
  649. //删除目录
  650. err := os.Remove("ofile/" + filename + ".xlsx")
  651. if err != nil {
  652. logs.Error(lib.FuncName(), err)
  653. }
  654. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  655. c.ServeJSON()
  656. return
  657. }
  658. func (c *StockController) StockIn_List() {
  659. // 分页参数 初始化
  660. page, _ := c.GetInt("page")
  661. if page < 1 {
  662. page = 1
  663. }
  664. page_z, _ := c.GetInt("page_z")
  665. if page_z < 1 {
  666. page_z = conf.Page_size
  667. }
  668. // 查询
  669. T_depot_id, _ := c.GetInt("T_depot_id")
  670. T_start_date := c.GetString("T_start_date")
  671. T_end_date := c.GetString("T_end_date")
  672. T_name := c.GetString("T_name")
  673. T_batch_number := c.GetString("T_batch_number")
  674. userList, _ := NatsServer.Read_User_List_All()
  675. Account.Read_User_All_Map(userList)
  676. Basic.Read_Depot_All_Map()
  677. StockInDao := Stock.NewStockIn(orm.NewOrm())
  678. R_List, R_cnt := StockInDao.Read_StockIn_List(T_batch_number, T_name, T_depot_id, T_start_date, T_end_date, page, page_z)
  679. var r_jsons lib.R_JSONS
  680. r_jsons.Num = R_cnt
  681. r_jsons.Data = R_List
  682. r_jsons.Page = page
  683. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  684. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  685. c.ServeJSON()
  686. return
  687. }
  688. func (c *StockController) StockIn_List_Product() {
  689. // 分页参数 初始化
  690. page, _ := c.GetInt("page")
  691. if page < 1 {
  692. page = 1
  693. }
  694. page_z, _ := c.GetInt("page_z")
  695. if page_z < 1 {
  696. page_z = conf.Page_size
  697. }
  698. // 查询
  699. T_depot_id, _ := c.GetInt("T_depot_id")
  700. T_name := c.GetString("T_name")
  701. T_start_date := c.GetString("T_start_date")
  702. T_end_date := c.GetString("T_end_date")
  703. T_product_name := c.GetString("T_product_name") // 产品名称
  704. T_product_model := c.GetString("T_product_model") // 产品型号
  705. userList, _ := NatsServer.Read_User_List_All()
  706. Account.Read_User_All_Map(userList)
  707. Basic.Read_Depot_All_Map()
  708. StockInDao := Stock.NewStockIn(orm.NewOrm())
  709. R_List, R_cnt := StockInDao.Read_StockInProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, page, page_z)
  710. var r_jsons lib.R_JSONS
  711. r_jsons.Num = R_cnt
  712. r_jsons.Data = R_List
  713. r_jsons.Page = page
  714. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  715. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  716. c.ServeJSON()
  717. return
  718. }
  719. func (c *StockController) StockIn_List_Product_Excel() {
  720. // 查询
  721. T_depot_id, _ := c.GetInt("T_depot_id")
  722. T_name := c.GetString("T_name")
  723. T_start_date := c.GetString("T_start_date")
  724. T_end_date := c.GetString("T_end_date")
  725. T_product_name := c.GetString("T_product_name") // 产品名称
  726. T_product_model := c.GetString("T_product_model") // 产品型号
  727. userList, _ := NatsServer.Read_User_List_All()
  728. Account.Read_User_All_Map(userList)
  729. Basic.Read_Depot_All_Map()
  730. StockInDao := Stock.NewStockIn(orm.NewOrm())
  731. R_List, _ := StockInDao.Read_StockInProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, 0, 9999)
  732. filename := fmt.Sprintf("入库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  733. f := excelize.NewFile() // 设置单元格的值
  734. Style1, _ := f.NewStyle(
  735. &excelize.Style{
  736. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  737. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  738. })
  739. Style2, _ := f.NewStyle(
  740. &excelize.Style{
  741. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  742. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  743. Border: []excelize.Border{
  744. {Type: "left", Color: "000000", Style: 1},
  745. {Type: "top", Color: "000000", Style: 1},
  746. {Type: "bottom", Color: "000000", Style: 1},
  747. {Type: "right", Color: "000000", Style: 1},
  748. },
  749. })
  750. Style3, _ := f.NewStyle(
  751. &excelize.Style{
  752. Font: &excelize.Font{Size: 11, Family: "宋体"},
  753. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  754. Border: []excelize.Border{
  755. {Type: "left", Color: "000000", Style: 1},
  756. {Type: "top", Color: "000000", Style: 1},
  757. {Type: "bottom", Color: "000000", Style: 1},
  758. {Type: "right", Color: "000000", Style: 1},
  759. },
  760. })
  761. f.MergeCell("Sheet1", "A1", "L1")
  762. f.SetRowStyle("Sheet1", 1, 1, Style1)
  763. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("入库明细单"))
  764. f.SetRowHeight("Sheet1", 1, 30)
  765. // 这里设置表头
  766. f.SetCellStyle("Sheet1", "A2", "L2", Style2)
  767. f.SetRowHeight("Sheet1", 2, 20)
  768. f.SetCellValue("Sheet1", "A2", "序号")
  769. f.SetCellValue("Sheet1", "B2", "入库单号")
  770. f.SetCellValue("Sheet1", "C2", "经办人")
  771. f.SetCellValue("Sheet1", "D2", "入库日期")
  772. f.SetCellValue("Sheet1", "E2", "入库仓库")
  773. f.SetCellValue("Sheet1", "F2", "入库类型")
  774. f.SetCellValue("Sheet1", "G2", "产品名称")
  775. f.SetCellValue("Sheet1", "H2", "产品型号")
  776. f.SetCellValue("Sheet1", "I2", "数量")
  777. f.SetCellValue("Sheet1", "J2", "是否关联SN")
  778. f.SetCellValue("Sheet1", "K2", "备注")
  779. f.SetCellValue("Sheet1", "L2", "SN")
  780. // 设置列宽
  781. f.SetColWidth("Sheet1", "A", "A", 10)
  782. f.SetColWidth("Sheet1", "B", "B", 15)
  783. f.SetColWidth("Sheet1", "C", "D", 10)
  784. f.SetColWidth("Sheet1", "D", "D", 10)
  785. f.SetColWidth("Sheet1", "E", "E", 10)
  786. f.SetColWidth("Sheet1", "F", "F", 10)
  787. f.SetColWidth("Sheet1", "G", "G", 20)
  788. f.SetColWidth("Sheet1", "H", "H", 15)
  789. f.SetColWidth("Sheet1", "I", "I", 10)
  790. f.SetColWidth("Sheet1", "J", "J", 12)
  791. f.SetColWidth("Sheet1", "K", "K", 30)
  792. f.SetColWidth("Sheet1", "L", "L", 100)
  793. line := 2
  794. for _, product := range R_List {
  795. line += 1
  796. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  797. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  798. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_submit_name)
  799. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  800. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  801. var type_str string
  802. if product.T_type == 1 {
  803. type_str = "入库"
  804. } else {
  805. type_str = "退库"
  806. }
  807. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), type_str)
  808. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
  809. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
  810. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
  811. var relation_sn string
  812. if product.T_product_relation_sn == 1 {
  813. relation_sn = "是"
  814. } else {
  815. relation_sn = "否"
  816. }
  817. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
  818. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
  819. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
  820. }
  821. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
  822. // 保存文件
  823. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  824. fmt.Println(err)
  825. }
  826. var url string
  827. //// 上传 OSS
  828. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  829. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  830. if !is {
  831. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  832. c.ServeJSON()
  833. return
  834. }
  835. //删除目录
  836. err := os.Remove("ofile/" + filename + ".xlsx")
  837. if err != nil {
  838. logs.Error(lib.FuncName(), err)
  839. }
  840. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  841. c.ServeJSON()
  842. return
  843. }
  844. func (c *StockController) StockIn_Get() {
  845. // 查询
  846. T_number := c.GetString("T_number")
  847. o := orm.NewOrm()
  848. StockInDao := Stock.NewStockIn(o)
  849. StockInProductDao := Stock.NewStockInProduct(o)
  850. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  851. if err != nil {
  852. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  853. c.ServeJSON()
  854. return
  855. }
  856. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  857. var pList []Stock.StockInProduct_R
  858. for _, v := range productList {
  859. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  860. }
  861. userList, _ := NatsServer.Read_User_List_All()
  862. Account.Read_User_All_Map(userList)
  863. Basic.Read_Depot_All_Map()
  864. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockInToStockIn_Detail(stockIn, pList)}
  865. c.ServeJSON()
  866. return
  867. }
  868. func (c *StockController) StockIn_generate_number() {
  869. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  870. rand_x := 0
  871. T_number := ""
  872. for true {
  873. T_number = "RK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  874. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  875. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  876. break
  877. }
  878. rand_x += 1
  879. }
  880. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  881. c.ServeJSON()
  882. return
  883. }
  884. func (c *StockController) StockIn_Add() {
  885. o := orm.NewOrm()
  886. o.Begin()
  887. StockInDao := Stock.NewStockIn(o)
  888. T_number := c.GetString("T_number")
  889. // 检查前端传入的T_number是否已提交过
  890. _, err := StockInDao.Read_StockIn_ByT_number(T_number)
  891. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  892. // 数据库查询错误
  893. o.Rollback()
  894. c.Data["json"] = lib.JSONS{Code: 203, Msg: "查询入库编号失败"}
  895. c.ServeJSON()
  896. return
  897. }
  898. if err == nil {
  899. // 入库编号已存在,不需要做任何处理
  900. o.Rollback()
  901. c.Data["json"] = lib.JSONS{Code: 200, Msg: "入库编号已存在,无需重复提交!", Data: T_number}
  902. c.ServeJSON()
  903. return
  904. }
  905. T_depot_id, _ := c.GetInt("T_depot_id")
  906. T_type, _ := c.GetInt("T_type") // 1-入库 2-退库
  907. T_date := c.GetString("T_date")
  908. T_product := c.GetString("T_product")
  909. T_remark := c.GetString("T_remark")
  910. T_project := c.GetString("T_project")
  911. T_return_user := c.GetString("T_return_user")
  912. T_batch_number := c.GetString("T_batch_number")
  913. T_company_name := c.GetString("T_company_name")
  914. date, is := lib.DateStrToTime(T_date)
  915. if !is {
  916. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  917. c.ServeJSON()
  918. return
  919. }
  920. if T_type == 2 && len(T_project) == 0 {
  921. c.Data["json"] = lib.JSONS{Code: 202, Msg: "退库时,项目不能为空!"}
  922. c.ServeJSON()
  923. return
  924. }
  925. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "入库", T_product)
  926. var_ := Stock.StockIn{
  927. T_number: T_number,
  928. T_depot_id: T_depot_id,
  929. T_type: T_type,
  930. T_date: T_date,
  931. T_remark: T_remark,
  932. T_submit: c.User.T_uuid,
  933. T_project: T_project,
  934. T_return_user: T_return_user,
  935. T_batch_number: T_batch_number,
  936. T_state: Stock.StockInWarehouse,
  937. T_warehouse: c.User.T_uuid,
  938. T_company_name: T_company_name,
  939. }
  940. StockInProductDao := Stock.NewStockInProduct(o)
  941. StockDao := Stock.NewStock(o)
  942. DeviceDao := Stock.NewDevice(o)
  943. _, err = StockInDao.Add_StockIn(var_)
  944. if err != nil {
  945. o.Rollback()
  946. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  947. c.ServeJSON()
  948. return
  949. }
  950. if len(T_product) == 0 {
  951. o.Rollback()
  952. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  953. c.ServeJSON()
  954. return
  955. }
  956. productList := lib.SplitString(T_product, "|")
  957. allProductList := []int{}
  958. for _, v := range productList {
  959. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  960. allProductList = append(allProductList, product_id)
  961. product, _ := Basic.Read_Product_ById(product_id)
  962. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  963. T_relation_sn := strings.Split(v, "-")[2]
  964. if T_relation_sn == "" && product.T_relation_sn == 1 {
  965. o.Rollback()
  966. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  967. c.ServeJSON()
  968. return
  969. }
  970. if len(T_relation_sn) > 0 {
  971. snList := strings.Split(strings.Trim(T_relation_sn, ","), ",")
  972. num = len(snList)
  973. for _, sn := range snList {
  974. mqtt := Stock.Read_MqttUser(sn)
  975. // 添加设备
  976. device := Stock.Device{
  977. T_contract_number: "",
  978. T_out_number: "",
  979. T_product_id: product_id,
  980. T_in_number: T_number,
  981. T_sn: sn,
  982. T_iccid: mqtt.Iccid,
  983. T_imei: mqtt.Imei,
  984. T_State: 2,
  985. T_batch_number: T_batch_number,
  986. CreateTime: date,
  987. }
  988. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  989. if err != nil {
  990. o.Rollback()
  991. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  992. c.ServeJSON()
  993. return
  994. }
  995. }
  996. }
  997. stockInProduct := Stock.StockInProduct{
  998. T_number: T_number,
  999. T_product_id: product_id,
  1000. T_depot_id: T_depot_id,
  1001. T_num: num, // 入库数量
  1002. T_date: T_date, // 入库日期
  1003. T_relation_sn: T_relation_sn,
  1004. T_batch_number: T_batch_number,
  1005. }
  1006. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  1007. if err != nil {
  1008. o.Rollback()
  1009. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1010. c.ServeJSON()
  1011. return
  1012. }
  1013. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  1014. if err != nil {
  1015. o.Rollback()
  1016. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1017. c.ServeJSON()
  1018. return
  1019. }
  1020. }
  1021. // 批量更新所有相关设备的批次号
  1022. err = DeviceDao.Update_Device_BatchNumber_ByInNumber(T_number, T_batch_number)
  1023. if err != nil {
  1024. o.Rollback()
  1025. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新设备批次号失败"}
  1026. c.ServeJSON()
  1027. return
  1028. }
  1029. o.Commit()
  1030. StockIn_Edit_StockMonth(T_date, T_depot_id, allProductList)
  1031. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "入库", var_)
  1032. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1033. c.ServeJSON()
  1034. return
  1035. }
  1036. func StockIn_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  1037. if len(T_date) == 0 {
  1038. return errors.New("日期不能为空")
  1039. }
  1040. date, _ := lib.DateStrToTime(T_date)
  1041. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  1042. // // 修改本月数据不同步库存
  1043. // return nil
  1044. //}
  1045. //T_month := date.Format("2006-01")
  1046. months := generateMonthList(date, "in")
  1047. o := orm.NewOrm()
  1048. StockOutDao := Stock.NewStockOut(o)
  1049. StockMonthDao := Stock.NewStockMonth(o)
  1050. StockOutProductDao := Stock.NewStockOutProduct(o)
  1051. StockInProductDao := Stock.NewStockInProduct(o)
  1052. for _, T_product_id := range allProductList {
  1053. for _, T_month := range months {
  1054. // 获取当前产品本月出入库数量
  1055. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  1056. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  1057. if stockMonth.Id > 0 {
  1058. // 修改
  1059. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  1060. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(T_depot_id, T_product_id, T_month)
  1061. if len(lastMonth) > 0 {
  1062. stockMonth.T_beginning = lastMonth[0].T_ending
  1063. } else {
  1064. stockMonth.T_beginning = 0
  1065. }
  1066. // 期末库存 = 期初库存+入库-出库
  1067. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  1068. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_return", "T_beginning", "T_ending")
  1069. if err != nil {
  1070. return err
  1071. }
  1072. } else {
  1073. product, _ := Basic.Read_Product_ById(T_product_id)
  1074. // 添加库存记录
  1075. stockMonth = Stock.StockMonth{
  1076. T_depot_id: T_depot_id,
  1077. T_product_id: T_product_id,
  1078. T_product_class: product.T_class,
  1079. T_month: T_month,
  1080. }
  1081. // 获取当前产品本月出入库数量
  1082. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  1083. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  1084. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(T_depot_id, T_product_id, T_month)
  1085. if len(lastMonth) > 0 {
  1086. stockMonth.T_beginning = lastMonth[0].T_ending
  1087. } else {
  1088. stockMonth.T_beginning = 0
  1089. }
  1090. // 期末库存 = 期初库存+入库-出库
  1091. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  1092. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  1093. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  1094. if err != nil {
  1095. return err
  1096. }
  1097. }
  1098. }
  1099. }
  1100. return nil
  1101. }
  1102. func (c *StockController) StockIn_Edit() {
  1103. T_number := c.GetString("T_number") // 入库单号
  1104. //T_depot_id, _ := c.GetInt("T_depot_id")
  1105. T_date := c.GetString("T_date")
  1106. T_product := c.GetString("T_product")
  1107. T_remark := c.GetString("T_remark")
  1108. T_project := c.GetString("T_project")
  1109. T_return_user := c.GetString("T_return_user")
  1110. T_batch_number := c.GetString("T_batch_number")
  1111. T_company_name := c.GetString("T_company_name")
  1112. date, is := lib.DateStrToTime(T_date)
  1113. if !is {
  1114. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1115. c.ServeJSON()
  1116. return
  1117. }
  1118. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  1119. T_date = ""
  1120. }
  1121. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改入库", T_product)
  1122. o := orm.NewOrm()
  1123. o.Begin()
  1124. StockInDao := Stock.NewStockIn(o)
  1125. StockInProductDao := Stock.NewStockInProduct(o)
  1126. StockDao := Stock.NewStock(o)
  1127. DeviceDao := Stock.NewDevice(o)
  1128. // 查询入库信息
  1129. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1130. if err != nil {
  1131. o.Rollback()
  1132. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1133. c.ServeJSON()
  1134. return
  1135. }
  1136. T_old_date := stockIn.T_date
  1137. // 查询入库产品信息
  1138. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1139. var oldProductList []dto.StockProduct
  1140. var newProductList []dto.StockProduct
  1141. allProductListMap := make(map[int]struct{})
  1142. productOldMap := map[int]dto.StockProduct{}
  1143. for _, product := range productOldList {
  1144. stockProduct := dto.StockProduct{
  1145. T_product_id: product.T_product_id,
  1146. T_num: product.T_num,
  1147. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1148. }
  1149. oldProductList = append(oldProductList, stockProduct)
  1150. productOldMap[product.T_product_id] = stockProduct
  1151. if _, ok := allProductListMap[product.T_product_id]; !ok {
  1152. allProductListMap[product.T_product_id] = struct{}{}
  1153. }
  1154. }
  1155. productNewList := lib.SplitString(T_product, "|")
  1156. productNewMap := map[int]dto.StockProduct{}
  1157. for _, v := range productNewList {
  1158. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  1159. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  1160. T_relation_sn := strings.Split(v, "-")[2]
  1161. snList := lib.SplitString(T_relation_sn, ",")
  1162. if len(snList) > 0 {
  1163. num = len(snList)
  1164. }
  1165. stockProduct := dto.StockProduct{
  1166. T_product_id: product_id,
  1167. T_num: num,
  1168. T_relation_sn: snList,
  1169. }
  1170. newProductList = append(newProductList, stockProduct)
  1171. productNewMap[product_id] = stockProduct
  1172. if _, ok := allProductListMap[product_id]; !ok {
  1173. allProductListMap[product_id] = struct{}{}
  1174. }
  1175. }
  1176. // 判断产品列表信息是否相同
  1177. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  1178. // 两次提交的数据相同,则不用修改
  1179. if !StockProductListIsEqual {
  1180. if len(needDelete) > 0 {
  1181. // 删除入库产品列表
  1182. for _, v := range needDelete {
  1183. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1184. // 删除入库产品列表
  1185. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  1186. if err != nil {
  1187. o.Rollback()
  1188. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  1189. c.ServeJSON()
  1190. return
  1191. }
  1192. if product.T_relation_sn == 1 {
  1193. // 删除该入库单关联sn
  1194. for _, sn := range v.T_relation_sn {
  1195. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1196. if err != nil {
  1197. o.Rollback()
  1198. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  1199. c.ServeJSON()
  1200. return
  1201. }
  1202. }
  1203. }
  1204. // 减少库存
  1205. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  1206. if err != nil {
  1207. o.Rollback()
  1208. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1209. c.ServeJSON()
  1210. return
  1211. }
  1212. }
  1213. }
  1214. if len(needAdd) > 0 {
  1215. // 新增入库产品列表
  1216. for _, v := range needAdd {
  1217. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1218. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  1219. o.Rollback()
  1220. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  1221. c.ServeJSON()
  1222. return
  1223. }
  1224. // 添加该入库单关联sn
  1225. for _, sn := range v.T_relation_sn {
  1226. mqtt := Stock.Read_MqttUser(sn)
  1227. // 添加设备
  1228. device := Stock.Device{
  1229. T_contract_number: "",
  1230. T_out_number: "",
  1231. T_product_id: v.T_product_id,
  1232. T_in_number: stockIn.T_number,
  1233. T_sn: sn,
  1234. T_iccid: mqtt.Iccid,
  1235. T_imei: mqtt.Imei,
  1236. T_State: 2,
  1237. T_batch_number: T_batch_number,
  1238. CreateTime: date,
  1239. UpdateTime: stockIn.UpdateTime,
  1240. }
  1241. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  1242. if err != nil {
  1243. o.Rollback()
  1244. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  1245. c.ServeJSON()
  1246. return
  1247. }
  1248. }
  1249. stockInProduct := Stock.StockInProduct{
  1250. T_number: T_number,
  1251. T_product_id: v.T_product_id,
  1252. T_depot_id: stockIn.T_depot_id,
  1253. T_num: v.T_num, // 入库数量
  1254. T_date: stockIn.T_date, // 入库日期
  1255. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  1256. }
  1257. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  1258. if err != nil {
  1259. o.Rollback()
  1260. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1261. c.ServeJSON()
  1262. return
  1263. }
  1264. // 增加库存
  1265. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  1266. if err != nil {
  1267. o.Rollback()
  1268. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  1269. c.ServeJSON()
  1270. return
  1271. }
  1272. }
  1273. }
  1274. if len(needEdit) > 0 {
  1275. for _, v := range needEdit {
  1276. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1277. stockInProduct := Stock.StockInProduct{
  1278. T_number: stockIn.T_number,
  1279. T_product_id: v.T_product_id,
  1280. T_depot_id: stockIn.T_depot_id,
  1281. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  1282. T_date: stockIn.T_date, // 入库日期
  1283. }
  1284. // 入库数量比之前多,增加库存
  1285. var T_type int
  1286. var T_num int
  1287. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  1288. // 增加库存
  1289. T_type = 2
  1290. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  1291. } else {
  1292. // 减少库存
  1293. T_type = 1
  1294. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  1295. }
  1296. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  1297. if err != nil {
  1298. o.Rollback()
  1299. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1300. c.ServeJSON()
  1301. return
  1302. }
  1303. // 更新产品库存表
  1304. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  1305. if err != nil {
  1306. o.Rollback()
  1307. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  1308. c.ServeJSON()
  1309. return
  1310. }
  1311. }
  1312. }
  1313. for _, diff := range snDiff {
  1314. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  1315. // 删除减少的sn
  1316. if len(diff.T_delete_relation_sn) > 0 {
  1317. for _, sn := range diff.T_delete_relation_sn {
  1318. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1319. if err != nil {
  1320. o.Rollback()
  1321. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1322. c.ServeJSON()
  1323. return
  1324. }
  1325. }
  1326. // 减少库存
  1327. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_delete_relation_sn), 1)
  1328. if err != nil {
  1329. o.Rollback()
  1330. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1331. c.ServeJSON()
  1332. return
  1333. }
  1334. }
  1335. if len(diff.T_add_relation_sn) > 0 {
  1336. for _, sn := range diff.T_add_relation_sn {
  1337. mqtt := Stock.Read_MqttUser(sn)
  1338. // 添加设备
  1339. device := Stock.Device{
  1340. T_contract_number: "",
  1341. T_out_number: "",
  1342. T_product_id: diff.T_product_id,
  1343. T_in_number: stockIn.T_number,
  1344. T_sn: sn,
  1345. T_iccid: mqtt.Iccid,
  1346. T_imei: mqtt.Imei,
  1347. T_State: 2,
  1348. CreateTime: date,
  1349. UpdateTime: stockIn.UpdateTime,
  1350. }
  1351. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  1352. if err != nil {
  1353. o.Rollback()
  1354. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  1355. c.ServeJSON()
  1356. return
  1357. }
  1358. }
  1359. // 增加库存
  1360. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_add_relation_sn), 2)
  1361. if err != nil {
  1362. o.Rollback()
  1363. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1364. c.ServeJSON()
  1365. return
  1366. }
  1367. }
  1368. stockInProduct := Stock.StockInProduct{
  1369. T_number: stockIn.T_number,
  1370. T_product_id: diff.T_product_id,
  1371. T_depot_id: stockIn.T_depot_id,
  1372. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  1373. T_date: stockIn.T_date, // 入库日期
  1374. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  1375. }
  1376. // 更新产品库存表
  1377. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  1378. if err != nil {
  1379. o.Rollback()
  1380. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  1381. c.ServeJSON()
  1382. return
  1383. }
  1384. }
  1385. }
  1386. stockIn.T_remark = T_remark
  1387. if len(T_date) > 0 {
  1388. stockIn.T_date = T_date
  1389. }
  1390. if len(T_project) > 0 {
  1391. stockIn.T_project = T_project
  1392. }
  1393. if len(T_return_user) > 0 {
  1394. stockIn.T_return_user = T_return_user
  1395. }
  1396. if len(T_batch_number) > 0 {
  1397. stockIn.T_batch_number = T_batch_number
  1398. }
  1399. if len(T_company_name) > 0 {
  1400. stockIn.T_company_name = T_company_name
  1401. }
  1402. err = StockInDao.Update_StockIn(stockIn, "T_remark", "T_date", "T_project", "T_return_user", "T_batch_number", "T_company_name")
  1403. if err != nil {
  1404. o.Rollback()
  1405. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  1406. c.ServeJSON()
  1407. return
  1408. }
  1409. o.Commit()
  1410. if len(T_date) > 0 && T_old_date != T_date {
  1411. // 修改出库产品日期
  1412. StockInProductDao.Update_StockInProduct_T_date(stockIn.T_number, T_date)
  1413. DeviceDao.Update_Device_CreateTimeByT_in_number(stockIn.T_number, T_date)
  1414. }
  1415. // 修改出库产品日期
  1416. StockInProductDao.Update_StockInProduct_T_batch_number(stockIn.T_number, T_batch_number)
  1417. // 修改设备批次号
  1418. DeviceDao.Update_Device_BatchNumber_ByInNumber(stockIn.T_number, T_batch_number)
  1419. // 更新月份统计表
  1420. allProductList := []int{}
  1421. for i, _ := range allProductListMap {
  1422. allProductList = append(allProductList, i)
  1423. }
  1424. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1425. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "修改", stockIn)
  1426. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1427. c.ServeJSON()
  1428. return
  1429. }
  1430. // 删除入库
  1431. func (c *StockController) StockIn_Del() {
  1432. T_number := c.GetString("T_number") // 入库单号
  1433. o := orm.NewOrm()
  1434. o.Begin()
  1435. StockInDao := Stock.NewStockIn(o)
  1436. StockInProductDao := Stock.NewStockInProduct(o)
  1437. StockDao := Stock.NewStock(o)
  1438. DeviceDao := Stock.NewDevice(o)
  1439. // 查询入库信息
  1440. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1441. if err != nil {
  1442. o.Rollback()
  1443. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1444. c.ServeJSON()
  1445. return
  1446. }
  1447. // 查询入库产品信息
  1448. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1449. var oldProductList []dto.StockProduct
  1450. allProductList := []int{}
  1451. for _, product := range productOldList {
  1452. oldProductList = append(oldProductList, dto.StockProduct{
  1453. T_product_id: product.T_product_id,
  1454. T_num: product.T_num,
  1455. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1456. })
  1457. allProductList = append(allProductList, product.T_product_id)
  1458. }
  1459. // 删除入库产品列表
  1460. for _, v := range oldProductList {
  1461. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1462. // 删除入库产品列表
  1463. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  1464. if err != nil {
  1465. o.Rollback()
  1466. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  1467. c.ServeJSON()
  1468. return
  1469. }
  1470. if product.T_relation_sn == 1 {
  1471. // 删除该入库单关联sn
  1472. for _, sn := range v.T_relation_sn {
  1473. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1474. if err != nil {
  1475. o.Rollback()
  1476. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  1477. c.ServeJSON()
  1478. return
  1479. }
  1480. }
  1481. }
  1482. // 减少库存
  1483. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  1484. if err != nil {
  1485. o.Rollback()
  1486. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1487. c.ServeJSON()
  1488. return
  1489. }
  1490. }
  1491. err = StockInDao.Delete_StockIn(stockIn)
  1492. if err != nil {
  1493. o.Rollback()
  1494. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库失败"}
  1495. c.ServeJSON()
  1496. return
  1497. }
  1498. o.Commit()
  1499. // 更新月份统计表
  1500. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1501. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "删除", stockIn)
  1502. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1503. c.ServeJSON()
  1504. return
  1505. }
  1506. // 导出出库单
  1507. func (c *StockController) StockIn_Excel() {
  1508. // 查询
  1509. T_number := c.GetString("T_number")
  1510. o := orm.NewOrm()
  1511. StockInDao := Stock.NewStockIn(o)
  1512. StockInProductDao := Stock.NewStockInProduct(o)
  1513. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1514. if err != nil {
  1515. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1516. c.ServeJSON()
  1517. return
  1518. }
  1519. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1520. var pList []Stock.StockInProduct_R
  1521. for _, v := range productList {
  1522. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1523. }
  1524. userList, _ := NatsServer.Read_User_List_All()
  1525. Account.Read_User_All_Map(userList)
  1526. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1527. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1528. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1529. f := excelize.NewFile() // 设置单元格的值
  1530. Style1, _ := f.NewStyle(
  1531. &excelize.Style{
  1532. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1533. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1534. })
  1535. Style2, _ := f.NewStyle(
  1536. &excelize.Style{
  1537. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1538. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1539. })
  1540. Style3, _ := f.NewStyle(
  1541. &excelize.Style{
  1542. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1543. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1544. Border: []excelize.Border{
  1545. {Type: "left", Color: "000000", Style: 1},
  1546. {Type: "top", Color: "000000", Style: 1},
  1547. {Type: "bottom", Color: "000000", Style: 1},
  1548. {Type: "right", Color: "000000", Style: 1},
  1549. },
  1550. })
  1551. Style4, _ := f.NewStyle(
  1552. &excelize.Style{
  1553. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1554. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1555. Border: []excelize.Border{
  1556. {Type: "left", Color: "000000", Style: 1},
  1557. {Type: "top", Color: "000000", Style: 1},
  1558. {Type: "bottom", Color: "000000", Style: 1},
  1559. {Type: "right", Color: "000000", Style: 1},
  1560. },
  1561. })
  1562. productList2 := lib.ChunkBy(productList, 10)
  1563. for i, products := range productList2 {
  1564. if len(products) < 10 {
  1565. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1566. }
  1567. row := i*17 + 1
  1568. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1569. f.SetRowStyle("Sheet1", row, row, Style1)
  1570. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1571. f.SetRowHeight("Sheet1", row, 20)
  1572. row += 1
  1573. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1574. f.SetRowStyle("Sheet1", row, row, Style1)
  1575. title := "硬件入库单"
  1576. if len(productList2) > 1 {
  1577. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1578. }
  1579. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1580. f.SetRowHeight("Sheet1", row, 20)
  1581. row += 1
  1582. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1583. f.SetRowStyle("Sheet1", row, row, Style2)
  1584. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1585. f.SetRowHeight("Sheet1", row, 20)
  1586. row += 1
  1587. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1588. f.SetRowStyle("Sheet1", row, row, Style2)
  1589. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1590. f.SetRowHeight("Sheet1", row, 20)
  1591. row += 1
  1592. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1593. f.SetRowStyle("Sheet1", row, row, Style2)
  1594. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1595. f.SetRowHeight("Sheet1", row, 20)
  1596. // 这里设置表头
  1597. row += 1
  1598. f.SetRowHeight("Sheet1", row, 20)
  1599. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1600. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1601. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1602. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1603. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1604. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1605. // 设置列宽
  1606. f.SetColWidth("Sheet1", "A", "A", 5)
  1607. f.SetColWidth("Sheet1", "B", "B", 20)
  1608. f.SetColWidth("Sheet1", "C", "C", 12)
  1609. f.SetColWidth("Sheet1", "D", "D", 10)
  1610. f.SetColWidth("Sheet1", "E", "E", 10)
  1611. f.SetColWidth("Sheet1", "F", "F", 15)
  1612. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1613. sRow := row
  1614. // 循环写入数据
  1615. for j, v := range products {
  1616. row += 1
  1617. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1618. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1619. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1620. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1621. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1622. if v.T_num > 0 {
  1623. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1624. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1625. }
  1626. }
  1627. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1628. row += 1
  1629. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1630. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1631. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1632. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1633. f.SetRowStyle("Sheet1", row, row, Style2)
  1634. f.SetRowHeight("Sheet1", row, 20)
  1635. }
  1636. // 保存文件
  1637. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1638. fmt.Println(err)
  1639. }
  1640. var url string
  1641. //// 上传 OSS
  1642. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1643. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1644. if !is {
  1645. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1646. c.ServeJSON()
  1647. return
  1648. }
  1649. //删除目录
  1650. err = os.Remove("ofile/" + filename + ".xlsx")
  1651. if err != nil {
  1652. logs.Error(lib.FuncName(), err)
  1653. }
  1654. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1655. c.ServeJSON()
  1656. return
  1657. }
  1658. // 批量导出出库单
  1659. func (c *StockController) StockIn_Excel_Batch() {
  1660. // 查询
  1661. T_number_list := c.GetString("T_number_list")
  1662. o := orm.NewOrm()
  1663. StockInDao := Stock.NewStockIn(o)
  1664. StockInProductDao := Stock.NewStockInProduct(o)
  1665. list := strings.Split(strings.Trim(T_number_list, "|"), "|")
  1666. userList, _ := NatsServer.Read_User_List_All()
  1667. Account.Read_User_All_Map(userList)
  1668. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1669. f := excelize.NewFile() // 设置单元格的值
  1670. Style1, _ := f.NewStyle(
  1671. &excelize.Style{
  1672. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1673. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1674. })
  1675. Style2, _ := f.NewStyle(
  1676. &excelize.Style{
  1677. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1678. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1679. })
  1680. Style3, _ := f.NewStyle(
  1681. &excelize.Style{
  1682. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1683. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1684. Border: []excelize.Border{
  1685. {Type: "left", Color: "000000", Style: 1},
  1686. {Type: "top", Color: "000000", Style: 1},
  1687. {Type: "bottom", Color: "000000", Style: 1},
  1688. {Type: "right", Color: "000000", Style: 1},
  1689. },
  1690. })
  1691. Style4, _ := f.NewStyle(
  1692. &excelize.Style{
  1693. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1694. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1695. Border: []excelize.Border{
  1696. {Type: "left", Color: "000000", Style: 1},
  1697. {Type: "top", Color: "000000", Style: 1},
  1698. {Type: "bottom", Color: "000000", Style: 1},
  1699. {Type: "right", Color: "000000", Style: 1},
  1700. },
  1701. })
  1702. var num int
  1703. for _, T_number := range list {
  1704. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1705. if err != nil {
  1706. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1707. c.ServeJSON()
  1708. return
  1709. }
  1710. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1711. var pList []Stock.StockInProduct_R
  1712. for _, v := range productList {
  1713. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1714. }
  1715. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1716. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1717. productList2 := lib.ChunkBy(productList, 10)
  1718. for i, products := range productList2 {
  1719. if len(products) < 10 {
  1720. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1721. }
  1722. row := num*18 + 1
  1723. num += 1
  1724. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1725. f.SetRowStyle("Sheet1", row, row, Style1)
  1726. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1727. f.SetRowHeight("Sheet1", row, 20)
  1728. row += 1
  1729. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1730. f.SetRowStyle("Sheet1", row, row, Style1)
  1731. title := "硬件入库单"
  1732. if len(productList2) > 1 {
  1733. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1734. }
  1735. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1736. f.SetRowHeight("Sheet1", row, 20)
  1737. row += 1
  1738. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1739. f.SetRowStyle("Sheet1", row, row, Style2)
  1740. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1741. f.SetRowHeight("Sheet1", row, 20)
  1742. row += 1
  1743. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1744. f.SetRowStyle("Sheet1", row, row, Style2)
  1745. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1746. f.SetRowHeight("Sheet1", row, 20)
  1747. row += 1
  1748. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1749. f.SetRowStyle("Sheet1", row, row, Style2)
  1750. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1751. f.SetRowHeight("Sheet1", row, 20)
  1752. // 这里设置表头
  1753. row += 1
  1754. f.SetRowHeight("Sheet1", row, 20)
  1755. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1756. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1757. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1758. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1759. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1760. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1761. // 设置列宽
  1762. f.SetColWidth("Sheet1", "A", "A", 5)
  1763. f.SetColWidth("Sheet1", "B", "B", 20)
  1764. f.SetColWidth("Sheet1", "C", "C", 12)
  1765. f.SetColWidth("Sheet1", "D", "D", 10)
  1766. f.SetColWidth("Sheet1", "E", "E", 10)
  1767. f.SetColWidth("Sheet1", "F", "F", 15)
  1768. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1769. sRow := row
  1770. // 循环写入数据
  1771. for j, v := range products {
  1772. row += 1
  1773. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1774. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1775. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1776. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1777. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1778. if v.T_num > 0 {
  1779. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1780. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1781. }
  1782. }
  1783. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1784. row += 1
  1785. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1786. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1787. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1788. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1789. f.SetRowStyle("Sheet1", row, row, Style2)
  1790. f.SetRowHeight("Sheet1", row, 20)
  1791. // 空一行
  1792. row += 1
  1793. //f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1794. //f.SetRowStyle("Sheet1", row, row, Style1)
  1795. //f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "")
  1796. // 插入下一页分页符
  1797. f.InsertPageBreak("Sheet1", fmt.Sprintf("A%d", row))
  1798. }
  1799. }
  1800. // 保存文件
  1801. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1802. fmt.Println(err)
  1803. }
  1804. var url string
  1805. //// 上传 OSS
  1806. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1807. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1808. if !is {
  1809. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1810. c.ServeJSON()
  1811. return
  1812. }
  1813. //删除目录
  1814. err := os.Remove("ofile/" + filename + ".xlsx")
  1815. if err != nil {
  1816. logs.Error(lib.FuncName(), err)
  1817. }
  1818. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1819. c.ServeJSON()
  1820. return
  1821. }
  1822. // 退库申请
  1823. func (c *StockController) StockIn_Apply_List() {
  1824. // 分页参数 初始化
  1825. page, _ := c.GetInt("page")
  1826. if page < 1 {
  1827. page = 1
  1828. }
  1829. page_z, _ := c.GetInt("page_z")
  1830. if page_z < 1 {
  1831. page_z = conf.Page_size
  1832. }
  1833. // 查询
  1834. T_depot_id, _ := c.GetInt("T_depot_id")
  1835. T_start_date := c.GetString("T_start_date")
  1836. T_end_date := c.GetString("T_end_date")
  1837. T_name := c.GetString("T_name")
  1838. T_state := c.GetString("T_state")
  1839. userList, _ := NatsServer.Read_User_List_All()
  1840. Account.Read_User_All_Map(userList)
  1841. Basic.Read_Depot_All_Map()
  1842. StockInDao := Stock.NewStockIn(orm.NewOrm())
  1843. R_List, R_cnt := StockInDao.Read_StockIn_Apply_List(Stock.StockInTypeApply, c.User.T_uuid, T_state, T_name, T_depot_id, T_start_date, T_end_date, page, page_z)
  1844. var r_jsons lib.R_JSONS
  1845. r_jsons.Num = R_cnt
  1846. r_jsons.Data = R_List
  1847. r_jsons.Page = page
  1848. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1849. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1850. c.ServeJSON()
  1851. return
  1852. }
  1853. func (c *StockController) StockIn_Apply_Warehouse_List() {
  1854. // 分页参数 初始化
  1855. page, _ := c.GetInt("page")
  1856. if page < 1 {
  1857. page = 1
  1858. }
  1859. page_z, _ := c.GetInt("page_z")
  1860. if page_z < 1 {
  1861. page_z = conf.Page_size
  1862. }
  1863. // 查询
  1864. T_depot_id, _ := c.GetInt("T_depot_id")
  1865. T_start_date := c.GetString("T_start_date")
  1866. T_end_date := c.GetString("T_end_date")
  1867. T_name := c.GetString("T_name")
  1868. T_state := c.GetString("T_state")
  1869. T_uuid := c.GetString("T_uuid")
  1870. userList, _ := NatsServer.Read_User_List_All()
  1871. Account.Read_User_All_Map(userList)
  1872. Basic.Read_Depot_All_Map()
  1873. StockInDao := Stock.NewStockIn(orm.NewOrm())
  1874. R_List, R_cnt := StockInDao.Read_StockIn_Apply_List(Stock.StockInTypeWarehouse, T_uuid, T_state, T_name, T_depot_id, T_start_date, T_end_date, page, page_z)
  1875. var r_jsons lib.R_JSONS
  1876. r_jsons.Num = R_cnt
  1877. r_jsons.Data = R_List
  1878. r_jsons.Page = page
  1879. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1880. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1881. c.ServeJSON()
  1882. return
  1883. }
  1884. func (c *StockController) StockIn_Apply() {
  1885. o := orm.NewOrm()
  1886. o.Begin()
  1887. StockInDao := Stock.NewStockIn(o)
  1888. T_number := c.GetString("T_number")
  1889. // 检查前端传入的T_number是否已提交过
  1890. _, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1891. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  1892. // 数据库查询错误
  1893. o.Rollback()
  1894. c.Data["json"] = lib.JSONS{Code: 203, Msg: "查询入库编号失败"}
  1895. c.ServeJSON()
  1896. return
  1897. }
  1898. if err == nil {
  1899. // 入库编号已存在,不需要做任何处理
  1900. o.Rollback()
  1901. c.Data["json"] = lib.JSONS{Code: 200, Msg: "入库编号已存在,无需重复提交!", Data: T_number}
  1902. c.ServeJSON()
  1903. return
  1904. }
  1905. T_depot_id, _ := c.GetInt("T_depot_id")
  1906. T_product := c.GetString("T_product")
  1907. T_remark := c.GetString("T_remark")
  1908. T_project := c.GetString("T_project")
  1909. T_company_name := c.GetString("T_company_name")
  1910. T_return_user := c.GetString("T_return_user")
  1911. if len(T_return_user) == 0 {
  1912. T_return_user = c.User.T_uuid
  1913. }
  1914. if len(T_project) == 0 {
  1915. c.Data["json"] = lib.JSONS{Code: 202, Msg: "项目不能为空!"}
  1916. c.ServeJSON()
  1917. return
  1918. }
  1919. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "退库", T_product)
  1920. var_ := Stock.StockIn{
  1921. T_number: T_number,
  1922. T_depot_id: T_depot_id,
  1923. T_type: 2,
  1924. T_remark: T_remark,
  1925. T_submit: c.User.T_uuid,
  1926. T_project: T_project,
  1927. T_return_user: T_return_user,
  1928. T_company_name: T_company_name,
  1929. T_state: 1,
  1930. T_application_date: time.Now().Format("2006-01-02"),
  1931. }
  1932. StockInProductDao := Stock.NewStockInProduct(o)
  1933. _, err = StockInDao.Add_StockIn(var_)
  1934. if err != nil {
  1935. o.Rollback()
  1936. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1937. c.ServeJSON()
  1938. return
  1939. }
  1940. if len(T_product) == 0 {
  1941. o.Rollback()
  1942. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  1943. c.ServeJSON()
  1944. return
  1945. }
  1946. productList := lib.SplitString(T_product, "|")
  1947. allProductList := []int{}
  1948. for _, v := range productList {
  1949. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  1950. allProductList = append(allProductList, product_id)
  1951. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  1952. stockInProduct := Stock.StockInProduct{
  1953. T_number: T_number,
  1954. T_product_id: product_id,
  1955. T_depot_id: T_depot_id,
  1956. T_num: num, // 入库数量
  1957. }
  1958. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  1959. if err != nil {
  1960. o.Rollback()
  1961. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1962. c.ServeJSON()
  1963. return
  1964. }
  1965. }
  1966. o.Commit()
  1967. NatsServer.AddUserLogs(c.User.T_uuid, "退库申请", "退库申请", var_)
  1968. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1969. c.ServeJSON()
  1970. return
  1971. }
  1972. func (c *StockController) StockIn_Apply_Edit() {
  1973. T_number := c.GetString("T_number") // 入库单号
  1974. //T_depot_id, _ := c.GetInt("T_depot_id")
  1975. T_product := c.GetString("T_product")
  1976. T_remark := c.GetString("T_remark")
  1977. T_project := c.GetString("T_project")
  1978. T_batch_number := c.GetString("T_batch_number")
  1979. T_company_name := c.GetString("T_company_name")
  1980. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改入库", T_product)
  1981. o := orm.NewOrm()
  1982. o.Begin()
  1983. StockInDao := Stock.NewStockIn(o)
  1984. StockInProductDao := Stock.NewStockInProduct(o)
  1985. // 查询入库信息
  1986. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1987. if err != nil {
  1988. o.Rollback()
  1989. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1990. c.ServeJSON()
  1991. return
  1992. }
  1993. // 查询入库产品信息
  1994. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1995. var oldProductList []dto.StockProduct
  1996. var newProductList []dto.StockProduct
  1997. allProductListMap := make(map[int]struct{})
  1998. productOldMap := map[int]dto.StockProduct{}
  1999. for _, product := range productOldList {
  2000. stockProduct := dto.StockProduct{
  2001. T_product_id: product.T_product_id,
  2002. T_num: product.T_num,
  2003. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2004. }
  2005. oldProductList = append(oldProductList, stockProduct)
  2006. productOldMap[product.T_product_id] = stockProduct
  2007. if _, ok := allProductListMap[product.T_product_id]; !ok {
  2008. allProductListMap[product.T_product_id] = struct{}{}
  2009. }
  2010. }
  2011. productNewList := lib.SplitString(T_product, "|")
  2012. productNewMap := map[int]dto.StockProduct{}
  2013. for _, v := range productNewList {
  2014. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2015. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2016. T_relation_sn := strings.Split(v, "-")[2]
  2017. snList := lib.SplitString(T_relation_sn, ",")
  2018. if len(snList) > 0 {
  2019. num = len(snList)
  2020. }
  2021. stockProduct := dto.StockProduct{
  2022. T_product_id: product_id,
  2023. T_num: num,
  2024. T_relation_sn: snList,
  2025. }
  2026. newProductList = append(newProductList, stockProduct)
  2027. productNewMap[product_id] = stockProduct
  2028. if _, ok := allProductListMap[product_id]; !ok {
  2029. allProductListMap[product_id] = struct{}{}
  2030. }
  2031. }
  2032. // 判断产品列表信息是否相同
  2033. StockProductListIsEqual, needDelete, needAdd, needEdit, _ := dto.StockProductListsEqual(oldProductList, newProductList)
  2034. // 两次提交的数据相同,则不用修改
  2035. if !StockProductListIsEqual {
  2036. if len(needDelete) > 0 {
  2037. // 删除入库产品列表
  2038. for _, v := range needDelete {
  2039. // 删除入库产品列表
  2040. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  2041. if err != nil {
  2042. o.Rollback()
  2043. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  2044. c.ServeJSON()
  2045. return
  2046. }
  2047. }
  2048. }
  2049. if len(needAdd) > 0 {
  2050. // 新增入库产品列表
  2051. for _, v := range needAdd {
  2052. stockInProduct := Stock.StockInProduct{
  2053. T_number: T_number,
  2054. T_product_id: v.T_product_id,
  2055. T_depot_id: stockIn.T_depot_id,
  2056. T_num: v.T_num, // 入库数量
  2057. T_date: stockIn.T_date, // 入库日期
  2058. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  2059. }
  2060. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  2061. if err != nil {
  2062. o.Rollback()
  2063. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2064. c.ServeJSON()
  2065. return
  2066. }
  2067. }
  2068. }
  2069. if len(needEdit) > 0 {
  2070. for _, v := range needEdit {
  2071. stockInProduct := Stock.StockInProduct{
  2072. T_number: stockIn.T_number,
  2073. T_product_id: v.T_product_id,
  2074. T_depot_id: stockIn.T_depot_id,
  2075. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  2076. T_date: stockIn.T_date, // 入库日期
  2077. }
  2078. // 更新产品库存表
  2079. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  2080. if err != nil {
  2081. o.Rollback()
  2082. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  2083. c.ServeJSON()
  2084. return
  2085. }
  2086. }
  2087. }
  2088. }
  2089. stockIn.T_remark = T_remark
  2090. if len(T_project) > 0 {
  2091. stockIn.T_project = T_project
  2092. }
  2093. if len(T_batch_number) > 0 {
  2094. stockIn.T_batch_number = T_batch_number
  2095. }
  2096. if len(T_company_name) > 0 {
  2097. stockIn.T_company_name = T_company_name
  2098. }
  2099. err = StockInDao.Update_StockIn(stockIn, "T_remark", "T_project", "T_batch_number", "T_company_name")
  2100. if err != nil {
  2101. o.Rollback()
  2102. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  2103. c.ServeJSON()
  2104. return
  2105. }
  2106. o.Commit()
  2107. NatsServer.AddUserLogs(c.User.T_uuid, "退库", "修改", stockIn)
  2108. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2109. c.ServeJSON()
  2110. return
  2111. }
  2112. // 删除入库
  2113. func (c *StockController) StockIn_Apply_Del() {
  2114. T_number := c.GetString("T_number") // 入库单号
  2115. o := orm.NewOrm()
  2116. o.Begin()
  2117. StockInDao := Stock.NewStockIn(o)
  2118. StockInProductDao := Stock.NewStockInProduct(o)
  2119. // 查询入库信息
  2120. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  2121. if err != nil {
  2122. o.Rollback()
  2123. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2124. c.ServeJSON()
  2125. return
  2126. }
  2127. // 查询入库产品信息
  2128. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  2129. var oldProductList []dto.StockProduct
  2130. allProductList := []int{}
  2131. for _, product := range productOldList {
  2132. oldProductList = append(oldProductList, dto.StockProduct{
  2133. T_product_id: product.T_product_id,
  2134. T_num: product.T_num,
  2135. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2136. })
  2137. allProductList = append(allProductList, product.T_product_id)
  2138. }
  2139. // 删除入库产品列表
  2140. for _, v := range oldProductList {
  2141. // 删除入库产品列表
  2142. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  2143. if err != nil {
  2144. o.Rollback()
  2145. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  2146. c.ServeJSON()
  2147. return
  2148. }
  2149. }
  2150. err = StockInDao.Delete_StockIn(stockIn)
  2151. if err != nil {
  2152. o.Rollback()
  2153. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库失败"}
  2154. c.ServeJSON()
  2155. return
  2156. }
  2157. o.Commit()
  2158. NatsServer.AddUserLogs(c.User.T_uuid, "入库申请", "删除", stockIn)
  2159. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2160. c.ServeJSON()
  2161. return
  2162. }
  2163. func (c *StockController) StockIn_Warehouse() {
  2164. T_number := c.GetString("T_number")
  2165. T_date := c.GetString("T_date")
  2166. T_remark := c.GetString("T_remark")
  2167. T_project := c.GetString("T_project")
  2168. T_product := c.GetString("T_product")
  2169. date, is := lib.DateStrToTime(T_date)
  2170. if !is {
  2171. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2172. c.ServeJSON()
  2173. return
  2174. }
  2175. if len(T_project) == 0 {
  2176. c.Data["json"] = lib.JSONS{Code: 202, Msg: "项目不能为空!"}
  2177. c.ServeJSON()
  2178. return
  2179. }
  2180. NatsServer.AddUserLogs(c.User.T_uuid, "退库申请", "确认入库", T_product)
  2181. o := orm.NewOrm()
  2182. o.Begin()
  2183. StockInDao := Stock.NewStockIn(o)
  2184. StockInProductDao := Stock.NewStockInProduct(o)
  2185. StockDao := Stock.NewStock(o)
  2186. DeviceDao := Stock.NewDevice(o)
  2187. // 查询入库信息
  2188. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  2189. if err != nil {
  2190. o.Rollback()
  2191. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2192. c.ServeJSON()
  2193. return
  2194. }
  2195. productList := lib.SplitString(T_product, "|")
  2196. allProductList := []int{}
  2197. for _, v := range productList {
  2198. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2199. allProductList = append(allProductList, product_id)
  2200. product, _ := Basic.Read_Product_ById(product_id)
  2201. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2202. if num == 0 {
  2203. o.Rollback()
  2204. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  2205. c.ServeJSON()
  2206. return
  2207. }
  2208. T_relation_sn := strings.Split(v, "-")[2]
  2209. if T_relation_sn == "" && product.T_relation_sn == 1 {
  2210. o.Rollback()
  2211. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  2212. c.ServeJSON()
  2213. return
  2214. }
  2215. if len(T_relation_sn) > 0 {
  2216. snList := strings.Split(T_relation_sn, ",")
  2217. if product.T_relation_sn == 1 && num != len(snList) {
  2218. o.Rollback()
  2219. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s SN数量与出库数量不一致,请核查!", product.T_name)}
  2220. c.ServeJSON()
  2221. return
  2222. }
  2223. if len(snList) > 0 {
  2224. for _, sn := range snList {
  2225. mqtt := Stock.Read_MqttUser(sn)
  2226. // 根据IMEI查询上次入库的批次号
  2227. batchNumber, err := DeviceDao.Read_Device_BatchNumber_ByImei(mqtt.Imei)
  2228. if err != nil {
  2229. logs.Error(lib.FuncName(), err)
  2230. }
  2231. device := Stock.Device{
  2232. T_product_id: product_id,
  2233. T_in_number: T_number,
  2234. T_sn: sn,
  2235. T_iccid: mqtt.Iccid,
  2236. T_imei: mqtt.Imei,
  2237. T_State: 2,
  2238. T_project: stockIn.T_project,
  2239. T_batch_number: batchNumber,
  2240. CreateTime: date,
  2241. }
  2242. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2243. if err != nil {
  2244. o.Rollback()
  2245. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  2246. c.ServeJSON()
  2247. return
  2248. }
  2249. }
  2250. }
  2251. }
  2252. // 3、添加出库产品清单
  2253. stockInProduct := Stock.StockInProduct{
  2254. T_number: T_number,
  2255. T_product_id: product_id,
  2256. T_depot_id: stockIn.T_depot_id,
  2257. T_num: num, // 入库数量
  2258. T_date: T_date, // 入库日期
  2259. T_relation_sn: T_relation_sn,
  2260. T_state: 1,
  2261. }
  2262. // 更新产品表
  2263. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  2264. if err != nil {
  2265. o.Rollback()
  2266. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  2267. c.ServeJSON()
  2268. return
  2269. }
  2270. // 5、更新产品库存列表、占用库存
  2271. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  2272. if err != nil {
  2273. o.Rollback()
  2274. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2275. c.ServeJSON()
  2276. return
  2277. }
  2278. }
  2279. if len(T_remark) > 0 {
  2280. stockIn.T_remark = T_remark
  2281. }
  2282. if len(T_project) > 0 {
  2283. stockIn.T_project = T_project
  2284. }
  2285. stockIn.T_warehouse = c.User.T_uuid
  2286. stockIn.T_state = Stock.StockInWarehouse
  2287. stockIn.T_date = T_date
  2288. err = StockInDao.Update_StockIn(stockIn, "T_state", "T_date", "T_warehouse", "T_project", "T_remark")
  2289. if err != nil {
  2290. o.Rollback()
  2291. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库审批失败"}
  2292. c.ServeJSON()
  2293. return
  2294. }
  2295. o.Commit()
  2296. // 更新月份统计表
  2297. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  2298. NatsServer.AddUserLogs(c.User.T_uuid, "入库申请", "确认出库", stockIn)
  2299. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2300. c.ServeJSON()
  2301. return
  2302. }
  2303. func (c *StockController) StockOut_List() {
  2304. // 分页参数 初始化
  2305. page, _ := c.GetInt("page")
  2306. if page < 1 {
  2307. page = 1
  2308. }
  2309. page_z, _ := c.GetInt("page_z")
  2310. if page_z < 1 {
  2311. page_z = conf.Page_size
  2312. }
  2313. // 查询
  2314. T_depot_id, _ := c.GetInt("T_depot_id")
  2315. T_contract_number := c.GetString("T_contract_number")
  2316. T_start_date := c.GetString("T_start_date")
  2317. T_end_date := c.GetString("T_end_date")
  2318. T_name := c.GetString("T_name")
  2319. userList, _ := NatsServer.Read_User_List_All()
  2320. Account.Read_User_All_Map(userList)
  2321. Basic.Read_Depot_All_Map()
  2322. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2323. R_List, R_cnt := StockOutDao.Read_StockOut_List(T_name, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  2324. var r_jsons lib.R_JSONS
  2325. r_jsons.Num = R_cnt
  2326. r_jsons.Data = R_List
  2327. r_jsons.Page = page
  2328. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  2329. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2330. c.ServeJSON()
  2331. return
  2332. }
  2333. func (c *StockController) StockOut_List_Product() {
  2334. // 分页参数 初始化
  2335. page, _ := c.GetInt("page")
  2336. if page < 1 {
  2337. page = 1
  2338. }
  2339. page_z, _ := c.GetInt("page_z")
  2340. if page_z < 1 {
  2341. page_z = conf.Page_size
  2342. }
  2343. // 查询
  2344. T_depot_id, _ := c.GetInt("T_depot_id")
  2345. T_name := c.GetString("T_name")
  2346. T_start_date := c.GetString("T_start_date")
  2347. T_end_date := c.GetString("T_end_date")
  2348. T_product_name := c.GetString("T_product_name") // 产品名称
  2349. T_product_model := c.GetString("T_product_model") // 产品型号
  2350. userList, _ := NatsServer.Read_User_List_All()
  2351. Account.Read_User_All_Map(userList)
  2352. Basic.Read_Depot_All_Map()
  2353. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2354. R_List, R_cnt := StockOutDao.Read_StockOutProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, page, page_z)
  2355. var r_jsons lib.R_JSONS
  2356. r_jsons.Num = R_cnt
  2357. r_jsons.Data = R_List
  2358. r_jsons.Page = page
  2359. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  2360. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2361. c.ServeJSON()
  2362. return
  2363. }
  2364. func (c *StockController) StockOut_List_Product_Excel() {
  2365. // 查询
  2366. T_depot_id, _ := c.GetInt("T_depot_id")
  2367. T_name := c.GetString("T_name")
  2368. T_start_date := c.GetString("T_start_date")
  2369. T_end_date := c.GetString("T_end_date")
  2370. T_product_name := c.GetString("T_product_name") // 产品名称
  2371. T_product_model := c.GetString("T_product_model") // 产品型号
  2372. userList, _ := NatsServer.Read_User_List_All()
  2373. Account.Read_User_All_Map(userList)
  2374. Basic.Read_Depot_All_Map()
  2375. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2376. R_List, _ := StockOutDao.Read_StockOutProduct_List(T_name, T_start_date, T_end_date, T_depot_id, T_product_name, T_product_model, 0, 9999)
  2377. filename := fmt.Sprintf("出库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  2378. f := excelize.NewFile() // 设置单元格的值
  2379. Style1, _ := f.NewStyle(
  2380. &excelize.Style{
  2381. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  2382. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  2383. })
  2384. Style2, _ := f.NewStyle(
  2385. &excelize.Style{
  2386. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  2387. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  2388. Border: []excelize.Border{
  2389. {Type: "left", Color: "000000", Style: 1},
  2390. {Type: "top", Color: "000000", Style: 1},
  2391. {Type: "bottom", Color: "000000", Style: 1},
  2392. {Type: "right", Color: "000000", Style: 1},
  2393. },
  2394. })
  2395. Style3, _ := f.NewStyle(
  2396. &excelize.Style{
  2397. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2398. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  2399. Border: []excelize.Border{
  2400. {Type: "left", Color: "000000", Style: 1},
  2401. {Type: "top", Color: "000000", Style: 1},
  2402. {Type: "bottom", Color: "000000", Style: 1},
  2403. {Type: "right", Color: "000000", Style: 1},
  2404. },
  2405. })
  2406. f.MergeCell("Sheet1", "A1", "L1")
  2407. f.SetRowStyle("Sheet1", 1, 1, Style1)
  2408. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("出库明细单"))
  2409. f.SetRowHeight("Sheet1", 1, 30)
  2410. // 这里设置表头
  2411. f.SetCellStyle("Sheet1", "A2", "L2", Style2)
  2412. f.SetRowHeight("Sheet1", 2, 20)
  2413. f.SetCellValue("Sheet1", "A2", "序号")
  2414. f.SetCellValue("Sheet1", "B2", "出库单号")
  2415. f.SetCellValue("Sheet1", "C2", "领取人")
  2416. f.SetCellValue("Sheet1", "D2", "出库日期")
  2417. f.SetCellValue("Sheet1", "E2", "出库仓库")
  2418. f.SetCellValue("Sheet1", "F2", "关联项目")
  2419. f.SetCellValue("Sheet1", "G2", "产品名称")
  2420. f.SetCellValue("Sheet1", "H2", "产品型号")
  2421. f.SetCellValue("Sheet1", "I2", "数量")
  2422. f.SetCellValue("Sheet1", "J2", "是否关联SN")
  2423. f.SetCellValue("Sheet1", "K2", "备注")
  2424. f.SetCellValue("Sheet1", "L2", "SN")
  2425. // 设置列宽
  2426. f.SetColWidth("Sheet1", "A", "A", 10)
  2427. f.SetColWidth("Sheet1", "B", "B", 15)
  2428. f.SetColWidth("Sheet1", "C", "D", 10)
  2429. f.SetColWidth("Sheet1", "D", "D", 10)
  2430. f.SetColWidth("Sheet1", "E", "E", 10)
  2431. f.SetColWidth("Sheet1", "F", "F", 20)
  2432. f.SetColWidth("Sheet1", "G", "G", 20)
  2433. f.SetColWidth("Sheet1", "H", "H", 15)
  2434. f.SetColWidth("Sheet1", "I", "I", 10)
  2435. f.SetColWidth("Sheet1", "J", "J", 12)
  2436. f.SetColWidth("Sheet1", "K", "K", 30)
  2437. f.SetColWidth("Sheet1", "L", "L", 100)
  2438. line := 2
  2439. for _, product := range R_List {
  2440. line += 1
  2441. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  2442. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  2443. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_receive_name)
  2444. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  2445. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  2446. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), product.T_project)
  2447. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
  2448. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
  2449. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
  2450. var relation_sn string
  2451. if product.T_product_relation_sn == 1 {
  2452. relation_sn = "是"
  2453. } else {
  2454. relation_sn = "否"
  2455. }
  2456. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
  2457. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
  2458. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
  2459. }
  2460. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
  2461. // 保存文件
  2462. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  2463. fmt.Println(err)
  2464. }
  2465. var url string
  2466. //// 上传 OSS
  2467. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  2468. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  2469. if !is {
  2470. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  2471. c.ServeJSON()
  2472. return
  2473. }
  2474. //删除目录
  2475. err := os.Remove("ofile/" + filename + ".xlsx")
  2476. if err != nil {
  2477. logs.Error(lib.FuncName(), err)
  2478. }
  2479. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  2480. c.ServeJSON()
  2481. return
  2482. }
  2483. func (c *StockController) StockOut_Get() {
  2484. // 查询
  2485. T_number := c.GetString("T_number")
  2486. o := orm.NewOrm()
  2487. StockOutDao := Stock.NewStockOut(o)
  2488. StockOutProductDao := Stock.NewStockOutProduct(o)
  2489. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2490. if err != nil {
  2491. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2492. c.ServeJSON()
  2493. return
  2494. }
  2495. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2496. var pList []Stock.StockOutProduct_R
  2497. for _, v := range productList {
  2498. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  2499. }
  2500. userList, _ := NatsServer.Read_User_List_All()
  2501. Account.Read_User_All_Map(userList)
  2502. Basic.Read_Depot_All_Map()
  2503. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockOutToStockOut_Detail(stockOut, pList)}
  2504. c.ServeJSON()
  2505. return
  2506. }
  2507. func (c *StockController) StockOut_generate_number() {
  2508. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  2509. rand_x := 0
  2510. T_number := ""
  2511. for true {
  2512. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  2513. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2514. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  2515. break
  2516. }
  2517. rand_x += 1
  2518. }
  2519. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2520. c.ServeJSON()
  2521. return
  2522. }
  2523. func (c *StockController) StockOut_Add() {
  2524. o := orm.NewOrm()
  2525. o.Begin()
  2526. StockOutDao := Stock.NewStockOut(o)
  2527. // 检查前端传入的T_number是否已提交过
  2528. T_number := c.GetString("T_number")
  2529. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2530. if err == nil {
  2531. // 入库编号已存在,不需要做任何处理
  2532. o.Rollback()
  2533. c.Data["json"] = lib.JSONS{Code: 200, Msg: "出库编号已存在,无需重复提交!", Data: T_number}
  2534. c.ServeJSON()
  2535. return
  2536. }
  2537. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  2538. // 数据库查询错误
  2539. o.Rollback()
  2540. c.Data["json"] = lib.JSONS{Code: 203, Msg: "查询入库编号失败"}
  2541. c.ServeJSON()
  2542. return
  2543. }
  2544. T_contract_number := c.GetString("T_contract_number")
  2545. T_depot_id, _ := c.GetInt("T_depot_id")
  2546. T_type, _ := c.GetInt("T_type") // 出库类型 1-领料出库 2-销售出库
  2547. T_date := c.GetString("T_date")
  2548. T_receive := c.GetString("T_receive")
  2549. T_product := c.GetString("T_product")
  2550. T_remark := c.GetString("T_remark")
  2551. T_project := c.GetString("T_project")
  2552. T_delivery_type, _ := c.GetInt("T_delivery_type")
  2553. T_signer_unit := c.GetString("T_signer_unit")
  2554. T_signer := c.GetString("T_signer")
  2555. T_signer_phone := c.GetString("T_signer_phone")
  2556. T_signer_date := c.GetString("T_signer_date")
  2557. T_courier_number := c.GetString("T_courier_number")
  2558. date, is := lib.DateStrToTime(T_date)
  2559. if !is {
  2560. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2561. c.ServeJSON()
  2562. return
  2563. }
  2564. var_ := Stock.StockOut{
  2565. T_number: T_number,
  2566. T_contract_number: T_contract_number,
  2567. T_depot_id: T_depot_id,
  2568. T_type: T_type,
  2569. T_date: T_date,
  2570. T_receive: T_receive,
  2571. T_remark: T_remark,
  2572. T_project: T_project,
  2573. T_submit: c.User.T_uuid,
  2574. // 销售出库
  2575. T_delivery_type: T_delivery_type, // 1-自送 2-自提 3-快递
  2576. T_signer_unit: T_signer_unit,
  2577. T_signer: T_signer,
  2578. T_signer_phone: T_signer_phone,
  2579. T_signer_date: T_signer_date,
  2580. T_courier_number: T_courier_number,
  2581. T_state: Stock.StockOutAlreadyOut,
  2582. }
  2583. StockOutProductDao := Stock.NewStockOutProduct(o)
  2584. StockDao := Stock.NewStock(o)
  2585. DeviceDao := Stock.NewDevice(o)
  2586. ContractDao := Contract.NewContract(o)
  2587. ContractProductDao := Contract.NewContractProduct(o)
  2588. _, err = StockOutDao.Add_StockOut(var_)
  2589. if err != nil {
  2590. o.Rollback()
  2591. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  2592. c.ServeJSON()
  2593. return
  2594. }
  2595. // 1、添加出库单
  2596. contract, _ := ContractDao.Read_Contract_ByT_number(T_contract_number)
  2597. if contract.Id == 0 && T_type == 2 {
  2598. o.Rollback()
  2599. c.Data["json"] = lib.JSONS{Code: 203, Msg: "合同编号错误!"}
  2600. c.ServeJSON()
  2601. return
  2602. }
  2603. if len(T_product) == 0 {
  2604. o.Rollback()
  2605. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  2606. c.ServeJSON()
  2607. return
  2608. }
  2609. productList := lib.SplitString(T_product, "|")
  2610. allProductList := []int{}
  2611. for _, v := range productList {
  2612. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2613. allProductList = append(allProductList, product_id)
  2614. product, _ := Basic.Read_Product_ById(product_id)
  2615. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2616. T_relation_sn := strings.Split(v, "-")[2]
  2617. if T_relation_sn == "" && product.T_relation_sn == 1 {
  2618. o.Rollback()
  2619. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  2620. c.ServeJSON()
  2621. return
  2622. }
  2623. if len(T_relation_sn) > 0 {
  2624. snList := lib.SplitString(T_relation_sn, ",")
  2625. num = len(snList)
  2626. for _, sn := range snList {
  2627. mqtt := Stock.Read_MqttUser(sn)
  2628. // 根据IMEI查询上次入库的批次号
  2629. batchNumber, err := DeviceDao.Read_Device_BatchNumber_ByImei(mqtt.Imei)
  2630. if err != nil {
  2631. logs.Error(lib.FuncName(), err)
  2632. }
  2633. device := Stock.Device{
  2634. T_contract_number: T_contract_number,
  2635. T_product_id: product_id,
  2636. T_out_number: T_number,
  2637. T_sn: sn,
  2638. T_iccid: mqtt.Iccid,
  2639. T_imei: mqtt.Imei,
  2640. T_State: 1,
  2641. T_project: T_project,
  2642. T_batch_number: batchNumber,
  2643. CreateTime: date,
  2644. }
  2645. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2646. if err != nil {
  2647. o.Rollback()
  2648. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  2649. c.ServeJSON()
  2650. return
  2651. }
  2652. }
  2653. }
  2654. // 3、添加出库产品清单
  2655. stockOutProduct := Stock.StockOutProduct{
  2656. T_number: T_number,
  2657. T_product_id: product_id,
  2658. T_depot_id: T_depot_id,
  2659. T_num: num, // 出库数量
  2660. T_date: T_date, // 出库日期
  2661. T_relation_sn: T_relation_sn,
  2662. T_state: 1,
  2663. }
  2664. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  2665. if err != nil {
  2666. o.Rollback()
  2667. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  2668. c.ServeJSON()
  2669. return
  2670. }
  2671. // 4、更改合同产品列表清单
  2672. contractProduct, _ := ContractProductDao.Read_ContractProduct_ByT_number_T_product_id(T_contract_number, product_id)
  2673. contractProduct.T_product_out += num
  2674. if contractProduct.T_product_out > contractProduct.T_product_total && T_type == 2 {
  2675. o.Rollback()
  2676. p, _ := Basic.Read_Product_ById(product_id)
  2677. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】出库数量超过预计出库数量!", p.T_name)}
  2678. c.ServeJSON()
  2679. return
  2680. }
  2681. contractProduct.T_State = 2
  2682. if contractProduct.T_product_out == 0 {
  2683. contractProduct.T_State = 1
  2684. }
  2685. if contractProduct.T_product_out == contractProduct.T_product_total {
  2686. contractProduct.T_State = 3
  2687. }
  2688. err = ContractProductDao.Update_ContractProduct(contractProduct, "T_product_out", "T_State")
  2689. if err != nil {
  2690. o.Rollback()
  2691. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同产品清单失败"}
  2692. c.ServeJSON()
  2693. return
  2694. }
  2695. // 5、更新产品库存列表
  2696. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 1)
  2697. if err != nil {
  2698. o.Rollback()
  2699. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存失败" + err.Error()}
  2700. c.ServeJSON()
  2701. return
  2702. }
  2703. }
  2704. o.Commit()
  2705. o2 := orm.NewOrm()
  2706. o2.Begin()
  2707. ContractDao2 := Contract.NewContract(o2)
  2708. ContractProductDao2 := Contract.NewContractProduct(o2)
  2709. // 5、更新合同出库状态
  2710. var T_out int
  2711. if contract.T_State == 1 {
  2712. T_out = 2
  2713. // 查询合同产品清单是否全部都为已出库
  2714. state := ContractProductDao2.Read_ContractProduct_T_State_List(contract.T_number)
  2715. if state == 1 {
  2716. T_out = 1
  2717. }
  2718. if state == 3 {
  2719. T_out = 3
  2720. }
  2721. }
  2722. if T_out != contract.T_out {
  2723. contract.T_out = T_out
  2724. err = ContractDao2.Update_Contract(contract, "T_out")
  2725. if err != nil {
  2726. o2.Rollback()
  2727. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
  2728. c.ServeJSON()
  2729. return
  2730. }
  2731. }
  2732. o2.Commit()
  2733. StockOut_Edit_StockMonth(T_date, T_depot_id, allProductList)
  2734. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "出库", var_)
  2735. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2736. c.ServeJSON()
  2737. return
  2738. }
  2739. func (c *StockController) StockOut_Edit_Delivery() {
  2740. T_number := c.GetString("T_number")
  2741. T_remark := c.GetString("T_remark")
  2742. T_project := c.GetString("T_project")
  2743. T_delivery_type, _ := c.GetInt("T_delivery_type")
  2744. T_signer_unit := c.GetString("T_signer_unit")
  2745. T_signer := c.GetString("T_signer")
  2746. T_signer_phone := c.GetString("T_signer_phone")
  2747. T_signer_date := c.GetString("T_signer_date")
  2748. T_courier_number := c.GetString("T_courier_number")
  2749. o := orm.NewOrm()
  2750. StockOutDao := Stock.NewStockOut(o)
  2751. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2752. if err != nil {
  2753. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库编号错误!"}
  2754. c.ServeJSON()
  2755. return
  2756. }
  2757. if len(T_remark) > 0 {
  2758. stockOut.T_remark = T_remark
  2759. }
  2760. if len(T_project) > 0 {
  2761. stockOut.T_project = T_project
  2762. }
  2763. if T_delivery_type > 0 {
  2764. stockOut.T_delivery_type = T_delivery_type
  2765. }
  2766. if len(T_signer_unit) > 0 {
  2767. stockOut.T_signer_unit = T_signer_unit
  2768. }
  2769. if len(T_signer) > 0 {
  2770. stockOut.T_signer = T_signer
  2771. }
  2772. if len(T_signer_phone) > 0 {
  2773. stockOut.T_signer_phone = T_signer_phone
  2774. }
  2775. if len(T_signer_date) > 0 {
  2776. stockOut.T_signer_date = T_signer_date
  2777. }
  2778. if len(T_courier_number) > 0 {
  2779. stockOut.T_courier_number = T_courier_number
  2780. }
  2781. err = StockOutDao.Update_StockOut(stockOut, "T_remark", "T_delivery_type", "T_signer_unit", "T_signer", "T_signer_phone", "T_signer_date", "T_courier_number", "T_project")
  2782. if err != nil {
  2783. o.Rollback()
  2784. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  2785. c.ServeJSON()
  2786. return
  2787. }
  2788. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改发货单", stockOut)
  2789. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2790. c.ServeJSON()
  2791. return
  2792. }
  2793. func (c *StockController) StockOut_Edit() {
  2794. T_number := c.GetString("T_number") // 出库单号
  2795. //T_depot_id, _ := c.GetInt("T_depot_id")
  2796. T_date := c.GetString("T_date")
  2797. T_receive := c.GetString("T_receive")
  2798. T_project := c.GetString("T_project")
  2799. T_product := c.GetString("T_product")
  2800. T_remark := c.GetString("T_remark")
  2801. date, is := lib.DateStrToTime(T_date)
  2802. if !is {
  2803. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  2804. c.ServeJSON()
  2805. return
  2806. }
  2807. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  2808. T_date = ""
  2809. }
  2810. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
  2811. o := orm.NewOrm()
  2812. o.Begin()
  2813. StockOutDao := Stock.NewStockOut(o)
  2814. StockOutProductDao := Stock.NewStockOutProduct(o)
  2815. StockDao := Stock.NewStock(o)
  2816. DeviceDao := Stock.NewDevice(o)
  2817. // 查询出库信息
  2818. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2819. if err != nil {
  2820. o.Rollback()
  2821. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2822. c.ServeJSON()
  2823. return
  2824. }
  2825. T_old_date := StockOut.T_date
  2826. // 查询出库产品信息
  2827. productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  2828. var oldProductList []dto.StockProduct
  2829. var newProductList []dto.StockProduct
  2830. productOldMap := map[int]dto.StockProduct{}
  2831. allProductListMap := make(map[int]struct{})
  2832. for _, product := range productOldList {
  2833. stockProduct := dto.StockProduct{
  2834. T_product_id: product.T_product_id,
  2835. T_num: product.T_num,
  2836. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2837. }
  2838. oldProductList = append(oldProductList, stockProduct)
  2839. productOldMap[product.T_product_id] = stockProduct
  2840. if _, ok := allProductListMap[product.T_product_id]; !ok {
  2841. allProductListMap[product.T_product_id] = struct{}{}
  2842. }
  2843. }
  2844. productNewList := lib.SplitString(T_product, "|")
  2845. productNewMap := map[int]dto.StockProduct{}
  2846. for _, v := range productNewList {
  2847. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  2848. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  2849. T_relation_sn := strings.Split(v, "-")[2]
  2850. snList := lib.SplitString(T_relation_sn, ",")
  2851. if len(snList) > 0 {
  2852. num = len(snList)
  2853. }
  2854. stockProduct := dto.StockProduct{
  2855. T_product_id: product_id,
  2856. T_num: num,
  2857. T_relation_sn: snList,
  2858. }
  2859. newProductList = append(newProductList, stockProduct)
  2860. productNewMap[product_id] = stockProduct
  2861. if _, ok := allProductListMap[product_id]; !ok {
  2862. allProductListMap[product_id] = struct{}{}
  2863. }
  2864. }
  2865. // 判断产品列表信息是否相同
  2866. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  2867. // 两次提交的数据相同,则不用修改
  2868. if !StockProductListIsEqual {
  2869. if len(needDelete) > 0 {
  2870. // 删除入库产品列表
  2871. for _, v := range needDelete {
  2872. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2873. // 删除入库产品列表
  2874. err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
  2875. if err != nil {
  2876. o.Rollback()
  2877. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  2878. c.ServeJSON()
  2879. return
  2880. }
  2881. if product.T_relation_sn == 1 {
  2882. // 删除该入库单关联sn
  2883. for _, sn := range v.T_relation_sn {
  2884. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  2885. if err != nil {
  2886. o.Rollback()
  2887. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  2888. c.ServeJSON()
  2889. return
  2890. }
  2891. }
  2892. }
  2893. // 增加库存
  2894. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  2895. if err != nil {
  2896. o.Rollback()
  2897. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  2898. c.ServeJSON()
  2899. return
  2900. }
  2901. }
  2902. }
  2903. if len(needAdd) > 0 {
  2904. // 新增入库产品列表
  2905. for _, v := range needAdd {
  2906. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2907. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  2908. o.Rollback()
  2909. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  2910. c.ServeJSON()
  2911. return
  2912. }
  2913. // 添加该入库单关联sn
  2914. for _, sn := range v.T_relation_sn {
  2915. mqtt := Stock.Read_MqttUser(sn)
  2916. // 添加设备
  2917. device := Stock.Device{
  2918. T_contract_number: "",
  2919. T_out_number: StockOut.T_number,
  2920. T_product_id: v.T_product_id,
  2921. T_in_number: "",
  2922. T_sn: sn,
  2923. T_iccid: mqtt.Iccid,
  2924. T_imei: mqtt.Imei,
  2925. T_State: 1,
  2926. CreateTime: date,
  2927. UpdateTime: StockOut.UpdateTime,
  2928. }
  2929. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  2930. if err != nil {
  2931. o.Rollback()
  2932. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  2933. c.ServeJSON()
  2934. return
  2935. }
  2936. }
  2937. StockOutProduct := Stock.StockOutProduct{
  2938. T_number: T_number,
  2939. T_product_id: v.T_product_id,
  2940. T_depot_id: StockOut.T_depot_id,
  2941. T_num: v.T_num, // 入库数量
  2942. T_date: StockOut.T_date, // 入库日期
  2943. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  2944. T_state: 1,
  2945. }
  2946. _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
  2947. if err != nil {
  2948. o.Rollback()
  2949. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2950. c.ServeJSON()
  2951. return
  2952. }
  2953. // 减少库存
  2954. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  2955. if err != nil {
  2956. o.Rollback()
  2957. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  2958. c.ServeJSON()
  2959. return
  2960. }
  2961. }
  2962. }
  2963. if len(needEdit) > 0 {
  2964. for _, v := range needEdit {
  2965. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2966. StockOutProduct := Stock.StockOutProduct{
  2967. T_number: StockOut.T_number,
  2968. T_product_id: v.T_product_id,
  2969. T_depot_id: StockOut.T_depot_id,
  2970. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  2971. T_date: StockOut.T_date, // 入库日期
  2972. T_state: 1,
  2973. }
  2974. // 出库数量比之前多,减少库存
  2975. var T_type int
  2976. var T_num int
  2977. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  2978. // 减少库存
  2979. T_type = 1
  2980. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  2981. } else {
  2982. // 增加库存
  2983. T_type = 2
  2984. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  2985. }
  2986. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  2987. if err != nil {
  2988. o.Rollback()
  2989. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  2990. c.ServeJSON()
  2991. return
  2992. }
  2993. // 更新产品库存表
  2994. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  2995. if err != nil {
  2996. o.Rollback()
  2997. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  2998. c.ServeJSON()
  2999. return
  3000. }
  3001. }
  3002. }
  3003. for _, diff := range snDiff {
  3004. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  3005. // 删除减少的sn
  3006. if len(diff.T_delete_relation_sn) > 0 {
  3007. for _, sn := range diff.T_delete_relation_sn {
  3008. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  3009. if err != nil {
  3010. o.Rollback()
  3011. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  3012. c.ServeJSON()
  3013. return
  3014. }
  3015. }
  3016. // 增加库存
  3017. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_delete_relation_sn), 2)
  3018. if err != nil {
  3019. o.Rollback()
  3020. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  3021. c.ServeJSON()
  3022. return
  3023. }
  3024. }
  3025. if len(diff.T_add_relation_sn) > 0 {
  3026. for _, sn := range diff.T_add_relation_sn {
  3027. mqtt := Stock.Read_MqttUser(sn)
  3028. // 添加设备
  3029. device := Stock.Device{
  3030. T_contract_number: "",
  3031. T_out_number: StockOut.T_number,
  3032. T_product_id: diff.T_product_id,
  3033. T_in_number: "",
  3034. T_sn: sn,
  3035. T_iccid: mqtt.Iccid,
  3036. T_imei: mqtt.Imei,
  3037. T_State: 1,
  3038. CreateTime: date,
  3039. UpdateTime: StockOut.UpdateTime,
  3040. }
  3041. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  3042. if err != nil {
  3043. o.Rollback()
  3044. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  3045. c.ServeJSON()
  3046. return
  3047. }
  3048. }
  3049. // 减少库存
  3050. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, len(diff.T_add_relation_sn), 1)
  3051. if err != nil {
  3052. o.Rollback()
  3053. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  3054. c.ServeJSON()
  3055. return
  3056. }
  3057. }
  3058. StockOutProduct := Stock.StockOutProduct{
  3059. T_number: StockOut.T_number,
  3060. T_product_id: diff.T_product_id,
  3061. T_depot_id: StockOut.T_depot_id,
  3062. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  3063. T_date: StockOut.T_date, // 入库日期
  3064. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  3065. T_state: 1,
  3066. }
  3067. // 更新产品库存表
  3068. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  3069. if err != nil {
  3070. o.Rollback()
  3071. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  3072. c.ServeJSON()
  3073. return
  3074. }
  3075. }
  3076. }
  3077. StockOut.T_remark = T_remark
  3078. if len(T_receive) > 0 {
  3079. StockOut.T_receive = T_receive
  3080. }
  3081. if len(T_project) > 0 {
  3082. StockOut.T_project = T_project
  3083. }
  3084. if len(T_date) > 0 {
  3085. StockOut.T_date = T_date
  3086. }
  3087. err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_date")
  3088. if err != nil {
  3089. o.Rollback()
  3090. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  3091. c.ServeJSON()
  3092. return
  3093. }
  3094. o.Commit()
  3095. if len(T_date) > 0 && T_old_date != T_date {
  3096. // 修改出库产品日期
  3097. StockOutProductDao.Update_StockOutProduct_T_date(StockOut.T_number, T_date)
  3098. DeviceDao.Update_Device_CreateTimeByT_out_number(StockOut.T_number, T_date)
  3099. }
  3100. if len(T_project) > 0 {
  3101. DeviceDao.Update_Device_ByT_out_number_T_project(StockOut.T_number, T_project)
  3102. }
  3103. // 更新月份统计表
  3104. allProductList := []int{}
  3105. for i, _ := range allProductListMap {
  3106. allProductList = append(allProductList, i)
  3107. }
  3108. StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
  3109. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改", StockOut)
  3110. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3111. c.ServeJSON()
  3112. return
  3113. }
  3114. // 删除出库
  3115. func (c *StockController) StockOut_Del() {
  3116. T_number := c.GetString("T_number") // 入库单号
  3117. o := orm.NewOrm()
  3118. o.Begin()
  3119. StockOutDao := Stock.NewStockOut(o)
  3120. StockOutProductDao := Stock.NewStockOutProduct(o)
  3121. StockDao := Stock.NewStock(o)
  3122. DeviceDao := Stock.NewDevice(o)
  3123. // 查询出库信息
  3124. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3125. if err != nil {
  3126. o.Rollback()
  3127. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3128. c.ServeJSON()
  3129. return
  3130. }
  3131. // 查询出库产品信息
  3132. productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3133. var oldProductList []dto.StockProduct
  3134. allProductList := []int{}
  3135. for _, product := range productOldList {
  3136. oldProductList = append(oldProductList, dto.StockProduct{
  3137. T_product_id: product.T_product_id,
  3138. T_num: product.T_num,
  3139. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  3140. })
  3141. allProductList = append(allProductList, product.T_product_id)
  3142. }
  3143. // 删除入库产品列表
  3144. for _, v := range oldProductList {
  3145. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3146. // 删除入库产品列表
  3147. err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
  3148. if err != nil {
  3149. o.Rollback()
  3150. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
  3151. c.ServeJSON()
  3152. return
  3153. }
  3154. if product.T_relation_sn == 1 {
  3155. // 删除该入库单关联sn
  3156. for _, sn := range v.T_relation_sn {
  3157. err = DeviceDao.Delete_Device_ByT_out_number(stockOut.T_number, sn)
  3158. if err != nil {
  3159. o.Rollback()
  3160. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库设备失败"}
  3161. c.ServeJSON()
  3162. return
  3163. }
  3164. }
  3165. }
  3166. // 增加库存
  3167. _, err = StockDao.AddOrUpdate_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  3168. if err != nil {
  3169. o.Rollback()
  3170. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  3171. c.ServeJSON()
  3172. return
  3173. }
  3174. }
  3175. err = StockOutDao.Delete_StockOut(stockOut)
  3176. if err != nil {
  3177. o.Rollback()
  3178. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
  3179. c.ServeJSON()
  3180. return
  3181. }
  3182. o.Commit()
  3183. // 更新月份统计表
  3184. StockOut_Edit_StockMonth(stockOut.T_date, stockOut.T_depot_id, allProductList)
  3185. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "删除", stockOut)
  3186. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3187. c.ServeJSON()
  3188. return
  3189. }
  3190. // 导出出库单
  3191. func (c *StockController) StockOut_Excel() {
  3192. // 查询
  3193. T_number := c.GetString("T_number")
  3194. o := orm.NewOrm()
  3195. StockOutDao := Stock.NewStockOut(o)
  3196. StockOutProductDao := Stock.NewStockOutProduct(o)
  3197. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3198. if err != nil {
  3199. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3200. c.ServeJSON()
  3201. return
  3202. }
  3203. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3204. //productList = append(productList, productList...)
  3205. //productList = append(productList, productList...)
  3206. //productList = append(productList, productList...)
  3207. var pList []Stock.StockOutProduct_R
  3208. for _, v := range productList {
  3209. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  3210. }
  3211. userList, _ := NatsServer.Read_User_List_All()
  3212. Account.Read_User_All_Map(userList)
  3213. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  3214. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  3215. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  3216. f := excelize.NewFile() // 设置单元格的值
  3217. Style1, _ := f.NewStyle(
  3218. &excelize.Style{
  3219. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  3220. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  3221. })
  3222. Style2, _ := f.NewStyle(
  3223. &excelize.Style{
  3224. Font: &excelize.Font{Size: 11, Family: "宋体"},
  3225. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  3226. })
  3227. Style3, _ := f.NewStyle(
  3228. &excelize.Style{
  3229. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  3230. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  3231. Border: []excelize.Border{
  3232. {Type: "left", Color: "000000", Style: 1},
  3233. {Type: "top", Color: "000000", Style: 1},
  3234. {Type: "bottom", Color: "000000", Style: 1},
  3235. {Type: "right", Color: "000000", Style: 1},
  3236. },
  3237. })
  3238. Style4, _ := f.NewStyle(
  3239. &excelize.Style{
  3240. Font: &excelize.Font{Size: 11, Family: "宋体"},
  3241. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  3242. Border: []excelize.Border{
  3243. {Type: "left", Color: "000000", Style: 1},
  3244. {Type: "top", Color: "000000", Style: 1},
  3245. {Type: "bottom", Color: "000000", Style: 1},
  3246. {Type: "right", Color: "000000", Style: 1},
  3247. },
  3248. })
  3249. productList2 := lib.ChunkBy(productList, 10)
  3250. for i, products := range productList2 {
  3251. if len(products) < 10 {
  3252. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  3253. }
  3254. row := i*17 + 1
  3255. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3256. f.SetRowStyle("Sheet1", row, row, Style1)
  3257. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  3258. f.SetRowHeight("Sheet1", row, 20)
  3259. row += 1
  3260. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3261. f.SetRowStyle("Sheet1", row, row, Style1)
  3262. title := "硬件出库单"
  3263. if len(productList2) > 1 {
  3264. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  3265. }
  3266. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  3267. f.SetRowHeight("Sheet1", row, 20)
  3268. row += 1
  3269. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3270. f.SetRowStyle("Sheet1", row, row, Style2)
  3271. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  3272. f.SetRowHeight("Sheet1", row, 20)
  3273. row += 1
  3274. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3275. f.SetRowStyle("Sheet1", row, row, Style2)
  3276. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  3277. f.SetRowHeight("Sheet1", row, 20)
  3278. row += 1
  3279. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3280. f.SetRowStyle("Sheet1", row, row, Style2)
  3281. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  3282. f.SetRowHeight("Sheet1", row, 20)
  3283. // 这里设置表头
  3284. row += 1
  3285. f.SetRowHeight("Sheet1", row, 20)
  3286. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  3287. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  3288. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  3289. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  3290. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  3291. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  3292. // 设置列宽
  3293. f.SetColWidth("Sheet1", "A", "A", 5)
  3294. f.SetColWidth("Sheet1", "B", "B", 20)
  3295. f.SetColWidth("Sheet1", "C", "C", 12)
  3296. f.SetColWidth("Sheet1", "D", "D", 10)
  3297. f.SetColWidth("Sheet1", "E", "E", 10)
  3298. f.SetColWidth("Sheet1", "F", "F", 15)
  3299. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  3300. sRow := row
  3301. // 循环写入数据
  3302. for j, v := range products {
  3303. row += 1
  3304. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3305. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  3306. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  3307. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  3308. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  3309. if v.T_num > 0 {
  3310. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  3311. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  3312. }
  3313. }
  3314. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  3315. row += 1
  3316. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  3317. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  3318. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  3319. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  3320. f.SetRowStyle("Sheet1", row, row, Style2)
  3321. f.SetRowHeight("Sheet1", row, 20)
  3322. }
  3323. // 保存文件
  3324. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  3325. fmt.Println(err)
  3326. }
  3327. var url string
  3328. //// 上传 OSS
  3329. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  3330. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  3331. if !is {
  3332. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  3333. c.ServeJSON()
  3334. return
  3335. }
  3336. //删除目录
  3337. err = os.Remove("ofile/" + filename + ".xlsx")
  3338. if err != nil {
  3339. logs.Error(lib.FuncName(), err)
  3340. }
  3341. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  3342. c.ServeJSON()
  3343. return
  3344. }
  3345. func (c *StockController) StockOut_Excel_Batch() {
  3346. // 查询
  3347. T_number_list := c.GetString("T_number_list")
  3348. o := orm.NewOrm()
  3349. StockOutDao := Stock.NewStockOut(o)
  3350. StockOutProductDao := Stock.NewStockOutProduct(o)
  3351. list := strings.Split(strings.Trim(T_number_list, "|"), "|")
  3352. userList, _ := NatsServer.Read_User_List_All()
  3353. Account.Read_User_All_Map(userList)
  3354. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  3355. f := excelize.NewFile() // 设置单元格的值
  3356. Style1, _ := f.NewStyle(
  3357. &excelize.Style{
  3358. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  3359. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  3360. })
  3361. Style2, _ := f.NewStyle(
  3362. &excelize.Style{
  3363. Font: &excelize.Font{Size: 11, Family: "宋体"},
  3364. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  3365. })
  3366. Style3, _ := f.NewStyle(
  3367. &excelize.Style{
  3368. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  3369. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  3370. Border: []excelize.Border{
  3371. {Type: "left", Color: "000000", Style: 1},
  3372. {Type: "top", Color: "000000", Style: 1},
  3373. {Type: "bottom", Color: "000000", Style: 1},
  3374. {Type: "right", Color: "000000", Style: 1},
  3375. },
  3376. })
  3377. Style4, _ := f.NewStyle(
  3378. &excelize.Style{
  3379. Font: &excelize.Font{Size: 11, Family: "宋体"},
  3380. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  3381. Border: []excelize.Border{
  3382. {Type: "left", Color: "000000", Style: 1},
  3383. {Type: "top", Color: "000000", Style: 1},
  3384. {Type: "bottom", Color: "000000", Style: 1},
  3385. {Type: "right", Color: "000000", Style: 1},
  3386. },
  3387. })
  3388. var num int
  3389. for _, T_number := range list {
  3390. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3391. if err != nil {
  3392. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3393. c.ServeJSON()
  3394. return
  3395. }
  3396. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3397. var pList []Stock.StockOutProduct_R
  3398. for _, v := range productList {
  3399. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  3400. }
  3401. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  3402. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  3403. productList2 := lib.ChunkBy(productList, 10)
  3404. for i, products := range productList2 {
  3405. if len(products) < 10 {
  3406. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  3407. }
  3408. row := num*18 + 1
  3409. num += 1
  3410. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3411. f.SetRowStyle("Sheet1", row, row, Style1)
  3412. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  3413. f.SetRowHeight("Sheet1", row, 20)
  3414. row += 1
  3415. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3416. f.SetRowStyle("Sheet1", row, row, Style1)
  3417. title := "硬件出库单"
  3418. if len(productList2) > 1 {
  3419. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  3420. }
  3421. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  3422. f.SetRowHeight("Sheet1", row, 20)
  3423. row += 1
  3424. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3425. f.SetRowStyle("Sheet1", row, row, Style2)
  3426. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  3427. f.SetRowHeight("Sheet1", row, 20)
  3428. row += 1
  3429. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3430. f.SetRowStyle("Sheet1", row, row, Style2)
  3431. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  3432. f.SetRowHeight("Sheet1", row, 20)
  3433. row += 1
  3434. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3435. f.SetRowStyle("Sheet1", row, row, Style2)
  3436. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  3437. f.SetRowHeight("Sheet1", row, 20)
  3438. // 这里设置表头
  3439. row += 1
  3440. f.SetRowHeight("Sheet1", row, 20)
  3441. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  3442. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  3443. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  3444. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  3445. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  3446. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  3447. // 设置列宽
  3448. f.SetColWidth("Sheet1", "A", "A", 5)
  3449. f.SetColWidth("Sheet1", "B", "B", 20)
  3450. f.SetColWidth("Sheet1", "C", "C", 12)
  3451. f.SetColWidth("Sheet1", "D", "D", 10)
  3452. f.SetColWidth("Sheet1", "E", "E", 10)
  3453. f.SetColWidth("Sheet1", "F", "F", 15)
  3454. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  3455. sRow := row
  3456. // 循环写入数据
  3457. for j, v := range products {
  3458. row += 1
  3459. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3460. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  3461. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  3462. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  3463. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  3464. if v.T_num > 0 {
  3465. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  3466. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  3467. }
  3468. }
  3469. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  3470. row += 1
  3471. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  3472. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  3473. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  3474. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  3475. f.SetRowStyle("Sheet1", row, row, Style2)
  3476. f.SetRowHeight("Sheet1", row, 20)
  3477. // 空一行
  3478. row += 1
  3479. //f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  3480. //f.SetRowStyle("Sheet1", row, row, Style1)
  3481. //f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "")
  3482. // 插入下一页分页符
  3483. f.InsertPageBreak("Sheet1", fmt.Sprintf("A%d", row))
  3484. }
  3485. }
  3486. // 保存文件
  3487. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  3488. fmt.Println(err)
  3489. }
  3490. var url string
  3491. //// 上传 OSS
  3492. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  3493. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  3494. if !is {
  3495. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  3496. c.ServeJSON()
  3497. return
  3498. }
  3499. //删除目录
  3500. err := os.Remove("ofile/" + filename + ".xlsx")
  3501. if err != nil {
  3502. logs.Error(lib.FuncName(), err)
  3503. }
  3504. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  3505. c.ServeJSON()
  3506. return
  3507. }
  3508. func StockOut_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  3509. date, _ := lib.DateStrToTime(T_date)
  3510. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  3511. // // 修改本月数据不同步库存
  3512. // return nil
  3513. //}
  3514. //T_month := date.Format("2006-01")
  3515. months := generateMonthList(date, "out")
  3516. o := orm.NewOrm()
  3517. StockOutDao := Stock.NewStockOut(o)
  3518. StockMonthDao := Stock.NewStockMonth(o)
  3519. StockOutProductDao := Stock.NewStockOutProduct(o)
  3520. StockInProductDao := Stock.NewStockInProduct(o)
  3521. for _, T_product_id := range allProductList {
  3522. for _, T_month := range months {
  3523. // 获取当前产品本月出入库数量
  3524. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  3525. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  3526. if stockMonth.Id > 0 {
  3527. // 修改
  3528. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  3529. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(T_depot_id, T_product_id, T_month)
  3530. if len(lastMonth) > 0 {
  3531. stockMonth.T_beginning = lastMonth[0].T_ending
  3532. } else {
  3533. stockMonth.T_beginning = 0
  3534. }
  3535. // 期末库存 = 期初库存+入库-出库
  3536. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3537. err := StockMonthDao.Update_StockMonth(stockMonth, "T_out", "T_return", "T_beginning", "T_ending")
  3538. if err != nil {
  3539. return err
  3540. }
  3541. } else {
  3542. product, _ := Basic.Read_Product_ById(T_product_id)
  3543. // 添加库存记录
  3544. stockMonth = Stock.StockMonth{
  3545. T_depot_id: T_depot_id,
  3546. T_product_id: T_product_id,
  3547. T_product_class: product.T_class,
  3548. T_month: T_month,
  3549. }
  3550. // 获取当前产品本月出入库数量
  3551. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  3552. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  3553. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(T_depot_id, T_product_id, T_month)
  3554. if len(lastMonth) > 0 {
  3555. stockMonth.T_beginning = lastMonth[0].T_ending
  3556. } else {
  3557. stockMonth.T_beginning = 0
  3558. }
  3559. // 期末库存 = 期初库存+入库-出库
  3560. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3561. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  3562. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  3563. if err != nil {
  3564. return err
  3565. }
  3566. }
  3567. }
  3568. }
  3569. return nil
  3570. }
  3571. func Cron_StockMonth() {
  3572. //创建一个定时任务对象
  3573. c := cron.New(cron.WithSeconds())
  3574. //给对象增加定时任务
  3575. // @monthly 每月运行一次,每月第一天午夜 0 0 0 1 * *
  3576. //c.AddFunc("0 */1 * * * ?", Cron_StockMonth_Add)
  3577. c.AddFunc("@monthly", Cron_StockMonth_Add)
  3578. //启动定时任务
  3579. c.Start()
  3580. defer c.Stop()
  3581. //查询语句,阻塞,让main函数不退出,保持程序运行
  3582. select {}
  3583. }
  3584. // 保存每月入库出库明细
  3585. func Cron_StockMonth_Add() {
  3586. T_month := time.Now().Format("2006-01")
  3587. //T_month := time.Now().AddDate(0, -1, 0).Format("2006-01")
  3588. logs.Info("开始统计" + T_month + "库存明细数据")
  3589. o := orm.NewOrm()
  3590. StockDao := Stock.NewStock(o)
  3591. StockOutDao := Stock.NewStockOut(o)
  3592. StockMonthDao := Stock.NewStockMonth(o)
  3593. StockOutProductDao := Stock.NewStockOutProduct(o)
  3594. StockInProductDao := Stock.NewStockInProduct(o)
  3595. stockList, _ := StockDao.Read_Stock_List(0, 0, "", "", 0, 9999)
  3596. for _, stock := range stockList {
  3597. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  3598. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  3599. if stockMonth.Id > 0 {
  3600. // 修改
  3601. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3602. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3603. stockMonth.T_return = StockInProductDao.Read_StockReturn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3604. if len(lastMonth) > 0 {
  3605. stockMonth.T_beginning = lastMonth[0].T_ending
  3606. } else {
  3607. stockMonth.T_beginning = 0
  3608. }
  3609. // 期末库存 = 期初库存+入库-出库
  3610. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3611. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_out", "T_beginning", "T_ending", "T_return")
  3612. if err != nil {
  3613. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  3614. }
  3615. } else {
  3616. stockMonth = Stock.StockMonth{
  3617. T_depot_id: stock.T_depot_id,
  3618. T_product_id: stock.T_product_id,
  3619. T_product_class: stock.T_product_class,
  3620. T_month: T_month,
  3621. }
  3622. // 获取当前产品本月出入库数量
  3623. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3624. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  3625. if len(lastMonth) > 0 {
  3626. stockMonth.T_beginning = lastMonth[0].T_ending
  3627. } else {
  3628. stockMonth.T_beginning = 0
  3629. }
  3630. // 期末库存 = 期初库存+入库-出库
  3631. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  3632. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(stock.T_depot_id, stock.T_product_id, T_month)
  3633. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  3634. if err != nil {
  3635. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  3636. }
  3637. }
  3638. }
  3639. }
  3640. func generateMonthList(startMonth time.Time, t_type string) []string {
  3641. var months []string
  3642. endMonth := time.Now()
  3643. if t_type == "in" {
  3644. endMonth = endMonth.AddDate(0, 1, 0)
  3645. }
  3646. for month := startMonth; month.Before(endMonth); month = month.AddDate(0, 1, 0) {
  3647. months = append(months, month.Format("2006-01"))
  3648. }
  3649. return months
  3650. }
  3651. // 我的申请列表
  3652. func (c *StockController) StockOut_Apply_List() {
  3653. // 分页参数 初始化
  3654. page, _ := c.GetInt("page")
  3655. if page < 1 {
  3656. page = 1
  3657. }
  3658. page_z, _ := c.GetInt("page_z")
  3659. if page_z < 1 {
  3660. page_z = conf.Page_size
  3661. }
  3662. // 查询
  3663. T_depot_id, _ := c.GetInt("T_depot_id")
  3664. T_state, _ := c.GetInt("T_state")
  3665. T_contract_number := c.GetString("T_contract_number")
  3666. T_start_date := c.GetString("T_start_date")
  3667. T_end_date := c.GetString("T_end_date")
  3668. T_name := c.GetString("T_name")
  3669. userList, _ := NatsServer.Read_User_List_All()
  3670. Account.Read_User_All_Map(userList)
  3671. Basic.Read_Depot_All_Map()
  3672. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  3673. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List(c.User.T_uuid, T_name, Stock.StockInTypeApply, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  3674. var r_jsons lib.R_JSONS
  3675. r_jsons.Num = R_cnt
  3676. r_jsons.Data = R_List
  3677. r_jsons.Page = page
  3678. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  3679. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  3680. c.ServeJSON()
  3681. return
  3682. }
  3683. // 出库申请
  3684. func (c *StockController) StockOut_Apply() {
  3685. rand_x := 0
  3686. T_number := ""
  3687. o := orm.NewOrm()
  3688. StockOutDao := Stock.NewStockOut(o)
  3689. for true {
  3690. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  3691. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3692. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  3693. break
  3694. }
  3695. rand_x += 1
  3696. }
  3697. T_contract_number := c.GetString("T_contract_number")
  3698. T_depot_id, _ := c.GetInt("T_depot_id")
  3699. T_product := c.GetString("T_product")
  3700. T_remark := c.GetString("T_remark")
  3701. T_project := c.GetString("T_project")
  3702. T_company_name := c.GetString("T_company_name")
  3703. T_payment_method := c.GetString("T_payment_method")
  3704. T_receive := c.GetString("T_receive")
  3705. if len(T_company_name) == 0 {
  3706. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司名称不能为空"}
  3707. c.ServeJSON()
  3708. return
  3709. }
  3710. if len(T_payment_method) == 0 {
  3711. c.Data["json"] = lib.JSONS{Code: 202, Msg: "付款方式不能为空"}
  3712. c.ServeJSON()
  3713. return
  3714. }
  3715. if len(T_receive) == 0 {
  3716. T_receive = c.User.T_uuid
  3717. }
  3718. var_ := Stock.StockOut{
  3719. T_number: T_number,
  3720. T_contract_number: T_contract_number,
  3721. T_depot_id: T_depot_id,
  3722. T_type: 1,
  3723. T_application_date: time.Now().Format("2006-01-02"),
  3724. T_receive: T_receive,
  3725. T_remark: T_remark,
  3726. T_project: T_project,
  3727. T_submit: c.User.T_uuid,
  3728. T_state: Stock.StockOutWaitAudit,
  3729. T_company_name: T_company_name,
  3730. T_payment_method: T_payment_method,
  3731. }
  3732. o.Begin()
  3733. StockOutProductDao := Stock.NewStockOutProduct(o)
  3734. StockDao := Stock.NewStock(o)
  3735. _, err := StockOutDao.Add_StockOut(var_)
  3736. if err != nil {
  3737. o.Rollback()
  3738. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  3739. c.ServeJSON()
  3740. return
  3741. }
  3742. // 1、添加出库单
  3743. if len(T_product) == 0 {
  3744. o.Rollback()
  3745. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  3746. c.ServeJSON()
  3747. return
  3748. }
  3749. productList := lib.SplitString(T_product, "|")
  3750. allProductList := []int{}
  3751. for _, v := range productList {
  3752. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  3753. allProductList = append(allProductList, product_id)
  3754. product, _ := Basic.Read_Product_ById(product_id)
  3755. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  3756. if num == 0 {
  3757. o.Rollback()
  3758. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  3759. c.ServeJSON()
  3760. return
  3761. }
  3762. // 2、添加出库产品清单
  3763. stockOutProduct := Stock.StockOutProduct{
  3764. T_number: T_number,
  3765. T_product_id: product_id,
  3766. T_depot_id: T_depot_id,
  3767. T_num: num, // 出库数量
  3768. T_state: 2,
  3769. }
  3770. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  3771. if err != nil {
  3772. o.Rollback()
  3773. c.Data["json"] = lib.JSONS{Code: 203, Msg: "申请出库失败"}
  3774. c.ServeJSON()
  3775. return
  3776. }
  3777. // 3、增加占用库存
  3778. _, err = StockDao.AddOrUpdate_Occupy_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  3779. if err != nil {
  3780. o.Rollback()
  3781. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败" + err.Error()}
  3782. c.ServeJSON()
  3783. return
  3784. }
  3785. }
  3786. o.Commit()
  3787. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "出库申请", var_)
  3788. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3789. c.ServeJSON()
  3790. return
  3791. }
  3792. // 修改出库申请
  3793. func (c *StockController) StockOut_Apply_Edit() {
  3794. T_number := c.GetString("T_number") // 出库单号
  3795. //T_depot_id, _ := c.GetInt("T_depot_id")
  3796. T_project := c.GetString("T_project")
  3797. T_product := c.GetString("T_product")
  3798. T_remark := c.GetString("T_remark")
  3799. T_receive := c.GetString("T_receive")
  3800. T_company_name := c.GetString("T_company_name")
  3801. T_payment_method := c.GetString("T_payment_method")
  3802. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
  3803. o := orm.NewOrm()
  3804. o.Begin()
  3805. StockOutDao := Stock.NewStockOut(o)
  3806. StockOutProductDao := Stock.NewStockOutProduct(o)
  3807. StockDao := Stock.NewStock(o)
  3808. // 查询入库信息
  3809. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3810. if err != nil {
  3811. o.Rollback()
  3812. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3813. c.ServeJSON()
  3814. return
  3815. }
  3816. if StockOut.T_state == Stock.StockOutAlreadyOut {
  3817. o.Rollback()
  3818. c.Data["json"] = lib.JSONS{Code: 202, Msg: "当前出库单已出库,不能修改!"}
  3819. c.ServeJSON()
  3820. return
  3821. }
  3822. // 查询入库产品信息
  3823. productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  3824. var oldProductList []dto.StockProduct
  3825. var newProductList []dto.StockProduct
  3826. productOldMap := map[int]dto.StockProduct{}
  3827. allProductListMap := make(map[int]struct{})
  3828. for _, product := range productOldList {
  3829. stockProduct := dto.StockProduct{
  3830. T_product_id: product.T_product_id,
  3831. T_num: product.T_num,
  3832. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  3833. }
  3834. oldProductList = append(oldProductList, stockProduct)
  3835. productOldMap[product.T_product_id] = stockProduct
  3836. if _, ok := allProductListMap[product.T_product_id]; !ok {
  3837. allProductListMap[product.T_product_id] = struct{}{}
  3838. }
  3839. if StockOut.T_state != Stock.StockOutAuditManagerUnPass && StockOut.T_state != Stock.StockOutAuditFinanceUnPass {
  3840. // 减少占用库存
  3841. product2, _ := Basic.Read_Product_ById(product.T_product_id)
  3842. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product2.Id, product2.T_class, product2.T_name, product2.T_model, product.T_num, 1)
  3843. if err != nil {
  3844. o.Rollback()
  3845. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败"}
  3846. c.ServeJSON()
  3847. return
  3848. }
  3849. }
  3850. }
  3851. productNewList := lib.SplitString(T_product, "|")
  3852. productNewMap := map[int]dto.StockProduct{}
  3853. for _, v := range productNewList {
  3854. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  3855. product, _ := Basic.Read_Product_ById(product_id)
  3856. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  3857. if num == 0 {
  3858. o.Rollback()
  3859. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  3860. c.ServeJSON()
  3861. return
  3862. }
  3863. T_relation_sn := strings.Split(v, "-")[2]
  3864. stockProduct := dto.StockProduct{
  3865. T_product_id: product_id,
  3866. T_num: num,
  3867. T_relation_sn: lib.SplitString(T_relation_sn, ","),
  3868. }
  3869. newProductList = append(newProductList, stockProduct)
  3870. productNewMap[product_id] = stockProduct
  3871. if _, ok := allProductListMap[product_id]; !ok {
  3872. allProductListMap[product_id] = struct{}{}
  3873. }
  3874. // 增加占用库存
  3875. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  3876. if err != nil {
  3877. o.Rollback()
  3878. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败"}
  3879. c.ServeJSON()
  3880. return
  3881. }
  3882. }
  3883. // 判断产品列表信息是否相同
  3884. StockProductListIsEqual, needDelete, needAdd, needEdit, _ := dto.StockProductListsEqual(oldProductList, newProductList)
  3885. // 两次提交的数据相同,则不用修改
  3886. if !StockProductListIsEqual {
  3887. if len(needDelete) > 0 {
  3888. // 删除入库产品列表
  3889. for _, v := range needDelete {
  3890. // 删除入库产品列表
  3891. err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
  3892. if err != nil {
  3893. o.Rollback()
  3894. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  3895. c.ServeJSON()
  3896. return
  3897. }
  3898. }
  3899. }
  3900. if len(needAdd) > 0 {
  3901. // 新增入库产品列表
  3902. for _, v := range needAdd {
  3903. StockOutProduct := Stock.StockOutProduct{
  3904. T_number: T_number,
  3905. T_product_id: v.T_product_id,
  3906. T_depot_id: StockOut.T_depot_id,
  3907. T_num: v.T_num, // 出库数量
  3908. T_state: 1,
  3909. }
  3910. _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
  3911. if err != nil {
  3912. o.Rollback()
  3913. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  3914. c.ServeJSON()
  3915. return
  3916. }
  3917. }
  3918. }
  3919. if len(needEdit) > 0 {
  3920. for _, v := range needEdit {
  3921. StockOutProduct := Stock.StockOutProduct{
  3922. T_number: StockOut.T_number,
  3923. T_product_id: v.T_product_id,
  3924. T_depot_id: StockOut.T_depot_id,
  3925. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  3926. T_state: 1,
  3927. }
  3928. // 更新产品表
  3929. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  3930. if err != nil {
  3931. o.Rollback()
  3932. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  3933. c.ServeJSON()
  3934. return
  3935. }
  3936. }
  3937. }
  3938. }
  3939. StockOut.T_remark = T_remark
  3940. if len(T_project) > 0 {
  3941. StockOut.T_project = T_project
  3942. }
  3943. if len(T_company_name) > 0 {
  3944. StockOut.T_company_name = T_company_name
  3945. }
  3946. if len(T_payment_method) > 0 {
  3947. StockOut.T_payment_method = T_payment_method
  3948. }
  3949. if len(T_receive) > 0 {
  3950. StockOut.T_receive = T_receive
  3951. }
  3952. StockOut.T_state = Stock.StockOutWaitAudit
  3953. err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_company_name", "T_payment_method", "T_state")
  3954. if err != nil {
  3955. o.Rollback()
  3956. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  3957. c.ServeJSON()
  3958. return
  3959. }
  3960. o.Commit()
  3961. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "修改", StockOut)
  3962. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  3963. c.ServeJSON()
  3964. return
  3965. }
  3966. // 删除出库申请
  3967. func (c *StockController) StockOut_Apply_Del() {
  3968. T_number := c.GetString("T_number") // 入库单号
  3969. o := orm.NewOrm()
  3970. o.Begin()
  3971. StockOutDao := Stock.NewStockOut(o)
  3972. StockOutProductDao := Stock.NewStockOutProduct(o)
  3973. StockDao := Stock.NewStock(o)
  3974. // 查询出库信息
  3975. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  3976. if err != nil {
  3977. o.Rollback()
  3978. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  3979. c.ServeJSON()
  3980. return
  3981. }
  3982. // 查询出库产品信息
  3983. productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  3984. var oldProductList []dto.StockProduct
  3985. allProductList := []int{}
  3986. for _, product := range productOldList {
  3987. oldProductList = append(oldProductList, dto.StockProduct{
  3988. T_product_id: product.T_product_id,
  3989. T_num: product.T_num,
  3990. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  3991. })
  3992. allProductList = append(allProductList, product.T_product_id)
  3993. }
  3994. // 删除入库产品列表
  3995. for _, v := range oldProductList {
  3996. product, _ := Basic.Read_Product_ById(v.T_product_id)
  3997. // 删除入库产品列表
  3998. err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
  3999. if err != nil {
  4000. o.Rollback()
  4001. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
  4002. c.ServeJSON()
  4003. return
  4004. }
  4005. // 减少占用库存
  4006. _, err = StockDao.AddOrUpdate_Occupy_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  4007. if err != nil {
  4008. o.Rollback()
  4009. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新占用库存失败" + err.Error()}
  4010. c.ServeJSON()
  4011. return
  4012. }
  4013. }
  4014. err = StockOutDao.Delete_StockOut(stockOut)
  4015. if err != nil {
  4016. o.Rollback()
  4017. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
  4018. c.ServeJSON()
  4019. return
  4020. }
  4021. o.Commit()
  4022. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "删除", stockOut)
  4023. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  4024. c.ServeJSON()
  4025. return
  4026. }
  4027. // 财务
  4028. func (c *StockController) StockOut_Finance_List() {
  4029. // 分页参数 初始化
  4030. page, _ := c.GetInt("page")
  4031. if page < 1 {
  4032. page = 1
  4033. }
  4034. page_z, _ := c.GetInt("page_z")
  4035. if page_z < 1 {
  4036. page_z = conf.Page_size
  4037. }
  4038. // 查询
  4039. T_depot_id, _ := c.GetInt("T_depot_id")
  4040. T_state, _ := c.GetInt("T_state")
  4041. T_contract_number := c.GetString("T_contract_number")
  4042. T_start_date := c.GetString("T_start_date")
  4043. T_end_date := c.GetString("T_end_date")
  4044. T_name := c.GetString("T_name")
  4045. userList, _ := NatsServer.Read_User_List_All()
  4046. Account.Read_User_All_Map(userList)
  4047. Basic.Read_Depot_All_Map()
  4048. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4049. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List("", T_name, Stock.StockOutTypeFinance, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  4050. var r_jsons lib.R_JSONS
  4051. r_jsons.Num = R_cnt
  4052. r_jsons.Data = R_List
  4053. r_jsons.Page = page
  4054. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  4055. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  4056. c.ServeJSON()
  4057. return
  4058. }
  4059. // 管理员
  4060. func (c *StockController) StockOut_Manager_List() {
  4061. // 分页参数 初始化
  4062. page, _ := c.GetInt("page")
  4063. if page < 1 {
  4064. page = 1
  4065. }
  4066. page_z, _ := c.GetInt("page_z")
  4067. if page_z < 1 {
  4068. page_z = conf.Page_size
  4069. }
  4070. // 查询
  4071. T_depot_id, _ := c.GetInt("T_depot_id")
  4072. T_state, _ := c.GetInt("T_state")
  4073. T_contract_number := c.GetString("T_contract_number")
  4074. T_start_date := c.GetString("T_start_date")
  4075. T_end_date := c.GetString("T_end_date")
  4076. T_name := c.GetString("T_name")
  4077. userList, _ := NatsServer.Read_User_List_All()
  4078. Account.Read_User_All_Map(userList)
  4079. Basic.Read_Depot_All_Map()
  4080. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4081. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List("", T_name, Stock.StockOutTypeManager, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  4082. var r_jsons lib.R_JSONS
  4083. r_jsons.Num = R_cnt
  4084. r_jsons.Data = R_List
  4085. r_jsons.Page = page
  4086. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  4087. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  4088. c.ServeJSON()
  4089. return
  4090. }
  4091. // 仓管
  4092. func (c *StockController) StockOut_Warehouse_List() {
  4093. // 分页参数 初始化
  4094. page, _ := c.GetInt("page")
  4095. if page < 1 {
  4096. page = 1
  4097. }
  4098. page_z, _ := c.GetInt("page_z")
  4099. if page_z < 1 {
  4100. page_z = conf.Page_size
  4101. }
  4102. // 查询
  4103. T_depot_id, _ := c.GetInt("T_depot_id")
  4104. T_state, _ := c.GetInt("T_state")
  4105. T_contract_number := c.GetString("T_contract_number")
  4106. T_start_date := c.GetString("T_start_date")
  4107. T_end_date := c.GetString("T_end_date")
  4108. T_name := c.GetString("T_name")
  4109. userList, _ := NatsServer.Read_User_List_All()
  4110. Account.Read_User_All_Map(userList)
  4111. Basic.Read_Depot_All_Map()
  4112. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4113. R_List, R_cnt := StockOutDao.Read_StockOut_Audit_List(c.User.T_uuid, T_name, Stock.StockOutTypeWarehouse, T_state, T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  4114. var r_jsons lib.R_JSONS
  4115. r_jsons.Num = R_cnt
  4116. r_jsons.Data = R_List
  4117. r_jsons.Page = page
  4118. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  4119. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  4120. c.ServeJSON()
  4121. return
  4122. }
  4123. // 财务/总经理审核
  4124. func (c *StockController) StockOut_Audit() {
  4125. T_number := c.GetString("T_number") // 出库单号
  4126. //T_depot_id, _ := c.GetInt("T_depot_id")
  4127. T_audit, _ := c.GetInt("T_audit") // 审核 2财务通过 3财务不通过 4总经理通过 5总经理不通过 6已出库
  4128. T_approval_opinion := c.GetString("T_approval_opinion") // 审批意见
  4129. T_approval_img := c.GetString("T_approval_img") // 审批意见
  4130. T_type := c.GetString("T_type") // 类型 Finance Manager
  4131. o := orm.NewOrm()
  4132. o.Begin()
  4133. StockOutDao := Stock.NewStockOut(o)
  4134. StockDao := Stock.NewStock(o)
  4135. StockOutProductDao := Stock.NewStockOutProduct(o)
  4136. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  4137. if err != nil {
  4138. o.Rollback()
  4139. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  4140. c.ServeJSON()
  4141. return
  4142. }
  4143. StockOut.T_state = T_audit
  4144. if T_type == Stock.StockOutTypeFinance {
  4145. StockOut.T_finance_approval_opinion = T_approval_opinion
  4146. StockOut.T_finance_approval_img = T_approval_img
  4147. } else {
  4148. StockOut.T_manager_approval_opinion = T_approval_opinion
  4149. StockOut.T_manager_approval_img = T_approval_img
  4150. }
  4151. if T_audit == Stock.StockOutAuditFinanceUnPass || T_audit == Stock.StockOutAuditManagerUnPass {
  4152. productList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  4153. for _, v := range productList {
  4154. product, _ := Basic.Read_Product_ById(v.T_product_id)
  4155. _, err = StockDao.AddOrUpdate_Occupy_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  4156. if err != nil {
  4157. o.Rollback()
  4158. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  4159. c.ServeJSON()
  4160. return
  4161. }
  4162. }
  4163. }
  4164. err = StockOutDao.Update_StockOut(StockOut, "T_state", "T_finance_approval_opinion", "T_manager_approval_opinion", "T_finance_approval_img", "T_manager_approval_img")
  4165. if err != nil {
  4166. o.Rollback()
  4167. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库审批失败"}
  4168. c.ServeJSON()
  4169. return
  4170. }
  4171. o.Commit()
  4172. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "审批", StockOut)
  4173. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  4174. c.ServeJSON()
  4175. return
  4176. }
  4177. // 仓管出库
  4178. func (c *StockController) StockOut_Warehouse() {
  4179. T_number := c.GetString("T_number") // 出库单号
  4180. T_date := c.GetString("T_date")
  4181. date, is := lib.DateStrToTime(T_date)
  4182. if !is {
  4183. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  4184. c.ServeJSON()
  4185. return
  4186. }
  4187. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "确认出库", T_number)
  4188. o := orm.NewOrm()
  4189. o.Begin()
  4190. StockOutDao := Stock.NewStockOut(o)
  4191. StockOutProductDao := Stock.NewStockOutProduct(o)
  4192. DeviceDao := Stock.NewDevice(o)
  4193. StockDao := Stock.NewStock(o)
  4194. // 查询入库信息
  4195. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  4196. if err != nil {
  4197. o.Rollback()
  4198. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  4199. c.ServeJSON()
  4200. return
  4201. }
  4202. T_product := c.GetString("T_product")
  4203. productList := lib.SplitString(T_product, "|")
  4204. allProductList := []int{}
  4205. for _, v := range productList {
  4206. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  4207. allProductList = append(allProductList, product_id)
  4208. product, _ := Basic.Read_Product_ById(product_id)
  4209. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  4210. if num == 0 {
  4211. o.Rollback()
  4212. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s出库数量不能为0!", product.T_name)}
  4213. c.ServeJSON()
  4214. return
  4215. }
  4216. T_relation_sn := strings.Split(v, "-")[2]
  4217. if T_relation_sn == "" && product.T_relation_sn == 1 {
  4218. o.Rollback()
  4219. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  4220. c.ServeJSON()
  4221. return
  4222. }
  4223. if len(T_relation_sn) > 0 {
  4224. snList := strings.Split(T_relation_sn, ",")
  4225. if product.T_relation_sn == 1 && num != len(snList) {
  4226. o.Rollback()
  4227. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s SN数量与出库数量不一致,请核查!", product.T_name)}
  4228. c.ServeJSON()
  4229. return
  4230. }
  4231. if len(snList) > 0 {
  4232. for _, sn := range snList {
  4233. mqtt := Stock.Read_MqttUser(sn)
  4234. // 根据IMEI查询上次入库的批次号
  4235. batchNumber, err := DeviceDao.Read_Device_BatchNumber_ByImei(mqtt.Imei)
  4236. if err != nil {
  4237. logs.Error(lib.FuncName(), err)
  4238. }
  4239. device := Stock.Device{
  4240. T_contract_number: StockOut.T_contract_number,
  4241. T_product_id: product_id,
  4242. T_out_number: T_number,
  4243. T_sn: sn,
  4244. T_iccid: mqtt.Iccid,
  4245. T_imei: mqtt.Imei,
  4246. T_State: 1,
  4247. T_project: StockOut.T_project,
  4248. T_batch_number: batchNumber,
  4249. CreateTime: date,
  4250. }
  4251. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  4252. if err != nil {
  4253. o.Rollback()
  4254. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  4255. c.ServeJSON()
  4256. return
  4257. }
  4258. }
  4259. }
  4260. }
  4261. // 3、添加出库产品清单
  4262. stockOutProduct := Stock.StockOutProduct{
  4263. T_number: T_number,
  4264. T_product_id: product_id,
  4265. T_depot_id: StockOut.T_depot_id,
  4266. T_num: num, // 出库数量
  4267. T_date: T_date, // 出库日期
  4268. T_relation_sn: T_relation_sn,
  4269. T_state: 1,
  4270. }
  4271. // 更新产品表
  4272. err = StockOutProductDao.Update_StockOutProduct(stockOutProduct)
  4273. if err != nil {
  4274. o.Rollback()
  4275. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  4276. c.ServeJSON()
  4277. return
  4278. }
  4279. // 5、更新产品库存列表、占用库存
  4280. _, err = StockDao.Stock_Warehouse(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num)
  4281. if err != nil {
  4282. o.Rollback()
  4283. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  4284. c.ServeJSON()
  4285. return
  4286. }
  4287. }
  4288. StockOut.T_warehouse = c.User.T_uuid
  4289. StockOut.T_state = Stock.StockOutAlreadyOut
  4290. StockOut.T_date = T_date
  4291. err = StockOutDao.Update_StockOut(StockOut, "T_state", "T_date", "T_warehouse")
  4292. if err != nil {
  4293. o.Rollback()
  4294. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库审批失败"}
  4295. c.ServeJSON()
  4296. return
  4297. }
  4298. o.Commit()
  4299. StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
  4300. NatsServer.AddUserLogs(c.User.T_uuid, "出库申请", "出库", StockOut)
  4301. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  4302. c.ServeJSON()
  4303. return
  4304. }
  4305. // StockMonth_Update_Return 批量更新所有StockMonth的退库数据
  4306. func (c *StockController) StockMonth_Update_Return() {
  4307. // 可选参数:指定仓库和时间范围
  4308. T_depot_id, _ := c.GetInt("T_depot_id")
  4309. T_start_date := c.GetString("T_start_date") // 格式:2024-01
  4310. T_end_date := c.GetString("T_end_date") // 格式:2024-12
  4311. // 调用后台更新函数
  4312. count, err := UpdateAllStockMonthReturn(T_depot_id, T_start_date, T_end_date)
  4313. if err != nil {
  4314. c.Data["json"] = lib.JSONS{Code: 203, Msg: fmt.Sprintf("更新失败:%v", err)}
  4315. c.ServeJSON()
  4316. return
  4317. }
  4318. NatsServer.AddUserLogs(c.User.T_uuid, "库存月度统计", "批量更新退库数据", fmt.Sprintf("更新了%d条记录", count))
  4319. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: fmt.Sprintf("成功更新%d条记录", count)}
  4320. c.ServeJSON()
  4321. }
  4322. // UpdateAllStockMonthReturn 批量更新所有StockMonth记录的退库数量
  4323. // T_depot_id: 仓库ID,0表示所有仓库
  4324. // T_start_date: 开始月份,格式:2024-01,为空则不限制
  4325. // T_end_date: 结束月份,格式:2024-12,为空则不限制
  4326. func UpdateAllStockMonthReturn(T_depot_id int, T_start_date, T_end_date string) (count int, err error) {
  4327. o := orm.NewOrm()
  4328. StockMonthDao := Stock.NewStockMonth(o)
  4329. StockInProductDao := Stock.NewStockInProduct(o)
  4330. // 读取所有StockMonth记录
  4331. allStockMonths, err := StockMonthDao.Read_All_StockMonth()
  4332. if err != nil {
  4333. logs.Error(lib.FuncName(), err)
  4334. return 0, err
  4335. }
  4336. count = 0
  4337. for _, stockMonth := range allStockMonths {
  4338. // 如果指定了仓库ID,则过滤
  4339. if T_depot_id > 0 && stockMonth.T_depot_id != T_depot_id {
  4340. continue
  4341. }
  4342. // 如果指定了开始月份,则过滤
  4343. if len(T_start_date) > 0 && stockMonth.T_month < T_start_date {
  4344. continue
  4345. }
  4346. // 如果指定了结束月份,则过滤
  4347. if len(T_end_date) > 0 && stockMonth.T_month > T_end_date {
  4348. continue
  4349. }
  4350. // 查询该产品在该月份的退库数量
  4351. returnTotal := StockInProductDao.Read_StockReturn_Total(stockMonth.T_depot_id, stockMonth.T_product_id, stockMonth.T_month)
  4352. // 如果退库数量与原值不同,则更新
  4353. if stockMonth.T_return != returnTotal {
  4354. stockMonth.T_return = returnTotal
  4355. err = StockMonthDao.Update_StockMonth(stockMonth, "T_return")
  4356. if err != nil {
  4357. logs.Error(lib.FuncName(), fmt.Sprintf("更新失败:仓库%d,产品%d,月份%s", stockMonth.T_depot_id, stockMonth.T_product_id, stockMonth.T_month), err)
  4358. continue
  4359. }
  4360. count++
  4361. }
  4362. }
  4363. logs.Info(fmt.Sprintf("批量更新退库数据完成,共更新%d条记录", count))
  4364. return count, nil
  4365. }
  4366. // Company_Device_Statistics 查询公司借出和退回设备统计
  4367. func (c *StockController) Company_Device_Statistics() {
  4368. // 查询参数(对前端返回扁平化分组列表,不再返回分页包装)
  4369. T_company_name := c.GetString("T_company_name") // 公司名称
  4370. T_depot_id, _ := c.GetInt("T_depot_id") // 仓库ID
  4371. T_start_date := c.GetString("T_start_date") // 开始日期
  4372. T_end_date := c.GetString("T_end_date") // 结束日期
  4373. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4374. R_List, _ := StockOutDao.Read_Company_Device_Statistics(T_company_name, T_start_date, T_end_date, T_depot_id, 1, 9999)
  4375. // 1) 先按公司分组并排序,顺序与导出一致
  4376. companyMap := make(map[string][]Stock.CompanyDeviceStatistics)
  4377. for _, stat := range R_List {
  4378. if len(stat.T_stock_out_list) == 0 {
  4379. continue
  4380. }
  4381. companyMap[stat.T_company_name] = append(companyMap[stat.T_company_name], stat)
  4382. }
  4383. companyNames := make([]string, 0, len(companyMap))
  4384. for name := range companyMap {
  4385. companyNames = append(companyNames, name)
  4386. }
  4387. sort.Strings(companyNames)
  4388. // 2) 构造前端友好的结构:按公司返回 rows 列表 + 汇总
  4389. type RowItem struct {
  4390. Seq int `json:"seq"`
  4391. OutNumber string `json:"out_number"`
  4392. ProductName string `json:"product_name"`
  4393. DeviceSN string `json:"device_sn"`
  4394. Num int `json:"num"`
  4395. Unit string `json:"unit"`
  4396. Remark string `json:"remark"`
  4397. ReturnNumber string `json:"return_number"`
  4398. ReturnDeviceSN string `json:"return_device_sn"`
  4399. ReturnRemark string `json:"return_remark"`
  4400. }
  4401. type CompanyBlock struct {
  4402. CompanyName string `json:"company_name"`
  4403. StatisticDateLabel string `json:"statistic_date_label"`
  4404. OutTotal int `json:"out_total"`
  4405. ReturnTotal int `json:"return_total"`
  4406. Rows []RowItem `json:"rows"`
  4407. }
  4408. resp := make([]CompanyBlock, 0, len(companyNames))
  4409. for _, companyName := range companyNames {
  4410. statList := companyMap[companyName]
  4411. // 构建退库 SN -> 信息 的映射
  4412. type ReturnInfo struct {
  4413. ReturnNumber string
  4414. SN string
  4415. Num int
  4416. Unit string
  4417. Total float64
  4418. Remark string
  4419. }
  4420. returnSNMap := make(map[string]ReturnInfo)
  4421. for _, stat := range statList {
  4422. for _, ret := range stat.T_stock_return_list {
  4423. for _, sn := range ret.T_device_list {
  4424. if len(sn) > 0 {
  4425. returnSNMap[sn] = ReturnInfo{
  4426. ReturnNumber: ret.T_stock_return_number,
  4427. SN: sn,
  4428. Num: ret.T_num,
  4429. Unit: ret.T_unit,
  4430. Total: ret.T_total,
  4431. Remark: ret.T_remark,
  4432. }
  4433. }
  4434. }
  4435. }
  4436. }
  4437. seqNo := 1
  4438. cumulativeOutTotal := 0
  4439. cumulativeReturnTotal := 0
  4440. rows := make([]RowItem, 0)
  4441. for _, stat := range statList {
  4442. for _, out := range stat.T_stock_out_list {
  4443. validSNs := make([]string, 0)
  4444. for _, sn := range out.T_device_list {
  4445. if len(sn) > 0 {
  4446. validSNs = append(validSNs, sn)
  4447. }
  4448. }
  4449. if len(validSNs) == 0 {
  4450. continue
  4451. }
  4452. cumulativeOutTotal += out.T_num
  4453. currentReturnNum := 0
  4454. returnRemark := ""
  4455. for _, sn := range validSNs {
  4456. if retInfo, exists := returnSNMap[sn]; exists {
  4457. currentReturnNum++
  4458. if len(returnRemark) == 0 {
  4459. returnRemark = retInfo.Remark
  4460. }
  4461. }
  4462. }
  4463. cumulativeReturnTotal += currentReturnNum
  4464. for idx, sn := range validSNs {
  4465. row := RowItem{
  4466. DeviceSN: sn,
  4467. }
  4468. if retInfo, ok := returnSNMap[sn]; ok {
  4469. row.ReturnDeviceSN = sn
  4470. row.ReturnNumber = retInfo.ReturnNumber
  4471. }
  4472. if idx == 0 {
  4473. row.Seq = seqNo
  4474. row.ProductName = out.T_product_name
  4475. row.OutNumber = stat.T_stock_out_number
  4476. row.Num = out.T_num
  4477. row.Unit = out.T_unit
  4478. row.Remark = out.T_remark
  4479. if len(returnRemark) > 0 {
  4480. row.ReturnRemark = returnRemark
  4481. }
  4482. }
  4483. rows = append(rows, row)
  4484. }
  4485. seqNo++
  4486. }
  4487. }
  4488. resp = append(resp, CompanyBlock{
  4489. OutTotal: cumulativeOutTotal,
  4490. ReturnTotal: cumulativeReturnTotal,
  4491. Rows: rows,
  4492. })
  4493. }
  4494. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: resp}
  4495. c.ServeJSON()
  4496. return
  4497. }
  4498. // Company_Device_Statistics_Excel 导出公司借出和退回设备统计Excel
  4499. func (c *StockController) Company_Device_Statistics_Excel() {
  4500. // 查询参数
  4501. T_company_name := c.GetString("T_company_name") // 公司名称
  4502. T_depot_id, _ := c.GetInt("T_depot_id") // 仓库ID
  4503. T_start_date := c.GetString("T_start_date") // 开始日期
  4504. T_end_date := c.GetString("T_end_date") // 结束日期
  4505. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4506. R_List, _ := StockOutDao.Read_Company_Device_Statistics(T_company_name, T_start_date, T_end_date, T_depot_id, 1, 9999)
  4507. // 生成Excel文件名
  4508. filename := fmt.Sprintf("公司设备借出退回统计(%s)", lib.GetRandstring(6, "0123456789", 0))
  4509. // 创建Excel文件
  4510. f := excelize.NewFile()
  4511. // 设置样式
  4512. StyleTitle, _ := f.NewStyle(
  4513. &excelize.Style{
  4514. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  4515. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  4516. })
  4517. StyleHeader, _ := f.NewStyle(
  4518. &excelize.Style{
  4519. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  4520. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  4521. Border: []excelize.Border{
  4522. {Type: "left", Color: "000000", Style: 1},
  4523. {Type: "top", Color: "000000", Style: 1},
  4524. {Type: "bottom", Color: "000000", Style: 1},
  4525. {Type: "right", Color: "000000", Style: 1},
  4526. },
  4527. Fill: excelize.Fill{Type: "pattern", Color: []string{"#E0E0E0"}, Pattern: 1},
  4528. })
  4529. StyleCell, _ := f.NewStyle(
  4530. &excelize.Style{
  4531. Font: &excelize.Font{Size: 10, Family: "宋体"},
  4532. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  4533. Border: []excelize.Border{
  4534. {Type: "left", Color: "000000", Style: 1},
  4535. {Type: "top", Color: "000000", Style: 1},
  4536. {Type: "bottom", Color: "000000", Style: 1},
  4537. {Type: "right", Color: "000000", Style: 1},
  4538. },
  4539. })
  4540. currentRow := 1
  4541. sheetName := "Sheet1"
  4542. // 按公司名称分组数据
  4543. companyMap := make(map[string][]Stock.CompanyDeviceStatistics)
  4544. for _, stat := range R_List {
  4545. if len(stat.T_stock_out_list) == 0 {
  4546. continue
  4547. }
  4548. companyMap[stat.T_company_name] = append(companyMap[stat.T_company_name], stat)
  4549. }
  4550. // 提取并排序公司名称,确保导出顺序稳定
  4551. companyNames := make([]string, 0, len(companyMap))
  4552. for name := range companyMap {
  4553. companyNames = append(companyNames, name)
  4554. }
  4555. sort.Strings(companyNames)
  4556. // 按排序后的公司名称遍历
  4557. for _, companyName := range companyNames {
  4558. statList := companyMap[companyName]
  4559. // 合并标题行
  4560. f.MergeCell(sheetName, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("L%d", currentRow))
  4561. f.SetCellValue(sheetName, fmt.Sprintf("A%d", currentRow), "出库申请单/退库单")
  4562. f.SetCellStyle(sheetName, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("L%d", currentRow), StyleTitle)
  4563. f.SetRowHeight(sheetName, currentRow, 25)
  4564. currentRow++
  4565. // 出库信息行
  4566. f.MergeCell(sheetName, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("H%d", currentRow))
  4567. f.SetCellValue(sheetName, fmt.Sprintf("A%d", currentRow), "公司名称:"+companyName)
  4568. f.MergeCell(sheetName, fmt.Sprintf("I%d", currentRow), fmt.Sprintf("L%d", currentRow))
  4569. f.SetCellValue(sheetName, fmt.Sprintf("I%d", currentRow), "")
  4570. f.SetCellStyle(sheetName, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("L%d", currentRow), StyleCell)
  4571. currentRow++
  4572. // 日期行
  4573. f.MergeCell(sheetName, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("H%d", currentRow))
  4574. f.SetCellValue(sheetName, fmt.Sprintf("A%d", currentRow), "统计日期:"+time.Now().Format("2006-01-02"))
  4575. f.MergeCell(sheetName, fmt.Sprintf("I%d", currentRow), fmt.Sprintf("L%d", currentRow))
  4576. f.SetCellValue(sheetName, fmt.Sprintf("I%d", currentRow), "")
  4577. f.SetCellStyle(sheetName, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("L%d", currentRow), StyleCell)
  4578. currentRow++
  4579. // 表头(增加出库单号和退库单号列)
  4580. headers := []string{"序号", "出库单号", "品名", "设备编号", "数量", "单位", "累计", "备注", "退库单号", "退库设备编号", "累计", "备注"}
  4581. for i, header := range headers {
  4582. cell := fmt.Sprintf("%s%d", string(rune('A'+i)), currentRow)
  4583. f.SetCellValue(sheetName, cell, header)
  4584. f.SetCellStyle(sheetName, cell, cell, StyleHeader)
  4585. }
  4586. currentRow++
  4587. // 构建出库SN到退库信息的映射(包含退库单号)
  4588. type ReturnInfo struct {
  4589. ReturnNumber string // 退库单号
  4590. SN string
  4591. Num int
  4592. Unit string
  4593. Total float64
  4594. Remark string
  4595. }
  4596. returnSNMap := make(map[string]ReturnInfo) // 出库SN -> 退库信息
  4597. // 收集该公司所有的退库信息
  4598. for _, stat := range statList {
  4599. for _, ret := range stat.T_stock_return_list {
  4600. for _, sn := range ret.T_device_list {
  4601. if len(sn) > 0 {
  4602. returnSNMap[sn] = ReturnInfo{
  4603. ReturnNumber: ret.T_stock_return_number, // 关联的退库单号
  4604. SN: sn,
  4605. Num: ret.T_num,
  4606. Unit: ret.T_unit,
  4607. Total: ret.T_total,
  4608. Remark: ret.T_remark,
  4609. }
  4610. }
  4611. }
  4612. }
  4613. }
  4614. // 遍历该公司所有出库单的出库明细,按SN展开
  4615. seqNo := 1
  4616. cumulativeOutTotal := 0 // 累计出库总数
  4617. cumulativeReturnTotal := 0 // 累计退库总数
  4618. cumulativeStartRow := currentRow
  4619. // 记录每个出库单号的起始行和结束行,用于后续合并
  4620. type OutNumberRange struct {
  4621. StartRow int
  4622. EndRow int
  4623. Number string
  4624. }
  4625. outNumberRanges := make([]OutNumberRange, 0)
  4626. // 记录每个退库单号的起始行和结束行
  4627. type ReturnNumberRange struct {
  4628. StartRow int
  4629. EndRow int
  4630. Number string
  4631. }
  4632. //returnNumberRanges := make([]ReturnNumberRange, 0)
  4633. for _, stat := range statList {
  4634. outNumberStartRow := currentRow // 记录当前出库单号的起始行
  4635. //hasReturnInThisOut := false // 标记当前出库单是否有退库
  4636. //returnNumberStartRow := currentRow
  4637. for _, out := range stat.T_stock_out_list {
  4638. // 过滤掉空的SN
  4639. validSNs := make([]string, 0)
  4640. for _, sn := range out.T_device_list {
  4641. if len(sn) > 0 {
  4642. validSNs = append(validSNs, sn)
  4643. }
  4644. }
  4645. if len(validSNs) == 0 {
  4646. continue
  4647. }
  4648. // 累加当前产品的出库数量
  4649. cumulativeOutTotal += out.T_num
  4650. // 计算当前产品的退库数量和获取退库备注
  4651. currentReturnNum := 0
  4652. returnRemark := ""
  4653. for _, sn := range validSNs {
  4654. if retInfo, exists := returnSNMap[sn]; exists {
  4655. currentReturnNum++
  4656. if len(returnRemark) == 0 {
  4657. returnRemark = retInfo.Remark // 使用第一个退库记录的备注
  4658. }
  4659. }
  4660. }
  4661. cumulativeReturnTotal += currentReturnNum
  4662. // 标记该出库单是否有退库
  4663. //if currentReturnNum > 0 {
  4664. // hasReturnInThisOut = true
  4665. //}
  4666. startRow := currentRow
  4667. // 为每个SN创建一行
  4668. for idx, sn := range validSNs {
  4669. // 出库单号(C列)和设备编号(D列)
  4670. f.SetCellValue(sheetName, fmt.Sprintf("D%d", currentRow), sn)
  4671. // 退库信息(如果该SN已退库,则显示)
  4672. if retInfo, exists := returnSNMap[sn]; exists {
  4673. f.SetCellValue(sheetName, fmt.Sprintf("J%d", currentRow), sn) // 退库设备编号
  4674. f.SetCellValue(sheetName, fmt.Sprintf("I%d", currentRow), retInfo.ReturnNumber)
  4675. // 退库单号只在第一行显示
  4676. // if idx == 0 && currentReturnNum > 0 {
  4677. // f.SetCellValue(sheetName, fmt.Sprintf("I%d", currentRow), retInfo.ReturnNumber)
  4678. // }
  4679. }
  4680. // 只在第一行设置序号、品名、出库单号、数量、单位、累计、备注
  4681. if idx == 0 {
  4682. f.SetCellValue(sheetName, fmt.Sprintf("A%d", currentRow), seqNo)
  4683. f.SetCellValue(sheetName, fmt.Sprintf("C%d", currentRow), out.T_product_name)
  4684. f.SetCellValue(sheetName, fmt.Sprintf("B%d", currentRow), stat.T_stock_out_number) // 出库单号
  4685. f.SetCellValue(sheetName, fmt.Sprintf("E%d", currentRow), out.T_num)
  4686. f.SetCellValue(sheetName, fmt.Sprintf("F%d", currentRow), out.T_unit)
  4687. //f.SetCellValue(sheetName, fmt.Sprintf("G%d", currentRow), cumulativeOutTotal) // 显示累计出库总数
  4688. f.SetCellValue(sheetName, fmt.Sprintf("H%d", currentRow), out.T_remark)
  4689. // 退库数量、单位、累计合计、备注
  4690. if currentReturnNum > 0 {
  4691. //f.SetCellValue(sheetName, fmt.Sprintf("K%d", currentRow), currentReturnNum)
  4692. //f.SetCellValue(sheetName, fmt.Sprintf("L%d", currentRow), out.T_unit)
  4693. //f.SetCellValue(sheetName, fmt.Sprintf("M%d", currentRow), cumulativeReturnTotal) // 显示累计退库总数
  4694. f.SetCellValue(sheetName, fmt.Sprintf("L%d", currentRow), returnRemark) // 显示退库备注
  4695. }
  4696. }
  4697. // 设置单元格样式
  4698. for col := 'A'; col <= 'L'; col++ {
  4699. cell := fmt.Sprintf("%s%d", string(col), currentRow)
  4700. f.SetCellStyle(sheetName, cell, cell, StyleCell)
  4701. }
  4702. currentRow++
  4703. }
  4704. // 如果有多个SN,合并序号、品名、数量、单位、累计、备注列(出库单号C列和退库单号I列在外层统一合并)
  4705. if len(validSNs) > 1 {
  4706. endRow := currentRow - 1
  4707. f.MergeCell(sheetName, fmt.Sprintf("A%d", startRow), fmt.Sprintf("A%d", endRow))
  4708. f.MergeCell(sheetName, fmt.Sprintf("C%d", startRow), fmt.Sprintf("C%d", endRow))
  4709. f.MergeCell(sheetName, fmt.Sprintf("E%d", startRow), fmt.Sprintf("E%d", endRow))
  4710. f.MergeCell(sheetName, fmt.Sprintf("F%d", startRow), fmt.Sprintf("F%d", endRow))
  4711. f.MergeCell(sheetName, fmt.Sprintf("G%d", startRow), fmt.Sprintf("G%d", endRow))
  4712. f.MergeCell(sheetName, fmt.Sprintf("H%d", startRow), fmt.Sprintf("H%d", endRow))
  4713. }
  4714. seqNo++
  4715. }
  4716. // 记录当前出库单号的范围
  4717. if currentRow > outNumberStartRow {
  4718. outNumberRanges = append(outNumberRanges, OutNumberRange{
  4719. StartRow: outNumberStartRow,
  4720. EndRow: currentRow - 1,
  4721. Number: stat.T_stock_out_number,
  4722. })
  4723. }
  4724. }
  4725. // 合并相同出库单号的单元格(C列)
  4726. for _, rng := range outNumberRanges {
  4727. if rng.EndRow > rng.StartRow {
  4728. f.MergeCell(sheetName, fmt.Sprintf("B%d", rng.StartRow), fmt.Sprintf("B%d", rng.EndRow))
  4729. }
  4730. }
  4731. // 合并相同退库单号的单元格(I列)
  4732. // for _, rng := range returnNumberRanges {
  4733. // if rng.EndRow > rng.StartRow {
  4734. // f.MergeCell(sheetName, fmt.Sprintf("I%d", rng.StartRow), fmt.Sprintf("I%d", rng.EndRow))
  4735. // }
  4736. // }
  4737. // 合并出库累计
  4738. f.MergeCell(sheetName, fmt.Sprintf("G%d", cumulativeStartRow), fmt.Sprintf("G%d", currentRow-1))
  4739. f.SetCellValue(sheetName, fmt.Sprintf("G%d", cumulativeStartRow), cumulativeOutTotal) // 显示累计出库总数
  4740. // 合并退库累计
  4741. f.MergeCell(sheetName, fmt.Sprintf("K%d", cumulativeStartRow), fmt.Sprintf("K%d", currentRow-1))
  4742. f.SetCellValue(sheetName, fmt.Sprintf("K%d", cumulativeStartRow), cumulativeReturnTotal) // 显示累计退库总数
  4743. // 添加空行
  4744. currentRow++
  4745. }
  4746. // 设置列宽(14列)
  4747. f.SetColWidth(sheetName, "A", "A", 8) // 序号
  4748. f.SetColWidth(sheetName, "B", "B", 20) // 品名
  4749. f.SetColWidth(sheetName, "C", "C", 20) // 出库单号
  4750. f.SetColWidth(sheetName, "D", "D", 30) // 设备编号
  4751. f.SetColWidth(sheetName, "E", "F", 10) // 数量、单位
  4752. f.SetColWidth(sheetName, "G", "G", 12) // 累计
  4753. f.SetColWidth(sheetName, "H", "H", 20) // 备注
  4754. f.SetColWidth(sheetName, "I", "I", 20) // 退库单号
  4755. f.SetColWidth(sheetName, "J", "J", 30) // 退库设备编号
  4756. f.SetColWidth(sheetName, "K", "K", 12) // 累计
  4757. f.SetColWidth(sheetName, "L", "L", 20) // 备注
  4758. // 保存文件
  4759. filepath := "./ofile/" + filename + ".xlsx"
  4760. if err := f.SaveAs(filepath); err != nil {
  4761. logs.Error(lib.FuncName(), err)
  4762. c.Data["json"] = lib.JSONS{Code: 201, Msg: "生成Excel失败"}
  4763. c.ServeJSON()
  4764. return
  4765. }
  4766. var url string
  4767. //// 上传 OSS
  4768. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  4769. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  4770. if !is {
  4771. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  4772. c.ServeJSON()
  4773. return
  4774. }
  4775. //删除目录
  4776. err := os.Remove("ofile/" + filename + ".xlsx")
  4777. if err != nil {
  4778. logs.Error(lib.FuncName(), err)
  4779. }
  4780. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  4781. c.ServeJSON()
  4782. return
  4783. }
  4784. // Company_Name_List 获取所有出库的公司名称列表
  4785. func (c *StockController) Stock_Out_Company_Name_List() {
  4786. T_name := c.GetString("T_name") // 公司名称(模糊查询)
  4787. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4788. R_List, err := StockOutDao.Read_Company_Name_List(T_name)
  4789. if err != nil {
  4790. c.Data["json"] = lib.JSONS{Code: 201, Msg: "查询失败"}
  4791. c.ServeJSON()
  4792. return
  4793. }
  4794. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_List}
  4795. c.ServeJSON()
  4796. return
  4797. }
  4798. // Company_Name_List 获取所有出库的公司名称列表
  4799. func (c *StockController) Stock_Out_Project_List() {
  4800. T_name := c.GetString("T_name") // 公司名称(模糊查询)
  4801. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  4802. R_List, err := StockOutDao.Read_Project_List(T_name)
  4803. if err != nil {
  4804. c.Data["json"] = lib.JSONS{Code: 201, Msg: "查询失败"}
  4805. c.ServeJSON()
  4806. return
  4807. }
  4808. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_List}
  4809. c.ServeJSON()
  4810. return
  4811. }