StoreLiveProduct.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 StoreLiveProduct extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'id';
  23. /**
  24. * 模型名称
  25. * @var string
  26. */
  27. protected $name = 'store_live_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($title = '', $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.title', 'like', '%' . $title . '%');
  47. $model = $model->where('a.is_show', 1);
  48. $model = $model->where('a.is_del', 0);
  49. if ($page) $model = $model->page($page, $limit);
  50. return $model->select()->each(function ($item) {
  51. $item['image'] = set_file_url($item['image']);
  52. });
  53. }
  54. /**
  55. * 获取一条活动商品数据
  56. * @param $id
  57. * @return mixed
  58. */
  59. public static function getLiveOne($id)
  60. {
  61. $model = new self();
  62. $model = $model->alias('a');
  63. $model = $model->join('StoreProduct s', 's.id=a.product_id');
  64. // $model = $model->field('a.*,s.price as product_price,SUM(s.sales+s.ficti) as total');
  65. $model = $model->field('a.*,s.price as product_price,s.ficti');
  66. $model = $model->where('a.is_show', 1);
  67. $model = $model->where('a.is_del', 0);
  68. $model = $model->where('a.id', $id);
  69. // $model = $model->where('a.start_time','<',time());
  70. // $model = $model->where('a.stop_time','>',time()-86400);
  71. return $model->find();
  72. }
  73. /**
  74. * 修改销量和库存
  75. * @param $num
  76. * @param $CombinationId
  77. * @return bool
  78. */
  79. public static function decCombinationStock($num, $CombinationId, $unique)
  80. {
  81. $product_id = self::where('id', $CombinationId)->value('product_id');
  82. if ($unique) {
  83. $res = false !== StoreProductAttrValue::decProductAttrStock($CombinationId, $unique, $num, 5);
  84. $res = $res && self::where('id', $CombinationId)->dec('stock', $num)->inc('sales', $num)->update();
  85. $sku = StoreProductAttrValue::where('product_id', $CombinationId)->where('unique', $unique)->where('type', 5)->value('suk');
  86. $res = $res && StoreProductAttrValue::where('product_id', $product_id)->where('suk', $sku)->where('type', 0)->dec('stock', $num)->inc('sales', $num)->update();
  87. } else {
  88. $res = false !== self::where('id', $CombinationId)->dec('stock', $num)->inc('sales', $num)->update();
  89. $res = $res && StoreProduct::where('id', $product_id)->dec('stock', $num)->inc('sales', $num)->update();
  90. }
  91. return $res;
  92. }
  93. }