Package.php 15 KB

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