Plan.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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\admin\model\study;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use app\admin\model\system\RecommendRelation;
  15. use app\admin\model\system\WebRecommendRelation;
  16. /**
  17. * Class plan 讲师
  18. * @package app\admin\model\special
  19. */
  20. class Plan extends ModelBasic
  21. {
  22. use ModelTrait;
  23. public function setShelfTimeAttr($val) {
  24. return $val ? strtotime($val) : time();
  25. }
  26. public function getShelfTimeAttr($val) {
  27. return $val ? date('Y-m-d H:i:s',$val) : '';
  28. }
  29. //设置where条件
  30. public static function setWhere($where, $alirs = '', $model = null)
  31. {
  32. $model = $model === null ? new self() : $model;
  33. $model = $alirs !== '' ? $model->alias($alirs) : $model;
  34. $alirs = $alirs === '' ? $alirs : $alirs . '.';
  35. $model = $model->where("{$alirs}is_del", 0);
  36. if (isset($where['is_show']) && $where['is_show'] !== '') $model = $model->where("{$alirs}is_show", $where['is_show']);
  37. if ($where['title'] && $where['title']) $model = $model->where("{$alirs}plan_name", 'LIKE', "%$where[title]%");
  38. $model = $model->order("{$alirs}shelf_time desc");
  39. return $model;
  40. }
  41. public static function setMerWhere($where, $alirs = '', $model = null)
  42. {
  43. $model = $model === null ? new self() : $model;
  44. $model = $alirs !== '' ? $model->alias($alirs) : $model;
  45. $alirs = $alirs === '' ? $alirs : $alirs . '.';
  46. $model = $model->where("{$alirs}is_del", 0);
  47. if (isset($where['is_show']) && $where['is_show'] !== '') $model = $model->where("{$alirs}is_show", $where['is_show']);
  48. if ($where['title'] && $where['title']) $model = $model->where("{$alirs}plan_name", 'LIKE', "%$where[title]%");
  49. $model = $model->order("{$alirs}sort desc");
  50. $model = $model->join('Merchant m', 'l.mer_id=m.id', 'left')->field('l.*,m.status,m.is_source');
  51. return $model;
  52. }
  53. /**计划列表
  54. * @param $where
  55. * @return array
  56. * @throws \think\Exception
  57. */
  58. public static function getplanList($where)
  59. {
  60. $data = self::setWhere($where, 'p')->page((int)$where['page'], (int)$where['limit'])->select();
  61. $data = count($data) ? $data->toArray() : [];
  62. $count = self::setWhere($where, 'p')->count();
  63. return compact('data', 'count');
  64. }
  65. /**
  66. * 删除计划
  67. * @param $id
  68. * @return bool|int
  69. * @throws \think\exception\DbException
  70. */
  71. public static function delPlan($id)
  72. {
  73. $plan = self::get($id);
  74. if (!$plan) return self::setErrorInfo('删除的数据不存在');
  75. // Special::where('is_del', 0)->where('id', $id)->update(['is_show' => 0, 'is_del' => 1]);
  76. // if ($plan['mer_id'] > 0) {
  77. // $merchant = Merchant::get($plan['mer_id']);
  78. // if ($merchant) {
  79. // Merchant::where('id', $plan['mer_id'])->update(['is_del' => 1]);
  80. // User::where('uid', $merchant['uid'])->update(['business' => 0]);
  81. // MerchantAdmin::where('mer_id', $plan['mer_id'])->update(['is_del' => 1]);
  82. // DownloadModel::where(['is_del' => 0, 'mer_id' => $plan['mer_id']])->update(['is_show' => 0, 'is_del' => 1]);
  83. // ProductModel::where(['is_del' => 0, 'mer_id' => $plan['mer_id']])->update(['is_show' => 0, 'is_del' => 1]);
  84. // EventRegistrationModel::where(['is_del' => 0, 'mer_id' => $plan['mer_id']])->update(['is_show' => 0, 'is_del' => 1]);
  85. // }
  86. // }
  87. return self::where('id', $id)->update(['is_del' => 1]);
  88. }
  89. /**获取商户id
  90. * @param $id
  91. */
  92. public static function getMerId($id)
  93. {
  94. return self::where('id', $id)->value('mei_id');
  95. }
  96. /**讲师课程订单
  97. * @param $plan_id
  98. */
  99. public static function planOrderList($where)
  100. {
  101. $model = self::getOrderWhere($where)->field('a.order_id,a.pay_price,a.pay_type,a.paid,a.status,a.total_price,a.refund_status,a.type,s.title,r.nickname,a.uid');
  102. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  103. foreach ($data as $key => &$value) {
  104. if ($value['paid'] == 0) {
  105. $value['status_name'] = '未支付';
  106. } else if ($value['paid'] == 1 && $value['status'] == 0 && $value['refund_status'] == 0 && $value['type'] != 2) {
  107. $value['status_name'] = '已支付';
  108. } else if ($value['paid'] == 1 && $value['status'] == 0 && $value['refund_status'] == 1 && $value['type'] != 2) {
  109. $value['status_name'] = '退款中';
  110. } else if ($value['paid'] == 1 && $value['status'] == 0 && $value['refund_status'] == 2 && $value['type'] != 2) {
  111. $value['status_name'] = '已退款';
  112. }
  113. if ($value['nickname']) {
  114. $value['nickname'] = $value['nickname'] . '/' . $value['uid'];
  115. } else {
  116. $value['nickname'] = '暂无昵称/' . $value['uid'];
  117. }
  118. if ($value['paid'] == 1) {
  119. switch ($value['pay_type']) {
  120. case 'weixin':
  121. $value['pay_type_name'] = '微信支付';
  122. break;
  123. case 'yue':
  124. $value['pay_type_name'] = '余额支付';
  125. break;
  126. case 'zhifubao':
  127. $value['pay_type_name'] = '支付宝支付';
  128. break;
  129. default:
  130. $value['pay_type_name'] = '其他支付';
  131. break;
  132. }
  133. } else {
  134. switch ($value['pay_type']) {
  135. default:
  136. $value['pay_type_name'] = '未支付';
  137. break;
  138. }
  139. }
  140. }
  141. $count = self::getOrderWhere($where)->count();
  142. return compact('count', 'data');
  143. }
  144. /**条件处理
  145. * @param $where
  146. * @return StoreOrderModel
  147. */
  148. public static function getOrderWhere($where)
  149. {
  150. $model = StoreOrderModel::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT')
  151. ->join('Special s', 'a.cart_id=s.id')
  152. ->where('a.type', 0)->where('a.paid', 1);
  153. if ($where['plan_id']) {
  154. $model = $model->where('s.plan_id', $where['plan_id']);
  155. }
  156. if ($where['data'] !== '') {
  157. $model = self::getModelTime($where, $model, 'a.add_time');
  158. }
  159. $model = $model->order('a.id desc');
  160. return $model;
  161. }
  162. /**
  163. * 处理订单金额
  164. * @param $where
  165. * @return array
  166. */
  167. public static function getOrderPrice($where)
  168. {
  169. $price = array();
  170. $price['pay_price'] = 0;//支付金额
  171. $price['pay_price_wx'] = 0;//微信支付金额
  172. $price['pay_price_yue'] = 0;//余额支付金额
  173. $price['pay_price_zhifubao'] = 0;//支付宝支付金额
  174. $list = self::getOrderWhere($where)->field([
  175. 'sum(a.total_num) as total_num',
  176. 'sum(a.pay_price) as pay_price',
  177. ])->find()->toArray();
  178. $price['total_num'] = $list['total_num'];//商品总数
  179. $price['pay_price'] = $list['pay_price'];//支付金额
  180. $list = self::getOrderWhere($where)->field('sum(a.pay_price) as pay_price,a.pay_type')->group('a.pay_type')->select()->toArray();
  181. foreach ($list as $v) {
  182. if ($v['pay_type'] == 'weixin') {
  183. $price['pay_price_wx'] = $v['pay_price'];
  184. } elseif ($v['pay_type'] == 'yue') {
  185. $price['pay_price_yue'] = $v['pay_price'];
  186. } elseif ($v['pay_type'] == 'zhifubao') {
  187. $price['pay_price_zhifubao'] = $v['pay_price'];
  188. } else {
  189. $price['pay_price_other'] = $v['pay_price'];
  190. }
  191. }
  192. $price['order_sum'] = self::getOrderWhere($where)->count();
  193. return $price;
  194. }
  195. public static function getBadge($where)
  196. {
  197. $price = self::getOrderPrice($where);
  198. return [
  199. [
  200. 'name' => '订单数量',
  201. 'field' => '件',
  202. 'count' => $price['order_sum'],
  203. 'background_color' => 'layui-bg-blue',
  204. 'col' => 4
  205. ],
  206. [
  207. 'name' => '售出课程',
  208. 'field' => '件',
  209. 'count' => $price['total_num'],
  210. 'background_color' => 'layui-bg-blue',
  211. 'col' => 4
  212. ],
  213. [
  214. 'name' => '订单金额',
  215. 'field' => '元',
  216. 'count' => $price['pay_price'],
  217. 'background_color' => 'layui-bg-blue',
  218. 'col' => 4
  219. ],
  220. [
  221. 'name' => '微信支付金额',
  222. 'field' => '元',
  223. 'count' => $price['pay_price_wx'],
  224. 'background_color' => 'layui-bg-blue',
  225. 'col' => 4
  226. ],
  227. [
  228. 'name' => '余额支付金额',
  229. 'field' => '元',
  230. 'count' => $price['pay_price_yue'],
  231. 'background_color' => 'layui-bg-blue',
  232. 'col' => 4
  233. ],
  234. [
  235. 'name' => '支付宝支付金额',
  236. 'field' => '元',
  237. 'count' => $price['pay_price_zhifubao'],
  238. 'background_color' => 'layui-bg-blue',
  239. 'col' => 4
  240. ]
  241. ];
  242. }
  243. }