SpecialSource.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\merchant\model\special;
  12. use app\merchant\model\special\Special as SpecialModel;
  13. use traits\ModelTrait;
  14. use basic\ModelBasic;
  15. /**
  16. * Class SpecialSource 专题素材关联表
  17. * @package app\merchant\model\special
  18. */
  19. class SpecialSource extends ModelBasic
  20. {
  21. use ModelTrait;
  22. public static function setWhere($special_id)
  23. {
  24. $model = self::alias('s')->where(['s.special_id' => $special_id, 't.is_del' => 0, 't.is_show' => 1])
  25. ->join('SpecialTask t', 's.source_id=t.id')->field('t.title,t.image,t.is_del,t.is_show,s.*');
  26. return $model;
  27. }
  28. /**查看专题下的素材
  29. * @param int $special_id
  30. * @param int $page
  31. * @param int $limit
  32. */
  33. public static function getSpecialSourceList($special_id = 0, $page = 1, $limit = 20, $order = 0)
  34. {
  35. $data = self::setWhere($special_id)->page($page, $limit);
  36. if ($order) {
  37. $data = $data->order('s.sort asc,s.id asc')->select();
  38. } else {
  39. $data = $data->order('s.sort desc,s.id desc')->select();
  40. }
  41. $count = self::setWhere($special_id)->count();
  42. return compact('data', 'count');
  43. }
  44. public static function setSpecialWhere($special_id)
  45. {
  46. $model = self::alias('s')->where(['s.special_id' => $special_id, 'l.is_del' => 0, 'l.is_show' => 1, 'l.status' => 1])
  47. ->join('Special l', 's.source_id=l.id')->field('l.title,l.image,l.type,l.is_show,l.is_del,l.light_type,l.is_light,s.special_id,s.source_id,s.sort,s.pay_status,s.id');
  48. return $model;
  49. }
  50. /**查看专栏专题下的专题
  51. * @param int $special_id
  52. * @param int $page
  53. * @param int $limit
  54. */
  55. public static function getSpecialList($special_id = 0, $page = 1, $limit = 20, $order = 0)
  56. {
  57. $data = self::setSpecialWhere($special_id)->page($page, $limit);
  58. if ($order) {
  59. $data = $data->order('s.sort asc,s.id asc')->select();
  60. } else {
  61. $data = $data->order('s.sort desc,s.id desc')->select();
  62. }
  63. $count = self::setSpecialWhere($special_id)->count();
  64. return compact('data', 'count');
  65. }
  66. /**新增素材
  67. * @param $ids
  68. * @param int $special_id
  69. * @param int $special_type
  70. * @return bool
  71. */
  72. public static function addSpecialSource($ids, $special_id = 0, $special_type = 1)
  73. {
  74. if (!$ids || !$special_id) return false;
  75. try {
  76. $source_list_ids = explode(',', $ids);
  77. $inster['special_id'] = $special_id;
  78. $data = SpecialModel::where('id', $special_id)->field('pay_type,member_pay_type,validity')->find();
  79. if (!$data) return false;
  80. foreach ($source_list_ids as $sk => $id) {
  81. if ($special_type == SPECIAL_COLUMN) {
  82. $special = SpecialModel::where('id', $id)->field('pay_type,member_pay_type')->find();
  83. if ($data['pay_type'] == 1 && $data['member_pay_type'] == 0) {
  84. if ($special['pay_type'] == 1 && $special['member_pay_type'] == 1) {
  85. SpecialModel::where('id', $id)->update(['member_pay_type' => 0, 'member_money' => 0]);
  86. }
  87. $inster['pay_status'] = 1;
  88. } else if ($data['pay_type'] == 0) {
  89. if ($special['pay_type'] == 1) {
  90. SpecialModel::where('id', $id)->update(['member_pay_type' => 0, 'member_money' => 0, 'pay_type' => 0, 'money' => 0]);
  91. }
  92. $inster['pay_status'] = 0;
  93. }
  94. SpecialModel::where('id', $id)->update(['validity' => $data['validity']]);
  95. } else {
  96. $inster['pay_status'] = 1;
  97. }
  98. $inster['type'] = $special_type;
  99. $inster['source_id'] = $id;
  100. $inster['add_time'] = time();
  101. $res = self::set($inster);
  102. if ($res) {
  103. SpecialModel::where('id', $special_id)->setInc('quantity', 1);
  104. } else {
  105. continue;
  106. }
  107. }
  108. return true;
  109. } catch (\Exception $e) {
  110. return false;
  111. }
  112. }
  113. /**获取专题素材
  114. * @param bool $special_id
  115. * @return false|\PDOStatement|string|\think\Collection
  116. * @throws \think\db\exception\DataNotFoundException
  117. * @throws \think\db\exception\ModelNotFoundException
  118. * @throws \think\exception\DbException
  119. */
  120. public static function getSpecialSource($special_id = false, $source_id = false, $order = 0)
  121. {
  122. $where = array();
  123. $data = self::where($where);
  124. if ($special_id && is_numeric($special_id)) {
  125. $where['special_id'] = $special_id;
  126. $data->where($where);
  127. }
  128. if ($source_id) {
  129. if (!is_array($source_id)) {
  130. $where['source_id'] = $source_id;
  131. $data->where($where);
  132. } else {
  133. $data->whereIn('source_id', $source_id);
  134. }
  135. }
  136. if ($order) {
  137. $data->order('sort asc,id asc');
  138. } else {
  139. $data->order('sort desc,id desc');
  140. }
  141. return $data->select();
  142. }
  143. /**获取专题下素材数量
  144. * @param bool $special_id
  145. * @param bool $source_id
  146. */
  147. public static function getSpecialSourceCount($special_id = false, $source_id = false)
  148. {
  149. $where = array();
  150. $data = self::where($where);
  151. if ($special_id && is_numeric($special_id)) {
  152. $where['special_id'] = $special_id;
  153. $data->where($where);
  154. }
  155. if ($source_id) {
  156. if (!is_array($source_id)) {
  157. $where['source_id'] = $source_id;
  158. $data->where($where);
  159. } else {
  160. $data->whereIn('source_id', $source_id);
  161. }
  162. }
  163. return $data->count();
  164. }
  165. /**更新及添加专题素材
  166. * @param $source_list_ids 一维数组,素材id
  167. * @param int $special_id 专题id
  168. * @return bool
  169. */
  170. public static function saveSpecialSource($source_list_ids, $special_id = 0, $special_type = 1, $data = [])
  171. {
  172. if (!$special_id || !is_numeric($special_id)) {
  173. return false;
  174. }
  175. if (!$source_list_ids || !is_array($source_list_ids)) {
  176. return false;
  177. }
  178. try {
  179. $specialSourceAll = self::getSpecialSource($special_id);
  180. $specialSourceAll = count($specialSourceAll) > 0 ? $specialSourceAll->toArray() : [];
  181. if ($specialSourceAll) {
  182. self::where(['special_id' => $special_id])->delete();
  183. SpecialModel::where('id', $special_id)->update(['quantity' => 0]);
  184. }
  185. $inster['special_id'] = $special_id;
  186. foreach ($source_list_ids as $sk => $sv) {
  187. if ($special_type == SPECIAL_COLUMN) {
  188. $special = SpecialModel::where('id', $sv->id)->field('pay_type,member_pay_type')->find();
  189. if ($data['pay_type'] == 1 && $data['member_pay_type'] == 0) {
  190. if ($special['pay_type'] == 1 && $special['member_pay_type'] == 1) {
  191. SpecialModel::where('id', $sv->id)->update(['member_pay_type' => 0, 'member_money' => 0]);
  192. }
  193. $inster['pay_status'] = 1;
  194. } else if ($data['pay_type'] == 0) {
  195. if ($special['pay_type'] == 1) {
  196. SpecialModel::where('id', $sv->id)->update(['member_pay_type' => 0, 'member_money' => 0, 'pay_type' => 0, 'money' => 0]);
  197. }
  198. $inster['pay_status'] = 0;
  199. }
  200. $dat['validity'] = $data['validity'];
  201. $dat['is_mer_visible'] = $data['is_mer_visible'];
  202. SpecialModel::edit($dat, $sv->id, 'id');
  203. } else {
  204. $inster['pay_status'] = $sv->pay_status;
  205. }
  206. $inster['source_id'] = $sv->id;
  207. $inster['type'] = $special_type;
  208. $inster['sort'] = $sv->sort;
  209. $inster['add_time'] = time();
  210. $res = self::set($inster);
  211. if ($res) {
  212. SpecialModel::where('id', $special_id)->setInc('quantity', 1);
  213. } else {
  214. continue;
  215. }
  216. }
  217. return true;
  218. } catch (\Exception $e) {
  219. return false;
  220. }
  221. }
  222. /**清空专题素材
  223. * @param int $special_id 专题id
  224. */
  225. public static function delSpecialSource($special_id = 0)
  226. {
  227. if (!$special_id || !is_numeric($special_id)) {
  228. return false;
  229. }
  230. try {
  231. $specialSourceAll = self::getSpecialSource($special_id);
  232. $specialSourceAll = count($specialSourceAll) > 0 ? $specialSourceAll->toArray() : [];
  233. if ($specialSourceAll) {
  234. self::where(['special_id' => $special_id])->delete();
  235. SpecialModel::where('id', $special_id)->update(['quantity' => 0]);
  236. }
  237. return true;
  238. } catch (\Exception $e) {
  239. return false;
  240. }
  241. }
  242. }