xhOrderService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\models\xhOrder;
  5. use common\components\stringUtil;
  6. use common\components\dict;
  7. class xhOrderService
  8. {
  9. /**
  10. * 创建订单
  11. */
  12. public static function add($data)
  13. {
  14. $date = date("Y-m-d H:i:s");
  15. $sjId = isset($data['sjId']) ? $data['sjId'] : 0;
  16. $data['id'] = self::generateOrderNo($data['sjId'], $data['userId']);
  17. $data['sjId'] = $sjId;
  18. $data['payStatus'] = 0;
  19. $data['createTime'] = $date;
  20. $data['addTime'] = time();
  21. $time = strtotime(date("Y-m-d"));
  22. $year = date("Y");
  23. $month = date("m");
  24. $dayReturn = xhOrderDayNumService::addOne($time, $sjId);
  25. $monthReturn = xhOrderMonthNumService::addOne($year, $month, $sjId);
  26. $data['sendNum'] = (string)$monthReturn['riseNum'];//将花店每月的总订单数作为编号
  27. $return = xhOrder::add($data);
  28. $id = $return['id'];
  29. self::refresh($id);
  30. return $return;
  31. }
  32. /**
  33. * 通过ID获取数据
  34. * @param $id
  35. * @param int $sjId 商户号 -- 默认为0; 当不为0时,会与订单的sjId进行校验(必须要相同)
  36. * @return array|null|\yii\db\ActiveRecord
  37. */
  38. public static function getById($id, $sjId = 0)
  39. {
  40. $order = xhOrder::find()->where(['id' => $id])->asArray()->one();
  41. return $order;
  42. }
  43. public static function getByOrderSn($orderSn)
  44. {
  45. return xhOrder::find()->where(['orderSn' => $orderSn])->asArray()->one();
  46. }
  47. public static function updateById($id, $data)
  48. {
  49. $re = xhOrder::updateById($id, $data);
  50. self::refresh($id);
  51. return $re;
  52. }
  53. public static function updateByCondition($condition, $data)
  54. {
  55. xhOrder::updateByCondition($condition, $data);
  56. }
  57. public static function refresh($id)
  58. {
  59. $cacheKey = dict::getCacheKey('order') . $id;
  60. Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
  61. self::getById($id);
  62. }
  63. public static function getAllByCondition($condition)
  64. {
  65. return xhOrder::find()->where($condition)->orderBy('createTime desc')->asArray()->all();
  66. }
  67. /**
  68. * 付款订单 表单提交数据处理
  69. * @param $payAdd
  70. * @param $post
  71. */
  72. public static function formatPost(&$payAdd, $post)
  73. {
  74. $payAdd['actPrice'] = self::handle($post, 'consumeAmount', 0);
  75. $payAdd['province'] = self::handle($post, 'province');
  76. $payAdd['city'] = self::handle($post, 'city');
  77. $payAdd['dist'] = self::handle($post, 'dist');
  78. $payAdd['address'] = self::handle($post, 'address');
  79. $payAdd['fullAddress'] = self::handle($post, 'fullAddress');
  80. $payAdd['floor'] = self::handle($post, 'floor');
  81. $payAdd['long'] = self::handle($post, 'long');
  82. $payAdd['lat'] = self::handle($post, 'lat');
  83. $payAdd['anonymity'] = (int)self::handle($post, 'anonymity', 0); //0不匿名派送 1匿名派送
  84. $payAdd['sendCost'] = self::handle($post, 'sendCost', 0);
  85. //$payAdd['sendNum'] = self::handle($post, 'sendNum'); //配送编号,方便寻找订单对应的花, '')束 -- 不在这处理
  86. $payAdd['sendDistance'] = self::handle($post, 'sendDistance', 0);
  87. $payAdd['reachTime'] = self::handle($post, 'reachTime', '');
  88. $payAdd['reachPeriod'] = (int)self::handle($post, 'reachPeriod', 0); //0上午1下午2晚上,具体看配置文件
  89. $payAdd['bookMobile'] = self::handle($post, 'bookMobile');
  90. $payAdd['bookName'] = self::handle($post, 'bookName');
  91. $payAdd['receiveMobile'] = self::handle($post, 'receiveMobile');
  92. $payAdd['receiveUserName'] = self::handle($post, 'receiveUserName');
  93. $payAdd['needSend'] = (int)self::handle($post, 'needSend', 1);//默认要配送
  94. $payAdd['blessing'] = self::handle($post, 'blessing');
  95. $payAdd['remark'] = self::handle($post, 'remark');
  96. $payAdd['uniCode'] = self::handle($post, 'uniCode'); //唯一号码,预留字段
  97. $payAdd['luckyNum'] = self::handle($post, 'luckyNum', 0); //抽奖号码
  98. $payAdd['printStatus'] = self::handle($post, 'printStatus', 0);
  99. $payAdd['status'] = self::handle($post, 'status', 0); //订单生存状态 0正常 1删除
  100. $payAdd['orderName'] = self::handle($post, 'orderName'); //订单名称(商品名称)
  101. $payAdd['sourceType'] = self::handle($post, 'sourceType', 1); //订单来源,0商城订单 1付款订单
  102. }
  103. /**
  104. * 默认值处理 与 数据校验
  105. * @param $post
  106. * @param $key
  107. * @param string $default
  108. * @param string $checkType
  109. * @return string
  110. */
  111. public static function handle($post, $key, $default = '', $checkType = '')
  112. {
  113. switch ($checkType) {
  114. case 'date':
  115. break;
  116. }
  117. return isset($post["$key"]) ? $post["$key"] : $default;
  118. }
  119. /**
  120. * 生成订单号
  121. */
  122. public static function generateOrderNo($sjId, $userId)
  123. {
  124. return stringUtil::generateOrderNo($sjId, $userId);
  125. }
  126. }