Package.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace app\models\store;
  3. use app\models\system\SystemUserLevel;
  4. use app\models\user\User;
  5. use app\models\user\UserBill;
  6. use crmeb\basic\BaseModel;
  7. use crmeb\services\UtilService;
  8. use crmeb\traits\ModelTrait;
  9. use think\Exception;
  10. use think\facade\Log;
  11. use think\Model;
  12. class Package extends BaseModel
  13. {
  14. use ModelTrait;
  15. /**
  16. * 预约
  17. * @param $uid
  18. * @param $package_manager
  19. * @param $price
  20. * @param $day
  21. * @param $proportion
  22. * @return Package|bool|Model
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\DbException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. */
  27. public static function reserve($uid,$whole_id,$time_id,$to_uid,$price,$first_price,$last_id)
  28. {
  29. $user = User::find($uid);
  30. $data['uid'] = $uid;
  31. $data['whole_id'] = $whole_id;
  32. $data['time_id'] = $time_id;
  33. $data['first_price'] = $first_price;
  34. $data['last_id'] = $last_id;
  35. $data['price'] = $price;
  36. if(cache('reserve_'.$uid,1) && false) return self::setErrorInfo('你已报名,无需重复报名');
  37. if($last_id>0 && self::where('last_id',$last_id)->where('uid',$uid)->find())
  38. {
  39. return self::setErrorInfo('你已报名,无需重复报名');
  40. }elseif($last_id>0 && self::where('last_id',$last_id)->find())
  41. {
  42. return self::setErrorInfo('该订单已有预约');
  43. }
  44. else
  45. {
  46. cache('reserve_'.$uid,1,60);
  47. }
  48. $integral = bcmul($price,bcdiv(sys_config('reserve_integral'),100,2),2);
  49. if($integral>$user['gold'])
  50. {
  51. cache('reserve_'.$uid,null);
  52. return self::setErrorInfo('金豆不足,请先买金豆专区');
  53. }
  54. if($last_id>0 && self::where('id',$last_id)->value('status')!=3) {
  55. cache('reserve_'.$uid,null);
  56. return self::setErrorInfo('已有用户预约,换个商品预约');
  57. }
  58. $data['add_time'] = time();
  59. $data['order_id'] = self::getNewOrderId();
  60. if($to_uid==0)
  61. {
  62. $to_uid = User::where('is_sys', 1)->order('sys_pay asc,uid asc')->value('uid');
  63. }
  64. $data['to_uid'] = $to_uid;
  65. self::beginTrans();
  66. try{
  67. $data['use_integral'] = $integral;
  68. $res = self::create($data);
  69. User::where('uid',$uid)->dec('gold',$integral)->update();
  70. UserBill::expend("预约扣金豆",$uid,'gold','pay_money',$integral,$res['id'],bcsub($user['gold'],$integral,2),'预约扣除'.$integral."金豆");
  71. if($last_id==0) User::edit(['is_whole'=>1],$uid);
  72. if($last_id>0) self::where('id',$last_id)->update(['status'=>3]);
  73. self::commitTrans();
  74. return $res->toArray();
  75. }catch (Exception $e)
  76. {
  77. cache('reserve_'.$uid,null);
  78. Log::error('预约出错:' . $e->getLine().'--'.$e->getMessage());
  79. return self::setErrorInfo($e->getMessage(),true);
  80. }
  81. }
  82. /**
  83. * 生成订单唯一id
  84. * @param $uid 用户uid
  85. * @return string
  86. */
  87. public static function getNewOrderId()
  88. {
  89. list($msec, $sec) = explode(' ', microtime());
  90. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  91. $orderId = 'pk' . $msectime . mt_rand(10000, 99999);
  92. while (self::be(['order_id' => $orderId])) $orderId = 'pk' . $msectime . mt_rand(10000, 99999);
  93. return $orderId;
  94. }
  95. /**
  96. * 我的订单
  97. * @return bool|string
  98. */
  99. public static function getlist()
  100. {
  101. $rs = sys_data('package_manager');
  102. $am= explode("~",sys_config('start_time'));
  103. $pm= explode("~",sys_config('end_time'));
  104. foreach ($rs as &$v) {
  105. $v['cts'] =self::where('package_manager',$v['id'])->where('add_time','>=',strtotime(date("Y-m-d ")))->where('add_time','<',strtotime(date("Y-m-d ").$v['end']))->count();
  106. $v['cts'] = bcadd($v['virtual'],$v['cts']);
  107. }
  108. return $rs;
  109. }
  110. /**
  111. * 收货确认
  112. * @param $id
  113. */
  114. public static function take($id)
  115. {
  116. $info = self::find($id);
  117. $data['status'] = 2;
  118. $data['paid'] = 1;
  119. self::beginTrans();
  120. try {
  121. $package_income = bcmul($info['price'],bcdiv(sys_config('package_income'),100,2),2);
  122. $confirm_receipt = bcmul($info['price'],bcdiv(sys_config('confirm_receipt'),100,2),2);
  123. $gold_bond = bcmul($info['price'],bcdiv(sys_config('gold_bond'),100,2),2);
  124. User::where('uid',$info['uid'])->inc('brokerage_price',$package_income)->inc('integral',$gold_bond)->update();
  125. $user = User::where('uid',$info['uid'])->find();
  126. UserBill::income('订单收益',$info['uid'],'now_money','brokerage',$package_income,$info['id'],$user['brokerage_price'],'订单收益'.$package_income);
  127. UserBill::income('订单金券收益',$info['uid'],'integral','package',$gold_bond,$info['id'],$user['gold'],'订单金券收益'.$gold_bond);
  128. $res = self::edit($data,$id);
  129. User::where('uid',$info['to_uid'])->inc('brokerage_price',$confirm_receipt)->update();
  130. $to_user = User::where('uid',$info['to_uid'])->find();
  131. UserBill::income('订单确认收益',$info['to_uid'],'now_money','brokerage',$confirm_receipt,$info['id'],$to_user['brokerage_price'],'订单确认收益'.$confirm_receipt);
  132. if($info['last_id']>0) self::edit(['status'=>4],$info['last_id']);
  133. self::commitTrans();
  134. }
  135. catch (Exception $e)
  136. {
  137. Log::error('收货错误:' .$e->getFile().'行'.$e->getLine()."原因:".$e->getMessage());
  138. self::setErrorInfo('收货确认错误',true);
  139. }
  140. return true;
  141. }
  142. /**
  143. * 订单无效
  144. * @param $id
  145. */
  146. public static function invalid($id,$re='')
  147. {
  148. $data['status'] = -1;
  149. $data['re'] = $re;
  150. $last_id = self::where('id',$id)->value('last_id');
  151. if($last_id>0) self::edit(['status'=>2],$last_id);
  152. return self::edit($data,$id);
  153. }
  154. /**
  155. * 支付超时
  156. */
  157. public static function time_out()
  158. {
  159. $list = self::where('status',1)->where('add_time','<',strtotime("-".sys_config('order_whole_time')." hour"))->select();
  160. foreach ($list as $v)
  161. {
  162. if($v['last_id']>0) self::edit(['status'=>2],$v['last_id']);
  163. self::edit(['status'=>-2],$v['id']);
  164. }
  165. return true;
  166. }
  167. /**
  168. * 收货确认超时
  169. * @return bool
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\DbException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. */
  174. public static function take_outs()
  175. {
  176. $data['status'] = 2;
  177. $data['paid'] = 1;
  178. $list = self::where('pay_time', '<', strtotime("-" . sys_config('order_whole_time') . " hour"))->select()->toArray();
  179. self::beginTrans();
  180. try {
  181. foreach ($list as $info) {
  182. $confirm_receipt = bcmul($info['price'], bcdiv(sys_config('confirm_receipt'), 100, 2), 2);
  183. $package_income = bcmul($info['price'], bcdiv(sys_config('package_income'), 100, 2), 2);
  184. $confirm_receipt1 = bcmul($info['price'], bcdiv(bcsub(sys_config('confirm_receipt'), sys_config('not_confirm_receipt', 2.2), 2), 100, 2), 2);
  185. User::where('uid', $info['uid'])->inc('brokerage_price', $package_income)->update();
  186. $user = User::where('uid', $info['uid'])->find();
  187. UserBill::income('订单收益', $info['uid'], 'now_money', 'brokerage', $package_income, $info['id'], $user['brokerage_price'], '订单收益' . $package_income);
  188. $res = self::edit($data, $info['id']);
  189. User::where('uid', $info['to_uid'])->inc('brokerage_price', $confirm_receipt1)->update();
  190. $to_user = User::where('uid', $info['to_uid'])->find();
  191. UserBill::income('订单确认收益', $info['to_uid'], 'now_money', 'brokerage', $confirm_receipt1, $info['id'], $to_user['brokerage_price'], '订单确认收益' . $confirm_receipt1 . ',未确认收货系统确认,扣除收益' . bcsub($confirm_receipt, $confirm_receipt1, 2));
  192. if ($info['last_id'] > 0) self::edit(['status' => 4], $info['last_id']);
  193. }
  194. } catch (Exception $e) {
  195. Log::error('收货错误:' . $e->getFile() . '行' . $e->getLine() . "原因:" . $e->getMessage());
  196. self::setErrorInfo('收货确认错误', true);
  197. }
  198. self::commitTrans();
  199. return true;
  200. }
  201. public static function take_out($id)
  202. {
  203. $info = self::where('pay_time','<',strtotime("-".sys_config('order_whole_time')." hour"))->where('id',$id)->find();
  204. if($info)
  205. {
  206. $data['status'] = 2;
  207. $data['paid'] = 1;
  208. self::beginTrans();
  209. try {
  210. $confirm_receipt = bcmul($info['price'],bcdiv(sys_config('confirm_receipt'),100,2),2);
  211. $package_income = bcmul($info['price'],bcdiv(sys_config('package_income'),100,2),2);
  212. $confirm_receipt1 = bcmul($info['price'],bcdiv(bcsub(sys_config('confirm_receipt'),sys_config('not_confirm_receipt',2.2),2),100,2),2);
  213. User::where('uid',$info['uid'])->inc('brokerage_price',$package_income)->update();
  214. $user = User::where('uid',$info['uid'])->find();
  215. UserBill::income('订单收益',$info['uid'],'now_money','brokerage',$package_income,$info['id'],$user['brokerage_price'],'订单收益'.$package_income);
  216. $res = self::edit($data,$id);
  217. User::where('uid',$info['to_uid'])->inc('brokerage_price',$confirm_receipt1)->update();
  218. $to_user = User::where('uid',$info['to_uid'])->find();
  219. UserBill::income('订单确认收益',$info['to_uid'],'now_money','brokerage',$confirm_receipt1,$info['id'],$to_user['brokerage_price'],'订单确认收益'.$confirm_receipt1.',未确认收货系统确认,扣除收益'.bcsub($confirm_receipt,$confirm_receipt1,2));
  220. if($info['last_id']>0) self::edit(['status'=>4],$info['last_id']);
  221. self::commitTrans();
  222. }
  223. catch (Exception $e)
  224. {
  225. Log::error('收货错误:' .$e->getFile().'行'.$e->getLine()."原因:".$e->getMessage());
  226. self::setErrorInfo('收货确认错误',true);
  227. }
  228. return true;
  229. }
  230. else
  231. {
  232. return self::setErrorInfo('未到'.sys_config('order_whole_time')."小时,不能后台确认");
  233. }
  234. }
  235. /**
  236. * 获取中奖用户编号
  237. * 先根据投入积分求出未中奖用户,然后在获取中奖用户
  238. * @param $user
  239. * @return array
  240. */
  241. public static function getrand($user, $prize_person_num = 1)
  242. {
  243. $user_arr = []; //随机数组
  244. $n = 0;
  245. $count = 0;$sum = 0;
  246. foreach ($user as $v)
  247. {
  248. $sum += bcadd(100,0,0);
  249. }
  250. foreach ($user as $v) {
  251. $user_arr [$n] = [
  252. 'id' => $v['id'],
  253. "start" => $count,
  254. "end" => $count + ($sum-bcadd(100,0,0)),
  255. ];
  256. $n++;
  257. $count +=($sum-bcadd(100,0,0))+1;
  258. }
  259. $prize_arr = [];
  260. $n = $prize_person_num; //中奖人数
  261. while (sizeof($prize_arr) < $n) {
  262. $random = mt_rand(0, $count); //随机数
  263. foreach ($user_arr as $item) {
  264. if ($random >= $item['start'] && $random <= $item['end'] && !in_array($item['id'],$prize_arr)) {
  265. $prize_arr[] = $item['id'];
  266. break;
  267. }
  268. }
  269. }
  270. return $prize_arr;
  271. }
  272. /**
  273. * 获取列表
  274. * @param $where
  275. */
  276. public static function lst($where)
  277. {
  278. $model = new self;
  279. if(isset($where['data']) && $where['data']!='')
  280. {
  281. $model = $model->getModelTime($where,$model);
  282. }
  283. else
  284. {
  285. $model = $model->where('pay_time','<',strtotime("-1 day"));
  286. }
  287. if(isset($where['uid']) && $where['uid']>0) $model = $model->where('uid',$where['uid']);
  288. if(isset($where['to_uid']) && $where['to_uid']>0) $model = $model->where('to_uid',$where['to_uid']);
  289. if(isset($where['status']) && $where['status']>-4) $model = $model->where('status',$where['status']);
  290. if(isset($where['time_id']) && $where['time_id']>-4) $model = $model->where('time_id',$where['time_id']);
  291. $count = $model->value('count(id)')?:0;
  292. $data = $model->order('id desc')->page($where['page'],$where['limit'])->select()->toarray();
  293. foreach ($data as &$v)
  294. {
  295. if($v['whole_id']) {
  296. $v['whole'] = StoreWholesale::find($v['whole_id'])->toArray();
  297. }
  298. else
  299. {
  300. $v['whole'] = [];
  301. }
  302. $v['user'] = User::where('uid',$v['uid'])->field('real_name,avatar,phone')->find()->toArray();
  303. if($v['to_uid']>0) {
  304. $v['touser'] = User::where('uid', $v['to_uid'])->field('real_name,avatar,phone,wx_qr,wx_no,alipay_no,alipay_name,account_Bank,bank_card,bank_name,bank_branch')->find()->toArray();
  305. }
  306. }
  307. return compact('count','data');
  308. }
  309. /**
  310. * 获取会员升级订单
  311. */
  312. public static function orderlist($where)
  313. {
  314. $model = new self;
  315. if(isset($where['data']) && $where['data'] !='') $model = $model->getModelTime($where,$model,"add_time");
  316. if(isset($where['paid']) && $where['paid'] >-1) $model = $model->where('paid',$where['paid']);
  317. if(isset($where['status']) && $where['status'] >-4) $model = $model->where('status',$where['status']);
  318. if(isset($where['key']) && $where['key'] !='') $model = $model->where('order_id','like',"%".$where['key']."%");
  319. $model = $model->order('id desc');
  320. return self::page($model, function ($v) {
  321. $v['user'] = User::where('uid',$v['uid'])->field('nickname,real_name,avatar,phone')->find()->toArray();
  322. if($v['to_uid']>0) {
  323. $v['touser'] = User::where('uid', $v['to_uid'])->field('real_name,real_name,avatar,phone,wx_qr,wx_no,alipay_no,alipay_name,account_Bank,bank_card,bank_name,bank_branch')->find()->toArray();
  324. }
  325. else
  326. {
  327. $v['touser'] = null;
  328. }
  329. }, $where);
  330. }
  331. }