Validation.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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. }