Stock.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package Stock
  2. import (
  3. "ERP_storage/models/Basic"
  4. "errors"
  5. "fmt"
  6. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  8. "strconv"
  9. "sync"
  10. "time"
  11. "ERP_storage/logs"
  12. _ "github.com/astaxie/beego/cache/redis"
  13. "github.com/beego/beego/v2/adapter/orm"
  14. _ "github.com/go-sql-driver/mysql"
  15. )
  16. // 库存
  17. type Stock struct {
  18. Id int `orm:"column(ID);size(11);auto;pk"`
  19. T_depot_id int `orm:"size(20);null"` // 仓库id
  20. T_product_id int `orm:"size(20);null"` // 产品id
  21. T_product_class int `orm:"size(20);null"` // 产品分类
  22. T_product_name string `orm:"size(256);null"` // 产品名称
  23. T_product_model string `orm:"size(256);null"` // 产品型号
  24. T_total int `orm:"size(20);null"` // 库存数量
  25. T_reality int `orm:"size(20);null"` // 盘点数量
  26. T_sort int `orm:"size(20);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 *Stock) TableName() string {
  31. return "stock" // 数据库名称 // ************** 替换 FormulaList **************
  32. }
  33. type StockDaoImpl struct {
  34. orm orm.Ormer
  35. }
  36. func NewStock(orm orm.Ormer) *StockDaoImpl {
  37. return &StockDaoImpl{orm: orm}
  38. }
  39. var Stock_list *sync.Map
  40. func init() {
  41. //注册模型
  42. orm.RegisterModel(new(Stock))
  43. Stock_list = new(sync.Map)
  44. }
  45. type Stock_R struct {
  46. Id int
  47. T_depot_id int // 仓库id
  48. T_total int // 库存数量
  49. T_reality int // 实际库存数量
  50. T_sort int // 排序
  51. // ---------产品信息-----------
  52. T_product_id int
  53. T_product_name string
  54. T_product_class int
  55. T_product_class_name string
  56. T_product_model string
  57. T_product_spec string
  58. T_product_relation_sn int
  59. T_product_img string
  60. }
  61. func StockToStock_R(t Stock) (r Stock_R) {
  62. r.Id = t.Id
  63. r.T_depot_id = t.T_depot_id
  64. r.T_total = t.T_total
  65. r.T_reality = t.T_reality
  66. r.T_sort = t.T_sort
  67. r.T_product_id = t.T_product_id
  68. r.T_product_class = t.T_product_class
  69. product, _ := Basic.Read_Product_ById(t.T_product_id)
  70. r.T_product_name = product.T_name
  71. r.T_product_class_name = Basic.Read_ProductClass_Get(product.T_class)
  72. r.T_product_model = product.T_model
  73. r.T_product_spec = product.T_spec
  74. r.T_product_relation_sn = product.T_relation_sn
  75. r.T_product_img = product.T_img
  76. return r
  77. }
  78. // 添加
  79. func (dao *StockDaoImpl) Add_Stock(r Stock) (id int64, err error) {
  80. id, err = dao.orm.Insert(&r)
  81. if err != nil {
  82. logs.Error(lib.FuncName(), err)
  83. }
  84. return id, err
  85. }
  86. // T_type 1-出库 2-入库
  87. func (dao *StockDaoImpl) AddOrUpdate_Stock(T_depot_id, T_product_id, T_product_class int, T_product_name, T_product_model string, T_num, T_type int) (id int64, err error) {
  88. qs := dao.orm.QueryTable(new(Stock))
  89. var stock Stock
  90. err = qs.Filter("T_depot_id", T_depot_id).Filter("T_product_id", T_product_id).One(&stock)
  91. if err != nil {
  92. if err.Error() == orm.ErrNoRows.Error() {
  93. r := Stock{
  94. T_depot_id: T_depot_id,
  95. T_product_id: T_product_id,
  96. T_product_name: T_product_name,
  97. T_product_class: T_product_class,
  98. T_product_model: T_product_model,
  99. T_total: T_num,
  100. T_reality: T_num,
  101. }
  102. id, err = dao.orm.Insert(&r)
  103. if err != nil {
  104. err = errors.New("添加库存失败")
  105. logs.Error(lib.FuncName(), err)
  106. }
  107. return
  108. } else {
  109. err = errors.New("查询库存失败")
  110. logs.Error(lib.FuncName(), err)
  111. return
  112. }
  113. }
  114. // T_type 1-出库 2-入库
  115. if T_type == 1 {
  116. stock.T_total -= T_num
  117. stock.T_reality -= T_num
  118. }
  119. if stock.T_total < 0 {
  120. err = errors.New(fmt.Sprintf("%s%s库存不足", T_product_name, T_product_model))
  121. logs.Error(lib.FuncName(), err.Error())
  122. return
  123. }
  124. if T_type == 2 {
  125. stock.T_total += T_num
  126. stock.T_reality += T_num
  127. }
  128. _, err = dao.orm.Update(&stock, "T_total", "T_reality")
  129. if err != nil {
  130. err = errors.New(fmt.Sprintf("修改库存失败", T_product_name, T_product_model))
  131. logs.Error(lib.FuncName(), err)
  132. return
  133. }
  134. return
  135. }
  136. // 获取 ById
  137. func (dao *StockDaoImpl) Read_Stock_ByT_depot_id_T_product_id(T_depot_id, T_product_id int) (r Stock, err error) {
  138. qs := dao.orm.QueryTable(new(Stock))
  139. err = qs.Filter("T_depot_id", T_depot_id).Filter("T_product_id", T_product_id).One(&r)
  140. if err != nil {
  141. logs.Error(lib.FuncName(), err)
  142. }
  143. return
  144. }
  145. // 同步库存产品信息
  146. func (dao *StockDaoImpl) Read_Stock_List_ByT_product_id(T_product_id int) (r []Stock, err error) {
  147. qs := dao.orm.QueryTable(new(Stock))
  148. _, err = qs.Filter("T_product_id", T_product_id).All(&r)
  149. if err != nil {
  150. logs.Error(lib.FuncName(), err)
  151. }
  152. return
  153. }
  154. // 获取 ById
  155. func (dao *StockDaoImpl) Read_Stock_ById(Id int) (r Stock, err error) {
  156. o := orm.NewOrm()
  157. qs := o.QueryTable(new(Stock))
  158. err = qs.Filter("Id", Id).One(&r)
  159. if err != nil {
  160. logs.Error(lib.FuncName(), err)
  161. }
  162. return
  163. }
  164. // 修改
  165. func (dao *StockDaoImpl) Update_Stock(m Stock, cols ...string) error {
  166. _, err := dao.orm.Update(&m, cols...)
  167. if err != nil {
  168. logs.Error(lib.FuncName(), err)
  169. return err
  170. }
  171. return nil
  172. }
  173. // 获取列表
  174. func (dao *StockDaoImpl) Read_Stock_List(T_depot_id, T_product_class int, T_product_name, T_product_model string, page, page_z int) (r_ []Stock_R, cnt int64) {
  175. qs := dao.orm.QueryTable(new(Stock))
  176. var offset int64
  177. if page <= 1 {
  178. offset = 0
  179. } else {
  180. offset = int64((page - 1) * page_z)
  181. }
  182. // 过滤
  183. cond := orm.NewCondition()
  184. if T_depot_id > 0 {
  185. cond = cond.And("T_depot_id", T_depot_id)
  186. }
  187. if T_product_class > 0 {
  188. cond = cond.And("T_product_class", T_product_class)
  189. }
  190. if len(T_product_name) > 0 {
  191. cond = cond.And("T_product_name__icontains", T_product_name)
  192. }
  193. if len(T_product_model) > 0 {
  194. cond = cond.And("T_product_model__icontains", T_product_model)
  195. }
  196. // 查询
  197. var r []Stock
  198. var err error
  199. if page_z == 9999 {
  200. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort").All(&r)
  201. } else {
  202. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("T_sort").All(&r)
  203. }
  204. if err != nil {
  205. logs.Error(lib.FuncName(), err)
  206. return
  207. }
  208. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  209. if err != nil {
  210. logs.Error(lib.FuncName(), err)
  211. return
  212. }
  213. for _, v := range r {
  214. r_ = append(r_, StockToStock_R(v))
  215. }
  216. return r_, cnt
  217. }
  218. // 获取仓库的货物总数量
  219. func (dao *StockDaoImpl) Read_Stock_Count_ByT_depot_id(T_depot_id int) (cnt int64) {
  220. sql := "SELECT SUM(t_total) FROM stock WHERE t_depot_id = " + strconv.Itoa(T_depot_id)
  221. var pl_lists orm2.ParamsList
  222. _, err := dao.orm.Raw(sql).ValuesFlat(&pl_lists)
  223. if err != nil {
  224. logs.Error(lib.FuncName(), err)
  225. return
  226. }
  227. if pl_lists[0] == nil {
  228. return 0
  229. }
  230. key, _ := strconv.ParseInt(pl_lists[0].(string), 10, 64)
  231. return key
  232. }
  233. // 获取全部
  234. func Read_Stock_All_Map(T_depot_id int) {
  235. o := orm.NewOrm()
  236. var r []Stock
  237. qs := o.QueryTable(new(Stock))
  238. _, err := qs.Filter("T_depot_id", T_depot_id).All(&r)
  239. if err != nil {
  240. logs.Error(lib.FuncName(), err)
  241. }
  242. for _, v := range r {
  243. key := fmt.Sprintf("%d-%d", v.T_depot_id, v.T_product_id)
  244. Stock_list.Store(key, v.T_reality)
  245. }
  246. }
  247. func Read_Stock_Get(T_depot_id, T_product_id int) int {
  248. // 有先加入 给全部人发消息
  249. key := fmt.Sprintf("%d-%d", T_depot_id, T_product_id)
  250. v, ok := Stock_list.Load(key) /*如果确定是真实的,则存在,否则不存在 */
  251. if ok {
  252. return v.(int)
  253. } else {
  254. return 0
  255. }
  256. }