InitializeLogic.php 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. <?php
  2. /**
  3. * 初始化逻辑类
  4. *
  5. * @author qifan
  6. * @date 2020-01-05
  7. */
  8. namespace app\admin\logic;
  9. use think\Db;
  10. use think\Exception;
  11. class InitializeLogic
  12. {
  13. public $log = '操作成功!';
  14. # 值为false时,终止其他方法的操作
  15. private $status = true;
  16. /**
  17. * 重置数据
  18. *
  19. * @param $param
  20. * @return bool
  21. */
  22. public function update($param)
  23. {
  24. # 设置脚本执行时间和内存
  25. ini_set('max_execution_time', 0);
  26. ini_set('memory_limit', '256M');
  27. # 重置单个或多个模块
  28. foreach ($param AS $key => $value) {
  29. if ($value == 'crm' && $this->status) $this->resetCustomerManagementData(); # 重置客户管理数据
  30. if ($value == 'taskExamine' && $this->status) $this->resetTaskExamineData(); # 重置任务/审批数据
  31. if ($value == 'log' && $this->status) $this->resetDailyRecordData(); # 重置日志数据
  32. if ($value == 'project' && $this->status) $this->resetProjectManagementData(); # 重置项目管理数据
  33. if ($value == 'calendar' && $this->status) $this->resetCalendarData(); # 重置日历数据
  34. }
  35. return true;
  36. }
  37. /**
  38. * 验证密码
  39. *
  40. * @param $userId
  41. * @param $password
  42. * @return bool
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. * @throws \think\exception\DbException
  46. */
  47. public function verification($userId, $password)
  48. {
  49. $userInfo = Db::name('admin_user')->field(['password', 'salt'])->where('id', $userId)->find();
  50. return user_md5($password, $userInfo['salt']) == $userInfo['password'];
  51. }
  52. /**
  53. * 重置客户管理数据
  54. */
  55. private function resetCustomerManagementData()
  56. {
  57. # 表前缀
  58. $prefix = config('database.prefix');
  59. # 文件数组
  60. $files = [];
  61. # 启动事务
  62. Db::startTrans();
  63. try {
  64. # ------ 重置产品数据START ------ #
  65. # 获取产品附件ID
  66. $productFileIds = Db::name('crm_product_file')->column('file_id');
  67. # 查询产品文件数据
  68. $productFileInfo = $this->getFileList($productFileIds);
  69. # 合并附件数据
  70. $files = array_merge($files, $productFileInfo);
  71. # 删除产品分类表
  72. Db::name('crm_product_category')->where(['category_id' => ['neq', 1]])->delete();
  73. # 重置产品分类自增ID
  74. Db::query("ALTER TABLE ".$prefix."crm_product_category AUTO_INCREMENT = 1");
  75. # 清除产品附件关联数据并重置自增ID
  76. Db::query("TRUNCATE TABLE ".$prefix."crm_product_file");
  77. # 删除产品附件
  78. Db::name('admin_file')->whereIn('file_id', $productFileIds)->delete();
  79. # 清除产品表数据并重置自增ID
  80. Db::query("TRUNCATE TABLE ".$prefix."crm_product");
  81. # ------ 重置产品数据 END ------ #
  82. # ------ 重置回访数据 START ------ #
  83. # 获取回访附件ID
  84. $visitFileIds = Db::name('crm_visit_file')->column('file_id');
  85. # 查询回访文件数据
  86. $visitFileInfo = $this->getFileList($visitFileIds);
  87. # 合并附件数据
  88. $files = array_merge($files, $visitFileInfo);
  89. # 清除回访附件关联数据并重置自增ID
  90. Db::query("TRUNCATE TABLE ".$prefix."crm_visit_file");
  91. # 删除回访附件
  92. Db::name('admin_file')->whereIn('file_id', $visitFileIds)->delete();
  93. # 清除回访表数据并重置自增ID
  94. Db::query("TRUNCATE TABLE ".$prefix."crm_visit");
  95. # ------ 重置回访数据 END ------ #
  96. # ------ 重置发票数据 START ------ #
  97. # 获取回访附件ID
  98. $invoiceFileIds = Db::name('crm_invoice_file')->column('file_id');
  99. # 查询回访文件数据
  100. $invoiceFileInfo = $this->getFileList($invoiceFileIds);
  101. # 合并附件数据
  102. $files = array_merge($files, $invoiceFileInfo);
  103. # 清除发票附件关联数据并重置自增ID
  104. Db::query("TRUNCATE TABLE ".$prefix."crm_invoice_file");
  105. # 删除发票附件
  106. Db::name('admin_file')->whereIn('file_id', $invoiceFileIds)->delete();
  107. # 清除发票开户行信息数据并重置自增ID
  108. Db::query("TRUNCATE TABLE ".$prefix."crm_invoice_info");
  109. # 清除发票数据并重置自增ID
  110. Db::query("TRUNCATE TABLE ".$prefix."crm_invoice");
  111. # ------ 重置发票数据 END ------ #
  112. # ------ 重置回款数据 START ------ #
  113. # 获取回款附件ID
  114. $receivablesFileIds = Db::name('crm_receivables_file')->column('file_id');
  115. # 查询回访文件数据
  116. $receivablesFileInfo = $this->getFileList($receivablesFileIds);
  117. # 合并附件数据
  118. $files = array_merge($files, $receivablesFileInfo);
  119. # 清除回款附件关联数据并重置自增ID
  120. Db::query("TRUNCATE TABLE ".$prefix."crm_receivables_file");
  121. # 删除回款附件
  122. Db::name('admin_file')->whereIn('file_id', $receivablesFileIds)->delete();
  123. # 清除回款计划数据并重置自增ID
  124. Db::query("TRUNCATE TABLE ".$prefix."crm_receivables_plan");
  125. # 清除回款数据并重置自增ID
  126. Db::query("TRUNCATE TABLE ".$prefix."crm_receivables");
  127. # ------ 重置回款数据 END ------ #
  128. # ------ 重置合同数据 START ------ #
  129. # 获取合同附件ID
  130. $contractFileIds = Db::name('crm_contract_file')->column('file_id');
  131. # 查询合同文件数据
  132. $contractFileInfo = $this->getFileList($contractFileIds);
  133. # 合并附件数据
  134. $files = array_merge($files, $contractFileInfo);
  135. # 清除合同附件关联数据并重置自增ID
  136. Db::query("TRUNCATE TABLE ".$prefix."crm_contract_file");
  137. # 删除合同附件
  138. Db::name('admin_file')->whereIn('file_id', $contractFileIds)->delete();
  139. # 删除合同产品关联数据并重置自增ID
  140. Db::query("TRUNCATE TABLE ".$prefix."crm_contract_product");
  141. # 删除合同数据并重置自增ID
  142. Db::query("TRUNCATE TABLE ".$prefix."crm_contract");
  143. # ------ 重置合同数据 END ------ #
  144. # ------ 重置商机数据 START ------ #
  145. # 获取商机附件ID
  146. $businessFileIds = Db::name('crm_business_file')->column('file_id');
  147. # 查询商机文件数据
  148. $businessFileInfo = $this->getFileList($businessFileIds);
  149. # 合并附件数据
  150. $files = array_merge($files, $businessFileInfo);
  151. # 清除商机附件关联数据并重置自增ID
  152. Db::query("TRUNCATE TABLE ".$prefix."crm_business_file");
  153. # 删除商机附件
  154. Db::name('admin_file')->whereIn('file_id', $businessFileIds)->delete();
  155. # 清除商机日志数据并重置自增ID
  156. Db::query("TRUNCATE TABLE ".$prefix."crm_business_log");
  157. # 清除商机产品关系数据并重置自增ID
  158. Db::query("TRUNCATE TABLE ".$prefix."crm_business_product");
  159. # 删除商机状态组类别数据
  160. Db::name('crm_business_type')->where(['type_id' => ['neq', 1]])->delete();
  161. # 重置商机状态组类别自增ID
  162. Db::query("ALTER TABLE ".$prefix."crm_business_type AUTO_INCREMENT = 1");
  163. # 删除商机状态数据
  164. Db::name('crm_business_status')->where(['type_id' => ['gt', 1]])->delete();
  165. # 重置商机状态自增ID
  166. Db::query("ALTER TABLE ".$prefix."crm_business_status AUTO_INCREMENT = 1");
  167. # 删除商机数据并重置自增ID
  168. Db::query("TRUNCATE TABLE ".$prefix."crm_business");
  169. # ------ 重置商机数据 END ------ #
  170. # ------ 重置联系人数据 START ------ #
  171. # 获取联系人附件ID
  172. $contactsFileIds = Db::name('crm_contacts_file')->column('file_id');
  173. # 查询联系人文件数据
  174. $contactsFileInfo = $this->getFileList($contactsFileIds);
  175. # 合并附件数据
  176. $files = array_merge($files, $contactsFileInfo);
  177. # 清除联系人附件关联数据并重置自增ID
  178. Db::query("TRUNCATE TABLE ".$prefix."crm_contacts_file");
  179. # 删除联系人附件
  180. Db::name('admin_file')->whereIn('file_id', $contactsFileIds)->delete();
  181. # 清除联系人商机关联数据并重置自增ID
  182. Db::query("TRUNCATE TABLE ".$prefix."crm_contacts_business");
  183. # 清除联系人数据并重置自增ID
  184. Db::query("TRUNCATE TABLE ".$prefix."crm_contacts");
  185. # ------ 重置联系人数据 END ------ #
  186. # ------ 重置客户数据 START ------ #
  187. # 获取客户附件ID
  188. $customerFileIds = Db::name('crm_customer_file')->column('file_id');
  189. # 查询联系人文件数据
  190. $customerFileInfo = $this->getFileList($customerFileIds);
  191. # 合并附件数据
  192. $files = array_merge($files, $customerFileInfo);
  193. # 清除客户附件关联数据并重置自增ID
  194. Db::query("TRUNCATE TABLE ".$prefix."crm_customer_file");
  195. # 删除客户附件
  196. Db::name('admin_file')->whereIn('file_id', $customerFileIds)->delete();
  197. # 清除客户数据并重置自增ID
  198. Db::query("TRUNCATE TABLE ".$prefix."crm_customer");
  199. # ------ 重置客户数据 END ------ #
  200. # ------ 重置线索数据 START ------ #
  201. # 获取线索附件ID
  202. $leadsFileIds = Db::name('crm_leads_file')->column('file_id');
  203. # 查询线索文件数据
  204. $leadsFileInfo = $this->getFileList($leadsFileIds);
  205. # 合并附件数据
  206. $files = array_merge($files, $leadsFileInfo);
  207. # 清除线索附件关联数据并重置自增ID
  208. Db::query("TRUNCATE TABLE ".$prefix."crm_leads_file");
  209. # 删除线索附件
  210. Db::name('admin_file')->whereIn('file_id', $leadsFileIds)->delete();
  211. # 清除线索数据并重置自增ID
  212. Db::query("TRUNCATE TABLE ".$prefix."crm_leads");
  213. # ------ 重置线索数据 END ------ #
  214. # ------ 重置附件表自增ID START ------ #
  215. Db::query("ALTER TABLE ".$prefix."admin_file AUTO_INCREMENT = 1");
  216. # ------ 重置附件表自增ID END ------ #
  217. # ------ 清除活动记录中关于客户管理模块的数据 START ------ #
  218. # 获取活动附件ID
  219. $activityFileIds = Db::name('crm_activity_file')->column('file_id');
  220. # 查询活动附件数据
  221. $activityFileInfo = $this->getFileList($activityFileIds);
  222. # 合并附件数据
  223. $files = array_merge($files, $activityFileInfo);
  224. # 清除活动关联附件数据并重置自增ID
  225. Db::query("TRUNCATE TABLE ".$prefix."crm_activity_file");
  226. # 清除活动数据并重置自增ID
  227. Db::query("TRUNCATE TABLE ".$prefix."crm_activity");
  228. # ------ 清除活动记录中关于客户管理模块的数据 END ------ #
  229. # ------ 清除我的关注数据 START ------ #
  230. Db::query("TRUNCATE TABLE ".$prefix."crm_star");
  231. # ------ 清除我的关注数据 END ------ #
  232. # ------ 清除数据操作日志数据 START ------ #
  233. Db::name('admin_operation_log')->where(['module' => ['like', 'crm_%']])->delete();
  234. # ------ 清除数据操作日志数据 END ------ #
  235. # ------ 清除模板打印记录数据 START ------ #
  236. Db::query("TRUNCATE TABLE ".$prefix."crm_customer_config");
  237. # ------ 清除模板打印记录数据 END ------ #
  238. # ------ 清除模板打印记录数据 START ------ #
  239. Db::query("TRUNCATE TABLE ".$prefix."crm_printing_record");
  240. # ------ 清除模板打印记录数据 END ------ #
  241. # ------ 清除导入数据记录表 START ------ #
  242. Db::name('admin_import_record')->where(['type' => ['like', 'crm_%']])->delete();
  243. # ------ 清除导入数据记录表 END ------ #
  244. # ------ 清除字段操作记录表 START ------ #
  245. Db::name('admin_action_record')->where(['types' => ['like', 'crm_%']])->delete();
  246. # ------ 清除字段操作记录表 END ------ #
  247. # ------ 清除数据操作记录表 START ------ #
  248. Db::name('admin_action_log')->where('module_name', 'crm')->delete();
  249. # ------ 清除数据操作记录表 END ------ #
  250. # ------ 清除有关客户模块的审批记录 START ------ #
  251. Db::name('admin_examine_record')->where(['types' => ['like', 'crm_%']])->delete();
  252. # ------ 清除有关客户模块的审批记录 END ------ #
  253. # ------ 清除跟客户模块有关的管理数据表 START ------ #
  254. # 清除项目关联客户模块表并重置字段ID
  255. Db::query("TRUNCATE TABLE ".$prefix."work_relation");
  256. # 清除任务关联客户模块表并重置字段ID
  257. Db::query("TRUNCATE TABLE ".$prefix."task_relation");
  258. # 清除日志关联客户模块表并重置字段ID
  259. Db::query("TRUNCATE TABLE ".$prefix."oa_log_relation");
  260. # 清除审批关联客户模块表并重置字段ID
  261. Db::query("TRUNCATE TABLE ".$prefix."oa_examine_relation");
  262. # 清除日程关联客户模块表并重置字段ID
  263. Db::query("TRUNCATE TABLE ".$prefix."oa_event_relation");
  264. # ------ 清除跟客户模块有关的管理数据表 END ------ #
  265. # ------ 删除审批记录 START ------ #
  266. Db::name('admin_examine_record')->whereLike('types', 'crm%')->delete();
  267. Db::query("ALTER TABLE ".$prefix."admin_examine_record AUTO_INCREMENT = 1");
  268. # ------ 删除审批记录 END ------ #
  269. # ------ 清除消息数据 START ------ #
  270. Db::name('admin_message')->where('module_name', 'crm')->delete();
  271. # ------ 清除消息数据 END ------ #
  272. # ------ 删除附件 START ------ #
  273. if (!empty($files)) {
  274. foreach ($files AS $key => $value) {
  275. unlink($value);
  276. }
  277. }
  278. # ------ 删除附件 START ------ #
  279. # 提交事务
  280. Db::commit();
  281. return true;
  282. } catch (Exception $e) {
  283. # 回滚事务
  284. Db::rollback();
  285. # 将状态设置为false,终止其他方法的操作
  286. $this->status = false;
  287. $this->log = '重置客户管理模块时出错!';
  288. return false;
  289. }
  290. }
  291. /**
  292. * 重置任务审批数据
  293. */
  294. private function resetTaskExamineData()
  295. {
  296. # 表前缀
  297. $prefix = config('database.prefix');
  298. # 文件数组
  299. $files = [];
  300. # 启动事务
  301. Db::startTrans();
  302. try {
  303. # ------ 清除任务数据 START ------ #
  304. // # 获取任务ID
  305. // $taskIds = Db::name('task')->where(['work_id' => ['eq', 0]])->column('task_id');
  306. //
  307. // # 获取任务下关联的附件ID
  308. // $taskFieldIds = Db::name('work_task_file')->whereIn('task_id', $taskIds)->column('file_id');
  309. //
  310. // # 查询活动附件数据
  311. // $taskFileInfo = $this->getFileList($taskFieldIds);
  312. //
  313. // # 合并附件数据
  314. // $files = array_merge($files, $taskFileInfo);
  315. //
  316. // # 删除任务附件关联表
  317. // Db::name('work_task_file')->whereIn('task_id', $taskIds)->delete();
  318. // # 重置自增ID
  319. // Db::query("ALTER TABLE ".$prefix."work_task_file AUTO_INCREMENT = 1");
  320. //
  321. // # 删除附件
  322. // Db::name('admin_file')->whereIn('file_id', $taskFieldIds)->delete();
  323. //
  324. // # 清除任务关联的客户模块数据数据并重置自增ID
  325. // Db::query("TRUNCATE TABLE ".$prefix."task_relation");
  326. //
  327. // # 删除任务log
  328. // Db::name('work_task_log')->whereIn('task_id', $taskIds)->delete();
  329. // # 重置任务log自增ID
  330. // Db::query("ALTER TABLE ".$prefix."work_task_log AUTO_INCREMENT = 1");
  331. //
  332. // # 删除任务
  333. // Db::name('task')->where(['work_id' => ['eq', 0]])->delete();
  334. // # 重置任务自增ID
  335. // Db::query("ALTER TABLE ".$prefix."task AUTO_INCREMENT = 1");
  336. # ------ 清除任务数据 END ------ #
  337. # ------ 清除审批数据 START ------ #
  338. # 获取审批下关联的附件ID
  339. $examineFileIds = Db::name('oa_examine_file')->column('file_id');
  340. # 查询审批附件数据
  341. $examineFileInfo = $this->getFileList($examineFileIds);
  342. # 合并附件数据
  343. $files = array_merge($files, $examineFileInfo);
  344. # 清除审批关联的附件信息并重置自增ID
  345. Db::query("TRUNCATE TABLE ".$prefix."oa_examine_file");
  346. # 获取差旅附件ID
  347. $travelFileId = Db::name('oa_examine_travel_file')->column('file_id');
  348. # 查询差旅附件数据
  349. $travelFileInfo = $this->getFileList($travelFileId);
  350. # 合并附件数据
  351. $files = array_merge($files, $travelFileInfo);
  352. # 清除差旅关联的附件信息并重置自增ID
  353. Db::query("TRUNCATE TABLE ".$prefix."oa_examine_travel_file");
  354. # 删除差率和审批的附件
  355. Db::name('admin_file')->whereIn('file_id', $examineFileIds)->delete();
  356. Db::name('admin_file')->whereIn('file_id', $travelFileId)->delete();
  357. # 删除差旅数据并重置自增ID
  358. Db::query("TRUNCATE TABLE ".$prefix."oa_examine_travel");
  359. # 删除审批关联的客户模块下的数据并重置ID
  360. Db::query("TRUNCATE TABLE ".$prefix."oa_examine_relation");
  361. # 删除审批数据并重置自增ID
  362. Db::query("TRUNCATE TABLE ".$prefix."oa_examine");
  363. # ------ 清除审批数据 END ------ #
  364. # ------ 清除活动中有关审批的数据 START ------ #
  365. # 获取有关审批的活动ID
  366. $activityIds = Db::name('crm_activity')->where('activity_type', 9)->column('activity_id');
  367. # 获取有关审批的附件ID
  368. $activityFileIds = Db::name('crm_activity_file')->whereIn('activity_id', $activityIds)->column('file_id');
  369. # 查询审批附件数据
  370. $activityFileInfo = $this->getFileList($activityFileIds);
  371. # 合并附件数据
  372. $files = array_merge($files, $activityFileInfo);
  373. # 删除有关审批的附件关联记录
  374. Db::name('crm_activity_file')->whereIn('activity_id', $activityIds)->delete();
  375. # 重置自增ID
  376. Db::query("ALTER TABLE ".$prefix."crm_activity_file AUTO_INCREMENT = 1");
  377. # 删除有关审批的活动记录
  378. Db::name('crm_activity')->where('activity_type', 9)->delete();
  379. # 重置自增ID
  380. Db::query("ALTER TABLE ".$prefix."crm_activity AUTO_INCREMENT = 1");
  381. # ------ 清除活动中有关审批的数据 END ------ #
  382. # ------ 清除有关审批的数据操作记录 START ------ #
  383. Db::name('admin_action_log')->where('module_name', 'oa')->where('controller_name', 'examine')->delete();
  384. Db::query("ALTER TABLE ".$prefix."admin_action_log AUTO_INCREMENT = 1");
  385. # ------ 清除活动中有关审批的数据 END ------ #
  386. # ------ 清除有关任务的数据操作记录 START ------ #
  387. // Db::name('admin_action_log')->where('module_name', 'oa')->where('controller_name', 'task')->delete();
  388. // Db::query("ALTER TABLE ".$prefix."admin_action_log AUTO_INCREMENT = 1");
  389. # ------ 清除有关任务的数据操作记录 END ------ #
  390. # ------ 清除审批日志 START ------ #
  391. Db::name('admin_examine_record')->where('types', 'oa_examine')->delete();
  392. Db::query("ALTER TABLE ".$prefix."admin_examine_record AUTO_INCREMENT = 1");
  393. # ------ 清除审批日志 END ------ #
  394. # ------ 清除任务和审批操作记录 START ------ #
  395. // Db::name('admin_operation_log')->where('module', 'oa_task')->delete();
  396. Db::name('admin_operation_log')->where('module', 'oa_examine')->delete();
  397. Db::query("ALTER TABLE ".$prefix."admin_operation_log AUTO_INCREMENT = 1");
  398. # ------ 清除任务操作记录 END ------ #
  399. # ------ 清除消息数据 START ------ #
  400. Db::name('admin_message')->where('module_name', 'oa')->where('controller_name', 'examine')->delete();
  401. # ------ 清除消息数据 END ------ #
  402. # ------ 重置附件表自增ID START ------ #
  403. Db::query("ALTER TABLE ".$prefix."admin_file AUTO_INCREMENT = 1");
  404. # ------ 重置附件表自增ID END ------ #
  405. # ------ 删除附件 START ------ #
  406. if (!empty($files)) {
  407. foreach ($files AS $key => $value) {
  408. unlink($value);
  409. }
  410. }
  411. # ------ 删除附件 START ------ #
  412. # 提交事务
  413. Db::commit();
  414. return true;
  415. } catch (Exception $e) {
  416. # 回滚事务
  417. Db::rollback();
  418. # 将状态设置为false,终止其他方法的操作
  419. $this->status = false;
  420. $this->log = '重置任务/审批模块时出错!';
  421. return false;
  422. }
  423. }
  424. /**
  425. * 重置日志数据
  426. */
  427. private function resetDailyRecordData()
  428. {
  429. # 表前缀
  430. $prefix = config('database.prefix');
  431. # 文件数组
  432. $files = [];
  433. # 启动事务
  434. Db::startTrans();
  435. try {
  436. # ------ 清除日志数据 START ------ #
  437. # 获取日志附件ID
  438. $logFileIds = Db::name('oa_log_file')->column('file_id');
  439. # 查询活动附件数据
  440. $logFileInfo = $this->getFileList($logFileIds);
  441. # 合并附件数据
  442. $files = array_merge($files, $logFileInfo);
  443. # 清除日志附件关联数据
  444. Db::query("TRUNCATE TABLE ".$prefix."oa_log_file");
  445. # 清除日志客户管理关联数据
  446. Db::query("TRUNCATE TABLE ".$prefix."oa_log_relation");
  447. # 清楚日志数据
  448. Db::query("TRUNCATE TABLE ".$prefix."oa_log");
  449. # ------ 清除日志数据 START ------ #
  450. # ------ 清除活动中有关审批的数据 START ------ #
  451. # 获取有关日志的活动ID
  452. $activityIds = Db::name('crm_activity')->where('activity_type', 8)->column('activity_id');
  453. # 获取有关日志的附件ID
  454. $activityFileIds = Db::name('crm_activity_file')->whereIn('activity_id', $activityIds)->column('file_id');
  455. # 查询日志附件数据
  456. $activityFileInfo = $this->getFileList($activityFileIds);
  457. # 合并附件数据
  458. $files = array_merge($files, $activityFileInfo);
  459. # 删除有关日志的附件关联记录
  460. Db::name('crm_activity_file')->whereIn('activity_id', $activityIds)->delete();
  461. # 重置自增ID
  462. Db::query("ALTER TABLE ".$prefix."crm_activity_file AUTO_INCREMENT = 1");
  463. # 删除有关日志的活动记录
  464. Db::name('crm_activity')->where('activity_type', 8)->delete();
  465. # 重置自增ID
  466. Db::query("ALTER TABLE ".$prefix."crm_activity AUTO_INCREMENT = 1");
  467. # ------ 清除活动中有关审批的数据 END ------ #
  468. # ------ 清除日志操作记录表 START ------ #
  469. Db::name('admin_action_log')->where('module_name', 'oa')->where('controller_name', 'log')->delete();
  470. Db::query("ALTER TABLE ".$prefix."admin_action_log AUTO_INCREMENT = 1");
  471. # ------ 清除日志操作记录表 END ------ #
  472. # ------ 清除日志操作记录 START ------ #
  473. Db::name('admin_operation_log')->where('module', 'oa_log')->delete();
  474. Db::query("ALTER TABLE ".$prefix."admin_operation_log AUTO_INCREMENT = 1");
  475. # ------ 清除日志操作记录 END ------ #
  476. # ------ 删除消息数据 START ------ #
  477. Db::name('admin_comment')->where('type', 'oa_log')->delete();
  478. Db::name('admin_message')->where('module_name', 'oa')->where('controller_name', 'log')->delete();
  479. # ------ 删除消息数据 END ------ #
  480. # ------ 重置附件表自增ID START ------ #
  481. Db::query("ALTER TABLE ".$prefix."admin_file AUTO_INCREMENT = 1");
  482. # ------ 重置附件表自增ID END ------ #
  483. # ------ 删除附件 START ------ #
  484. if (!empty($files)) {
  485. foreach ($files AS $key => $value) {
  486. unlink($value);
  487. }
  488. }
  489. # ------ 删除附件 START ------ #
  490. # 提交事务
  491. Db::commit();
  492. return true;
  493. } catch (Exception $e) {
  494. # 回滚事务
  495. Db::rollback();
  496. # 将状态设置为false,终止其他方法的操作
  497. $this->status = false;
  498. $this->log = '重置日志模块时出错!';
  499. return false;
  500. }
  501. }
  502. /**
  503. * 重置项目管理数据
  504. */
  505. private function resetProjectManagementData()
  506. {
  507. # 表前缀
  508. $prefix = config('database.prefix');
  509. # 文件数组
  510. $files = [];
  511. # 启动事务
  512. Db::startTrans();
  513. try {
  514. # ------ 清除项目管理数据 START ------ #
  515. # 获取项目ID
  516. // $workIds = Db::name('work')->column('work_id');
  517. # 获取任务ID
  518. // $taskIds = Db::name('task')->whereIn('work_id', $workIds)->column('task_id');
  519. $taskIds = Db::name('task')->column('task_id');
  520. # 获取关联附件ID
  521. $workTaskFileIds = Db::name('work_task_file')->whereIn('task_id', $taskIds)->column('file_id');
  522. # 查询项目附件数据
  523. $workTaskFileInfo = $this->getFileList($workTaskFileIds);
  524. # 合并附件数据
  525. $files = array_merge($files, $workTaskFileInfo);
  526. # 清除项目中的任务附件关联数据
  527. Db::name('work_task_file')->whereIn('task_id', $taskIds)->delete();
  528. # 重置自增ID
  529. Db::query("ALTER TABLE ".$prefix."work_task_file AUTO_INCREMENT = 1");
  530. # 清除标签数据
  531. Db::query("TRUNCATE TABLE ".$prefix."work_task_lable");
  532. # 清除任务日志
  533. Db::name('work_task_log')->whereIn('task_id', $taskIds)->delete();
  534. # 重置任务日志自增ID
  535. Db::query("ALTER TABLE ".$prefix."work_task_log AUTO_INCREMENT = 1");
  536. # 清除项目成员
  537. Db::query("TRUNCATE TABLE ".$prefix."work_user");
  538. # 清除任务分类
  539. Db::query("TRUNCATE TABLE ".$prefix."work_task_class");
  540. # 清除任务与客户模块的管理数据
  541. Db::query("TRUNCATE TABLE ".$prefix."work_relation");
  542. # 清除任务数据
  543. Db::query("TRUNCATE TABLE ".$prefix."task");
  544. // Db::name('task')->where('work_id', '<>', 0)->delete();
  545. // # 重置自增ID
  546. // Db::query("ALTER TABLE ".$prefix."task AUTO_INCREMENT = 1");
  547. # 清除项目数据
  548. Db::query("TRUNCATE TABLE ".$prefix."work");
  549. # ------ 清除项目管理数据 END ------ #
  550. # ------ 清除活动中有关任务的数据 START ------ #
  551. # 获取有关任务的活动ID
  552. $activityIds = Db::name('crm_activity')->where('activity_type', 11)->whereIn('activity_type_id', $taskIds)->column('activity_id');
  553. # 获取有关任务的附件ID
  554. $activityFileIds = Db::name('crm_activity_file')->whereIn('activity_id', $activityIds)->column('file_id');
  555. # 查询任务附件数据
  556. $activityFileInfo = $this->getFileList($activityFileIds);
  557. # 合并附件数据
  558. $files = array_merge($files, $activityFileInfo);
  559. # 删除有关任务的附件关联记录
  560. Db::name('crm_activity_file')->whereIn('activity_id', $activityIds)->delete();
  561. # 重置自增ID
  562. Db::query("ALTER TABLE ".$prefix."crm_activity_file AUTO_INCREMENT = 1");
  563. # 删除有关任务的活动记录
  564. Db::name('crm_activity')->where('activity_type', 11)->whereIn('activity_type_id', $taskIds)->delete();
  565. # 重置自增ID
  566. Db::query("ALTER TABLE ".$prefix."crm_activity AUTO_INCREMENT = 1");
  567. # ------ 清除活动中有关任务的数据 END ------ #
  568. # ------ 清除有关项目的数据操作记录 START ------ #
  569. Db::name('admin_action_log')->where('module_name', 'work')->delete();
  570. Db::query("ALTER TABLE ".$prefix."admin_action_log AUTO_INCREMENT = 1");
  571. # ------ 清除有关项目的数据操作记录 END ------ #
  572. # ------ 清除项目操作记录 START ------ #
  573. Db::name('admin_operation_log')->where('module', 'work_task')->delete();
  574. Db::query("ALTER TABLE ".$prefix."admin_operation_log AUTO_INCREMENT = 1");
  575. # ------ 清除项目操作记录 END ------ #
  576. # ------ 清除评论和消息数据 START ------#
  577. Db::name('admin_comment')->where('type', 'task')->delete();
  578. Db::name('admin_message')->where('module_name', 'work')->delete();
  579. Db::name('admin_message')->where('module_name', 'oa')->where('controller_name', 'task')->delete();
  580. # ------ 清除评论和消息数据 END ------ #
  581. # ------ 重置附件表自增ID START ------ #
  582. Db::query("ALTER TABLE ".$prefix."admin_file AUTO_INCREMENT = 1");
  583. # ------ 重置附件表自增ID END ------ #
  584. # ------ 删除附件 START ------ #
  585. if (!empty($files)) {
  586. foreach ($files AS $key => $value) {
  587. unlink($value);
  588. }
  589. }
  590. # ------ 删除附件 START ------ #
  591. # 提交事务
  592. Db::commit();
  593. return true;
  594. } catch (Exception $e) {
  595. # 回滚事务
  596. Db::rollback();
  597. # 将状态设置为false,终止其他方法的操作
  598. $this->status = false;
  599. $this->log = '重置项目管理模块时出错!';
  600. return false;
  601. }
  602. }
  603. /**
  604. * 重置日历数据
  605. */
  606. private function resetCalendarData()
  607. {
  608. # 表前缀
  609. $prefix = config('database.prefix');
  610. # 文件数组
  611. $files = [];
  612. # 启动事务
  613. Db::startTrans();
  614. try {
  615. # ------ 清除日历数据 START ------ #
  616. # 获取日历ID
  617. $eventIds = Db::name('oa_event')->column('event_id');
  618. # 删除日历关联的客户数据
  619. Db::query("TRUNCATE TABLE ".$prefix."oa_event_relation");
  620. # 删除日历的数据操作记录
  621. Db::name('admin_action_log')->where('module_name', 'oa')->where('controller_name', 'event')->delete();
  622. # 重置自增ID
  623. Db::query("ALTER TABLE ".$prefix."admin_action_log AUTO_INCREMENT = 1");
  624. # 删除日历的字段操作记录
  625. Db::name('admin_action_record')->where('types', 'oa_event')->delete();
  626. # 重置自增ID
  627. Db::query("ALTER TABLE ".$prefix."admin_action_record AUTO_INCREMENT = 1");
  628. # 删除日历提醒
  629. Db::query("TRUNCATE TABLE ".$prefix."oa_event_notice");
  630. # 删除日历数据
  631. Db::query("TRUNCATE TABLE ".$prefix."oa_event");
  632. # ------ 清除日历数据 END ------ #
  633. # ------ 删除活动数据 START ------ #
  634. # 获取活动ID
  635. $activityIds = Db::name('crm_activity')->where('activity_type', 10)->whereIn('activity_type_id', $eventIds)->column('activity_id');
  636. # 获取有关任务的附件ID
  637. $activityFileIds = Db::name('crm_activity_file')->whereIn('activity_id', $activityIds)->column('file_id');
  638. # 查询任务附件数据
  639. $activityFileInfo = $this->getFileList($activityFileIds);
  640. # 合并附件数据
  641. $files = array_merge($files, $activityFileInfo);
  642. # 删除有关任务的附件关联记录
  643. Db::name('crm_activity_file')->whereIn('activity_id', $activityIds)->delete();
  644. # 重置自增ID
  645. Db::query("ALTER TABLE ".$prefix."crm_activity_file AUTO_INCREMENT = 1");
  646. # 删除有关任务的活动记录
  647. Db::name('crm_activity')->where('activity_type', 10)->whereIn('activity_type_id', $eventIds)->delete();
  648. # 重置自增ID
  649. Db::query("ALTER TABLE ".$prefix."crm_activity AUTO_INCREMENT = 1");
  650. # ------ 删除活动数据 END ------ #
  651. # ------ 删除消息数据 START ------ #
  652. Db::name('admin_message')->where('module_name', 'oa')->where('controller_name', 'event')->delete();
  653. # ------ 删除消息数据 END ------ #
  654. # ------ 重置附件表自增ID START ------ #
  655. Db::query("ALTER TABLE ".$prefix."admin_file AUTO_INCREMENT = 1");
  656. # ------ 重置附件表自增ID END ------ #
  657. # ------ 删除附件 START ------ #
  658. if (!empty($files)) {
  659. foreach ($files AS $key => $value) {
  660. unlink($value);
  661. }
  662. }
  663. # ------ 删除附件 START ------ #
  664. # 提交事务
  665. Db::commit();
  666. return true;
  667. } catch (Exception $e) {
  668. # 回滚事务
  669. Db::rollback();
  670. # 将状态设置为false,终止其他方法的操作
  671. $this->status = false;
  672. $this->log = '重置日历模块时出错!';
  673. return false;
  674. }
  675. }
  676. /**
  677. * 获取文件列表
  678. *
  679. * @param $fileIds
  680. * @return array
  681. * @throws \think\db\exception\DataNotFoundException
  682. * @throws \think\db\exception\ModelNotFoundException
  683. * @throws \think\exception\DbException
  684. */
  685. private function getFileList($fileIds)
  686. {
  687. $result = [];
  688. $list = Db::name('admin_file')->field([
  689. 'file_path',
  690. 'file_path_thumb'
  691. ])->whereIn('file_id', $fileIds)->select();
  692. foreach ($list AS $key => $value) {
  693. if (!empty($value['file_path'])) $result[] = 'public/uploads/' . $value['file_path'];
  694. if (!empty($value['file_path_thumb'])) $result[] = 'public/uploads/' . $value['file_path_thumb'];
  695. }
  696. return $result;
  697. }
  698. }