Product.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\supplier\model\plus\bargain;
  3. use app\common\model\plus\bargain\Product as ProductModel;
  4. use app\common\model\plus\bargain\BargainSku as BargainSkuModel;
  5. /**
  6. * 参加记录模型
  7. */
  8. class Product extends ProductModel
  9. {
  10. /**
  11. * 获取秒杀商品列表
  12. */
  13. public static function getList($shop_supplier_id, $data)
  14. {
  15. return (new static())->with(['product.image.file', 'active'])
  16. ->where('shop_supplier_id', '=', $shop_supplier_id)
  17. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  18. ->paginate($data);
  19. }
  20. /**
  21. * 修改
  22. */
  23. public function edit($data){
  24. $this->startTrans();
  25. try {
  26. //添加商品
  27. $stock = array_sum(array_column($data['product']['spec_list'], 'bargain_stock'));
  28. $this->save([
  29. 'status' => 0,
  30. 'remark' => '',
  31. 'stock' => $stock,
  32. 'limit_num' => $data['limit_num']
  33. ]);
  34. //商品规格
  35. $sku_model = new BargainSkuModel();
  36. //先删除所有规格
  37. $sku_model->where('bargain_product_id', '=', $this['bargain_product_id'])->delete();
  38. $sku_data = [];
  39. foreach ($data['product']['spec_list'] as $sku) {
  40. $sku_data[] = [
  41. 'bargain_product_id' => $this['bargain_product_id'],
  42. 'product_id' => $data['product_id'],
  43. 'product_sku_id' => $sku['product_sku_id'],
  44. 'bargain_price' => $sku['bargain_price'],
  45. 'product_price' => $sku['product_price'],
  46. 'bargain_stock' => $sku['bargain_stock'],
  47. 'product_attr' => isset($sku['product_attr'])?$sku['product_attr']:'',
  48. 'bargain_activity_id' => $this['bargain_activity_id'],
  49. 'bargain_num' => $sku['bargain_num'],
  50. 'app_id' => self::$app_id,
  51. ];
  52. }
  53. $sku_model->saveAll($sku_data);
  54. // 事务提交
  55. $this->commit();
  56. return true;
  57. } catch (\Exception $e) {
  58. $this->error = $e->getMessage();
  59. $this->rollback();
  60. return false;
  61. }
  62. }
  63. /**
  64. * 获取排除商品id,报名了活动,待审核,或者通过了,并且活动未结束
  65. */
  66. public function getExcludeIds($shop_supplier_id){
  67. $list = $this->alias('product')->distinct(true)->field('product.*')
  68. ->where('product.is_delete', '=', 0)
  69. ->where('product.shop_supplier_id', '=', $shop_supplier_id)
  70. ->where('product.status', 'in', [0,10])
  71. ->where('activity.end_time', '>', time())
  72. ->join('bargain_activity activity', 'activity.bargain_activity_id = product.bargain_activity_id','left')
  73. ->column('product_id');
  74. $exclude_ids = [];
  75. foreach ($list as $key => $item){
  76. array_push($exclude_ids, $item);
  77. }
  78. return $exclude_ids;
  79. }
  80. /**
  81. * 删除
  82. */
  83. public function remove($bargain_product_id, $shop_supplier_id){
  84. return $this->where('bargain_product_id', '=', $bargain_product_id)
  85. ->where('shop_supplier_id', '=', $shop_supplier_id)
  86. ->where('status', '<>', 10)
  87. ->delete();
  88. }
  89. /**
  90. * 检查商品是否能报名
  91. */
  92. public function checkProduct($product_id, $shop_supplier_id){
  93. $excludeIds = $this->getExcludeIds($shop_supplier_id);
  94. if(in_array($product_id, $excludeIds)){
  95. return false;
  96. }
  97. return true;
  98. }
  99. }