StoreProduct.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\merchant\model\store;
  12. use think\Db;
  13. use traits\ModelTrait;
  14. use basic\ModelBasic;
  15. use app\merchant\model\system\Relation;
  16. use app\merchant\model\special\SpecialBuy;
  17. use app\merchant\model\special\Special;
  18. use app\merchant\model\special\SpecialSource;
  19. use app\merchant\model\order\StoreOrder;
  20. use app\admin\model\system\SystemConfig;
  21. use service\PhpSpreadsheetService;
  22. use app\merchant\model\download\DataDownloadBuy;
  23. /**
  24. * 产品管理 model
  25. * Class StoreProduct
  26. * @package app\merchant\model\store
  27. */
  28. class StoreProduct extends ModelBasic
  29. {
  30. use ModelTrait;
  31. /**
  32. * 获取连表查询条件
  33. * @param $type
  34. * @return array
  35. */
  36. public static function setData($type)
  37. {
  38. $store_stock = SystemConfig::getValue('store_stock');
  39. switch ((int)$type) {
  40. case 1:
  41. $data = ['p.is_show' => 1, 'p.is_del' => 0, 'p.status' => 1];
  42. break;
  43. case 2:
  44. $data = ['p.is_show' => 0, 'p.is_del' => 0, 'p.status' => 1];
  45. break;
  46. case 3:
  47. $data = ['p.is_del' => 0, 'p.status' => 1];
  48. break;
  49. case 4:
  50. $data = ['p.is_show' => 1, 'p.is_del' => 0, 'p.stock' => 0, 'p.status' => 1];
  51. break;
  52. case 5:
  53. $data = ['p.is_show' => 1, 'p.is_del' => 0, 'p.status' => 1, 'p.stock' => ['elt', $store_stock]];
  54. break;
  55. case 6:
  56. $data = ['p.is_del' => 1, 'p.status' => 1];
  57. break;
  58. case 7:
  59. $data = ['p.is_del' => 0];
  60. break;
  61. };
  62. return isset($data) ? $data : [];
  63. }
  64. public static function PreWhere($alert = '')
  65. {
  66. $alert = $alert ? $alert . '.' : '';
  67. return self::where([$alert . 'is_show' => 1, $alert . 'is_del' => 0]);
  68. }
  69. /**
  70. * 获取连表MOdel
  71. * @param $model
  72. * @return object
  73. */
  74. public static function getModelObject($where = [])
  75. {
  76. $model = new self();
  77. $model = $model->alias('p');
  78. $model = $model->group('p.id');
  79. if (isset($where['type']) && $where['type'] != '' && ($data = self::setData($where['type']))) {
  80. $model = $model->where($data);
  81. }
  82. if (isset($where['type']) && $where['type'] == 7) {
  83. if (isset($where['status']) && $where['status'] != '') {
  84. $model = $model->where('status', $where['status']);
  85. } else {
  86. $model = $model->where('status', 'in', [1, -1, 0]);
  87. }
  88. }
  89. if (isset($where['store_name']) && $where['store_name'] != '') {
  90. $model = $model->where('p.store_name|p.keyword|p.id', 'LIKE', "%$where[store_name]%");
  91. }
  92. if (isset($where['cate_id']) && trim($where['cate_id']) != 0) {
  93. $model = $model->where('p.cate_id', $where['cate_id']);
  94. }
  95. if (isset($where['mer_id']) && $where['mer_id']) {
  96. $model = $model->where('p.mer_id', $where['mer_id']);
  97. }
  98. if (isset($where['order']) && $where['order'] != '') {
  99. $model = $model->order(self::setOrder($where['order']));
  100. } else {
  101. $model = $model->order('p.sort DESC,p.add_time DESC');
  102. }
  103. $model = $model->join('StoreCategory c', 'p.cate_id=c.id', 'left');
  104. return $model;
  105. }
  106. /*
  107. * 获取产品列表
  108. * @param $where array
  109. * @return array
  110. *
  111. */
  112. public static function ProductList($where)
  113. {
  114. $model = self::getModelObject($where)->field('p.*,c.cate_name');
  115. if ($where['excel'] == 0) $model = $model->page((int)$where['page'], (int)$where['limit']);
  116. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  117. if ($where['excel'] == 1) {
  118. self::SaveExcel($data);
  119. }
  120. $count = self::getModelObject($where)->count();
  121. return compact('count', 'data');
  122. }
  123. public static function SaveExcel($data)
  124. {
  125. $export = [];
  126. foreach ($data as $index => $item) {
  127. $export[] = [
  128. $item['store_name'],
  129. $item['store_info'],
  130. $item['cate_name'],
  131. '¥' . $item['price'],
  132. $item['stock'],
  133. $item['sales'],
  134. ];
  135. }
  136. $filename = '产品导出' . time() . '.xlsx';
  137. $head = ['产品名称', '产品简介', '产品分类', '价格', '库存', '销量'];
  138. PhpSpreadsheetService::outdata($filename, $export, $head);
  139. }
  140. /**
  141. * 获取商品
  142. */
  143. public static function storeProductList($where, $special_source)
  144. {
  145. $where['store_name'] = $where['title'];
  146. $model = self::getModelObject($where)->where('p.id', 'not in', $special_source)->field(['p.*']);
  147. $model = $model->page((int)$where['page'], (int)$where['limit']);
  148. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  149. $count = self::getModelObject($where)->where('p.id', 'not in', $special_source)->count();
  150. return compact('count', 'data');
  151. }
  152. /**
  153. * 获取连表MOdel
  154. * @param $model
  155. * @return object
  156. */
  157. public static function getModelExamine($where = [])
  158. {
  159. $model = new self();
  160. $model = $model->alias('p');
  161. $model = $model->group('p.id');
  162. if (isset($where['store_name']) && $where['store_name'] != '') {
  163. $model = $model->where('p.store_name|p.keyword|p.id', 'LIKE', "%$where[store_name]%");
  164. }
  165. if (isset($where['cate_id']) && trim($where['cate_id']) != 0) {
  166. $model = $model->where('p.cate_id', $where['cate_id']);
  167. }
  168. if (isset($where['mer_id']) && $where['mer_id']) {
  169. $model = $model->where('p.mer_id', $where['mer_id']);
  170. }
  171. if (isset($where['order']) && $where['order'] != '') {
  172. $model = $model->order(self::setOrder($where['order']));
  173. } else {
  174. $model = $model->order('p.sort DESC,p.add_time DESC');
  175. }
  176. $model = $model->join('StoreCategory c', 'p.cate_id=c.id', 'left');
  177. $model = $model->where('p.status', 'in', [1, -1, 0])->where('is_del',0);
  178. return $model;
  179. }
  180. /*
  181. * 获取产品列表
  182. * @param $where array
  183. * @return array
  184. *
  185. */
  186. public static function productExamineList($where)
  187. {
  188. $model = self::getModelExamine($where)->field('p.*,c.cate_name');
  189. $model = $model->page((int)$where['page'], (int)$where['limit']);
  190. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  191. foreach ($data as $key => &$value) {
  192. $value['fail_time'] = date('Y-m-d H:i:s', $value['fail_time']);
  193. }
  194. $count = self::getModelExamine($where)->count();
  195. return compact('count', 'data');
  196. }
  197. //获取 badge 内容
  198. public static function getbadge($where, $type)
  199. {
  200. $StoreOrderModel = new StoreOrder;
  201. $replenishment_num = SystemConfig::getValue('replenishment_num');
  202. $replenishment_num = $replenishment_num > 0 ? $replenishment_num : 20;
  203. $stock1 = self::getModelTime($where, new self())->where('stock', '<', $replenishment_num)->column('stock');
  204. $sum_stock = self::where('stock', '<', $replenishment_num)->column('stock');
  205. $stk = [];
  206. foreach ($stock1 as $item) {
  207. $stk[] = $replenishment_num - $item;
  208. }
  209. $lack = array_sum($stk);
  210. $sum = [];
  211. foreach ($sum_stock as $val) {
  212. $sum[] = $replenishment_num - $val;
  213. }
  214. return [
  215. [
  216. 'name' => '商品数量',
  217. 'field' => '件',
  218. 'count' => self::setWhereType(new self(), $type)->where('add_time', '<', mktime(0, 0, 0, date('m'), date('d'), date('Y')))->sum('stock'),
  219. 'content' => '商品数量总数',
  220. 'background_color' => 'layui-bg-blue',
  221. 'sum' => self::sum('stock'),
  222. 'class' => 'fa fa fa-ioxhost',
  223. ],
  224. [
  225. 'name' => '新增商品',
  226. 'field' => '件',
  227. 'count' => self::setWhereType(self::getModelTime($where, new self), $type)->where('is_new', 1)->sum('stock'),
  228. 'content' => '新增商品总数',
  229. 'background_color' => 'layui-bg-cyan',
  230. 'sum' => self::where('is_new', 1)->sum('stock'),
  231. 'class' => 'fa fa-line-chart',
  232. ],
  233. [
  234. 'name' => '活动商品',
  235. 'field' => '件',
  236. 'count' => self::getModelTime($where, $StoreOrderModel)->sum('total_num'),
  237. 'content' => '活动商品总数',
  238. 'background_color' => 'layui-bg-green',
  239. 'sum' => $StoreOrderModel->sum('total_num'),
  240. 'class' => 'fa fa-bar-chart',
  241. ],
  242. [
  243. 'name' => '缺货商品',
  244. 'field' => '件',
  245. 'count' => $lack,
  246. 'content' => '总商品数量',
  247. 'background_color' => 'layui-bg-orange',
  248. 'sum' => array_sum($sum),
  249. 'class' => 'fa fa-cube',
  250. ],
  251. ];
  252. }
  253. public static function setWhereType($model, $type)
  254. {
  255. switch ($type) {
  256. case 1:
  257. $data = ['is_show' => 1, 'is_del' => 0, 'status' => 1];
  258. break;
  259. case 2:
  260. $data = ['is_show' => 0, 'is_del' => 0, 'status' => 1];
  261. break;
  262. case 3:
  263. $data = ['is_del' => 0, 'status' => 1];
  264. break;
  265. case 4:
  266. $data = ['is_show' => 1, 'is_del' => 0, 'stock' => 0, 'status' => 1];
  267. break;
  268. case 5:
  269. $data = ['is_show' => 1, 'is_del' => 0, 'status' => 1, 'stock' => ['elt', 1]];
  270. break;
  271. case 6:
  272. $data = ['is_del' => 1, 'status' => 1];
  273. break;
  274. }
  275. if (isset($data)) $model = $model->where($data);
  276. return $model;
  277. }
  278. /**
  279. * 设置查询条件
  280. * @param array $where
  281. * @return array
  282. */
  283. public static function setWhere($where)
  284. {
  285. $time['data'] = '';
  286. if (isset($where['start_time']) && $where['start_time'] != '' && isset($where['end_time']) && $where['end_time'] != '') {
  287. $time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
  288. } else {
  289. $time['data'] = isset($where['data']) ? $where['data'] : '';
  290. }
  291. $model = self::getModelTime($time, db('store_cart')->alias('a')->join('store_product b', 'a.product_id=b.id'), 'a.add_time');
  292. if (isset($where['title']) && $where['title'] != '') {
  293. $model = $model->where('b.store_name|b.id', 'like', "%$where[title]%");
  294. }
  295. return $model;
  296. }
  297. }