stock_service.go 54KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func AddSigleDealer(dealer *models.Dealer) (error, *models.Dealer) {
  11. err := writeDb.Create(&dealer).Error
  12. return err, dealer
  13. }
  14. func ModifyDealer(dealer *models.Dealer) error {
  15. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", dealer.ID).Updates(map[string]interface{}{
  16. "dealer_name": dealer.DealerName,
  17. "contact": dealer.Contact,
  18. "contact_phone": dealer.ContactPhone,
  19. "platform_number": dealer.PlatformNumber,
  20. "email": dealer.Email,
  21. "contact_address": dealer.ContactAddress,
  22. "modifier": dealer.Modifier,
  23. "mtime": time.Now().Unix(),
  24. "remark": dealer.Remark,
  25. }).Error
  26. return err
  27. }
  28. func FindAllDealerTotal(orgId int64) (total int64) {
  29. db := readDb.Model(&models.Dealer{})
  30. db = db.Where("org_id = ? ", orgId).Count(&total)
  31. return
  32. }
  33. func FindAllDealerList(orgId int64, page int64, limit int64) (list []*models.Dealer, total int64, err error) {
  34. offset := (page - 1) * limit
  35. db := readDb.Model(&models.Dealer{})
  36. db = db.Where("org_id = ? AND status = 1", orgId)
  37. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  38. return
  39. }
  40. func DeleteDealerById(id int64) error {
  41. err := writeDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  42. return err
  43. }
  44. func FindDealerById(id int64) (*models.Dealer, error) {
  45. dealer := &models.Dealer{}
  46. err := readDb.Model(&models.Dealer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  47. return dealer, err
  48. }
  49. func AddSigleManufacturer(manufacturer *models.Manufacturer) (error, *models.Manufacturer) {
  50. err := writeDb.Create(&manufacturer).Error
  51. return err, manufacturer
  52. }
  53. func ModifyManufacturer(manufacturer *models.Manufacturer) error {
  54. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", manufacturer.ID).Updates(map[string]interface{}{
  55. "manufacturer_name": manufacturer.ManufacturerName,
  56. "contact": manufacturer.Contact,
  57. "contact_phone": manufacturer.ContactPhone,
  58. "platform_number": manufacturer.PlatformNumber,
  59. "email": manufacturer.Email,
  60. "contact_address": manufacturer.ContactAddress,
  61. "modifier": manufacturer.Modifier,
  62. "mtime": time.Now().Unix(),
  63. "remark": manufacturer.Remark,
  64. }).Error
  65. return err
  66. }
  67. func FindAllManufacturerList(orgId int64, page int64, limit int64) (list []*models.Manufacturer, total int64, err error) {
  68. offset := (page - 1) * limit
  69. db := readDb.Model(&models.Manufacturer{})
  70. db = db.Where("org_id = ? AND status = 1", orgId)
  71. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  72. return
  73. }
  74. func DeleteManufacturerById(id int64) error {
  75. err := writeDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0}).Error
  76. return err
  77. }
  78. func FindManufacturerById(id int64) (*models.Manufacturer, error) {
  79. dealer := &models.Manufacturer{}
  80. err := readDb.Model(&models.Manufacturer{}).Where("id = ? AND status = 1", id).First(&dealer).Error
  81. return dealer, err
  82. }
  83. func FindManufacturerTotal(orgId int64) (total int64) {
  84. db := readDb.Model(&models.Manufacturer{})
  85. db = db.Where("org_id = ?", orgId).Count(&total)
  86. return
  87. }
  88. func FindAllDealer(orgId int64) (dealers []*models.Dealer, err error) {
  89. err = readDb.Model(&models.Dealer{}).Where("org_id = ? AND status = 1", orgId).Find(&dealers).Error
  90. return dealers, err
  91. }
  92. func FindAllManufacturer(orgId int64) (manufacturer []*models.Manufacturer, err error) {
  93. err = readDb.Model(&models.Manufacturer{}).Where("org_id = ? AND status = 1", orgId).Find(&manufacturer).Error
  94. return manufacturer, err
  95. }
  96. func FindAllGoodTypeTotal(orgId int64) (total int64) {
  97. db := readDb.Model(&models.GoodsType{})
  98. db = db.Where("org_id = ?", orgId).Count(&total)
  99. return
  100. }
  101. func AddSigleGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  102. err := writeDb.Create(&goodType).Error
  103. return err, goodType
  104. }
  105. func ModifyGoodType(goodType *models.GoodsType) (error, *models.GoodsType) {
  106. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", goodType.ID).Updates(map[string]interface{}{
  107. "modifier": goodType.Modifier,
  108. "mtime": time.Now().Unix(),
  109. "remark": goodType.Remark,
  110. "type_name": goodType.TypeName,
  111. }).Error
  112. return err, goodType
  113. }
  114. func FindAllGoodTypeList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodsType, total int64, err error) {
  115. offset := (page - 1) * limit
  116. db := readDb.Model(&models.GoodsType{})
  117. db = db.Where("(org_id=? AND status = 1) OR (type = 1 AND status = 1)", orgId)
  118. if len(keyword) > 0 {
  119. likeKey := "%" + keyword + "%"
  120. db = db.Where("type_code LIKE ? OR type_name LIKE ?", likeKey, likeKey)
  121. }
  122. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  123. return
  124. }
  125. func DeleteGoodTypeById(id int64, operateId int64) error {
  126. err := writeDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  127. writeDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId})
  128. return err
  129. }
  130. func FindGoodTypeById(id int64) (*models.GoodsType, error) {
  131. goodType := &models.GoodsType{}
  132. err := readDb.Model(&models.GoodsType{}).Where("id = ? AND status = 1", id).First(&goodType).Error
  133. return goodType, err
  134. }
  135. func FindAllGoodInfoTotal(orgId int64) (total int64) {
  136. db := readDb.Model(&models.GoodInfo{})
  137. db = db.Where("org_id = ?", orgId).Count(&total)
  138. return
  139. }
  140. func AddSigleGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  141. err := writeDb.Create(&goodInfo).Error
  142. return err, goodInfo
  143. }
  144. func ModifyGoodInfo(goodInfo *models.GoodInfo) (error, *models.GoodInfo) {
  145. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", goodInfo.ID).Updates(map[string]interface{}{
  146. "modifier": goodInfo.Modifier,
  147. "mtime": time.Now().Unix(),
  148. "remark": goodInfo.Remark,
  149. "specification_name": goodInfo.SpecificationName,
  150. "good_unit": goodInfo.GoodUnit,
  151. "buy_price": goodInfo.BuyPrice,
  152. "sell_price": goodInfo.SellPrice,
  153. "manufacturer": goodInfo.Manufacturer,
  154. "dealer": goodInfo.Dealer,
  155. "expiry_date_warn_day_count": goodInfo.ExpiryDateWarnDayCount,
  156. "stock_warn_count": goodInfo.StockWarnCount,
  157. "is_reuse": goodInfo.IsReuse,
  158. }).Error
  159. return err, goodInfo
  160. }
  161. func FindGoodInfoList(orgId int64, page int64, limit int64, keyword string) (list []*models.GoodInfo, total int64, err error) {
  162. offset := (page - 1) * limit
  163. db := readDb.Model(&models.GoodInfo{})
  164. db = db.Where("org_id = ? AND status = 1", orgId)
  165. if len(keyword) > 0 {
  166. likeKey := "%" + keyword + "%"
  167. db = db.Where("good_code LIKE ? OR specification_name LIKE ?", likeKey, likeKey)
  168. }
  169. err = db.Count(&total).Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  170. return
  171. }
  172. func DeleteGoodInfoById(id int64, operateId int64) error {
  173. err := writeDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).Updates(map[string]interface{}{"mtime": time.Now().Unix(), "status": 0, "modifier": operateId}).Error
  174. return err
  175. }
  176. func FindGoodInfoByGoodId(id int64) (list []*models.GoodInfo, err error) {
  177. err = readDb.Model(&models.GoodInfo{}).Where("good_type_id = ? AND status = 1", id).Find(&list).Error
  178. return list, err
  179. }
  180. func FindGoodInfoById(id int64) (*models.GoodInfo, error) {
  181. goodInfo := &models.GoodInfo{}
  182. err := readDb.Model(&models.GoodInfo{}).Where("id = ? AND status = 1", id).First(&goodInfo).Error
  183. return goodInfo, err
  184. }
  185. func FindAllGoodType(org_id int64) (goodType []*models.GoodsType, err error) {
  186. err = readDb.Model(&models.GoodsType{}).Where("(org_id = ? AND status = 1) OR (type = 1 AND status = 1)", org_id).Find(&goodType).Error
  187. return goodType, err
  188. }
  189. func FindAllGoodInfo(org_id int64) (goodInfo []*models.GoodInfo, err error) {
  190. err = readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id).Find(&goodInfo).Error
  191. return goodInfo, err
  192. }
  193. func FindAllWarehouseTotal(org_id int64) (total int64, err error) {
  194. err = readDb.Model(&models.Warehousing{}).Where("org_id = ?", org_id).Count(&total).Error
  195. return total, err
  196. }
  197. func AddSigleWarehouse(warehouse *models.Warehousing) error {
  198. err := writeDb.Create(&warehouse).Error
  199. return err
  200. }
  201. func AddSigleSalesReturn(salesReturn *models.SalesReturn) error {
  202. err := writeDb.Create(&salesReturn).Error
  203. return err
  204. }
  205. func UpDateGoodReturnStatus(ids []string) {
  206. writeDb.Model(&models.WarehousingInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_return": 1})
  207. }
  208. func UpDateCanCelStockStatus(ids []string) {
  209. writeDb.Model(&models.WarehouseOutInfo{}).Where("id IN (?)", ids).Update(map[string]interface{}{"is_cancel": 1})
  210. }
  211. func CreateWarehousingInfo(warehousingInfo []*models.WarehousingInfo) (err error) {
  212. if len(warehousingInfo) > 0 {
  213. utx := writeDb.Begin()
  214. if len(warehousingInfo) > 0 {
  215. thisSQL := "INSERT INTO xt_warehouse_info (warehousing_id, good_id, good_type_id, number, product_date,expiry_date,warehousing_count,price,total_price,dealer,manufacturer,remark,ctime,mtime,status,org_id,warehousing_order,type) VALUES "
  216. insertParams := make([]string, 0)
  217. insertData := make([]interface{}, 0)
  218. for _, info := range warehousingInfo {
  219. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  220. insertData = append(insertData, info.WarehousingId)
  221. insertData = append(insertData, info.GoodId)
  222. insertData = append(insertData, info.GoodTypeId)
  223. insertData = append(insertData, info.Number)
  224. insertData = append(insertData, info.ProductDate)
  225. insertData = append(insertData, info.ExpiryDate)
  226. insertData = append(insertData, info.WarehousingCount)
  227. insertData = append(insertData, info.Price)
  228. insertData = append(insertData, info.TotalPrice)
  229. insertData = append(insertData, info.Dealer)
  230. insertData = append(insertData, info.Manufacturer)
  231. insertData = append(insertData, info.Remark)
  232. insertData = append(insertData, info.Ctime)
  233. insertData = append(insertData, info.Mtime)
  234. insertData = append(insertData, info.Status)
  235. insertData = append(insertData, info.OrgId)
  236. insertData = append(insertData, info.WarehousingOrder)
  237. insertData = append(insertData, info.Type)
  238. }
  239. thisSQL += strings.Join(insertParams, ", ")
  240. err = utx.Exec(thisSQL, insertData...).Error
  241. if err != nil {
  242. utx.Rollback()
  243. return
  244. }
  245. }
  246. utx.Commit()
  247. }
  248. return
  249. }
  250. func FindLastWarehousingInfo(order string) (info models.WarehousingInfo, err error) {
  251. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", order).Last(&info).Error
  252. return info, err
  253. }
  254. func FindAllWarehousingList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  255. db := readDb.Model(&models.Warehousing{})
  256. db = db.Where("xt_warehouse.org_id = ? AND xt_warehouse.status = 1 AND xt_warehouse.type = ?", orgId, types)
  257. if len(keywords) > 0 {
  258. likeKey := "%" + keywords + "%"
  259. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  260. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse.manufacturer")
  261. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_warehouse.warehousing_order LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse.id")
  262. }
  263. if startTime > 0 {
  264. db = db.Where("xt_warehouse.operation_time >=?", startTime)
  265. }
  266. if endTime > 0 {
  267. db = db.Where("xt_warehouse.operation_time<= ?", endTime)
  268. }
  269. db = db.Count(&total)
  270. offset := (page - 1) * limit
  271. err = db.Offset(offset).Limit(limit).Order("xt_warehouse.ctime desc").Find(&list).Error
  272. return
  273. }
  274. func FindWarehousingInfoById(id int64) (list []*models.WarehousingInfo, err error) {
  275. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_id = ? AND status = 1", id).Find(&list).Error
  276. return list, err
  277. }
  278. func FindWarehousingById(id int64) (warehousing models.Warehousing, err error) {
  279. err = readDb.Model(&models.Warehousing{}).Where("id = ? AND status = 1 ", id).First(&warehousing).Error
  280. return warehousing, err
  281. }
  282. func GetInfoByIds(ids []string) (info []*models.WarehousingInfo, err error) {
  283. err = readDb.Model(&models.WarehousingInfo{}).Preload("Warehousing", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  284. return
  285. }
  286. func GetWarehousOutInfoByIds(ids []string) (info []*models.WarehouseOutInfo, err error) {
  287. err = readDb.Model(&models.WarehouseOutInfo{}).Preload("WarehouseOut", "status = 1").Where("id in (?) AND status = 1", ids).Find(&info).Error
  288. return
  289. }
  290. func CreateSalesReturnInfo(salesReturnInfo []*models.SalesReturnInfo) (err error) {
  291. if len(salesReturnInfo) > 0 {
  292. utx := writeDb.Begin()
  293. if len(salesReturnInfo) > 0 {
  294. thisSQL := "INSERT INTO xt_sales_return_info (good_id, sales_return_id, good_type_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer) VALUES "
  295. insertParams := make([]string, 0)
  296. insertData := make([]interface{}, 0)
  297. for _, info := range salesReturnInfo {
  298. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  299. insertData = append(insertData, info.GoodId)
  300. insertData = append(insertData, info.SalesReturnId)
  301. insertData = append(insertData, info.GoodTypeId)
  302. insertData = append(insertData, info.Count)
  303. insertData = append(insertData, info.Price)
  304. insertData = append(insertData, info.Total)
  305. insertData = append(insertData, info.Ctime)
  306. insertData = append(insertData, info.Status)
  307. insertData = append(insertData, info.OrgId)
  308. insertData = append(insertData, info.OrderNumber)
  309. insertData = append(insertData, info.Type)
  310. insertData = append(insertData, info.Dealer)
  311. insertData = append(insertData, info.Manufacturer)
  312. }
  313. thisSQL += strings.Join(insertParams, ", ")
  314. err = utx.Exec(thisSQL, insertData...).Error
  315. if err != nil {
  316. utx.Rollback()
  317. return
  318. }
  319. }
  320. utx.Commit()
  321. }
  322. return
  323. }
  324. func FindAllSalesReturnTotal(org_id int64) (total int64, err error) {
  325. err = readDb.Model(&models.SalesReturn{}).Where("org_id = ?", org_id).Count(&total).Error
  326. return total, err
  327. }
  328. func FindAllCancelStockTotal(org_id int64) (total int64, err error) {
  329. err = readDb.Model(&models.CancelStock{}).Where("org_id = ?", org_id).Count(&total).Error
  330. return total, err
  331. }
  332. func FindAllReturnList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.SalesReturn, total int64, err error) {
  333. db := readDb.Model(&models.SalesReturn{})
  334. db = db.Where("xt_sales_return.org_id = ? AND xt_sales_return.status = 1 AND xt_sales_return.type = ?", orgId, types)
  335. if len(keywords) > 0 {
  336. likeKey := "%" + keywords + "%"
  337. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  338. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_sales_return.manufacturer")
  339. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_sales_return.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_sales_return.id")
  340. }
  341. if startTime > 0 {
  342. db = db.Where("xt_sales_return.opera_time >=?", startTime)
  343. }
  344. if endTime > 0 {
  345. db = db.Where("xt_sales_return.opera_time<= ?", endTime)
  346. }
  347. db = db.Count(&total)
  348. offset := (page - 1) * limit
  349. err = db.Offset(offset).Limit(limit).Order("xt_sales_return.ctime desc").Find(&list).Error
  350. return
  351. }
  352. func FindReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  353. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  354. return list, err
  355. }
  356. func FindCancelStockInfoById(id int64) (list []*models.CancelStockInfo, err error) {
  357. err = readDb.Model(&models.CancelStockInfo{}).Where("cancel_stock_id = ? AND status = 1", id).Find(&list).Error
  358. return list, err
  359. }
  360. func FindAllWarehousingInfo(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehousingInfo, total int64, err error) {
  361. db := readDb.Model(&models.WarehousingInfo{})
  362. db = db.Where("org_id = ? AND status = 1", orgId)
  363. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  364. if startTime > 0 {
  365. db = db.Where("ctime >=?", startTime)
  366. }
  367. if endTime > 0 {
  368. db = db.Where("ctime<= ?", endTime)
  369. }
  370. db = db.Count(&total)
  371. offset := (page - 1) * limit
  372. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  373. return list, total, err
  374. }
  375. func FindAllWarehouseOut(org_id int64) (total int64, err error) {
  376. err = readDb.Model(&models.WarehouseOut{}).Where("org_id = ?", org_id).Count(&total).Error
  377. return total, err
  378. }
  379. func AddSigleWarehouseOut(warehouseOut *models.WarehouseOut) error {
  380. err := writeDb.Create(&warehouseOut).Error
  381. return err
  382. }
  383. func CreateWarehousingOutInfo(warehouseOutInfo []*models.WarehouseOutInfo) (err error) {
  384. if len(warehouseOutInfo) > 0 {
  385. utx := writeDb.Begin()
  386. if len(warehouseOutInfo) > 0 {
  387. thisSQL := "INSERT INTO xt_warehouse_out_info (warehouse_out_id, good_id, good_type_id, product_date,expiry_date,count,price,total_price,remark,ctime,status,org_id,warehouse_out_order_number,type,dealer,manufacturer) VALUES "
  388. insertParams := make([]string, 0)
  389. insertData := make([]interface{}, 0)
  390. for _, info := range warehouseOutInfo {
  391. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  392. insertData = append(insertData, info.WarehouseOutId)
  393. insertData = append(insertData, info.GoodId)
  394. insertData = append(insertData, info.GoodTypeId)
  395. insertData = append(insertData, info.ProductDate)
  396. insertData = append(insertData, info.ExpiryDate)
  397. insertData = append(insertData, info.Count)
  398. insertData = append(insertData, info.Price)
  399. insertData = append(insertData, info.TotalPrice)
  400. insertData = append(insertData, info.Remark)
  401. insertData = append(insertData, info.Ctime)
  402. insertData = append(insertData, info.Status)
  403. insertData = append(insertData, info.OrgId)
  404. insertData = append(insertData, info.WarehouseOutOrderNumber)
  405. insertData = append(insertData, info.Type)
  406. insertData = append(insertData, info.Dealer)
  407. insertData = append(insertData, info.Manufacturer)
  408. }
  409. thisSQL += strings.Join(insertParams, ", ")
  410. err = utx.Exec(thisSQL, insertData...).Error
  411. if err != nil {
  412. utx.Rollback()
  413. return
  414. }
  415. }
  416. utx.Commit()
  417. }
  418. return
  419. }
  420. func FindLastWarehousingOutInfo(order string) (info models.WarehouseOutInfo, err error) {
  421. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", order).Last(&info).Error
  422. return info, err
  423. }
  424. func FindAllWarehouseOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.WarehouseOut, total int64, err error) {
  425. db := readDb.Model(&models.WarehouseOut{})
  426. db = db.Where("xt_warehouse_out.org_id = ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.type = ?", orgId, types)
  427. if len(keywords) > 0 {
  428. likeKey := "%" + keywords + "%"
  429. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  430. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse_out.manufacturer")
  431. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out.warehouse_out_order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_out.id")
  432. }
  433. if startTime > 0 {
  434. db = db.Where("xt_warehouse_out.ctime >=?", startTime)
  435. }
  436. if endTime > 0 {
  437. db = db.Where("xt_warehouse_out.ctime<= ?", endTime)
  438. }
  439. db = db.Count(&total)
  440. offset := (page - 1) * limit
  441. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out.ctime desc").Find(&list).Error
  442. return list, total, err
  443. return
  444. }
  445. func FindWarehouseOutInfoById(id int64) (list []*models.WarehouseOutInfo, err error) {
  446. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id = ? AND status = 1 AND count <> 0 AND good_id <> 0", id).Order("good_type_id desc").Find(&list).Error
  447. return list, err
  448. }
  449. func FindWarehouseInfoByGoodId(good_id int64) (int64, *models.WarehousingInfo) {
  450. info := &models.WarehousingInfo{}
  451. type WarehouseInfoCountAmount struct {
  452. Total int64
  453. }
  454. var result WarehouseInfoCountAmount
  455. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(warehousing_count) as total").Scan(&result)
  456. readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", good_id).First(info)
  457. return result.Total, info
  458. }
  459. func FindAllSalesReturnCountByGoodId(good_id int64) (int64, error) {
  460. type SalesReturnCountAmount struct {
  461. Total int64
  462. }
  463. var result SalesReturnCountAmount
  464. err = readDb.Model(&models.SalesReturnInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  465. return result.Total, err
  466. }
  467. func FindAllWarehouseOutInfoByGoodId(good_id int64) (int64, error) {
  468. type WarehouseOutInfoCountAmount struct {
  469. Total int64
  470. }
  471. var result WarehouseOutInfoCountAmount
  472. err = readDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  473. return result.Total, err
  474. }
  475. func FindAllCancleStockInfoByGoodId(good_id int64) (int64, error) {
  476. type CancleStockoCountAmount struct {
  477. Total int64
  478. }
  479. var result CancleStockoCountAmount
  480. err = readDb.Model(&models.CancelStockInfo{}).Where("good_id = ? AND status = 1", good_id).Select("sum(count) as total").Scan(&result).Error
  481. return result.Total, err
  482. }
  483. func FindWarehouseInfoByGoodTypeId(good_type_id int64) (models.WarehousingInfo, error) {
  484. info := models.WarehousingInfo{}
  485. err := readDb.Model(&models.WarehousingInfo{}).Where("good_type_id = ? AND status = 1", good_type_id).First(&info).Error
  486. return info, err
  487. }
  488. func AddSigleCancelStock(cancelStock *models.CancelStock) error {
  489. err := writeDb.Create(&cancelStock).Error
  490. return err
  491. }
  492. func CreateCancelStockInfo(cancelStockInfo []*models.CancelStockInfo) (err error) {
  493. if len(cancelStockInfo) > 0 {
  494. utx := writeDb.Begin()
  495. if len(cancelStockInfo) > 0 {
  496. thisSQL := "INSERT INTO xt_cancel_stock_info (good_id, cancel_stock_id, good_type_id, count, price,total,ctime,status,org_id,order_number,type,dealer,manufacturer) VALUES "
  497. insertParams := make([]string, 0)
  498. insertData := make([]interface{}, 0)
  499. for _, info := range cancelStockInfo {
  500. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  501. insertData = append(insertData, info.GoodId)
  502. insertData = append(insertData, info.CancelStockId)
  503. insertData = append(insertData, info.GoodTypeId)
  504. insertData = append(insertData, info.Count)
  505. insertData = append(insertData, info.Price)
  506. insertData = append(insertData, info.Total)
  507. insertData = append(insertData, info.Ctime)
  508. insertData = append(insertData, info.Status)
  509. insertData = append(insertData, info.OrgId)
  510. insertData = append(insertData, info.OrderNumber)
  511. insertData = append(insertData, info.Type)
  512. insertData = append(insertData, info.Dealer)
  513. insertData = append(insertData, info.Manufacturer)
  514. }
  515. thisSQL += strings.Join(insertParams, ", ")
  516. err = utx.Exec(thisSQL, insertData...).Error
  517. if err != nil {
  518. utx.Rollback()
  519. return
  520. }
  521. }
  522. utx.Commit()
  523. }
  524. return
  525. }
  526. func FindAllCancelStockList(orgId int64) (list []*models.CancelStock, err error) {
  527. db := readDb.Model(&models.CancelStock{})
  528. db = db.Where("org_id = ? AND status = 1", orgId)
  529. err = db.Order("ctime desc").Find(&list).Error
  530. return
  531. }
  532. func FindAllStockOutList(orgId int64, page int64, limit int64, startTime int64, endTime int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  533. db := readDb.Model(&models.WarehousingInfo{})
  534. db = db.Where("org_id = ? AND status = 1", orgId)
  535. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  536. if startTime > 0 {
  537. db = db.Where("ctime >=?", startTime)
  538. }
  539. if endTime > 0 {
  540. db = db.Where("ctime<= ?", endTime)
  541. }
  542. db = db.Count(&total)
  543. offset := (page - 1) * limit
  544. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  545. return
  546. }
  547. func FindAllStockInfo(orgId int64, page int64, limit int64, keyword string) (list []*models.StockInfo, total int64, err error) {
  548. db := readDb.Model(&models.StockInfo{})
  549. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  550. db = db.Joins("JOIN xt_warehouse_info AS info ON info.good_id=xt_good_information.id AND info.status = 1").Group("xt_good_information.id")
  551. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  552. return db.Where("org_id = ? AND status = 1", orgId)
  553. })
  554. db = db.Preload("QuerySalesReturnInfo", "org_id = ? AND status = 1", orgId)
  555. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  556. return db.Where("org_id = ? AND status = 1", orgId)
  557. })
  558. db = db.Preload("QueryCancelStockInfo", "org_id = ? AND status = 1", orgId)
  559. db = db.Preload("GoodsType", "(org_id = ? AND status = 1) OR (status = 1 AND type = 1)", orgId)
  560. if len(keyword) > 0 {
  561. likeKey := "%" + keyword + "%"
  562. db = db.Joins("join xt_goods_type on xt_goods_type.id = xt_good_information.good_type_id AND ((xt_goods_type.org_id = ? AND xt_goods_type.status = 1) OR (xt_goods_type.type = 1 AND xt_goods_type.status = 1))", orgId)
  563. db = db.Where("xt_good_information.good_code LIKE ? OR xt_good_information.specification_name LIKE ? OR xt_goods_type.type_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_good_information.id")
  564. }
  565. db = db.Count(&total)
  566. offset := (page - 1) * limit
  567. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  568. return
  569. }
  570. func FindWarehouseInfoTotalByGoodId(goodId int64) (total int64, err error) {
  571. err = readDb.Model(&models.WarehousingInfo{}).Where("good_id = ? AND status = 1", goodId).Count(&total).Error
  572. return
  573. }
  574. func UpDateWarehouse(warehouse *models.Warehousing) (err error) {
  575. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", warehouse.WarehousingOrder).Update(map[string]interface{}{"operation_time": warehouse.OperationTime, "mtime": warehouse.Mtime, "modifier": warehouse.Modifier}).Error
  576. return
  577. }
  578. func UpDateWarehouseOut(warehouseOut *models.WarehouseOut) (err error) {
  579. err = readDb.Model(&models.WarehouseOut{}).Where("warehouse_out_order_number = ? AND status = 1", warehouseOut.WarehouseOutOrderNumber).Update(map[string]interface{}{"operation_time": warehouseOut.OperationTime, "mtime": warehouseOut.Mtime, "modifier": warehouseOut.Modifier}).Error
  580. return
  581. }
  582. func UpDateWarehouseInfo(warehouseInfo *models.WarehousingInfo) (err error) {
  583. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", warehouseInfo.WarehousingOrder).Update(map[string]interface{}{"good_type_id": warehouseInfo.GoodTypeId,
  584. "good_id": warehouseInfo.GoodId,
  585. "number": warehouseInfo.Number,
  586. "product_date": warehouseInfo.ProductDate,
  587. "expiry_date": warehouseInfo.ExpiryDate,
  588. "warehousing_count": warehouseInfo.WarehousingCount,
  589. "price": warehouseInfo.Price,
  590. "total_price": warehouseInfo.TotalPrice,
  591. "mtime": warehouseInfo.Mtime,
  592. "remark": warehouseInfo.Remark,
  593. }).Error
  594. return
  595. }
  596. func UpDateWarehouseStatus(id int64) (err error) {
  597. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  598. return
  599. }
  600. func DeleteWarehouse(ids []string) (err error) {
  601. ut := writeDb.Begin()
  602. err = ut.Model(&models.Warehousing{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  603. if err != nil {
  604. ut.Rollback()
  605. return
  606. }
  607. err = ut.Model(&models.WarehousingInfo{}).Where("warehousing_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  608. if err != nil {
  609. ut.Rollback()
  610. return
  611. }
  612. ut.Commit()
  613. return
  614. }
  615. func FindWareHouseById(orderNumber string) (*models.Warehousing, error) {
  616. warehouse := &models.Warehousing{}
  617. err := readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).First(&warehouse).Error
  618. return warehouse, err
  619. }
  620. func FindWareHouseOutById(id int64) (*models.WarehouseOut, error) {
  621. warehouseOut := &models.WarehouseOut{}
  622. err := readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", id).First(&warehouseOut).Error
  623. return warehouseOut, err
  624. }
  625. func FindWarehouseInfoByOrderNumber(orderNumber string) (info []*models.WarehousingInfo, err error) {
  626. err = readDb.Model(&models.WarehousingInfo{}).Where("warehousing_order = ? AND status = 1", orderNumber).Find(&info).Error
  627. return
  628. }
  629. func FindWarehouseOutInfoByOrderNumber(orderNumber string) (info []*models.WarehouseOutInfo, err error) {
  630. err = readDb.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_order_number = ? AND status = 1", orderNumber).Find(&info).Error
  631. return
  632. }
  633. func DeleteStockInRecordByOrderNumber(orderNumber string, id int64) (err error) {
  634. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  635. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", orderNumber).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  636. return
  637. }
  638. func DeleteStockInRecordByReocordId(id int64) (err error) {
  639. err = readDb.Model(&models.WarehousingInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  640. return
  641. }
  642. func FindAllGoodByManufactureId(manufacturer_id int64, dealer_id int64, org_id int64) (goodInfo []*models.GoodInfo, err error) {
  643. db := readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1", org_id)
  644. if manufacturer_id > 0 {
  645. db = db.Where("manufacturer = ?", manufacturer_id)
  646. }
  647. if dealer_id > 0 {
  648. db = db.Where("dealer = ?", dealer_id)
  649. }
  650. err = db.Preload("GoodsType", "status = 1 AND (org_id = ? OR type = 1)", org_id).Find(&goodInfo).Error
  651. return goodInfo, err
  652. }
  653. func FindAllWarehouseByKeyword(orgId int64, page int64, limit int64, keywords string) (list []*models.Warehousing, total int64, err error) {
  654. db := readDb.Model(&models.Warehousing{})
  655. likekey := "%" + keywords + "%"
  656. db = db.Where("org_id = ? AND status = 1 ", orgId)
  657. db = db.Where("warehousing_order LIKE ? ", likekey)
  658. db = db.Count(&total)
  659. offset := (page - 1) * limit
  660. err = db.Offset(offset).Limit(limit).Order("ctime desc").Find(&list).Error
  661. return
  662. }
  663. func UpDateWarehousingInfo(info *models.WarehousingInfo) (err error) {
  664. err = writeDb.Save(&info).Error
  665. return err
  666. }
  667. func EditWarehousing(warehouse models.Warehousing) {
  668. err = readDb.Model(&models.Warehousing{}).Where("warehousing_order = ? AND status = 1", warehouse.WarehousingOrder).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehousing_time": warehouse.WarehousingTime, "modifier": warehouse.Modifier, "dealer": warehouse.Dealer, "manufacturer": warehouse.Manufacturer}).Error
  669. }
  670. func FindAllWarehouse(org_id int64, types int64) (list []*models.Warehousing, err error) {
  671. err = readDb.Model(&models.Warehousing{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ? ", org_id).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  672. return list, err
  673. }
  674. func FindAllWarehouseInfo(org_id int64, types int64) (list []*models.WarehousingInfoConfig, err error) {
  675. err = readDb.Model(&models.WarehousingInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  676. return db.Where("status = 1 AND org_id = ?", org_id).Preload("GoodsType", "(status = 1 AND org_id = ?)OR(type = 1 AND status = 1)", org_id).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id)
  677. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  678. return list, err
  679. }
  680. func DeleteSalesReturn(ids []string) (err error) {
  681. ut := writeDb.Begin()
  682. err = ut.Model(&models.SalesReturn{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  683. if err != nil {
  684. ut.Rollback()
  685. return
  686. }
  687. err = ut.Model(&models.SalesReturnInfo{}).Where("sales_return_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  688. if err != nil {
  689. ut.Rollback()
  690. return
  691. }
  692. ut.Commit()
  693. return
  694. }
  695. func FindAllSalesReturnInfoById(id int64) (list []*models.SalesReturnInfo, err error) {
  696. err = readDb.Model(&models.SalesReturnInfo{}).Where("sales_return_id = ? AND status = 1", id).Find(&list).Error
  697. return
  698. }
  699. func FindAllSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  700. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  701. return
  702. }
  703. func UpDateSaleReturnStatus(id int64) (err error) {
  704. err = readDb.Model(&models.SalesReturnInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  705. return
  706. }
  707. func FindSalesReturnById(id int64) (salesReturn models.SalesReturn, err error) {
  708. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", id).First(&salesReturn).Error
  709. return salesReturn, err
  710. }
  711. func EditSaleReturn(sales models.SalesReturn) {
  712. err = readDb.Model(&models.SalesReturn{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer}).Error
  713. }
  714. func UpDateSalesReturnInfo(info *models.SalesReturnInfo) (err error) {
  715. err = writeDb.Save(&info).Error
  716. return err
  717. }
  718. func DeleteWarehouseOut(ids []string) (err error) {
  719. ut := writeDb.Begin()
  720. err = ut.Model(&models.WarehouseOut{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  721. if err != nil {
  722. ut.Rollback()
  723. return
  724. }
  725. err = ut.Model(&models.WarehouseOutInfo{}).Where("warehouse_out_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  726. if err != nil {
  727. ut.Rollback()
  728. return
  729. }
  730. ut.Commit()
  731. return
  732. }
  733. func UpDateWarehouseOutStatus(id int64) (err error) {
  734. err = readDb.Model(&models.WarehouseOutInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  735. return
  736. }
  737. func EditWarehouseOut(warehouseOut models.WarehouseOut) {
  738. err = readDb.Model(&models.WarehouseOut{}).Where("id = ? AND status = 1", warehouseOut.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "warehouse_out_time": warehouseOut.WarehouseOutTime, "dealer": warehouseOut.Dealer, "manufacturer": warehouseOut.Manufacturer}).Error
  739. }
  740. func UpDateWarehouseOutInfo(info *models.WarehouseOutInfo) (err error) {
  741. err = writeDb.Save(&info).Error
  742. return err
  743. }
  744. func FindAllCancelList(orgId int64, page int64, limit int64, startTime int64, endTime int64, types int64, keywords string) (list []*models.CancelStock, total int64, err error) {
  745. db := readDb.Model(&models.CancelStock{})
  746. db = db.Where("xt_cancel_stock.org_id = ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.type = ?", orgId, types)
  747. if len(keywords) > 0 {
  748. likeKey := "%" + keywords + "%"
  749. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  750. db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_cancel_stock.manufacturer")
  751. db = db.Where("xt_manufacturer.manufacturer_name LIKE ? OR sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock.order_number LIKE ?", likeKey, likeKey, likeKey).Group("xt_cancel_stock.id")
  752. }
  753. if startTime > 0 {
  754. db = db.Where("xt_cancel_stock.opera_time >=?", startTime)
  755. }
  756. if endTime > 0 {
  757. db = db.Where("xt_cancel_stock.opera_time<= ?", endTime)
  758. }
  759. db = db.Count(&total)
  760. offset := (page - 1) * limit
  761. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock.ctime desc").Find(&list).Error
  762. return
  763. }
  764. func DeleteCancelStock(ids []string) (err error) {
  765. ut := writeDb.Begin()
  766. err = ut.Model(&models.CancelStock{}).Where("id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  767. if err != nil {
  768. ut.Rollback()
  769. return
  770. }
  771. err = ut.Model(&models.CancelStockInfo{}).Where("cancel_stock_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  772. if err != nil {
  773. ut.Rollback()
  774. return
  775. }
  776. ut.Commit()
  777. return
  778. }
  779. func UpDateCancleStockStatus(id int64) (err error) {
  780. err = readDb.Model(&models.CancelStockInfo{}).Where("id = ? AND status = 1", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  781. return
  782. }
  783. func EditCancelStock(sales models.CancelStock) {
  784. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", sales.ID).Update(map[string]interface{}{"mtime": time.Now().Unix(), "return_time": sales.ReturnTime, "dealer": sales.Dealer, "manufacturer": sales.Manufacturer}).Error
  785. }
  786. func FindCancelStockById(id int64) (cancelStock models.CancelStock, err error) {
  787. err = readDb.Model(&models.CancelStock{}).Where("id = ? AND status = 1", id).First(&cancelStock).Error
  788. return cancelStock, err
  789. }
  790. func UpDateCancelStockInfo(info *models.CancelStockInfo) (err error) {
  791. err = writeDb.Save(&info).Error
  792. return err
  793. }
  794. func FindWarehouseOutList(org_id int64, types int64) (list []*models.WarehouseOut, err error) {
  795. err = readDb.Model(&models.WarehouseOut{}).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ? ", org_id).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  796. return list, err
  797. }
  798. func FindAllWarehouseOutInfo(org_id int64, types int64) (list []*models.WarehousingOutInfoConfig, err error) {
  799. err = readDb.Model(&models.WarehousingOutInfoConfig{}).Preload("WarehousingGoodInfo", func(db *gorm.DB) *gorm.DB {
  800. return db.Where("status = 1 AND org_id = ?", org_id).Preload("GoodsType", "(status = 1 AND org_id = ?) OR (status = 1 AND type = 1) ", org_id).Preload("Manufacturers", "status = 1 AND org_id = ?", org_id).Preload("Dealers", "status = 1 AND org_id = ?", org_id)
  801. }).Where("org_id = ? AND status = 1 AND type = ?", org_id, types).Find(&list).Error
  802. return list, err
  803. }
  804. type StockDetailModel struct {
  805. GoodId int64 `gorm:"good_id" json:"good_id"`
  806. GoodTypeId int64 `gorm:"good_type_id" json:"good_type_id"`
  807. WarehousingOrder string `gorm:"warehousing_order" json:"warehousing_order"`
  808. Price float64 `gorm:"price" json:"price"`
  809. WarehousingCount int64 `gorm:"warehousing_count" json:"warehousing_count"`
  810. Type int `gorm:"type" json:"type"`
  811. Creater int64 `gorm:"creater" json:"creater"`
  812. WarehousingTime int64 `gorm:"warehousing_time" json:"warehousing_time"`
  813. Types int64 `gorm:"types" json:"types"`
  814. }
  815. func FindStockInDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.WarehousingInfo, total int64, err error) {
  816. db := readDb.Model(&models.WarehousingInfo{})
  817. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  818. if len(keywords) > 0 {
  819. likeKey := "%" + keywords + "%"
  820. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  821. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  822. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  823. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  824. //db = db.Joins("join xt_manufacturer on xt_manufacturer.id = xt_warehouse.manufacturer")
  825. //db = db.Where(" sgj_user_admin_role.user_name LIKE ?", likeKey).Group("xt_warehouse_info.id")
  826. db = db.Where(" sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_info.warehousing_order LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_info.id")
  827. } else {
  828. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  829. }
  830. if manufacturer > 0 {
  831. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  832. }
  833. if order_type > 0 {
  834. db = db.Where("xt_warehouse_info.type = ?", order_type)
  835. }
  836. db = db.Preload("Warehousing", "status = 1 AND org_id = ?", orgId)
  837. db = db.Count(&total)
  838. offset := (page - 1) * limit
  839. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_info.ctime desc").Find(&list).Error
  840. return
  841. }
  842. func GetStockInDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (err error, totalPrice float64) {
  843. db := readDb.Model(&models.WarehousingInfo{})
  844. db = db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId)
  845. if len(keywords) > 0 {
  846. likeKey := "%" + keywords + "%"
  847. //db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND ")
  848. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  849. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse.creater")
  850. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_info.good_id")
  851. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_info.warehousing_order LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_info.id")
  852. } else {
  853. db = db.Joins("join xt_warehouse on xt_warehouse.id = xt_warehouse_info.warehousing_id AND xt_warehouse.warehousing_time >=? AND xt_warehouse.warehousing_time<= ? AND xt_warehouse.status = 1 AND xt_warehouse.org_id = ?", startTime, endTime, orgId)
  854. }
  855. if manufacturer > 0 {
  856. db = db.Joins("join xt_warehouse as wa on wa.id = xt_warehouse_info.warehousing_id AND wa.manufacturer =?", manufacturer)
  857. }
  858. if order_type > 0 {
  859. db = db.Where("xt_warehouse_info.type = ?", order_type)
  860. }
  861. type PriceAmount struct {
  862. Total float64
  863. }
  864. var result []PriceAmount
  865. err = db.Select("total_price as total").Scan(&result).Error
  866. var total float64
  867. for _, value := range result {
  868. total = total + value.Total
  869. }
  870. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  871. return err, total
  872. }
  873. func FloatRound(f float64, n int) float64 {
  874. format := "%." + strconv.Itoa(n) + "f"
  875. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  876. return res
  877. }
  878. func FindStockOutDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.WarehouseOutInfo, total int64, err error) {
  879. db := readDb.Model(&models.WarehouseOutInfo{})
  880. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0 ", orgId)
  881. if len(keywords) > 0 {
  882. likeKey := "%" + keywords + "%"
  883. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  884. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  885. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  886. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out_info.warehouse_out_order_number LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_warehouse_out_info.id")
  887. } else {
  888. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  889. }
  890. if manufacturer > 0 {
  891. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  892. }
  893. if order_type > 0 {
  894. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  895. }
  896. db = db.Preload("WarehouseOut", "status = 1 AND org_id = ?", orgId)
  897. db = db.Count(&total)
  898. offset := (page - 1) * limit
  899. err = db.Offset(offset).Limit(limit).Order("xt_warehouse_out_info.ctime desc").Find(&list).Error
  900. return
  901. }
  902. func GetStockOutDetailTotal(orgId int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (err error, totalPrice float64) {
  903. db := readDb.Model(&models.WarehouseOutInfo{})
  904. db = db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.good_id <> 0 AND xt_warehouse_out_info.count <> 0", orgId)
  905. if len(keywords) > 0 {
  906. likeKey := "%" + keywords + "%"
  907. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  908. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_warehouse_out.creater")
  909. db = db.Joins("join xt_good_information on xt_good_information.id = xt_warehouse_out_info.good_id")
  910. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_warehouse_out_info.warehouse_out_order_number LIKE ?", likeKey, likeKey).Group("xt_warehouse_out_info.id")
  911. } else {
  912. db = db.Joins("join xt_warehouse_out on xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.status = 1 AND xt_warehouse_out.org_id = ?", startTime, endTime, orgId)
  913. }
  914. if manufacturer > 0 {
  915. db = db.Joins("join xt_warehouse_out as wa on wa.id = xt_warehouse_out_info.warehouse_out_id AND wa.manufacturer =?", manufacturer)
  916. }
  917. if order_type > 0 {
  918. db = db.Where("xt_warehouse_out_info.type = ?", order_type)
  919. }
  920. type PriceAmount struct {
  921. Total float64
  922. }
  923. var result []PriceAmount
  924. err = db.Select("(count * price) as total").Scan(&result).Error
  925. var total float64
  926. for _, value := range result {
  927. total = total + value.Total
  928. }
  929. total, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", total), 64)
  930. fmt.Println(total)
  931. return err, total
  932. }
  933. func FindSalesReturnDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.SalesReturnInfo, total int64, err error) {
  934. db := readDb.Model(&models.SalesReturnInfo{})
  935. db = db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId)
  936. if len(keywords) > 0 {
  937. likeKey := "%" + keywords + "%"
  938. db = db.Joins("join xt_sales_return on xt_sales_return.id = xt_sales_return_info.sales_return_id AND xt_sales_return.return_time >=? AND xt_sales_return.return_time<= ? AND xt_sales_return.status = 1 AND xt_sales_return.org_id = ?", startTime, endTime, orgId)
  939. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_sales_return.creater")
  940. db = db.Joins("join xt_good_information on xt_good_information.id = xt_sales_return_info.good_id")
  941. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_sales_return_info.order_number LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_sales_return_info.id")
  942. } else {
  943. db = db.Joins("join xt_sales_return on xt_sales_return.id = xt_sales_return_info.sales_return_id AND xt_sales_return.return_time >=? AND xt_sales_return.return_time<= ? AND xt_sales_return.status = 1 AND xt_sales_return.org_id = ?", startTime, endTime, orgId)
  944. }
  945. if manufacturer > 0 {
  946. db = db.Joins("join xt_sales_return as sr on sr.id = xt_sales_return_info.sales_return_id AND sr.manufacturer =?", manufacturer)
  947. }
  948. if order_type > 0 {
  949. db = db.Where("xt_sales_return_info.type = ?", order_type)
  950. }
  951. db = db.Preload("SalesReturn", "status = 1 AND org_id = ?", orgId)
  952. offset := (page - 1) * limit
  953. db = db.Count(&total)
  954. err = db.Offset(offset).Limit(limit).Order("xt_sales_return_info.ctime desc").Find(&list).Error
  955. return
  956. }
  957. func FindCancelDetailList(orgId int64, page int64, limit int64, startTime int64, endTime int64, keywords string, manufacturer int64, order_type int64, dealer int64) (list []*models.CancelStockInfo, total int64, err error) {
  958. db := readDb.Model(&models.CancelStockInfo{})
  959. db = db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId)
  960. if len(keywords) > 0 {
  961. likeKey := "%" + keywords + "%"
  962. db = db.Joins("join xt_cancel_stock on xt_cancel_stock.id = xt_cancel_stock_info.cancel_stock_id AND xt_cancel_stock.return_time >=? AND xt_cancel_stock.return_time<= ? AND xt_cancel_stock.status = 1 AND xt_cancel_stock.org_id = ?", startTime, endTime, orgId)
  963. db = db.Joins("join sgj_users.sgj_user_admin_role on sgj_user_admin_role.admin_user_id = xt_cancel_stock.creater")
  964. db = db.Joins("join xt_good_information on xt_good_information.id = xt_cancel_stock_info.good_id")
  965. db = db.Where("sgj_user_admin_role.user_name LIKE ? OR xt_cancel_stock_info.order_number LIKE ? OR xt_good_information.specification_name LIKE ?", likeKey, likeKey, likeKey).Group("xt_cancel_stock_info.id")
  966. } else {
  967. db = db.Joins("join xt_cancel_stock on xt_cancel_stock.id = xt_cancel_stock_info.cancel_stock_id AND xt_cancel_stock.return_time >=? AND xt_cancel_stock.return_time<= ? ", startTime, endTime)
  968. }
  969. if manufacturer > 0 {
  970. db = db.Joins("join xt_cancel_stock as cs on cs.id = xt_cancel_stock_info.cancel_stock_id AND cs.manufacturer =?", manufacturer)
  971. }
  972. if order_type > 0 {
  973. db = db.Where("xt_cancel_stock_info.type = ?", order_type)
  974. }
  975. db = db.Preload("CancelStock", "status = 1 AND org_id = ?", orgId)
  976. db = db.Count(&total)
  977. offset := (page - 1) * limit
  978. err = db.Offset(offset).Limit(limit).Order("xt_cancel_stock_info.ctime desc").Find(&list).Error
  979. return
  980. }
  981. func FindStockInByDealerId(id int64) (total int64, err error) {
  982. err = readDb.Model(&models.Warehousing{}).Where("dealer = ? AND status = 1", id).Count(&total).Error
  983. return
  984. }
  985. func FindStockInByManufacturerId(id int64) (total int64, err error) {
  986. err = readDb.Model(&models.Warehousing{}).Where("manufacturer = ? AND status = 1", id).Count(&total).Error
  987. return
  988. }
  989. func FindGoodTypeByName(name string, org_id int64) (total int64) {
  990. readDb.Model(&models.GoodsType{}).Where("(org_id = ? OR type = 1) AND status = 1 AND type_name = ? ", org_id, name).Count(&total)
  991. return
  992. }
  993. func FindGoodInfoByName(name string, org_id int64) (total int64) {
  994. readDb.Model(&models.GoodInfo{}).Where("org_id = ? AND status = 1 AND specification_name = ? ", org_id, name).Count(&total)
  995. return
  996. }
  997. func FindAllGoodTypeByType(types int64) (goodType []*models.GoodsType, err error) {
  998. err = readDb.Model(&models.GoodsType{}).Where("type = ? AND status = 1", types).Find(&goodType).Error
  999. return goodType, err
  1000. }
  1001. func FindWarehouseInfoByGoodType(good_type_id int64, org_id int64) (info []*models.WarehousingInfo, err error) {
  1002. err = readDb.Model(&models.WarehousingInfo{}).Preload("GoodInfo", "status = 1 AND org_id = ?", org_id).Where("good_type_id = ? AND status = 1 AND org_id = ?", good_type_id, org_id).Find(&info).Error
  1003. return info, err
  1004. }
  1005. func FindStockOutByIsSys(org_id int64, is_sys int, record_time int64) (out models.WarehouseOut, err error) {
  1006. err = readDb.Model(&models.WarehouseOut{}).Where("is_sys = ? AND status = 1 AND org_id = ? AND warehouse_out_time = ?", is_sys, org_id, record_time).First(&out).Error
  1007. return
  1008. }
  1009. func FindLastStockInInfoRecord(good_id int64, org_id int64) (in models.WarehousingInfo, err error) {
  1010. err = readDb.Model(&models.WarehousingInfo{}).Where("status = 1 AND org_id = ? AND good_id = ?", org_id, good_id).Last(&in).Error
  1011. return
  1012. }
  1013. func AddSigleWarehouseOutInfo(info *models.WarehouseOutInfo) error {
  1014. err := writeDb.Create(&info).Error
  1015. return err
  1016. }
  1017. func AddSigleAutoReduceRecordInfo(detail *models.AutomaticReduceDetail) error {
  1018. err := writeDb.Create(&detail).Error
  1019. return err
  1020. }
  1021. func FindStockOutInfoByTypeId(org_id int64, good_type_id int64, good_id int64, number string) (out models.WarehouseOutInfo, err error) {
  1022. err = readDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND good_type_id = ? AND good_id = ? AND is_sys = 1 AND warehouse_out_order_number = ?", org_id, good_type_id, good_id, number).First(&out).Error
  1023. return
  1024. }
  1025. func UpdateStockOutInfoCount(org_id int64, id int64) {
  1026. writeDb.Model(&models.WarehouseOutInfo{}).Where("status = 1 AND org_id = ? AND id = ?", org_id, id).UpdateColumn("count", gorm.Expr("count + ?", 1))
  1027. }
  1028. func FindUserDetailById(org_id int64, id int64) (user []*models.AutomaticReduceDetail, err error, total int64) {
  1029. db := readDb.Model(&models.AutomaticReduceDetail{})
  1030. db = db.Preload("GoodInfo", "org_id = ? AND status = 1", org_id)
  1031. db = db.Preload("GoodsType", "type = 1 AND status = 1")
  1032. db = db.Preload("Patients", "user_org_id = ? AND status = 1", org_id)
  1033. db = db.Where("status = 1 AND org_id = ? AND warehouse_out_id = ? AND good_id != 0", org_id, id)
  1034. db = db.Count(&total)
  1035. err = db.Find(&user).Error
  1036. return
  1037. }