NoticeTemplate.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use app\admin\model\AuthGroup;
  4. use think\Model;
  5. /**
  6. *通知模板
  7. */
  8. class NoticeTemplate extends Model
  9. {
  10. // 表名,不含前缀
  11. protected $name = 'qingdongams_notice_template';
  12. // 开启自动写入时间戳字段
  13. protected $autoWriteTimestamp = false;
  14. public function getDataAttr($value)
  15. {
  16. return json_decode($value, true);
  17. }
  18. public function getEnterpriseDataAttr($value)
  19. {
  20. return json_decode($value, true);
  21. }
  22. public static function getTypeForData($type)
  23. {
  24. $data = self::where(['type' => $type])->value('data');
  25. return json_decode($data, true);
  26. }
  27. public static function getTypeForEnterpriseData($type)
  28. {
  29. $data = self::where(['type' => $type])->value('enterprise_data');
  30. return json_decode($data, true);
  31. }
  32. public function getVariable($type)
  33. {
  34. switch ($type) {
  35. case Message::EXAMINE_TYPE:
  36. $data = [
  37. '{{staff_name}}' => '员工姓名',
  38. '{{staff_department}}' => '员工角色组',
  39. '{{examine_type}}' => '审批类型',
  40. '{{customer_name}}' => '关联客户',
  41. '{{date}}' => '操作时间',
  42. ];
  43. break;
  44. case Message::RECORD_TYPE:
  45. $data = [
  46. '{{staff_name}}' => '员工姓名',
  47. '{{staff_department}}' => '员工角色组',
  48. '{{customer_name}}' => '关联客户',
  49. '{{customer_follow}}' => '跟进状态',
  50. '{{content}}' => '跟进内容',
  51. '{{date}}' => '操作时间',
  52. ];
  53. break;
  54. case Message::DAILY_TYPE:
  55. $data = [
  56. '{{staff_name}}' => '员工姓名',
  57. '{{staff_department}}' => '员工角色组',
  58. '{{daily_type}}' => '报告类型',
  59. '{{date}}' => '操作时间',
  60. ];
  61. break;
  62. case Message::SEAS_TYPE:
  63. $data = [
  64. '{{staff_name}}' => '员工姓名',
  65. '{{staff_department}}' => '员工角色组',
  66. '{{customer_name}}' => '客户名称',
  67. '{{day}}' => '未跟进天数',
  68. '{{date}}' => '时间',
  69. ];
  70. break;
  71. case Message::SIGN_TYPE://签到
  72. $data = [
  73. '{{staff_name}}' => '员工姓名',
  74. '{{staff_department}}' => '员工角色组',
  75. '{{customer_name}}' => '客户名称',
  76. '{{address}}' => '签到地点',
  77. '{{date}}' => '时间',
  78. ];
  79. break;
  80. case Message::EXAMINE_ADOPT_TYPE://
  81. $data = [
  82. '{{staff_name}}' => '员工姓名',
  83. '{{staff_department}}' => '员工角色组',
  84. '{{customer_name}}' => '关联客户',
  85. '{{examine_type}}' => '审批类型',
  86. '{{check_name}}' => '审核人',
  87. '{{examine_desc}}' => '审核备注',
  88. '{{submit_time}}' => '提交时间',
  89. '{{date}}' => '操作时间',
  90. ];
  91. break;
  92. case Message::EXAMINE_REFUSE_TYPE://
  93. $data = [
  94. '{{staff_name}}' => '员工姓名',
  95. '{{staff_department}}' => '员工角色组',
  96. '{{customer_name}}' => '关联客户',
  97. '{{examine_type}}' => '审批类型',
  98. '{{check_name}}' => '审核人',
  99. '{{examine_desc}}' => '审核备注',
  100. '{{submit_time}}' => '提交时间',
  101. '{{date}}' => '操作时间',
  102. ];
  103. break;
  104. case Message::CONTRACT_EXPIRE_TYPE://
  105. $data = [
  106. '{{staff_name}}' => '员工姓名',
  107. '{{staff_department}}' => '员工角色组',
  108. '{{customer_name}}' => '关联客户',
  109. '{{money}}' => '合同到期金额',
  110. '{{submit_time}}' => '合同创建时间',
  111. '{{date}}' => '操作时间',
  112. ];
  113. break;
  114. case Message::PLAN_EXPIRE_TYPE://
  115. $data = [
  116. '{{staff_name}}' => '员工姓名',
  117. '{{staff_department}}' => '员工角色组',
  118. '{{customer_name}}' => '关联客户',
  119. '{{plan_money}}' => '计划回款金额',
  120. '{{plan_time}}' => '计划回款时间',
  121. '{{date}}' => '操作时间',
  122. ];
  123. break;
  124. case Message::COMMENT_TYPE://
  125. $data = [
  126. '{{staff_name}}' => '员工姓名',
  127. '{{staff_department}}' => '员工角色组',
  128. '{{content}}' => '评论内容',
  129. '{{date}}' => '操作时间',
  130. ];
  131. break;
  132. case Message::EVENT_TYPE://
  133. $data = [
  134. '{{staff_name}}' => '员工姓名',
  135. '{{staff_department}}' => '员工角色组',
  136. '{{event_title}}' => '日程标题',
  137. '{{event_start_time}}' => '日程开始时间',
  138. '{{event_end_time}}' => '日程结束时间',
  139. '{{date}}' => '操作时间',
  140. ];
  141. break;
  142. default:
  143. $data = [
  144. '{{staff_name}}' => '员工姓名',
  145. '{{staff_department}}' => '员工角色组',
  146. '{{date}}' => '操作时间',
  147. ];
  148. }
  149. return $data;
  150. }
  151. //替换模板内容
  152. public static function replaceTemplateContent($data, $relation_type, $relation_id)
  153. {
  154. $staff = Staff::info();
  155. $staff_name = $staff->name;
  156. $staff_department = AuthGroup::where(['id' => ['in', $staff->group_ids]])->column('name');
  157. $staff_department=implode(',',$staff_department);
  158. $date = date('Y-m-d H:i:s');
  159. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  160. $search = ['{{staff_name}}', '{{staff_department}}', '{{date}}'];
  161. $replace = [$staff_name, $staff_department, $date];
  162. $data = str_replace($search, $replace, $data);
  163. switch ($relation_type) {
  164. case Message::EXAMINE_TYPE:
  165. $examine_type = ExamineRecord::where(['id' => $relation_id])->value('relation_type');
  166. if($examine_type){
  167. if ($examine_type == ExamineRecord::CONSUME_TYPE) {
  168. $examine_type = '费用审批';
  169. $consume = Consume::where(['id' => $relation_id])->with(['customer'])->find();
  170. $customer_name = $consume['customer']['name'] ?? '';
  171. } elseif ($examine_type == ExamineRecord::CONTRACT_TYPE) {
  172. $examine_type = '合同审批';
  173. $contract = Contract::where(['id' => $relation_id])->with(['customer'])->find();
  174. $customer_name = $contract['customer']['name'] ?? '';
  175. } elseif ($examine_type == ExamineRecord::RECEIVABLES_TYPE) {
  176. $consume = Consume::where(['id' => $relation_id])->with(['customer'])->find();
  177. $examine_type = '回款审批';
  178. $customer_name = $consume['customer']['name'] ?? '';
  179. } elseif ($examine_type == ExamineRecord::ACHIEVEMENT_TYPE) {
  180. $examine_type = '业绩目标';
  181. $customer_name = '';
  182. } else {
  183. $examine_type = '';
  184. $customer_name = '';
  185. }
  186. $search = ['{{examine_type}}', '{{customer_name}}'];
  187. $replace = [$examine_type, $customer_name];
  188. $data = str_replace($search, $replace, $data);
  189. }
  190. break;
  191. case Message::RECORD_TYPE:
  192. $record = Record::where(['id' => $relation_id])->find();
  193. if($record){
  194. if ($record['relation_type'] == 1) {//客户
  195. $customer = Customer::where(['id'=>$record['relation_id']])->value('name');
  196. $customer_name = $customer ?? '';
  197. } elseif ($record['relation_type'] == 2) {//联系人
  198. $customer = Contacts::where(['id' => $record['relation_id']])->with(['customer'])->find();
  199. $customer_name = $customer['customer']['name'] ?? '';
  200. } elseif ($record['relation_type'] == 3) {//合同
  201. $customer = Contract::where(['id' => $record['relation_id']])->with(['customer'])->find();
  202. $customer_name = $customer['customer']['name'] ?? '';
  203. } elseif ($record['relation_type'] == 4) {//线索
  204. $leads = Leads::where(['id' => $record['relation_id']])->field('name,follow')->find();
  205. $customer_name = $leads['name'] ?? '';
  206. }elseif ($record['relation_type'] == 5) {//商机
  207. $leads = Business::where(['id' => $record['relation_id']])->field('name')->find();
  208. $customer_name = $leads['name'] ?? '';
  209. }
  210. $customer_follow = $record['follow'] ?? '';
  211. $content = $record['content'] ?? '';
  212. $search = ['{{customer_name}}', '{{customer_follow}}', '{{content}}'];
  213. $replace = [$customer_name, $customer_follow, $content];
  214. $data = str_replace($search, $replace, $data);
  215. }
  216. break;
  217. case Message::DAILY_TYPE:
  218. $daily = Daily::get($relation_id);
  219. if($daily){
  220. $search = ['{{daily_type}}'];
  221. $replace = [$daily['type']];
  222. $data = str_replace($search, $replace, $data);
  223. }
  224. break;
  225. case Message::SEAS_TYPE:
  226. $customer = Customer::where(['id' => $relation_id])->field('id,name,receivetime')->find();
  227. if($customer){
  228. $customer_name = $customer['name'] ?? '';
  229. $day = intval((time() - $customer['receivetime']) / 86400);
  230. $search = ['{{customer_name}}', '{{day}}'];
  231. $replace = [$customer_name, $day];
  232. $data = str_replace($search, $replace, $data);
  233. }
  234. break;
  235. case Message::SIGN_TYPE://签到
  236. $sign = StaffSignIn::where(['id' => $relation_id])->with(['customer'])->find();
  237. if($sign){
  238. $customer_name = $sign['customer']['name'] ?? '';
  239. $address = $sign['location'] ?? '';
  240. $search = ['{{customer_name}}', '{{customer_follow}}', '{{address}}'];
  241. $replace = [$customer_name,$sign['relation_process'], $address];
  242. $data = str_replace($search, $replace, $data);
  243. }
  244. break;
  245. case Message::EXAMINE_ADOPT_TYPE://
  246. case Message::EXAMINE_REFUSE_TYPE://
  247. $examine = ExamineRecord::where(['id' => $relation_id])->with(['checkStaff'])->find();
  248. if($examine){
  249. $examine_type = $examine['relation_type'];
  250. if ($examine_type == ExamineRecord::CONSUME_TYPE) {
  251. $examine_type = '费用审批';
  252. $consume = Consume::where(['id' => $relation_id])->with(['customer'])->find();
  253. $customer_name = $consume['customer']['name'] ?? '';
  254. } elseif ($examine_type == ExamineRecord::CONTRACT_TYPE) {
  255. $examine_type = '合同审批';
  256. $contract = Contract::where(['id' => $relation_id])->with(['customer'])->find();
  257. $customer_name = $contract['customer']['name'] ?? '';
  258. } elseif ($examine_type == ExamineRecord::RECEIVABLES_TYPE) {
  259. $consume = Consume::where(['id' => $relation_id])->with(['customer'])->find();
  260. $examine_type = '回款审批';
  261. $customer_name = $consume['customer']['name'] ?? '';
  262. } elseif ($examine_type == ExamineRecord::ACHIEVEMENT_TYPE) {
  263. $examine_type = '业绩目标';
  264. $customer_name = '';
  265. } else {
  266. $examine_type = '';
  267. $customer_name = '';
  268. }
  269. $check_name = $examine['check_staff']['name'] ?? '';
  270. $examine_desc = $examine['content'];
  271. $submit_time =$examine['check_time'];
  272. $search = ['{{examine_type}}', '{{customer_name}}', '{{check_name}}', '{{examine_desc}}', '{{submit_time}}'];
  273. $replace = [$examine_type, $customer_name, $check_name, $examine_desc, $submit_time];
  274. $data = str_replace($search, $replace, $data);
  275. }
  276. break;
  277. case Message::CONTRACT_EXPIRE_TYPE://
  278. $contract = Contract::where(['id' => $relation_id])->with(['customer'])->find();
  279. if($contract){
  280. $customer_name = $contract['customer']['name'] ?? '';
  281. $money = $contract['money'];
  282. $submit_time = $contract['order_date'];
  283. $search = ['{{customer_name}}', '{{money}}', '{{submit_time}}'];
  284. $replace = [$customer_name, $money, $submit_time];
  285. $data = str_replace($search, $replace, $data);
  286. }
  287. break;
  288. case Message::PLAN_EXPIRE_TYPE://
  289. $plan=ReceivablesPlan::where(['id'=>$relation_id])->with(['customer'])->find();
  290. if($plan){
  291. $customer_name=$plan['customer']['name']??'';
  292. $plan_money=$plan['money'];
  293. $plan_time=$plan['return_date'];
  294. $search = ['{{customer_name}}', '{{plan_money}}', '{{plan_time}}'];
  295. $replace = [$customer_name, $plan_money, $plan_time];
  296. $data = str_replace($search, $replace, $data);
  297. }
  298. break;
  299. case Message::COMMENT_TYPE://
  300. $comment=Comment::where(['id'=>$relation_id])->order('id desc')->find();
  301. if($comment){
  302. $content=$comment['content'];
  303. $search = ['{{content}}'];
  304. $replace = [$content];
  305. $data = str_replace($search, $replace, $data);
  306. }
  307. break;
  308. case Message::EVENT_TYPE://
  309. $event=Event::where(['id'=>$relation_id])->find();
  310. if($event){
  311. $search = ['{{event_title}}','{{event_start_time}}','{{event_end_time}}'];
  312. $replace = [$event['title'],$event['start_time'],$event['end_time']];
  313. $data = str_replace($search, $replace, $data);
  314. }
  315. break;
  316. }
  317. return json_decode($data,true);
  318. }
  319. }