Stock.go 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825
  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/Property"
  12. "ERP_storage/models/Stock"
  13. "fmt"
  14. natslibs "gogs.baozhida.cn/zoie/ERP_libs/Nats"
  15. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  16. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  17. "github.com/beego/beego/v2/adapter/orm"
  18. beego "github.com/beego/beego/v2/server/web"
  19. "github.com/robfig/cron/v3"
  20. "github.com/xuri/excelize/v2"
  21. "math"
  22. "os"
  23. "strconv"
  24. "strings"
  25. "time"
  26. )
  27. type StockController struct {
  28. beego.Controller
  29. User userlibs.User
  30. }
  31. func (c *StockController) Prepare() {
  32. c.User = *Account.User_r
  33. }
  34. func (c *StockController) Device_List() {
  35. // 分页参数 初始化
  36. page, _ := c.GetInt("page")
  37. if page < 1 {
  38. page = 1
  39. }
  40. page_z, _ := c.GetInt("page_z")
  41. if page_z < 1 {
  42. page_z = conf.Page_size
  43. }
  44. T_state, _ := c.GetInt("T_state")
  45. T_name := c.GetString("T_name")
  46. T_product_name := c.GetString("T_product_name") // 产品名称
  47. T_product_model := c.GetString("T_product_model") // 产品型号
  48. DeviceDao := Stock.NewDevice(orm.NewOrm())
  49. R_List, R_cnt := DeviceDao.Read_Device_List(T_name, T_product_name, T_product_model, T_state, page, page_z)
  50. var r_jsons lib.R_JSONS
  51. r_jsons.Num = R_cnt
  52. r_jsons.Data = R_List
  53. r_jsons.Page = page
  54. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  55. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  56. c.ServeJSON()
  57. return
  58. }
  59. func (c *StockController) Device_Check() {
  60. T_sn := c.GetString("T_sn")
  61. T_type, _ := c.GetInt("T_type") //1-出库 2-入库
  62. DeviceDao := Stock.NewDevice(orm.NewOrm())
  63. device, err := DeviceDao.Read_Device_ByT_sn(T_sn)
  64. if err != nil && err.Error() != orm.ErrNoRows.Error() {
  65. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请稍后重试!"}
  66. c.ServeJSON()
  67. return
  68. }
  69. // T_State 1-已出库 2-未出库/入库
  70. if len(device.T_in_number) > 0 && device.T_State == 2 && T_type == 2 {
  71. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已入库,请勿重复提交!", T_sn)}
  72. c.ServeJSON()
  73. return
  74. }
  75. if T_type == 2 {
  76. mqtt := Stock.Read_MqttUser(T_sn)
  77. if len(mqtt.Username) == 0 {
  78. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未授权,请先授权!", T_sn)}
  79. c.ServeJSON()
  80. return
  81. }
  82. }
  83. if len(device.T_out_number) > 0 && device.T_State == 1 && T_type == 1 {
  84. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】已出库,请勿重复提交!", T_sn)}
  85. c.ServeJSON()
  86. return
  87. }
  88. if T_type == 1 {
  89. if device.Id == 0 {
  90. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】未入库,请先入库!", T_sn)}
  91. c.ServeJSON()
  92. return
  93. }
  94. }
  95. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  96. c.ServeJSON()
  97. return
  98. }
  99. func (c *StockController) Stock_List() {
  100. // 分页参数 初始化
  101. page, _ := c.GetInt("page")
  102. if page < 1 {
  103. page = 1
  104. }
  105. page_z, _ := c.GetInt("page_z")
  106. if page_z < 1 {
  107. page_z = conf.Page_size
  108. }
  109. // 查询
  110. T_depot_id, _ := c.GetInt("T_depot_id")
  111. // 查询
  112. T_product_name := c.GetString("T_product_name")
  113. T_product_model := c.GetString("T_product_model")
  114. T_product_class, _ := c.GetInt("T_product_class")
  115. userList, _ := NatsServer.Read_User_List_All()
  116. Account.Read_User_All_Map(userList)
  117. Basic.Read_Depot_All_Map()
  118. StockDao := Stock.NewStock(orm.NewOrm())
  119. R_List, R_cnt := StockDao.Read_Stock_List(T_depot_id, T_product_class, T_product_name, T_product_model, page, page_z)
  120. var r_jsons lib.R_JSONS
  121. r_jsons.Num = R_cnt
  122. r_jsons.Data = R_List
  123. r_jsons.Page = page
  124. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  125. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  126. c.ServeJSON()
  127. return
  128. }
  129. // 修改库存排序
  130. func (c *StockController) Stock_Edit_Sort() {
  131. T_id, _ := c.GetInt("T_id")
  132. T_sort, _ := c.GetInt("T_sort")
  133. StockDao := Stock.NewStock(orm.NewOrm())
  134. Stock_r, err := StockDao.Read_Stock_ById(T_id)
  135. if err != nil {
  136. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  137. c.ServeJSON()
  138. return
  139. }
  140. Stock_r.T_sort = T_sort
  141. if err = StockDao.Update_Stock(Stock_r, "T_sort"); err != nil {
  142. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  143. c.ServeJSON()
  144. return
  145. }
  146. NatsServer.AddUserLogs(c.User.T_uuid, "库存", "修改", Stock_r)
  147. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  148. c.ServeJSON()
  149. return
  150. }
  151. func (c *StockController) Stock_Detail_List() {
  152. // 查询
  153. T_depot_id, _ := c.GetInt("T_depot_id")
  154. // 查询
  155. T_product_id, _ := c.GetInt("T_product_id")
  156. T_start_date := c.GetString("T_start_date")
  157. T_end_date := c.GetString("T_end_date")
  158. now := time.Now()
  159. if len(T_start_date) == 0 {
  160. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  161. } else {
  162. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  163. T_start_date = T_start_date_t.Format("2006-01")
  164. }
  165. if len(T_end_date) == 0 {
  166. T_end_date = now.Format("2006-01")
  167. } else {
  168. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  169. T_end_date = T_end_date_t.Format("2006-01")
  170. }
  171. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  172. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  173. R_List := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, 0, T_start_date, T_end_date)
  174. for i := 0; i < len(R_List); i++ {
  175. 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), "|")
  176. }
  177. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_List}
  178. c.ServeJSON()
  179. return
  180. }
  181. func (c *StockController) Stock_Detail_Excel() {
  182. // 查询
  183. T_depot_id, _ := c.GetInt("T_depot_id")
  184. T_product_id, _ := c.GetInt("T_product_id")
  185. // 查询
  186. T_start_date := c.GetString("T_start_date")
  187. T_end_date := c.GetString("T_end_date")
  188. now := time.Now()
  189. if len(T_start_date) == 0 {
  190. T_start_date = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local).Format("2006-01")
  191. } else {
  192. T_start_date_t, _ := lib.DateStrToTime(T_start_date)
  193. T_start_date = T_start_date_t.Format("2006-01")
  194. }
  195. if len(T_end_date) == 0 {
  196. T_end_date = now.Format("2006-01")
  197. } else {
  198. T_end_date_t, _ := lib.DateStrToTime(T_end_date)
  199. T_end_date = T_end_date_t.Format("2006-01")
  200. }
  201. var class_List []Basic.ProductClass_R
  202. filename := fmt.Sprintf("进销存(%s)", lib.GetRandstring(6, "0123456789", 0))
  203. if T_product_id > 0 {
  204. product, _ := Basic.Read_Product_ById(T_product_id)
  205. filename = fmt.Sprintf("进销存-%s明细(%s)", product.T_name, lib.GetRandstring(6, "0123456789", 0))
  206. class, _ := Basic.Read_ProductClass_ById(product.T_class)
  207. class_List = append(class_List, Basic.ProductClassToProductClass_R(class))
  208. } else {
  209. class_List, _ = Basic.Read_ProductClass_List("", 0, 9999)
  210. }
  211. f := excelize.NewFile() // 设置单元格的值
  212. Style1, _ := f.NewStyle(
  213. &excelize.Style{
  214. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  215. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  216. })
  217. Style2, _ := f.NewStyle(
  218. &excelize.Style{
  219. Font: &excelize.Font{Bold: true, Size: 10, Family: "宋体"},
  220. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  221. Border: []excelize.Border{
  222. {Type: "left", Color: "000000", Style: 1},
  223. {Type: "top", Color: "000000", Style: 1},
  224. {Type: "bottom", Color: "000000", Style: 1},
  225. {Type: "right", Color: "000000", Style: 1},
  226. },
  227. })
  228. StockMonthDao := Stock.NewStockMonth(orm.NewOrm())
  229. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  230. var j = 0
  231. for _, r := range class_List {
  232. StockList := StockMonthDao.Read_StockMonth_List(T_depot_id, T_product_id, r.Id, T_start_date, T_end_date)
  233. if len(StockList) == 0 {
  234. continue
  235. }
  236. if j == 0 {
  237. f.SetSheetName("Sheet1", r.T_name)
  238. } else {
  239. f.NewSheet(r.T_name)
  240. }
  241. j += 1
  242. f.MergeCell(r.T_name, "A1", "J1")
  243. f.SetRowStyle(r.T_name, 1, 1, Style1)
  244. f.SetCellValue(r.T_name, "A1", fmt.Sprintf("宝智达科技产品进销存统计表"))
  245. f.SetRowHeight(r.T_name, 1, 30)
  246. // 这里设置表头
  247. f.SetCellStyle(r.T_name, "A2", "J2", Style2)
  248. f.SetRowHeight(r.T_name, 2, 20)
  249. f.SetCellValue(r.T_name, "A2", "序号")
  250. f.SetCellValue(r.T_name, "B2", "产品名称")
  251. f.SetCellValue(r.T_name, "C2", "型号")
  252. f.SetCellValue(r.T_name, "D2", "规格")
  253. f.SetCellValue(r.T_name, "E2", "月份")
  254. f.SetCellValue(r.T_name, "F2", "期初库存")
  255. f.SetCellValue(r.T_name, "G2", "当月入库")
  256. f.SetCellValue(r.T_name, "H2", "当月出库")
  257. f.SetCellValue(r.T_name, "I2", "期末库存")
  258. f.SetCellValue(r.T_name, "J2", "出库项目")
  259. //f.SetCellValue(r.T_name, "K2", "备注")
  260. // 设置列宽
  261. f.SetColWidth(r.T_name, "A", "A", 10)
  262. f.SetColWidth(r.T_name, "B", "B", 10)
  263. f.SetColWidth(r.T_name, "C", "D", 10)
  264. f.SetColWidth(r.T_name, "D", "D", 10)
  265. f.SetColWidth(r.T_name, "E", "E", 10)
  266. f.SetColWidth(r.T_name, "F", "F", 10)
  267. f.SetColWidth(r.T_name, "G", "G", 10)
  268. f.SetColWidth(r.T_name, "H", "H", 10)
  269. f.SetColWidth(r.T_name, "I", "I", 10)
  270. f.SetColWidth(r.T_name, "J", "J", 70)
  271. //f.SetColWidth(r.T_name, "K", "K", 10)
  272. line := 2
  273. // 循环写入数据
  274. for _, v := range StockList {
  275. line++
  276. product, _ := Basic.Read_Product_ById(v.T_product_id)
  277. f.SetCellValue(r.T_name, fmt.Sprintf("A%d", line), line-2)
  278. f.SetCellValue(r.T_name, fmt.Sprintf("B%d", line), product.T_name)
  279. f.SetCellValue(r.T_name, fmt.Sprintf("C%d", line), product.T_model)
  280. f.SetCellValue(r.T_name, fmt.Sprintf("D%d", line), product.T_spec)
  281. f.SetCellValue(r.T_name, fmt.Sprintf("E%d", line), v.T_month)
  282. f.SetCellValue(r.T_name, fmt.Sprintf("F%d", line), v.T_beginning)
  283. f.SetCellValue(r.T_name, fmt.Sprintf("G%d", line), v.T_in)
  284. f.SetCellValue(r.T_name, fmt.Sprintf("H%d", line), v.T_out)
  285. f.SetCellValue(r.T_name, fmt.Sprintf("I%d", line), v.T_ending)
  286. T_project := StockOutDao.Read_StockOut_T_project(T_depot_id, product.Id, v.T_month)
  287. //f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), v.T_project)
  288. f.SetCellValue(r.T_name, fmt.Sprintf("J%d", line), strings.Join(T_project, "\r\n"))
  289. }
  290. Style4, _ := f.NewStyle(
  291. &excelize.Style{
  292. Font: &excelize.Font{Size: 10, Family: "宋体"},
  293. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  294. Border: []excelize.Border{
  295. {Type: "left", Color: "000000", Style: 1},
  296. {Type: "top", Color: "000000", Style: 1},
  297. {Type: "bottom", Color: "000000", Style: 1},
  298. {Type: "right", Color: "000000", Style: 1},
  299. },
  300. })
  301. f.SetCellStyle(r.T_name, "A2", fmt.Sprintf("I%d", line), Style4)
  302. Style5, _ := f.NewStyle(
  303. &excelize.Style{
  304. Font: &excelize.Font{Size: 10, Family: "宋体"},
  305. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  306. Border: []excelize.Border{
  307. {Type: "left", Color: "000000", Style: 1},
  308. {Type: "top", Color: "000000", Style: 1},
  309. {Type: "bottom", Color: "000000", Style: 1},
  310. {Type: "right", Color: "000000", Style: 1},
  311. },
  312. })
  313. f.SetCellStyle(r.T_name, "J2", fmt.Sprintf("J%d", line), Style5)
  314. }
  315. // 保存文件
  316. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  317. fmt.Println(err)
  318. }
  319. var url string
  320. //// 上传 OSS
  321. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  322. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  323. if !is {
  324. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  325. c.ServeJSON()
  326. return
  327. }
  328. //删除目录
  329. err := os.Remove("ofile/" + filename + ".xlsx")
  330. if err != nil {
  331. logs.Error(lib.FuncName(), err)
  332. }
  333. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  334. c.ServeJSON()
  335. return
  336. }
  337. func (c *StockController) StockIn_List() {
  338. // 分页参数 初始化
  339. page, _ := c.GetInt("page")
  340. if page < 1 {
  341. page = 1
  342. }
  343. page_z, _ := c.GetInt("page_z")
  344. if page_z < 1 {
  345. page_z = conf.Page_size
  346. }
  347. // 查询
  348. T_depot_id, _ := c.GetInt("T_depot_id")
  349. T_start_date := c.GetString("T_start_date")
  350. T_end_date := c.GetString("T_end_date")
  351. userList, _ := NatsServer.Read_User_List_All()
  352. Account.Read_User_All_Map(userList)
  353. Basic.Read_Depot_All_Map()
  354. StockInDao := Stock.NewStockIn(orm.NewOrm())
  355. R_List, R_cnt := StockInDao.Read_StockIn_List(T_depot_id, T_start_date, T_end_date, page, page_z)
  356. var r_jsons lib.R_JSONS
  357. r_jsons.Num = R_cnt
  358. r_jsons.Data = R_List
  359. r_jsons.Page = page
  360. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  361. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  362. c.ServeJSON()
  363. return
  364. }
  365. func (c *StockController) StockIn_List_Product() {
  366. // 分页参数 初始化
  367. page, _ := c.GetInt("page")
  368. if page < 1 {
  369. page = 1
  370. }
  371. page_z, _ := c.GetInt("page_z")
  372. if page_z < 1 {
  373. page_z = conf.Page_size
  374. }
  375. // 查询
  376. T_depot_id, _ := c.GetInt("T_depot_id")
  377. T_name := c.GetString("T_name")
  378. T_start_date := c.GetString("T_start_date")
  379. T_end_date := c.GetString("T_end_date")
  380. T_product_name := c.GetString("T_product_name") // 产品名称
  381. T_product_model := c.GetString("T_product_model") // 产品型号
  382. userList, _ := NatsServer.Read_User_List_All()
  383. Account.Read_User_All_Map(userList)
  384. Basic.Read_Depot_All_Map()
  385. StockInDao := Stock.NewStockIn(orm.NewOrm())
  386. 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)
  387. var r_jsons lib.R_JSONS
  388. r_jsons.Num = R_cnt
  389. r_jsons.Data = R_List
  390. r_jsons.Page = page
  391. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  392. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  393. c.ServeJSON()
  394. return
  395. }
  396. func (c *StockController) StockIn_List_Product_Excel() {
  397. // 查询
  398. T_depot_id, _ := c.GetInt("T_depot_id")
  399. T_name := c.GetString("T_name")
  400. T_start_date := c.GetString("T_start_date")
  401. T_end_date := c.GetString("T_end_date")
  402. T_product_name := c.GetString("T_product_name") // 产品名称
  403. T_product_model := c.GetString("T_product_model") // 产品型号
  404. userList, _ := NatsServer.Read_User_List_All()
  405. Account.Read_User_All_Map(userList)
  406. Basic.Read_Depot_All_Map()
  407. StockInDao := Stock.NewStockIn(orm.NewOrm())
  408. 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)
  409. filename := fmt.Sprintf("入库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  410. f := excelize.NewFile() // 设置单元格的值
  411. Style1, _ := f.NewStyle(
  412. &excelize.Style{
  413. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  414. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  415. })
  416. Style2, _ := f.NewStyle(
  417. &excelize.Style{
  418. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  419. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  420. Border: []excelize.Border{
  421. {Type: "left", Color: "000000", Style: 1},
  422. {Type: "top", Color: "000000", Style: 1},
  423. {Type: "bottom", Color: "000000", Style: 1},
  424. {Type: "right", Color: "000000", Style: 1},
  425. },
  426. })
  427. Style3, _ := f.NewStyle(
  428. &excelize.Style{
  429. Font: &excelize.Font{Size: 11, Family: "宋体"},
  430. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  431. Border: []excelize.Border{
  432. {Type: "left", Color: "000000", Style: 1},
  433. {Type: "top", Color: "000000", Style: 1},
  434. {Type: "bottom", Color: "000000", Style: 1},
  435. {Type: "right", Color: "000000", Style: 1},
  436. },
  437. })
  438. f.MergeCell("Sheet1", "A1", "K1")
  439. f.SetRowStyle("Sheet1", 1, 1, Style1)
  440. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("入库明细单"))
  441. f.SetRowHeight("Sheet1", 1, 30)
  442. // 这里设置表头
  443. f.SetCellStyle("Sheet1", "A2", "K2", Style2)
  444. f.SetRowHeight("Sheet1", 2, 20)
  445. f.SetCellValue("Sheet1", "A2", "序号")
  446. f.SetCellValue("Sheet1", "B2", "入库单号")
  447. f.SetCellValue("Sheet1", "C2", "经办人")
  448. f.SetCellValue("Sheet1", "D2", "入库日期")
  449. f.SetCellValue("Sheet1", "E2", "入库仓库")
  450. f.SetCellValue("Sheet1", "F2", "产品名称")
  451. f.SetCellValue("Sheet1", "G2", "产品型号")
  452. f.SetCellValue("Sheet1", "H2", "数量")
  453. f.SetCellValue("Sheet1", "I2", "是否关联SN")
  454. f.SetCellValue("Sheet1", "J2", "备注")
  455. f.SetCellValue("Sheet1", "K2", "SN")
  456. // 设置列宽
  457. f.SetColWidth("Sheet1", "A", "A", 10)
  458. f.SetColWidth("Sheet1", "B", "B", 15)
  459. f.SetColWidth("Sheet1", "C", "D", 10)
  460. f.SetColWidth("Sheet1", "D", "D", 10)
  461. f.SetColWidth("Sheet1", "E", "E", 10)
  462. f.SetColWidth("Sheet1", "F", "F", 20)
  463. f.SetColWidth("Sheet1", "G", "G", 15)
  464. f.SetColWidth("Sheet1", "H", "H", 10)
  465. f.SetColWidth("Sheet1", "I", "I", 12)
  466. f.SetColWidth("Sheet1", "J", "J", 30)
  467. f.SetColWidth("Sheet1", "K", "K", 100)
  468. line := 2
  469. for _, product := range R_List {
  470. line += 1
  471. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  472. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  473. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_submit_name)
  474. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  475. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  476. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), product.T_product_name)
  477. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_model)
  478. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_num)
  479. var relation_sn string
  480. if product.T_product_relation_sn == 1 {
  481. relation_sn = "是"
  482. } else {
  483. relation_sn = "否"
  484. }
  485. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), relation_sn)
  486. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), product.T_remark)
  487. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_relation_sn)
  488. }
  489. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("K%d", line), Style3)
  490. // 保存文件
  491. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  492. fmt.Println(err)
  493. }
  494. var url string
  495. //// 上传 OSS
  496. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  497. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  498. if !is {
  499. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  500. c.ServeJSON()
  501. return
  502. }
  503. //删除目录
  504. err := os.Remove("ofile/" + filename + ".xlsx")
  505. if err != nil {
  506. logs.Error(lib.FuncName(), err)
  507. }
  508. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  509. c.ServeJSON()
  510. return
  511. }
  512. func (c *StockController) StockIn_Get() {
  513. // 查询
  514. T_number := c.GetString("T_number")
  515. o := orm.NewOrm()
  516. StockInDao := Stock.NewStockIn(o)
  517. StockInProductDao := Stock.NewStockInProduct(o)
  518. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  519. if err != nil {
  520. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  521. c.ServeJSON()
  522. return
  523. }
  524. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  525. var pList []Stock.StockInProduct_R
  526. for _, v := range productList {
  527. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  528. }
  529. userList, _ := NatsServer.Read_User_List_All()
  530. Account.Read_User_All_Map(userList)
  531. Basic.Read_Depot_All_Map()
  532. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockInToStockIn_Detail(stockIn, pList)}
  533. c.ServeJSON()
  534. return
  535. }
  536. func (c *StockController) StockIn_Add() {
  537. rand_x := 0
  538. T_number := ""
  539. o := orm.NewOrm()
  540. o.Begin()
  541. StockInDao := Stock.NewStockIn(o)
  542. for true {
  543. T_number = "RK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  544. _, err := StockInDao.Read_StockIn_ByT_number(T_number)
  545. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  546. break
  547. }
  548. rand_x += 1
  549. }
  550. T_depot_id, _ := c.GetInt("T_depot_id")
  551. T_date := c.GetString("T_date")
  552. T_product := c.GetString("T_product")
  553. T_remark := c.GetString("T_remark")
  554. _, is := lib.DateStrToTime(T_date)
  555. if !is {
  556. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  557. c.ServeJSON()
  558. return
  559. }
  560. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "入库", T_product)
  561. var_ := Stock.StockIn{
  562. T_number: T_number,
  563. T_depot_id: T_depot_id,
  564. T_date: T_date,
  565. T_remark: T_remark,
  566. T_submit: c.User.T_uuid,
  567. }
  568. StockInProductDao := Stock.NewStockInProduct(o)
  569. StockDao := Stock.NewStock(o)
  570. DeviceDao := Stock.NewDevice(o)
  571. IotCardDao := Property.NewIotCard(&o)
  572. _, err := StockInDao.Add_StockIn(var_)
  573. if err != nil {
  574. o.Rollback()
  575. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  576. c.ServeJSON()
  577. return
  578. }
  579. if len(T_product) == 0 {
  580. o.Rollback()
  581. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  582. c.ServeJSON()
  583. return
  584. }
  585. productList := lib.SplitString(T_product, "|")
  586. allProductList := []int{}
  587. for _, v := range productList {
  588. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  589. allProductList = append(allProductList, product_id)
  590. product, _ := Basic.Read_Product_ById(product_id)
  591. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  592. T_relation_sn := strings.Split(v, "-")[2]
  593. if T_relation_sn == "" && product.T_relation_sn == 1 {
  594. o.Rollback()
  595. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  596. c.ServeJSON()
  597. return
  598. }
  599. if len(T_relation_sn) > 0 {
  600. snList := strings.Split(strings.Trim(T_relation_sn, ","), ",")
  601. for _, sn := range snList {
  602. mqtt := Stock.Read_MqttUser(sn)
  603. // 添加设备
  604. device := Stock.Device{
  605. T_contract_number: "",
  606. T_out_number: "",
  607. T_product_id: product_id,
  608. T_in_number: T_number,
  609. T_sn: sn,
  610. T_iccid: mqtt.Iccid,
  611. T_imei: mqtt.Imei,
  612. T_State: 2,
  613. }
  614. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  615. if err != nil {
  616. o.Rollback()
  617. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  618. c.ServeJSON()
  619. return
  620. }
  621. // 添加物联网卡
  622. iotCard := Property.IotCard{
  623. T_iccid: mqtt.Iccid,
  624. T_sn: sn,
  625. T_State: 2, //1-未使用 2-已使用 3-已作废
  626. T_type: "4G",
  627. }
  628. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  629. if err != nil {
  630. o.Rollback()
  631. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  632. c.ServeJSON()
  633. return
  634. }
  635. }
  636. }
  637. stockInProduct := Stock.StockInProduct{
  638. T_number: T_number,
  639. T_product_id: product_id,
  640. T_depot_id: T_depot_id,
  641. T_num: num, // 入库数量
  642. T_date: T_date, // 入库日期
  643. T_relation_sn: T_relation_sn,
  644. }
  645. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  646. if err != nil {
  647. o.Rollback()
  648. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  649. c.ServeJSON()
  650. return
  651. }
  652. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  653. if err != nil {
  654. o.Rollback()
  655. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  656. c.ServeJSON()
  657. return
  658. }
  659. }
  660. o.Commit()
  661. StockIn_Edit_StockMonth(T_date, T_depot_id, allProductList)
  662. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "入库", var_)
  663. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  664. c.ServeJSON()
  665. return
  666. }
  667. func StockIn_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  668. date, _ := lib.DateStrToTime(T_date)
  669. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  670. // // 修改本月数据不同步库存
  671. // return nil
  672. //}
  673. //T_month := date.Format("2006-01")
  674. months := generateMonthList(date)
  675. o := orm.NewOrm()
  676. StockOutDao := Stock.NewStockOut(o)
  677. StockMonthDao := Stock.NewStockMonth(o)
  678. StockOutProductDao := Stock.NewStockOutProduct(o)
  679. StockInProductDao := Stock.NewStockInProduct(o)
  680. for _, T_product_id := range allProductList {
  681. for _, T_month := range months {
  682. // 获取当前产品本月出入库数量
  683. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  684. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  685. if stockMonth.Id > 0 {
  686. // 修改
  687. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  688. if len(lastMonth) > 0 {
  689. stockMonth.T_beginning = lastMonth[0].T_ending
  690. } else {
  691. stockMonth.T_beginning = 0
  692. }
  693. // 期末库存 = 期初库存+入库-出库
  694. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  695. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_beginning", "T_ending")
  696. if err != nil {
  697. return err
  698. }
  699. } else {
  700. product, _ := Basic.Read_Product_ById(T_product_id)
  701. // 添加库存记录
  702. stockMonth = Stock.StockMonth{
  703. T_depot_id: T_depot_id,
  704. T_product_id: T_product_id,
  705. T_product_class: product.T_class,
  706. T_month: T_month,
  707. }
  708. // 获取当前产品本月出入库数量
  709. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  710. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  711. if len(lastMonth) > 0 {
  712. stockMonth.T_beginning = lastMonth[0].T_ending
  713. } else {
  714. stockMonth.T_beginning = 0
  715. }
  716. // 期末库存 = 期初库存+入库-出库
  717. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  718. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  719. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  720. if err != nil {
  721. return err
  722. }
  723. }
  724. }
  725. }
  726. return nil
  727. }
  728. func (c *StockController) StockIn_Edit() {
  729. T_number := c.GetString("T_number") // 入库单号
  730. //T_depot_id, _ := c.GetInt("T_depot_id")
  731. T_date := c.GetString("T_date")
  732. T_product := c.GetString("T_product")
  733. T_remark := c.GetString("T_remark")
  734. date, is := lib.DateStrToTime(T_date)
  735. if !is {
  736. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  737. c.ServeJSON()
  738. return
  739. }
  740. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  741. T_date = ""
  742. }
  743. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改入库", T_product)
  744. o := orm.NewOrm()
  745. o.Begin()
  746. StockInDao := Stock.NewStockIn(o)
  747. StockInProductDao := Stock.NewStockInProduct(o)
  748. StockDao := Stock.NewStock(o)
  749. DeviceDao := Stock.NewDevice(o)
  750. IotCardDao := Property.NewIotCard(&o)
  751. // 查询入库信息
  752. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  753. T_old_date := stockIn.T_date
  754. if err != nil {
  755. o.Rollback()
  756. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  757. c.ServeJSON()
  758. return
  759. }
  760. // 查询入库产品信息
  761. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  762. var oldProductList []dto.StockProduct
  763. var newProductList []dto.StockProduct
  764. allProductListMap := make(map[int]struct{})
  765. productOldMap := map[int]dto.StockProduct{}
  766. for _, product := range productOldList {
  767. stockProduct := dto.StockProduct{
  768. T_product_id: product.T_product_id,
  769. T_num: product.T_num,
  770. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  771. }
  772. oldProductList = append(oldProductList, stockProduct)
  773. productOldMap[product.T_product_id] = stockProduct
  774. if _, ok := allProductListMap[product.T_product_id]; !ok {
  775. allProductListMap[product.T_product_id] = struct{}{}
  776. }
  777. }
  778. productNewList := lib.SplitString(T_product, "|")
  779. productNewMap := map[int]dto.StockProduct{}
  780. for _, v := range productNewList {
  781. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  782. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  783. T_relation_sn := strings.Split(v, "-")[2]
  784. stockProduct := dto.StockProduct{
  785. T_product_id: product_id,
  786. T_num: num,
  787. T_relation_sn: lib.SplitString(T_relation_sn, ","),
  788. }
  789. newProductList = append(newProductList, stockProduct)
  790. productNewMap[product_id] = stockProduct
  791. if _, ok := allProductListMap[product_id]; !ok {
  792. allProductListMap[product_id] = struct{}{}
  793. }
  794. }
  795. // 判断产品列表信息是否相同
  796. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  797. // 两次提交的数据相同,则不用修改
  798. if !StockProductListIsEqual {
  799. if len(needDelete) > 0 {
  800. // 删除入库产品列表
  801. for _, v := range needDelete {
  802. product, _ := Basic.Read_Product_ById(v.T_product_id)
  803. // 删除入库产品列表
  804. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  805. if err != nil {
  806. o.Rollback()
  807. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  808. c.ServeJSON()
  809. return
  810. }
  811. if product.T_relation_sn == 1 {
  812. // 删除该入库单关联sn
  813. for _, sn := range v.T_relation_sn {
  814. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  815. if err != nil {
  816. o.Rollback()
  817. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  818. c.ServeJSON()
  819. return
  820. }
  821. }
  822. }
  823. // 减少库存
  824. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  825. if err != nil {
  826. o.Rollback()
  827. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  828. c.ServeJSON()
  829. return
  830. }
  831. }
  832. }
  833. if len(needAdd) > 0 {
  834. // 新增入库产品列表
  835. for _, v := range needAdd {
  836. product, _ := Basic.Read_Product_ById(v.T_product_id)
  837. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  838. o.Rollback()
  839. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  840. c.ServeJSON()
  841. return
  842. }
  843. // 添加该入库单关联sn
  844. for _, sn := range v.T_relation_sn {
  845. mqtt := Stock.Read_MqttUser(sn)
  846. // 添加设备
  847. device := Stock.Device{
  848. T_contract_number: "",
  849. T_out_number: "",
  850. T_product_id: v.T_product_id,
  851. T_in_number: stockIn.T_number,
  852. T_sn: sn,
  853. T_iccid: mqtt.Iccid,
  854. T_imei: mqtt.Imei,
  855. T_State: 2,
  856. CreateTime: stockIn.CreateTime,
  857. UpdateTime: stockIn.UpdateTime,
  858. }
  859. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  860. if err != nil {
  861. o.Rollback()
  862. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  863. c.ServeJSON()
  864. return
  865. }
  866. // 添加物联网卡
  867. iotCard := Property.IotCard{
  868. T_iccid: mqtt.Iccid,
  869. T_sn: sn,
  870. T_State: 2, //1-未使用 2-已使用 3-已作废
  871. T_type: "4G",
  872. }
  873. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  874. if err != nil {
  875. o.Rollback()
  876. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  877. c.ServeJSON()
  878. return
  879. }
  880. }
  881. stockInProduct := Stock.StockInProduct{
  882. T_number: T_number,
  883. T_product_id: v.T_product_id,
  884. T_depot_id: stockIn.T_depot_id,
  885. T_num: v.T_num, // 入库数量
  886. T_date: stockIn.T_date, // 入库日期
  887. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  888. }
  889. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  890. if err != nil {
  891. o.Rollback()
  892. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  893. c.ServeJSON()
  894. return
  895. }
  896. // 增加库存
  897. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  898. if err != nil {
  899. o.Rollback()
  900. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  901. c.ServeJSON()
  902. return
  903. }
  904. }
  905. }
  906. if len(needEdit) > 0 {
  907. for _, v := range needEdit {
  908. product, _ := Basic.Read_Product_ById(v.T_product_id)
  909. stockInProduct := Stock.StockInProduct{
  910. T_number: stockIn.T_number,
  911. T_product_id: v.T_product_id,
  912. T_depot_id: stockIn.T_depot_id,
  913. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  914. T_date: stockIn.T_date, // 入库日期
  915. }
  916. // 入库数量比之前多,增加库存
  917. var T_type int
  918. var T_num int
  919. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  920. // 增加库存
  921. T_type = 2
  922. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  923. } else {
  924. // 减少库存
  925. T_type = 1
  926. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  927. }
  928. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  929. if err != nil {
  930. o.Rollback()
  931. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  932. c.ServeJSON()
  933. return
  934. }
  935. // 更新产品库存表
  936. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  937. if err != nil {
  938. o.Rollback()
  939. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  940. c.ServeJSON()
  941. return
  942. }
  943. }
  944. }
  945. for _, diff := range snDiff {
  946. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  947. // 删除减少的sn
  948. if len(diff.T_delete_relation_sn) > 0 {
  949. for _, sn := range diff.T_delete_relation_sn {
  950. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  951. if err != nil {
  952. o.Rollback()
  953. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  954. c.ServeJSON()
  955. return
  956. }
  957. }
  958. // 减少库存
  959. _, 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)
  960. if err != nil {
  961. o.Rollback()
  962. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  963. c.ServeJSON()
  964. return
  965. }
  966. }
  967. if len(diff.T_add_relation_sn) > 0 {
  968. for _, sn := range diff.T_add_relation_sn {
  969. mqtt := Stock.Read_MqttUser(sn)
  970. // 添加设备
  971. device := Stock.Device{
  972. T_contract_number: "",
  973. T_out_number: "",
  974. T_product_id: diff.T_product_id,
  975. T_in_number: stockIn.T_number,
  976. T_sn: sn,
  977. T_iccid: mqtt.Iccid,
  978. T_imei: mqtt.Imei,
  979. T_State: 2,
  980. CreateTime: stockIn.CreateTime,
  981. UpdateTime: stockIn.UpdateTime,
  982. }
  983. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  984. if err != nil {
  985. o.Rollback()
  986. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  987. c.ServeJSON()
  988. return
  989. }
  990. // 添加物联网卡
  991. iotCard := Property.IotCard{
  992. T_iccid: mqtt.Iccid,
  993. T_sn: sn,
  994. T_State: 2, //1-未使用 2-已使用 3-已作废
  995. T_type: "4G",
  996. }
  997. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  998. if err != nil {
  999. o.Rollback()
  1000. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  1001. c.ServeJSON()
  1002. return
  1003. }
  1004. }
  1005. // 增加库存
  1006. _, 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)
  1007. if err != nil {
  1008. o.Rollback()
  1009. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1010. c.ServeJSON()
  1011. return
  1012. }
  1013. }
  1014. stockInProduct := Stock.StockInProduct{
  1015. T_number: stockIn.T_number,
  1016. T_product_id: diff.T_product_id,
  1017. T_depot_id: stockIn.T_depot_id,
  1018. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  1019. T_date: stockIn.T_date, // 入库日期
  1020. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  1021. }
  1022. // 更新产品库存表
  1023. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  1024. if err != nil {
  1025. o.Rollback()
  1026. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  1027. c.ServeJSON()
  1028. return
  1029. }
  1030. }
  1031. }
  1032. if len(T_remark) > 0 {
  1033. stockIn.T_remark = T_remark
  1034. }
  1035. if len(T_date) > 0 {
  1036. stockIn.T_date = T_date
  1037. }
  1038. err = StockInDao.Update_StockIn(stockIn, "T_remark", "T_date")
  1039. if err != nil {
  1040. o.Rollback()
  1041. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  1042. c.ServeJSON()
  1043. return
  1044. }
  1045. o.Commit()
  1046. if len(T_date) > 0 && T_old_date != T_date {
  1047. // 修改出库产品日期
  1048. StockInProductDao.Update_StockInProduct_T_date(stockIn.T_number, T_date)
  1049. DeviceDao.Update_Device_CreateTimeByT_in_number(stockIn.T_number, T_date)
  1050. }
  1051. // 更新月份统计表
  1052. allProductList := []int{}
  1053. for i, _ := range allProductListMap {
  1054. allProductList = append(allProductList, i)
  1055. }
  1056. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1057. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "修改", stockIn)
  1058. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1059. c.ServeJSON()
  1060. return
  1061. }
  1062. // 删除入库
  1063. func (c *StockController) StockIn_Del() {
  1064. T_number := c.GetString("T_number") // 入库单号
  1065. o := orm.NewOrm()
  1066. o.Begin()
  1067. StockInDao := Stock.NewStockIn(o)
  1068. StockInProductDao := Stock.NewStockInProduct(o)
  1069. StockDao := Stock.NewStock(o)
  1070. DeviceDao := Stock.NewDevice(o)
  1071. // 查询入库信息
  1072. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1073. if err != nil {
  1074. o.Rollback()
  1075. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1076. c.ServeJSON()
  1077. return
  1078. }
  1079. // 查询入库产品信息
  1080. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1081. var oldProductList []dto.StockProduct
  1082. allProductList := []int{}
  1083. for _, product := range productOldList {
  1084. oldProductList = append(oldProductList, dto.StockProduct{
  1085. T_product_id: product.T_product_id,
  1086. T_num: product.T_num,
  1087. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1088. })
  1089. allProductList = append(allProductList, product.T_product_id)
  1090. }
  1091. // 删除入库产品列表
  1092. for _, v := range oldProductList {
  1093. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1094. // 删除入库产品列表
  1095. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  1096. if err != nil {
  1097. o.Rollback()
  1098. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  1099. c.ServeJSON()
  1100. return
  1101. }
  1102. if product.T_relation_sn == 1 {
  1103. // 删除该入库单关联sn
  1104. for _, sn := range v.T_relation_sn {
  1105. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  1106. if err != nil {
  1107. o.Rollback()
  1108. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  1109. c.ServeJSON()
  1110. return
  1111. }
  1112. }
  1113. }
  1114. // 减少库存
  1115. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  1116. if err != nil {
  1117. o.Rollback()
  1118. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1119. c.ServeJSON()
  1120. return
  1121. }
  1122. }
  1123. err = StockInDao.Delete_StockIn(stockIn)
  1124. if err != nil {
  1125. o.Rollback()
  1126. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库失败"}
  1127. c.ServeJSON()
  1128. return
  1129. }
  1130. o.Commit()
  1131. // 更新月份统计表
  1132. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1133. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "删除", stockIn)
  1134. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1135. c.ServeJSON()
  1136. return
  1137. }
  1138. // 导出出库单
  1139. func (c *StockController) StockIn_Excel() {
  1140. // 查询
  1141. T_number := c.GetString("T_number")
  1142. o := orm.NewOrm()
  1143. StockInDao := Stock.NewStockIn(o)
  1144. StockInProductDao := Stock.NewStockInProduct(o)
  1145. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1146. if err != nil {
  1147. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1148. c.ServeJSON()
  1149. return
  1150. }
  1151. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1152. var pList []Stock.StockInProduct_R
  1153. for _, v := range productList {
  1154. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1155. }
  1156. userList, _ := NatsServer.Read_User_List_All()
  1157. Account.Read_User_All_Map(userList)
  1158. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1159. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1160. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1161. f := excelize.NewFile() // 设置单元格的值
  1162. Style1, _ := f.NewStyle(
  1163. &excelize.Style{
  1164. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1165. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1166. })
  1167. Style2, _ := f.NewStyle(
  1168. &excelize.Style{
  1169. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1170. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1171. })
  1172. Style3, _ := f.NewStyle(
  1173. &excelize.Style{
  1174. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1175. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1176. Border: []excelize.Border{
  1177. {Type: "left", Color: "000000", Style: 1},
  1178. {Type: "top", Color: "000000", Style: 1},
  1179. {Type: "bottom", Color: "000000", Style: 1},
  1180. {Type: "right", Color: "000000", Style: 1},
  1181. },
  1182. })
  1183. Style4, _ := f.NewStyle(
  1184. &excelize.Style{
  1185. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1186. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1187. Border: []excelize.Border{
  1188. {Type: "left", Color: "000000", Style: 1},
  1189. {Type: "top", Color: "000000", Style: 1},
  1190. {Type: "bottom", Color: "000000", Style: 1},
  1191. {Type: "right", Color: "000000", Style: 1},
  1192. },
  1193. })
  1194. productList2 := lib.ChunkBy(productList, 10)
  1195. for i, products := range productList2 {
  1196. if len(products) < 10 {
  1197. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1198. }
  1199. row := i*17 + 1
  1200. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1201. f.SetRowStyle("Sheet1", row, row, Style1)
  1202. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1203. f.SetRowHeight("Sheet1", row, 20)
  1204. row += 1
  1205. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1206. f.SetRowStyle("Sheet1", row, row, Style1)
  1207. title := "硬件入库单"
  1208. if len(productList2) > 1 {
  1209. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1210. }
  1211. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1212. f.SetRowHeight("Sheet1", row, 20)
  1213. row += 1
  1214. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1215. f.SetRowStyle("Sheet1", row, row, Style2)
  1216. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1217. f.SetRowHeight("Sheet1", row, 20)
  1218. row += 1
  1219. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1220. f.SetRowStyle("Sheet1", row, row, Style2)
  1221. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1222. f.SetRowHeight("Sheet1", row, 20)
  1223. row += 1
  1224. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1225. f.SetRowStyle("Sheet1", row, row, Style2)
  1226. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1227. f.SetRowHeight("Sheet1", row, 20)
  1228. // 这里设置表头
  1229. row += 1
  1230. f.SetRowHeight("Sheet1", row, 20)
  1231. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1232. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1233. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1234. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1235. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1236. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1237. // 设置列宽
  1238. f.SetColWidth("Sheet1", "A", "A", 5)
  1239. f.SetColWidth("Sheet1", "B", "B", 20)
  1240. f.SetColWidth("Sheet1", "C", "C", 12)
  1241. f.SetColWidth("Sheet1", "D", "D", 10)
  1242. f.SetColWidth("Sheet1", "E", "E", 10)
  1243. f.SetColWidth("Sheet1", "F", "F", 15)
  1244. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1245. sRow := row
  1246. // 循环写入数据
  1247. for j, v := range products {
  1248. row += 1
  1249. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1250. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1251. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1252. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1253. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1254. if v.T_num > 0 {
  1255. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1256. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1257. }
  1258. }
  1259. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1260. row += 1
  1261. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1262. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1263. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1264. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1265. f.SetRowStyle("Sheet1", row, row, Style2)
  1266. f.SetRowHeight("Sheet1", row, 20)
  1267. }
  1268. // 保存文件
  1269. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1270. fmt.Println(err)
  1271. }
  1272. var url string
  1273. //// 上传 OSS
  1274. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1275. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1276. if !is {
  1277. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1278. c.ServeJSON()
  1279. return
  1280. }
  1281. //删除目录
  1282. err = os.Remove("ofile/" + filename + ".xlsx")
  1283. if err != nil {
  1284. logs.Error(lib.FuncName(), err)
  1285. }
  1286. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1287. c.ServeJSON()
  1288. return
  1289. }
  1290. func (c *StockController) StockOut_List() {
  1291. // 分页参数 初始化
  1292. page, _ := c.GetInt("page")
  1293. if page < 1 {
  1294. page = 1
  1295. }
  1296. page_z, _ := c.GetInt("page_z")
  1297. if page_z < 1 {
  1298. page_z = conf.Page_size
  1299. }
  1300. // 查询
  1301. T_depot_id, _ := c.GetInt("T_depot_id")
  1302. T_contract_number := c.GetString("T_contract_number")
  1303. T_start_date := c.GetString("T_start_date")
  1304. T_end_date := c.GetString("T_end_date")
  1305. userList, _ := NatsServer.Read_User_List_All()
  1306. Account.Read_User_All_Map(userList)
  1307. Basic.Read_Depot_All_Map()
  1308. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1309. R_List, R_cnt := StockOutDao.Read_StockOut_List(T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  1310. var r_jsons lib.R_JSONS
  1311. r_jsons.Num = R_cnt
  1312. r_jsons.Data = R_List
  1313. r_jsons.Page = page
  1314. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1315. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1316. c.ServeJSON()
  1317. return
  1318. }
  1319. func (c *StockController) StockOut_List_Product() {
  1320. // 分页参数 初始化
  1321. page, _ := c.GetInt("page")
  1322. if page < 1 {
  1323. page = 1
  1324. }
  1325. page_z, _ := c.GetInt("page_z")
  1326. if page_z < 1 {
  1327. page_z = conf.Page_size
  1328. }
  1329. // 查询
  1330. T_depot_id, _ := c.GetInt("T_depot_id")
  1331. T_name := c.GetString("T_name")
  1332. T_start_date := c.GetString("T_start_date")
  1333. T_end_date := c.GetString("T_end_date")
  1334. T_product_name := c.GetString("T_product_name") // 产品名称
  1335. T_product_model := c.GetString("T_product_model") // 产品型号
  1336. userList, _ := NatsServer.Read_User_List_All()
  1337. Account.Read_User_All_Map(userList)
  1338. Basic.Read_Depot_All_Map()
  1339. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1340. 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)
  1341. var r_jsons lib.R_JSONS
  1342. r_jsons.Num = R_cnt
  1343. r_jsons.Data = R_List
  1344. r_jsons.Page = page
  1345. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1346. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1347. c.ServeJSON()
  1348. return
  1349. }
  1350. func (c *StockController) StockOut_List_Product_Excel() {
  1351. // 查询
  1352. T_depot_id, _ := c.GetInt("T_depot_id")
  1353. T_name := c.GetString("T_name")
  1354. T_start_date := c.GetString("T_start_date")
  1355. T_end_date := c.GetString("T_end_date")
  1356. T_product_name := c.GetString("T_product_name") // 产品名称
  1357. T_product_model := c.GetString("T_product_model") // 产品型号
  1358. userList, _ := NatsServer.Read_User_List_All()
  1359. Account.Read_User_All_Map(userList)
  1360. Basic.Read_Depot_All_Map()
  1361. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1362. 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)
  1363. filename := fmt.Sprintf("出库明细单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1364. f := excelize.NewFile() // 设置单元格的值
  1365. Style1, _ := f.NewStyle(
  1366. &excelize.Style{
  1367. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1368. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1369. })
  1370. Style2, _ := f.NewStyle(
  1371. &excelize.Style{
  1372. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  1373. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1374. Border: []excelize.Border{
  1375. {Type: "left", Color: "000000", Style: 1},
  1376. {Type: "top", Color: "000000", Style: 1},
  1377. {Type: "bottom", Color: "000000", Style: 1},
  1378. {Type: "right", Color: "000000", Style: 1},
  1379. },
  1380. })
  1381. Style3, _ := f.NewStyle(
  1382. &excelize.Style{
  1383. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1384. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  1385. Border: []excelize.Border{
  1386. {Type: "left", Color: "000000", Style: 1},
  1387. {Type: "top", Color: "000000", Style: 1},
  1388. {Type: "bottom", Color: "000000", Style: 1},
  1389. {Type: "right", Color: "000000", Style: 1},
  1390. },
  1391. })
  1392. f.MergeCell("Sheet1", "A1", "L1")
  1393. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1394. f.SetCellValue("Sheet1", "A1", fmt.Sprintf("出库明细单"))
  1395. f.SetRowHeight("Sheet1", 1, 30)
  1396. // 这里设置表头
  1397. f.SetCellStyle("Sheet1", "A2", "L2", Style2)
  1398. f.SetRowHeight("Sheet1", 2, 20)
  1399. f.SetCellValue("Sheet1", "A2", "序号")
  1400. f.SetCellValue("Sheet1", "B2", "出库单号")
  1401. f.SetCellValue("Sheet1", "C2", "领取人")
  1402. f.SetCellValue("Sheet1", "D2", "出库日期")
  1403. f.SetCellValue("Sheet1", "E2", "出库仓库")
  1404. f.SetCellValue("Sheet1", "F2", "关联项目")
  1405. f.SetCellValue("Sheet1", "G2", "产品名称")
  1406. f.SetCellValue("Sheet1", "H2", "产品型号")
  1407. f.SetCellValue("Sheet1", "I2", "数量")
  1408. f.SetCellValue("Sheet1", "J2", "是否关联SN")
  1409. f.SetCellValue("Sheet1", "K2", "备注")
  1410. f.SetCellValue("Sheet1", "L2", "SN")
  1411. // 设置列宽
  1412. f.SetColWidth("Sheet1", "A", "A", 10)
  1413. f.SetColWidth("Sheet1", "B", "B", 15)
  1414. f.SetColWidth("Sheet1", "C", "D", 10)
  1415. f.SetColWidth("Sheet1", "D", "D", 10)
  1416. f.SetColWidth("Sheet1", "E", "E", 10)
  1417. f.SetColWidth("Sheet1", "F", "F", 20)
  1418. f.SetColWidth("Sheet1", "G", "G", 20)
  1419. f.SetColWidth("Sheet1", "H", "H", 15)
  1420. f.SetColWidth("Sheet1", "I", "I", 10)
  1421. f.SetColWidth("Sheet1", "J", "J", 12)
  1422. f.SetColWidth("Sheet1", "K", "K", 30)
  1423. f.SetColWidth("Sheet1", "L", "L", 100)
  1424. line := 2
  1425. for _, product := range R_List {
  1426. line += 1
  1427. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  1428. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), product.T_number)
  1429. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), product.T_receive_name)
  1430. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), product.T_date)
  1431. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), product.T_depot_name)
  1432. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), product.T_project)
  1433. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), product.T_product_name)
  1434. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), product.T_product_model)
  1435. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), product.T_num)
  1436. var relation_sn string
  1437. if product.T_product_relation_sn == 1 {
  1438. relation_sn = "是"
  1439. } else {
  1440. relation_sn = "否"
  1441. }
  1442. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), relation_sn)
  1443. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), product.T_remark)
  1444. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), product.T_relation_sn)
  1445. }
  1446. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("L%d", line), Style3)
  1447. // 保存文件
  1448. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1449. fmt.Println(err)
  1450. }
  1451. var url string
  1452. //// 上传 OSS
  1453. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1454. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1455. if !is {
  1456. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1457. c.ServeJSON()
  1458. return
  1459. }
  1460. //删除目录
  1461. err := os.Remove("ofile/" + filename + ".xlsx")
  1462. if err != nil {
  1463. logs.Error(lib.FuncName(), err)
  1464. }
  1465. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1466. c.ServeJSON()
  1467. return
  1468. }
  1469. func (c *StockController) StockOut_Get() {
  1470. // 查询
  1471. T_number := c.GetString("T_number")
  1472. o := orm.NewOrm()
  1473. StockOutDao := Stock.NewStockOut(o)
  1474. StockOutProductDao := Stock.NewStockOutProduct(o)
  1475. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1476. if err != nil {
  1477. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1478. c.ServeJSON()
  1479. return
  1480. }
  1481. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  1482. var pList []Stock.StockOutProduct_R
  1483. for _, v := range productList {
  1484. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  1485. }
  1486. userList, _ := NatsServer.Read_User_List_All()
  1487. Account.Read_User_All_Map(userList)
  1488. Basic.Read_Depot_All_Map()
  1489. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockOutToStockOut_Detail(stockOut, pList)}
  1490. c.ServeJSON()
  1491. return
  1492. }
  1493. func (c *StockController) StockOut_Add() {
  1494. rand_x := 0
  1495. T_number := ""
  1496. o := orm.NewOrm()
  1497. o.Begin()
  1498. StockOutDao := Stock.NewStockOut(o)
  1499. for true {
  1500. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  1501. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1502. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  1503. break
  1504. }
  1505. rand_x += 1
  1506. }
  1507. T_contract_number := c.GetString("T_contract_number")
  1508. T_depot_id, _ := c.GetInt("T_depot_id")
  1509. T_type, _ := c.GetInt("T_type") // 出库类型 1-领料出库 2-销售出库
  1510. T_date := c.GetString("T_date")
  1511. T_receive := c.GetString("T_receive")
  1512. T_product := c.GetString("T_product")
  1513. T_remark := c.GetString("T_remark")
  1514. T_project := c.GetString("T_project")
  1515. T_delivery_type, _ := c.GetInt("T_delivery_type")
  1516. T_signer_unit := c.GetString("T_signer_unit")
  1517. T_signer := c.GetString("T_signer")
  1518. T_signer_phone := c.GetString("T_signer_phone")
  1519. T_signer_date := c.GetString("T_signer_date")
  1520. T_courier_number := c.GetString("T_courier_number")
  1521. var_ := Stock.StockOut{
  1522. T_number: T_number,
  1523. T_contract_number: T_contract_number,
  1524. T_depot_id: T_depot_id,
  1525. T_type: T_type,
  1526. T_date: T_date,
  1527. T_receive: T_receive,
  1528. T_remark: T_remark,
  1529. T_project: T_project,
  1530. T_submit: c.User.T_uuid,
  1531. // 销售出库
  1532. T_delivery_type: T_delivery_type, // 1-自送 2-自提 3-快递
  1533. T_signer_unit: T_signer_unit,
  1534. T_signer: T_signer,
  1535. T_signer_phone: T_signer_phone,
  1536. T_signer_date: T_signer_date,
  1537. T_courier_number: T_courier_number,
  1538. }
  1539. StockOutProductDao := Stock.NewStockOutProduct(o)
  1540. StockDao := Stock.NewStock(o)
  1541. DeviceDao := Stock.NewDevice(o)
  1542. ContractDao := Contract.NewContract(o)
  1543. ContractProductDao := Contract.NewContractProduct(o)
  1544. _, err := StockOutDao.Add_StockOut(var_)
  1545. if err != nil {
  1546. o.Rollback()
  1547. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  1548. c.ServeJSON()
  1549. return
  1550. }
  1551. // 1、添加出库单
  1552. contract, _ := ContractDao.Read_Contract_ByT_number(T_contract_number)
  1553. if contract.Id == 0 && T_type == 2 {
  1554. o.Rollback()
  1555. c.Data["json"] = lib.JSONS{Code: 203, Msg: "合同编号错误!"}
  1556. c.ServeJSON()
  1557. return
  1558. }
  1559. if len(T_product) == 0 {
  1560. o.Rollback()
  1561. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  1562. c.ServeJSON()
  1563. return
  1564. }
  1565. productList := lib.SplitString(T_product, "|")
  1566. allProductList := []int{}
  1567. for _, v := range productList {
  1568. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  1569. allProductList = append(allProductList, product_id)
  1570. product, _ := Basic.Read_Product_ById(product_id)
  1571. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  1572. T_relation_sn := strings.Split(v, "-")[2]
  1573. if T_relation_sn == "" && product.T_relation_sn == 1 {
  1574. o.Rollback()
  1575. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  1576. c.ServeJSON()
  1577. return
  1578. }
  1579. // 2、更新设备状态为已出库
  1580. if len(T_relation_sn) > 0 {
  1581. snList := strings.Split(T_relation_sn, ",")
  1582. for _, sn := range snList {
  1583. mqtt := Stock.Read_MqttUser(sn)
  1584. device := Stock.Device{
  1585. T_contract_number: T_contract_number,
  1586. T_product_id: product_id,
  1587. T_out_number: T_number,
  1588. T_sn: sn,
  1589. T_iccid: mqtt.Iccid,
  1590. T_imei: mqtt.Imei,
  1591. T_State: 1,
  1592. T_project: T_project,
  1593. }
  1594. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  1595. if err != nil {
  1596. o.Rollback()
  1597. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  1598. c.ServeJSON()
  1599. return
  1600. }
  1601. }
  1602. }
  1603. // 3、添加出库产品清单
  1604. stockOutProduct := Stock.StockOutProduct{
  1605. T_number: T_number,
  1606. T_product_id: product_id,
  1607. T_depot_id: T_depot_id,
  1608. T_num: num, // 出库数量
  1609. T_date: T_date, // 出库数量
  1610. T_relation_sn: T_relation_sn,
  1611. }
  1612. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  1613. if err != nil {
  1614. o.Rollback()
  1615. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  1616. c.ServeJSON()
  1617. return
  1618. }
  1619. // 4、更改合同产品列表清单
  1620. contractProduct, _ := ContractProductDao.Read_ContractProduct_ByT_number_T_product_id(T_contract_number, product_id)
  1621. contractProduct.T_product_out += num
  1622. if contractProduct.T_product_out > contractProduct.T_product_total && T_type == 2 {
  1623. o.Rollback()
  1624. p, _ := Basic.Read_Product_ById(product_id)
  1625. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】出库数量超过预计出库数量!", p.T_name)}
  1626. c.ServeJSON()
  1627. return
  1628. }
  1629. contractProduct.T_State = 2
  1630. if contractProduct.T_product_out == 0 {
  1631. contractProduct.T_State = 1
  1632. }
  1633. if contractProduct.T_product_out == contractProduct.T_product_total {
  1634. contractProduct.T_State = 3
  1635. }
  1636. err = ContractProductDao.Update_ContractProduct(contractProduct, "T_product_out", "T_State")
  1637. if err != nil {
  1638. o.Rollback()
  1639. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同产品清单失败"}
  1640. c.ServeJSON()
  1641. return
  1642. }
  1643. // 5、更新产品库存列表
  1644. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 1)
  1645. if err != nil {
  1646. o.Rollback()
  1647. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存失败"}
  1648. c.ServeJSON()
  1649. return
  1650. }
  1651. }
  1652. o.Commit()
  1653. o2 := orm.NewOrm()
  1654. o2.Begin()
  1655. ContractDao2 := Contract.NewContract(o2)
  1656. ContractProductDao2 := Contract.NewContractProduct(o2)
  1657. // 5、更新合同出库状态
  1658. var T_out int
  1659. if contract.T_State == 1 {
  1660. T_out = 2
  1661. // 查询合同产品清单是否全部都为已出库
  1662. state := ContractProductDao2.Read_ContractProduct_T_State_List(contract.T_number)
  1663. if state == 1 {
  1664. T_out = 1
  1665. }
  1666. if state == 3 {
  1667. T_out = 3
  1668. }
  1669. }
  1670. if T_out != contract.T_out {
  1671. contract.T_out = T_out
  1672. err = ContractDao2.Update_Contract(contract, "T_out")
  1673. if err != nil {
  1674. o2.Rollback()
  1675. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
  1676. c.ServeJSON()
  1677. return
  1678. }
  1679. }
  1680. o2.Commit()
  1681. StockOut_Edit_StockMonth(T_date, T_depot_id, allProductList)
  1682. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "出库", var_)
  1683. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1684. c.ServeJSON()
  1685. return
  1686. }
  1687. func (c *StockController) StockOut_Edit_Delivery() {
  1688. T_number := c.GetString("T_number")
  1689. T_remark := c.GetString("T_remark")
  1690. T_project := c.GetString("T_project")
  1691. T_delivery_type, _ := c.GetInt("T_delivery_type")
  1692. T_signer_unit := c.GetString("T_signer_unit")
  1693. T_signer := c.GetString("T_signer")
  1694. T_signer_phone := c.GetString("T_signer_phone")
  1695. T_signer_date := c.GetString("T_signer_date")
  1696. T_courier_number := c.GetString("T_courier_number")
  1697. o := orm.NewOrm()
  1698. StockOutDao := Stock.NewStockOut(o)
  1699. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1700. if err != nil {
  1701. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库编号错误!"}
  1702. c.ServeJSON()
  1703. return
  1704. }
  1705. if len(T_remark) > 0 {
  1706. stockOut.T_remark = T_remark
  1707. }
  1708. if len(T_project) > 0 {
  1709. stockOut.T_project = T_project
  1710. }
  1711. if T_delivery_type > 0 {
  1712. stockOut.T_delivery_type = T_delivery_type
  1713. }
  1714. if len(T_signer_unit) > 0 {
  1715. stockOut.T_signer_unit = T_signer_unit
  1716. }
  1717. if len(T_signer) > 0 {
  1718. stockOut.T_signer = T_signer
  1719. }
  1720. if len(T_signer_phone) > 0 {
  1721. stockOut.T_signer_phone = T_signer_phone
  1722. }
  1723. if len(T_signer_date) > 0 {
  1724. stockOut.T_signer_date = T_signer_date
  1725. }
  1726. if len(T_courier_number) > 0 {
  1727. stockOut.T_courier_number = T_courier_number
  1728. }
  1729. 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")
  1730. if err != nil {
  1731. o.Rollback()
  1732. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  1733. c.ServeJSON()
  1734. return
  1735. }
  1736. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改发货单", stockOut)
  1737. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1738. c.ServeJSON()
  1739. return
  1740. }
  1741. func (c *StockController) StockOut_Edit() {
  1742. T_number := c.GetString("T_number") // 出库单号
  1743. //T_depot_id, _ := c.GetInt("T_depot_id")
  1744. T_date := c.GetString("T_date")
  1745. T_receive := c.GetString("T_receive")
  1746. T_project := c.GetString("T_project")
  1747. T_product := c.GetString("T_product")
  1748. T_remark := c.GetString("T_remark")
  1749. date, is := lib.DateStrToTime(T_date)
  1750. if !is {
  1751. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1752. c.ServeJSON()
  1753. return
  1754. }
  1755. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  1756. T_date = ""
  1757. }
  1758. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
  1759. o := orm.NewOrm()
  1760. o.Begin()
  1761. StockOutDao := Stock.NewStockOut(o)
  1762. StockOutProductDao := Stock.NewStockOutProduct(o)
  1763. StockDao := Stock.NewStock(o)
  1764. DeviceDao := Stock.NewDevice(o)
  1765. IotCardDao := Property.NewIotCard(&o)
  1766. // 查询入库信息
  1767. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1768. T_old_date := StockOut.T_date
  1769. if err != nil {
  1770. o.Rollback()
  1771. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1772. c.ServeJSON()
  1773. return
  1774. }
  1775. // 查询入库产品信息
  1776. productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  1777. var oldProductList []dto.StockProduct
  1778. var newProductList []dto.StockProduct
  1779. productOldMap := map[int]dto.StockProduct{}
  1780. allProductListMap := make(map[int]struct{})
  1781. for _, product := range productOldList {
  1782. stockProduct := dto.StockProduct{
  1783. T_product_id: product.T_product_id,
  1784. T_num: product.T_num,
  1785. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1786. }
  1787. oldProductList = append(oldProductList, stockProduct)
  1788. productOldMap[product.T_product_id] = stockProduct
  1789. if _, ok := allProductListMap[product.T_product_id]; !ok {
  1790. allProductListMap[product.T_product_id] = struct{}{}
  1791. }
  1792. }
  1793. productNewList := lib.SplitString(T_product, "|")
  1794. productNewMap := map[int]dto.StockProduct{}
  1795. for _, v := range productNewList {
  1796. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  1797. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  1798. T_relation_sn := strings.Split(v, "-")[2]
  1799. stockProduct := dto.StockProduct{
  1800. T_product_id: product_id,
  1801. T_num: num,
  1802. T_relation_sn: lib.SplitString(T_relation_sn, ","),
  1803. }
  1804. newProductList = append(newProductList, stockProduct)
  1805. productNewMap[product_id] = stockProduct
  1806. if _, ok := allProductListMap[product_id]; !ok {
  1807. allProductListMap[product_id] = struct{}{}
  1808. }
  1809. }
  1810. // 判断产品列表信息是否相同
  1811. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  1812. // 两次提交的数据相同,则不用修改
  1813. if !StockProductListIsEqual {
  1814. if len(needDelete) > 0 {
  1815. // 删除入库产品列表
  1816. for _, v := range needDelete {
  1817. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1818. // 删除入库产品列表
  1819. err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
  1820. if err != nil {
  1821. o.Rollback()
  1822. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  1823. c.ServeJSON()
  1824. return
  1825. }
  1826. if product.T_relation_sn == 1 {
  1827. // 删除该入库单关联sn
  1828. for _, sn := range v.T_relation_sn {
  1829. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  1830. if err != nil {
  1831. o.Rollback()
  1832. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  1833. c.ServeJSON()
  1834. return
  1835. }
  1836. }
  1837. }
  1838. // 增加库存
  1839. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  1840. if err != nil {
  1841. o.Rollback()
  1842. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1843. c.ServeJSON()
  1844. return
  1845. }
  1846. }
  1847. }
  1848. if len(needAdd) > 0 {
  1849. // 新增入库产品列表
  1850. for _, v := range needAdd {
  1851. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1852. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  1853. o.Rollback()
  1854. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  1855. c.ServeJSON()
  1856. return
  1857. }
  1858. // 添加该入库单关联sn
  1859. for _, sn := range v.T_relation_sn {
  1860. mqtt := Stock.Read_MqttUser(sn)
  1861. // 添加设备
  1862. device := Stock.Device{
  1863. T_contract_number: "",
  1864. T_out_number: StockOut.T_number,
  1865. T_product_id: v.T_product_id,
  1866. T_in_number: "",
  1867. T_sn: sn,
  1868. T_iccid: mqtt.Iccid,
  1869. T_imei: mqtt.Imei,
  1870. T_State: 1,
  1871. CreateTime: StockOut.CreateTime,
  1872. UpdateTime: StockOut.UpdateTime,
  1873. }
  1874. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  1875. if err != nil {
  1876. o.Rollback()
  1877. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  1878. c.ServeJSON()
  1879. return
  1880. }
  1881. // 添加物联网卡
  1882. iotCard := Property.IotCard{
  1883. T_iccid: mqtt.Iccid,
  1884. T_sn: sn,
  1885. T_State: 2, //1-未使用 2-已使用 3-已作废
  1886. T_type: "4G",
  1887. }
  1888. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  1889. if err != nil {
  1890. o.Rollback()
  1891. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  1892. c.ServeJSON()
  1893. return
  1894. }
  1895. }
  1896. StockOutProduct := Stock.StockOutProduct{
  1897. T_number: T_number,
  1898. T_product_id: v.T_product_id,
  1899. T_depot_id: StockOut.T_depot_id,
  1900. T_num: v.T_num, // 入库数量
  1901. T_date: StockOut.T_date, // 入库日期
  1902. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  1903. }
  1904. _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
  1905. if err != nil {
  1906. o.Rollback()
  1907. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1908. c.ServeJSON()
  1909. return
  1910. }
  1911. // 减少库存
  1912. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  1913. if err != nil {
  1914. o.Rollback()
  1915. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  1916. c.ServeJSON()
  1917. return
  1918. }
  1919. }
  1920. }
  1921. if len(needEdit) > 0 {
  1922. for _, v := range needEdit {
  1923. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1924. StockOutProduct := Stock.StockOutProduct{
  1925. T_number: StockOut.T_number,
  1926. T_product_id: v.T_product_id,
  1927. T_depot_id: StockOut.T_depot_id,
  1928. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  1929. T_date: StockOut.T_date, // 入库日期
  1930. }
  1931. // 出库数量比之前多,减少库存
  1932. var T_type int
  1933. var T_num int
  1934. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  1935. // 减少库存
  1936. T_type = 1
  1937. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  1938. } else {
  1939. // 增加库存
  1940. T_type = 2
  1941. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  1942. }
  1943. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  1944. if err != nil {
  1945. o.Rollback()
  1946. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1947. c.ServeJSON()
  1948. return
  1949. }
  1950. // 更新产品库存表
  1951. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  1952. if err != nil {
  1953. o.Rollback()
  1954. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  1955. c.ServeJSON()
  1956. return
  1957. }
  1958. }
  1959. }
  1960. for _, diff := range snDiff {
  1961. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  1962. // 删除减少的sn
  1963. if len(diff.T_delete_relation_sn) > 0 {
  1964. for _, sn := range diff.T_delete_relation_sn {
  1965. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  1966. if err != nil {
  1967. o.Rollback()
  1968. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1969. c.ServeJSON()
  1970. return
  1971. }
  1972. }
  1973. // 增加库存
  1974. _, 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)
  1975. if err != nil {
  1976. o.Rollback()
  1977. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1978. c.ServeJSON()
  1979. return
  1980. }
  1981. }
  1982. if len(diff.T_add_relation_sn) > 0 {
  1983. for _, sn := range diff.T_add_relation_sn {
  1984. mqtt := Stock.Read_MqttUser(sn)
  1985. // 添加设备
  1986. device := Stock.Device{
  1987. T_contract_number: "",
  1988. T_out_number: StockOut.T_number,
  1989. T_product_id: diff.T_product_id,
  1990. T_in_number: "",
  1991. T_sn: sn,
  1992. T_iccid: mqtt.Iccid,
  1993. T_imei: mqtt.Imei,
  1994. T_State: 1,
  1995. CreateTime: StockOut.CreateTime,
  1996. UpdateTime: StockOut.UpdateTime,
  1997. }
  1998. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  1999. if err != nil {
  2000. o.Rollback()
  2001. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  2002. c.ServeJSON()
  2003. return
  2004. }
  2005. // 添加物联网卡
  2006. iotCard := Property.IotCard{
  2007. T_iccid: mqtt.Iccid,
  2008. T_sn: sn,
  2009. T_State: 2, //1-未使用 2-已使用 3-已作废
  2010. T_type: "4G",
  2011. }
  2012. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  2013. if err != nil {
  2014. o.Rollback()
  2015. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  2016. c.ServeJSON()
  2017. return
  2018. }
  2019. }
  2020. // 减少库存
  2021. _, 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)
  2022. if err != nil {
  2023. o.Rollback()
  2024. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  2025. c.ServeJSON()
  2026. return
  2027. }
  2028. }
  2029. StockOutProduct := Stock.StockOutProduct{
  2030. T_number: StockOut.T_number,
  2031. T_product_id: diff.T_product_id,
  2032. T_depot_id: StockOut.T_depot_id,
  2033. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  2034. T_date: StockOut.T_date, // 入库日期
  2035. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  2036. }
  2037. // 更新产品库存表
  2038. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  2039. if err != nil {
  2040. o.Rollback()
  2041. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  2042. c.ServeJSON()
  2043. return
  2044. }
  2045. }
  2046. }
  2047. if len(T_remark) > 0 {
  2048. StockOut.T_remark = T_remark
  2049. }
  2050. if len(T_receive) > 0 {
  2051. StockOut.T_receive = T_receive
  2052. }
  2053. if len(T_project) > 0 {
  2054. StockOut.T_project = T_project
  2055. }
  2056. if len(T_date) > 0 {
  2057. StockOut.T_date = T_date
  2058. }
  2059. err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_date")
  2060. if err != nil {
  2061. o.Rollback()
  2062. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  2063. c.ServeJSON()
  2064. return
  2065. }
  2066. o.Commit()
  2067. if len(T_date) > 0 && T_old_date != T_date {
  2068. // 修改出库产品日期
  2069. StockOutProductDao.Update_StockOutProduct_T_date(StockOut.T_number, T_date)
  2070. DeviceDao.Update_Device_CreateTimeByT_out_number(StockOut.T_number, T_date)
  2071. }
  2072. if len(T_project) > 0 {
  2073. DeviceDao.Update_Device_ByT_out_number_T_project(StockOut.T_number, T_project)
  2074. }
  2075. // 更新月份统计表
  2076. allProductList := []int{}
  2077. for i, _ := range allProductListMap {
  2078. allProductList = append(allProductList, i)
  2079. }
  2080. StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
  2081. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改", StockOut)
  2082. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2083. c.ServeJSON()
  2084. return
  2085. }
  2086. // 删除入库
  2087. func (c *StockController) StockOut_Del() {
  2088. T_number := c.GetString("T_number") // 入库单号
  2089. o := orm.NewOrm()
  2090. o.Begin()
  2091. StockOutDao := Stock.NewStockOut(o)
  2092. StockOutProductDao := Stock.NewStockOutProduct(o)
  2093. StockDao := Stock.NewStock(o)
  2094. DeviceDao := Stock.NewDevice(o)
  2095. // 查询入库信息
  2096. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2097. if err != nil {
  2098. o.Rollback()
  2099. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2100. c.ServeJSON()
  2101. return
  2102. }
  2103. // 查询出库产品信息
  2104. productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2105. var oldProductList []dto.StockProduct
  2106. allProductList := []int{}
  2107. for _, product := range productOldList {
  2108. oldProductList = append(oldProductList, dto.StockProduct{
  2109. T_product_id: product.T_product_id,
  2110. T_num: product.T_num,
  2111. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  2112. })
  2113. allProductList = append(allProductList, product.T_product_id)
  2114. }
  2115. // 删除入库产品列表
  2116. for _, v := range oldProductList {
  2117. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2118. // 删除入库产品列表
  2119. err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
  2120. if err != nil {
  2121. o.Rollback()
  2122. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
  2123. c.ServeJSON()
  2124. return
  2125. }
  2126. if product.T_relation_sn == 1 {
  2127. // 删除该入库单关联sn
  2128. for _, sn := range v.T_relation_sn {
  2129. err = DeviceDao.Delete_Device_ByT_out_number(stockOut.T_number, sn)
  2130. if err != nil {
  2131. o.Rollback()
  2132. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库设备失败"}
  2133. c.ServeJSON()
  2134. return
  2135. }
  2136. }
  2137. }
  2138. // 增加库存
  2139. _, err = StockDao.AddOrUpdate_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  2140. if err != nil {
  2141. o.Rollback()
  2142. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  2143. c.ServeJSON()
  2144. return
  2145. }
  2146. }
  2147. err = StockOutDao.Delete_StockOut(stockOut)
  2148. if err != nil {
  2149. o.Rollback()
  2150. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
  2151. c.ServeJSON()
  2152. return
  2153. }
  2154. o.Commit()
  2155. // 更新月份统计表
  2156. StockOut_Edit_StockMonth(stockOut.T_date, stockOut.T_depot_id, allProductList)
  2157. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "删除", stockOut)
  2158. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  2159. c.ServeJSON()
  2160. return
  2161. }
  2162. // 导出出库单
  2163. func (c *StockController) StockOut_Excel() {
  2164. // 查询
  2165. T_number := c.GetString("T_number")
  2166. o := orm.NewOrm()
  2167. StockOutDao := Stock.NewStockOut(o)
  2168. StockOutProductDao := Stock.NewStockOutProduct(o)
  2169. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  2170. if err != nil {
  2171. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2172. c.ServeJSON()
  2173. return
  2174. }
  2175. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2176. //productList = append(productList, productList...)
  2177. //productList = append(productList, productList...)
  2178. //productList = append(productList, productList...)
  2179. var pList []Stock.StockOutProduct_R
  2180. for _, v := range productList {
  2181. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  2182. }
  2183. userList, _ := NatsServer.Read_User_List_All()
  2184. Account.Read_User_All_Map(userList)
  2185. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  2186. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  2187. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  2188. f := excelize.NewFile() // 设置单元格的值
  2189. Style1, _ := f.NewStyle(
  2190. &excelize.Style{
  2191. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  2192. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  2193. })
  2194. Style2, _ := f.NewStyle(
  2195. &excelize.Style{
  2196. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2197. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  2198. })
  2199. Style3, _ := f.NewStyle(
  2200. &excelize.Style{
  2201. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  2202. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2203. Border: []excelize.Border{
  2204. {Type: "left", Color: "000000", Style: 1},
  2205. {Type: "top", Color: "000000", Style: 1},
  2206. {Type: "bottom", Color: "000000", Style: 1},
  2207. {Type: "right", Color: "000000", Style: 1},
  2208. },
  2209. })
  2210. Style4, _ := f.NewStyle(
  2211. &excelize.Style{
  2212. Font: &excelize.Font{Size: 11, Family: "宋体"},
  2213. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  2214. Border: []excelize.Border{
  2215. {Type: "left", Color: "000000", Style: 1},
  2216. {Type: "top", Color: "000000", Style: 1},
  2217. {Type: "bottom", Color: "000000", Style: 1},
  2218. {Type: "right", Color: "000000", Style: 1},
  2219. },
  2220. })
  2221. productList2 := lib.ChunkBy(productList, 10)
  2222. for i, products := range productList2 {
  2223. if len(products) < 10 {
  2224. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  2225. }
  2226. row := i*17 + 1
  2227. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2228. f.SetRowStyle("Sheet1", row, row, Style1)
  2229. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  2230. f.SetRowHeight("Sheet1", row, 20)
  2231. row += 1
  2232. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2233. f.SetRowStyle("Sheet1", row, row, Style1)
  2234. title := "硬件出库单"
  2235. if len(productList2) > 1 {
  2236. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  2237. }
  2238. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  2239. f.SetRowHeight("Sheet1", row, 20)
  2240. row += 1
  2241. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2242. f.SetRowStyle("Sheet1", row, row, Style2)
  2243. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  2244. f.SetRowHeight("Sheet1", row, 20)
  2245. row += 1
  2246. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2247. f.SetRowStyle("Sheet1", row, row, Style2)
  2248. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  2249. f.SetRowHeight("Sheet1", row, 20)
  2250. row += 1
  2251. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2252. f.SetRowStyle("Sheet1", row, row, Style2)
  2253. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  2254. f.SetRowHeight("Sheet1", row, 20)
  2255. // 这里设置表头
  2256. row += 1
  2257. f.SetRowHeight("Sheet1", row, 20)
  2258. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  2259. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  2260. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  2261. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  2262. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  2263. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  2264. // 设置列宽
  2265. f.SetColWidth("Sheet1", "A", "A", 5)
  2266. f.SetColWidth("Sheet1", "B", "B", 20)
  2267. f.SetColWidth("Sheet1", "C", "C", 12)
  2268. f.SetColWidth("Sheet1", "D", "D", 10)
  2269. f.SetColWidth("Sheet1", "E", "E", 10)
  2270. f.SetColWidth("Sheet1", "F", "F", 15)
  2271. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  2272. sRow := row
  2273. // 循环写入数据
  2274. for j, v := range products {
  2275. row += 1
  2276. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2277. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  2278. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  2279. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  2280. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  2281. if v.T_num > 0 {
  2282. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  2283. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  2284. }
  2285. }
  2286. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  2287. row += 1
  2288. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  2289. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  2290. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  2291. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  2292. f.SetRowStyle("Sheet1", row, row, Style2)
  2293. f.SetRowHeight("Sheet1", row, 20)
  2294. }
  2295. // 保存文件
  2296. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  2297. fmt.Println(err)
  2298. }
  2299. var url string
  2300. //// 上传 OSS
  2301. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  2302. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  2303. if !is {
  2304. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  2305. c.ServeJSON()
  2306. return
  2307. }
  2308. //删除目录
  2309. err = os.Remove("ofile/" + filename + ".xlsx")
  2310. if err != nil {
  2311. logs.Error(lib.FuncName(), err)
  2312. }
  2313. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  2314. c.ServeJSON()
  2315. return
  2316. }
  2317. func StockOut_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  2318. date, _ := lib.DateStrToTime(T_date)
  2319. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  2320. // // 修改本月数据不同步库存
  2321. // return nil
  2322. //}
  2323. //T_month := date.Format("2006-01")
  2324. months := generateMonthList(date)
  2325. o := orm.NewOrm()
  2326. StockOutDao := Stock.NewStockOut(o)
  2327. StockMonthDao := Stock.NewStockMonth(o)
  2328. StockOutProductDao := Stock.NewStockOutProduct(o)
  2329. StockInProductDao := Stock.NewStockInProduct(o)
  2330. for _, T_product_id := range allProductList {
  2331. for _, T_month := range months {
  2332. // 获取当前产品本月出入库数量
  2333. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  2334. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  2335. if stockMonth.Id > 0 {
  2336. // 修改
  2337. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  2338. if len(lastMonth) > 0 {
  2339. stockMonth.T_beginning = lastMonth[0].T_ending
  2340. } else {
  2341. stockMonth.T_beginning = 0
  2342. }
  2343. // 期末库存 = 期初库存+入库-出库
  2344. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2345. err := StockMonthDao.Update_StockMonth(stockMonth, "T_out", "T_beginning", "T_ending")
  2346. if err != nil {
  2347. return err
  2348. }
  2349. } else {
  2350. product, _ := Basic.Read_Product_ById(T_product_id)
  2351. // 添加库存记录
  2352. stockMonth = Stock.StockMonth{
  2353. T_depot_id: T_depot_id,
  2354. T_product_id: T_product_id,
  2355. T_product_class: product.T_class,
  2356. T_month: T_month,
  2357. }
  2358. // 获取当前产品本月出入库数量
  2359. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  2360. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  2361. if len(lastMonth) > 0 {
  2362. stockMonth.T_beginning = lastMonth[0].T_ending
  2363. } else {
  2364. stockMonth.T_beginning = 0
  2365. }
  2366. // 期末库存 = 期初库存+入库-出库
  2367. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2368. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  2369. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  2370. if err != nil {
  2371. return err
  2372. }
  2373. }
  2374. }
  2375. }
  2376. return nil
  2377. }
  2378. func Cron_StockMonth() {
  2379. //创建一个定时任务对象
  2380. c := cron.New(cron.WithSeconds())
  2381. //给对象增加定时任务
  2382. // @monthly 每月运行一次,每月第一天午夜 0 0 0 1 * *
  2383. //c.AddFunc("0 */1 * * * ?", Cron_StockMonth_Add)
  2384. c.AddFunc("@monthly", Cron_StockMonth_Add)
  2385. //启动定时任务
  2386. c.Start()
  2387. defer c.Stop()
  2388. //查询语句,阻塞,让main函数不退出,保持程序运行
  2389. select {}
  2390. }
  2391. // 保存每月入库出库明细
  2392. func Cron_StockMonth_Add() {
  2393. T_month := time.Now().Format("2006-01")
  2394. //T_month := time.Now().AddDate(0, -1, 0).Format("2006-01")
  2395. logs.Info("开始统计" + T_month + "库存明细数据")
  2396. o := orm.NewOrm()
  2397. StockDao := Stock.NewStock(o)
  2398. StockOutDao := Stock.NewStockOut(o)
  2399. StockMonthDao := Stock.NewStockMonth(o)
  2400. StockOutProductDao := Stock.NewStockOutProduct(o)
  2401. StockInProductDao := Stock.NewStockInProduct(o)
  2402. stockList, _ := StockDao.Read_Stock_List(0, 0, "", "", 0, 9999)
  2403. for _, stock := range stockList {
  2404. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  2405. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  2406. if stockMonth.Id > 0 {
  2407. // 修改
  2408. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2409. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2410. if len(lastMonth) > 0 {
  2411. stockMonth.T_beginning = lastMonth[0].T_ending
  2412. } else {
  2413. stockMonth.T_beginning = 0
  2414. }
  2415. // 期末库存 = 期初库存+入库-出库
  2416. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2417. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_out", "T_beginning", "T_ending")
  2418. if err != nil {
  2419. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  2420. }
  2421. } else {
  2422. stockMonth = Stock.StockMonth{
  2423. T_depot_id: stock.T_depot_id,
  2424. T_product_id: stock.T_product_id,
  2425. T_product_class: stock.T_product_class,
  2426. T_month: T_month,
  2427. }
  2428. // 获取当前产品本月出入库数量
  2429. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2430. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2431. if len(lastMonth) > 0 {
  2432. stockMonth.T_beginning = lastMonth[0].T_ending
  2433. } else {
  2434. stockMonth.T_beginning = 0
  2435. }
  2436. // 期末库存 = 期初库存+入库-出库
  2437. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2438. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(stock.T_depot_id, stock.T_product_id, T_month)
  2439. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  2440. if err != nil {
  2441. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  2442. }
  2443. }
  2444. }
  2445. }
  2446. // 重写设备出入库记录
  2447. func Cron_Device_Add() {
  2448. logs.Info("开始统计写入设备出入库数据")
  2449. o := orm.NewOrm()
  2450. o.Begin()
  2451. DeviceDao := Stock.NewDevice(o)
  2452. StockOutDao := Stock.NewStockOut(o)
  2453. StockInDao := Stock.NewStockIn(o)
  2454. StockOutProductDao := Stock.NewStockOutProduct(o)
  2455. StockInProductDao := Stock.NewStockInProduct(o)
  2456. // 添加入库设备信息
  2457. StockInList, _ := StockInDao.Read_StockIn_List(0, "", "", 0, 9999)
  2458. for _, stockIn := range StockInList {
  2459. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  2460. for _, product := range productList {
  2461. if len(product.T_relation_sn) == 0 {
  2462. continue
  2463. }
  2464. for _, sn := range lib.SplitString(product.T_relation_sn, ",") {
  2465. mqtt := Stock.Read_MqttUser(sn)
  2466. // 添加设备
  2467. device := Stock.Device{
  2468. T_contract_number: "",
  2469. T_out_number: "",
  2470. T_product_id: product.T_product_id,
  2471. T_in_number: stockIn.T_number,
  2472. T_sn: sn,
  2473. T_iccid: mqtt.Iccid,
  2474. T_imei: mqtt.Imei,
  2475. T_State: 2,
  2476. CreateTime: product.CreateTime,
  2477. UpdateTime: product.UpdateTime,
  2478. }
  2479. _, err := DeviceDao.AddOrUpdate_Device(device, 2)
  2480. if err != nil {
  2481. o.Rollback()
  2482. return
  2483. }
  2484. }
  2485. }
  2486. }
  2487. // 添加出库设备信息
  2488. StockOutList, _ := StockOutDao.Read_StockOut_List(0, "", "", "", 0, 9999)
  2489. for _, stockOut := range StockOutList {
  2490. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2491. for _, product := range productList {
  2492. if len(product.T_relation_sn) == 0 {
  2493. continue
  2494. }
  2495. for _, sn := range lib.SplitString(product.T_relation_sn, ",") {
  2496. mqtt := Stock.Read_MqttUser(sn)
  2497. // 添加设备
  2498. device := Stock.Device{
  2499. T_contract_number: stockOut.T_contract_number,
  2500. T_product_id: product.T_product_id,
  2501. T_out_number: stockOut.T_number,
  2502. T_sn: sn,
  2503. T_iccid: mqtt.Iccid,
  2504. T_imei: mqtt.Imei,
  2505. T_State: 1,
  2506. T_project: stockOut.T_project,
  2507. CreateTime: product.CreateTime,
  2508. UpdateTime: product.UpdateTime,
  2509. }
  2510. _, err := DeviceDao.AddOrUpdate_Device(device, 1)
  2511. if err != nil {
  2512. o.Rollback()
  2513. return
  2514. }
  2515. }
  2516. }
  2517. }
  2518. o.Commit()
  2519. }
  2520. func generateMonthList(startMonth time.Time) []string {
  2521. var months []string
  2522. endMonth := time.Now().AddDate(0, 0, 0)
  2523. for month := startMonth; month.Before(endMonth); month = month.AddDate(0, 1, 0) {
  2524. fmt.Println("=========================", month.Format("2006-01"))
  2525. months = append(months, month.Format("2006-01"))
  2526. }
  2527. return months
  2528. }