NoticeTemplate.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace addons\qingdong\model;
  3. use app\admin\model\AuthGroup;
  4. use think\Model;
  5. /**
  6. *通知模板
  7. */
  8. class NoticeTemplate extends Model
  9. {
  10. // 表名,不含前缀
  11. protected $name = 'qingdong_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,$to_staff_id='')
  153. {
  154. if($to_staff_id){
  155. $staff = Staff::get($to_staff_id);
  156. $staff_name = $staff['name'];
  157. $group_ids =$staff['group_ids'];
  158. }else{
  159. $staff = Staff::info();
  160. $staff_name = $staff->name;
  161. $group_ids = $staff->group_ids;
  162. }
  163. $staff_department = AuthGroup::where(['id' => ['in', $group_ids]])->column('name');
  164. $staff_department=implode(',',$staff_department);
  165. $date = date('Y-m-d H:i:s');
  166. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  167. $search = ['{{staff_name}}', '{{staff_department}}', '{{date}}'];
  168. $replace = [$staff_name, $staff_department, $date];
  169. $data = str_replace($search, $replace, $data);
  170. switch ($relation_type) {
  171. case Message::EXAMINE_TYPE:
  172. $examine_type = ExamineRecord::where(['id' => $relation_id])->value('relation_type');
  173. if($examine_type){
  174. if ($examine_type == ExamineRecord::CONSUME_TYPE) {
  175. $examine_type = '费用审批';
  176. $consume = Consume::where(['id' => $relation_id])->with(['customer'])->find();
  177. $customer_name = $consume['customer']['name'] ?? '';
  178. } elseif ($examine_type == ExamineRecord::CONTRACT_TYPE) {
  179. $examine_type = '合同审批';
  180. $contract = Contract::where(['id' => $relation_id])->with(['customer'])->find();
  181. $customer_name = $contract['customer']['name'] ?? '';
  182. } elseif ($examine_type == ExamineRecord::RECEIVABLES_TYPE) {
  183. $consume = Consume::where(['id' => $relation_id])->with(['customer'])->find();
  184. $examine_type = '回款审批';
  185. $customer_name = $consume['customer']['name'] ?? '';
  186. } elseif ($examine_type == ExamineRecord::ACHIEVEMENT_TYPE) {
  187. $examine_type = '业绩目标';
  188. $customer_name = '';
  189. } else {
  190. $examine_type = '';
  191. $customer_name = '';
  192. }
  193. $search = ['{{examine_type}}', '{{customer_name}}'];
  194. $replace = [$examine_type, $customer_name];
  195. $data = str_replace($search, $replace, $data);
  196. }
  197. break;
  198. case Message::RECORD_TYPE:
  199. $record = Record::where(['id' => $relation_id])->find();
  200. if($record){
  201. if ($record['relation_type'] == 1) {//客户
  202. $customer = Customer::where(['id' => $record['relation_id']])->field('id,name')->find();
  203. $customer_name = $customer['name'] ?? '';
  204. } elseif ($record['relation_type'] == 2) {//联系人
  205. $customer = Contacts::where(['id' => $record['relation_id']])->with(['customer'])->find();
  206. $customer_name = $customer['customer']['name'] ?? '';
  207. } elseif ($record['relation_type'] == 3) {//合同
  208. $customer = Contract::where(['id' => $record['relation_id']])->with(['customer'])->find();
  209. $customer_name = $customer['customer']['name'] ?? '';
  210. } elseif ($record['relation_type'] == 4) {//线索
  211. $leads = Leads::where(['id' => $record['relation_id']])->field('name,follow')->find();
  212. $customer_name = $leads['name'] ?? '';
  213. }elseif ($record['relation_type'] == 5) {//商机
  214. $leads = Business::where(['id' => $record['relation_id']])->field('name')->find();
  215. $customer_name = $leads['name'] ?? '';
  216. }
  217. $customer_follow = $record['follow'] ?? '';
  218. $content = $record['content'] ?? '';
  219. $search = ['{{customer_name}}', '{{customer_follow}}', '{{content}}'];
  220. $replace = [$customer_name, $customer_follow, $content];
  221. $data = str_replace($search, $replace, $data);
  222. }
  223. break;
  224. case Message::DAILY_TYPE:
  225. $daily = Daily::get($relation_id);
  226. if($daily){
  227. $search = ['{{daily_type}}'];
  228. $replace = [$daily['type']];
  229. $data = str_replace($search, $replace, $data);
  230. }
  231. break;
  232. case Message::SEAS_TYPE:
  233. $customer = Customer::where(['id' => $relation_id])->field('id,name,receivetime')->find();
  234. if($customer){
  235. $customer_name = $customer['name'] ?? '';
  236. $day = intval((time() - $customer['receivetime']) / 86400);
  237. $search = ['{{customer_name}}', '{{day}}'];
  238. $replace = [$customer_name, $day];
  239. $data = str_replace($search, $replace, $data);
  240. }
  241. break;
  242. case Message::SIGN_TYPE://签到
  243. $sign = StaffSignIn::where(['id' => $relation_id])->with(['customer'])->find();
  244. if($sign){
  245. $customer_name = $sign['customer']['name'] ?? '';
  246. $address = $sign['location'] ?? '';
  247. $search = ['{{customer_name}}', '{{address}}'];
  248. $replace = [$customer_name, $address];
  249. $data = str_replace($search, $replace, $data);
  250. }
  251. break;
  252. case Message::EXAMINE_ADOPT_TYPE://
  253. case Message::EXAMINE_REFUSE_TYPE://
  254. $examine = ExamineRecord::where(['id' => $relation_id])->with(['checkStaff'])->find();
  255. if($examine){
  256. $examine_type = $examine['relation_type'];
  257. if ($examine_type == ExamineRecord::CONSUME_TYPE) {
  258. $examine_type = '费用审批';
  259. $consume = Consume::where(['id' => $relation_id])->with(['customer'])->find();
  260. $customer_name = $consume['customer']['name'] ?? '';
  261. } elseif ($examine_type == ExamineRecord::CONTRACT_TYPE) {
  262. $examine_type = '合同审批';
  263. $contract = Contract::where(['id' => $relation_id])->with(['customer'])->find();
  264. $customer_name = $contract['customer']['name'] ?? '';
  265. } elseif ($examine_type == ExamineRecord::RECEIVABLES_TYPE) {
  266. $consume = Consume::where(['id' => $relation_id])->with(['customer'])->find();
  267. $examine_type = '回款审批';
  268. $customer_name = $consume['customer']['name'] ?? '';
  269. } elseif ($examine_type == ExamineRecord::ACHIEVEMENT_TYPE) {
  270. $examine_type = '业绩目标';
  271. $customer_name = '';
  272. } else {
  273. $examine_type = '';
  274. $customer_name = '';
  275. }
  276. $check_name = $examine['check_staff']['name'] ?? '';
  277. $examine_desc = $examine['content'];
  278. $submit_time =$examine['check_time'];
  279. $search = ['{{examine_type}}', '{{customer_name}}', '{{check_name}}', '{{examine_desc}}', '{{submit_time}}'];
  280. $replace = [$examine_type, $customer_name, $check_name, $examine_desc, $submit_time];
  281. $data = str_replace($search, $replace, $data);
  282. }
  283. break;
  284. case Message::CONTRACT_EXPIRE_TYPE://
  285. $contract = Contract::where(['id' => $relation_id])->with(['customer'])->find();
  286. if($contract){
  287. $customer_name = $contract['customer']['name'] ?? '';
  288. $money = $contract['money'];
  289. $submit_time = $contract['order_date'];
  290. $search = ['{{customer_name}}', '{{money}}', '{{submit_time}}'];
  291. $replace = [$customer_name, $money, $submit_time];
  292. $data = str_replace($search, $replace, $data);
  293. }
  294. break;
  295. case Message::PLAN_EXPIRE_TYPE://
  296. $plan=ReceivablesPlan::where(['id'=>$relation_id])->with(['customer'])->find();
  297. if($plan){
  298. $customer_name=$plan['customer']['name']??'';
  299. $plan_money=$plan['money'];
  300. $plan_time=$plan['return_date'];
  301. $search = ['{{customer_name}}', '{{plan_money}}', '{{plan_time}}'];
  302. $replace = [$customer_name, $plan_money, $plan_time];
  303. $data = str_replace($search, $replace, $data);
  304. }
  305. break;
  306. case Message::COMMENT_TYPE://
  307. $comment=Comment::where(['id'=>$relation_id])->find();
  308. if($comment){
  309. $content=$comment['content'];
  310. $search = ['{{content}}'];
  311. $replace = [$content];
  312. $data = str_replace($search, $replace, $data);
  313. }
  314. break;
  315. case Message::EVENT_TYPE://
  316. $event=Event::where(['id'=>$relation_id])->find();
  317. if($event){
  318. $search = ['{{event_title}}','{{event_start_time}}','{{event_end_time}}'];
  319. $replace = [$event['title'],$event['start_time'],$event['end_time']];
  320. $data = str_replace($search, $replace, $data);
  321. }
  322. break;
  323. }
  324. return json_decode($data,true);
  325. }
  326. }