StoreActivityProduct.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * Author: 向往那片天空
  5. * Date: 2020/6/9
  6. * Time: 9:07
  7. * 微信/QQ: 250023777
  8. * 格言: 抓住中心,宁精勿杂,宁专勿多
  9. */
  10. namespace app\models\store;
  11. use app\admin\model\order\StoreOrder;
  12. use app\admin\model\store\StoreProductAttrValue;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\services\PHPExcelService;
  15. use crmeb\traits\ModelTrait;
  16. class StoreActivityProduct extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'id';
  23. /**
  24. * 模型名称
  25. * @var string
  26. */
  27. protected $name = 'store_activity_product';
  28. use ModelTrait;
  29. public static function validWhere()
  30. {
  31. return self::where('is_del', 0);
  32. }
  33. /**
  34. * 获取活动数据
  35. * @param int $page
  36. * @param int $limit
  37. * @return mixed
  38. */
  39. public static function getAll($aid, $page = 0, $limit = 20)
  40. {
  41. $model = new self();
  42. $model = $model->alias('a');
  43. $model = $model->join('StoreProduct s', 's.id=a.product_id');
  44. $model = $model->field('a.*,s.price as product_price,s.ficti');
  45. $model = $model->order('a.sort desc,a.id desc');
  46. $model = $model->where('a.is_show', 1);
  47. if($aid)
  48. $model = $model->where('a.aid', $aid);
  49. $model = $model->where('a.is_del', 0);
  50. if ($page) $model = $model->page($page, $limit);
  51. return $model->select()->each(function ($item) {
  52. $item['image'] = set_file_url($item['image']);
  53. $item['activity']=StoreActivity::where("id={$item['aid']}")->find();
  54. });
  55. }
  56. /**
  57. * 获取一条活动商品数据
  58. * @param $id
  59. * @return mixed
  60. */
  61. public static function getActivityOne($id)
  62. {
  63. $model = new self();
  64. $model = $model->alias('a');
  65. $model = $model->join('StoreProduct s', 's.id=a.product_id');
  66. // $model = $model->field('a.*,s.price as product_price,SUM(s.sales+s.ficti) as total');
  67. $model = $model->field('a.*,s.price as product_price,s.ficti');
  68. $model = $model->where('a.is_show', 1);
  69. $model = $model->where('a.is_del', 0);
  70. $model = $model->where('a.id', $id);
  71. // $model = $model->where('a.start_time','<',time());
  72. // $model = $model->where('a.stop_time','>',time()-86400);
  73. return $model->find();
  74. }
  75. /**
  76. * 修改销量和库存
  77. * @param $num
  78. * @param $CombinationId
  79. * @return bool
  80. */
  81. public static function decCombinationStock($num, $CombinationId, $unique)
  82. {
  83. $product_id = self::where('id',$CombinationId)->value('product_id');
  84. if ($unique) {
  85. $res = false !== StoreProductAttrValue::decProductAttrStock($CombinationId, $unique, $num,4);
  86. $res = $res && self::where('id', $CombinationId)->dec('stock', $num)->inc('sales', $num)->update();
  87. $sku = StoreProductAttrValue::where('product_id',$CombinationId)->where('unique',$unique)->where('type',4)->value('suk');
  88. $res = $res && StoreProductAttrValue::where('product_id',$product_id)->where('suk',$sku)->where('type',0)->dec('stock',$num)->inc('sales',$num)->update();
  89. } else {
  90. $res = false !== self::where('id', $CombinationId)->dec('stock', $num)->inc('sales', $num)->update();
  91. $res = $res && StoreProduct::where('id',$product_id)->dec('stock', $num)->inc('sales', $num)->update();
  92. }
  93. return $res;
  94. }
  95. }