Vrorder.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 数据层模型
  10. */
  11. class Vrorder extends BaseModel
  12. {
  13. public $page_info;
  14. /**
  15. * 取单条订单信息
  16. * @access public
  17. * @author csdeshang
  18. * @param type $condition 条件
  19. * @param type $fields 字段
  20. * @return type
  21. */
  22. public function getVrorderInfo($condition = array(), $fields = '*')
  23. {
  24. $order_info = Db::name('vrorder')->field($fields)->where($condition)->find();
  25. if (empty($order_info)) {
  26. return array();
  27. }
  28. if (isset($order_info['order_state'])) {
  29. $state_desc = $this->_vrorderState($order_info['order_state']);
  30. $order_info['state_desc'] = $state_desc[0];
  31. $order_info['order_state_text'] = $state_desc[1];
  32. }
  33. if (isset($order_info['payment_code'])) {
  34. $order_info['payment_name'] = get_order_payment_name($order_info['payment_code']);
  35. }
  36. return $order_info;
  37. }
  38. /**
  39. * 新增订单
  40. * @access public
  41. * @author csdeshang
  42. * @param type $data 参数内容
  43. * @return type
  44. */
  45. public function addVrorder($data)
  46. {
  47. return Db::name('vrorder')->insertGetId($data);
  48. }
  49. /**
  50. * 新增订单
  51. * @access public
  52. * @author csdeshang
  53. * @param type $order_info 订单信息
  54. * @return boolean
  55. */
  56. public function addVrorderCode($order_info)
  57. {
  58. $vrc_num = Db::name('vrordercode')->where(array('order_id' => $order_info['order_id']))->count();
  59. if (!empty($vrc_num) && intval($vrc_num) >= intval($order_info['goods_num']))
  60. return false;
  61. if (empty($order_info))
  62. return false;
  63. //均摊后每个兑换码支付金额
  64. $each_pay_price = ds_price_format($order_info['order_amount'] / $order_info['goods_num']);
  65. //取得店铺兑换码前缀
  66. $store_info = model('store')->getStoreInfoByID($order_info['store_id']);
  67. $virtual_code_perfix = $store_info['store_vrcode_prefix'] ? $store_info['store_vrcode_prefix'] : rand(100, 999);
  68. //生成兑换码
  69. $code_list = $this->_makeVrordercode($virtual_code_perfix, $order_info['store_id'], $order_info['buyer_id'], $order_info['goods_num']);
  70. for ($i = 0; $i < $order_info['goods_num']; $i++) {
  71. $order_code[$i]['order_id'] = $order_info['order_id'];
  72. $order_code[$i]['store_id'] = $order_info['store_id'];
  73. $order_code[$i]['buyer_id'] = $order_info['buyer_id'];
  74. $order_code[$i]['vr_code'] = $code_list[$i];
  75. $order_code[$i]['pay_price'] = $each_pay_price;
  76. $order_code[$i]['vr_indate'] = $order_info['vr_indate'];
  77. $order_code[$i]['vr_invalid_refund'] = $order_info['vr_invalid_refund'];
  78. }
  79. //将因舍出小数部分出现的差值补到最后一个商品的实际成交价中
  80. // $diff_amount = $order_info['order_amount'] - $each_pay_price * $order_info['goods_num'];
  81. // $order_code[$i-1]['pay_price'] += $diff_amount;
  82. return Db::name('vrordercode')->insertAll($order_code);
  83. }
  84. /**
  85. * 更改订单信息
  86. * @access public
  87. * @author csdeshang
  88. * @param type $data 数据
  89. * @param type $condition 条件
  90. * @param type $limit 限制
  91. * @return type
  92. */
  93. public function editVrorder($data, $condition, $limit = 0)
  94. {
  95. return Db::name('vrorder')->where($condition)->limit($limit)->update($data);
  96. }
  97. /**
  98. * 更新兑换码
  99. * @access public
  100. * @author csdeshang
  101. * @param type $data 数据
  102. * @param type $condition 条件
  103. * @return type
  104. */
  105. public function editVrorderCode($data, $condition)
  106. {
  107. return Db::name('vrordercode')->where($condition)->update($data);
  108. }
  109. /**
  110. * 兑换码列表
  111. * @access public
  112. * @author csdeshang
  113. * @param type $condition 条件
  114. * @param type $fields 字段
  115. * @param type $pagesize 分页
  116. * @param type $order 排序
  117. * @return type
  118. */
  119. public function getVrordercodeList($condition = array(), $fields = '*', $pagesize = '', $order = 'rec_id desc')
  120. {
  121. if ($pagesize) {
  122. $res = Db::name('vrordercode')->field($fields)->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  123. $this->page_info = $res;
  124. return $res->items();
  125. } else {
  126. return Db::name('vrordercode')->field($fields)->where($condition)->order($order)->select()->toArray();
  127. }
  128. }
  129. /**
  130. * 兑换码列表
  131. * @access public
  132. * @author csdeshang
  133. * @param type $condition 条件
  134. * @param type $fields 字段
  135. * @return type
  136. */
  137. public function getCodeUnusedList($condition = array(), $fields = '*')
  138. {
  139. $condition[] = array('vr_state', '=', 0);
  140. $condition[] = array('refund_lock', '=', 0);
  141. return $this->getVrordercodeList($condition, $fields);
  142. }
  143. /**
  144. * 根据虚拟订单取没有使用的兑换码列表
  145. * @access public
  146. * @author csdeshang
  147. * @param type $order_list 订单列表
  148. * @return type
  149. */
  150. public function getCodeRefundList($order_list = array())
  151. {
  152. if (!empty($order_list) && is_array($order_list)) {
  153. $order_ids = array(); //订单编号数组
  154. foreach ($order_list as $key => $value) {
  155. $order_id = $value['order_id'];
  156. $order_ids[$order_id] = $key;
  157. }
  158. $condition = array();
  159. $condition[] = array('order_id', 'in', array_keys($order_ids));
  160. $condition[] = array('refund_lock', '=', '0'); //退款锁定状态:0为正常(能退款),1为锁定(待审核),2为同意
  161. $code_list = $this->getVrordercodeList($condition);
  162. if (!empty($code_list) && is_array($code_list)) {
  163. foreach ($code_list as $key => $value) {
  164. $order_id = $value['order_id']; //虚拟订单编号
  165. $rec_id = $value['rec_id']; //兑换码表编号
  166. if ($value['vr_state'] != '1') { //使用状态 0:未使用1:已使用2:已过期
  167. $order_key = $order_ids[$order_id];
  168. $order_list[$order_key]['code_list'][$rec_id] = $value;
  169. }
  170. }
  171. }
  172. }
  173. return $order_list;
  174. }
  175. /**
  176. * 取得兑换码列表
  177. * @access public
  178. * @author csdeshang
  179. * @param type $condition 条件
  180. * @param type $fields 字段
  181. * @return array
  182. */
  183. public function getShowVrordercodeList($condition = array(), $fields = '*')
  184. {
  185. $code_list = $this->getVrordercodeList($condition);
  186. //进一步处理
  187. if (!empty($code_list)) {
  188. $i = 0;
  189. foreach ($code_list as $k => $v) {
  190. if ($v['vr_state'] == '1') {
  191. $content = '已使用,使用时间 ' . date('Y-m-d', $v['vr_usetime']);
  192. } else if ($v['vr_state'] == '0') {
  193. if ($v['vr_indate'] < TIMESTAMP) {
  194. $content = '已过期,过期时间 ' . date('Y-m-d', $v['vr_indate']);
  195. } else {
  196. $content = '未使用,有效期至 ' . date('Y-m-d', $v['vr_indate']);
  197. }
  198. }
  199. if ($v['refund_lock'] == '1') {
  200. $content = '退款审核中';
  201. } else if ($v['refund_lock'] == '2') {
  202. $content = '退款已完成';
  203. }
  204. $code_list[$k]['vr_code_desc'] = $content;
  205. if ($v['vr_state'] == '0')
  206. $i++;
  207. }
  208. $code_list[0]['vr_code_valid_count'] = $i;
  209. }
  210. return $code_list;
  211. }
  212. /**
  213. * 取得兑换码信息
  214. * @param type $condition 条件
  215. * @param type $fields 字段
  216. * @return type
  217. */
  218. public function getVrordercodeInfo($condition = array(), $fields = '*')
  219. {
  220. return Db::name('vrordercode')->field($fields)->where($condition)->find();
  221. }
  222. /**
  223. * 取得兑换码数量
  224. * @access public
  225. * @author csdeshang
  226. * @param type $condition 条件
  227. * @return type
  228. */
  229. public function getVrordercodeCount($condition)
  230. {
  231. return Db::name('vrordercode')->where($condition)->count();
  232. }
  233. /**
  234. * 生成兑换码 长度 =3位 + 4位 + 2位 + 3位 + 1位 + 5位随机 = 18位
  235. * @access public
  236. * @author csdeshang
  237. * @param string $perfix 前缀
  238. * @param type $store_id 店铺id
  239. * @param type $member_id 会员id
  240. * @param type $num 数字
  241. * @return string
  242. */
  243. private function _makeVrordercode($perfix, $store_id, $member_id, $num)
  244. {
  245. $perfix .= sprintf('%04d', (int) $store_id * $member_id % 10000)
  246. . sprintf('%02d', (int) $member_id % 100)
  247. . sprintf('%03d', (float) microtime() * 1000);
  248. $code_list = array();
  249. for ($i = 0; $i < $num; $i++) {
  250. $code_list[$i] = $perfix . sprintf('%01d', (int) $i % 10) . random(5, 1);
  251. }
  252. return $code_list;
  253. }
  254. /**
  255. * 取得订单列表(所有)
  256. * @access public
  257. * @author csdeshang
  258. * @param type $condition 条件
  259. * @param type $pagesize 分页
  260. * @param type $field 字段
  261. * @param type $order 排序
  262. * @param type $limit 限制
  263. * @return type
  264. */
  265. public function getVrorderList($condition, $pagesize = '', $field = '*', $order = 'order_id desc', $limit = 0)
  266. {
  267. if ($pagesize) {
  268. $list = Db::name('vrorder')->field($field)->where($condition)->order($order)->limit($limit)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  269. $this->page_info = $list;
  270. $list = $list->items();
  271. } else {
  272. $list = Db::name('vrorder')->field($field)->where($condition)->order($order)->limit($limit)->select()->toArray();
  273. }
  274. if (empty($list))
  275. return array();
  276. foreach ($list as $key => $order) {
  277. if (isset($order['order_state'])) {
  278. list($list[$key]['state_desc'], $list[$key]['order_state_text']) = $this->_vrorderState($order['order_state']);
  279. }
  280. if (isset($order['payment_code'])) {
  281. $list[$key]['payment_name'] = get_order_payment_name($order['payment_code']);
  282. }
  283. }
  284. return $list;
  285. }
  286. /**
  287. * 取得订单状态文字输出形式
  288. * @access public
  289. * @author csdeshang
  290. * @param type $order_state 订单状态
  291. * @return type
  292. */
  293. private function _vrorderState($order_state)
  294. {
  295. switch ($order_state) {
  296. case ORDER_STATE_CANCEL:
  297. $order_state = '<span style="color:#999">已取消</span>';
  298. $order_state_text = '已取消';
  299. break;
  300. case ORDER_STATE_NEW:
  301. $order_state = '<span style="color:#36C">待付款</span>';
  302. $order_state_text = '待付款';
  303. break;
  304. case ORDER_STATE_PAY:
  305. $order_state = '<span style="color:#999">已支付</span>';
  306. $order_state_text = '已支付';
  307. break;
  308. case ORDER_STATE_SUCCESS:
  309. $order_state = '<span style="color:#999">已完成</span>';
  310. $order_state_text = '已完成';
  311. break;
  312. }
  313. return array($order_state, $order_state_text);;
  314. }
  315. /**
  316. * 返回是否允许某些操作
  317. * @access public
  318. * @author csdeshang
  319. * @param type $operate 操作
  320. * @param type $order_info 订单信息
  321. * @return boolean
  322. */
  323. public function getVrorderOperateState($operate, $order_info)
  324. {
  325. $state = false;
  326. if (!is_array($order_info) || empty($order_info))
  327. return false;
  328. switch ($operate) {
  329. //买家取消订单
  330. case 'buyer_cancel':
  331. $state = $order_info['order_state'] == ORDER_STATE_NEW;
  332. break;
  333. //商家取消订单
  334. case 'store_cancel':
  335. $state = $order_info['order_state'] == ORDER_STATE_NEW;
  336. break;
  337. //平台取消订单
  338. case 'system_cancel':
  339. $state = $order_info['order_state'] == ORDER_STATE_NEW;
  340. break;
  341. //平台收款
  342. case 'system_receive_pay':
  343. $state = $order_info['order_state'] == ORDER_STATE_NEW;
  344. break;
  345. //支付
  346. case 'payment':
  347. $state = $order_info['order_state'] == ORDER_STATE_NEW;
  348. break;
  349. //评价
  350. case 'evaluation':
  351. $state = !$order_info['refund_state'] && !isset($order_info['lock_state']) && $order_info['evaluation_state'] == '0' && $order_info['use_state'] && $order_info['order_state'] == ORDER_STATE_SUCCESS;
  352. break;
  353. //买家退款
  354. case 'refund':
  355. $state = false;
  356. $code_list = isset($order_info['code_list']) ? $order_info['code_list'] : ''; //没有使用的兑换码列表
  357. if (!empty($code_list) && is_array($code_list)) { //没结算可以退款
  358. if ($order_info['vr_indate'] > TIMESTAMP) { //有效期内的能退款
  359. $state = true;
  360. }
  361. if ($order_info['vr_invalid_refund'] == 1 && ($order_info['vr_indate'] + 60 * 60 * 24 * config('ds_config.code_invalid_refund')) > TIMESTAMP) { //兑换码过期后可退款
  362. $state = true;
  363. }
  364. }
  365. break;
  366. }
  367. return $state;
  368. }
  369. /**
  370. * 订单详情页显示进行步骤
  371. * @access public
  372. * @author csdeshang
  373. * @param array $order_info 订单信息
  374. * @return array
  375. */
  376. public function getVrorderStep($order_info)
  377. {
  378. if (!is_array($order_info) || empty($order_info))
  379. return array();
  380. $step_list = array();
  381. // 第一步 下单完成
  382. $step_list['step1'] = true;
  383. //第二步 付款完成
  384. $step_list['step2'] = !empty($order_info['payment_time']);
  385. //第三步 兑换码使用中
  386. $step_list['step3'] = !empty($order_info['payment_time']);
  387. //第四步 使用完成或到期结束
  388. $step_list['step4'] = $order_info['order_state'] == ORDER_STATE_SUCCESS;
  389. return $step_list;
  390. }
  391. /**
  392. * 取得订单数量
  393. * @access public
  394. * @author csdeshang
  395. * @param array $condition 条件
  396. * @return int
  397. */
  398. public function getVrorderCount($condition)
  399. {
  400. return Db::name('vrorder')->where($condition)->count();
  401. }
  402. /**
  403. * 订单销售记录 订单状态为20、30、40时
  404. * @access public
  405. * @author csdeshang
  406. * @param type $condition 条件
  407. * @param type $field 字段
  408. * @param type $pagesize 分页
  409. * @param type $order 排序
  410. * @return type
  411. */
  412. public function getVrorderAndOrderGoodsSalesRecordList($condition, $field = "*", $pagesize = 0, $order = 'order_id desc')
  413. {
  414. $condition[] = array('order_state', 'in', array(ORDER_STATE_PAY, ORDER_STATE_SUCCESS));
  415. return $this->getVrorderList($condition, $pagesize, $field, $order);
  416. }
  417. /**
  418. * 发送兑换码
  419. * @param unknown $param
  420. * @return boolean
  421. */
  422. public function sendVrCode($param)
  423. {
  424. if (empty($param) && !is_array($param))
  425. return ds_callback(true);
  426. $condition = array();
  427. $condition[] = array('order_id', '=', $param['order_id']);
  428. $condition[] = array('buyer_id', '=', $param['buyer_id']);
  429. $condition[] = array('vr_state', '=', 0);
  430. $condition[] = array('refund_lock', '=', 0);
  431. $code_list = $this->getShowVrordercodeList($condition, 'vr_code,vr_indate');
  432. if (empty($code_list))
  433. return ds_callback(true);
  434. $content = '';
  435. foreach ($code_list as $v) {
  436. $content .= $v['vr_code'] . ',';
  437. }
  438. $tpl_info = model('mailtemplates')->getTplInfo(array('mailmt_code' => 'send_vr_code'));
  439. $data = array();
  440. $data['vr_code'] = rtrim($content, ',');
  441. $ten_data = array($data['vr_code']);
  442. $message = ds_replace_text($tpl_info['mailmt_content'], $data);
  443. $smslog_param = array(
  444. 'ali_template_code' => $tpl_info['ali_template_code'],
  445. 'ali_template_param' => $data,
  446. 'ten_template_code' => $tpl_info['ten_template_code'],
  447. 'ten_template_param' => $ten_data,
  448. 'message' => $message,
  449. );
  450. $result = model('smslog')->sendSms($param["buyer_phone"], $smslog_param);
  451. if (!$result) {
  452. return ds_callback(false, '兑换码发送失败order_id:' . $param['order_id']);
  453. } else {
  454. return ds_callback(true);
  455. }
  456. }
  457. }