ServeOrderRepository.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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\common\repositories\system\serve;
  12. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\AddShortUrlResponseBody\data;
  13. use app\common\dao\system\serve\ServeOrderDao;
  14. use app\common\model\system\serve\ServeOrder;
  15. use app\common\repositories\BaseRepository;
  16. use app\common\repositories\store\product\ProductCopyRepository;
  17. use app\common\repositories\system\merchant\MerchantRepository;
  18. use app\common\repositories\user\UserRepository;
  19. use crmeb\services\CombinePayService;
  20. use crmeb\services\PayService;
  21. use think\exception\ValidateException;
  22. use think\facade\Cache;
  23. use think\facade\Db;
  24. use think\facade\Log;
  25. class ServeOrderRepository extends BaseRepository
  26. {
  27. protected $dao;
  28. public function __construct(ServeOrderDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. //复制商品
  33. const TYPE_COPY_PRODUCT = 1;
  34. //电子面单
  35. const TYPE_DUMP = 2;
  36. //保证金 margin
  37. const TYPE_MARGIN = 10;
  38. //同城配送delivery
  39. const TYPE_DELIVERY = 20;
  40. /**
  41. * TODO 购买一号通 支付
  42. * @param $merId
  43. * @param $data
  44. * @return array
  45. * @author Qinii
  46. * @day 1/26/22
  47. */
  48. public function meal($merId, $data)
  49. {
  50. $ret = app()->make(ServeMealRepository::class)->get($data['meal_id']);
  51. if(!$ret) throw new ValidateException('数据不存在');
  52. $key = 'Meal_'.$merId.'_'.$data['meal_id'].'_'.date('YmdH',time());
  53. $arr = [
  54. 'meal_id' => $ret['meal_id'],
  55. 'name' => $ret['name'],
  56. 'num' => $ret['num'],
  57. 'price' => $ret['price'],
  58. 'type' => $ret['type'],
  59. ];
  60. $param = [
  61. 'status' => 0,
  62. 'is_del' => 0,
  63. 'mer_id' => $merId,
  64. 'type' => $ret['type'],
  65. 'meal_id'=> $ret['meal_id'],
  66. 'pay_type' => $data['pay_type'],
  67. 'attach' => 'meal',
  68. 'order_info' => json_encode($arr,JSON_UNESCAPED_UNICODE),
  69. 'pay_price' => $ret['price'],
  70. ];
  71. return compact('key', 'param');
  72. }
  73. /**
  74. * TODO 商户保证金 支付
  75. * @param $merId
  76. * @param $data
  77. * @return array
  78. * @author Qinii
  79. * @day 1/26/22
  80. */
  81. public function margin($merId, $data)
  82. {
  83. $ret = app()->make(MerchantRepository::class)->get($merId);
  84. if ($ret['is_margin'] !== 1) throw new ValidateException('此商户无需支付保证金');
  85. $key = 'Margin_'.$merId.'_'.date('YmdH',time());
  86. $arr = [
  87. 'type_id' => $ret['type_id'],
  88. 'is_margin' => $ret['is_margin'],
  89. 'margin' => $ret['margin'],
  90. ];
  91. $param = [
  92. 'status' => 0,
  93. 'is_del' => 0,
  94. 'mer_id' => $merId,
  95. 'type' => 10,
  96. 'meal_id'=> $ret['type_id'],
  97. 'pay_type' => $data['pay_type'],
  98. 'attach' => 'meal',
  99. 'order_info' => json_encode($arr,JSON_UNESCAPED_UNICODE),
  100. 'pay_price' => $ret['margin'],
  101. ];
  102. return compact('key', 'param');
  103. }
  104. public function delivery($merId, $data)
  105. {
  106. $key = 'Delivery_'.$merId.'_'.md5(date('YmdH',time()).$data['price']);
  107. $arr = ['price' => $data['price']];
  108. $param = [
  109. 'status' => 0,
  110. 'is_del' => 0,
  111. 'mer_id' => $merId,
  112. 'type' => 20,
  113. 'meal_id'=> 0,
  114. 'pay_type' => $data['pay_type'],
  115. 'attach' => 'meal',
  116. 'order_info' => json_encode($arr,JSON_UNESCAPED_UNICODE),
  117. 'pay_price' => $data['price'],
  118. ];
  119. return compact('key', 'param');
  120. }
  121. public function QrCode(int $merId, string $type, array $data)
  122. {
  123. $res = $this->{$type}($merId, $data);
  124. $key = $res['key'];
  125. $param = $res['param'];
  126. if(!$result = Cache::store('file')->get($key)){
  127. $order_sn = $this->setOrderSn(null);
  128. $param['order_sn'] = $order_sn;
  129. $param['body'] = $order_sn;
  130. $payType = $data['pay_type'] == 1 ? 'weixinQr' : 'alipayQr';
  131. $service = new PayService($payType,$param);
  132. $code = $service->pay(null);
  133. $endtime = time() + 1800 ;
  134. $result = [
  135. 'config' => $code['config'],
  136. 'endtime'=> date('Y-m-d H:i:s',$endtime),
  137. 'price' => $param['pay_price']
  138. ];
  139. Cache::store('file')->set($key,$result,30);
  140. $param['key'] = $key;
  141. Cache::store('file')->set($order_sn,$param,60 * 24);
  142. }
  143. return $result;
  144. }
  145. public function paySuccess($data)
  146. {
  147. $get = $this->dao->getWhere(['order_sn' => $data['order_sn']]);
  148. if(!$get){
  149. $dat = Cache::store('file')->get($data['order_sn']);
  150. $key = $dat['key'];
  151. unset($dat['attach'],$dat['body'],$dat['key']);
  152. $dat['status'] = 1;
  153. $dat['pay_time'] = date('y_m-d H:i:s', time());
  154. Db::transaction(function () use($data, $dat,$key){
  155. $res = $this->dao->create($dat);
  156. $this->payAfter($dat,$res);
  157. Cache::store('file')->delete($data['order_sn']);
  158. Cache::store('file')->delete($key);
  159. });
  160. }
  161. }
  162. public function payAfter($dat, $ret = null)
  163. {
  164. $info = json_decode($dat['order_info']);
  165. switch ($dat['type']) {
  166. case self::TYPE_COPY_PRODUCT:
  167. app()->make(ProductCopyRepository::class)->add([
  168. 'type' => 'pay_copy',
  169. 'num' => $info->num,
  170. 'info' => $dat['order_info'],
  171. 'message' => '购买复制商品套餐' ,
  172. ],$dat['mer_id']);
  173. break;
  174. case self::TYPE_DUMP:
  175. app()->make(ProductCopyRepository::class)->add(['type' => 'pay_dump', 'num' => $info->num, 'info' => $dat['order_info'], 'message' => '购买电子面单套餐',],$dat['mer_id']);
  176. break;
  177. case self::TYPE_MARGIN:
  178. $res = app()->make(MerchantRepository::class)->get($dat['mer_id']);
  179. if (bccomp($res['margin'] , $dat['pay_price'], 2) === 0) {
  180. $res->ot_margin = $res['margin'];
  181. $res->is_margin = 10;
  182. } else {
  183. $res->is_margin = 20;
  184. }
  185. $res->save();
  186. break;
  187. case self::TYPE_DELIVERY:
  188. $res = app()->make(MerchantRepository::class)->get($dat['mer_id']);
  189. if ($res) {
  190. $res->delivery_balance = bcadd($res->delivery_balance, $dat['pay_price'],2);
  191. $res->save();
  192. } else {
  193. Log::info('同城配送充值异常 :'. json_encode($dat));
  194. }
  195. break;
  196. default:
  197. break;
  198. }
  199. return ;
  200. }
  201. public function setOrderSn($profix)
  202. {
  203. list($msec, $sec) = explode(' ', microtime());
  204. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  205. $orderId = ($profix ?:'cs') . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369));
  206. return $orderId;
  207. }
  208. public function getList(array $where, int $page, int $limit)
  209. {
  210. $where['is_del'] = 0;
  211. $query = $this->dao->search($where)->with([
  212. 'merchant' => function($query){
  213. $query->with(['merchantType']);
  214. $query->field('mer_id,mer_name,is_trader,mer_avatar,type_id,mer_phone,mer_address,is_margin,margin,real_name,ot_margin');
  215. }
  216. ])->order('ServeOrder.create_time DESC');
  217. $count = $query->count();
  218. $list = $query->page($page, $limit)->select();
  219. return compact('count','list');
  220. }
  221. }