ContactsService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Contacts;
  5. use App\Model\ContactsInfo;
  6. use App\Model\Employee;
  7. use Illuminate\Support\Facades\DB;
  8. class ContactsService extends Service
  9. {
  10. public function contactsEdit($data,$user){
  11. list($status,$msg) = $this->contactsRule($data,false);
  12. if(!$status) return [$status,$msg];
  13. try {
  14. DB::beginTransaction();
  15. $model = Contacts::where('id',$data['id'])->first();
  16. $model->img = $data['img'] ?? '';
  17. $model->title = $data['title'];
  18. $model->mailbox = $data['mailbox'] ?? '';
  19. $model->qq = $data['qq'] ?? '';
  20. $model->postal_code = $data['postal_code'] ?? '';
  21. $model->sex = $data['sex'] ?? '';
  22. $model->gregorian_bir = $data['gregorian_bir'] ?? '';
  23. $model->lunar_bir = $data['lunar_bir'] ?? '';
  24. $model->address1 = $data['address1'] ?? '';
  25. $model->address2 = $data['address2'] ?? '';
  26. $model->hobby = $data['hobby'] ?? '';
  27. $model->mark = $data['mark'] ?? '';
  28. $model->importance = $data['importance'] ?? '';
  29. $model->intimacy = $data['intimacy'] ?? '';
  30. $model->depart = $data['depart'] ?? '';
  31. $model->depart_id = $data['depart_id'] ?? '';
  32. $model->job = $data['job'] ?? '';
  33. $model->decision = $data['decision'] ?? 0;
  34. $model->grade = $data['grade'] ?? 0;
  35. $model->save();
  36. $time = time();
  37. ContactsInfo::where('del_time',0)
  38. ->where('contacts_id',$data['id'])
  39. ->update(['del_time' => $time]);
  40. if(! empty($data['contacts'])){
  41. $insert = [];
  42. foreach ($data['contacts'] as $value){
  43. $insert[] = [
  44. 'contacts_id' => $model->id,
  45. 'contact_type' => $value['id'],
  46. 'contact_info' => $value['info'],
  47. 'type' => ContactsInfo::type_one,
  48. 'crt_time' => $time,
  49. ];
  50. }
  51. ContactsInfo::insert($insert);
  52. }
  53. if(! empty($data['customers'])){
  54. $insert = [];
  55. foreach ($data['customers'] as $value){
  56. $insert[] = [
  57. 'contacts_id' => $model->id,
  58. 'employee_id' => $value,
  59. 'type' => ContactsInfo::type_two,
  60. 'crt_time' => $time,
  61. ];
  62. }
  63. ContactsInfo::insert($insert);
  64. }
  65. if(! empty($data['employee_one'])){
  66. $insert = [];
  67. foreach ($data['employee_one'] as $value){
  68. $insert[] = [
  69. 'contacts_id' => $model->id,
  70. 'employee_id' => $value,
  71. 'type' => ContactsInfo::type_three,
  72. 'crt_time' => $time,
  73. ];
  74. }
  75. ContactsInfo::insert($insert);
  76. }
  77. if(! empty($data['employee_two'])){
  78. $insert = [];
  79. foreach ($data['employee_two'] as $value){
  80. $insert[] = [
  81. 'contacts_id' => $model->id,
  82. 'employee_id' => $value,
  83. 'type' => ContactsInfo::type_four,
  84. 'crt_time' => $time,
  85. ];
  86. }
  87. ContactsInfo::insert($insert);
  88. }
  89. DB::commit();
  90. }catch (\Exception $exception){
  91. DB::rollBack();
  92. return [false,$exception->getMessage()];
  93. }
  94. return [true,''];
  95. }
  96. public function contactsAdd($data,$user){
  97. list($status,$msg) = $this->contactsRule($data);
  98. if(!$status) return [$status,$msg];
  99. try {
  100. DB::beginTransaction();
  101. $model = new Contacts();
  102. $model->img = $data['img'] ?? '';
  103. $model->title = $data['title'];
  104. $model->mailbox = $data['mailbox'] ?? '';
  105. $model->qq = $data['qq'] ?? '';
  106. $model->postal_code = $data['postal_code'] ?? '';
  107. $model->sex = $data['sex'] ?? '';
  108. $model->gregorian_bir = $data['gregorian_bir'] ?? '';
  109. $model->lunar_bir = $data['lunar_bir'] ?? '';
  110. $model->address1 = $data['address1'] ?? '';
  111. $model->address2 = $data['address2'] ?? '';
  112. $model->hobby = $data['hobby'] ?? '';
  113. $model->crt_id = $user['id'];
  114. $model->mark = $data['mark'] ?? '';
  115. $model->importance = $data['importance'] ?? '';
  116. $model->intimacy = $data['intimacy'] ?? '';
  117. $model->depart = $data['depart'] ?? '';
  118. $model->depart_id = $data['depart_id'] ?? '';
  119. $model->job = $data['job'] ?? '';
  120. $model->decision = $data['decision'] ?? 0;
  121. $model->grade = $data['grade'] ?? 0;
  122. $model->save();
  123. $time = time();
  124. if(! empty($data['contacts'])){
  125. $insert = [];
  126. foreach ($data['contacts'] as $value){
  127. $insert[] = [
  128. 'contacts_id' => $model->id,
  129. 'contact_type' => $value['id'],
  130. 'contact_info' => $value['info'],
  131. 'type' => ContactsInfo::type_one,
  132. 'crt_time' => $time,
  133. ];
  134. }
  135. ContactsInfo::insert($insert);
  136. }
  137. if(! empty($data['customers'])){
  138. $insert = [];
  139. foreach ($data['customers'] as $value){
  140. $insert[] = [
  141. 'contacts_id' => $model->id,
  142. 'employee_id' => $value,
  143. 'type' => ContactsInfo::type_two,
  144. 'crt_time' => $time,
  145. ];
  146. }
  147. ContactsInfo::insert($insert);
  148. }
  149. if(! empty($data['employee_one'])){
  150. $insert = [];
  151. foreach ($data['employee_one'] as $value){
  152. $insert[] = [
  153. 'contacts_id' => $model->id,
  154. 'employee_id' => $value,
  155. 'type' => ContactsInfo::type_three,
  156. 'crt_time' => $time,
  157. ];
  158. }
  159. ContactsInfo::insert($insert);
  160. }
  161. if(! empty($data['employee_two'])){
  162. $insert = [];
  163. foreach ($data['employee_two'] as $value){
  164. $insert[] = [
  165. 'contacts_id' => $model->id,
  166. 'employee_id' => $value,
  167. 'type' => ContactsInfo::type_four,
  168. 'crt_time' => $time,
  169. ];
  170. }
  171. ContactsInfo::insert($insert);
  172. }
  173. DB::commit();
  174. }catch (\Exception $exception){
  175. DB::rollBack();
  176. return [false,$exception->getMessage()];
  177. }
  178. return [true,''];
  179. }
  180. public function contactsDel($data){
  181. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  182. try {
  183. DB::beginTransaction();
  184. Contacts::whereIn('id',$data['id'])->update([
  185. 'del_time'=> time()
  186. ]);
  187. ContactsInfo::where('del_time',0)
  188. ->where('contacts_id',$data['id'])
  189. ->update(['del_time' => time()]);
  190. DB::commit();
  191. }catch (\Exception $exception){
  192. DB::rollBack();
  193. return [false,$exception->getMessage()];
  194. }
  195. return [true,''];
  196. }
  197. public function contactsDetail($data){
  198. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  199. $customer = Contacts::where('del_time',0)
  200. ->where('id',$data['id'])
  201. ->first();
  202. if(empty($customer)) return [false,'联系人不存在或已被删除'];
  203. $customer = $customer->toArray();
  204. $customer['contacts'] = $customer['customers'] = $customer['employee_one'] = $customer['employee_two'] = [];
  205. $array = [
  206. $customer['decision'],
  207. $customer['grade'],
  208. ];
  209. $basic_map = BasicType::whereIn('id',$array)
  210. ->pluck('title','id')
  211. ->toArray();
  212. $customer = [$customer];
  213. foreach ($customer as $key => $value){
  214. $customer[$key]['decision_title'] = $basic_map[$value['decision']] ?? '';
  215. $customer[$key]['grade_title'] = $basic_map[$value['grade']] ?? '';
  216. }
  217. $customer = $customer[0];
  218. $customer_info = ContactsInfo::where('del_time',0)
  219. ->where('contacts_id',$customer['id'])
  220. ->select('id','contacts_id','contact_type','contact_info','employee_id','type')
  221. ->get()->toArray();
  222. foreach ($customer_info as $value){
  223. if($value['type'] == ContactsInfo::type_one){
  224. $customer['contacts'][] = [
  225. 'id' => $value['contact_type'],
  226. 'info' => $value['contact_info']
  227. ];
  228. }elseif ($value['type'] == ContactsInfo::type_two){
  229. $customer['customers'][] = $value['employee_id'];
  230. }elseif ($value['type'] == ContactsInfo::type_three){
  231. $customer['employee_one'][] = $value['employee_id'];
  232. }elseif ($value['type'] == ContactsInfo::type_four){
  233. $customer['employee_two'][] = $value['employee_id'];
  234. }
  235. }
  236. return [true, $customer];
  237. }
  238. public function contactsList($data,$user){
  239. $model = Contacts::where('del_time',0)
  240. ->select('title','id','img','mailbox','qq','postal_code','sex','gregorian_bir','lunar_bir','hobby','address1','address2','crt_id','crt_time','mark','importance','depart','job','decision','grade')
  241. ->orderby('id', 'desc');
  242. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  243. $list = $this->limit($model,'',$data);
  244. $list = $this->fillData($list);
  245. return [true, $list];
  246. }
  247. public function contactsRule($data, $is_add = true){
  248. if(empty($data['title'])) return [false,'联系人名不能为空'];
  249. if(empty($data['contacts'])) return [false,'联系方式不能为空'];
  250. if(empty($data['customers'])) return [false,'关联客户不能为空'];
  251. if(empty($data['employee_one'])) return [false,'负责人不能为空'];
  252. if($is_add){
  253. }else{
  254. }
  255. return [true, $data];
  256. }
  257. public function fillData($data){
  258. if(empty($data['data'])) return $data;
  259. $array = array_unique(array_merge_recursive(array_column($data['data'],'decision'),array_column($data['data'],'grade')));
  260. $basic_map = BasicType::whereIn('id',$array)
  261. ->pluck('title','id')
  262. ->toArray();
  263. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  264. ->pluck('emp_name','id')
  265. ->toArray();
  266. foreach ($data['data'] as $key => $value){
  267. $data['data'][$key]['decision_title'] = $basic_map[$value['decision']] ?? '';
  268. $data['data'][$key]['grade_title'] = $basic_map[$value['grade']] ?? '';
  269. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  270. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  271. }
  272. return $data;
  273. }
  274. }