SpecialSource.php 9.9 KB

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