Lecturer.php 12 KB

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