Device.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package Stock
  2. import (
  3. "ERP_storage/logs"
  4. "ERP_storage/models/Basic"
  5. "fmt"
  6. "git.baozhida.cn/ERP_libs/lib"
  7. _ "github.com/astaxie/beego/cache/redis"
  8. "github.com/beego/beego/v2/adapter/orm"
  9. orm2 "github.com/beego/beego/v2/client/orm"
  10. "time"
  11. )
  12. type Device struct {
  13. Id int `orm:"column(ID);size(11);auto;pk"`
  14. T_contract_number string `orm:"size(256);null"` // 合同编号
  15. T_product_id int `orm:"size(20);null"` // 产品id
  16. T_in_number string `orm:"size(256);null"` // 入库编号
  17. T_out_number string `orm:"size(256);null"` // 出库编号
  18. T_sn string `orm:"size(256);null"` // 设备sn
  19. T_iccid string `orm:"size(256);null"` // sim卡号
  20. T_imei string `orm:"size(256);null"` // 模组imei
  21. T_State int `orm:"size(2);default(2)"` // 1-已出库 2-未出库/入库
  22. T_remark string `orm:"type(text);null"` // 备注
  23. T_project string `orm:"type(text);null"` // 出库项目
  24. T_project_log string `orm:"type(text);null"` // 出库项目
  25. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  26. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  27. }
  28. func (t *Device) TableName() string {
  29. return "device" // 数据库名称 // ************** 替换 FormulaList **************
  30. }
  31. type DeviceDaoImpl struct {
  32. orm orm.Ormer
  33. }
  34. func NewDevice(orm orm.Ormer) *DeviceDaoImpl {
  35. return &DeviceDaoImpl{orm: orm}
  36. }
  37. func init() {
  38. //注册模型
  39. orm.RegisterModel(new(Device))
  40. }
  41. type Device_R struct {
  42. Id int
  43. T_contract_number string // 合同编号
  44. T_in_number string // 入库编号
  45. T_out_number string // 出库编号
  46. T_sn string // 设备sn
  47. T_iccid string // 物联网卡号
  48. T_imei string // 模组imei
  49. T_State int // 1-已出库 2-未出库
  50. T_remark string
  51. T_project string
  52. T_project_log string
  53. // ---------产品信息-----------
  54. T_product_id int
  55. T_product_name string
  56. T_product_class_name string
  57. T_product_model string
  58. T_product_spec string
  59. T_product_img string
  60. }
  61. func DeviceToDevice_R(t Device) (r Device_R) {
  62. r.Id = t.Id
  63. r.T_contract_number = t.T_contract_number
  64. r.T_product_id = t.T_product_id
  65. r.T_in_number = t.T_in_number
  66. r.T_out_number = t.T_out_number
  67. r.T_sn = t.T_sn
  68. r.T_iccid = t.T_iccid
  69. r.T_imei = t.T_imei
  70. r.T_State = t.T_State
  71. r.T_remark = t.T_remark
  72. r.T_project = t.T_project
  73. r.T_project_log = t.T_project_log
  74. r.T_product_id = t.T_product_id
  75. product, _ := Basic.Read_Product_ById(t.T_product_id)
  76. r.T_product_name = product.T_name
  77. r.T_product_class_name = Basic.Read_ProductClass_Get(product.T_class)
  78. r.T_product_model = product.T_model
  79. r.T_product_spec = product.T_spec
  80. r.T_product_img = product.T_img
  81. return r
  82. }
  83. // 添加
  84. func (dao *DeviceDaoImpl) Add_Device(r Device) (id int64, err error) {
  85. id, err = dao.orm.Insert(&r)
  86. if err != nil {
  87. logs.Error(lib.FuncName(), err)
  88. }
  89. return id, err
  90. }
  91. // T_type 1-出库 2-入库
  92. func (dao *DeviceDaoImpl) AddOrUpdate_Device(r Device, T_type int) (id int64, err error) {
  93. qs := dao.orm.QueryTable(new(Device))
  94. var device Device
  95. err = qs.Filter("T_sn", r.T_sn).One(&device)
  96. if err != nil {
  97. if err.Error() == orm.ErrNoRows.Error() {
  98. r.T_remark = fmt.Sprintf("%s:%s(%s)|", time.Now().Format("2006-01-02"), "入库", r.T_in_number)
  99. if len(r.T_project) > 0 {
  100. r.T_project_log += r.T_project + "|"
  101. }
  102. id, err = dao.orm.Insert(&r)
  103. if err != nil {
  104. logs.Error(lib.FuncName(), err)
  105. }
  106. return
  107. } else {
  108. logs.Error(lib.FuncName(), err)
  109. return
  110. }
  111. }
  112. T_remark := ""
  113. if T_type == 1 {
  114. T_remark = fmt.Sprintf("%s:%s(%s)|", time.Now().Format("2006-01-02"), "出库", r.T_out_number)
  115. }
  116. if T_type == 2 {
  117. T_remark = fmt.Sprintf("%s:%s(%s)|", time.Now().Format("2006-01-02"), "入库", device.T_in_number)
  118. }
  119. r.Id = device.Id
  120. // T_State 1-出库 2-入库
  121. r.T_State = T_type
  122. // 出库时将入库编号服务给update结构体
  123. if len(r.T_in_number) == 0 && len(device.T_in_number) > 0 {
  124. r.T_in_number = device.T_in_number
  125. }
  126. r.T_remark = device.T_remark + T_remark
  127. if len(r.T_project) > 0 {
  128. r.T_project_log = device.T_project_log + r.T_project + "|"
  129. } else {
  130. r.T_project_log = device.T_project_log
  131. r.T_project = device.T_project
  132. }
  133. _, err = dao.orm.Update(&r, "T_contract_number", "T_in_number", "T_out_number", "T_State", "T_remark", "T_project", "T_project_log")
  134. if err != nil {
  135. logs.Error(lib.FuncName(), err)
  136. return
  137. }
  138. return
  139. }
  140. func (dao *DeviceDaoImpl) Read_Device_ByT_sn(T_sn string) (r Device, err error) {
  141. qs := dao.orm.QueryTable(new(Device))
  142. err = qs.Filter("T_sn", T_sn).One(&r)
  143. if err != nil {
  144. logs.Error(lib.FuncName(), err)
  145. }
  146. return
  147. }
  148. func (dao *DeviceDaoImpl) Read_DeviceSn_List(T_contract_number string, T_product_id int, T_in_number, T_out_number string) (r []string, err error) {
  149. qs := dao.orm.QueryTable(new(Device))
  150. // 过滤
  151. cond := orm.NewCondition()
  152. cond.And("T_product_id", T_product_id)
  153. if len(T_contract_number) > 0 {
  154. cond = cond.And("T_contract_number", T_contract_number)
  155. }
  156. if len(T_in_number) > 0 {
  157. cond = cond.And("T_in_number", T_in_number)
  158. }
  159. if len(T_out_number) > 0 {
  160. cond = cond.And("T_out_number", T_out_number)
  161. }
  162. var maps []Device
  163. _, err = qs.SetCond((*orm2.Condition)(cond)).All(&maps)
  164. if err != nil {
  165. logs.Error(lib.FuncName(), err)
  166. return
  167. }
  168. for _, v := range maps {
  169. r = append(r, v.T_sn)
  170. }
  171. return
  172. }
  173. func (dao *DeviceDaoImpl) Read_Device_List(T_name string, T_State, page, page_z int) (r []Device_R, cnt int64) {
  174. qs := dao.orm.QueryTable(new(Device))
  175. var offset int64
  176. if page <= 1 {
  177. offset = 0
  178. } else {
  179. offset = int64((page - 1) * page_z)
  180. }
  181. // 过滤
  182. cond := orm.NewCondition()
  183. cond1 := cond.And("T_State__gt", 0)
  184. if len(T_name) > 0 {
  185. cond1 = cond1.AndCond(
  186. cond.Or("T_contract_number__icontains", T_name).
  187. Or("T_out_number__icontains", T_name).
  188. Or("T_sn__icontains", T_name).
  189. Or("T_iccid__icontains", T_name).
  190. Or("T_project__icontains", T_name))
  191. }
  192. if T_State > 0 {
  193. cond1 = cond1.And("T_State", T_State)
  194. }
  195. var maps []Device
  196. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).All(&maps)
  197. if err != nil {
  198. logs.Error(lib.FuncName(), err)
  199. return
  200. }
  201. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  202. if err != nil {
  203. logs.Error(lib.FuncName(), err)
  204. return
  205. }
  206. for _, v := range maps {
  207. r = append(r, DeviceToDevice_R(v))
  208. }
  209. return
  210. }