InitializeLogic.php 40KB

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