Validation.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. package controllers
  2. import (
  3. "ERP_storage/Nats/NatsServer"
  4. "ERP_storage/conf"
  5. "ERP_storage/logs"
  6. "ERP_storage/models/Account"
  7. "ERP_storage/models/Basic"
  8. "ERP_storage/models/Stock"
  9. "ERP_storage/models/validationtool"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. "github.com/beego/beego/v2/adapter/orm"
  14. beego "github.com/beego/beego/v2/server/web"
  15. "github.com/xuri/excelize/v2"
  16. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  17. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  18. "math"
  19. "net/url"
  20. "os"
  21. "strconv"
  22. "strings"
  23. "time"
  24. )
  25. type ValidationController struct {
  26. beego.Controller
  27. User userlibs.User
  28. }
  29. func (c *ValidationController) Prepare() {
  30. c.User = *Account.User_r
  31. }
  32. func (c *ValidationController) AddValidation() {
  33. var ValidationList []validationtool.AddValidationTool
  34. errs := json.Unmarshal(c.Ctx.Input.RequestBody, &ValidationList)
  35. if errs != nil {
  36. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "json 序列化失败!", Data: nil}
  37. c.ServeJSON()
  38. return
  39. }
  40. o := orm.NewOrm()
  41. o.Begin() // 开始事务
  42. vali := validationtool.NewValidationTool(o)
  43. valiRecord := validationtool.NewValidationToolRecord(o)
  44. BatchNumber := time.Now().Format("2006-01-02 15:04:05")
  45. for _, tool := range ValidationList {
  46. if len(tool.T_sn) == 0 {
  47. o.Rollback() // 回滚事务
  48. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "T_sn不能为空!", Data: nil}
  49. c.ServeJSON()
  50. return
  51. }
  52. validation, err := vali.ReadValidationBytSn(tool.T_sn)
  53. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  54. o.Rollback() // 回滚事务
  55. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "查询失败!", Data: nil}
  56. c.ServeJSON()
  57. return
  58. } else if errors.Is(err, orm.ErrNoRows) {
  59. var validataiontool validationtool.ValidationTool
  60. user := Stock.Read_MqttUser(tool.T_sn)
  61. validataiontool.T_iccid = user.Iccid
  62. validataiontool.T_imei = user.Imei
  63. validataiontool.T_uuid = c.User.T_uuid
  64. validataiontool.Validationnumber = tool.Validationnumber
  65. validataiontool.T_sn = tool.T_sn
  66. validataiontool.T_remark = tool.T_remark
  67. validataiontool.T_state = validationtool.ValidationToolStateStockIn
  68. validataiontool.T_class = tool.T_class
  69. validataiontool.UpdateTime = time.Now()
  70. _, err = vali.ADDValidationTool(validataiontool)
  71. if err != nil {
  72. o.Rollback() // 回滚事务
  73. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "入库失败!", Data: nil}
  74. c.ServeJSON()
  75. return
  76. }
  77. _, err = valiRecord.ADD(validataiontool, BatchNumber)
  78. if err != nil {
  79. o.Rollback() // 回滚事务
  80. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "保存历史记录失败!", Data: nil}
  81. c.ServeJSON()
  82. return
  83. }
  84. }
  85. //修改设备状态
  86. validation.T_state = validationtool.ValidationToolStateStockIn
  87. cols := []string{"T_state"}
  88. if len(tool.T_remark) != 0 {
  89. validation.T_remark = tool.T_remark
  90. cols = append(cols, "T_remark")
  91. } else {
  92. validation.T_remark = ""
  93. cols = append(cols, "T_remark")
  94. }
  95. if len(tool.Validationnumber) != 0 {
  96. validation.Validationnumber = tool.Validationnumber
  97. cols = append(cols, "validationnumber")
  98. }
  99. _, err = valiRecord.ADD(validation, BatchNumber)
  100. if err != nil {
  101. o.Rollback() // 回滚事务
  102. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "保存历史记录失败!", Data: nil}
  103. c.ServeJSON()
  104. return
  105. }
  106. validation.LendUser = ""
  107. cols = append(cols, "LendUser")
  108. validation.T_project = ""
  109. cols = append(cols, "T_project")
  110. err = vali.UpdateValidationTool(validation, cols...)
  111. if err != nil {
  112. o.Rollback() // 回滚事务
  113. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "入库失败!", Data: nil}
  114. c.ServeJSON()
  115. return
  116. }
  117. }
  118. o.Commit() // 提交事务
  119. NatsServer.AddUserLogs(c.User.T_uuid, "验证工具", "添加", ValidationList)
  120. c.Data["json"] = lib.JSONS{Code: 200, Msg: "入库成功!", Data: nil}
  121. c.ServeJSON()
  122. return
  123. }
  124. func (c *ValidationController) ValidationList() {
  125. // 分页参数 初始化
  126. page, _ := c.GetInt("page")
  127. if page < 1 {
  128. page = 1
  129. }
  130. page_z, _ := c.GetInt("page_z")
  131. if page_z < 1 {
  132. page_z = conf.Page_size
  133. }
  134. T_state := c.GetString("T_state")
  135. validationnumber := c.GetString("Validationnumber")
  136. T_sn := c.GetString("T_sn")
  137. t_iccid := c.GetString("t_iccid")
  138. t_imei := c.GetString("t_imei")
  139. LendUser := c.GetString("LendUser")
  140. T_project := c.GetString("T_project")
  141. T_class := c.GetString("T_class")
  142. vali := validationtool.NewValidationTool(orm.NewOrm())
  143. R_List, R_cnt := vali.Read_Validation_List(validationnumber, T_sn, t_iccid, t_imei, T_state, LendUser, T_project, T_class, page, page_z)
  144. var r_jsons lib.R_JSONS
  145. r_jsons.Num = R_cnt
  146. r_jsons.Data = R_List
  147. r_jsons.Page = page
  148. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  149. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  150. c.ServeJSON()
  151. return
  152. }
  153. func (c *ValidationController) DeleteValidationTool() {
  154. id := c.GetString("t_sn")
  155. if len(id) <= 0 {
  156. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "t_sn不能为空!", Data: nil}
  157. c.ServeJSON()
  158. return
  159. }
  160. vali := validationtool.NewValidationTool(orm.NewOrm())
  161. err := vali.DeleteValidationTool(id)
  162. if err != nil {
  163. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "删除失败!", Data: nil}
  164. c.ServeJSON()
  165. return
  166. }
  167. c.Data["json"] = lib.JSONS{Code: 200, Msg: "删除成功!", Data: nil}
  168. c.ServeJSON()
  169. }
  170. // 出库
  171. func (c *ValidationController) UpdateValidationTool() {
  172. var lendValidationList []validationtool.LendValidationTool
  173. err := json.Unmarshal(c.Ctx.Input.RequestBody, &lendValidationList)
  174. if err != nil {
  175. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "json 序列化失败!", Data: nil}
  176. c.ServeJSON()
  177. return
  178. }
  179. o := orm.NewOrm()
  180. o.Begin()
  181. vali := validationtool.NewValidationTool(o)
  182. valiRecord := validationtool.NewValidationToolRecord(o)
  183. BatchNumber := time.Now().Format("2006-01-02 15:04:05")
  184. for _, tool := range lendValidationList {
  185. if len(tool.T_sn) <= 0 {
  186. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "T_sn不能为空!", Data: nil}
  187. c.ServeJSON()
  188. return
  189. }
  190. validation, err2 := vali.ReadValidationBytSn(tool.T_sn)
  191. sprintf := fmt.Sprintf("当前sn:%v 未入库", tool.T_sn)
  192. if err2 != nil || validation.T_state != validationtool.ValidationToolStateStockIn {
  193. o.Rollback()
  194. c.Data["json"] = lib.JSONS{Code: 1201, Msg: sprintf, Data: nil}
  195. c.ServeJSON()
  196. return
  197. }
  198. if validation.T_state == validationtool.ValidationToolStateStockOut {
  199. continue
  200. }
  201. //修改设备状态
  202. validation.T_state = validationtool.ValidationToolStateStockOut
  203. cols := []string{"t_state"}
  204. if len(tool.T_remark) != 0 {
  205. validation.T_remark = tool.T_remark
  206. cols = append(cols, "T_remark")
  207. }
  208. if len(tool.Validationnumber) != 0 {
  209. validation.Validationnumber = tool.Validationnumber
  210. cols = append(cols, "validationnumber")
  211. }
  212. if len(tool.LendUser) != 0 {
  213. validation.LendUser = tool.LendUser
  214. cols = append(cols, "LendUser")
  215. }
  216. if len(tool.T_project) != 0 {
  217. validation.T_project = tool.T_project
  218. cols = append(cols, "T_project")
  219. }
  220. err = vali.UpdateValidationTool(validation, cols...)
  221. if err != nil {
  222. o.Rollback()
  223. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "修改失败!", Data: nil}
  224. c.ServeJSON()
  225. return
  226. }
  227. _, err = valiRecord.ADD(validation, BatchNumber)
  228. if err != nil {
  229. o.Rollback() // 回滚事务
  230. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "保存历史记录失败!", Data: nil}
  231. c.ServeJSON()
  232. return
  233. }
  234. }
  235. err = o.Commit() // 提交事务
  236. if err != nil {
  237. o.Rollback() // 回滚事务
  238. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "提交事务失败!", Data: nil}
  239. c.ServeJSON()
  240. return
  241. }
  242. NatsServer.AddUserLogs(c.User.T_uuid, "验证工具", "出库", lendValidationList)
  243. c.Data["json"] = lib.JSONS{Code: 200, Msg: "修改成功!", Data: nil}
  244. c.ServeJSON()
  245. return
  246. }
  247. // 报废
  248. func (c *ValidationController) ScrapValidationTool() {
  249. var lendValidationList []validationtool.LendValidationTool
  250. err := json.Unmarshal(c.Ctx.Input.RequestBody, &lendValidationList)
  251. if err != nil {
  252. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "json 序列化失败!", Data: nil}
  253. c.ServeJSON()
  254. return
  255. }
  256. o := orm.NewOrm()
  257. vali := validationtool.NewValidationTool(o)
  258. valiRecord := validationtool.NewValidationToolRecord(o)
  259. BatchNumber := time.Now().Format("2006-01-02 15:04:05")
  260. o.Begin()
  261. for _, tool := range lendValidationList {
  262. if len(tool.T_sn) <= 0 {
  263. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "T_sn不能为空!", Data: nil}
  264. c.ServeJSON()
  265. return
  266. }
  267. validation, errs := vali.ReadValidationBytSn(tool.T_sn)
  268. if errs != nil {
  269. o.Rollback()
  270. if errs.Error() == orm.ErrNoRows.Error() {
  271. c.Data["json"] = lib.JSONS{Code: 1201, Msg: fmt.Sprintf("当前sn:%v 未入库", tool.T_sn), Data: nil}
  272. c.ServeJSON()
  273. return
  274. } else {
  275. c.Data["json"] = lib.JSONS{Code: 1201, Msg: fmt.Sprintf("查询sn:%v 失败", tool.T_sn), Data: nil}
  276. c.ServeJSON()
  277. return
  278. }
  279. }
  280. //修改设备状态
  281. validation.T_state = validationtool.ValidationToolStateStockScrap
  282. cols := []string{"t_state"}
  283. if len(tool.T_remark) != 0 {
  284. validation.T_remark = tool.T_remark
  285. cols = append(cols, "T_remark")
  286. }
  287. if len(tool.Validationnumber) != 0 {
  288. validation.Validationnumber = tool.Validationnumber
  289. cols = append(cols, "validationnumber")
  290. }
  291. if len(tool.LendUser) != 0 {
  292. validation.LendUser = tool.LendUser
  293. cols = append(cols, "LendUser")
  294. }
  295. if len(tool.T_project) != 0 {
  296. validation.T_project = tool.T_project
  297. cols = append(cols, "T_project")
  298. }
  299. err = vali.UpdateValidationTool(validation, cols...)
  300. if err != nil {
  301. o.Rollback()
  302. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "修改失败!", Data: nil}
  303. c.ServeJSON()
  304. return
  305. }
  306. _, err = valiRecord.ADD(validation, BatchNumber)
  307. if err != nil {
  308. o.Rollback() // 回滚事务
  309. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "保存历史记录失败!", Data: nil}
  310. c.ServeJSON()
  311. return
  312. }
  313. }
  314. err = o.Commit() // 提交事务
  315. if err != nil {
  316. o.Rollback() // 回滚事务
  317. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "提交事务失败!", Data: nil}
  318. c.ServeJSON()
  319. return
  320. }
  321. NatsServer.AddUserLogs(c.User.T_uuid, "验证工具", "报废", lendValidationList)
  322. c.Data["json"] = lib.JSONS{Code: 200, Msg: "提交成功!", Data: nil}
  323. c.ServeJSON()
  324. return
  325. }
  326. // 维修
  327. func (c *ValidationController) RepairValidationTool() {
  328. var lendValidationList []validationtool.LendValidationTool
  329. err := json.Unmarshal(c.Ctx.Input.RequestBody, &lendValidationList)
  330. if err != nil {
  331. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "json 序列化失败!", Data: nil}
  332. c.ServeJSON()
  333. return
  334. }
  335. o := orm.NewOrm()
  336. o.Begin()
  337. vali := validationtool.NewValidationTool(o)
  338. valiRecord := validationtool.NewValidationToolRecord(o)
  339. BatchNumber := time.Now().Format("2006-01-02 15:04:05")
  340. for _, tool := range lendValidationList {
  341. if len(tool.T_sn) <= 0 {
  342. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "T_sn不能为空!", Data: nil}
  343. c.ServeJSON()
  344. return
  345. }
  346. validation, errs := vali.ReadValidationBytSn(tool.T_sn)
  347. if errs != nil {
  348. o.Rollback()
  349. if errs.Error() == orm.ErrNoRows.Error() {
  350. c.Data["json"] = lib.JSONS{Code: 1201, Msg: fmt.Sprintf("当前sn:%v 未入库", tool.T_sn), Data: nil}
  351. c.ServeJSON()
  352. return
  353. } else {
  354. c.Data["json"] = lib.JSONS{Code: 1201, Msg: fmt.Sprintf("查询sn:%v 失败", tool.T_sn), Data: nil}
  355. c.ServeJSON()
  356. return
  357. }
  358. }
  359. //修改设备状态
  360. validation.T_state = validationtool.ValidationToolStateStockRepair
  361. cols := []string{"t_state"}
  362. if len(tool.T_remark) != 0 {
  363. validation.T_remark = tool.T_remark
  364. cols = append(cols, "T_remark")
  365. }
  366. if len(tool.Validationnumber) != 0 {
  367. validation.Validationnumber = tool.Validationnumber
  368. cols = append(cols, "validationnumber")
  369. }
  370. if len(tool.LendUser) != 0 {
  371. validation.LendUser = tool.LendUser
  372. cols = append(cols, "LendUser")
  373. }
  374. if len(tool.T_project) != 0 {
  375. validation.T_project = tool.T_project
  376. cols = append(cols, "T_project")
  377. }
  378. err = vali.UpdateValidationTool(validation, cols...)
  379. if err != nil {
  380. o.Rollback()
  381. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "修改失败!", Data: nil}
  382. c.ServeJSON()
  383. return
  384. }
  385. _, err = valiRecord.ADD(validation, BatchNumber)
  386. if err != nil {
  387. o.Rollback() // 回滚事务
  388. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "保存历史记录失败!", Data: nil}
  389. c.ServeJSON()
  390. return
  391. }
  392. }
  393. err = o.Commit() // 提交事务
  394. if err != nil {
  395. o.Rollback() // 回滚事务
  396. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "提交事务失败!", Data: nil}
  397. c.ServeJSON()
  398. return
  399. }
  400. NatsServer.AddUserLogs(c.User.T_uuid, "验证工具", "维修", lendValidationList)
  401. c.Data["json"] = lib.JSONS{Code: 200, Msg: "提交成功!", Data: nil}
  402. c.ServeJSON()
  403. return
  404. }
  405. // GetValidationToolBySn 根据sn获取设备信息
  406. func (c *ValidationController) GetValidationToolBySn() {
  407. sn := c.GetString("sn")
  408. if len(sn) <= 0 {
  409. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "sn不能为空!", Data: nil}
  410. c.ServeJSON()
  411. return
  412. }
  413. vali := validationtool.NewValidationTool(orm.NewOrm())
  414. r, err := vali.ReadValidationBytSn(sn)
  415. if err != nil {
  416. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "当前设备不存在!", Data: nil}
  417. c.ServeJSON()
  418. return
  419. }
  420. c.Data["json"] = lib.JSONS{Code: 200, Msg: "成功!", Data: r}
  421. c.ServeJSON()
  422. }
  423. // UpdateValidationToolBySn 根据sn修改设备信息
  424. func (c *ValidationController) UpdateValidationToolBySn() {
  425. sn := c.GetString("T_sn")
  426. Id := c.GetString("Id")
  427. Validationnumber := c.GetString("Validationnumber")
  428. T_remark := c.GetString("T_remark")
  429. T_state, _ := c.GetInt("T_state")
  430. T_class, _ := c.GetInt("T_class")
  431. if len(sn) <= 0 {
  432. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "sn不能为空!", Data: nil}
  433. c.ServeJSON()
  434. return
  435. }
  436. if len(Id) <= 0 {
  437. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "Id不能为空!", Data: nil}
  438. c.ServeJSON()
  439. return
  440. }
  441. vali := validationtool.NewValidationTool(orm.NewOrm())
  442. valiRecord := validationtool.NewValidationToolRecord(orm.NewOrm())
  443. r, err := vali.ReadValidationById(Id)
  444. if err != nil {
  445. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "当前设备不存在!", Data: nil}
  446. c.ServeJSON()
  447. return
  448. }
  449. cols := []string{}
  450. if len(Validationnumber) > 0 {
  451. r.Validationnumber = Validationnumber
  452. cols = append(cols, "Validationnumber")
  453. }
  454. if len(T_remark) > 0 {
  455. r.T_remark = T_remark
  456. cols = append(cols, "T_remark")
  457. }
  458. if T_state > 0 {
  459. r.T_state = T_state
  460. cols = append(cols, "T_state")
  461. }
  462. if T_class > 0 {
  463. r.T_class = T_class
  464. cols = append(cols, "T_class")
  465. }
  466. if !strings.EqualFold(r.T_sn, sn) {
  467. r.T_sn = sn
  468. user := Stock.Read_MqttUser(sn)
  469. r.T_iccid = user.Iccid
  470. r.T_imei = user.Imei
  471. cols = append(cols, "T_sn", "T_iccid", "T_imei")
  472. }
  473. err = vali.UpdateValidationTool(r, cols...)
  474. if err != nil {
  475. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "当前设备不存在!", Data: nil}
  476. c.ServeJSON()
  477. return
  478. }
  479. BatchNumber := time.Now().Format("2006-01-02 15:04:05")
  480. _, err = valiRecord.ADD(r, BatchNumber)
  481. if err != nil {
  482. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "保存历史记录失败!", Data: nil}
  483. c.ServeJSON()
  484. return
  485. }
  486. NatsServer.AddUserLogs(c.User.T_uuid, "验证工具", "通过sn修改验证工具", r)
  487. c.Data["json"] = lib.JSONS{Code: 200, Msg: "成功!", Data: nil}
  488. c.ServeJSON()
  489. }
  490. func (c *ValidationController) ImportExcel() {
  491. // 获取上传的文件
  492. vali := validationtool.NewValidationTool(orm.NewOrm())
  493. f, _, err := c.GetFile("excelFile")
  494. if err != nil {
  495. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "读取文件失败!", Data: nil}
  496. c.ServeJSON()
  497. return
  498. }
  499. defer f.Close()
  500. // 读取 Excel 文件
  501. reader, err := excelize.OpenReader(f)
  502. if err != nil {
  503. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "打开文件失败!", Data: nil}
  504. c.ServeJSON()
  505. return
  506. }
  507. defer func() {
  508. if err := reader.Close(); err != nil {
  509. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "关闭文件失败!", Data: nil}
  510. c.ServeJSON()
  511. return
  512. }
  513. }()
  514. // 读取工作表中的数据
  515. rows, err := reader.GetRows("Sheet1")
  516. if err != nil {
  517. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "读取文件失败!", Data: nil}
  518. c.ServeJSON()
  519. return
  520. }
  521. for i, row := range rows {
  522. if i == 0 {
  523. continue // 忽略第一排
  524. }
  525. if len(row) >= 4 {
  526. atoi, _ := strconv.Atoi(row[2])
  527. isout, _ := strconv.Atoi(row[3])
  528. user := Stock.Read_MqttUser(row[0])
  529. tool := validationtool.ValidationTool{
  530. T_sn: row[0],
  531. T_class: atoi,
  532. Validationnumber: row[1],
  533. T_uuid: c.User.T_uuid,
  534. T_state: isout,
  535. T_iccid: user.Iccid,
  536. T_imei: user.Imei,
  537. }
  538. _, err := vali.ADDValidationTool(tool)
  539. if err != nil {
  540. sprintf := fmt.Sprintf("设备sn添加失败%v", row[0])
  541. c.Data["json"] = lib.JSONS{Code: 1201, Msg: sprintf, Data: nil}
  542. c.ServeJSON()
  543. return
  544. }
  545. }
  546. }
  547. c.Data["json"] = lib.JSONS{Code: 200, Msg: "成功!", Data: nil}
  548. c.ServeJSON()
  549. }
  550. func (c *ValidationController) ExportValidationListExcel() {
  551. // 分页参数 初始化
  552. page, _ := c.GetInt("page")
  553. if page < 1 {
  554. page = 1
  555. }
  556. page_z, _ := c.GetInt("page_z")
  557. if page_z < 1 {
  558. page_z = 9999
  559. }
  560. T_state := c.GetString("T_state")
  561. validationnumber := c.GetString("validationnumber")
  562. T_sn := c.GetString("T_sn")
  563. t_iccid := c.GetString("t_iccid")
  564. t_imei := c.GetString("t_imei")
  565. LendUser := c.GetString("LendUser")
  566. T_project := c.GetString("T_project")
  567. T_class := c.GetString("T_class")
  568. vali := validationtool.NewValidationTool(orm.NewOrm())
  569. R_List, _ := vali.Read_Validation_List(validationnumber, T_sn, t_iccid, t_imei, T_state, LendUser, T_project, T_class, page, page_z)
  570. f := excelize.NewFile() //设置单元格值
  571. // 这里设置表头ÒÒ
  572. f.SetCellValue("Sheet1", "A1", "序号")
  573. f.SetCellValue("Sheet1", "B1", "SN")
  574. f.SetCellValue("Sheet1", "C1", "编号")
  575. f.SetCellValue("Sheet1", "D1", "模组imei")
  576. f.SetCellValue("Sheet1", "E1", "物联网卡号")
  577. f.SetCellValue("Sheet1", "F1", "设备类型")
  578. f.SetCellValue("Sheet1", "G1", "借出人")
  579. f.SetCellValue("Sheet1", "H1", "借出项目")
  580. f.SetCellValue("Sheet1", "I1", "备注")
  581. // 设置列宽
  582. f.SetColWidth("Sheet1", "A", "A", 6)
  583. f.SetColWidth("Sheet1", "B", "B", 20)
  584. f.SetColWidth("Sheet1", "C", "C", 15)
  585. f.SetColWidth("Sheet1", "D", "D", 15)
  586. f.SetColWidth("Sheet1", "E", "E", 30)
  587. f.SetColWidth("Sheet1", "F", "F", 15)
  588. f.SetColWidth("Sheet1", "G", "G", 15)
  589. f.SetColWidth("Sheet1", "H", "H", 30)
  590. f.SetColWidth("Sheet1", "I", "J", 15)
  591. line := 1
  592. for i, v := range R_List {
  593. line++
  594. r, _ := Basic.Read_ProductClass_ById(v.T_class)
  595. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  596. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_sn)
  597. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.Validationnumber)
  598. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_imei)
  599. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_iccid)
  600. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), r.T_name)
  601. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.LendUser)
  602. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_project)
  603. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.T_remark)
  604. }
  605. timeStr := time.Now().Format("20060102150405")
  606. fileName := fmt.Sprintf("验证工具记录表%v.xlsx", timeStr)
  607. filePath := "ofile/" + "验证工具记录表" + timeStr + ".xlsx"
  608. // 保存文件
  609. if err := f.SaveAs(filePath); err != nil {
  610. logs.Error("文件失败:", err)
  611. }
  612. defer func() {
  613. os.Remove(filePath)
  614. }()
  615. c.Ctx.Output.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  616. c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName))
  617. c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
  618. c.Ctx.Output.Download(filePath, fileName)
  619. }
  620. func (c *ValidationController) Read_ValidationClass_List() {
  621. // 分页参数 初始化
  622. page, _ := c.GetInt("page")
  623. if page < 1 {
  624. page = 1
  625. }
  626. page_z, _ := c.GetInt("page_z")
  627. if page_z < 1 {
  628. page_z = conf.Page_size
  629. }
  630. // 查询
  631. T_name := c.GetString("T_name")
  632. R_List, R_cnt := validationtool.Read_ValidationToolClass_List(T_name, page, page_z)
  633. var r_jsons lib.R_JSONS
  634. r_jsons.Num = R_cnt
  635. r_jsons.Data = R_List
  636. r_jsons.Page = page
  637. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  638. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  639. c.ServeJSON()
  640. return
  641. }
  642. // 统计验证工具
  643. func (c *ValidationController) Validation_stat() {
  644. // 分页参数 初始化
  645. page, _ := c.GetInt("page")
  646. if page < 1 {
  647. page = 1
  648. }
  649. page_z, _ := c.GetInt("page_z")
  650. if page_z < 1 {
  651. page_z = conf.Page_size
  652. }
  653. LendUser := c.GetString("LendUser")
  654. T_project := c.GetString("T_project")
  655. vali := validationtool.NewValidationTool(orm.NewOrm())
  656. R_List, R_cnt := vali.Validation_stat(LendUser, T_project, page, page_z)
  657. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: lib.R_JSONS{Data: R_List, Num: R_cnt}}
  658. c.ServeJSON()
  659. return
  660. }
  661. func (c *ValidationController) User_List() {
  662. var r_jsons lib.R_JSONS
  663. // 分页参数 初始化
  664. page, _ := c.GetInt("page")
  665. if page < 1 {
  666. page = 1
  667. }
  668. page_z, _ := c.GetInt("page_z")
  669. if page_z < 1 {
  670. page_z = conf.Page_size
  671. }
  672. T_name := c.GetString("T_name")
  673. vali := validationtool.NewValidationTool(orm.NewOrm())
  674. R_List, R_cnt := vali.Read_lend_user_List(T_name, page, page_z)
  675. for i := 0; i < len(R_List); i++ {
  676. if R_List[i].LendUser == "" {
  677. R_List[i].LendUser = "未出库"
  678. }
  679. }
  680. r_jsons.Num = R_cnt
  681. r_jsons.Data = R_List
  682. r_jsons.Page = page
  683. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  684. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  685. c.ServeJSON()
  686. return
  687. }
  688. func (c *ValidationController) Record_List() {
  689. // 分页参数 初始化
  690. page, _ := c.GetInt("page")
  691. if page < 1 {
  692. page = 1
  693. }
  694. page_z, _ := c.GetInt("page_z")
  695. if page_z < 1 {
  696. page_z = conf.Page_size
  697. }
  698. T_state := c.GetString("T_state")
  699. validationnumber := c.GetString("Validationnumber")
  700. T_sn := c.GetString("T_sn")
  701. t_iccid := c.GetString("t_iccid")
  702. t_imei := c.GetString("t_imei")
  703. LendUser := c.GetString("LendUser")
  704. T_project := c.GetString("T_project")
  705. T_class := c.GetString("T_class")
  706. valiRecord := validationtool.NewValidationToolRecord(orm.NewOrm())
  707. R_List, R_cnt := valiRecord.Read_Validation_List(validationnumber, T_sn, t_iccid, t_imei, T_state, LendUser, T_project, T_class, page, page_z)
  708. var r_jsons lib.R_JSONS
  709. r_jsons.Num = R_cnt
  710. r_jsons.Data = R_List
  711. r_jsons.Page = page
  712. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  713. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  714. c.ServeJSON()
  715. return
  716. }
  717. func (c *ValidationController) Operation_List() {
  718. // 分页参数 初始化
  719. page, _ := c.GetInt("page")
  720. if page < 1 {
  721. page = 1
  722. }
  723. page_z, _ := c.GetInt("page_z")
  724. if page_z < 1 {
  725. page_z = conf.Page_size
  726. }
  727. T_state := c.GetString("T_state")
  728. T_sn := c.GetString("T_sn")
  729. LendUser := c.GetString("LendUser")
  730. T_project := c.GetString("T_project")
  731. valiRecord := validationtool.NewValidationToolRecord(orm.NewOrm())
  732. R_List, R_cnt := valiRecord.Read_Operation_List(T_sn, T_state, LendUser, T_project, page, page_z)
  733. var r_jsons lib.R_JSONS
  734. r_jsons.Num = R_cnt
  735. r_jsons.Data = R_List
  736. r_jsons.Page = page
  737. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  738. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  739. c.ServeJSON()
  740. return
  741. }
  742. // 下载操作记录
  743. func (c *ValidationController) Operation_Excel() {
  744. T_state := c.GetString("T_state")
  745. T_sn := c.GetString("T_sn")
  746. LendUser := c.GetString("LendUser")
  747. T_project := c.GetString("T_project")
  748. valiRecord := validationtool.NewValidationToolRecord(orm.NewOrm())
  749. R_List, _ := valiRecord.Read_Operation_List(T_sn, T_state, LendUser, T_project, 0, 9999)
  750. f := excelize.NewFile() //设置单元格值
  751. // 这里设置表头ÒÒ
  752. Style2, _ := f.NewStyle(
  753. &excelize.Style{
  754. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  755. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  756. Border: []excelize.Border{
  757. {Type: "left", Color: "000000", Style: 1},
  758. {Type: "top", Color: "000000", Style: 1},
  759. {Type: "bottom", Color: "000000", Style: 1},
  760. {Type: "right", Color: "000000", Style: 1},
  761. },
  762. })
  763. Style4, _ := f.NewStyle(
  764. &excelize.Style{
  765. Font: &excelize.Font{Size: 10, Family: "宋体"},
  766. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  767. Border: []excelize.Border{
  768. {Type: "left", Color: "000000", Style: 1},
  769. {Type: "top", Color: "000000", Style: 1},
  770. {Type: "bottom", Color: "000000", Style: 1},
  771. {Type: "right", Color: "000000", Style: 1},
  772. },
  773. })
  774. f.SetCellValue("Sheet1", "A1", "序号")
  775. f.SetCellValue("Sheet1", "B1", "操作时间")
  776. f.SetCellValue("Sheet1", "C1", "操作")
  777. f.SetCellValue("Sheet1", "D1", "借出(归还)人")
  778. f.SetCellValue("Sheet1", "E1", "关联项目")
  779. f.SetCellValue("Sheet1", "F1", "备注")
  780. f.SetCellValue("Sheet1", "G1", "设备数量")
  781. f.SetCellValue("Sheet1", "H1", "SN关联项目")
  782. f.SetCellValue("Sheet1", "I1", "SN数量")
  783. f.SetCellValue("Sheet1", "J1", "SN")
  784. // 这里设置表头
  785. f.SetCellStyle("Sheet1", "A1", "J1", Style2)
  786. f.SetRowHeight("Sheet1", 2, 20)
  787. // 设置列宽
  788. f.SetColWidth("Sheet1", "A", "A", 6)
  789. f.SetColWidth("Sheet1", "B", "B", 20)
  790. f.SetColWidth("Sheet1", "C", "C", 15)
  791. f.SetColWidth("Sheet1", "D", "D", 15)
  792. f.SetColWidth("Sheet1", "E", "E", 20)
  793. f.SetColWidth("Sheet1", "F", "F", 20)
  794. f.SetColWidth("Sheet1", "G", "G", 12)
  795. f.SetColWidth("Sheet1", "H", "H", 20)
  796. f.SetColWidth("Sheet1", "I", "I", 15)
  797. f.SetColWidth("Sheet1", "J", "J", 15)
  798. line := 1
  799. for i, v := range R_List {
  800. startRow := line + 1
  801. for _, sn := range v.T_sn_List {
  802. startRow2 := line + 1
  803. for _, v2 := range sn.T_sn {
  804. line++
  805. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v2)
  806. }
  807. f.MergeCell("Sheet1", fmt.Sprintf("H%d", startRow2), fmt.Sprintf("H%d", line))
  808. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), sn.T_project)
  809. f.MergeCell("Sheet1", fmt.Sprintf("I%d", startRow2), fmt.Sprintf("I%d", line))
  810. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), len(sn.T_sn))
  811. }
  812. f.MergeCell("Sheet1", fmt.Sprintf("A%d", startRow), fmt.Sprintf("A%d", line))
  813. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  814. f.MergeCell("Sheet1", fmt.Sprintf("B%d", startRow), fmt.Sprintf("B%d", line))
  815. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.BatchNumber)
  816. var state string
  817. switch v.T_state {
  818. case 1:
  819. state = "出库"
  820. case 2:
  821. state = "入库"
  822. case 3:
  823. state = "维修"
  824. case 4:
  825. state = "报废"
  826. case 5:
  827. state = "损坏"
  828. }
  829. f.MergeCell("Sheet1", fmt.Sprintf("C%d", startRow), fmt.Sprintf("C%d", line))
  830. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), state)
  831. f.MergeCell("Sheet1", fmt.Sprintf("D%d", startRow), fmt.Sprintf("D%d", line))
  832. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.LendUser)
  833. f.MergeCell("Sheet1", fmt.Sprintf("E%d", startRow), fmt.Sprintf("E%d", line))
  834. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), strings.Join(v.T_project, "\r\n"))
  835. f.MergeCell("Sheet1", fmt.Sprintf("F%d", startRow), fmt.Sprintf("F%d", line))
  836. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_remark)
  837. f.MergeCell("Sheet1", fmt.Sprintf("G%d", startRow), fmt.Sprintf("G%d", line))
  838. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_sn_quantity)
  839. }
  840. f.SetCellStyle("Sheet1", "A2", fmt.Sprintf("J%d", line), Style4)
  841. timeStr := time.Now().Format("20060102150405")
  842. fileName := fmt.Sprintf("验证工具操作记录表%v.xlsx", timeStr)
  843. filePath := "ofile/" + "验证工具操作记录表" + timeStr + ".xlsx"
  844. // 保存文件
  845. if err := f.SaveAs(filePath); err != nil {
  846. logs.Error("保存文件失败:", err)
  847. }
  848. defer func() {
  849. os.Remove(filePath)
  850. }()
  851. c.Ctx.Output.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
  852. c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fileName))
  853. c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
  854. c.Ctx.Output.Download(filePath, fileName)
  855. }
  856. // 设备转移请求结构体
  857. type TransferValidationToolRequest struct {
  858. T_sn []string `json:"T_sn"`
  859. CurrentUser string `json:"CurrentUser"`
  860. TargetUser string `json:"TargetUser"`
  861. TransferRemark string `json:"TransferRemark"` // 转移备注
  862. }
  863. // 设备转移结果结构体
  864. type TransferResult struct {
  865. T_sn string `json:"T_sn"`
  866. Success bool `json:"success"`
  867. Message string `json:"message"`
  868. Status string `json:"status"`
  869. }
  870. // 检查所有转移结果是否都失败
  871. func allFailed(results []TransferResult) bool {
  872. for _, r := range results {
  873. if r.Success {
  874. return false
  875. }
  876. }
  877. return true
  878. }
  879. // 检查是否有任何转移结果成功
  880. func anySuccess(results []TransferResult) bool {
  881. for _, r := range results {
  882. if r.Success {
  883. return true
  884. }
  885. }
  886. return false
  887. }
  888. // 设备转移请求
  889. func (c *ValidationController) TransferValidationTool() {
  890. var request TransferValidationToolRequest
  891. err := json.Unmarshal(c.Ctx.Input.RequestBody, &request)
  892. if err != nil {
  893. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "json 序列化失败!", Data: nil}
  894. c.ServeJSON()
  895. return
  896. }
  897. // 验证参数
  898. if len(request.T_sn) <= 0 {
  899. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "T_sn不能为空!", Data: nil}
  900. c.ServeJSON()
  901. return
  902. }
  903. if len(request.TargetUser) <= 0 {
  904. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "目标借出用户不能为空!", Data: nil}
  905. c.ServeJSON()
  906. return
  907. }
  908. if request.CurrentUser == request.TargetUser {
  909. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "目标用户不能与当前用户相同!", Data: nil}
  910. c.ServeJSON()
  911. return
  912. }
  913. o := orm.NewOrm()
  914. o.Begin()
  915. vali := validationtool.NewValidationTool(o)
  916. valiRecord := validationtool.NewValidationToolRecord(o)
  917. BatchNumber := time.Now().Format("2006-01-02 15:04:05")
  918. var results []TransferResult
  919. // 遍历所有SN进行检查和更新
  920. for _, sn := range request.T_sn {
  921. result := TransferResult{T_sn: sn, Success: false, Status: "failed"}
  922. validation, err2 := vali.ReadValidationBytSn(sn)
  923. if err2 != nil {
  924. if err2.Error() == orm.ErrNoRows.Error() {
  925. result.Message = fmt.Sprintf("当前sn:%v 未入库", sn)
  926. } else {
  927. result.Message = fmt.Sprintf("查询sn:%v 失败", sn)
  928. }
  929. results = append(results, result)
  930. continue
  931. }
  932. // 检查设备当前状态是否为已出库
  933. if validation.T_state != validationtool.ValidationToolStateStockOut {
  934. result.Message = fmt.Sprintf("当前sn:%v 未出库,无法进行转移", sn)
  935. results = append(results, result)
  936. continue
  937. }
  938. // 检查当前借出用户是否匹配
  939. if validation.LendUser != request.CurrentUser {
  940. result.Message = fmt.Sprintf("当前sn:%v 的借出用户与请求不符", sn)
  941. results = append(results, result)
  942. continue
  943. }
  944. // 检查设备是否已经在转移中
  945. if validation.T_state == validationtool.ValidationToolStateTransferring {
  946. result.Message = fmt.Sprintf("当前sn:%v 正在转移中,请等待对方确认或取消当前转移", sn)
  947. results = append(results, result)
  948. continue
  949. }
  950. // 修改设备状态和备注,记录转移信息
  951. originalRemark := validation.T_remark
  952. transferInfo := fmt.Sprintf("[转移请求] 从 %s 转移至 %s", request.CurrentUser, request.TargetUser)
  953. if len(request.TransferRemark) > 0 {
  954. transferInfo += fmt.Sprintf(",备注:%s", request.TransferRemark)
  955. }
  956. if len(originalRemark) > 0 {
  957. validation.T_remark = originalRemark + "\n" + transferInfo
  958. } else {
  959. validation.T_remark = transferInfo
  960. }
  961. // 设置转移中的状态
  962. validation.T_state = validationtool.ValidationToolStateTransferring
  963. cols := []string{"T_remark", "T_state"}
  964. err = vali.UpdateValidationTool(validation, cols...)
  965. if err != nil {
  966. result.Message = fmt.Sprintf("更新设备sn:%v 状态失败", sn)
  967. results = append(results, result)
  968. continue
  969. }
  970. _, err = valiRecord.ADD(validation, BatchNumber)
  971. if err != nil {
  972. result.Message = fmt.Sprintf("保存sn:%v 历史记录失败", sn)
  973. results = append(results, result)
  974. continue
  975. }
  976. result.Success = true
  977. result.Status = "pending"
  978. result.Message = fmt.Sprintf("sn:%v 转移请求已发送", sn)
  979. results = append(results, result)
  980. }
  981. if anySuccess(results) {
  982. // 创建转移记录到新表
  983. snListJSON, err := json.Marshal(request.T_sn)
  984. if err != nil {
  985. o.Rollback()
  986. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "T_sn列表序列化失败!", Data: nil}
  987. c.ServeJSON()
  988. return
  989. }
  990. transferRecord := validationtool.ValidationToolTransfer{
  991. T_sn: string(snListJSON),
  992. FromUser: request.CurrentUser,
  993. ToUser: request.TargetUser,
  994. Status: "pending",
  995. Remark: request.TransferRemark,
  996. }
  997. transferTool := validationtool.NewValidationToolTransfer(o)
  998. _, err = transferTool.Add(&transferRecord)
  999. if err != nil {
  1000. o.Rollback()
  1001. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "创建转移记录失败!", Data: nil}
  1002. c.ServeJSON()
  1003. return
  1004. }
  1005. err = o.Commit() // 提交事务
  1006. if err != nil {
  1007. o.Rollback() // 回滚事务
  1008. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "提交事务失败!", Data: nil}
  1009. c.ServeJSON()
  1010. return
  1011. }
  1012. NatsServer.AddUserLogs(c.User.T_uuid, "验证工具", "转移请求", request.T_sn)
  1013. c.Data["json"] = lib.JSONS{Code: 200, Msg: "批量转移请求处理完成", Data: results}
  1014. c.ServeJSON()
  1015. return
  1016. }
  1017. o.Rollback() // 如果没有一个成功,则回滚所有操作
  1018. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "所有SN转移失败", Data: results}
  1019. c.ServeJSON()
  1020. return
  1021. }
  1022. // 确认接收设备请求结构体
  1023. type ConfirmTransferValidationTool struct {
  1024. T_sn []string `json:"T_sn"`
  1025. ConfirmUser string `json:"ConfirmUser"` // 确认接收的用户
  1026. // 添加设备信息字段用于比对
  1027. DeviceInfo map[string]interface{} `json:"DeviceInfo"` // 设备信息用于比对
  1028. }
  1029. // 设备一致性比对结果结构体
  1030. type DeviceComparisonResult struct {
  1031. Field string `json:"field"`
  1032. Expected string `json:"expected"`
  1033. Actual string `json:"actual"`
  1034. }
  1035. // 确认接收设备
  1036. func (c *ValidationController) ConfirmTransferValidationTool() {
  1037. var request ConfirmTransferValidationTool
  1038. err := json.Unmarshal(c.Ctx.Input.RequestBody, &request)
  1039. if err != nil {
  1040. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "json 序列化失败!", Data: nil}
  1041. c.ServeJSON()
  1042. return
  1043. }
  1044. // 验证参数
  1045. if len(request.T_sn) <= 0 {
  1046. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "T_sn不能为空!", Data: nil}
  1047. c.ServeJSON()
  1048. return
  1049. }
  1050. if len(request.ConfirmUser) <= 0 {
  1051. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "确认用户不能为空!", Data: nil}
  1052. c.ServeJSON()
  1053. return
  1054. }
  1055. o := orm.NewOrm()
  1056. o.Begin()
  1057. vali := validationtool.NewValidationTool(o)
  1058. valiRecord := validationtool.NewValidationToolRecord(o)
  1059. BatchNumber := time.Now().Format("2006-01-02 15:04:05")
  1060. // 查询设备信息
  1061. validation, err2 := vali.ReadValidationBytSn(request.T_sn)
  1062. if err2 != nil {
  1063. o.Rollback()
  1064. if err2.Error() == orm.ErrNoRows.Error() {
  1065. c.Data["json"] = lib.JSONS{Code: 1201, Msg: fmt.Sprintf("当前sn:%v 未入库", request.T_sn), Data: nil}
  1066. c.ServeJSON()
  1067. return
  1068. } else {
  1069. c.Data["json"] = lib.JSONS{Code: 1201, Msg: fmt.Sprintf("查询sn:%v 失败", request.T_sn), Data: nil}
  1070. c.ServeJSON()
  1071. return
  1072. }
  1073. }
  1074. // 检查设备是否在转移中
  1075. if validation.T_state != validationtool.ValidationToolStateTransferring {
  1076. o.Rollback()
  1077. c.Data["json"] = lib.JSONS{Code: 1201, Msg: fmt.Sprintf("当前sn:%v 不在转移状态", request.T_sn), Data: nil}
  1078. c.ServeJSON()
  1079. return
  1080. }
  1081. // 查询待处理的转移记录
  1082. transfer := validationtool.NewValidationToolTransfer(o)
  1083. transferRecord, err := transfer.GetPendingTransferBySn(request.T_sn)
  1084. if err != nil {
  1085. o.Rollback()
  1086. c.Data["json"] = lib.JSONS{Code: 1201, Msg: fmt.Sprintf("当前sn:%v 没有待确认的转移请求", request.T_sn), Data: nil}
  1087. c.ServeJSON()
  1088. return
  1089. }
  1090. // 检查确认用户是否为目标用户
  1091. if transferRecord.ToUser != request.ConfirmUser {
  1092. o.Rollback()
  1093. c.Data["json"] = lib.JSONS{Code: 1201, Msg: fmt.Sprintf("您不是当前sn:%v 的目标接收用户", request.T_sn), Data: nil}
  1094. c.ServeJSON()
  1095. return
  1096. }
  1097. // 设备一致性比对
  1098. var comparisonResults []DeviceComparisonResult
  1099. // 如果提供了设备信息进行比对
  1100. if request.DeviceInfo != nil {
  1101. // 比对T_iccid
  1102. if iccid, ok := request.DeviceInfo["T_iccid"].(string); ok && iccid != validation.T_iccid {
  1103. comparisonResults = append(comparisonResults, DeviceComparisonResult{
  1104. Field: "T_iccid",
  1105. Expected: validation.T_iccid,
  1106. Actual: iccid,
  1107. })
  1108. }
  1109. // 比对T_imei
  1110. if imei, ok := request.DeviceInfo["T_imei"].(string); ok && imei != validation.T_imei {
  1111. comparisonResults = append(comparisonResults, DeviceComparisonResult{
  1112. Field: "T_imei",
  1113. Expected: validation.T_imei,
  1114. Actual: imei,
  1115. })
  1116. }
  1117. // 比对T_class
  1118. if class, ok := request.DeviceInfo["T_class"]; ok {
  1119. classStr := fmt.Sprintf("%v", class)
  1120. if classStr != fmt.Sprintf("%v", validation.T_class) {
  1121. comparisonResults = append(comparisonResults, DeviceComparisonResult{
  1122. Field: "T_class",
  1123. Expected: fmt.Sprintf("%v", validation.T_class),
  1124. Actual: classStr,
  1125. })
  1126. }
  1127. }
  1128. // 如果比对结果不一致,返回不一致信息
  1129. if len(comparisonResults) > 0 {
  1130. o.Rollback()
  1131. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "设备信息比对不一致", Data: comparisonResults}
  1132. c.ServeJSON()
  1133. return
  1134. }
  1135. }
  1136. // 更新借出用户
  1137. validation.LendUser = request.ConfirmUser
  1138. // 将状态改回已出库
  1139. validation.T_state = validationtool.ValidationToolStateStockOut
  1140. // 在备注中记录转移确认信息
  1141. confirmInfo := fmt.Sprintf("[转移完成] %s 确认接收", request.ConfirmUser)
  1142. validation.T_remark += "\n" + confirmInfo
  1143. cols := []string{"LendUser", "T_remark", "T_state"}
  1144. err = vali.UpdateValidationTool(validation, cols...)
  1145. if err != nil {
  1146. o.Rollback()
  1147. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "修改失败!", Data: nil}
  1148. c.ServeJSON()
  1149. return
  1150. }
  1151. _, err = valiRecord.ADD(validation, BatchNumber)
  1152. if err != nil {
  1153. o.Rollback() // 回滚事务
  1154. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "保存历史记录失败!", Data: nil}
  1155. c.ServeJSON()
  1156. return
  1157. }
  1158. err = o.Commit() // 提交事务
  1159. if err != nil {
  1160. o.Rollback() // 回滚事务
  1161. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "提交事务失败!", Data: nil}
  1162. c.ServeJSON()
  1163. return
  1164. }
  1165. NatsServer.AddUserLogs(c.User.T_uuid, "验证工具", "确认转移", []string{request.T_sn})
  1166. // 返回转移结果
  1167. result := TransferResult{
  1168. T_sn: request.T_sn,
  1169. Success: true,
  1170. Message: "设备转移确认成功!",
  1171. Status: "completed",
  1172. }
  1173. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设备转移确认成功!", Data: result}
  1174. c.ServeJSON()
  1175. return
  1176. }
  1177. // ... existing code ...