StoreAssistance.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace app\models\store;
  3. use crmeb\services\UtilService;
  4. use crmeb\traits\ModelTrait;
  5. use crmeb\basic\BaseModel;
  6. /**
  7. * TODO 助力商品Model
  8. * Class StoreAssistance
  9. * @package app\models\store
  10. */
  11. class StoreAssistance extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'store_assistance';
  23. use ModelTrait;
  24. public static function merSet($mer_id, $alias = '')
  25. {
  26. return $mer_id ? self::where($alias ? $alias . '.mer_id' : 'mer_id', $mer_id) : new self;
  27. }
  28. protected function getAddTimeAttr($value)
  29. {
  30. if ($value) return date('Y-m-d H:i:s', $value);
  31. return '';
  32. }
  33. /**
  34. * 获取助力数据
  35. * @param int $page
  36. * @param int $limit
  37. * @return mixed
  38. */
  39. public static function getAll($page = 0, $limit = 20, $mer_id = false)
  40. {
  41. $model = self::merSet($mer_id, 'c');
  42. $model = $model->alias('c');
  43. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  44. $model = $model->field('c.*,s.price as product_price');
  45. $model = $model->order('c.sort desc,c.id desc');
  46. $model = $model->where('c.is_show', 1);
  47. $model = $model->where('c.is_del', 0);
  48. $model = $model->where('c.start_time', '<', time());
  49. $model = $model->where('c.stop_time', '>', time());
  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['price'] = floatval($item['price']);
  54. $item['product_price'] = floatval($item['product_price']);
  55. $item['subsidy'] = bcmul($item['price'], $item['p_separate_account']/100, 2);
  56. });
  57. }
  58. /**
  59. * 获取一条助力数据
  60. * @param $id
  61. * @return mixed
  62. */
  63. public static function getAssistanceOne($id)
  64. {
  65. $model = new self();
  66. $model = $model->alias('c');
  67. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  68. $model = $model->field('c.*,s.price as product_price,SUM(s.sales+s.ficti) as total');
  69. $model = $model->where('c.is_show', 1);
  70. $model = $model->where('c.is_del', 0);
  71. $model = $model->where('c.id', $id);
  72. $model = $model->where('c.start_time', '<', time());
  73. $model = $model->where('c.stop_time', '>', time() - 86400);
  74. $info = $model->find();
  75. if ($info['id']) {
  76. return $info;
  77. } else {
  78. return [];
  79. }
  80. }
  81. /**
  82. * 获取推荐的助力商品
  83. * @return mixed
  84. */
  85. public static function getAssistanceHost($limit = 0, $mer_id = false)
  86. {
  87. $model = self::merSet($mer_id,'c');
  88. $model = $model->alias('c');
  89. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  90. $model = $model->field('c.id,c.image,c.price,c.sales,c.title,c.people,s.price as product_price');
  91. $model = $model->where('c.is_del', 0);
  92. $model = $model->where('c.is_host', 1);
  93. $model = $model->where('c.start_time', '<', time());
  94. $model = $model->where('c.stop_time', '>', time());
  95. if ($limit) $model = $model->limit($limit);
  96. return $model->select();
  97. }
  98. /**
  99. * 修改销量和库存
  100. * @param $num
  101. * @param $assistanceId
  102. * @return bool
  103. */
  104. public static function decAssistanceStock($num, $assistanceId, $unique)
  105. {
  106. $product_id = self::where('id', $assistanceId)->value('product_id');
  107. if ($unique) {
  108. $res = false !== StoreProductAttrValue::decProductAttrStock($assistanceId, $unique, $num, 4);
  109. $res = $res && self::where('id', $assistanceId)->dec('stock', $num)->inc('sales', $num)->update();
  110. $sku = StoreProductAttrValue::where('product_id', $assistanceId)->where('unique', $unique)->where('type', 4)->value('suk');
  111. $res = $res && StoreProductAttrValue::where('product_id', $product_id)->where('suk', $sku)->where('type', 0)->dec('stock', $num)->inc('sales', $num)->update();
  112. } else {
  113. $res = false !== self::where('id', $assistanceId)->dec('stock', $num)->inc('sales', $num)->update();
  114. }
  115. $res = $res && StoreProduct::where('id', $product_id)->dec('stock', $num)->inc('sales', $num)->update();
  116. return $res;
  117. }
  118. /**
  119. * 获取查看拼团统计
  120. * @return array
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. * @throws \think\exception\DbException
  124. */
  125. public static function getStatistics($mer_id = '')
  126. {
  127. $statistics = array();
  128. $statistics['browseCount'] = self::where('mer_id', $mer_id)->value('sum(browse) as browse');//总展现量
  129. $statistics['browseCount'] = $statistics['browseCount'] ? $statistics['browseCount'] : 0;
  130. $statistics['visitCount'] = StoreVisit::where('mer_id', $mer_id)->where('product_type', 'assistance')->count();//访客人数
  131. $statistics['partakeCount'] = StoreAssistanceActive::getCountPeopleAll(0, $mer_id);//参与人数
  132. $statistics['assistanceCount'] = StoreAssistanceActive::getCountPeopleAssistance(0, $mer_id);//成团数量
  133. $res = [
  134. ['col' => 6, 'count' => $statistics['browseCount'], 'name' => '总展现量(次)', 'className' => 'md-trending-up'],
  135. ['col' => 6, 'count' => $statistics['visitCount'], 'name' => '访客人数(人)', 'className' => 'md-stats'],
  136. ['col' => 6, 'count' => $statistics['partakeCount'], 'name' => '参与人数(人)', 'className' => 'ios-speedometer-outline'],
  137. ['col' => 6, 'count' => $statistics['assistanceCount'], 'name' => '成团数量(个)', 'className' => 'md-rose'],
  138. ];
  139. return compact('res');
  140. }
  141. /**
  142. * 助力列表
  143. * @param $where
  144. * @return array
  145. */
  146. public static function systemPage($where)
  147. {
  148. $model = self::setWhere($where);
  149. $count = $model->count();
  150. $list = $model->page((int)$where['page'], (int)$where['limit'])
  151. ->select()
  152. ->each(function ($item) {
  153. $item['count_people_all'] = StoreAssistanceActive::getCountPeopleAll($item['id'], $item['mer_id']);//参与人数
  154. $item['count_people_assistance'] = StoreAssistanceActive::getCountPeopleAssistance($item['id'], $item['mer_id']);//助力人数
  155. $item['count_people_browse'] = StoreVisit::getVisitPeople($item['id'], 'assistance');//访问人数
  156. });
  157. return compact('count', 'list');
  158. }
  159. /**
  160. * 设置助力 where 条件
  161. * @param $where
  162. * @param null $model
  163. * @return mixed
  164. */
  165. public static function setWhere($where, $model = null)
  166. {
  167. $model = $model === null ? new self() : $model;
  168. $model = $model->alias('c');
  169. $model = $model->field('c.*,p.store_name,p.price as ot_price');
  170. $model = $model->where('c.mer_id', $where['mer_id']);
  171. $model = $model->join('StoreProduct p', 'p.id=c.product_id', 'LEFT');
  172. if (isset($where['is_show']) && $where['is_show'] != '') $model = $model->where('c.is_show', $where['is_show']);
  173. // if(isset($where['is_host']) && $where['is_host'] != '') $model = $model->where('c.is_host',$where['is_host']);
  174. if (isset($where['store_name']) && $where['store_name'] != '') $model = $model->where('p.store_name|p.id|c.id|c.title', 'LIKE', "%$where[store_name]%");
  175. return $model->order('c.id desc')->where('c.is_del', 0);
  176. }
  177. /**
  178. *查出导出数据
  179. * @param $where
  180. */
  181. public static function exportData($where)
  182. {
  183. $list = self::setWhere($where)->select();
  184. count($list) && $list = $list->toArray();
  185. foreach ($list as &$item) {
  186. $item['count_people_all'] = StoreAssistanceActive::getCountPeopleAll($item['id'], $item['mer_id']);//参与人数
  187. $item['count_people_assistance'] = StoreAssistanceActive::getCountPeopleAssistance($item['id'], $item['mer_id']);//助力人数
  188. $item['count_people_browse'] = StoreVisit::getVisitPeople($item['id'], 'assistance');//访问人数
  189. $item['_stop_time'] = date('Y/m/d H:i:s', $item['stop_time']);
  190. }
  191. return $list;
  192. }
  193. /**
  194. * 详情
  195. */
  196. public static function getOne($id)
  197. {
  198. $info = self::get($id);
  199. if ($info) {
  200. if ($info['start_time'])
  201. $start_time = date('Y-m-d H:i:s', $info['start_time']);
  202. if ($info['stop_time'])
  203. $stop_time = date('Y-m-d H:i:s', $info['stop_time']);
  204. if (isset($start_time) && isset($stop_time))
  205. $info['section_time'] = [$start_time, $stop_time];
  206. else
  207. $info['section_time'] = [];
  208. unset($info['start_time'], $info['stop_time']);
  209. }
  210. if ($info['images'])
  211. $info['images'] = json_decode($info['images'], true);
  212. else
  213. $info['images'] = [];
  214. $info['price'] = floatval($info['price']);
  215. $info['postage'] = floatval($info['postage']);
  216. $info['weight'] = floatval($info['weight']);
  217. $info['volume'] = floatval($info['volume']);
  218. $info['description'] = StoreDescription::getDescription($id, 4);
  219. $info['attrs'] = self::attr_list($id);
  220. return $info;
  221. }
  222. public static function attr_list($id)
  223. {
  224. $productId = self::where('id', $id)->value('product_id');
  225. $combinationResult = StoreProductAttrResult::where('product_id', $id)->where('type', 4)->value('result');
  226. $items = json_decode($combinationResult, true)['attr'];
  227. $productAttr = self::get_attr($items, $productId, 0);
  228. $seckillAttr = self::get_attr($items, $id, 4);
  229. foreach ($productAttr as $pk => $pv) {
  230. foreach ($seckillAttr as &$sv) {
  231. if ($pv['detail'] == $sv['detail']) {
  232. $productAttr[$pk] = $sv;
  233. }
  234. }
  235. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  236. }
  237. $attrs['items'] = $items;
  238. $attrs['value'] = $productAttr;
  239. foreach ($items as $key => $item) {
  240. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  241. }
  242. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  243. $header[] = ['title' => '助力价', 'key' => 'price', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  244. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  245. $header[] = ['title' => '原价', 'key' => 'ot_price', 'align' => 'center', 'minWidth' => 80];
  246. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  247. $header[] = ['title' => '限量', 'key' => 'quota', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  248. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  249. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  250. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  251. $attrs['header'] = $header;
  252. return $attrs;
  253. }
  254. public static function get_attr($attr, $id, $type)
  255. {
  256. $value = attr_format($attr)[1];
  257. $valueNew = [];
  258. $count = 0;
  259. foreach ($value as $key => $item) {
  260. $detail = $item['detail'];
  261. sort($item['detail'], SORT_STRING);
  262. $suk = implode(',', $item['detail']);
  263. $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', $type)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota', 'suk');
  264. if (count($sukValue)) {
  265. foreach (array_values($detail) as $k => $v) {
  266. $valueNew[$count]['value' . ($k + 1)] = $v;
  267. }
  268. $valueNew[$count]['detail'] = json_encode($detail);
  269. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  270. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  271. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  272. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  273. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  274. $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  275. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  276. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  277. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  278. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  279. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  280. $valueNew[$count]['_checked'] = $type != 0 ? true : false;
  281. $count++;
  282. }
  283. }
  284. return $valueNew;
  285. }
  286. }