ScratchLogModel.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace addons\Scratch\common\models;
  3. use addons\Coupon\common\models\AddonsCoupon;
  4. use addons\Scratch\common\enums\ScratchEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use common\models\goods\Goods;
  7. use common\models\mall\MallAddons;
  8. use common\models\user\User;
  9. /**
  10. * Class ScratchLogModel
  11. * @property int $id
  12. * @property int $mall_id
  13. * @property int $scratch_id
  14. * @property int $user_id
  15. * @property int $status 0预领取 1 未领取 2 已领取
  16. * @property int $type 1.红包2.余额3.优惠卷4.积分5.实物
  17. * @property int $num 积分数量
  18. * @property string $detail 详情
  19. * @property string $coupon_id 优惠券id
  20. * @property string $goods_id 赠品
  21. * @property string $attr_id 赠品
  22. * @property string $price 价格
  23. * @property int $order_id
  24. * @property int $raffled_at
  25. * @property int $created_at
  26. * @property Goods $goods
  27. * @package addons\Scratch\common\models
  28. */
  29. class ScratchLogModel extends BaseActiveRecord
  30. {
  31. const STATUS_PRE = 0;
  32. const STATUS_NO_RECEIVED = 1;
  33. const STATUS_RECEIVED = 2;
  34. const LOG_STATUS_ARR = ['预领取', '未领取', '已领取'];
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public static function tableName()
  39. {
  40. return '{{%addons_scratch_log}}';
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function rules()
  46. {
  47. return [
  48. [['mall_id', 'scratch_id', 'user_id'], 'required'],
  49. [['updated_at', 'mall_id', 'scratch_id', 'user_id', 'status', 'type', 'num', 'order_id', 'attr_id', 'goods_id', 'coupon_id'], 'integer'],
  50. [['price'], 'number'],
  51. [['raffled_at', 'created_at'], 'safe'],
  52. [['detail'], 'string'],
  53. ];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'mall_id' => 'Mall ID',
  63. 'scratch_id' => 'Scratch ID',
  64. 'user_id' => 'User ID',
  65. 'status' => ' 0预领取 1 未领取 2 已领取',
  66. 'type' => '1.红包2.优惠券3.积分4.实物',
  67. 'num' => '积分数量',
  68. 'detail' => '详情',
  69. 'coupon_id' => '优惠券id',
  70. 'goods_id' => '赠品',
  71. 'attr_id' => '赠品规格',
  72. 'price' => '价格',
  73. 'order_id' => 'Order ID',
  74. 'raffled_at' => 'Raffled At',
  75. 'created_at' => 'Created At',
  76. ];
  77. }
  78. /**
  79. * @return \yii\db\ActiveQuery
  80. */
  81. public function getUser()
  82. {
  83. return $this->hasOne(User::className(), ['id' => 'user_id'])->alias('u');
  84. }
  85. /**
  86. * @return \yii\db\ActiveQuery
  87. */
  88. public function getGoods()
  89. {
  90. return $this->hasOne(Goods::className(), ['id' => 'goods_id']);
  91. }
  92. /**
  93. * @return \yii\db\ActiveQuery
  94. */
  95. public function getCoupon()
  96. {
  97. return $this->hasOne(ScratchCouponModel::className(), ['id' => 'coupon_id']);
  98. }
  99. /**
  100. * @return \yii\db\ActiveQuery
  101. */
  102. public function getScratch()
  103. {
  104. return $this->hasOne(ScratchModel::className(), ['id' => 'scratch_id']);
  105. }
  106. /**
  107. * 获取数据
  108. * @param $params
  109. * @param $fields 字段
  110. * @return \app\models\BaseActiveQuery|array|\yii\db\ActiveRecord|\yii\db\ActiveRecord[]|null
  111. */
  112. public static function getData($params,$fields = []){
  113. $query = self::find()->where(["is_delete" => self::NO]);
  114. if(isset($params["id"]) && !empty($params["id"])){
  115. $params["is_one"] = 1;
  116. $query->andWhere(["id" => $params["id"]]);
  117. }
  118. if(isset($params["mall_id"]) && !empty($params["mall_id"])){
  119. $query->andWhere(["mall_id" => $params["mall_id"]]);
  120. }
  121. if(isset($params["user_id"]) && !empty($params["user_id"])){
  122. $query->andWhere(["user_id" => $params["user_id"]]);
  123. }
  124. if(isset($params["not_user_id"]) && !empty($params["not_user_id"])){
  125. $query->andWhere(['!=',"user_id" , $params["not_user_id"]]);
  126. }
  127. if(isset($params["status"]) && !empty($params["status"])){
  128. $query->andWhere(["status" => $params["status"]]);
  129. }
  130. if(isset($params["type"]) && !empty($params["type"])){
  131. $query->andWhere(["type" => $params["type"]]);
  132. }
  133. if(isset($params["filter_time_start"]) && isset($params["filter_time_end"]) && !empty($params["filter_time_start"]) && !empty($params["filter_time_end"])){
  134. $query->andFilterWhere(['between','created_at',$params["filter_time_start"], $params["filter_time_end"]]);
  135. }
  136. //排序
  137. $orderByColumn = isset($params["sort_key"]) ? $params["sort_key"] : "id";
  138. $orderByType = isset($params["sort_val"]) ? $params["sort_val"] : " desc";
  139. $orderBy = $orderByColumn.$orderByType;
  140. if(!empty($fields)){
  141. $query->select($fields);
  142. }
  143. if(isset($params["return_count"])){
  144. return $query->count();
  145. }
  146. $pagination = null;
  147. if(isset($params["limit"]) && isset($params["page"])){
  148. $query->page($pagination, $params['limit'], $params['page']);
  149. }
  150. $query->with(["user"])->asArray()->orderBy($orderBy);
  151. //查询单条数据
  152. if(isset($params["is_one"]) && $params["is_one"] == 1){
  153. $list = $query->one();
  154. $returnData = $list;
  155. }else{
  156. $list = $query->all();
  157. if(isset($params["limit"]) && isset($params["page"])) {
  158. $returnData["list"] = $list;
  159. $returnData["pagination"] = $pagination;
  160. }else{
  161. $returnData = $list;
  162. }
  163. }
  164. return $returnData;
  165. }
  166. /**
  167. * 操作数据库
  168. * @param $data
  169. * @return bool
  170. */
  171. public static function operateData($data){
  172. $scratchModel = new ScratchLog();
  173. if(isset($data["id"]) && !empty($data["id"])){
  174. $scratchModel = self::findOne($data["id"]);
  175. if(empty($scratchModel)){
  176. return false;
  177. }
  178. if($scratchModel->is_delete == self::YES){
  179. return false;
  180. }
  181. $data["user_id"] = $scratchModel->user_id;
  182. $data["mall_id"] = $scratchModel->mall_id;
  183. }
  184. $scratchModel->user_id = $data["user_id"];
  185. $scratchModel->mall_id = $data["mall_id"];
  186. if(isset($data["status"])){
  187. $scratchModel->status = $data["mall_id"];
  188. }
  189. if(isset($data["type"])){
  190. $scratchModel->type = $data["type"];
  191. }
  192. if(isset($data["num"])){
  193. $scratchModel->num = $data["num"];
  194. }
  195. if(isset($data["detail"])){
  196. $scratchModel->detail = $data["detail"];
  197. }
  198. if(isset($data["price"])){
  199. $scratchModel->price = $data["price"];
  200. }
  201. if(isset($data["order_id"])){
  202. $scratchModel->order_id = $data["order_id"];
  203. }
  204. if(isset($data["goods_id"])){
  205. $scratchModel->goods_id = $data["goods_id"];
  206. }
  207. if(isset($data["raffled_at"])){
  208. $scratchModel->raffled_at = $data["raffled_at"];
  209. }
  210. if(isset($data["is_delete"])){
  211. $scratchModel->is_delete = $data["is_delete"];
  212. }
  213. if(!$scratchModel->save()){
  214. return false;
  215. }
  216. return $scratchModel->id;
  217. }
  218. /**
  219. * @param $mallId
  220. * @param $params
  221. * @return ScratchLogModel
  222. * @throws \Exception
  223. */
  224. public static function setData($mallId, $params)
  225. {
  226. try {
  227. $id = $params['id'] ?? 0;
  228. $data = self::findOne([
  229. 'id' => $id,
  230. 'status' => ScratchEnum::ENABLED,
  231. ]);
  232. if (!empty($data)) {
  233. return $data;
  234. }
  235. $model = new self();
  236. $model->mall_id = $mallId;
  237. $model->name = $params['name'] ?? '';
  238. $model->pic = $params['pic'] ?? '';
  239. $model->type = $params['type'] ?? '';
  240. $model->goods_id = $params['goods_id'] ?? 0;
  241. $model->num = $params['num'] ?? 0;
  242. $model->price = $params['price'] ?? 0;
  243. $model->coupon_id = $params['coupon_id'] ?? 0;
  244. $model->stock = $params['stock'] ?? 0;
  245. $model->balance = $params['balance'] ?? 0;
  246. $model->status = $params['status'] ?? 1;
  247. return $model;
  248. } catch (\Exception $exception) {
  249. throw new \Exception($exception->getMessage());
  250. }
  251. }
  252. }