StockInProduct.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package Stock
  2. import (
  3. "ERP_storage/models/Account"
  4. "ERP_storage/models/Basic"
  5. orm2 "github.com/beego/beego/v2/client/orm"
  6. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  7. "strconv"
  8. "time"
  9. "ERP_storage/logs"
  10. _ "github.com/astaxie/beego/cache/redis"
  11. "github.com/beego/beego/v2/adapter/orm"
  12. _ "github.com/go-sql-driver/mysql"
  13. )
  14. // 入库产品mingxi
  15. type StockInProduct struct {
  16. Id int `orm:"column(ID);size(11);auto;pk"`
  17. T_number string `orm:"size(256);null"` // 入库单号
  18. T_depot_id int `orm:"size(20);null"` // 仓库id
  19. T_product_id int `orm:"size(20);null"` // 产品id
  20. T_num int `orm:"size(20);null"` // 入库数量
  21. T_date string `orm:"size(256);null"` // 业务日期
  22. T_relation_sn string `orm:"type(text);null"` // 关联sn,20220101,20230202,
  23. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  24. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  25. }
  26. // 入库列表
  27. type StockInProductList struct {
  28. Id int
  29. T_number string // StockIn - 入库单号
  30. T_depot_id int // StockIn - 仓库id
  31. T_depot_name string // StockIn - 仓库名称
  32. T_type int // StockIn - 入库类型
  33. T_date string // StockIn - 业务日期
  34. T_submit string // StockIn - 经办人
  35. T_submit_name string // StockIn - 经办人名称
  36. T_remark string // StockIn - 备注
  37. T_num int // 入库数量
  38. T_relation_sn string // 关联sn,20220101,20230202
  39. T_device_list []string // sn关联列表
  40. // ---------产品信息-----------
  41. T_product_id int
  42. T_product_name string
  43. T_product_class_name string
  44. T_product_model string
  45. T_product_spec string
  46. T_product_relation_sn int
  47. T_product_img string
  48. }
  49. func (t *StockInProduct) TableName() string {
  50. return "stock_in_product" // 数据库名称 // ************** 替换 FormulaList **************
  51. }
  52. type StockInProductDaoImpl struct {
  53. orm orm.Ormer
  54. }
  55. func NewStockInProduct(orm orm.Ormer) *StockInProductDaoImpl {
  56. return &StockInProductDaoImpl{orm: orm}
  57. }
  58. func init() {
  59. //注册模型
  60. orm.RegisterModel(new(StockInProduct))
  61. }
  62. type StockInProduct_R struct {
  63. Id int
  64. T_number string // 入库单号
  65. T_depot_id int // 仓库id
  66. T_num int // 入库数量
  67. T_date string // 日期
  68. //T_relation_sn string // 关联sn,20220101,20230202,
  69. T_device_list []string // sn关联列表
  70. // ---------产品信息-----------
  71. T_product_id int
  72. T_product_name string
  73. T_product_class_name string
  74. T_product_model string
  75. T_product_spec string
  76. T_product_relation_sn int
  77. T_product_img string
  78. }
  79. func StockInProductToStockInProduct_R(t StockInProduct) (r StockInProduct_R) {
  80. r.Id = t.Id
  81. r.T_number = t.T_number
  82. r.T_depot_id = t.T_depot_id
  83. r.T_num = t.T_num
  84. r.T_date = t.T_date
  85. //r.T_relation_sn = t.T_relation_sn
  86. r.T_device_list = lib.SplitString(t.T_relation_sn, ",")
  87. r.T_product_id = t.T_product_id
  88. product, _ := Basic.Read_Product_ById(t.T_product_id)
  89. r.T_product_name = product.T_name
  90. r.T_product_class_name = Basic.Read_ProductClass_Get(product.T_class)
  91. r.T_product_model = product.T_model
  92. r.T_product_spec = product.T_spec
  93. r.T_product_relation_sn = product.T_relation_sn
  94. r.T_product_img = product.T_img
  95. return r
  96. }
  97. func StockInProductListToStockInProductList(t StockInProductList) (r StockInProductList) {
  98. r.Id = t.Id
  99. r.T_number = t.T_number
  100. r.T_depot_id = t.T_depot_id
  101. r.T_type = t.T_type
  102. r.T_depot_name = Basic.Read_Depot_Get(t.T_depot_id)
  103. r.T_num = t.T_num
  104. r.T_date = t.T_date
  105. r.T_submit = t.T_submit
  106. r.T_submit_name = Account.Read_User_T_name_Get(t.T_submit)
  107. r.T_remark = t.T_remark
  108. r.T_num = t.T_num
  109. r.T_relation_sn = t.T_relation_sn
  110. r.T_device_list = lib.SplitString(t.T_relation_sn, ",")
  111. r.T_product_id = t.T_product_id
  112. product, _ := Basic.Read_Product_ById(t.T_product_id)
  113. r.T_product_name = product.T_name
  114. r.T_product_class_name = Basic.Read_ProductClass_Get(product.T_class)
  115. r.T_product_model = product.T_model
  116. r.T_product_spec = product.T_spec
  117. r.T_product_relation_sn = product.T_relation_sn
  118. r.T_product_img = product.T_img
  119. return r
  120. }
  121. // 添加
  122. func (dao *StockInProductDaoImpl) Add_StockInProduct(r StockInProduct) (id int64, err error) {
  123. id, err = dao.orm.Insert(&r)
  124. if err != nil {
  125. logs.Error(lib.FuncName(), err)
  126. }
  127. return id, err
  128. }
  129. // 通过入库编号查询入库产品列表
  130. func (dao *StockInProductDaoImpl) Read_StockInProduct_List_ByT_number(T_number string) (r []StockInProduct) {
  131. qs := dao.orm.QueryTable(new(StockInProduct))
  132. _, err := qs.Filter("T_number", T_number).All(&r)
  133. if err != nil {
  134. logs.Error(lib.FuncName(), err)
  135. }
  136. return
  137. }
  138. // 修改
  139. func (dao *StockInProductDaoImpl) Update_StockInProduct(r StockInProduct) error {
  140. qs := dao.orm.QueryTable(new(StockInProduct))
  141. var product StockInProduct
  142. err := qs.Filter("T_number", r.T_number).Filter("T_product_id", r.T_product_id).One(&product)
  143. if err != nil {
  144. if err.Error() == orm.ErrNoRows.Error() {
  145. _, err = dao.orm.Insert(&product)
  146. if err != nil {
  147. logs.Error(lib.FuncName(), err)
  148. return err
  149. } else {
  150. return nil
  151. }
  152. }
  153. logs.Error(lib.FuncName(), err)
  154. return err
  155. }
  156. product.T_depot_id = r.T_depot_id
  157. product.T_num = r.T_num
  158. product.T_relation_sn = r.T_relation_sn
  159. _, err = dao.orm.Update(&product, "T_depot_id", "T_num", "T_relation_sn")
  160. if err != nil {
  161. logs.Error(lib.FuncName(), err)
  162. return err
  163. }
  164. return nil
  165. }
  166. func (dao *StockInProductDaoImpl) Update_StockInProduct_T_date(T_number, T_date string) error {
  167. qs := dao.orm.QueryTable(new(StockInProduct))
  168. var list []StockInProduct
  169. _, err := qs.Filter("T_number", T_number).All(&list)
  170. if err != nil {
  171. logs.Error(lib.FuncName(), err)
  172. return err
  173. }
  174. for _, product := range list {
  175. product.T_date = T_date
  176. _, err = dao.orm.Update(&product, "T_date")
  177. if err != nil {
  178. logs.Error(lib.FuncName(), err)
  179. return err
  180. }
  181. }
  182. return nil
  183. }
  184. // 删除
  185. func (dao *StockInProductDaoImpl) Delete_StockInProduct(T_number string, T_depot_id, T_product_id int) error {
  186. qs := dao.orm.QueryTable(new(StockInProduct))
  187. _, err := qs.Filter("T_number", T_number).Filter("T_depot_id", T_depot_id).Filter("T_product_id", T_product_id).Delete()
  188. if err != nil {
  189. logs.Error(lib.FuncName(), err)
  190. return err
  191. }
  192. return nil
  193. }
  194. // 根据库存id、产品id、月份获取本月出库总数量
  195. func (dao *StockInProductDaoImpl) Read_StockIn_Total(T_depot_id, T_product_id int, T_date string) int {
  196. sql := "SELECT SUM(t_num) FROM stock_in_product WHERE t_depot_id = " + strconv.Itoa(T_depot_id) + " AND t_product_id = " + strconv.Itoa(T_product_id) + " AND t_date like '" + T_date + "%'"
  197. var pl_lists orm2.ParamsList
  198. _, err := dao.orm.Raw(sql).ValuesFlat(&pl_lists)
  199. if err != nil {
  200. logs.Error(lib.FuncName(), err)
  201. return 0
  202. }
  203. if pl_lists[0] == nil {
  204. return 0
  205. }
  206. key, _ := strconv.Atoi(pl_lists[0].(string))
  207. return key
  208. }