TuanGoods.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\model;
  3. use think\Model;
  4. use think\facade\Db;
  5. class TuanGoods extends Model
  6. {
  7. protected $connection = 'mysql';
  8. protected $pk = 'id';
  9. protected $name = 'tuan_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 getTuanGoods($id)
  43. {
  44. $tuangoods = self::find($id);
  45. if (!empty($tuangoods)) {
  46. $data['tuan'] = $tuangoods->toArray();
  47. $goods = Goods::find($data['tuan']['goods_id']);
  48. if (!empty($goods)) {
  49. $data['goods'] = $goods->toArray();
  50. $data['goods']['image'] = toimg($data['goods']['image']);
  51. }
  52. }
  53. return $data;
  54. }
  55. public static function getDiyTuanGoods($count = 4, $goodsSort = "all", $ocid = 0)
  56. {
  57. $Configdata = Config::getconfig();
  58. $query = Db::name('tuan_goods')
  59. ->alias('tg')
  60. ->leftJoin('goods', 'tg.goods_id = goods.id')
  61. ->field('tg.*, goods.district_name, goods.city_name, goods.province_name');
  62. $query->where('tg.weid', weid());
  63. $query->where('tg.status', 1);
  64. if (empty($Configdata['show_storegoods'])) {
  65. $query->where('tg.sid', 0);
  66. }
  67. //$query = MiaoshaGoods::where($where);
  68. /*
  69. $withJoin = [
  70. 'goods' => ['weid'=>'goodsweid','district_name', 'city_name', 'province_name'],
  71. ];
  72. $query->withJoin($withJoin, 'left');
  73. */
  74. if ($ocid) {
  75. $query->where(function ($q) use ($ocid) {
  76. $Operatingcity = Operatingcity::find($ocid);
  77. if (!empty($Operatingcity)) {
  78. $Operatingcity = $Operatingcity->toArray();
  79. if (empty($Operatingcity['areatype'])) {
  80. $Operatingcity['areatype'] = 3;
  81. }
  82. if ($Operatingcity['areatype'] == 3) {
  83. $q->where('goods.district_name', $Operatingcity['district_name'])->whereOr('goods.district_name', '');
  84. } elseif ($Operatingcity['areatype'] == 2) {
  85. $q->where('goods.city_name', $Operatingcity['city_name'])->whereOr('goods.city_name', '');
  86. } elseif ($Operatingcity['areatype'] == 1) {
  87. $q->where('goods.province_name', $Operatingcity['province_name'])->whereOr('goods.province_name', '');
  88. }
  89. } else {
  90. $q->where('goods.city_name', '');
  91. }
  92. });
  93. } else {
  94. $query->where('goods.city_name', '');
  95. }
  96. if ($goodsSort == "all") {
  97. $Sort = 'tg.sort asc,tg.id desc';
  98. } elseif ($goodsSort == "sales") {
  99. $Sort = 'tg.sale_count desc';
  100. } elseif ($goodsSort == "price") {
  101. $Sort = 'tg.price asc';
  102. }
  103. $data = $query->limit((int) $count)
  104. ->order($Sort)->select()->toArray();
  105. //$sql = $query->getLastSql();
  106. $retdata = Goods::setGoodslist($data);
  107. //$retdata['sql'] = $sql;
  108. return $retdata;
  109. }
  110. }