StockIn.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package Stock
  2. import (
  3. "ERP_storage/models/Account"
  4. "ERP_storage/models/Basic"
  5. "fmt"
  6. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  8. "strconv"
  9. "time"
  10. "ERP_storage/logs"
  11. _ "github.com/astaxie/beego/cache/redis"
  12. "github.com/beego/beego/v2/adapter/orm"
  13. _ "github.com/go-sql-driver/mysql"
  14. )
  15. // 入库
  16. type StockIn struct {
  17. Id int `orm:"column(ID);size(11);auto;pk"`
  18. T_number string `orm:"size(256);null"` // 入库单号
  19. T_type int `orm:"size(256);;default(1)"` // 入库类型 1-入库 2-退库
  20. T_depot_id int `orm:"size(20);null"` // 仓库id
  21. T_date string `orm:"size(256);null"` // 业务日期
  22. T_submit string `orm:"size(256);null"` // 经办人
  23. T_remark string `orm:"type(text);null"` // 备注
  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 *StockIn) TableName() string {
  28. return "stock_in" // 数据库名称 // ************** 替换 FormulaList **************
  29. }
  30. type StockInDaoImpl struct {
  31. orm orm.Ormer
  32. }
  33. func NewStockIn(orm orm.Ormer) *StockInDaoImpl {
  34. return &StockInDaoImpl{orm: orm}
  35. }
  36. func init() {
  37. //注册模型
  38. orm.RegisterModel(new(StockIn))
  39. }
  40. type StockIn_R struct {
  41. Id int
  42. T_number string
  43. T_type int
  44. T_depot_id int
  45. T_depot_name string
  46. T_date string
  47. T_submit string
  48. T_submit_name string
  49. T_remark string
  50. }
  51. type StockIn_Detail struct {
  52. Id int
  53. T_number string
  54. T_type int
  55. T_depot_id int
  56. T_depot_name string
  57. T_date string
  58. T_submit string
  59. T_submit_name string
  60. T_remark string
  61. T_Product []StockInProduct_R
  62. }
  63. func StockInToStockIn_R(t StockIn) (r StockIn_R) {
  64. r.Id = t.Id
  65. r.T_number = t.T_number
  66. r.T_type = t.T_type
  67. r.T_depot_id = t.T_depot_id
  68. r.T_depot_name = Basic.Read_Depot_Get(t.T_depot_id)
  69. r.T_date = t.T_date
  70. r.T_submit = t.T_submit
  71. r.T_submit_name = Account.Read_User_T_name_Get(t.T_submit)
  72. r.T_remark = t.T_remark
  73. return r
  74. }
  75. func StockInToStockIn_Detail(t StockIn, productList []StockInProduct_R) (r StockIn_Detail) {
  76. r.Id = t.Id
  77. r.T_number = t.T_number
  78. r.T_type = t.T_type
  79. r.T_depot_id = t.T_depot_id
  80. r.T_depot_name = Basic.Read_Depot_Get(t.T_depot_id)
  81. r.T_date = t.T_date
  82. r.T_submit = t.T_submit
  83. r.T_submit_name = Account.Read_User_T_name_Get(t.T_submit)
  84. r.T_remark = t.T_remark
  85. r.T_Product = productList
  86. return r
  87. }
  88. // 添加
  89. func (dao *StockInDaoImpl) Add_StockIn(r StockIn) (id int64, err error) {
  90. id, err = dao.orm.Insert(&r)
  91. if err != nil {
  92. logs.Error(lib.FuncName(), err)
  93. }
  94. return id, err
  95. }
  96. // 获取 ById
  97. func (dao *StockInDaoImpl) Read_StockIn_ByT_number(T_number string) (r StockIn, err error) {
  98. qs := dao.orm.QueryTable(new(StockIn))
  99. err = qs.Filter("T_number", T_number).One(&r)
  100. if err != nil {
  101. logs.Error(lib.FuncName(), err)
  102. }
  103. return
  104. }
  105. // 修改
  106. func (dao *StockInDaoImpl) Update_StockIn(m StockIn, cols ...string) error {
  107. _, err := dao.orm.Update(&m, cols...)
  108. if err != nil {
  109. logs.Error(lib.FuncName(), err)
  110. return err
  111. }
  112. return nil
  113. }
  114. // 删除
  115. func (dao *StockInDaoImpl) Delete_StockIn(m StockIn) error {
  116. _, err := dao.orm.Delete(&m)
  117. if err != nil {
  118. logs.Error(lib.FuncName(), err)
  119. return err
  120. }
  121. return nil
  122. }
  123. // 获取列表
  124. func (dao *StockInDaoImpl) Read_StockIn_List(T_depot_id int, T_start_date, T_end_date string, page, page_z int) (r_ []StockIn_R, cnt int64) {
  125. qs := dao.orm.QueryTable(new(StockIn))
  126. var offset int64
  127. if page <= 1 {
  128. offset = 0
  129. } else {
  130. offset = int64((page - 1) * page_z)
  131. }
  132. // 过滤
  133. cond := orm.NewCondition()
  134. if T_depot_id > 0 {
  135. cond = cond.And("T_depot_id", T_depot_id)
  136. }
  137. if len(T_start_date) > 0 {
  138. cond = cond.And("T_date__gte", T_start_date)
  139. }
  140. if len(T_end_date) > 0 {
  141. cond = cond.And("T_date__lte", T_end_date)
  142. }
  143. // 查询
  144. var r []StockIn
  145. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r)
  146. if err != nil {
  147. logs.Error(lib.FuncName(), err)
  148. return
  149. }
  150. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  151. if err != nil {
  152. logs.Error(lib.FuncName(), err)
  153. return
  154. }
  155. for _, v := range r {
  156. r_ = append(r_, StockInToStockIn_R(v))
  157. }
  158. return r_, cnt
  159. }
  160. // 通过入库编号查询入库产品列表
  161. func (dao *StockInDaoImpl) Read_StockInProduct_List(T_name, T_start_date, T_end_date string, T_depot_id int,
  162. T_product_name, T_product_model string, page int, page_z int) (
  163. r_ []StockInProductList, cnt int64) {
  164. var offset int
  165. if page <= 1 {
  166. offset = 0
  167. } else {
  168. offset = (page - 1) * page_z
  169. }
  170. var maps_z []orm2.ParamsList
  171. var r []StockInProductList
  172. sqlWhere := ""
  173. if len(T_name) > 0 {
  174. sqlWhere += " AND (si.t_number like \"%" + T_name + "%\" or si.t_remark like \"%" + T_name + "%\")"
  175. }
  176. if T_depot_id > 0 {
  177. sqlWhere += fmt.Sprintf(" AND si.t_depot_id = %d", T_depot_id)
  178. }
  179. if len(T_start_date) > 0 {
  180. sqlWhere += " AND si.t_date >= \"" + T_start_date + "\""
  181. }
  182. if len(T_end_date) > 0 {
  183. sqlWhere += " AND si.t_date <= \"" + T_end_date + "\""
  184. }
  185. if len(T_product_name) > 0 {
  186. sqlWhere += " AND p.t_name like \"%" + T_product_name + "%\""
  187. }
  188. if len(T_product_model) > 0 {
  189. sqlWhere += " AND p.t_model like \"%" + T_product_model + "%\""
  190. }
  191. // 获取总条数
  192. sql := "SELECT COUNT(product.ID) FROM stock_in_product product " +
  193. "JOIN product p ON product.t_product_id = p.ID " +
  194. "LEFT JOIN stock_in si ON si.t_number=product.t_number WHERE 1=1 "
  195. sql = sql + sqlWhere
  196. fmt.Println(sql)
  197. _, err := dao.orm.Raw(sql).ValuesList(&maps_z)
  198. if err != nil {
  199. return r, 0
  200. }
  201. if len(maps_z) == 0 {
  202. return r, 0
  203. }
  204. sql = "SELECT si.t_number as t_number,si.t_depot_id as t_depot_id,si.t_date as t_date,product.*,si.* FROM stock_in_product product " +
  205. "JOIN product p ON product.t_product_id = p.ID " +
  206. "LEFT JOIN stock_in si ON si.t_number=product.t_number WHERE 1=1"
  207. sql = sql + sqlWhere
  208. sql += " ORDER BY si.t_date DESC"
  209. if page_z != 9999 {
  210. sql += " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
  211. }
  212. fmt.Println(sql)
  213. _, err = dao.orm.Raw(sql).QueryRows(&r)
  214. for _, v := range r {
  215. r_ = append(r_, StockInProductListToStockInProductList(v))
  216. }
  217. count, _ := strconv.Atoi(maps_z[0][0].(string))
  218. return r_, int64(count)
  219. }