Vrorder.php 18 KB

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