Package.php 14 KB

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