StockOutProduct.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 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. T_state int `orm:"size(2);column(t_state);default(1)"` // 1已出库 2待出库
  24. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  25. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  26. }
  27. func (t *StockOutProduct) TableName() string {
  28. return "stock_out_product" // 数据库名称 // ************** 替换 FormulaList **************
  29. }
  30. type StockOutProductDaoImpl struct {
  31. orm orm.Ormer
  32. }
  33. func NewStockOutProduct(orm orm.Ormer) *StockOutProductDaoImpl {
  34. return &StockOutProductDaoImpl{orm: orm}
  35. }
  36. func init() {
  37. //注册模型
  38. orm.RegisterModel(new(StockOutProduct))
  39. }
  40. type StockOutProduct_R struct {
  41. Id int
  42. T_number string // 入库单号
  43. T_depot_id int // 仓库id
  44. T_num int // 入库数量
  45. T_date string // 业务日期
  46. T_device_list []string // sn关联列表
  47. // ---------产品信息-----------
  48. T_product_id int // 产品id
  49. T_product_name string // 产品名称
  50. T_product_class_name string // 产品分类名称
  51. T_product_model string // 产品型号
  52. T_product_spec string // 产品规格
  53. T_product_relation_sn int // 是否关联sn 0-否 1-是
  54. T_product_img string // 产品图片
  55. }
  56. func StockOutProductToStockOutProduct_R(t StockOutProduct) (r StockOutProduct_R) {
  57. r.Id = t.Id
  58. r.T_number = t.T_number
  59. r.T_depot_id = t.T_depot_id
  60. r.T_num = t.T_num
  61. r.T_date = t.T_date
  62. //r.T_relation_sn = t.T_relation_sn
  63. r.T_device_list = lib.SplitString(t.T_relation_sn, ",")
  64. r.T_product_id = t.T_product_id
  65. product, _ := Basic.Read_Product_ById(t.T_product_id)
  66. r.T_product_name = product.T_name
  67. r.T_product_class_name = Basic.Read_ProductClass_Get(product.T_class)
  68. r.T_product_model = product.T_model
  69. r.T_product_spec = product.T_spec
  70. r.T_product_relation_sn = product.T_relation_sn
  71. r.T_product_img = product.T_img
  72. return r
  73. }
  74. type StockOutProductList struct {
  75. Id int
  76. T_number string // StockOut - 出库单号
  77. T_depot_id int // StockOut - 仓库id
  78. T_depot_name string // StockOut - 仓库名称
  79. T_date string // StockOut - 业务日期
  80. T_submit string // StockOut - 经办人
  81. T_remark string // StockOut - 备注
  82. T_project string // StockOut - 项目
  83. T_receive string // StockOut - 领取人
  84. T_receive_name string // 领取人名称
  85. T_num int // 入库数量
  86. T_relation_sn string // 关联sn,20220101,20230202
  87. T_device_list []string // sn关联列表
  88. // ---------产品信息-----------
  89. T_product_id int
  90. T_product_name string
  91. T_product_class_name string
  92. T_product_model string
  93. T_product_spec string
  94. T_product_relation_sn int
  95. T_product_img string
  96. }
  97. func StockOutProductListToStockOutProductList(t StockOutProductList) (r StockOutProductList) {
  98. r.Id = t.Id
  99. r.T_number = t.T_number
  100. r.T_depot_id = t.T_depot_id
  101. r.T_depot_name = Basic.Read_Depot_Get(t.T_depot_id)
  102. r.T_num = t.T_num
  103. r.T_date = t.T_date
  104. r.T_receive = t.T_receive
  105. r.T_receive_name = Account.Read_User_T_name_Get(t.T_receive)
  106. r.T_submit = t.T_submit
  107. r.T_remark = t.T_remark
  108. r.T_project = t.T_project
  109. r.T_num = t.T_num
  110. r.T_relation_sn = t.T_relation_sn
  111. r.T_device_list = lib.SplitString(t.T_relation_sn, ",")
  112. r.T_product_id = t.T_product_id
  113. product, _ := Basic.Read_Product_ById(t.T_product_id)
  114. r.T_product_name = product.T_name
  115. r.T_product_class_name = Basic.Read_ProductClass_Get(product.T_class)
  116. r.T_product_model = product.T_model
  117. r.T_product_spec = product.T_spec
  118. r.T_product_relation_sn = product.T_relation_sn
  119. r.T_product_img = product.T_img
  120. return r
  121. }
  122. // 添加
  123. func (dao *StockOutProductDaoImpl) Add_StockOutProduct(r StockOutProduct) (id int64, err error) {
  124. id, err = dao.orm.Insert(&r)
  125. if err != nil {
  126. logs.Error(lib.FuncName(), err)
  127. }
  128. return id, err
  129. }
  130. // 获取 ById
  131. func (dao *StockOutProductDaoImpl) Read_StockOutProduct_List(T_number string) (r []StockOutProduct) {
  132. qs := dao.orm.QueryTable(new(StockOutProduct))
  133. _, err := qs.Filter("T_number", T_number).All(&r)
  134. if err != nil {
  135. logs.Error(lib.FuncName(), err)
  136. }
  137. return
  138. }
  139. // 修改
  140. func (dao *StockOutProductDaoImpl) Update_StockOutProduct(r StockOutProduct) error {
  141. qs := dao.orm.QueryTable(new(StockOutProduct))
  142. var product StockOutProduct
  143. err := qs.Filter("T_number", r.T_number).Filter("T_product_id", r.T_product_id).One(&product)
  144. if err != nil {
  145. if err.Error() == orm.ErrNoRows.Error() {
  146. _, err = dao.orm.Insert(&product)
  147. if err != nil {
  148. logs.Error(lib.FuncName(), err)
  149. return err
  150. } else {
  151. return nil
  152. }
  153. }
  154. logs.Error(lib.FuncName(), err)
  155. return err
  156. }
  157. product.T_depot_id = r.T_depot_id
  158. product.T_num = r.T_num
  159. product.T_relation_sn = r.T_relation_sn
  160. product.T_state = r.T_state
  161. product.T_date = r.T_date
  162. _, err = dao.orm.Update(&product, "T_depot_id", "T_num", "T_relation_sn", "T_state", "T_date")
  163. if err != nil {
  164. logs.Error(lib.FuncName(), err)
  165. return err
  166. }
  167. return nil
  168. }
  169. func (dao *StockOutProductDaoImpl) Update_StockOutProduct_T_date(T_number, T_date string) error {
  170. qs := dao.orm.QueryTable(new(StockOutProduct))
  171. var list []StockOutProduct
  172. _, err := qs.Filter("T_number", T_number).All(&list)
  173. if err != nil {
  174. logs.Error(lib.FuncName(), err)
  175. return err
  176. }
  177. for _, product := range list {
  178. product.T_date = T_date
  179. _, err = dao.orm.Update(&product, "T_date")
  180. if err != nil {
  181. logs.Error(lib.FuncName(), err)
  182. return err
  183. }
  184. }
  185. return nil
  186. }
  187. // 删除
  188. func (dao *StockOutProductDaoImpl) Delete_StockOutProduct(T_number string, T_depot_id, T_product_id int) error {
  189. qs := dao.orm.QueryTable(new(StockOutProduct))
  190. _, err := qs.Filter("T_number", T_number).Filter("T_depot_id", T_depot_id).Filter("T_product_id", T_product_id).Delete()
  191. if err != nil {
  192. logs.Error(lib.FuncName(), err)
  193. return err
  194. }
  195. return nil
  196. }
  197. // 根据库存id、产品id、月份获取本月出库总数量
  198. func (dao *StockOutProductDaoImpl) Read_StockOut_Total(T_depot_id, T_product_id int, T_date string) int {
  199. sql := "SELECT SUM(t_num) FROM stock_out_product WHERE t_depot_id = " + strconv.Itoa(T_depot_id) + " AND t_state=1 AND t_product_id = " + strconv.Itoa(T_product_id) + " AND t_date like '" + T_date + "%'"
  200. var pl_lists orm2.ParamsList
  201. _, err := dao.orm.Raw(sql).ValuesFlat(&pl_lists)
  202. if err != nil {
  203. logs.Error(lib.FuncName(), err)
  204. return 0
  205. }
  206. if pl_lists[0] == nil {
  207. return 0
  208. }
  209. key, _ := strconv.Atoi(pl_lists[0].(string))
  210. return key
  211. }