OtherOrderServices.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\dao\order\OtherOrderDao;
  13. use app\services\BaseServices;
  14. use app\services\pay\PayServices;
  15. use app\services\statistic\TradeStatisticServices;
  16. use app\services\user\MemberShipServices;
  17. use app\services\user\UserBillServices;
  18. use app\services\user\UserServices;
  19. use app\services\user\MemberCardServices;
  20. use crmeb\services\GroupDataService;
  21. use app\jobs\OtherOrderJob;
  22. use think\App;
  23. use think\exception\ValidateException;
  24. /**
  25. * Class OtherOrderServices
  26. * @package app\services\order
  27. * @method getDistinctCount(array $where, $field, ?bool $search = true)
  28. * @method getPayUserCount(int $time, string $channel_type)
  29. * @method getTrendData($time, $type, $timeType)
  30. */
  31. class OtherOrderServices extends BaseServices
  32. {
  33. /**初始化,获得dao层句柄
  34. * OtherOrderServices constructor.
  35. * @param OtherOrderDao $dao
  36. */
  37. public function __construct(OtherOrderDao $dao)
  38. {
  39. $this->dao = $dao;
  40. }
  41. /** 生成会员购买订单数据
  42. * @param array $data
  43. * @return mixed
  44. */
  45. public function addOtherOrderData(array $data)
  46. {
  47. if (!$data) throw new ValidateException('数据不能为空');
  48. $add = [
  49. 'uid' => $data['uid'],
  50. 'type' => isset($data['type']) ? $data['type'] : 1,
  51. 'order_id' => $data['order_id'],
  52. 'channel_type' => $data['channel_type'],
  53. 'pay_type' => isset($data['pay_type']) ? $data['pay_type'] : 0,
  54. 'member_type' => isset($data['member_type']) ? $data['member_type'] : 0,
  55. 'member_price' => isset($data['member_price']) ? $data['member_price'] : 0.00,
  56. 'pay_price' => isset($data['pay_price']) ? $data['pay_price'] : 0.00,
  57. 'code' => isset($data['member_code']) ? $data['member_code'] : "",
  58. 'vip_day' => isset($data['vip_day']) ? $data['vip_day'] : 0,
  59. 'is_permanent' => isset($data['is_permanent']) ? $data['is_permanent'] : 0,
  60. 'is_free' => isset($data['is_free']) ? $data['is_free'] : 0,
  61. 'overdue_time' => isset($data['overdue_time']) ? $data['overdue_time'] : 0,
  62. 'status' => 0,
  63. 'paid' => isset($data['paid']) ? $data['paid'] : 0,
  64. 'pay_time' => isset($data['pay_time']) ? $data['pay_time'] : 0,
  65. 'money' => isset($data['money']) ? $data['money'] : 0,
  66. 'add_time' => time(),
  67. ];
  68. return $this->dao->save($add);
  69. }
  70. /** 查询会员卡订单数据
  71. * @param array $where
  72. * @param string $field
  73. * @return array|\think\Model|null
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public function getOne(array $where, string $field = '*')
  79. {
  80. return $this->dao->getOne($where, $field);
  81. }
  82. /**
  83. * @param int $uid
  84. * @param string $channelType 支付渠道
  85. * @param bool $memberType 会员卡类型
  86. * @param string $payPrice 支付金额
  87. * @param string $payType 支付方式
  88. * @param $type 订单类型
  89. * @return mixed
  90. * @throws \Exception
  91. */
  92. public function createOrder(int $uid, string $channelType, $memberType = false, string $payPrice, string $payType, $type, $money)
  93. {
  94. /** @var StoreOrderCreateServices $storeOrderCreateService */
  95. $storeOrderCreateService = app()->make(StoreOrderCreateServices::class);
  96. $orderInfo = [
  97. 'uid' => $uid,
  98. 'order_id' => $storeOrderCreateService->getNewOrderId(),
  99. 'pay_price' => $payPrice,
  100. 'pay_type' => $payType,
  101. 'channel_type' => $channelType,
  102. 'member_code' => "",
  103. ];
  104. $orderInfo['type'] = $type;
  105. $orderInfo['member_code'] = "";
  106. $changeType = "create_offline_scan_order";
  107. $orderInfo['money'] = $money ? $money : $payPrice;
  108. $memberOrder = $this->addOtherOrderData($orderInfo);
  109. if (!$memberOrder) {
  110. throw new ValidateException('订单生成失败!');
  111. }
  112. // CacheService::redisHandler()->delete('user_order_' . $uid . $key);
  113. /** @var OtherOrderStatusServices $statusService */
  114. $statusService = app()->make(OtherOrderStatusServices::class);
  115. $statusService->save([
  116. 'oid' => $memberOrder['id'],
  117. 'change_type' => $changeType,
  118. 'change_message' => '订单生成',
  119. 'change_time' => time(),
  120. 'shop_type' => $type,
  121. ]);
  122. //$this->pushJob($order['id'], $combinationId, $seckillId, $bargainId);
  123. return $memberOrder;
  124. }
  125. /** 免费卡领取支付
  126. * @param $orderInfo
  127. * @return bool
  128. */
  129. public function zeroYuanPayment($orderInfo)
  130. {
  131. if ($orderInfo['paid']) {
  132. throw new ValidateException('该订单已支付!');
  133. }
  134. if ($orderInfo['member_type'] != 'free') {
  135. throw new ValidateException('支付失败!');
  136. }
  137. $res = $this->paySuccess($orderInfo, 'yue');//余额支付成功
  138. return $res;
  139. }
  140. /** 会员卡支付成功
  141. * @param array $orderInfo
  142. * @param string $paytype
  143. * @return bool
  144. */
  145. public function paySuccess(array $orderInfo, string $paytype = PayServices::WEIXIN_PAY, array $other = [])
  146. {
  147. /** @var OtherOrderStatusServices $statusService */
  148. $statusService = app()->make(OtherOrderStatusServices::class);
  149. /** @var UserServices $userServices */
  150. $userServices = app()->make(UserServices::class);
  151. /** @var UserBillServices $userBillServices */
  152. $userBillServices = app()->make(UserBillServices::class);
  153. switch ($orderInfo['type']) {
  154. case 0 :
  155. case 1:
  156. case 2 :
  157. $type = "pay_member";
  158. break;
  159. case 3:
  160. $type = "offline_scan";
  161. $res1 = true;
  162. break;
  163. }
  164. if ($paytype == PayServices::ALIAPY_PAY && isset($other['trade_no'])) {
  165. $updata['trade_no'] = $other['trade_no'];
  166. }
  167. $updata['paid'] = 1;
  168. $updata['pay_type'] = $paytype;
  169. $updata['pay_time'] = time();
  170. $res2 = $this->dao->update($orderInfo['id'], $updata);
  171. $res3 = $statusService->save([
  172. 'oid' => $orderInfo['id'],
  173. 'change_type' => 'pay_success',
  174. 'change_message' => '用户付款成功',
  175. 'shop_type' => $orderInfo['type'],
  176. 'change_time' => time()
  177. ]);
  178. $now_money = $userServices->value(['uid' => $orderInfo['uid']], 'now_money');
  179. $res4 = $userBillServices->income($type, $orderInfo['uid'], $orderInfo['pay_price'], $now_money, $orderInfo['id']);
  180. //支付成功后发送消息
  181. OtherOrderJob::dispatch([$orderInfo]);
  182. $res = $res1 && $res2 && $res3 && $res4;
  183. return false !== $res;
  184. }
  185. /** 修改
  186. * @param array $where
  187. * @param array $data
  188. * @return mixed
  189. */
  190. public function update(array $where, array $data)
  191. {
  192. return $this->dao->update($where, $data);
  193. }
  194. /**线下收银列表
  195. * @param array $where
  196. * @return array
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\DbException
  199. * @throws \think\db\exception\ModelNotFoundException
  200. */
  201. public function getScanOrderList(array $where)
  202. {
  203. $where['type'] = 3;
  204. $where['paid'] = 1;
  205. [$page, $limit] = $this->getPageValue();
  206. if ($where['add_time']) {
  207. [$startTime, $endTime] = explode('-', $where['add_time']);
  208. if ($startTime || $endTime) {
  209. $startTime = strtotime($startTime);
  210. $endTime = strtotime($endTime . ' 23:59:59');
  211. $where['add_time'] = [$startTime, $endTime];
  212. }
  213. }
  214. if ($where['name']) {
  215. /** @var UserServices $userService */
  216. $userService = app()->make(UserServices::class);
  217. $userInfo = $userService->getUserInfoList(['nickname' => $where['name']], "uid");
  218. if ($userInfo) $where['uid'] = array_column($userInfo, 'uid');
  219. }
  220. $list = $this->dao->getScanOrderList($where, $page, $limit);
  221. /** @var UserServices $userService */
  222. $userService = app()->make(UserServices::class);
  223. if ($list) {
  224. foreach ($list as &$v) {
  225. $v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
  226. $v['pay_time'] = date('Y-m-d H:i:s', $v['pay_time']);
  227. $userInfo = $userService->getUserInfo($v['uid']);
  228. $v['phone'] = $userInfo['phone'];
  229. $v['nickname'] = $userInfo['nickname'];
  230. switch ($v['pay_type']) {
  231. case "yue" :
  232. $v['pay_type'] = "余额";
  233. break;
  234. case "weixin" :
  235. $v['pay_type'] = "微信";
  236. break;
  237. case "alipay" :
  238. $v['pay_type'] = "支付宝";
  239. break;
  240. }
  241. $v['true_price'] = bcsub($v['money'], $v['pay_price'], 2);
  242. }
  243. }
  244. $count = $this->dao->count($where);
  245. return compact('list', 'count');
  246. }
  247. }