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