| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- namespace common\services;
- use Yii;
- use common\models\xhOrder;
- use common\components\stringUtil;
- use common\components\dict;
- class xhOrderService
- {
-
- /**
- * 创建订单
- */
- public static function add($data)
- {
- $date = date("Y-m-d H:i:s");
- $sjId = isset($data['sjId']) ? $data['sjId'] : 0;
- $data['id'] = self::generateOrderNo($data['sjId'], $data['userId']);
- $data['sjId'] = $sjId;
- $data['payStatus'] = 0;
- $data['createTime'] = $date;
- $data['addTime'] = time();
-
- $time = strtotime(date("Y-m-d"));
- $year = date("Y");
- $month = date("m");
- $dayReturn = xhOrderDayNumService::addOne($time, $sjId);
- $monthReturn = xhOrderMonthNumService::addOne($year, $month, $sjId);
- $data['sendNum'] = (string)$monthReturn['riseNum'];//将花店每月的总订单数作为编号
- $return = xhOrder::add($data);
- $id = $return['id'];
- self::refresh($id);
- return $return;
- }
-
- /**
- * 通过ID获取数据
- * @param $id
- * @param int $sjId 商户号 -- 默认为0; 当不为0时,会与订单的sjId进行校验(必须要相同)
- * @return array|null|\yii\db\ActiveRecord
- */
- public static function getById($id, $sjId = 0)
- {
- $order = xhOrder::find()->where(['id' => $id])->asArray()->one();
- return $order;
- }
-
- public static function getByOrderSn($orderSn)
- {
- return xhOrder::find()->where(['orderSn' => $orderSn])->asArray()->one();
- }
- public static function updateById($id, $data)
- {
- $re = xhOrder::updateById($id, $data);
- self::refresh($id);
- return $re;
- }
-
- public static function updateByCondition($condition, $data)
- {
- xhOrder::updateByCondition($condition, $data);
- }
-
- public static function refresh($id)
- {
- $cacheKey = dict::getCacheKey('order') . $id;
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
- self::getById($id);
- }
-
- public static function getAllByCondition($condition)
- {
- return xhOrder::find()->where($condition)->orderBy('createTime desc')->asArray()->all();
- }
-
- /**
- * 付款订单 表单提交数据处理
- * @param $payAdd
- * @param $post
- */
- public static function formatPost(&$payAdd, $post)
- {
- $payAdd['actPrice'] = self::handle($post, 'consumeAmount', 0);
- $payAdd['province'] = self::handle($post, 'province');
- $payAdd['city'] = self::handle($post, 'city');
- $payAdd['dist'] = self::handle($post, 'dist');
- $payAdd['address'] = self::handle($post, 'address');
- $payAdd['fullAddress'] = self::handle($post, 'fullAddress');
- $payAdd['floor'] = self::handle($post, 'floor');
- $payAdd['long'] = self::handle($post, 'long');
- $payAdd['lat'] = self::handle($post, 'lat');
- $payAdd['anonymity'] = (int)self::handle($post, 'anonymity', 0); //0不匿名派送 1匿名派送
- $payAdd['sendCost'] = self::handle($post, 'sendCost', 0);
- //$payAdd['sendNum'] = self::handle($post, 'sendNum'); //配送编号,方便寻找订单对应的花, '')束 -- 不在这处理
- $payAdd['sendDistance'] = self::handle($post, 'sendDistance', 0);
- $payAdd['reachTime'] = self::handle($post, 'reachTime', '');
- $payAdd['reachPeriod'] = (int)self::handle($post, 'reachPeriod', 0); //0上午1下午2晚上,具体看配置文件
- $payAdd['bookMobile'] = self::handle($post, 'bookMobile');
- $payAdd['bookName'] = self::handle($post, 'bookName');
- $payAdd['receiveMobile'] = self::handle($post, 'receiveMobile');
- $payAdd['receiveUserName'] = self::handle($post, 'receiveUserName');
- $payAdd['needSend'] = (int)self::handle($post, 'needSend', 1);//默认要配送
- $payAdd['blessing'] = self::handle($post, 'blessing');
- $payAdd['remark'] = self::handle($post, 'remark');
-
- $payAdd['uniCode'] = self::handle($post, 'uniCode'); //唯一号码,预留字段
- $payAdd['luckyNum'] = self::handle($post, 'luckyNum', 0); //抽奖号码
- $payAdd['printStatus'] = self::handle($post, 'printStatus', 0);
- $payAdd['status'] = self::handle($post, 'status', 0); //订单生存状态 0正常 1删除
- $payAdd['orderName'] = self::handle($post, 'orderName'); //订单名称(商品名称)
- $payAdd['sourceType'] = self::handle($post, 'sourceType', 1); //订单来源,0商城订单 1付款订单
- }
-
- /**
- * 默认值处理 与 数据校验
- * @param $post
- * @param $key
- * @param string $default
- * @param string $checkType
- * @return string
- */
- public static function handle($post, $key, $default = '', $checkType = '')
- {
- switch ($checkType) {
- case 'date':
- break;
- }
- return isset($post["$key"]) ? $post["$key"] : $default;
- }
-
- /**
- * 生成订单号
- */
- public static function generateOrderNo($sjId, $userId)
- {
- return stringUtil::generateOrderNo($sjId, $userId);
- }
-
- }
|