RiceCard.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\store\model\card;
  3. use app\common\model\card\RiceCard as RiceCardModel;
  4. /**
  5. * 米卡模型
  6. * @package app\common\model
  7. */
  8. class RiceCard extends RiceCardModel
  9. {
  10. protected $append = ['total_stock', 'status_text'];
  11. public function setDkCatIdsAttr($value)
  12. {
  13. return !empty($value) ? implode(',', $value) : '';
  14. }
  15. public function getDkCatIdsAttr($value, $data)
  16. {
  17. return !empty($value) ? explode(',', $value) : [];
  18. }
  19. public function getList(array $param = [])
  20. {
  21. // 检索查询条件
  22. $query = $this->setQueryFilter($param);
  23. // 查询列表数据
  24. return $query->with(['image'])
  25. ->where("is_delete", '=', 0)
  26. ->order(["id" => 'desc'])
  27. ->paginate(15);
  28. }
  29. public function getListAll(array $param = [])
  30. {
  31. // 检索查询条件
  32. $query = $this->setQueryFilter($param);
  33. // 查询列表数据
  34. return $query->where("is_delete", '=', 0)
  35. ->where('status',1)
  36. ->where('type',$param['type']??3)
  37. ->field('id,name,type,stock,sale_num,status')
  38. ->order(["id" => 'desc'])
  39. ->select();
  40. }
  41. /**
  42. * 米卡简易列表
  43. * @return array
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public static function getSimpleList()
  49. {
  50. // 查询列表数据
  51. return self::where('type',2)->where("is_delete", '=', 0)
  52. ->order(["id" => 'desc'])
  53. ->field('id,name,stock,sale_num,status')->select()->toArray();
  54. }
  55. /**
  56. * 检索查询条件
  57. * @param array $param
  58. * @return \think\db\BaseQuery
  59. */
  60. private function setQueryFilter(array $param)
  61. {
  62. // 实例化查询对象
  63. $query = $this->getNewQuery();
  64. // 查询参数
  65. $params = $this->setQueryDefaultValue($param, [
  66. 'name' => '', // 米卡名称
  67. 'status' => -1, // 评价状态 -1全部 0-已下架 1-上架中
  68. ]);
  69. // 米卡名称
  70. !empty($params['name']) && $query->where('name', 'like', "%{$params['name']}%");
  71. // 米卡状态
  72. $params['status'] > -1 && $query->where("{$this->name}.status", '=', $params['status']);
  73. return $query;
  74. }
  75. /**
  76. * 获取详情
  77. * @param int $id
  78. * @return Comment|array|null
  79. */
  80. public function getDetail(int $id)
  81. {
  82. // 详情
  83. $detail = static::detail($id, ['image', 'riceCardGoods', 'riceCardGoodsExcept', 'riceCardPriceValues']);
  84. return $detail;
  85. }
  86. /**
  87. * 新增记录
  88. * @param array $data
  89. * @return bool
  90. */
  91. public function add(array $data)
  92. {
  93. $this->transaction(function () use ($data) {
  94. // 新增记录
  95. $this->save($data);
  96. // 新增关联数据
  97. if (!empty($data['rice_card_price_values'])) { // 米卡面额-现金卡
  98. (new RiceCardPriceValues())->add($this->id, $data['rice_card_price_values']);
  99. }
  100. // 米卡除外商品-现金卡
  101. if(isset($data['rice_card_goods_expect'])){
  102. $riceCardGoodsModel = new RiceCardGoods();
  103. $riceCardGoodsModel->add($this->id, RiceCardGoods::EXCEPT_YES, $data['rice_card_goods_expect']);
  104. }
  105. // 米卡兑换商品-兑换卡
  106. if(isset($data['rice_card_goods'])){
  107. $riceCardGoodsModel = new RiceCardGoods();
  108. $riceCardGoodsModel->add($this->id, RiceCardGoods::EXCEPT_NO, $data['rice_card_goods']);
  109. }
  110. });
  111. return true;
  112. }
  113. /**
  114. * 更新记录
  115. * @param array $data
  116. * @return bool
  117. */
  118. public function edit(array $data)
  119. {
  120. $this->transaction(function () use ($data) {
  121. // 更新记录
  122. $this->save($data);
  123. // 更新关联数据
  124. if (!empty($data['rice_card_price_values'])) { // 米卡面额-现金卡
  125. (new RiceCardPriceValues())->add($this->id, $data['rice_card_price_values']);
  126. }
  127. $riceCardGoodsModel = new RiceCardGoods();
  128. // 米卡除外商品-现金卡
  129. if(isset($data['rice_card_goods_expect'])){
  130. $riceCardGoodsModel->add($this->id, RiceCardGoods::EXCEPT_YES, $data['rice_card_goods_expect']);
  131. }
  132. if (isset($data['rice_card_goods'])) {
  133. //米卡可兑换商品-兑换卡 todo 下一期需求开发
  134. $riceCardGoodsModel->add($this->id, RiceCardGoods::EXCEPT_NO, $data['rice_card_goods']);
  135. }
  136. });
  137. return true;
  138. }
  139. /**
  140. * @param array $ids
  141. * @param bool $state
  142. * @return bool
  143. */
  144. public function setStatus(array $ids, bool $state)
  145. {
  146. // 批量更新记录
  147. return static::updateBase(['status' => $state ? 1 : 0], [['id', 'in', $ids]]);
  148. }
  149. }