StockOutProduct.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package Stock
  2. import (
  3. "ERP_storage/models/Account"
  4. "ERP_storage/models/Basic"
  5. "git.baozhida.cn/ERP_libs/lib"
  6. orm2 "github.com/beego/beego/v2/client/orm"
  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 StockOutProduct 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. func (t *StockOutProduct) TableName() string {
  27. return "stock_out_product" // 数据库名称 // ************** 替换 FormulaList **************
  28. }
  29. type StockOutProductDaoImpl struct {
  30. orm orm.Ormer
  31. }
  32. func NewStockOutProduct(orm orm.Ormer) *StockOutProductDaoImpl {
  33. return &StockOutProductDaoImpl{orm: orm}
  34. }
  35. func init() {
  36. //注册模型
  37. orm.RegisterModel(new(StockOutProduct))
  38. }
  39. type StockOutProduct_R struct {
  40. Id int
  41. T_number string // 入库单号
  42. T_depot_id int // 仓库id
  43. T_num int // 入库数量
  44. T_date string // 业务日期
  45. T_device_list []string // sn关联列表
  46. // ---------产品信息-----------
  47. T_product_id int // 产品id
  48. T_product_name string // 产品名称
  49. T_product_class_name string // 产品分类名称
  50. T_product_model string // 产品型号
  51. T_product_spec string // 产品规格
  52. T_product_relation_sn int // 是否关联sn 0-否 1-是
  53. T_product_img string // 产品图片
  54. }
  55. func StockOutProductToStockOutProduct_R(t StockOutProduct) (r StockOutProduct_R) {
  56. r.Id = t.Id
  57. r.T_number = t.T_number
  58. r.T_depot_id = t.T_depot_id
  59. r.T_num = t.T_num
  60. r.T_date = t.T_date
  61. //r.T_relation_sn = t.T_relation_sn
  62. r.T_device_list = lib.SplitString(t.T_relation_sn, ",")
  63. r.T_product_id = t.T_product_id
  64. product, _ := Basic.Read_Product_ById(t.T_product_id)
  65. r.T_product_name = product.T_name
  66. r.T_product_class_name = Basic.Read_ProductClass_Get(product.T_class)
  67. r.T_product_model = product.T_model
  68. r.T_product_spec = product.T_spec
  69. r.T_product_relation_sn = product.T_relation_sn
  70. r.T_product_img = product.T_img
  71. return r
  72. }
  73. type StockOutProductList struct {
  74. Id int
  75. T_number string // StockOut - 出库单号
  76. T_depot_id int // StockOut - 仓库id
  77. T_depot_name string // StockOut - 仓库名称
  78. T_date string // StockOut - 业务日期
  79. T_submit string // StockOut - 经办人
  80. T_remark string // StockOut - 备注
  81. T_project string // StockOut - 项目
  82. T_receive string // StockOut - 领取人
  83. T_receive_name string // 领取人名称
  84. T_num int // 入库数量
  85. T_relation_sn string // 关联sn,20220101,20230202
  86. T_device_list []string // sn关联列表
  87. // ---------产品信息-----------
  88. T_product_id int
  89. T_product_name string
  90. T_product_class_name string
  91. T_product_model string
  92. T_product_spec string
  93. T_product_relation_sn int
  94. T_product_img string
  95. }
  96. func StockOutProductListToStockOutProductList(t StockOutProductList) (r StockOutProductList) {
  97. r.Id = t.Id
  98. r.T_number = t.T_number
  99. r.T_depot_id = t.T_depot_id
  100. r.T_depot_name = Basic.Read_Depot_Get(t.T_depot_id)
  101. r.T_num = t.T_num
  102. r.T_date = t.T_date
  103. r.T_receive = t.T_receive
  104. r.T_receive_name = Account.Read_User_T_name_Get(t.T_receive)
  105. r.T_submit = t.T_submit
  106. r.T_remark = t.T_remark
  107. r.T_project = t.T_project
  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 *StockOutProductDaoImpl) Add_StockOutProduct(r StockOutProduct) (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. // 获取 ById
  130. func (dao *StockOutProductDaoImpl) Read_StockOutProduct_List(T_number string) (r []StockOutProduct) {
  131. qs := dao.orm.QueryTable(new(StockOutProduct))
  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 *StockOutProductDaoImpl) Update_StockOutProduct(r StockOutProduct) error {
  140. qs := dao.orm.QueryTable(new(StockOutProduct))
  141. var product StockOutProduct
  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 *StockOutProductDaoImpl) Update_StockOutProduct_T_date(T_number, T_date string) error {
  167. qs := dao.orm.QueryTable(new(StockOutProduct))
  168. var list []StockOutProduct
  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 *StockOutProductDaoImpl) Delete_StockOutProduct(T_number string, T_depot_id, T_product_id int) error {
  186. qs := dao.orm.QueryTable(new(StockOutProduct))
  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 *StockOutProductDaoImpl) Read_StockOut_Total(T_depot_id, T_product_id int, T_date string) int {
  196. sql := "SELECT SUM(t_num) FROM stock_out_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. }