xhOrderService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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['useType'] = self::handle($post, 'useType', 0); //赠送类型,0送给别人,1寄给自己
  78. $payAdd['anonymity'] = (int)self::handle($post, 'anonymity', 0); //0不匿名派送 1匿名派送
  79. $payAdd['sendCost'] = self::handle($post, 'sendCost', 0);
  80. //$payAdd['sendNum'] = self::handle($post, 'sendNum'); //配送编号,方便寻找订单对应的花, '')束 -- 不在这处理
  81. $payAdd['sendDistance'] = self::handle($post, 'sendDistance', 0);
  82. $payAdd['reachTime'] = self::handle($post, 'reachTime', '');
  83. $payAdd['reachPeriod'] = (int)self::handle($post, 'reachPeriod', 0); //0上午1下午2晚上,具体看配置文件
  84. $payAdd['bookMobile'] = self::handle($post, 'bookMobile');
  85. $payAdd['bookName'] = self::handle($post, 'bookName');
  86. $payAdd['receiveMobile'] = self::handle($post, 'receiveMobile');
  87. $payAdd['receiveUserName'] = self::handle($post, 'receiveUserName');
  88. $payAdd['needCard'] = (int)self::handle($post, 'needCard', 0);//默认不要贺卡
  89. $payAdd['needSend'] = (int)self::handle($post, 'needSend', 1);//默认要配送
  90. $payAdd['blessing'] = self::handle($post, 'blessing');
  91. $payAdd['remark'] = self::handle($post, 'remark');
  92. $payAdd['uniCode'] = self::handle($post, 'uniCode'); //唯一号码,预留字段
  93. $payAdd['luckyNum'] = self::handle($post, 'luckyNum', 0); //抽奖号码
  94. $payAdd['printStatus'] = self::handle($post, 'printStatus', 0);
  95. $payAdd['status'] = self::handle($post, 'status', 0); //订单生存状态 0正常 1删除
  96. $payAdd['orderName'] = self::handle($post, 'orderName'); //订单名称(商品名称)
  97. $payAdd['sourceType'] = self::handle($post, 'sourceType', 1); //订单来源,0商城订单 1付款订单
  98. }
  99. /**
  100. * 默认值处理 与 数据校验
  101. * @param $post
  102. * @param $key
  103. * @param string $default
  104. * @param string $checkType
  105. * @return string
  106. */
  107. public static function handle($post, $key, $default='', $checkType=''){
  108. switch ($checkType){
  109. case 'date':
  110. break;
  111. }
  112. return isset($post["$key"]) ? $post["$key"] : $default;
  113. }
  114. /**
  115. * 生成订单号
  116. */
  117. public static function generateOrderNo($merchantId,$userId)
  118. {
  119. return stringUtil::generateOrderNo($merchantId,$userId);
  120. }
  121. }