Device.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package Stock
  2. import (
  3. "ERP_storage/logs"
  4. "ERP_storage/models/Basic"
  5. "errors"
  6. "fmt"
  7. "git.baozhida.cn/ERP_libs/lib"
  8. _ "github.com/astaxie/beego/cache/redis"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. orm2 "github.com/beego/beego/v2/client/orm"
  11. "strconv"
  12. "time"
  13. )
  14. type Device struct {
  15. Id int `orm:"column(ID);size(11);auto;pk"`
  16. T_contract_number string `orm:"size(256);null"` // 合同编号
  17. T_product_id int `orm:"size(20);null"` // 产品id
  18. T_in_number string `orm:"size(256);null"` // 入库编号
  19. T_out_number string `orm:"size(256);null"` // 出库编号
  20. T_sn string `orm:"size(256);null"` // 设备sn
  21. T_iccid string `orm:"size(256);null"` // sim卡号
  22. T_imei string `orm:"size(256);null"` // 模组imei
  23. T_State int `orm:"size(2);default(2)"` // 1-已出库 2-未出库/入库
  24. T_remark string `orm:"type(text);null"` // 备注
  25. T_project string `orm:"type(text);null"` // 出库项目
  26. T_project_log string `orm:"type(text);null"` // 出库项目
  27. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  28. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  29. }
  30. func (t *Device) TableName() string {
  31. return "device" // 数据库名称 // ************** 替换 FormulaList **************
  32. }
  33. type DeviceDaoImpl struct {
  34. orm orm.Ormer
  35. }
  36. func NewDevice(orm orm.Ormer) *DeviceDaoImpl {
  37. return &DeviceDaoImpl{orm: orm}
  38. }
  39. func init() {
  40. //注册模型
  41. orm.RegisterModel(new(Device))
  42. }
  43. type Device_R struct {
  44. Id int
  45. T_contract_number string // 合同编号
  46. T_in_number string // 入库编号
  47. T_out_number string // 出库编号
  48. T_sn string // 设备sn
  49. T_iccid string // 物联网卡号
  50. T_imei string // 模组imei
  51. T_State int // 1-已出库 2-未出库
  52. T_device_state int // 1-已出库 2-未出库
  53. T_remark string
  54. T_project string
  55. T_project_log string
  56. // ---------产品信息-----------
  57. T_product_id int
  58. T_product_name string
  59. T_product_class int
  60. T_product_class_name string
  61. T_product_model string
  62. T_product_spec string
  63. T_product_img string
  64. }
  65. func DeviceToDevice_R(t Device_R) (r Device_R) {
  66. r.Id = t.Id
  67. r.T_contract_number = t.T_contract_number
  68. r.T_product_id = t.T_product_id
  69. r.T_in_number = t.T_in_number
  70. r.T_out_number = t.T_out_number
  71. r.T_sn = t.T_sn
  72. r.T_iccid = t.T_iccid
  73. r.T_imei = t.T_imei
  74. r.T_State = t.T_device_state
  75. r.T_device_state = t.T_device_state
  76. r.T_remark = t.T_remark
  77. r.T_project = t.T_project
  78. r.T_project_log = t.T_project_log
  79. r.T_product_name = t.T_product_name
  80. r.T_product_class = t.T_product_class
  81. r.T_product_model = t.T_product_model
  82. r.T_product_spec = t.T_product_spec
  83. r.T_product_img = t.T_product_img
  84. r.T_product_class_name = Basic.Read_ProductClass_Get(t.T_product_class)
  85. return r
  86. }
  87. // 添加
  88. func (dao *DeviceDaoImpl) Add_Device(r Device) (id int64, err error) {
  89. id, err = dao.orm.Insert(&r)
  90. if err != nil {
  91. logs.Error(lib.FuncName(), err)
  92. }
  93. return id, err
  94. }
  95. // T_type 1-出库 2-入库
  96. func (dao *DeviceDaoImpl) AddOrUpdate_Device(r Device, T_type int) (id int64, err error) {
  97. id, err = dao.orm.Insert(&r)
  98. if err != nil {
  99. logs.Error(lib.FuncName(), err)
  100. }
  101. return
  102. }
  103. func (dao *DeviceDaoImpl) AddOrUpdate_Device1(r Device, T_type int) (id int64, err error) {
  104. qs := dao.orm.QueryTable(new(Device))
  105. var device Device
  106. err = qs.Filter("T_sn", r.T_sn).One(&device)
  107. if err != nil {
  108. if err.Error() == orm.ErrNoRows.Error() {
  109. r.T_remark = fmt.Sprintf("%s:%s(%s)|", time.Now().Format("2006-01-02"), "入库", r.T_in_number)
  110. if len(r.T_project) > 0 {
  111. r.T_project_log += r.T_project + "|"
  112. }
  113. id, err = dao.orm.Insert(&r)
  114. if err != nil {
  115. logs.Error(lib.FuncName(), err)
  116. }
  117. return
  118. } else {
  119. logs.Error(lib.FuncName(), err)
  120. return
  121. }
  122. }
  123. T_remark := ""
  124. if T_type == 1 {
  125. T_remark = fmt.Sprintf("%s:%s(%s)|", time.Now().Format("2006-01-02"), "出库", r.T_out_number)
  126. }
  127. if T_type == 2 {
  128. T_remark = fmt.Sprintf("%s:%s(%s)|", time.Now().Format("2006-01-02"), "入库", device.T_in_number)
  129. }
  130. r.Id = device.Id
  131. // T_State 1-出库 2-入库
  132. r.T_State = T_type
  133. // 出库时将入库编号服务给update结构体
  134. if len(r.T_in_number) == 0 && len(device.T_in_number) > 0 {
  135. r.T_in_number = device.T_in_number
  136. }
  137. r.T_remark = device.T_remark + T_remark
  138. if len(r.T_project) > 0 {
  139. r.T_project_log = device.T_project_log + r.T_project + "|"
  140. } else {
  141. r.T_project_log = device.T_project_log
  142. r.T_project = device.T_project
  143. }
  144. _, err = dao.orm.Update(&r, "T_contract_number", "T_in_number", "T_out_number", "T_State", "T_remark", "T_project", "T_project_log")
  145. if err != nil {
  146. logs.Error(lib.FuncName(), err)
  147. return
  148. }
  149. return
  150. }
  151. func (dao *DeviceDaoImpl) Read_Device_ByT_sn(T_sn string) (r Device, err error) {
  152. qs := dao.orm.QueryTable(new(Device))
  153. var list []Device
  154. _, err = qs.Limit(1, 0).Filter("T_sn", T_sn).OrderBy("-CreateTime").All(&list)
  155. if err != nil {
  156. logs.Error(lib.FuncName(), err)
  157. }
  158. if len(list) == 0 {
  159. return r, orm.ErrNoRows
  160. }
  161. if len(list) > 0 {
  162. r = list[0]
  163. }
  164. return
  165. }
  166. 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) {
  167. qs := dao.orm.QueryTable(new(Device))
  168. // 过滤
  169. cond := orm.NewCondition()
  170. cond.And("T_product_id", T_product_id)
  171. if len(T_contract_number) > 0 {
  172. cond = cond.And("T_contract_number", T_contract_number)
  173. }
  174. if len(T_in_number) > 0 {
  175. cond = cond.And("T_in_number", T_in_number)
  176. }
  177. if len(T_out_number) > 0 {
  178. cond = cond.And("T_out_number", T_out_number)
  179. }
  180. var maps []Device
  181. _, err = qs.SetCond((*orm2.Condition)(cond)).All(&maps)
  182. if err != nil {
  183. logs.Error(lib.FuncName(), err)
  184. return
  185. }
  186. for _, v := range maps {
  187. r = append(r, v.T_sn)
  188. }
  189. return
  190. }
  191. func (dao *DeviceDaoImpl) Read_Device_List(T_name, T_product_name, T_product_model string, T_State, page, page_z int) (r []Device_R, cnt int64) {
  192. var offset int
  193. if page <= 1 {
  194. offset = 0
  195. } else {
  196. offset = (page - 1) * page_z
  197. }
  198. // 过滤
  199. sqlWhere := " WHERE d.t__state > 0"
  200. if len(T_name) > 0 {
  201. sqlWhere += " AND (d.t_contract_number like \"%" + T_name + "%\" or d.t_out_number like \"%" + T_name +
  202. "%\" or d.t_sn like \"%" + T_name + "%\" or d.t_iccid like \"%" + T_name + "%\" or d.t_project like \"%" + T_name + "%\")"
  203. }
  204. if T_State > 0 {
  205. sqlWhere += fmt.Sprintf(" AND d.t__state = %d", T_State)
  206. }
  207. if len(T_product_name) > 0 {
  208. sqlWhere += " AND p.t_name like \"%" + T_product_name + "%\""
  209. }
  210. if len(T_product_model) > 0 {
  211. sqlWhere += " AND p.t_model like \"%" + T_product_model + "%\""
  212. }
  213. var maps_z []orm2.ParamsList
  214. // 获取总条数
  215. sql := "SELECT COUNT(*) FROM device d JOIN product p ON d.t_product_id = p.ID JOIN (SELECT t_sn, MAX(create_time) AS max_create_time FROM device GROUP BY t_sn) latest " +
  216. "ON d.t_sn = latest.t_sn AND d.create_time = latest.max_create_time"
  217. sql = sql + sqlWhere
  218. fmt.Println(sql)
  219. _, err := dao.orm.Raw(sql).ValuesList(&maps_z)
  220. if err != nil {
  221. return r, 0
  222. }
  223. if len(maps_z) == 0 {
  224. return r, 0
  225. }
  226. sql = "SELECT *,latest.t__state as t_device_state,p.t_name AS t_product_name,p.t_model AS t_product_model,p.t_class AS t_product_class,p.t_spec AS t_product_spec " +
  227. "FROM device d JOIN product p ON d.t_product_id = p.ID JOIN (SELECT t_sn,t__state, MAX(create_time) AS max_create_time FROM device GROUP BY t_sn) latest " +
  228. "ON d.t_sn = latest.t_sn AND d.create_time = latest.max_create_time"
  229. sql = sql + sqlWhere
  230. sql += " ORDER BY d.create_time DESC"
  231. sql += " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  232. fmt.Println(sql)
  233. var maps []Device_R
  234. _, err = dao.orm.Raw(sql).QueryRows(&maps)
  235. if err != nil {
  236. return r, 0
  237. }
  238. for _, v := range maps {
  239. v.T_remark, v.T_project_log = dao.Read_Device_History(v.T_sn)
  240. r = append(r, DeviceToDevice_R(v))
  241. }
  242. count, _ := strconv.Atoi(maps_z[0][0].(string))
  243. return r, int64(count)
  244. }
  245. func (dao *DeviceDaoImpl) Read_Device_History(T_sn string) (T_remark string, T_project string) {
  246. qs := dao.orm.QueryTable(new(Device))
  247. var maps []Device
  248. _, err := qs.Filter("T_sn", T_sn).OrderBy("CreateTime").All(&maps)
  249. if err != nil {
  250. logs.Error(lib.FuncName(), err)
  251. return
  252. }
  253. for _, v := range maps {
  254. if v.T_State == 1 {
  255. T_remark += fmt.Sprintf("%s:%s(%s)|", v.CreateTime.Format("2006-01-02"), "出库", v.T_out_number)
  256. if len(v.T_project) > 0 {
  257. T_project += fmt.Sprintf("%s:%s(%s):%s|", v.CreateTime.Format("2006-01-02"), "出库", v.T_out_number, v.T_project)
  258. }
  259. }
  260. if v.T_State == 2 {
  261. T_remark += fmt.Sprintf("%s:%s(%s)|", v.CreateTime.Format("2006-01-02"), "入库", v.T_in_number)
  262. }
  263. }
  264. return
  265. }
  266. func (dao *DeviceDaoImpl) Delete_Device_ByT_in_number(T_number, T_sn string) (err error) {
  267. qs := dao.orm.QueryTable(new(Device))
  268. var device Device
  269. err = qs.Filter("T_sn", T_sn).Filter("T_in_number", T_number).One(&device)
  270. if err != nil {
  271. return
  272. }
  273. var count int64
  274. count, err = qs.Filter("T_sn", T_sn).Filter("CreateTime__gt", device.CreateTime).Count()
  275. if err != nil {
  276. logs.Error(lib.FuncName(), err)
  277. return
  278. }
  279. if count > 1 {
  280. return errors.New("设备已出库,无法删除")
  281. }
  282. _, err = dao.orm.Delete(&device)
  283. if err != nil {
  284. logs.Error(lib.FuncName(), err)
  285. return
  286. }
  287. return err
  288. }
  289. func (dao *DeviceDaoImpl) Delete_Device_ByT_out_number(T_number, T_sn string) (err error) {
  290. qs := dao.orm.QueryTable(new(Device))
  291. var device Device
  292. err = qs.Filter("T_sn", T_sn).Filter("T_out_number", T_number).One(&device)
  293. if err != nil {
  294. return
  295. }
  296. var count int64
  297. count, err = qs.Filter("T_sn", T_sn).Filter("CreateTime__gt", device.CreateTime).Count()
  298. if err != nil {
  299. logs.Error(lib.FuncName(), err)
  300. return
  301. }
  302. if count > 1 {
  303. return errors.New("设备已入库,无法删除")
  304. }
  305. _, err = dao.orm.Delete(&device)
  306. if err != nil {
  307. logs.Error(lib.FuncName(), err)
  308. return
  309. }
  310. return err
  311. }
  312. func (dao *DeviceDaoImpl) Update_Device_ByT_out_number_T_project(T_number, T_project string) error {
  313. qs := dao.orm.QueryTable(new(Device))
  314. var list []Device
  315. _, err := qs.Filter("T_out_number", T_number).All(&list)
  316. if err != nil {
  317. logs.Error(lib.FuncName(), err)
  318. return err
  319. }
  320. for _, deivce := range list {
  321. deivce.T_project = T_project
  322. _, err = dao.orm.Update(&deivce, "T_project")
  323. if err != nil {
  324. logs.Error(lib.FuncName(), err)
  325. return err
  326. }
  327. }
  328. return nil
  329. }
  330. func (dao *DeviceDaoImpl) Update_Device_CreateTimeByT_out_number(T_number, T_date string) error {
  331. now := time.Now().Format("15:04:05")
  332. date, _ := lib.TimeStrToTime(T_date + " " + now)
  333. qs := dao.orm.QueryTable(new(Device))
  334. var list []Device
  335. _, err := qs.Filter("T_out_number", T_number).All(&list)
  336. if err != nil {
  337. logs.Error(lib.FuncName(), err)
  338. return err
  339. }
  340. for _, device := range list {
  341. //device.CreateTime = date
  342. //_, err = dao.orm.Update(&device, "CreateTime")
  343. _, err = dao.orm.Raw("UPDATE device SET create_time = ? WHERE id = ?", date, device.Id).Exec()
  344. if err != nil {
  345. logs.Error(lib.FuncName(), err)
  346. return err
  347. }
  348. }
  349. return nil
  350. }
  351. func (dao *DeviceDaoImpl) Update_Device_CreateTimeByT_in_number(T_number, T_date string) error {
  352. now := time.Now().Format("15:04:05")
  353. date, _ := lib.TimeStrToTime(T_date + " " + now)
  354. qs := dao.orm.QueryTable(new(Device))
  355. var list []Device
  356. _, err := qs.Filter("T_in_number", T_number).All(&list)
  357. if err != nil {
  358. logs.Error(lib.FuncName(), err)
  359. return err
  360. }
  361. for _, device := range list {
  362. //device.CreateTime = date
  363. //_, err = dao.orm.Update(&device, "CreateTime")
  364. _, err = dao.orm.Raw("UPDATE device SET create_time = ? WHERE id = ?", date, device.Id).Exec()
  365. if err != nil {
  366. logs.Error(lib.FuncName(), err)
  367. return err
  368. }
  369. }
  370. return nil
  371. }