Stock.go 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561
  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 "git.baozhida.cn/ERP_libs/Nats"
  15. userlibs "git.baozhida.cn/ERP_libs/User"
  16. "git.baozhida.cn/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. userList, _ := NatsServer.Read_User_List_All()
  381. Account.Read_User_All_Map(userList)
  382. Basic.Read_Depot_All_Map()
  383. StockInDao := Stock.NewStockIn(orm.NewOrm())
  384. R_List, R_cnt := StockInDao.Read_StockInProduct_List(T_name, T_start_date, T_end_date, T_depot_id, page, page_z)
  385. var r_jsons lib.R_JSONS
  386. r_jsons.Num = R_cnt
  387. r_jsons.Data = R_List
  388. r_jsons.Page = page
  389. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  390. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  391. c.ServeJSON()
  392. return
  393. }
  394. func (c *StockController) StockIn_Get() {
  395. // 查询
  396. T_number := c.GetString("T_number")
  397. o := orm.NewOrm()
  398. StockInDao := Stock.NewStockIn(o)
  399. StockInProductDao := Stock.NewStockInProduct(o)
  400. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  401. if err != nil {
  402. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  403. c.ServeJSON()
  404. return
  405. }
  406. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  407. var pList []Stock.StockInProduct_R
  408. for _, v := range productList {
  409. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  410. }
  411. userList, _ := NatsServer.Read_User_List_All()
  412. Account.Read_User_All_Map(userList)
  413. Basic.Read_Depot_All_Map()
  414. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockInToStockIn_Detail(stockIn, pList)}
  415. c.ServeJSON()
  416. return
  417. }
  418. func (c *StockController) StockIn_Add() {
  419. rand_x := 0
  420. T_number := ""
  421. o := orm.NewOrm()
  422. o.Begin()
  423. StockInDao := Stock.NewStockIn(o)
  424. for true {
  425. T_number = "RK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  426. _, err := StockInDao.Read_StockIn_ByT_number(T_number)
  427. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  428. break
  429. }
  430. rand_x += 1
  431. }
  432. T_depot_id, _ := c.GetInt("T_depot_id")
  433. T_date := c.GetString("T_date")
  434. T_product := c.GetString("T_product")
  435. T_remark := c.GetString("T_remark")
  436. _, is := lib.DateStrToTime(T_date)
  437. if !is {
  438. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  439. c.ServeJSON()
  440. return
  441. }
  442. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "入库", T_product)
  443. var_ := Stock.StockIn{
  444. T_number: T_number,
  445. T_depot_id: T_depot_id,
  446. T_date: T_date,
  447. T_remark: T_remark,
  448. T_submit: c.User.T_uuid,
  449. }
  450. StockInProductDao := Stock.NewStockInProduct(o)
  451. StockDao := Stock.NewStock(o)
  452. DeviceDao := Stock.NewDevice(o)
  453. IotCardDao := Property.NewIotCard(&o)
  454. _, err := StockInDao.Add_StockIn(var_)
  455. if err != nil {
  456. o.Rollback()
  457. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  458. c.ServeJSON()
  459. return
  460. }
  461. if len(T_product) == 0 {
  462. o.Rollback()
  463. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  464. c.ServeJSON()
  465. return
  466. }
  467. productList := lib.SplitString(T_product, "|")
  468. allProductList := []int{}
  469. for _, v := range productList {
  470. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  471. allProductList = append(allProductList, product_id)
  472. product, _ := Basic.Read_Product_ById(product_id)
  473. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  474. T_relation_sn := strings.Split(v, "-")[2]
  475. if T_relation_sn == "" && product.T_relation_sn == 1 {
  476. o.Rollback()
  477. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  478. c.ServeJSON()
  479. return
  480. }
  481. if len(T_relation_sn) > 0 {
  482. snList := strings.Split(strings.Trim(T_relation_sn, ","), ",")
  483. for _, sn := range snList {
  484. mqtt := Stock.Read_MqttUser(sn)
  485. // 添加设备
  486. device := Stock.Device{
  487. T_contract_number: "",
  488. T_out_number: "",
  489. T_product_id: product_id,
  490. T_in_number: T_number,
  491. T_sn: sn,
  492. T_iccid: mqtt.Iccid,
  493. T_imei: mqtt.Imei,
  494. T_State: 2,
  495. }
  496. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  497. if err != nil {
  498. o.Rollback()
  499. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  500. c.ServeJSON()
  501. return
  502. }
  503. // 添加物联网卡
  504. iotCard := Property.IotCard{
  505. T_iccid: mqtt.Iccid,
  506. T_sn: sn,
  507. T_State: 2, //1-未使用 2-已使用 3-已作废
  508. T_type: "4G",
  509. }
  510. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  511. if err != nil {
  512. o.Rollback()
  513. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  514. c.ServeJSON()
  515. return
  516. }
  517. }
  518. }
  519. stockInProduct := Stock.StockInProduct{
  520. T_number: T_number,
  521. T_product_id: product_id,
  522. T_depot_id: T_depot_id,
  523. T_num: num, // 入库数量
  524. T_date: T_date, // 入库日期
  525. T_relation_sn: T_relation_sn,
  526. }
  527. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  528. if err != nil {
  529. o.Rollback()
  530. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  531. c.ServeJSON()
  532. return
  533. }
  534. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 2)
  535. if err != nil {
  536. o.Rollback()
  537. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  538. c.ServeJSON()
  539. return
  540. }
  541. }
  542. o.Commit()
  543. StockIn_Edit_StockMonth(T_date, T_depot_id, allProductList)
  544. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "入库", var_)
  545. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  546. c.ServeJSON()
  547. return
  548. }
  549. func StockIn_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  550. date, _ := lib.DateStrToTime(T_date)
  551. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  552. // // 修改本月数据不同步库存
  553. // return nil
  554. //}
  555. //T_month := date.Format("2006-01")
  556. months := generateMonthList(date)
  557. o := orm.NewOrm()
  558. StockOutDao := Stock.NewStockOut(o)
  559. StockMonthDao := Stock.NewStockMonth(o)
  560. StockOutProductDao := Stock.NewStockOutProduct(o)
  561. StockInProductDao := Stock.NewStockInProduct(o)
  562. for _, T_product_id := range allProductList {
  563. for _, T_month := range months {
  564. // 获取当前产品本月出入库数量
  565. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  566. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  567. if stockMonth.Id > 0 {
  568. // 修改
  569. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  570. if len(lastMonth) > 0 {
  571. stockMonth.T_beginning = lastMonth[0].T_ending
  572. } else {
  573. stockMonth.T_beginning = 0
  574. }
  575. // 期末库存 = 期初库存+入库-出库
  576. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  577. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_beginning", "T_ending")
  578. if err != nil {
  579. return err
  580. }
  581. } else {
  582. product, _ := Basic.Read_Product_ById(T_product_id)
  583. // 添加库存记录
  584. stockMonth = Stock.StockMonth{
  585. T_depot_id: T_depot_id,
  586. T_product_id: T_product_id,
  587. T_product_class: product.T_class,
  588. T_month: T_month,
  589. }
  590. // 获取当前产品本月出入库数量
  591. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  592. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  593. if len(lastMonth) > 0 {
  594. stockMonth.T_beginning = lastMonth[0].T_ending
  595. } else {
  596. stockMonth.T_beginning = 0
  597. }
  598. // 期末库存 = 期初库存+入库-出库
  599. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  600. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  601. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  602. if err != nil {
  603. return err
  604. }
  605. }
  606. }
  607. }
  608. return nil
  609. }
  610. func (c *StockController) StockIn_Edit() {
  611. T_number := c.GetString("T_number") // 入库单号
  612. //T_depot_id, _ := c.GetInt("T_depot_id")
  613. T_date := c.GetString("T_date")
  614. T_product := c.GetString("T_product")
  615. T_remark := c.GetString("T_remark")
  616. date, is := lib.DateStrToTime(T_date)
  617. if !is {
  618. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  619. c.ServeJSON()
  620. return
  621. }
  622. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  623. T_date = ""
  624. }
  625. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改入库", T_product)
  626. o := orm.NewOrm()
  627. o.Begin()
  628. StockInDao := Stock.NewStockIn(o)
  629. StockInProductDao := Stock.NewStockInProduct(o)
  630. StockDao := Stock.NewStock(o)
  631. DeviceDao := Stock.NewDevice(o)
  632. IotCardDao := Property.NewIotCard(&o)
  633. // 查询入库信息
  634. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  635. T_old_date := stockIn.T_date
  636. if err != nil {
  637. o.Rollback()
  638. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  639. c.ServeJSON()
  640. return
  641. }
  642. // 查询入库产品信息
  643. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  644. var oldProductList []dto.StockProduct
  645. var newProductList []dto.StockProduct
  646. allProductListMap := make(map[int]struct{})
  647. productOldMap := map[int]dto.StockProduct{}
  648. for _, product := range productOldList {
  649. stockProduct := dto.StockProduct{
  650. T_product_id: product.T_product_id,
  651. T_num: product.T_num,
  652. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  653. }
  654. oldProductList = append(oldProductList, stockProduct)
  655. productOldMap[product.T_product_id] = stockProduct
  656. if _, ok := allProductListMap[product.T_product_id]; !ok {
  657. allProductListMap[product.T_product_id] = struct{}{}
  658. }
  659. }
  660. productNewList := lib.SplitString(T_product, "|")
  661. productNewMap := map[int]dto.StockProduct{}
  662. for _, v := range productNewList {
  663. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  664. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  665. T_relation_sn := strings.Split(v, "-")[2]
  666. stockProduct := dto.StockProduct{
  667. T_product_id: product_id,
  668. T_num: num,
  669. T_relation_sn: lib.SplitString(T_relation_sn, ","),
  670. }
  671. newProductList = append(newProductList, stockProduct)
  672. productNewMap[product_id] = stockProduct
  673. if _, ok := allProductListMap[product_id]; !ok {
  674. allProductListMap[product_id] = struct{}{}
  675. }
  676. }
  677. // 判断产品列表信息是否相同
  678. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  679. // 两次提交的数据相同,则不用修改
  680. if !StockProductListIsEqual {
  681. if len(needDelete) > 0 {
  682. // 删除入库产品列表
  683. for _, v := range needDelete {
  684. product, _ := Basic.Read_Product_ById(v.T_product_id)
  685. // 删除入库产品列表
  686. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  687. if err != nil {
  688. o.Rollback()
  689. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  690. c.ServeJSON()
  691. return
  692. }
  693. if product.T_relation_sn == 1 {
  694. // 删除该入库单关联sn
  695. for _, sn := range v.T_relation_sn {
  696. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  697. if err != nil {
  698. o.Rollback()
  699. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  700. c.ServeJSON()
  701. return
  702. }
  703. }
  704. }
  705. // 减少库存
  706. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  707. if err != nil {
  708. o.Rollback()
  709. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  710. c.ServeJSON()
  711. return
  712. }
  713. }
  714. }
  715. if len(needAdd) > 0 {
  716. // 新增入库产品列表
  717. for _, v := range needAdd {
  718. product, _ := Basic.Read_Product_ById(v.T_product_id)
  719. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  720. o.Rollback()
  721. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  722. c.ServeJSON()
  723. return
  724. }
  725. // 添加该入库单关联sn
  726. for _, sn := range v.T_relation_sn {
  727. mqtt := Stock.Read_MqttUser(sn)
  728. // 添加设备
  729. device := Stock.Device{
  730. T_contract_number: "",
  731. T_out_number: "",
  732. T_product_id: v.T_product_id,
  733. T_in_number: stockIn.T_number,
  734. T_sn: sn,
  735. T_iccid: mqtt.Iccid,
  736. T_imei: mqtt.Imei,
  737. T_State: 2,
  738. CreateTime: stockIn.CreateTime,
  739. UpdateTime: stockIn.UpdateTime,
  740. }
  741. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  742. if err != nil {
  743. o.Rollback()
  744. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  745. c.ServeJSON()
  746. return
  747. }
  748. // 添加物联网卡
  749. iotCard := Property.IotCard{
  750. T_iccid: mqtt.Iccid,
  751. T_sn: sn,
  752. T_State: 2, //1-未使用 2-已使用 3-已作废
  753. T_type: "4G",
  754. }
  755. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  756. if err != nil {
  757. o.Rollback()
  758. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  759. c.ServeJSON()
  760. return
  761. }
  762. }
  763. stockInProduct := Stock.StockInProduct{
  764. T_number: T_number,
  765. T_product_id: v.T_product_id,
  766. T_depot_id: stockIn.T_depot_id,
  767. T_num: v.T_num, // 入库数量
  768. T_date: stockIn.T_date, // 入库日期
  769. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  770. }
  771. _, err = StockInProductDao.Add_StockInProduct(stockInProduct)
  772. if err != nil {
  773. o.Rollback()
  774. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  775. c.ServeJSON()
  776. return
  777. }
  778. // 增加库存
  779. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  780. if err != nil {
  781. o.Rollback()
  782. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  783. c.ServeJSON()
  784. return
  785. }
  786. }
  787. }
  788. if len(needEdit) > 0 {
  789. for _, v := range needEdit {
  790. product, _ := Basic.Read_Product_ById(v.T_product_id)
  791. stockInProduct := Stock.StockInProduct{
  792. T_number: stockIn.T_number,
  793. T_product_id: v.T_product_id,
  794. T_depot_id: stockIn.T_depot_id,
  795. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  796. T_date: stockIn.T_date, // 入库日期
  797. }
  798. // 入库数量比之前多,增加库存
  799. var T_type int
  800. var T_num int
  801. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  802. // 增加库存
  803. T_type = 2
  804. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  805. } else {
  806. // 减少库存
  807. T_type = 1
  808. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  809. }
  810. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  811. if err != nil {
  812. o.Rollback()
  813. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  814. c.ServeJSON()
  815. return
  816. }
  817. // 更新产品库存表
  818. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  819. if err != nil {
  820. o.Rollback()
  821. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  822. c.ServeJSON()
  823. return
  824. }
  825. }
  826. }
  827. for _, diff := range snDiff {
  828. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  829. // 删除减少的sn
  830. if len(diff.T_delete_relation_sn) > 0 {
  831. for _, sn := range diff.T_delete_relation_sn {
  832. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  833. if err != nil {
  834. o.Rollback()
  835. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  836. c.ServeJSON()
  837. return
  838. }
  839. }
  840. // 减少库存
  841. _, 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)
  842. if err != nil {
  843. o.Rollback()
  844. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  845. c.ServeJSON()
  846. return
  847. }
  848. }
  849. if len(diff.T_add_relation_sn) > 0 {
  850. for _, sn := range diff.T_add_relation_sn {
  851. mqtt := Stock.Read_MqttUser(sn)
  852. // 添加设备
  853. device := Stock.Device{
  854. T_contract_number: "",
  855. T_out_number: "",
  856. T_product_id: diff.T_product_id,
  857. T_in_number: stockIn.T_number,
  858. T_sn: sn,
  859. T_iccid: mqtt.Iccid,
  860. T_imei: mqtt.Imei,
  861. T_State: 2,
  862. CreateTime: stockIn.CreateTime,
  863. UpdateTime: stockIn.UpdateTime,
  864. }
  865. _, err = DeviceDao.AddOrUpdate_Device(device, 2)
  866. if err != nil {
  867. o.Rollback()
  868. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  869. c.ServeJSON()
  870. return
  871. }
  872. // 添加物联网卡
  873. iotCard := Property.IotCard{
  874. T_iccid: mqtt.Iccid,
  875. T_sn: sn,
  876. T_State: 2, //1-未使用 2-已使用 3-已作废
  877. T_type: "4G",
  878. }
  879. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  880. if err != nil {
  881. o.Rollback()
  882. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  883. c.ServeJSON()
  884. return
  885. }
  886. }
  887. // 增加库存
  888. _, 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)
  889. if err != nil {
  890. o.Rollback()
  891. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  892. c.ServeJSON()
  893. return
  894. }
  895. }
  896. stockInProduct := Stock.StockInProduct{
  897. T_number: stockIn.T_number,
  898. T_product_id: diff.T_product_id,
  899. T_depot_id: stockIn.T_depot_id,
  900. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  901. T_date: stockIn.T_date, // 入库日期
  902. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  903. }
  904. // 更新产品库存表
  905. err = StockInProductDao.Update_StockInProduct(stockInProduct)
  906. if err != nil {
  907. o.Rollback()
  908. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库产品信息失败"}
  909. c.ServeJSON()
  910. return
  911. }
  912. }
  913. }
  914. if len(T_remark) > 0 {
  915. stockIn.T_remark = T_remark
  916. }
  917. if len(T_date) > 0 {
  918. stockIn.T_date = T_date
  919. }
  920. err = StockInDao.Update_StockIn(stockIn, "T_remark", "T_date")
  921. if err != nil {
  922. o.Rollback()
  923. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  924. c.ServeJSON()
  925. return
  926. }
  927. o.Commit()
  928. if len(T_date) > 0 && T_old_date != T_date {
  929. // 修改出库产品日期
  930. StockInProductDao.Update_StockInProduct_T_date(stockIn.T_number, T_date)
  931. DeviceDao.Update_Device_CreateTimeByT_in_number(stockIn.T_number, T_date)
  932. }
  933. // 更新月份统计表
  934. allProductList := []int{}
  935. for i, _ := range allProductListMap {
  936. allProductList = append(allProductList, i)
  937. }
  938. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  939. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "修改", stockIn)
  940. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  941. c.ServeJSON()
  942. return
  943. }
  944. // 删除入库
  945. func (c *StockController) StockIn_Del() {
  946. T_number := c.GetString("T_number") // 入库单号
  947. o := orm.NewOrm()
  948. o.Begin()
  949. StockInDao := Stock.NewStockIn(o)
  950. StockInProductDao := Stock.NewStockInProduct(o)
  951. StockDao := Stock.NewStock(o)
  952. DeviceDao := Stock.NewDevice(o)
  953. // 查询入库信息
  954. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  955. if err != nil {
  956. o.Rollback()
  957. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  958. c.ServeJSON()
  959. return
  960. }
  961. // 查询入库产品信息
  962. productOldList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  963. var oldProductList []dto.StockProduct
  964. allProductList := []int{}
  965. for _, product := range productOldList {
  966. oldProductList = append(oldProductList, dto.StockProduct{
  967. T_product_id: product.T_product_id,
  968. T_num: product.T_num,
  969. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  970. })
  971. allProductList = append(allProductList, product.T_product_id)
  972. }
  973. // 删除入库产品列表
  974. for _, v := range oldProductList {
  975. product, _ := Basic.Read_Product_ById(v.T_product_id)
  976. // 删除入库产品列表
  977. err = StockInProductDao.Delete_StockInProduct(stockIn.T_number, stockIn.T_depot_id, v.T_product_id)
  978. if err != nil {
  979. o.Rollback()
  980. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  981. c.ServeJSON()
  982. return
  983. }
  984. if product.T_relation_sn == 1 {
  985. // 删除该入库单关联sn
  986. for _, sn := range v.T_relation_sn {
  987. err = DeviceDao.Delete_Device_ByT_in_number(stockIn.T_number, sn)
  988. if err != nil {
  989. o.Rollback()
  990. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  991. c.ServeJSON()
  992. return
  993. }
  994. }
  995. }
  996. // 减少库存
  997. _, err = StockDao.AddOrUpdate_Stock(stockIn.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  998. if err != nil {
  999. o.Rollback()
  1000. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1001. c.ServeJSON()
  1002. return
  1003. }
  1004. }
  1005. err = StockInDao.Delete_StockIn(stockIn)
  1006. if err != nil {
  1007. o.Rollback()
  1008. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库失败"}
  1009. c.ServeJSON()
  1010. return
  1011. }
  1012. o.Commit()
  1013. // 更新月份统计表
  1014. StockIn_Edit_StockMonth(stockIn.T_date, stockIn.T_depot_id, allProductList)
  1015. NatsServer.AddUserLogs(c.User.T_uuid, "入库", "删除", stockIn)
  1016. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1017. c.ServeJSON()
  1018. return
  1019. }
  1020. // 导出出库单
  1021. func (c *StockController) StockIn_Excel() {
  1022. // 查询
  1023. T_number := c.GetString("T_number")
  1024. o := orm.NewOrm()
  1025. StockInDao := Stock.NewStockIn(o)
  1026. StockInProductDao := Stock.NewStockInProduct(o)
  1027. stockIn, err := StockInDao.Read_StockIn_ByT_number(T_number)
  1028. if err != nil {
  1029. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1030. c.ServeJSON()
  1031. return
  1032. }
  1033. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  1034. var pList []Stock.StockInProduct_R
  1035. for _, v := range productList {
  1036. pList = append(pList, Stock.StockInProductToStockInProduct_R(v))
  1037. }
  1038. userList, _ := NatsServer.Read_User_List_All()
  1039. Account.Read_User_All_Map(userList)
  1040. stockInDetail := Stock.StockInToStockIn_Detail(stockIn, pList)
  1041. T_date, _ := lib.DateStrToTime(stockIn.T_date)
  1042. filename := fmt.Sprintf("冷链产品入库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1043. f := excelize.NewFile() // 设置单元格的值
  1044. Style1, _ := f.NewStyle(
  1045. &excelize.Style{
  1046. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1047. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1048. })
  1049. Style2, _ := f.NewStyle(
  1050. &excelize.Style{
  1051. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1052. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1053. })
  1054. Style3, _ := f.NewStyle(
  1055. &excelize.Style{
  1056. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1057. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1058. Border: []excelize.Border{
  1059. {Type: "left", Color: "000000", Style: 1},
  1060. {Type: "top", Color: "000000", Style: 1},
  1061. {Type: "bottom", Color: "000000", Style: 1},
  1062. {Type: "right", Color: "000000", Style: 1},
  1063. },
  1064. })
  1065. Style4, _ := f.NewStyle(
  1066. &excelize.Style{
  1067. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1068. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1069. Border: []excelize.Border{
  1070. {Type: "left", Color: "000000", Style: 1},
  1071. {Type: "top", Color: "000000", Style: 1},
  1072. {Type: "bottom", Color: "000000", Style: 1},
  1073. {Type: "right", Color: "000000", Style: 1},
  1074. },
  1075. })
  1076. productList2 := lib.ChunkBy(productList, 10)
  1077. for i, products := range productList2 {
  1078. if len(products) < 10 {
  1079. products = append(products, make([]Stock.StockInProduct, 10-len(products))...)
  1080. }
  1081. row := i*17 + 1
  1082. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1083. f.SetRowStyle("Sheet1", row, row, Style1)
  1084. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1085. f.SetRowHeight("Sheet1", row, 20)
  1086. row += 1
  1087. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1088. f.SetRowStyle("Sheet1", row, row, Style1)
  1089. title := "硬件入库单"
  1090. if len(productList2) > 1 {
  1091. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1092. }
  1093. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  1094. f.SetRowHeight("Sheet1", row, 20)
  1095. row += 1
  1096. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1097. f.SetRowStyle("Sheet1", row, row, Style2)
  1098. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库单号:%s", stockIn.T_number))
  1099. f.SetRowHeight("Sheet1", row, 20)
  1100. row += 1
  1101. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1102. f.SetRowStyle("Sheet1", row, row, Style2)
  1103. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockIn.T_remark))
  1104. f.SetRowHeight("Sheet1", row, 20)
  1105. row += 1
  1106. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1107. f.SetRowStyle("Sheet1", row, row, Style2)
  1108. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("入库时间:%s", T_date.Format("2006年01月02日")))
  1109. f.SetRowHeight("Sheet1", row, 20)
  1110. // 这里设置表头
  1111. row += 1
  1112. f.SetRowHeight("Sheet1", row, 20)
  1113. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  1114. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  1115. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  1116. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  1117. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  1118. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  1119. // 设置列宽
  1120. f.SetColWidth("Sheet1", "A", "A", 5)
  1121. f.SetColWidth("Sheet1", "B", "B", 20)
  1122. f.SetColWidth("Sheet1", "C", "C", 12)
  1123. f.SetColWidth("Sheet1", "D", "D", 10)
  1124. f.SetColWidth("Sheet1", "E", "E", 10)
  1125. f.SetColWidth("Sheet1", "F", "F", 15)
  1126. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  1127. sRow := row
  1128. // 循环写入数据
  1129. for j, v := range products {
  1130. row += 1
  1131. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1132. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  1133. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  1134. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  1135. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  1136. if v.T_num > 0 {
  1137. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  1138. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockIn.T_remark)
  1139. }
  1140. }
  1141. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  1142. row += 1
  1143. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  1144. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  1145. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  1146. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("经办人:%s", stockInDetail.T_submit_name))
  1147. f.SetRowStyle("Sheet1", row, row, Style2)
  1148. f.SetRowHeight("Sheet1", row, 20)
  1149. }
  1150. // 保存文件
  1151. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1152. fmt.Println(err)
  1153. }
  1154. var url string
  1155. //// 上传 OSS
  1156. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1157. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1158. if !is {
  1159. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1160. c.ServeJSON()
  1161. return
  1162. }
  1163. //删除目录
  1164. err = os.Remove("ofile/" + filename + ".xlsx")
  1165. if err != nil {
  1166. logs.Error(lib.FuncName(), err)
  1167. }
  1168. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1169. c.ServeJSON()
  1170. return
  1171. }
  1172. func (c *StockController) StockOut_List() {
  1173. // 分页参数 初始化
  1174. page, _ := c.GetInt("page")
  1175. if page < 1 {
  1176. page = 1
  1177. }
  1178. page_z, _ := c.GetInt("page_z")
  1179. if page_z < 1 {
  1180. page_z = conf.Page_size
  1181. }
  1182. // 查询
  1183. T_depot_id, _ := c.GetInt("T_depot_id")
  1184. T_contract_number := c.GetString("T_contract_number")
  1185. T_start_date := c.GetString("T_start_date")
  1186. T_end_date := c.GetString("T_end_date")
  1187. userList, _ := NatsServer.Read_User_List_All()
  1188. Account.Read_User_All_Map(userList)
  1189. Basic.Read_Depot_All_Map()
  1190. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1191. R_List, R_cnt := StockOutDao.Read_StockOut_List(T_depot_id, T_contract_number, T_start_date, T_end_date, page, page_z)
  1192. var r_jsons lib.R_JSONS
  1193. r_jsons.Num = R_cnt
  1194. r_jsons.Data = R_List
  1195. r_jsons.Page = page
  1196. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1197. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1198. c.ServeJSON()
  1199. return
  1200. }
  1201. func (c *StockController) StockOut_List_Product() {
  1202. // 分页参数 初始化
  1203. page, _ := c.GetInt("page")
  1204. if page < 1 {
  1205. page = 1
  1206. }
  1207. page_z, _ := c.GetInt("page_z")
  1208. if page_z < 1 {
  1209. page_z = conf.Page_size
  1210. }
  1211. // 查询
  1212. T_depot_id, _ := c.GetInt("T_depot_id")
  1213. T_name := c.GetString("T_name")
  1214. T_start_date := c.GetString("T_start_date")
  1215. T_end_date := c.GetString("T_end_date")
  1216. userList, _ := NatsServer.Read_User_List_All()
  1217. Account.Read_User_All_Map(userList)
  1218. Basic.Read_Depot_All_Map()
  1219. StockOutDao := Stock.NewStockOut(orm.NewOrm())
  1220. R_List, R_cnt := StockOutDao.Read_StockOutProduct_List(T_name, T_start_date, T_end_date, T_depot_id, page, page_z)
  1221. var r_jsons lib.R_JSONS
  1222. r_jsons.Num = R_cnt
  1223. r_jsons.Data = R_List
  1224. r_jsons.Page = page
  1225. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1226. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1227. c.ServeJSON()
  1228. return
  1229. }
  1230. func (c *StockController) StockOut_Get() {
  1231. // 查询
  1232. T_number := c.GetString("T_number")
  1233. o := orm.NewOrm()
  1234. StockOutDao := Stock.NewStockOut(o)
  1235. StockOutProductDao := Stock.NewStockOutProduct(o)
  1236. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1237. if err != nil {
  1238. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1239. c.ServeJSON()
  1240. return
  1241. }
  1242. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  1243. var pList []Stock.StockOutProduct_R
  1244. for _, v := range productList {
  1245. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  1246. }
  1247. userList, _ := NatsServer.Read_User_List_All()
  1248. Account.Read_User_All_Map(userList)
  1249. Basic.Read_Depot_All_Map()
  1250. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Stock.StockOutToStockOut_Detail(stockOut, pList)}
  1251. c.ServeJSON()
  1252. return
  1253. }
  1254. func (c *StockController) StockOut_Add() {
  1255. rand_x := 0
  1256. T_number := ""
  1257. o := orm.NewOrm()
  1258. o.Begin()
  1259. StockOutDao := Stock.NewStockOut(o)
  1260. for true {
  1261. T_number = "CK-" + lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x))
  1262. _, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1263. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  1264. break
  1265. }
  1266. rand_x += 1
  1267. }
  1268. T_contract_number := c.GetString("T_contract_number")
  1269. T_depot_id, _ := c.GetInt("T_depot_id")
  1270. T_type, _ := c.GetInt("T_type") // 出库类型 1-领料出库 2-销售出库
  1271. T_date := c.GetString("T_date")
  1272. T_receive := c.GetString("T_receive")
  1273. T_product := c.GetString("T_product")
  1274. T_remark := c.GetString("T_remark")
  1275. T_project := c.GetString("T_project")
  1276. T_delivery_type, _ := c.GetInt("T_delivery_type")
  1277. T_signer_unit := c.GetString("T_signer_unit")
  1278. T_signer := c.GetString("T_signer")
  1279. T_signer_phone := c.GetString("T_signer_phone")
  1280. T_signer_date := c.GetString("T_signer_date")
  1281. T_courier_number := c.GetString("T_courier_number")
  1282. var_ := Stock.StockOut{
  1283. T_number: T_number,
  1284. T_contract_number: T_contract_number,
  1285. T_depot_id: T_depot_id,
  1286. T_type: T_type,
  1287. T_date: T_date,
  1288. T_receive: T_receive,
  1289. T_remark: T_remark,
  1290. T_project: T_project,
  1291. T_submit: c.User.T_uuid,
  1292. // 销售出库
  1293. T_delivery_type: T_delivery_type, // 1-自送 2-自提 3-快递
  1294. T_signer_unit: T_signer_unit,
  1295. T_signer: T_signer,
  1296. T_signer_phone: T_signer_phone,
  1297. T_signer_date: T_signer_date,
  1298. T_courier_number: T_courier_number,
  1299. }
  1300. StockOutProductDao := Stock.NewStockOutProduct(o)
  1301. StockDao := Stock.NewStock(o)
  1302. DeviceDao := Stock.NewDevice(o)
  1303. ContractDao := Contract.NewContract(o)
  1304. ContractProductDao := Contract.NewContractProduct(o)
  1305. _, err := StockOutDao.Add_StockOut(var_)
  1306. if err != nil {
  1307. o.Rollback()
  1308. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  1309. c.ServeJSON()
  1310. return
  1311. }
  1312. // 1、添加出库单
  1313. contract, _ := ContractDao.Read_Contract_ByT_number(T_contract_number)
  1314. if contract.Id == 0 && T_type == 2 {
  1315. o.Rollback()
  1316. c.Data["json"] = lib.JSONS{Code: 203, Msg: "合同编号错误!"}
  1317. c.ServeJSON()
  1318. return
  1319. }
  1320. if len(T_product) == 0 {
  1321. o.Rollback()
  1322. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  1323. c.ServeJSON()
  1324. return
  1325. }
  1326. productList := lib.SplitString(T_product, "|")
  1327. allProductList := []int{}
  1328. for _, v := range productList {
  1329. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  1330. allProductList = append(allProductList, product_id)
  1331. product, _ := Basic.Read_Product_ById(product_id)
  1332. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  1333. T_relation_sn := strings.Split(v, "-")[2]
  1334. if T_relation_sn == "" && product.T_relation_sn == 1 {
  1335. o.Rollback()
  1336. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  1337. c.ServeJSON()
  1338. return
  1339. }
  1340. // 2、更新设备状态为已出库
  1341. if len(T_relation_sn) > 0 {
  1342. snList := strings.Split(T_relation_sn, ",")
  1343. for _, sn := range snList {
  1344. mqtt := Stock.Read_MqttUser(sn)
  1345. device := Stock.Device{
  1346. T_contract_number: T_contract_number,
  1347. T_product_id: product_id,
  1348. T_out_number: T_number,
  1349. T_sn: sn,
  1350. T_iccid: mqtt.Iccid,
  1351. T_imei: mqtt.Imei,
  1352. T_State: 1,
  1353. T_project: T_project,
  1354. }
  1355. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  1356. if err != nil {
  1357. o.Rollback()
  1358. c.Data["json"] = lib.JSONS{Code: 202, Msg: "出库失败"}
  1359. c.ServeJSON()
  1360. return
  1361. }
  1362. }
  1363. }
  1364. // 3、添加出库产品清单
  1365. stockOutProduct := Stock.StockOutProduct{
  1366. T_number: T_number,
  1367. T_product_id: product_id,
  1368. T_depot_id: T_depot_id,
  1369. T_num: num, // 出库数量
  1370. T_date: T_date, // 出库数量
  1371. T_relation_sn: T_relation_sn,
  1372. }
  1373. _, err = StockOutProductDao.Add_StockOutProduct(stockOutProduct)
  1374. if err != nil {
  1375. o.Rollback()
  1376. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  1377. c.ServeJSON()
  1378. return
  1379. }
  1380. // 4、更改合同产品列表清单
  1381. contractProduct, _ := ContractProductDao.Read_ContractProduct_ByT_number_T_product_id(T_contract_number, product_id)
  1382. contractProduct.T_product_out += num
  1383. if contractProduct.T_product_out > contractProduct.T_product_total && T_type == 2 {
  1384. o.Rollback()
  1385. p, _ := Basic.Read_Product_ById(product_id)
  1386. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("【%s】出库数量超过预计出库数量!", p.T_name)}
  1387. c.ServeJSON()
  1388. return
  1389. }
  1390. contractProduct.T_State = 2
  1391. if contractProduct.T_product_out == 0 {
  1392. contractProduct.T_State = 1
  1393. }
  1394. if contractProduct.T_product_out == contractProduct.T_product_total {
  1395. contractProduct.T_State = 3
  1396. }
  1397. err = ContractProductDao.Update_ContractProduct(contractProduct, "T_product_out", "T_State")
  1398. if err != nil {
  1399. o.Rollback()
  1400. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同产品清单失败"}
  1401. c.ServeJSON()
  1402. return
  1403. }
  1404. // 5、更新产品库存列表
  1405. _, err = StockDao.AddOrUpdate_Stock(T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, num, 1)
  1406. if err != nil {
  1407. o.Rollback()
  1408. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存失败"}
  1409. c.ServeJSON()
  1410. return
  1411. }
  1412. }
  1413. o.Commit()
  1414. o2 := orm.NewOrm()
  1415. o2.Begin()
  1416. ContractDao2 := Contract.NewContract(o2)
  1417. ContractProductDao2 := Contract.NewContractProduct(o2)
  1418. // 5、更新合同出库状态
  1419. var T_out int
  1420. if contract.T_State == 1 {
  1421. T_out = 2
  1422. // 查询合同产品清单是否全部都为已出库
  1423. state := ContractProductDao2.Read_ContractProduct_T_State_List(contract.T_number)
  1424. if state == 1 {
  1425. T_out = 1
  1426. }
  1427. if state == 3 {
  1428. T_out = 3
  1429. }
  1430. }
  1431. if T_out != contract.T_out {
  1432. contract.T_out = T_out
  1433. err = ContractDao2.Update_Contract(contract, "T_out")
  1434. if err != nil {
  1435. o2.Rollback()
  1436. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
  1437. c.ServeJSON()
  1438. return
  1439. }
  1440. }
  1441. o2.Commit()
  1442. StockOut_Edit_StockMonth(T_date, T_depot_id, allProductList)
  1443. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "出库", var_)
  1444. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1445. c.ServeJSON()
  1446. return
  1447. }
  1448. func (c *StockController) StockOut_Edit_Delivery() {
  1449. T_number := c.GetString("T_number")
  1450. T_remark := c.GetString("T_remark")
  1451. T_project := c.GetString("T_project")
  1452. T_delivery_type, _ := c.GetInt("T_delivery_type")
  1453. T_signer_unit := c.GetString("T_signer_unit")
  1454. T_signer := c.GetString("T_signer")
  1455. T_signer_phone := c.GetString("T_signer_phone")
  1456. T_signer_date := c.GetString("T_signer_date")
  1457. T_courier_number := c.GetString("T_courier_number")
  1458. o := orm.NewOrm()
  1459. StockOutDao := Stock.NewStockOut(o)
  1460. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1461. if err != nil {
  1462. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库编号错误!"}
  1463. c.ServeJSON()
  1464. return
  1465. }
  1466. if len(T_remark) > 0 {
  1467. stockOut.T_remark = T_remark
  1468. }
  1469. if len(T_project) > 0 {
  1470. stockOut.T_project = T_project
  1471. }
  1472. if T_delivery_type > 0 {
  1473. stockOut.T_delivery_type = T_delivery_type
  1474. }
  1475. if len(T_signer_unit) > 0 {
  1476. stockOut.T_signer_unit = T_signer_unit
  1477. }
  1478. if len(T_signer) > 0 {
  1479. stockOut.T_signer = T_signer
  1480. }
  1481. if len(T_signer_phone) > 0 {
  1482. stockOut.T_signer_phone = T_signer_phone
  1483. }
  1484. if len(T_signer_date) > 0 {
  1485. stockOut.T_signer_date = T_signer_date
  1486. }
  1487. if len(T_courier_number) > 0 {
  1488. stockOut.T_courier_number = T_courier_number
  1489. }
  1490. 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")
  1491. if err != nil {
  1492. o.Rollback()
  1493. c.Data["json"] = lib.JSONS{Code: 203, Msg: "出库失败"}
  1494. c.ServeJSON()
  1495. return
  1496. }
  1497. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改发货单", stockOut)
  1498. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1499. c.ServeJSON()
  1500. return
  1501. }
  1502. func (c *StockController) StockOut_Edit() {
  1503. T_number := c.GetString("T_number") // 出库单号
  1504. //T_depot_id, _ := c.GetInt("T_depot_id")
  1505. T_date := c.GetString("T_date")
  1506. T_receive := c.GetString("T_receive")
  1507. T_project := c.GetString("T_project")
  1508. T_product := c.GetString("T_product")
  1509. T_remark := c.GetString("T_remark")
  1510. date, is := lib.DateStrToTime(T_date)
  1511. if !is {
  1512. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1513. c.ServeJSON()
  1514. return
  1515. }
  1516. if date.Year() != time.Now().Year() && date.Month() != time.Now().Month() {
  1517. T_date = ""
  1518. }
  1519. NatsServer.AddUserLogs(c.User.T_uuid, "仓库管理", "修改出库", T_product)
  1520. o := orm.NewOrm()
  1521. o.Begin()
  1522. StockOutDao := Stock.NewStockOut(o)
  1523. StockOutProductDao := Stock.NewStockOutProduct(o)
  1524. StockDao := Stock.NewStock(o)
  1525. DeviceDao := Stock.NewDevice(o)
  1526. IotCardDao := Property.NewIotCard(&o)
  1527. // 查询入库信息
  1528. StockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1529. T_old_date := StockOut.T_date
  1530. if err != nil {
  1531. o.Rollback()
  1532. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1533. c.ServeJSON()
  1534. return
  1535. }
  1536. // 查询入库产品信息
  1537. productOldList := StockOutProductDao.Read_StockOutProduct_List(StockOut.T_number)
  1538. var oldProductList []dto.StockProduct
  1539. var newProductList []dto.StockProduct
  1540. productOldMap := map[int]dto.StockProduct{}
  1541. allProductListMap := make(map[int]struct{})
  1542. for _, product := range productOldList {
  1543. stockProduct := dto.StockProduct{
  1544. T_product_id: product.T_product_id,
  1545. T_num: product.T_num,
  1546. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1547. }
  1548. oldProductList = append(oldProductList, stockProduct)
  1549. productOldMap[product.T_product_id] = stockProduct
  1550. if _, ok := allProductListMap[product.T_product_id]; !ok {
  1551. allProductListMap[product.T_product_id] = struct{}{}
  1552. }
  1553. }
  1554. productNewList := lib.SplitString(T_product, "|")
  1555. productNewMap := map[int]dto.StockProduct{}
  1556. for _, v := range productNewList {
  1557. product_id, _ := strconv.Atoi(strings.Split(v, "-")[0])
  1558. num, _ := strconv.Atoi(strings.Split(v, "-")[1])
  1559. T_relation_sn := strings.Split(v, "-")[2]
  1560. stockProduct := dto.StockProduct{
  1561. T_product_id: product_id,
  1562. T_num: num,
  1563. T_relation_sn: lib.SplitString(T_relation_sn, ","),
  1564. }
  1565. newProductList = append(newProductList, stockProduct)
  1566. productNewMap[product_id] = stockProduct
  1567. if _, ok := allProductListMap[product_id]; !ok {
  1568. allProductListMap[product_id] = struct{}{}
  1569. }
  1570. }
  1571. // 判断产品列表信息是否相同
  1572. StockProductListIsEqual, needDelete, needAdd, needEdit, snDiff := dto.StockProductListsEqual(oldProductList, newProductList)
  1573. // 两次提交的数据相同,则不用修改
  1574. if !StockProductListIsEqual {
  1575. if len(needDelete) > 0 {
  1576. // 删除入库产品列表
  1577. for _, v := range needDelete {
  1578. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1579. // 删除入库产品列表
  1580. err = StockOutProductDao.Delete_StockOutProduct(StockOut.T_number, StockOut.T_depot_id, v.T_product_id)
  1581. if err != nil {
  1582. o.Rollback()
  1583. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品入库信息失败!"}
  1584. c.ServeJSON()
  1585. return
  1586. }
  1587. if product.T_relation_sn == 1 {
  1588. // 删除该入库单关联sn
  1589. for _, sn := range v.T_relation_sn {
  1590. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  1591. if err != nil {
  1592. o.Rollback()
  1593. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除入库设备失败"}
  1594. c.ServeJSON()
  1595. return
  1596. }
  1597. }
  1598. }
  1599. // 增加库存
  1600. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  1601. if err != nil {
  1602. o.Rollback()
  1603. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1604. c.ServeJSON()
  1605. return
  1606. }
  1607. }
  1608. }
  1609. if len(needAdd) > 0 {
  1610. // 新增入库产品列表
  1611. for _, v := range needAdd {
  1612. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1613. if product.T_relation_sn == 1 && len(v.T_relation_sn) == 0 {
  1614. o.Rollback()
  1615. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s关联SN,请先添加SN!", product.T_name)}
  1616. c.ServeJSON()
  1617. return
  1618. }
  1619. // 添加该入库单关联sn
  1620. for _, sn := range v.T_relation_sn {
  1621. mqtt := Stock.Read_MqttUser(sn)
  1622. // 添加设备
  1623. device := Stock.Device{
  1624. T_contract_number: "",
  1625. T_out_number: StockOut.T_number,
  1626. T_product_id: v.T_product_id,
  1627. T_in_number: "",
  1628. T_sn: sn,
  1629. T_iccid: mqtt.Iccid,
  1630. T_imei: mqtt.Imei,
  1631. T_State: 1,
  1632. CreateTime: StockOut.CreateTime,
  1633. UpdateTime: StockOut.UpdateTime,
  1634. }
  1635. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  1636. if err != nil {
  1637. o.Rollback()
  1638. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  1639. c.ServeJSON()
  1640. return
  1641. }
  1642. // 添加物联网卡
  1643. iotCard := Property.IotCard{
  1644. T_iccid: mqtt.Iccid,
  1645. T_sn: sn,
  1646. T_State: 2, //1-未使用 2-已使用 3-已作废
  1647. T_type: "4G",
  1648. }
  1649. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  1650. if err != nil {
  1651. o.Rollback()
  1652. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  1653. c.ServeJSON()
  1654. return
  1655. }
  1656. }
  1657. StockOutProduct := Stock.StockOutProduct{
  1658. T_number: T_number,
  1659. T_product_id: v.T_product_id,
  1660. T_depot_id: StockOut.T_depot_id,
  1661. T_num: v.T_num, // 入库数量
  1662. T_date: StockOut.T_date, // 入库日期
  1663. T_relation_sn: strings.Join(v.T_relation_sn, ","),
  1664. }
  1665. _, err = StockOutProductDao.Add_StockOutProduct(StockOutProduct)
  1666. if err != nil {
  1667. o.Rollback()
  1668. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1669. c.ServeJSON()
  1670. return
  1671. }
  1672. // 减少库存
  1673. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 1)
  1674. if err != nil {
  1675. o.Rollback()
  1676. c.Data["json"] = lib.JSONS{Code: 203, Msg: "增加库存失败"}
  1677. c.ServeJSON()
  1678. return
  1679. }
  1680. }
  1681. }
  1682. if len(needEdit) > 0 {
  1683. for _, v := range needEdit {
  1684. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1685. StockOutProduct := Stock.StockOutProduct{
  1686. T_number: StockOut.T_number,
  1687. T_product_id: v.T_product_id,
  1688. T_depot_id: StockOut.T_depot_id,
  1689. T_num: productNewMap[v.T_product_id].T_num, // 入库数量
  1690. T_date: StockOut.T_date, // 入库日期
  1691. }
  1692. // 出库数量比之前多,减少库存
  1693. var T_type int
  1694. var T_num int
  1695. if productNewMap[v.T_product_id].T_num > productOldMap[v.T_product_id].T_num {
  1696. // 减少库存
  1697. T_type = 1
  1698. T_num = productNewMap[v.T_product_id].T_num - productOldMap[v.T_product_id].T_num
  1699. } else {
  1700. // 增加库存
  1701. T_type = 2
  1702. T_num = productOldMap[v.T_product_id].T_num - productNewMap[v.T_product_id].T_num
  1703. }
  1704. _, err = StockDao.AddOrUpdate_Stock(StockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, T_num, T_type)
  1705. if err != nil {
  1706. o.Rollback()
  1707. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1708. c.ServeJSON()
  1709. return
  1710. }
  1711. // 更新产品库存表
  1712. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  1713. if err != nil {
  1714. o.Rollback()
  1715. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  1716. c.ServeJSON()
  1717. return
  1718. }
  1719. }
  1720. }
  1721. for _, diff := range snDiff {
  1722. product, _ := Basic.Read_Product_ById(diff.T_product_id)
  1723. // 删除减少的sn
  1724. if len(diff.T_delete_relation_sn) > 0 {
  1725. for _, sn := range diff.T_delete_relation_sn {
  1726. err = DeviceDao.Delete_Device_ByT_out_number(StockOut.T_number, sn)
  1727. if err != nil {
  1728. o.Rollback()
  1729. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1730. c.ServeJSON()
  1731. return
  1732. }
  1733. }
  1734. // 增加库存
  1735. _, 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)
  1736. if err != nil {
  1737. o.Rollback()
  1738. c.Data["json"] = lib.JSONS{Code: 203, Msg: err.Error()}
  1739. c.ServeJSON()
  1740. return
  1741. }
  1742. }
  1743. if len(diff.T_add_relation_sn) > 0 {
  1744. for _, sn := range diff.T_add_relation_sn {
  1745. mqtt := Stock.Read_MqttUser(sn)
  1746. // 添加设备
  1747. device := Stock.Device{
  1748. T_contract_number: "",
  1749. T_out_number: StockOut.T_number,
  1750. T_product_id: diff.T_product_id,
  1751. T_in_number: "",
  1752. T_sn: sn,
  1753. T_iccid: mqtt.Iccid,
  1754. T_imei: mqtt.Imei,
  1755. T_State: 1,
  1756. CreateTime: StockOut.CreateTime,
  1757. UpdateTime: StockOut.UpdateTime,
  1758. }
  1759. _, err = DeviceDao.AddOrUpdate_Device(device, 1)
  1760. if err != nil {
  1761. o.Rollback()
  1762. c.Data["json"] = lib.JSONS{Code: 202, Msg: sn + "入库失败"}
  1763. c.ServeJSON()
  1764. return
  1765. }
  1766. // 添加物联网卡
  1767. iotCard := Property.IotCard{
  1768. T_iccid: mqtt.Iccid,
  1769. T_sn: sn,
  1770. T_State: 2, //1-未使用 2-已使用 3-已作废
  1771. T_type: "4G",
  1772. }
  1773. _, err = IotCardDao.AddOrUpdate_IotCard(iotCard)
  1774. if err != nil {
  1775. o.Rollback()
  1776. c.Data["json"] = lib.JSONS{Code: 202, Msg: "入库失败"}
  1777. c.ServeJSON()
  1778. return
  1779. }
  1780. }
  1781. // 减少库存
  1782. _, 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)
  1783. if err != nil {
  1784. o.Rollback()
  1785. c.Data["json"] = lib.JSONS{Code: 203, Msg: "入库失败"}
  1786. c.ServeJSON()
  1787. return
  1788. }
  1789. }
  1790. StockOutProduct := Stock.StockOutProduct{
  1791. T_number: StockOut.T_number,
  1792. T_product_id: diff.T_product_id,
  1793. T_depot_id: StockOut.T_depot_id,
  1794. T_num: productNewMap[diff.T_product_id].T_num, // 入库数量
  1795. T_date: StockOut.T_date, // 入库日期
  1796. T_relation_sn: strings.Join(productNewMap[diff.T_product_id].T_relation_sn, ","),
  1797. }
  1798. // 更新产品库存表
  1799. err = StockOutProductDao.Update_StockOutProduct(StockOutProduct)
  1800. if err != nil {
  1801. o.Rollback()
  1802. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改出库产品信息失败"}
  1803. c.ServeJSON()
  1804. return
  1805. }
  1806. }
  1807. }
  1808. if len(T_remark) > 0 {
  1809. StockOut.T_remark = T_remark
  1810. }
  1811. if len(T_receive) > 0 {
  1812. StockOut.T_receive = T_receive
  1813. }
  1814. if len(T_project) > 0 {
  1815. StockOut.T_project = T_project
  1816. }
  1817. if len(T_date) > 0 {
  1818. StockOut.T_date = T_date
  1819. }
  1820. err = StockOutDao.Update_StockOut(StockOut, "T_remark", "T_receive", "T_project", "T_date")
  1821. if err != nil {
  1822. o.Rollback()
  1823. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改入库失败"}
  1824. c.ServeJSON()
  1825. return
  1826. }
  1827. o.Commit()
  1828. if len(T_date) > 0 && T_old_date != T_date {
  1829. // 修改出库产品日期
  1830. StockOutProductDao.Update_StockOutProduct_T_date(StockOut.T_number, T_date)
  1831. DeviceDao.Update_Device_CreateTimeByT_out_number(StockOut.T_number, T_date)
  1832. }
  1833. if len(T_project) > 0 {
  1834. DeviceDao.Update_Device_ByT_out_number_T_project(StockOut.T_number, T_project)
  1835. }
  1836. // 更新月份统计表
  1837. allProductList := []int{}
  1838. for i, _ := range allProductListMap {
  1839. allProductList = append(allProductList, i)
  1840. }
  1841. StockOut_Edit_StockMonth(StockOut.T_date, StockOut.T_depot_id, allProductList)
  1842. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "修改", StockOut)
  1843. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1844. c.ServeJSON()
  1845. return
  1846. }
  1847. // 删除入库
  1848. func (c *StockController) StockOut_Del() {
  1849. T_number := c.GetString("T_number") // 入库单号
  1850. o := orm.NewOrm()
  1851. o.Begin()
  1852. StockOutDao := Stock.NewStockOut(o)
  1853. StockOutProductDao := Stock.NewStockOutProduct(o)
  1854. StockDao := Stock.NewStock(o)
  1855. DeviceDao := Stock.NewDevice(o)
  1856. // 查询入库信息
  1857. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1858. if err != nil {
  1859. o.Rollback()
  1860. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1861. c.ServeJSON()
  1862. return
  1863. }
  1864. // 查询出库产品信息
  1865. productOldList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  1866. var oldProductList []dto.StockProduct
  1867. allProductList := []int{}
  1868. for _, product := range productOldList {
  1869. oldProductList = append(oldProductList, dto.StockProduct{
  1870. T_product_id: product.T_product_id,
  1871. T_num: product.T_num,
  1872. T_relation_sn: lib.SplitString(product.T_relation_sn, ","),
  1873. })
  1874. allProductList = append(allProductList, product.T_product_id)
  1875. }
  1876. // 删除入库产品列表
  1877. for _, v := range oldProductList {
  1878. product, _ := Basic.Read_Product_ById(v.T_product_id)
  1879. // 删除入库产品列表
  1880. err = StockOutProductDao.Delete_StockOutProduct(stockOut.T_number, stockOut.T_depot_id, v.T_product_id)
  1881. if err != nil {
  1882. o.Rollback()
  1883. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除产品出库信息失败!"}
  1884. c.ServeJSON()
  1885. return
  1886. }
  1887. if product.T_relation_sn == 1 {
  1888. // 删除该入库单关联sn
  1889. for _, sn := range v.T_relation_sn {
  1890. err = DeviceDao.Delete_Device_ByT_out_number(stockOut.T_number, sn)
  1891. if err != nil {
  1892. o.Rollback()
  1893. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库设备失败"}
  1894. c.ServeJSON()
  1895. return
  1896. }
  1897. }
  1898. }
  1899. // 增加库存
  1900. _, err = StockDao.AddOrUpdate_Stock(stockOut.T_depot_id, product.Id, product.T_class, product.T_name, product.T_model, v.T_num, 2)
  1901. if err != nil {
  1902. o.Rollback()
  1903. c.Data["json"] = lib.JSONS{Code: 203, Msg: "更新库存信息失败"}
  1904. c.ServeJSON()
  1905. return
  1906. }
  1907. }
  1908. err = StockOutDao.Delete_StockOut(stockOut)
  1909. if err != nil {
  1910. o.Rollback()
  1911. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除出库失败"}
  1912. c.ServeJSON()
  1913. return
  1914. }
  1915. o.Commit()
  1916. // 更新月份统计表
  1917. StockOut_Edit_StockMonth(stockOut.T_date, stockOut.T_depot_id, allProductList)
  1918. NatsServer.AddUserLogs(c.User.T_uuid, "出库", "删除", stockOut)
  1919. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  1920. c.ServeJSON()
  1921. return
  1922. }
  1923. // 导出出库单
  1924. func (c *StockController) StockOut_Excel() {
  1925. // 查询
  1926. T_number := c.GetString("T_number")
  1927. o := orm.NewOrm()
  1928. StockOutDao := Stock.NewStockOut(o)
  1929. StockOutProductDao := Stock.NewStockOutProduct(o)
  1930. stockOut, err := StockOutDao.Read_StockOut_ByT_number(T_number)
  1931. if err != nil {
  1932. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1933. c.ServeJSON()
  1934. return
  1935. }
  1936. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  1937. //productList = append(productList, productList...)
  1938. //productList = append(productList, productList...)
  1939. //productList = append(productList, productList...)
  1940. var pList []Stock.StockOutProduct_R
  1941. for _, v := range productList {
  1942. pList = append(pList, Stock.StockOutProductToStockOutProduct_R(v))
  1943. }
  1944. userList, _ := NatsServer.Read_User_List_All()
  1945. Account.Read_User_All_Map(userList)
  1946. stockOutDetail := Stock.StockOutToStockOut_Detail(stockOut, pList)
  1947. T_date, _ := lib.DateStrToTime(stockOut.T_date)
  1948. filename := fmt.Sprintf("冷链产品出库单(%s)", lib.GetRandstring(6, "0123456789", 0))
  1949. f := excelize.NewFile() // 设置单元格的值
  1950. Style1, _ := f.NewStyle(
  1951. &excelize.Style{
  1952. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1953. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1954. })
  1955. Style2, _ := f.NewStyle(
  1956. &excelize.Style{
  1957. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1958. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  1959. })
  1960. Style3, _ := f.NewStyle(
  1961. &excelize.Style{
  1962. Font: &excelize.Font{Size: 10, Family: "宋体", Bold: true},
  1963. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1964. Border: []excelize.Border{
  1965. {Type: "left", Color: "000000", Style: 1},
  1966. {Type: "top", Color: "000000", Style: 1},
  1967. {Type: "bottom", Color: "000000", Style: 1},
  1968. {Type: "right", Color: "000000", Style: 1},
  1969. },
  1970. })
  1971. Style4, _ := f.NewStyle(
  1972. &excelize.Style{
  1973. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1974. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1975. Border: []excelize.Border{
  1976. {Type: "left", Color: "000000", Style: 1},
  1977. {Type: "top", Color: "000000", Style: 1},
  1978. {Type: "bottom", Color: "000000", Style: 1},
  1979. {Type: "right", Color: "000000", Style: 1},
  1980. },
  1981. })
  1982. productList2 := lib.ChunkBy(productList, 10)
  1983. for i, products := range productList2 {
  1984. if len(products) < 10 {
  1985. products = append(products, make([]Stock.StockOutProduct, 10-len(products))...)
  1986. }
  1987. row := i*17 + 1
  1988. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1989. f.SetRowStyle("Sheet1", row, row, Style1)
  1990. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("宝智达科技产品进销存统计表"))
  1991. f.SetRowHeight("Sheet1", row, 20)
  1992. row += 1
  1993. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  1994. f.SetRowStyle("Sheet1", row, row, Style1)
  1995. title := "硬件出库单"
  1996. if len(productList2) > 1 {
  1997. title += fmt.Sprintf(" %d/%d", i+1, len(productList2))
  1998. }
  1999. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), title)
  2000. f.SetRowHeight("Sheet1", row, 20)
  2001. row += 1
  2002. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2003. f.SetRowStyle("Sheet1", row, row, Style2)
  2004. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("出库单号:%s", stockOut.T_number))
  2005. f.SetRowHeight("Sheet1", row, 20)
  2006. row += 1
  2007. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2008. f.SetRowStyle("Sheet1", row, row, Style2)
  2009. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("项目名称:%s", stockOut.T_project))
  2010. f.SetRowHeight("Sheet1", row, 20)
  2011. row += 1
  2012. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row))
  2013. f.SetRowStyle("Sheet1", row, row, Style2)
  2014. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("领用时间:%s", T_date.Format("2006年01月02日")))
  2015. f.SetRowHeight("Sheet1", row, 20)
  2016. // 这里设置表头
  2017. row += 1
  2018. f.SetRowHeight("Sheet1", row, 20)
  2019. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), "序号")
  2020. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), "产品名称")
  2021. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), "型号")
  2022. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), "单位")
  2023. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), "领用数量")
  2024. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), "备注")
  2025. // 设置列宽
  2026. f.SetColWidth("Sheet1", "A", "A", 5)
  2027. f.SetColWidth("Sheet1", "B", "B", 20)
  2028. f.SetColWidth("Sheet1", "C", "C", 12)
  2029. f.SetColWidth("Sheet1", "D", "D", 10)
  2030. f.SetColWidth("Sheet1", "E", "E", 10)
  2031. f.SetColWidth("Sheet1", "F", "F", 15)
  2032. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("F%d", row), Style3)
  2033. sRow := row
  2034. // 循环写入数据
  2035. for j, v := range products {
  2036. row += 1
  2037. product, _ := Basic.Read_Product_ById(v.T_product_id)
  2038. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), j+1)
  2039. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), product.T_name)
  2040. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), product.T_model)
  2041. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), product.T_spec)
  2042. if v.T_num > 0 {
  2043. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), v.T_num)
  2044. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", row), stockOut.T_remark)
  2045. }
  2046. }
  2047. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", sRow), fmt.Sprintf("F%d", row), Style4)
  2048. row += 1
  2049. f.MergeCell("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("C%d", row))
  2050. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), fmt.Sprintf("制单:%s", c.User.T_name))
  2051. f.MergeCell("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("F%d", row))
  2052. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), fmt.Sprintf("领用人:%s", stockOutDetail.T_receive_name))
  2053. f.SetRowStyle("Sheet1", row, row, Style2)
  2054. f.SetRowHeight("Sheet1", row, 20)
  2055. }
  2056. // 保存文件
  2057. if err = f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  2058. fmt.Println(err)
  2059. }
  2060. var url string
  2061. //// 上传 OSS
  2062. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  2063. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  2064. if !is {
  2065. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  2066. c.ServeJSON()
  2067. return
  2068. }
  2069. //删除目录
  2070. err = os.Remove("ofile/" + filename + ".xlsx")
  2071. if err != nil {
  2072. logs.Error(lib.FuncName(), err)
  2073. }
  2074. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  2075. c.ServeJSON()
  2076. return
  2077. }
  2078. func StockOut_Edit_StockMonth(T_date string, T_depot_id int, allProductList []int) error {
  2079. date, _ := lib.DateStrToTime(T_date)
  2080. //if date.Year() == time.Now().Year() && date.Month() == time.Now().Month() {
  2081. // // 修改本月数据不同步库存
  2082. // return nil
  2083. //}
  2084. //T_month := date.Format("2006-01")
  2085. months := generateMonthList(date)
  2086. o := orm.NewOrm()
  2087. StockOutDao := Stock.NewStockOut(o)
  2088. StockMonthDao := Stock.NewStockMonth(o)
  2089. StockOutProductDao := Stock.NewStockOutProduct(o)
  2090. StockInProductDao := Stock.NewStockInProduct(o)
  2091. for _, T_product_id := range allProductList {
  2092. for _, T_month := range months {
  2093. // 获取当前产品本月出入库数量
  2094. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  2095. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id, T_month)
  2096. if stockMonth.Id > 0 {
  2097. // 修改
  2098. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  2099. if len(lastMonth) > 0 {
  2100. stockMonth.T_beginning = lastMonth[0].T_ending
  2101. } else {
  2102. stockMonth.T_beginning = 0
  2103. }
  2104. // 期末库存 = 期初库存+入库-出库
  2105. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2106. err := StockMonthDao.Update_StockMonth(stockMonth, "T_out", "T_beginning", "T_ending")
  2107. if err != nil {
  2108. return err
  2109. }
  2110. } else {
  2111. product, _ := Basic.Read_Product_ById(T_product_id)
  2112. // 添加库存记录
  2113. stockMonth = Stock.StockMonth{
  2114. T_depot_id: T_depot_id,
  2115. T_product_id: T_product_id,
  2116. T_product_class: product.T_class,
  2117. T_month: T_month,
  2118. }
  2119. // 获取当前产品本月出入库数量
  2120. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(T_depot_id, T_product_id, T_month)
  2121. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(T_depot_id, T_product_id, T_month)
  2122. if len(lastMonth) > 0 {
  2123. stockMonth.T_beginning = lastMonth[0].T_ending
  2124. } else {
  2125. stockMonth.T_beginning = 0
  2126. }
  2127. // 期末库存 = 期初库存+入库-出库
  2128. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2129. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(T_depot_id, T_product_id, T_month)
  2130. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  2131. if err != nil {
  2132. return err
  2133. }
  2134. }
  2135. }
  2136. }
  2137. return nil
  2138. }
  2139. func Cron_StockMonth() {
  2140. //创建一个定时任务对象
  2141. c := cron.New(cron.WithSeconds())
  2142. //给对象增加定时任务
  2143. // @monthly 每月运行一次,每月第一天午夜 0 0 0 1 * *
  2144. //c.AddFunc("0 */1 * * * ?", Cron_StockMonth_Add)
  2145. c.AddFunc("@monthly", Cron_StockMonth_Add)
  2146. //启动定时任务
  2147. c.Start()
  2148. defer c.Stop()
  2149. //查询语句,阻塞,让main函数不退出,保持程序运行
  2150. select {}
  2151. }
  2152. // 保存每月入库出库明细
  2153. func Cron_StockMonth_Add() {
  2154. T_month := time.Now().Format("2006-01")
  2155. //T_month := time.Now().AddDate(0, -1, 0).Format("2006-01")
  2156. logs.Info("开始统计" + T_month + "库存明细数据")
  2157. o := orm.NewOrm()
  2158. StockDao := Stock.NewStock(o)
  2159. StockOutDao := Stock.NewStockOut(o)
  2160. StockMonthDao := Stock.NewStockMonth(o)
  2161. StockOutProductDao := Stock.NewStockOutProduct(o)
  2162. StockInProductDao := Stock.NewStockInProduct(o)
  2163. stockList, _ := StockDao.Read_Stock_List(0, 0, "", "", 0, 9999)
  2164. for _, stock := range stockList {
  2165. lastMonth := StockMonthDao.Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  2166. stockMonth := StockMonthDao.Read_StockMonth_ByT_depot_id_T_product_id(stock.T_depot_id, stock.T_product_id, T_month)
  2167. if stockMonth.Id > 0 {
  2168. // 修改
  2169. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2170. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2171. if len(lastMonth) > 0 {
  2172. stockMonth.T_beginning = lastMonth[0].T_ending
  2173. } else {
  2174. stockMonth.T_beginning = 0
  2175. }
  2176. // 期末库存 = 期初库存+入库-出库
  2177. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2178. err := StockMonthDao.Update_StockMonth(stockMonth, "T_in", "T_out", "T_beginning", "T_ending")
  2179. if err != nil {
  2180. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  2181. }
  2182. } else {
  2183. stockMonth = Stock.StockMonth{
  2184. T_depot_id: stock.T_depot_id,
  2185. T_product_id: stock.T_product_id,
  2186. T_product_class: stock.T_product_class,
  2187. T_month: T_month,
  2188. }
  2189. // 获取当前产品本月出入库数量
  2190. stockMonth.T_in = StockInProductDao.Read_StockIn_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2191. stockMonth.T_out = StockOutProductDao.Read_StockOut_Total(stock.T_depot_id, stock.T_product_id, T_month)
  2192. if len(lastMonth) > 0 {
  2193. stockMonth.T_beginning = lastMonth[0].T_ending
  2194. } else {
  2195. stockMonth.T_beginning = 0
  2196. }
  2197. // 期末库存 = 期初库存+入库-出库
  2198. stockMonth.T_ending = stockMonth.T_beginning + stockMonth.T_in - stockMonth.T_out
  2199. stockMonth.T_project = StockOutDao.Read_StockOut_T_contract_number(stock.T_depot_id, stock.T_product_id, T_month)
  2200. _, err := StockMonthDao.Add_StockMonth(stockMonth)
  2201. if err != nil {
  2202. NatsServer.AddSysLogs("库存明细统计", fmt.Sprintf("库存明细统计失败%s:%d:%d", T_month, stock.T_depot_id, stock.T_product_id), stockMonth)
  2203. }
  2204. }
  2205. }
  2206. }
  2207. // 重写设备出入库记录
  2208. func Cron_Device_Add() {
  2209. logs.Info("开始统计写入设备出入库数据")
  2210. o := orm.NewOrm()
  2211. o.Begin()
  2212. DeviceDao := Stock.NewDevice(o)
  2213. StockOutDao := Stock.NewStockOut(o)
  2214. StockInDao := Stock.NewStockIn(o)
  2215. StockOutProductDao := Stock.NewStockOutProduct(o)
  2216. StockInProductDao := Stock.NewStockInProduct(o)
  2217. // 添加入库设备信息
  2218. StockInList, _ := StockInDao.Read_StockIn_List(0, "", "", 0, 9999)
  2219. for _, stockIn := range StockInList {
  2220. productList := StockInProductDao.Read_StockInProduct_List_ByT_number(stockIn.T_number)
  2221. for _, product := range productList {
  2222. if len(product.T_relation_sn) == 0 {
  2223. continue
  2224. }
  2225. for _, sn := range lib.SplitString(product.T_relation_sn, ",") {
  2226. mqtt := Stock.Read_MqttUser(sn)
  2227. // 添加设备
  2228. device := Stock.Device{
  2229. T_contract_number: "",
  2230. T_out_number: "",
  2231. T_product_id: product.T_product_id,
  2232. T_in_number: stockIn.T_number,
  2233. T_sn: sn,
  2234. T_iccid: mqtt.Iccid,
  2235. T_imei: mqtt.Imei,
  2236. T_State: 2,
  2237. CreateTime: product.CreateTime,
  2238. UpdateTime: product.UpdateTime,
  2239. }
  2240. _, err := DeviceDao.AddOrUpdate_Device(device, 2)
  2241. if err != nil {
  2242. o.Rollback()
  2243. return
  2244. }
  2245. }
  2246. }
  2247. }
  2248. // 添加出库设备信息
  2249. StockOutList, _ := StockOutDao.Read_StockOut_List(0, "", "", "", 0, 9999)
  2250. for _, stockOut := range StockOutList {
  2251. productList := StockOutProductDao.Read_StockOutProduct_List(stockOut.T_number)
  2252. for _, product := range productList {
  2253. if len(product.T_relation_sn) == 0 {
  2254. continue
  2255. }
  2256. for _, sn := range lib.SplitString(product.T_relation_sn, ",") {
  2257. mqtt := Stock.Read_MqttUser(sn)
  2258. // 添加设备
  2259. device := Stock.Device{
  2260. T_contract_number: stockOut.T_contract_number,
  2261. T_product_id: product.T_product_id,
  2262. T_out_number: stockOut.T_number,
  2263. T_sn: sn,
  2264. T_iccid: mqtt.Iccid,
  2265. T_imei: mqtt.Imei,
  2266. T_State: 1,
  2267. T_project: stockOut.T_project,
  2268. CreateTime: product.CreateTime,
  2269. UpdateTime: product.UpdateTime,
  2270. }
  2271. _, err := DeviceDao.AddOrUpdate_Device(device, 1)
  2272. if err != nil {
  2273. o.Rollback()
  2274. return
  2275. }
  2276. }
  2277. }
  2278. }
  2279. o.Commit()
  2280. }
  2281. func generateMonthList(startMonth time.Time) []string {
  2282. var months []string
  2283. endMonth := time.Now().AddDate(0, 0, 0)
  2284. for month := startMonth; month.Before(endMonth); month = month.AddDate(0, 1, 0) {
  2285. fmt.Println("=========================", month.Format("2006-01"))
  2286. months = append(months, month.Format("2006-01"))
  2287. }
  2288. return months
  2289. }