SystemVerifyOrder.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\admin\model\system;
  8. use app\admin\model\order\StoreOrderCartInfo;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\traits\ModelTrait;
  11. use think\facade\Route as Url;
  12. use app\models\store\StoreCart;
  13. use app\admin\model\wechat\WechatUser;
  14. use app\admin\model\store\StoreProduct;
  15. use app\models\routine\RoutineTemplate;
  16. use app\admin\model\user\{
  17. User, UserBill
  18. };
  19. use app\admin\model\ump\{
  20. StoreCouponUser, StorePink
  21. };
  22. use crmeb\services\{
  23. PHPExcelService, WechatTemplateService
  24. };
  25. /**
  26. * 订单管理Model
  27. * Class StoreOrder
  28. * @package app\admin\model\store
  29. */
  30. class SystemVerifyOrder extends BaseModel
  31. {
  32. /**
  33. * 数据表主键
  34. * @var string
  35. */
  36. protected $pk = 'id';
  37. /**
  38. * 模型名称
  39. * @var string
  40. */
  41. protected $name = 'store_order';
  42. use ModelTrait;
  43. public static function orderCount()
  44. {
  45. $data['wz'] = self::statusByWhere(0, new self())->where(['is_system_del' => 0])->count();
  46. $data['wf'] = self::statusByWhere(1, new self())->where(['is_system_del' => 0, 'shipping_type' => 1])->count();
  47. $data['ds'] = self::statusByWhere(2, new self())->where(['is_system_del' => 0, 'shipping_type' => 1])->count();
  48. $data['dp'] = self::statusByWhere(3, new self())->where(['is_system_del' => 0])->count();
  49. $data['jy'] = self::statusByWhere(4, new self())->where(['is_system_del' => 0])->count();
  50. $data['tk'] = self::statusByWhere(-1, new self())->where(['is_system_del' => 0])->count();
  51. $data['yt'] = self::statusByWhere(-2, new self())->where(['is_system_del' => 0])->count();
  52. $data['del'] = self::statusByWhere(-4, new self())->where(['is_system_del' => 0])->count();
  53. $data['write_off'] = self::statusByWhere(5, new self())->where(['is_system_del' => 0])->count();
  54. $data['general'] = self::where(['pink_id' => 0, 'combination_id' => 0, 'seckill_id' => 0, 'bargain_id' => 0, 'is_system_del' => 0])->count();
  55. $data['pink'] = self::where('pink_id|combination_id', '>', 0)->where('is_system_del', 0)->count();
  56. $data['seckill'] = self::where('seckill_id', '>', 0)->where('is_system_del', 0)->count();
  57. $data['bargain'] = self::where('bargain_id', '>', 0)->where('is_system_del', 0)->count();
  58. return $data;
  59. }
  60. /**
  61. * 核销订单列表
  62. * @param $where
  63. * @return array
  64. */
  65. public static function OrderList($where)
  66. {
  67. $model = self::getOrderWhere($where, self::alias('a')
  68. ->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')
  69. ->field('a.*,r.nickname,r.phone,r.spread_uid')->order('a.add_time desc,id desc');
  70. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  71. foreach ($data as &$item) {
  72. $_info = StoreOrderCartInfo::where('oid', $item['id'])->field('cart_info')->select();
  73. $_info = count($_info) ? $_info->toArray() : [];
  74. foreach ($_info as $k => $v) {
  75. $cart_info = json_decode($v['cart_info'], true);
  76. if (!isset($cart_info['productInfo'])) $cart_info['productInfo'] = [];
  77. $_info[$k]['cart_info'] = $cart_info;
  78. unset($cart_info);
  79. }
  80. $item['_info'] = $_info;
  81. $item['spread_nickname'] = User::where('uid', $item['spread_uid'])->value('nickname');
  82. $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
  83. $item['back_integral'] = $item['back_integral'] ?: 0;
  84. if ($item['paid'] == 1) {
  85. switch ($item['pay_type']) {
  86. case 'weixin':
  87. $item['pay_type_name'] = '微信支付';
  88. break;
  89. case 'yue':
  90. $item['pay_type_name'] = '余额支付';
  91. break;
  92. case 'offline':
  93. $item['pay_type_name'] = '线下支付';
  94. break;
  95. default:
  96. $item['pay_type_name'] = '其他支付';
  97. break;
  98. }
  99. } else {
  100. switch ($item['pay_type']) {
  101. default:
  102. $item['pay_type_name'] = '未支付';
  103. break;
  104. case 'offline':
  105. $item['pay_type_name'] = '线下支付';
  106. $item['pay_type_info'] = 1;
  107. break;
  108. }
  109. }
  110. if ($item['paid'] == 0 && $item['status'] == 0) {
  111. $item['status_name'] = '未支付';
  112. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  113. $item['status_name'] = '未发货';
  114. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  115. $item['status_name'] = '未核销';
  116. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  117. $item['status_name'] = '待收货';
  118. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  119. $item['status_name'] = '未核销';
  120. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  121. $item['status_name'] = '待评价';
  122. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  123. $item['status_name'] = '已完成';
  124. }
  125. if ($item['clerk_id']) {
  126. $item['clerk_name'] = SystemStoreStaff::where('a.id', $item['clerk_id'])->alias('a')->join('user u', 'u.uid = a.uid')->value('u.nickname');
  127. } else {
  128. $item['clerk_name'] = '总平台';
  129. }
  130. if ($item['store_id']) {
  131. $item['store_name'] = SystemStore::where('id', $item['store_id'])->value('name');
  132. } else {
  133. $item['store_name'] = '';
  134. }
  135. }
  136. $count = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
  137. return compact('count', 'data');
  138. }
  139. /**
  140. * 处理where条件
  141. * @param $where
  142. * @param $model
  143. * @return mixed
  144. */
  145. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  146. {
  147. $model = $model->where($aler . 'is_system_del', 0)
  148. ->where($aler . 'shipping_type', 2)
  149. ->where($aler . 'paid', 1)
  150. ->where($aler . 'status', 2);
  151. if (isset($where['real_name']) && $where['real_name'] != '') {
  152. $model = $model->where($aler . 'order_id|' . $aler . 'real_name|' . $aler . 'user_phone' . ($join ? '|' . $join . '.nickname|' . $join . '.uid|' . $join . '.phone' : ''), 'LIKE', "%$where[real_name]%");
  153. }
  154. if (isset($where['data']) && $where['data'] !== '') {
  155. $model = self::getModelTime($where, $model, $aler . 'add_time');
  156. }
  157. if (isset($where['store_id']) && $where['store_id'] !== '') {
  158. $model = $model->where($aler . 'store_id', $where['store_id']);
  159. }
  160. return $model;
  161. }
  162. public static function getBadge($where)
  163. {
  164. $price = self::getOrderPrice($where);
  165. return [
  166. [
  167. 'name' => '订单数量',
  168. 'field' => '件',
  169. 'count' => $price['count_sum'],
  170. 'background_color' => 'layui-bg-blue',
  171. 'col' => 2
  172. ],
  173. [
  174. 'name' => '订单金额',
  175. 'field' => '元',
  176. 'count' => $price['pay_price'],
  177. 'background_color' => 'layui-bg-blue',
  178. 'col' => 2
  179. ],
  180. [
  181. 'name' => '退款金额',
  182. 'field' => '元',
  183. 'count' => $price['refund_price'],
  184. 'background_color' => 'layui-bg-blue',
  185. 'col' => 2
  186. ],
  187. [
  188. 'name' => '退款订单数',
  189. 'field' => '个',
  190. 'count' => $price['refund_sum'],
  191. 'background_color' => 'layui-bg-blue',
  192. 'col' => 2
  193. ],
  194. ];
  195. }
  196. public static function statusByWhere($status, $model = null, $alert = '')
  197. {
  198. if ($model == null) $model = new self;
  199. if ('' === $status)
  200. return $model;
  201. else if ($status == 8)
  202. return $model;
  203. else if ($status == 0)//未支付
  204. return $model->where($alert . 'paid', 0)->where($alert . 'status', 0)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  205. else if ($status == 1)//已支付 未发货
  206. return $model->where($alert . 'paid', 1)->where($alert . 'status', 0)->where($alert . 'shipping_type', 1)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  207. else if ($status == 2)//已支付 待收货
  208. return $model->where($alert . 'paid', 1)->where($alert . 'status', 1)->where($alert . 'shipping_type', 1)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  209. else if ($status == 5)//已支付 待核销
  210. return $model->where($alert . 'paid', 1)->where($alert . 'status', 0)->where($alert . 'shipping_type', 2)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  211. else if ($status == 3)// 已支付 已收货 待评价
  212. return $model->where($alert . 'paid', 1)->where($alert . 'status', 2)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  213. else if ($status == 4)// 交易完成
  214. return $model->where($alert . 'paid', 1)->where($alert . 'status', 3)->where($alert . 'refund_status', 0)->where($alert . 'is_del', 0);
  215. else if ($status == -1)//退款中
  216. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 1)->where($alert . 'is_del', 0);
  217. else if ($status == -2)//已退款
  218. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 2)->where($alert . 'is_del', 0);
  219. else if ($status == -3)//退款
  220. return $model->where($alert . 'paid', 1)->where($alert . 'refund_status', 'in', '1,2')->where($alert . 'is_del', 0);
  221. else if ($status == -4)//已删除
  222. return $model->where($alert . 'is_del', 1);
  223. else
  224. return $model;
  225. }
  226. /**
  227. * 订单数量 支付方式
  228. * @return array
  229. */
  230. public static function payTypeCount()
  231. {
  232. $where['status'] = 8;
  233. $where['is_del'] = 0;
  234. $where['real_name'] = '';
  235. $where['data'] = '';
  236. $where['type'] = '';
  237. $where['order'] = '';
  238. $where['pay_type'] = 1;
  239. $weixin = self::getOrderWhere($where, new self)->count();
  240. $where['pay_type'] = 2;
  241. $yue = self::getOrderWhere($where, new self)->count();
  242. $where['pay_type'] = 3;
  243. $offline = self::getOrderWhere($where, new self)->count();
  244. return compact('weixin', 'yue', 'offline');
  245. }
  246. /**
  247. * 处理订单金额
  248. * @param $where
  249. * @return array
  250. */
  251. public static function getOrderPrice($where)
  252. {
  253. $where['is_del'] = 0;//删除订单不统计
  254. $model = new self;
  255. $price = [];
  256. $price['pay_price'] = 0;//支付金额
  257. $price['refund_price'] = 0;//退款金额
  258. $price['count_sum'] = 0; //核销订单数
  259. $price['refund_sum'] = 0;//退款订单数
  260. $whereData = ['is_del' => 0];
  261. if ($where['status'] == '') {
  262. $whereData['paid'] = 1;
  263. $whereData['refund_status'] = 0;
  264. }
  265. $price['refund_price'] = self::getOrderWhere($where, $model)->where(['is_del' => 0, 'paid' => 1, 'refund_status' => 2])->sum('refund_price');
  266. $price['refund_sum'] = self::getOrderWhere($where, $model)->where(['is_del' => 0, 'paid' => 1, 'refund_status' => 2])->count();
  267. $sumNumber = self::getOrderWhere($where, $model)->where($whereData)->field([
  268. 'count(id) as count_sum',
  269. 'sum(pay_price) as sum_pay_price',
  270. ])->find();
  271. if ($sumNumber) {
  272. $price['count_sum'] = $sumNumber['count_sum'];
  273. $price['pay_price'] = $sumNumber['sum_pay_price'];
  274. }
  275. return $price;
  276. }
  277. }