Vrorder.php 18 KB

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