validationHistory.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package validationtool
  2. import (
  3. "ERP_storage/logs"
  4. "fmt"
  5. "github.com/beego/beego/v2/adapter/orm"
  6. orm2 "github.com/beego/beego/v2/client/orm"
  7. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  8. "strconv"
  9. "time"
  10. )
  11. type ValidationToolHistory struct {
  12. Id int `orm:"column(ID);size(11);auto;pk"` // 合同编号
  13. T_class int `orm:"size(20);default(0)"` // 产品分类 T_class=0 冷链验证 // 产品id
  14. Validationnumber string `orm:"size(256);null"` // 验证工具编号
  15. T_sn string `orm:"size(256);null"` // 设备sn
  16. T_iccid string `orm:"size(256);null"` // sim卡号
  17. T_imei string `orm:"size(256);null"` // 模组imei
  18. History_iccid string `orm:"size(256);type(json);null"` // 历史sim卡号
  19. History_imei string `orm:"size(256);type(json);null"` // 历史模组
  20. T_state int `orm:"size(2);default(2)"` // 1-已出库 2-待使用(已入库) 3-维修中 4-已报废
  21. T_remark string `orm:"type(text);null"` // 备注r
  22. T_project string `orm:"type(text);null"` // 出库项目
  23. T_uuid string `orm:"size(256);null"` //用户uuid
  24. LendUser string `orm:"size(256);null"` //借出用户
  25. BatchNumber string `orm:"size(256);null"` //批号
  26. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  27. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  28. }
  29. type ValidationToolHistory_R struct {
  30. Id int // 合同编号
  31. T_class int // 产品分类 T_class=0 冷链验证 // 产品id
  32. Validationnumber string // 验证工具编号
  33. T_sn string // 设备sn
  34. T_iccid string // sim卡号
  35. T_imei string // 模组imei
  36. History_iccid string // 历史sim卡号
  37. History_imei string // 历史模组
  38. T_state int // 1-已出库 2-待使用(已入库) 3-维修中 4-已报废
  39. T_remark string // 备注r
  40. T_project string // 出库项目
  41. T_uuid string //用户uuid
  42. LendUser string //借出用户
  43. CreateTime string //auto_now_add 第一次保存时才设置时间
  44. UpdateTime string //auto_now 每次 model 保存时都会对时间自动更新
  45. }
  46. type ValidationToolHistory_R2 struct {
  47. Id int // 合同编号
  48. T_class int // 产品分类 T_class=0 冷链验证 // 产品id
  49. Validationnumber string // 验证工具编号
  50. T_sn string // 设备sn
  51. T_iccid string // sim卡号
  52. T_imei string // 模组imei
  53. History_iccid string // 历史sim卡号
  54. History_imei string // 历史模组
  55. T_state int // 1-已出库 2-待使用(已入库) 3-维修中 4-已报废
  56. T_remark string // 备注r
  57. T_project []string // 出库项目
  58. T_uuid string // 用户uuid
  59. LendUser string // 借出用户
  60. BatchNumber string // 批号
  61. T_sn_List []SN_R2 // 批号
  62. T_sn_quantity int // 批号
  63. CreateTime string // auto_now_add 第一次保存时才设置时间
  64. UpdateTime string // auto_now 每次 model 保存时都会对时间自动更新
  65. }
  66. type SN_R2 struct {
  67. T_project string // 出库项目
  68. T_sn []string // 批号
  69. }
  70. func ValidationToolHistoryToValidationToolHistory_R(t ValidationToolHistory) (r ValidationToolHistory_R) {
  71. r.Id = t.Id
  72. r.T_class = t.T_class
  73. r.Validationnumber = t.Validationnumber
  74. r.T_sn = t.T_sn
  75. r.T_iccid = t.T_iccid
  76. r.T_imei = t.T_imei
  77. r.History_iccid = t.History_iccid
  78. r.History_imei = t.History_imei
  79. r.T_state = t.T_state
  80. r.T_remark = t.T_remark
  81. r.T_project = t.T_project
  82. r.T_uuid = t.T_uuid
  83. r.LendUser = t.LendUser
  84. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  85. r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
  86. return r
  87. }
  88. func ValidationToolHistoryToValidationToolHistory_R2(t ValidationToolHistory) (r ValidationToolHistory_R2) {
  89. r.T_project, r.T_sn_List = ReadSNANDProjectListBytBatchNumber(t.BatchNumber)
  90. for _, v := range r.T_sn_List {
  91. r.T_sn_quantity += len(v.T_sn)
  92. }
  93. r.Id = t.Id
  94. r.T_class = t.T_class
  95. r.Validationnumber = t.Validationnumber
  96. r.T_sn = t.T_sn
  97. r.T_iccid = t.T_iccid
  98. r.T_imei = t.T_imei
  99. r.History_iccid = t.History_iccid
  100. r.History_imei = t.History_imei
  101. r.T_state = t.T_state
  102. r.T_remark = t.T_remark
  103. r.T_uuid = t.T_uuid
  104. r.LendUser = t.LendUser
  105. r.BatchNumber = t.BatchNumber
  106. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  107. r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
  108. return r
  109. }
  110. func (t *ValidationToolHistory) TableName() string {
  111. return "validation_tool_history"
  112. }
  113. type ValidationToolRecordImpl struct {
  114. orm orm.Ormer
  115. }
  116. func NewValidationToolRecord(orm orm.Ormer) *ValidationToolRecordImpl {
  117. return &ValidationToolRecordImpl{orm: orm}
  118. }
  119. func init() {
  120. //注册模型
  121. orm.RegisterModel(new(ValidationToolHistory))
  122. }
  123. // ADDValidationTool 添加
  124. func (dao *ValidationToolRecordImpl) ADD(r ValidationTool, BatchNumber string) (id int64, err error) {
  125. his := ValidationToolHistory{
  126. T_class: r.T_class,
  127. Validationnumber: r.Validationnumber,
  128. T_sn: r.T_sn,
  129. T_iccid: r.T_iccid,
  130. T_imei: r.T_imei,
  131. History_iccid: r.History_iccid,
  132. History_imei: r.History_imei,
  133. T_state: r.T_state,
  134. T_remark: r.T_remark,
  135. T_project: r.T_project,
  136. T_uuid: r.T_uuid,
  137. LendUser: r.LendUser,
  138. BatchNumber: BatchNumber,
  139. }
  140. id, err = dao.orm.Insert(&his)
  141. if err != nil {
  142. logs.Error(lib.FuncName(), err)
  143. }
  144. return id, err
  145. }
  146. func ReadSNANDProjectListBytBatchNumber(BatchNumber string) (project []string, sn []SN_R2) {
  147. o := orm.NewOrm()
  148. qs := o.QueryTable(new(ValidationToolHistory))
  149. var maps []ValidationToolHistory
  150. _, err := qs.Filter("BatchNumber", BatchNumber).OrderBy("CreateTime").All(&maps)
  151. if err != nil {
  152. logs.Error(lib.FuncName(), err)
  153. }
  154. snMaps := make(map[string][]string)
  155. for _, v := range maps {
  156. if _, ok := snMaps[v.T_project]; !ok {
  157. snMaps[v.T_project] = []string{v.T_sn}
  158. } else {
  159. snMaps[v.T_project] = append(snMaps[v.T_project], v.T_sn)
  160. }
  161. }
  162. for s, snList := range snMaps {
  163. project = append(project, s)
  164. sn = append(sn, SN_R2{
  165. T_project: s,
  166. T_sn: snList,
  167. })
  168. }
  169. return
  170. }
  171. func (dao *ValidationToolRecordImpl) Read_Validation_List(Validationnumber, T_sn, T_iccid, T_imei, T_State, LendUser, T_project, T_class string,
  172. page, page_z int) (r []ValidationToolHistory_R, cnt int64) {
  173. var offset int
  174. if page <= 1 {
  175. offset = 0
  176. } else {
  177. offset = (page - 1) * page_z
  178. }
  179. // 过滤
  180. sqlWhere := " WHERE id > 0"
  181. if len(Validationnumber) > 0 {
  182. sqlWhere += " AND validationnumber like \"%" + Validationnumber + "%\""
  183. }
  184. if len(T_State) > 0 {
  185. sqlWhere += " AND t_state like \"%" + T_State + "%\""
  186. }
  187. if len(T_sn) > 0 {
  188. sqlWhere += " AND t_sn like \"%" + T_sn + "%\""
  189. }
  190. if len(T_iccid) > 0 {
  191. sqlWhere += " AND t_iccid like \"%" + T_iccid + "%\""
  192. }
  193. if len(T_imei) > 0 {
  194. sqlWhere += " AND t_imei like \"%" + T_imei + "%\""
  195. }
  196. if len(LendUser) > 0 {
  197. sqlWhere += " AND lend_user like \"%" + LendUser + "%\""
  198. }
  199. if len(T_project) > 0 {
  200. sqlWhere += " AND t_project like \"%" + T_project + "%\""
  201. }
  202. if len(T_class) > 0 {
  203. sqlWhere += " AND t_class like \"%" + T_class + "%\""
  204. }
  205. var maps_z []orm2.ParamsList
  206. // 获取总条数
  207. sql := "SELECT COUNT(*) FROM validation_tool_history"
  208. sql = sql + sqlWhere
  209. fmt.Println(sql)
  210. _, err := dao.orm.Raw(sql).ValuesList(&maps_z)
  211. if err != nil {
  212. return r, 0
  213. }
  214. if len(maps_z) == 0 {
  215. return r, 0
  216. }
  217. sql = "SELECT * FROM validation_tool_history"
  218. sql = sql + sqlWhere
  219. sql += " ORDER BY create_time DESC"
  220. sql += " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  221. fmt.Println(sql)
  222. var maps []ValidationToolHistory
  223. _, err = dao.orm.Raw(sql).QueryRows(&maps)
  224. if err != nil {
  225. return r, 0
  226. }
  227. for _, v := range maps {
  228. r = append(r, ValidationToolHistoryToValidationToolHistory_R(v))
  229. }
  230. count, _ := strconv.Atoi(maps_z[0][0].(string))
  231. return r, int64(count)
  232. }
  233. func (dao *ValidationToolRecordImpl) Read_Operation_List(T_sn, T_State, LendUser, T_project string,
  234. page, page_z int) (r []ValidationToolHistory_R2, cnt int64) {
  235. var offset int
  236. if page <= 1 {
  237. offset = 0
  238. } else {
  239. offset = (page - 1) * page_z
  240. }
  241. // 过滤
  242. sqlWhere := " WHERE id > 0"
  243. if len(T_State) > 0 {
  244. sqlWhere += " AND t_state like \"%" + T_State + "%\""
  245. }
  246. if len(T_sn) > 0 {
  247. sqlWhere += " AND t_sn like \"%" + T_sn + "%\""
  248. }
  249. if len(LendUser) > 0 {
  250. sqlWhere += " AND lend_user like \"%" + LendUser + "%\""
  251. }
  252. if len(T_project) > 0 {
  253. sqlWhere += " AND t_project like \"%" + T_project + "%\""
  254. }
  255. var maps_z []orm2.ParamsList
  256. // 获取总条数
  257. sql := "SELECT COUNT(*) from (select batch_number FROM validation_tool_history "
  258. sql += sqlWhere
  259. sql += " GROUP BY batch_number) A"
  260. fmt.Println(sql)
  261. _, err := dao.orm.Raw(sql).ValuesList(&maps_z)
  262. if err != nil {
  263. return r, 0
  264. }
  265. if len(maps_z) == 0 {
  266. return r, 0
  267. }
  268. sql = "SELECT * FROM validation_tool_history"
  269. sql = sql + sqlWhere
  270. sql += " GROUP BY batch_number ORDER BY batch_number DESC"
  271. if page_z != 9999 {
  272. sql += " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  273. }
  274. fmt.Println(sql)
  275. var maps []ValidationToolHistory
  276. _, err = dao.orm.Raw(sql).QueryRows(&maps)
  277. if err != nil {
  278. return r, 0
  279. }
  280. for _, v := range maps {
  281. r = append(r, ValidationToolHistoryToValidationToolHistory_R2(v))
  282. }
  283. count, _ := strconv.Atoi(maps_z[0][0].(string))
  284. return r, int64(count)
  285. }