OrderModel.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * OrderModel.php UTF-8
  4. * 订单
  5. *
  6. * @date : 2018/1/19 21:31
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\order;
  13. use huo\model\common\CommonModel;
  14. use huo\model\member\MemberModel;
  15. use huolib\constant\CacheConst;
  16. use huolib\constant\CommonConst;
  17. use huolib\constant\OrderConst;
  18. use think\Cache;
  19. class OrderModel extends CommonModel {
  20. protected $name = 'pay';
  21. // 开启自动写入时间戳字段
  22. protected $autoWriteTimestamp = true;
  23. protected $cache_tag = CacheConst::TAG_CACHE_SERVER_ROLE;
  24. /**
  25. * 关联订单扩展
  26. *
  27. * @return \think\model\relation\HasOne
  28. */
  29. public function payext() {
  30. return $this->hasOne('huo\model\order\OrderExtModel', 'pay_id', 'id', [], 'left');
  31. }
  32. /**
  33. * 关联订单扩展
  34. *
  35. * @return \think\model\relation\HasOne
  36. */
  37. public function leftpayext() {
  38. return $this->hasOne('huo\model\order\OrderExtModel', 'pay_id', 'id', [], 'left')->setEagerlyType(0);
  39. }
  40. public function mem() {
  41. return $this->belongsTo(MemberModel::className(), 'mem_id', 'id', [], 'left')->setEagerlyType(0);
  42. }
  43. public function agent() {
  44. return $this->belongsTo('huo\model\user\UserModel', 'agent_id', 'id')->field('id, user_login');
  45. }
  46. public function game() {
  47. return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->field('id, name');
  48. }
  49. /**
  50. * @param array $data
  51. *
  52. * @return bool|array
  53. */
  54. public function createOrder($data) {
  55. $_data['order_id'] = get_val($data, 'order_id', '');
  56. $_data['cp_order_id'] = get_val($data, 'cp_order_id', '');
  57. $_data['mem_id'] = get_val($data, 'mem_id', 0);
  58. $_data['mg_mem_id'] = get_val($data, 'mg_mem_id', 0);
  59. $_data['agent_id'] = get_val($data, 'agent_id', 0);
  60. $_data['app_id'] = get_val($data, 'app_id', 0);
  61. $_data['vb_id'] = get_val($data, 'vb_id', 0);
  62. $_data['amount'] = get_val($data, 'amount', 0.00);
  63. $_data['real_amount'] = get_val($data, 'real_amount', 0.00);
  64. $_data['product_id'] = get_val($data, 'product_id', '');
  65. $_data['product_cnt'] = get_val($data, 'product_cnt', 1);
  66. $_data['product_name'] = get_val($data, 'product_name', '');
  67. $_data['ext'] = get_val($data, 'ext', '');
  68. $_data['coupon_amount'] = get_val($data, 'coupon_amount', 0.00);
  69. $_data['ptb_amount'] = get_val($data, 'ptb_amount', 0.00);
  70. $_data['gm_amount'] = get_val($data, 'gm_amount', 0.00);
  71. $_data['integral'] = get_val($data, 'integral', 0.00);
  72. $_data['integral_money'] = get_val($data, 'integral_money', 0.00);
  73. $_data['rebate_amount'] = get_val($data, 'rebate_amount', 0.00);
  74. $_data['rate'] = get_val($data, 'rate', 1);
  75. $_data['status'] = get_val($data, 'status', OrderConst::PAY_STATUS_NOT);
  76. $_data['is_switch'] = get_val($data, 'is_switch', OrderConst::PAY_SWITCH_NO);
  77. $_data['cp_status'] = get_val($data, 'cp_status', OrderConst::CP_STATUS_NOT);
  78. $_data['payway'] = get_val($data, 'payway', '');
  79. $_data['currency'] = get_val($data, 'currency', 'CNY');
  80. $_data['is_distribute'] = get_val($data, 'is_distribute', OrderConst::DTB_STATUS_NOT);
  81. if ($_obj = self::create($_data, true)) {
  82. $_data['id'] = $_obj->id;
  83. return $_data;
  84. } else {
  85. return false;
  86. }
  87. }
  88. /**
  89. * @param array $order_data
  90. * @param string $pay_id
  91. *
  92. * @return bool
  93. */
  94. public function updateOrder($order_data, $pay_id) {
  95. $_map['id'] = $pay_id;
  96. $_data = $order_data;
  97. $_rs = self::update($_data, $_map, true);
  98. if (false == $_rs) {
  99. return false;
  100. } else {
  101. Cache::clear($this->cache_tag);
  102. return true;
  103. }
  104. }
  105. public function getStatus($order_id) {
  106. $_map['order_id'] = $order_id;
  107. $_status = self::where($_map)->field('cp_status, status')->find();
  108. if (false == $_status) {
  109. return false;
  110. }
  111. $_rdata['status'] = $_status['status'];
  112. $_rdata['cp_status'] = $_status['cp_status'];
  113. return $_rdata;
  114. }
  115. public function getDetail($order_id) {
  116. $_map['order_id'] = $order_id;
  117. $_data = self::where($_map)->find();
  118. if (false == $_data) {
  119. return false;
  120. }
  121. $_data = $_data->toArray();
  122. return $_data;
  123. }
  124. /**
  125. * 获取最近的订单ID
  126. *
  127. * @param INT $mem_id 玩家ID
  128. * @param INT $app_id 游戏ID
  129. *
  130. * @return mixed|string
  131. */
  132. public function getLastOrderId($mem_id, $app_id) {
  133. $_map['mem_id'] = $mem_id;
  134. $_map['app_id'] = $app_id;
  135. $_max_id = $this->where($_map)->max('id');
  136. $_o_map['id'] = $_max_id;
  137. $_order_id = $this->where($_o_map)->value('order_id');
  138. if (empty($_order_id)) {
  139. return '';
  140. }
  141. return $_order_id;
  142. }
  143. /**
  144. * 根据条件获取金额
  145. *
  146. * @param $map
  147. *
  148. * @return float|int
  149. */
  150. public function getTotalAmount($map) {
  151. $_tag = $this->cache_tag;
  152. $_cache_key = md5($_tag.json_encode($map));
  153. $_sum_money = $this->where($map)
  154. ->cache($_cache_key, CommonConst::CONST_DAY_SECONDS, $_tag)
  155. ->sum('amount');
  156. return $_sum_money;
  157. }
  158. /**
  159. * 总金额
  160. *
  161. * @param array $where
  162. *
  163. * @return double
  164. */
  165. public function totalMoney($where = []) {
  166. $where['status'] = 2;
  167. return $this->where($where)->sum('amount');
  168. }
  169. /**
  170. * 昨日金额
  171. *
  172. *
  173. * @param array $map
  174. *
  175. * @return float
  176. */
  177. public function yesterdayMoney($map = []) {
  178. return $this->getSumMoney(strtotime('yesterday'), strtotime('today') - 1, $map);
  179. }
  180. /**
  181. * 今日金额
  182. *
  183. * @param array $map
  184. *
  185. * @return float
  186. */
  187. public function todayMoney($map = []) {
  188. return $this->getSumMoney(strtotime('today'), time(), $map);
  189. }
  190. /**
  191. * 本周金额
  192. *
  193. * @param array $map
  194. *
  195. * @return float|int
  196. */
  197. public function thisWeekMoney($map = []) {
  198. return $this->getSumMoney(strtotime(date('Y-m-d 00:00:00', strtotime('this week'))), time(), $map);
  199. }
  200. /**
  201. * 本月金额
  202. *
  203. * @param array $map
  204. *
  205. * @return float|int
  206. */
  207. public function thisMonthMoney($map = []) {
  208. return $this->getSumMoney(strtotime(date('Y-m-01 00:00:00')), time(), $map);
  209. }
  210. /**
  211. * 获取金额
  212. *
  213. * @param int $start_time 开始时间
  214. * @param int $end_time 结束时间
  215. *
  216. * @param array $map
  217. *
  218. * @return float|int
  219. */
  220. public function getSumMoney($start_time, $end_time, $map = []) {
  221. $where['status'] = OrderConst::PAY_STATUS_SUC;
  222. $where['create_time'] = ['between', [$start_time, $end_time]];
  223. return $this->where($map)->where($where)->sum('amount');
  224. }
  225. /**
  226. * 按天统计付费用户数
  227. *
  228. * @param int $start_time 开始时间
  229. * @param int $end_time 结束时间
  230. *
  231. * @return array
  232. * @throws \think\db\exception\DataNotFoundException
  233. * @throws \think\db\exception\ModelNotFoundException
  234. * @throws \think\exception\DbException
  235. */
  236. public function statDatePaidUser($start_time, $end_time) {
  237. $end_time += CommonConst::CONST_DAY_SECONDS - 1;
  238. $_rows = $this->where('create_time', 'between', [$start_time, $end_time])
  239. ->group('DATE(FROM_UNIXTIME(create_time))')
  240. ->field('COUNT(DISTINCT mem_id) count, DATE(FROM_UNIXTIME(create_time)) date')
  241. ->select();
  242. $_data = [];
  243. foreach ($_rows as $_row) {
  244. $_data[$_row['date']] = $_row['count'];
  245. }
  246. $_result = [];
  247. for ($_i = $start_time; $_i <= $end_time; $_i += CommonConst::CONST_DAY_SECONDS) {
  248. $_date = date('Y-m-d', $_i);
  249. $_result[$_date] = isset($_data[$_date]) ? $_data[$_date] : 0;
  250. }
  251. return $_result;
  252. }
  253. /**
  254. * 获取某玩家最后一个订单order_id
  255. *
  256. * @param $mem_id
  257. *
  258. * @return mixed
  259. */
  260. public function getMemLastOrderId($mem_id) {
  261. $_map = ['mem_id' => $mem_id];
  262. $_order_id = $this->where($_map)->order('create_time desc')->limit(1)->value('order_id');
  263. return $_order_id;
  264. }
  265. }