| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace addons\Scratch\common\logic;
- use addons\Scratch\common\enums\ScratchEnum;
- use addons\Scratch\common\models\ScratchLogModel;
- use addons\Scratch\common\models\ScratchModel;
- use addons\Scratch\common\models\ScratchSettingModel;
- use common\models\goods\Goods;
- /**
- * Class CommonScratchLogic
- * @package addons\Scratch\common\logic
- */
- class CommonScratchLogic
- {
- public static $setting;
- public static function getNewName($item, $status = 'start')
- {
- switch ($item['type']) {
- case ScratchModel::TYPE_REDPACKET:
- return $item['price'] . '元红包';
- break;
- case ScratchModel::TYPE_BALANCE:
- return $item['price'] . '元余额';
- case ScratchModel::TYPE_COUPON:
- if ($status == 'start') {
- return $item['coupon']['name'];
- } else {
- if (isset($item['coupon']['coupon_data']) && !empty($item['coupon']['coupon_data'])) {
- $couponName = \Yii::$app->serializer->decode($item['coupon']['coupon_data'])->name;
- } else {
- $couponName = "";
- }
- return $couponName;
- }
- case ScratchModel::TYPE_SCORE:
- return $item['num'] . '积分';
- case ScratchModel::TYPE_GOODS:
- $goodsId = $item["goods_id"];
- if (isset($item["goods"])) {
- $goods = $item['goods'];
- } else {
- $goods = Goods::findOne($goodsId);
- }
- if ($status != "start") {
- $data = ["name" => $goods['goods_name'], "pic" => $goods['cover_pic']];
- return $data;
- }
- return $goods['goods_name'];
- case ScratchModel::TYPE_THANKS:
- return '谢谢参与';
- default:
- return '';
- }
- }
- public static function getNewName1($mallId, $item, $status = 'start')
- {
- $typeList = ScratchModel::getTypeList($mallId);
- switch ($item['type']) {
- case ScratchModel::TYPE_REDPACKET:
- return $item['price'] . '元' . $typeList[ScratchModel::TYPE_REDPACKET];
- break;
- case ScratchModel::TYPE_BALANCE:
- return $item['balance'] . '元' . $typeList[ScratchModel::TYPE_BALANCE];
- case ScratchModel::TYPE_COUPON:
- if ($status == 'start') {
- return $item['coupon']['name'];
- } else {
- if (isset($item['coupon']['coupon_data']) && !empty($item['coupon']['coupon_data'])) {
- $couponName = \Yii::$app->serializer->decode($item['coupon']['coupon_data'])->name;
- } else {
- $couponName = "";
- }
- return $couponName;
- }
- case ScratchModel::TYPE_SCORE:
- return $item['num'] . $typeList[ScratchModel::TYPE_SCORE];
- default:
- return '';
- }
- }
- /**
- * 获取商品信息
- * @param $item
- * @return array
- * @throws \Exception
- */
- public static function getGoodsInfo($item)
- {
- $data = [];
- $goodsId = $item["goods_id"];
- $goods = Goods::find()->select(['g.*', 'a.sign_id', 'a.id as attr_id'])->alias('g')->where([
- 'g.id' => $goodsId,
- 'a.is_delete' => 0,
- 'g.is_delete' => 0,
- 'g.mall_id' => $mallId
- ])->with(["goodsWarehouse"])->leftJoin(['a' => GoodsAttr::tableName()], 'a.goods_id = g.id')->asArray()->one();
- if (!$goods) {
- throw new \Exception('数据异常,该条数据不存在');
- }
- $data['attr_list'] = empty($goods["attr_id"]) ? null : (new Goods())->signToAttr($goods['sign_id'], $goods['attr_groups']);
- $data['attr_id'] = $goods['attr_id'];
- $data["name"] = $goods['goodsWarehouse']['name'];
- $data["pic"] = $goods['goodsWarehouse']['cover_pic'];
- return $data;
- }
- /**
- * 获取配置
- * @param string $fields
- * @return ScratchSetting|null
- */
- public static function getSetting($mallId, $fields = "*")
- {
- if (self::$setting) {
- return self::$setting;
- }
- $setting = ScratchSettingModel::find()->where(['mall_id' => $mallId])->select($fields)->one();
- if ($setting) {
- if ($setting->payment_type) {
- $setting->payment_type = json_decode($setting->payment_type, true);
- } else {
- $setting->payment_type = ['online_pay'];
- }
- $setting['send_type'] = self::sendType($setting['send_type']);
- $setting["start_at"] = date("Y-m-d H:i:s", $setting["start_at"]);
- $setting["end_at"] = date("Y-m-d H:i:s", $setting["end_at"]);
- }
- self::$setting = $setting;
- return $setting;
- }
- /**
- * 兼容4.1.0之前的发货方式
- * @desc
- *
- * @param null $data
- *
- * @return array|int|mixed|string|string[]|null
- */
- public static function sendType($data = null)
- {
- if (!$data) {
- $data = ['express', 'offline'];
- } elseif (!is_array($data)) {
- $data = json_decode($data, true);
- if (!is_array($data)) {
- if (is_numeric($data)) {
- if ($data == 2) {
- $data = ['offline'];
- } elseif ($data == 1) {
- $data = ['express'];
- } else {
- $data = ['express', 'offline'];
- }
- } else {
- $data = ['express', 'offline'];
- }
- }
- }
- return $data;
- }
- /**
- * 获取中奖记录
- * @param $mallId
- * @param array $params
- * @return array
- * @throws \ErrorException
- */
- public static function getLotteryRecord($mallId, $params = [])
- {
- $fields = ["id", "scratch_id", "coupon_id", "user_id", "status", "type", "num", "detail", "price", "order_id", "goods_id", 'raffled_at', 'created_at'];
- $params = [
- 'with' => [
- 'user' => function($q){
- $q->select('id,nickname,avatar_url');
- },
- 'goods' => function($q){
- $q->select('id,goods_name,price');
- },
- 'coupon' => function($q){
- $q->select('id,name,sub_price');
- },
- ],
- 'where' => [
- ['=', 'mall_id', $mallId],
- ],
- 'select' => $fields,
- 'page' => $postData['page'] ?? 1,
- 'limit' => $postData['limit'] ?? 10,
- 'order' => $postData['order'] ?? 'id desc',
- ];
- if (!empty($postData['type'])) {
- $params['where'][] = ['=', 'type', $postData['type']];
- } else {
- $params['where'][] = ['in', 'type', [1, 2, 3, 4, 5]];
- }
- $pageData = ScratchLogModel::listPage($params, false);
- if ($pageData === false) throw new \ErrorException(ScratchModel::getStaticError());
- $typeList = ScratchModel::getTypeList($mallId);
- $returnData["list"] = [];
- foreach ($pageData['list'] as $item) {
- $data["name"] = $typeList[$item["type"]];
- if ($item["type"] == ScratchModel::TYPE_GOODS) {
- $data['name'] = $item['goods']['goods_name'] ?? $typeList[$item["type"]];
- $data['price'] = $item['goods']['price'] ?? 0;
- $item["num"] = 1;
- }
- if ($item['type'] == ScratchModel::TYPE_COUPON) {
- $data['name'] = $item['coupon']['name'] ?? $typeList[$item["type"]];
- $data['price'] = $item['coupon']['price'] ?? 0;
- }
- $data['type'] = $item['type'];
- $data["price"] = $item["price"];
- $data["num"] = $item["num"];
- $data["nickname"] = isset($item["user"]) ? $item["user"]["nickname"] : "普通用户";
- $data["raffled_at"] = !empty($item["raffled_at"]) ? date("Y-m-d H:i:s", $item["raffled_at"]) : date("Y-m-d H:i:s", $item["created_at"]);
- $returnData["list"][] = $data;
- }
- $returnData["pagination"] = $pageData['pagination'];
- $returnData["page_count"] = $pageData['page_count'];
- return $returnData;
- }
- }
|