MiaoshaGoods.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\model;
  3. use think\Model;
  4. use think\facade\Db;
  5. class MiaoshaGoods extends Model
  6. {
  7. protected $connection = 'mysql';
  8. protected $pk = 'id';
  9. protected $name = 'miaosha_goods';
  10. public function goods()
  11. {
  12. return $this->hasOne(Goods::class, 'id', 'goods_id')->bind([
  13. 'district_name',
  14. 'city_name',
  15. 'province_name'
  16. ]);
  17. }
  18. public static function setGoodslist($goods)
  19. {
  20. if (!empty($goods)) {
  21. foreach ($goods as &$vo) {
  22. $goodsdata = Goods::find($vo['goods_id']);
  23. if (!empty($goodsdata)) {
  24. $vo['goods'] = $goodsdata->toArray();
  25. $vo['image'] = toimg($vo['goods']['image']);
  26. $vo['ptype'] = $vo['goods']['ptype'];
  27. $vo['time_amount'] = $vo['goods']['time_amount'];
  28. $vo['quantity_unit'] = $vo['goods']['quantity_unit'];
  29. if (empty($vo['quantity_unit'] && $vo['is_times'] != 3)) {
  30. $vo['quantity_unit'] = '次';
  31. }
  32. $vo['is_timer'] = $vo['goods']['is_timer'];
  33. $vo['is_times'] = $vo['goods']['is_times'];
  34. $vo['timesmum'] = $vo['goods']['timesmum'];
  35. //$vo['price'] = floatval($vo['price']);
  36. $vo['price'] = floatval(Goods::setPrice($vo)["price"]);
  37. }
  38. }
  39. }
  40. return $goods;
  41. }
  42. public static function getDiyMiaoshaGoods($count = 4, $goodsSort = "all", $ocid = 0)
  43. {
  44. $Configdata = Config::getconfig();
  45. $query = Db::name('miaosha_goods')
  46. ->alias('mg')
  47. ->leftJoin('goods', 'mg.goods_id = goods.id')
  48. ->field('mg.*, goods.district_name, goods.city_name, goods.province_name');
  49. $query->where('mg.weid', weid());
  50. $query->where('mg.status', 1);
  51. if (empty($Configdata['show_storegoods'])) {
  52. $query->where('mg.sid', 0);
  53. }
  54. //$query = MiaoshaGoods::where($where);
  55. /*
  56. $withJoin = [
  57. 'goods' => ['weid'=>'goodsweid','district_name', 'city_name', 'province_name'],
  58. ];
  59. $query->withJoin($withJoin, 'left');
  60. */
  61. if ($ocid) {
  62. $query->where(function ($q) use ($ocid) {
  63. $Operatingcity = Operatingcity::find($ocid);
  64. if (!empty($Operatingcity)) {
  65. $Operatingcity = $Operatingcity->toArray();
  66. if (empty($Operatingcity['areatype'])) {
  67. $Operatingcity['areatype'] = 3;
  68. }
  69. if ($Operatingcity['areatype'] == 3) {
  70. $q->where('goods.district_name', $Operatingcity['district_name'])->whereOr('goods.district_name', '');
  71. } elseif ($Operatingcity['areatype'] == 2) {
  72. $q->where('goods.city_name', $Operatingcity['city_name'])->whereOr('goods.city_name', '');
  73. } elseif ($Operatingcity['areatype'] == 1) {
  74. $q->where('goods.province_name', $Operatingcity['province_name'])->whereOr('goods.province_name', '');
  75. }
  76. } else {
  77. $q->where('goods.city_name', '');
  78. }
  79. });
  80. } else {
  81. $query->where('goods.city_name', '');
  82. }
  83. if ($goodsSort == "all") {
  84. $Sort = 'mg.sort asc,mg.id desc';
  85. } elseif ($goodsSort == "sales") {
  86. $Sort = 'mg.sale_count desc';
  87. } elseif ($goodsSort == "price") {
  88. $Sort = 'mg.price asc';
  89. }
  90. $data = $query->limit((int) $count)
  91. ->order($Sort)->select()->toArray();
  92. //$sql = $query->getLastSql();
  93. $retdata = Goods::setGoodslist($data);
  94. //$retdata['sql'] = $sql;
  95. return $retdata;
  96. }
  97. }