xhOrderService.php 4.7 KB

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