SystemClearData.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\v1\system;
  12. use app\services\other\HotDataServices;
  13. use app\services\product\product\StoreDescriptionServices;
  14. use app\services\product\product\StoreProductCouponServices;
  15. use app\services\product\product\StoreProductRelationServices;
  16. use app\services\user\UserRelationServices;
  17. use app\services\product\product\StoreProductReplyServices;
  18. use app\services\product\sku\StoreProductAttrResultServices;
  19. use app\services\product\sku\StoreProductAttrServices;
  20. use app\services\product\sku\StoreProductAttrValueServices;
  21. use crmeb\services\CacheService;
  22. use think\exception\ValidateException;
  23. use think\facade\App;
  24. use app\controller\admin\AuthController;
  25. use app\services\system\SystemClearServices;
  26. use app\services\product\product\StoreProductServices;
  27. use app\services\system\attachment\SystemAttachmentServices;
  28. /**
  29. * 清除默认数据理控制器
  30. * Class SystemClearData
  31. * @package app\controller\admin\v1\system
  32. */
  33. class SystemClearData extends AuthController
  34. {
  35. /**
  36. * 构造方法
  37. * SystemClearData constructor.
  38. * @param App $app
  39. * @param SystemClearServices $services
  40. */
  41. public function __construct(App $app, SystemClearServices $services)
  42. {
  43. parent::__construct($app);
  44. $this->services = $services;
  45. //生产模式下不允许清除数据
  46. if (!env('APP_DEBUG', false)) {
  47. throw new ValidateException('生产模式下,禁止操作');
  48. }
  49. }
  50. /**
  51. * 清除方法入口
  52. * @param $type
  53. * @return mixed
  54. */
  55. public function index($type)
  56. {
  57. switch ($type) {
  58. case 'temp':
  59. return $this->userTemp();
  60. break;
  61. case 'recycle':
  62. return $this->recycleProduct();
  63. break;
  64. case 'store':
  65. return $this->storeData();
  66. break;
  67. case 'order':
  68. return $this->orderData();
  69. break;
  70. case 'kefu':
  71. return $this->kefuData();
  72. break;
  73. case 'wechat':
  74. return $this->wechatData();
  75. break;
  76. case 'attachment':
  77. return $this->attachmentData();
  78. break;
  79. case 'article':
  80. return $this->articledata();
  81. break;
  82. case 'system':
  83. return $this->systemdata();
  84. break;
  85. case 'user':
  86. return $this->userRelevantData();
  87. break;
  88. default:
  89. return $this->fail('参数有误');
  90. }
  91. }
  92. /**
  93. * 清除用户生成的临时文件
  94. * @return mixed
  95. */
  96. public function userTemp()
  97. {
  98. /** @var SystemAttachmentServices $services */
  99. $services = app()->make(SystemAttachmentServices::class);
  100. $ids = implode(',', $services->getColumn(['module_type' => 2], 'att_id'));
  101. $services->del($ids);
  102. $services->delete(2, 'module_type');
  103. return $this->success('清除数据成功!');
  104. }
  105. /**
  106. * 清除回收站商品
  107. * @return mixed
  108. */
  109. public function recycleProduct()
  110. {
  111. /** @var StoreProductServices $product */
  112. $product = app()->make(StoreProductServices::class);
  113. $ids = $product->getColumn(['is_del' => 1], 'id');
  114. //清除规格表数据
  115. /** @var StoreProductAttrServices $ProductAttr */
  116. $productAttr = app()->make(StoreProductAttrServices::class);
  117. $productAttr->delete([['product_id', 'in', $ids], ['type', '=', '0']]);
  118. /** @var StoreProductAttrResultServices $productAttrResult */
  119. $productAttrResult = app()->make(StoreProductAttrResultServices::class);
  120. $productAttrResult->delete([['product_id', 'in', $ids], ['type', '=', '0']]);
  121. /** @var StoreProductAttrValueServices $productAttrValue */
  122. $productAttrValue = app()->make(StoreProductAttrValueServices::class);
  123. $productAttrValue->delete([['product_id', 'in', $ids], ['type', '=', '0']]);
  124. //删除商品详情
  125. /** @var StoreDescriptionServices $productDescription */
  126. $productDescription = app()->make(StoreDescriptionServices::class);
  127. $productDescription->delete([['product_id', 'in', $ids], ['type', '=', '0']]);
  128. //删除商品关联数据
  129. /** @var StoreProductRelationServices $productRelation */
  130. $productRelation = app()->make(StoreProductRelationServices::class);
  131. $productRelation->delete([['product_id', 'in', $ids]]);
  132. //删除商品关联优惠券数据
  133. /** @var StoreProductCouponServices $productCoupon */
  134. $productCoupon = app()->make(StoreProductCouponServices::class);
  135. $productCoupon->delete([['product_id', 'in', $ids]]);
  136. //删除商品收藏记录
  137. /** @var UserRelationServices $productRelation */
  138. $productRelation = app()->make(UserRelationServices::class);
  139. $productRelation->delete([['relation_id', 'in', $ids], ['category', '=', UserRelationServices::CATEGORY_PRODUCT]]);
  140. //删除商品的评论
  141. /** @var StoreProductReplyServices $productReply */
  142. $productReply = app()->make(StoreProductReplyServices::class);
  143. $productReply->delete([['product_id', 'in', $ids]]);
  144. $product->delete(1, 'is_del');
  145. return $this->success('清除数据成功!');
  146. }
  147. /**
  148. * 清除用户数据
  149. * @return mixed
  150. */
  151. public function userRelevantData()
  152. {
  153. $this->services->clearData([
  154. 'wechat_user', 'wechat_message', 'user_search', 'user_visit', 'user_sign', 'user_recharge', 'user_notice_see', 'user_notice',
  155. 'user_level', 'user_label_relation', 'user_label', 'user_invoice', 'user_group', 'user_friends', 'user_extract', 'user_enter',
  156. 'user_brokerage_frozen', 'user_bill', 'user_address', 'user', 'system_store_staff', 'store_visit', 'store_service_record',
  157. 'store_service_speechcraft', 'store_service_record', 'store_service_log', 'store_service_feedback', 'store_service',
  158. 'store_product_reply', 'user_relation', 'store_product_log', 'store_pink', 'store_order_status', 'store_order_invoice',
  159. 'store_order_economize', 'store_order_cart_info', 'store_order', 'store_coupon_user', 'store_coupon_issue_user', 'store_cart',
  160. 'store_bargain_user_help', 'store_bargain_user', 'sms_record', 'qrcode', 'other_order_status', 'other_order', 'member_card',
  161. 'member_card_batch', 'delivery_service', 'queue_auxiliary', 'queue_list', 'luck_lottery_record', 'agent_level_task_record',
  162. 'user_brokerage', 'user_money', 'system_message', 'award_lake', 'user_integral'
  163. ], true);
  164. $this->services->delDirAndFile('./public/uploads/store/comment');
  165. return $this->success('清除数据成功!');
  166. }
  167. /**
  168. * 清除商城数据
  169. * @return mixed
  170. */
  171. public function storeData()
  172. {
  173. $this->services->clearData([
  174. 'store_product_category', 'live_anchor', 'live_goods', 'live_room', 'live_room_goods', 'store_bargain', 'store_bargain_user',
  175. 'store_bargain_user_help', 'store_cart', 'store_product_category', 'store_combination', 'store_coupon_issue', 'store_coupon_issue_user',
  176. 'store_coupon_product', 'store_coupon_user', 'store_product', 'store_product_attr', 'store_product_attr_result',
  177. 'store_product_attr_value', 'store_product_cate', 'store_product_coupon', 'store_product_description', 'store_product_log',
  178. 'user_relation', 'store_product_reply', 'store_product_rule', 'store_seckill', 'store_visit', 'store_integral'
  179. ], true);
  180. return $this->success('清除数据成功!');
  181. }
  182. /**
  183. * 清除订单数据
  184. * @return mixed
  185. */
  186. public function orderData()
  187. {
  188. $this->services->clearData([
  189. 'queue_list', 'queue_auxiliary', 'other_order_status', 'store_order', 'store_order_cart_info', 'store_order_economize', 'store_order_invoice', 'store_order_refund',
  190. 'store_order_status', 'store_pink', 'other_order', 'store_integral_order', 'store_integral_order_status'
  191. ], true);
  192. return $this->success('清除数据成功!');
  193. }
  194. /**
  195. * 清除客服数据
  196. * @return mixed
  197. */
  198. public function kefuData()
  199. {
  200. $this->services->clearData([
  201. 'queue_auxiliary', 'store_service', 'store_service_feedback', 'store_service_log', 'store_service_record', 'store_service_speechcraft'
  202. ], true);
  203. $this->services->delDirAndFile('./public/uploads/store/service');
  204. return $this->success('清除数据成功!');
  205. }
  206. /**
  207. * 清除微信管理数据
  208. * @return mixed
  209. */
  210. public function wechatData()
  211. {
  212. $this->services->clearData([
  213. 'wechat_key', 'wechat_media', 'wechat_message', 'wechat_news_category', 'wechat_reply'
  214. ], true);
  215. $this->services->delDirAndFile('./public/uploads/wechat');
  216. return $this->success('清除数据成功!');
  217. }
  218. /**
  219. * 清除所有附件
  220. * @return mixed
  221. */
  222. public function attachmentData()
  223. {
  224. $this->services->clearData([
  225. 'system_attachment', 'system_attachment_category'
  226. ], true);
  227. $this->services->delDirAndFile('./public/uploads/');
  228. return $this->success('清除上传文件成功!');
  229. }
  230. /**
  231. * 清楚内容数据
  232. * @return mixed
  233. */
  234. public function articledata()
  235. {
  236. $this->services->clearData([
  237. 'article_category', 'article', 'article_content'
  238. ], true);
  239. return $this->success('清除数据成功!');
  240. }
  241. /**
  242. * 清楚系统记录
  243. * @return mixed
  244. */
  245. public function systemdata()
  246. {
  247. $this->services->clearData([
  248. 'system_notice_admin', 'system_log'
  249. ], true);
  250. return $this->success('清除数据成功!');
  251. }
  252. /**
  253. * 替换域名方法
  254. * @return mixed
  255. */
  256. public function replaceSiteUrl()
  257. {
  258. [$url] = $this->request->postMore([
  259. ['url', '']
  260. ], true);
  261. if (!$url)
  262. return $this->fail('请输入需要更换的域名');
  263. if (!verify_domain($url))
  264. return $this->fail('域名不合法');
  265. $this->services->replaceSiteUrl($url);
  266. return $this->success('替换成功!');
  267. }
  268. /**
  269. * 预热营销商品库存
  270. * @param HotDataServices $hotDataServices
  271. * @return mixed
  272. */
  273. public function hotProductStock(HotDataServices $hotDataServices)
  274. {
  275. $key = 'hot_product_stock';
  276. $re = CacheService::get($key);
  277. if (!$re) {
  278. $f = [1, 2, 3, 4, 5];
  279. try {
  280. foreach ($f as $type) {
  281. $hotDataServices->hot($type);
  282. }
  283. } catch (\Throwable $e) {
  284. }
  285. CacheService::set($key, time(), 86400);
  286. }
  287. return $this->success('预热成功!');
  288. }
  289. }