Vrorder.php 18 KB

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