CommonScratchLogic.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace addons\Scratch\common\logic;
  3. use addons\Scratch\common\enums\ScratchEnum;
  4. use addons\Scratch\common\models\ScratchLogModel;
  5. use addons\Scratch\common\models\ScratchModel;
  6. use addons\Scratch\common\models\ScratchSettingModel;
  7. use common\models\goods\Goods;
  8. /**
  9. * Class CommonScratchLogic
  10. * @package addons\Scratch\common\logic
  11. */
  12. class CommonScratchLogic
  13. {
  14. public static $setting;
  15. public static function getNewName($item, $status = 'start')
  16. {
  17. switch ($item['type']) {
  18. case ScratchModel::TYPE_REDPACKET:
  19. return $item['price'] . '元红包';
  20. break;
  21. case ScratchModel::TYPE_BALANCE:
  22. return $item['price'] . '元余额';
  23. case ScratchModel::TYPE_COUPON:
  24. if ($status == 'start') {
  25. return $item['coupon']['name'];
  26. } else {
  27. if (isset($item['coupon']['coupon_data']) && !empty($item['coupon']['coupon_data'])) {
  28. $couponName = \Yii::$app->serializer->decode($item['coupon']['coupon_data'])->name;
  29. } else {
  30. $couponName = "";
  31. }
  32. return $couponName;
  33. }
  34. case ScratchModel::TYPE_SCORE:
  35. return $item['num'] . '积分';
  36. case ScratchModel::TYPE_GOODS:
  37. $goodsId = $item["goods_id"];
  38. if (isset($item["goods"])) {
  39. $goods = $item['goods'];
  40. } else {
  41. $goods = Goods::findOne($goodsId);
  42. }
  43. if ($status != "start") {
  44. $data = ["name" => $goods['goods_name'], "pic" => $goods['cover_pic']];
  45. return $data;
  46. }
  47. return $goods['goods_name'];
  48. case ScratchModel::TYPE_THANKS:
  49. return '谢谢参与';
  50. default:
  51. return '';
  52. }
  53. }
  54. public static function getNewName1($mallId, $item, $status = 'start')
  55. {
  56. $typeList = ScratchModel::getTypeList($mallId);
  57. switch ($item['type']) {
  58. case ScratchModel::TYPE_REDPACKET:
  59. return $item['price'] . '元' . $typeList[ScratchModel::TYPE_REDPACKET];
  60. break;
  61. case ScratchModel::TYPE_BALANCE:
  62. return $item['balance'] . '元' . $typeList[ScratchModel::TYPE_BALANCE];
  63. case ScratchModel::TYPE_COUPON:
  64. if ($status == 'start') {
  65. return $item['coupon']['name'];
  66. } else {
  67. if (isset($item['coupon']['coupon_data']) && !empty($item['coupon']['coupon_data'])) {
  68. $couponName = \Yii::$app->serializer->decode($item['coupon']['coupon_data'])->name;
  69. } else {
  70. $couponName = "";
  71. }
  72. return $couponName;
  73. }
  74. case ScratchModel::TYPE_SCORE:
  75. return $item['num'] . $typeList[ScratchModel::TYPE_SCORE];
  76. default:
  77. return '';
  78. }
  79. }
  80. /**
  81. * 获取商品信息
  82. * @param $item
  83. * @return array
  84. * @throws \Exception
  85. */
  86. public static function getGoodsInfo($item)
  87. {
  88. $data = [];
  89. $goodsId = $item["goods_id"];
  90. $goods = Goods::find()->select(['g.*', 'a.sign_id', 'a.id as attr_id'])->alias('g')->where([
  91. 'g.id' => $goodsId,
  92. 'a.is_delete' => 0,
  93. 'g.is_delete' => 0,
  94. 'g.mall_id' => $mallId
  95. ])->with(["goodsWarehouse"])->leftJoin(['a' => GoodsAttr::tableName()], 'a.goods_id = g.id')->asArray()->one();
  96. if (!$goods) {
  97. throw new \Exception('数据异常,该条数据不存在');
  98. }
  99. $data['attr_list'] = empty($goods["attr_id"]) ? null : (new Goods())->signToAttr($goods['sign_id'], $goods['attr_groups']);
  100. $data['attr_id'] = $goods['attr_id'];
  101. $data["name"] = $goods['goodsWarehouse']['name'];
  102. $data["pic"] = $goods['goodsWarehouse']['cover_pic'];
  103. return $data;
  104. }
  105. /**
  106. * 获取配置
  107. * @param string $fields
  108. * @return ScratchSetting|null
  109. */
  110. public static function getSetting($mallId, $fields = "*")
  111. {
  112. if (self::$setting) {
  113. return self::$setting;
  114. }
  115. $setting = ScratchSettingModel::find()->where(['mall_id' => $mallId])->select($fields)->one();
  116. if ($setting) {
  117. if ($setting->payment_type) {
  118. $setting->payment_type = json_decode($setting->payment_type, true);
  119. } else {
  120. $setting->payment_type = ['online_pay'];
  121. }
  122. $setting['send_type'] = self::sendType($setting['send_type']);
  123. $setting["start_at"] = date("Y-m-d H:i:s", $setting["start_at"]);
  124. $setting["end_at"] = date("Y-m-d H:i:s", $setting["end_at"]);
  125. }
  126. self::$setting = $setting;
  127. return $setting;
  128. }
  129. /**
  130. * 兼容4.1.0之前的发货方式
  131. * @desc
  132. *
  133. * @param null $data
  134. *
  135. * @return array|int|mixed|string|string[]|null
  136. */
  137. public static function sendType($data = null)
  138. {
  139. if (!$data) {
  140. $data = ['express', 'offline'];
  141. } elseif (!is_array($data)) {
  142. $data = json_decode($data, true);
  143. if (!is_array($data)) {
  144. if (is_numeric($data)) {
  145. if ($data == 2) {
  146. $data = ['offline'];
  147. } elseif ($data == 1) {
  148. $data = ['express'];
  149. } else {
  150. $data = ['express', 'offline'];
  151. }
  152. } else {
  153. $data = ['express', 'offline'];
  154. }
  155. }
  156. }
  157. return $data;
  158. }
  159. /**
  160. * 获取中奖记录
  161. * @param $mallId
  162. * @param array $params
  163. * @return array
  164. * @throws \ErrorException
  165. */
  166. public static function getLotteryRecord($mallId, $params = [])
  167. {
  168. $fields = ["id", "scratch_id", "coupon_id", "user_id", "status", "type", "num", "detail", "price", "order_id", "goods_id", 'raffled_at', 'created_at'];
  169. $params = [
  170. 'with' => [
  171. 'user' => function($q){
  172. $q->select('id,nickname,avatar_url');
  173. },
  174. 'goods' => function($q){
  175. $q->select('id,goods_name,price');
  176. },
  177. 'coupon' => function($q){
  178. $q->select('id,name,sub_price');
  179. },
  180. ],
  181. 'where' => [
  182. ['=', 'mall_id', $mallId],
  183. ],
  184. 'select' => $fields,
  185. 'page' => $postData['page'] ?? 1,
  186. 'limit' => $postData['limit'] ?? 10,
  187. 'order' => $postData['order'] ?? 'id desc',
  188. ];
  189. if (!empty($postData['type'])) {
  190. $params['where'][] = ['=', 'type', $postData['type']];
  191. } else {
  192. $params['where'][] = ['in', 'type', [1, 2, 3, 4, 5]];
  193. }
  194. $pageData = ScratchLogModel::listPage($params, false);
  195. if ($pageData === false) throw new \ErrorException(ScratchModel::getStaticError());
  196. $typeList = ScratchModel::getTypeList($mallId);
  197. $returnData["list"] = [];
  198. foreach ($pageData['list'] as $item) {
  199. $data["name"] = $typeList[$item["type"]];
  200. if ($item["type"] == ScratchModel::TYPE_GOODS) {
  201. $data['name'] = $item['goods']['goods_name'] ?? $typeList[$item["type"]];
  202. $data['price'] = $item['goods']['price'] ?? 0;
  203. $item["num"] = 1;
  204. }
  205. if ($item['type'] == ScratchModel::TYPE_COUPON) {
  206. $data['name'] = $item['coupon']['name'] ?? $typeList[$item["type"]];
  207. $data['price'] = $item['coupon']['price'] ?? 0;
  208. }
  209. $data['type'] = $item['type'];
  210. $data["price"] = $item["price"];
  211. $data["num"] = $item["num"];
  212. $data["nickname"] = isset($item["user"]) ? $item["user"]["nickname"] : "普通用户";
  213. $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"]);
  214. $returnData["list"][] = $data;
  215. }
  216. $returnData["pagination"] = $pageData['pagination'];
  217. $returnData["page_count"] = $pageData['page_count'];
  218. return $returnData;
  219. }
  220. }