BusinessOpportunityService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BusinessOpportunity;
  4. use App\Model\BusinessOpportunityInfo;
  5. use App\Model\Employee;
  6. use App\Model\FollowUpRecord;
  7. use Illuminate\Support\Facades\DB;
  8. class BusinessOpportunityService extends Service
  9. {
  10. public function BusinessOpportunityEdit($data,$user){
  11. list($status,$msg) = $this->BusinessOpportunityRule($data,$user, false);
  12. if(!$status) return [$status,$msg];
  13. $params = $this->getDataFile($data);
  14. (new OperationLogService())->setOperationList($params,$user,2);
  15. try {
  16. DB::beginTransaction();
  17. $model = BusinessOpportunity::where('id',$data['id'])->first();
  18. $model->title = $data['title'] ?? "";
  19. $model->relationship_level = $data['relationship_level'] ?? 0;
  20. $model->is_company = $data['is_company'] ?? 0;
  21. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  22. $model->address2 = $data['address2'] ?? '';
  23. $model->mark = $data['mark'] ?? '';
  24. $model->intention = $data['intention'] ?? 0;
  25. $model->customer_type = $data['customer_type'] ?? '';
  26. $model->save();
  27. $time = time();
  28. $old = BusinessOpportunityInfo::where('del_time',0)
  29. ->where('business_opportunity_id',$data['id'])
  30. ->whereIn('type',[BusinessOpportunityInfo::type_five,BusinessOpportunityInfo::type_six])
  31. ->select('file')
  32. ->get()->toArray();
  33. $old = array_column($old,'file');
  34. BusinessOpportunityInfo::where('del_time',0)
  35. ->where('business_opportunity_id',$data['id'])
  36. ->update(['del_time' => $time]);
  37. if(! empty($data['customer_contact'])){
  38. $insert = [];
  39. foreach ($data['customer_contact'] as $value){
  40. $insert[] = [
  41. 'business_opportunity_id' => $model->id,
  42. 'contact_type' => $value['type'],
  43. 'contact_info' => $value['info'],
  44. 'type' => BusinessOpportunityInfo::type_one,
  45. 'crt_time' => $time,
  46. ];
  47. }
  48. BusinessOpportunityInfo::insert($insert);
  49. }
  50. $new = [];
  51. if(! empty($data['img'])){
  52. $insert = [];
  53. foreach ($data['img'] as $value){
  54. $insert[] = [
  55. 'business_opportunity_id' => $model->id,
  56. 'file' => $value['url'],
  57. 'type' => BusinessOpportunityInfo::type_five,
  58. 'name' => $value['name'],
  59. 'crt_time' => $time,
  60. ];
  61. if(in_array($value['url'], $old)) {
  62. foreach ($old as $o_k => $o_v){
  63. if($o_v == $value['url']) unset($old[$o_k]);
  64. }
  65. }else{
  66. $new[] = $value['url'];
  67. }
  68. }
  69. BusinessOpportunityInfo::insert($insert);
  70. }
  71. if(! empty($data['file'])){
  72. $insert = [];
  73. foreach ($data['file'] as $value){
  74. $insert[] = [
  75. 'business_opportunity_id' => $model->id,
  76. 'file' => $value['url'],
  77. 'type' => BusinessOpportunityInfo::type_six,
  78. 'name' => $value['name'],
  79. 'crt_time' => $time,
  80. ];
  81. if(in_array($value['url'], $old)) {
  82. foreach ($old as $o_k => $o_v){
  83. if($o_v == $value['url']) unset($old[$o_k]);
  84. }
  85. }else{
  86. $new[] = $value['url'];
  87. }
  88. }
  89. BusinessOpportunityInfo::insert($insert);
  90. }
  91. DB::commit();
  92. }catch (\Exception $exception){
  93. DB::rollBack();
  94. return [false,$exception->getMessage()];
  95. }
  96. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  97. }
  98. public function BusinessOpportunityAdd($data,$user){
  99. list($status,$msg) = $this->BusinessOpportunityRule($data, $user);
  100. if(! $status) return [$status,$msg];
  101. try {
  102. DB::beginTransaction();
  103. $model = new BusinessOpportunity();
  104. $model->order_number = $data['order_number'];
  105. $model->title = $data['title'] ?? "";
  106. $model->relationship_level = $data['relationship_level'] ?? 0;
  107. $model->is_company = $data['is_company'] ?? 0;
  108. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  109. $model->address2 = $data['address2'] ?? '';
  110. $model->mark = $data['mark'] ?? '';
  111. $model->intention = $data['intention'] ?? 0;
  112. $model->customer_type = $data['customer_type'] ?? '';
  113. $model->crt_id = $user['id'];
  114. $model->save();
  115. $time = time();
  116. if(! empty($data['customer_contact'])){
  117. $insert = [];
  118. foreach ($data['customer_contact'] as $value){
  119. $insert[] = [
  120. 'business_opportunity_id' => $model->id,
  121. 'contact_type' => $value['type'],
  122. 'contact_info' => $value['info'],
  123. 'type' => BusinessOpportunityInfo::type_one,
  124. 'crt_time' => $time,
  125. ];
  126. }
  127. BusinessOpportunityInfo::insert($insert);
  128. }
  129. $new = [];
  130. if(! empty($data['img'])){
  131. $insert = [];
  132. foreach ($data['img'] as $value){
  133. $insert[] = [
  134. 'business_opportunity_id' => $model->id,
  135. 'file' => $value['url'],
  136. 'type' => BusinessOpportunityInfo::type_five,
  137. 'name' => $value['name'],
  138. 'crt_time' => $time,
  139. ];
  140. if(! empty($value['url'])) $new[] = $value['url'];
  141. }
  142. BusinessOpportunityInfo::insert($insert);
  143. }
  144. if(! empty($data['file'])){
  145. $insert = [];
  146. foreach ($data['file'] as $value){
  147. $insert[] = [
  148. 'business_opportunity_id' => $model->id,
  149. 'file' => $value['url'],
  150. 'type' => BusinessOpportunityInfo::type_six,
  151. 'name' => $value['name'],
  152. 'crt_time' => $time,
  153. ];
  154. if(! empty($value['url'])) $new[] = $value['url'];
  155. }
  156. BusinessOpportunityInfo::insert($insert);
  157. }
  158. DB::commit();
  159. }catch (\Exception $exception){
  160. DB::rollBack();
  161. return [false,$exception->getMessage()];
  162. }
  163. (new OperationLogService())->setOperationList($data,$user);
  164. return [true, ['file' => ['new' => $new]]];
  165. }
  166. public function BusinessOpportunityDel($data){
  167. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  168. if($data['id'] < 0) return [false, '系统数据,操作失败!'];
  169. try {
  170. DB::beginTransaction();
  171. BusinessOpportunity::where('id',$data['id'])->update([
  172. 'del_time'=> time()
  173. ]);
  174. $old = BusinessOpportunityInfo::where('del_time',0)
  175. ->where('business_opportunity_id',$data['id'])
  176. ->whereIn('type',[BusinessOpportunityInfo::type_five,BusinessOpportunityInfo::type_six])
  177. ->select('file')
  178. ->get()->toArray();
  179. $old = array_column($old,'file');
  180. BusinessOpportunityInfo::where('del_time',0)
  181. ->where('business_opportunity_id',$data['id'])
  182. ->update(['del_time' => time()]);
  183. DB::commit();
  184. }catch (\Exception $exception){
  185. DB::rollBack();
  186. return [false,$exception->getMessage()];
  187. }
  188. return [true, ['file' => ['old' => $old]]];
  189. }
  190. public function BusinessOpportunityDetail($data,$user){
  191. if($this->isEmpty($data,'id')) return [false,'请选择商机资源'];
  192. $customer = BusinessOpportunity::where('del_time',0)
  193. ->where('id',$data['id'])
  194. ->first();
  195. if(empty($customer)) return [false,'商机不存在或已被删除'];
  196. $customer = $customer->toArray();
  197. //地址
  198. $address_map = config('address');
  199. $address_str = [];
  200. if(! empty($customer['address1'])) {
  201. $tmp = json_decode($customer['address1'],true);
  202. $this->findLabelsByValue($address_map,$tmp,$address_str);
  203. $customer['address1'] = $tmp;
  204. $tmp = implode(' ',$address_str);
  205. $tmp .= ' ' . $customer['address2'];
  206. $address = $tmp;
  207. }else{
  208. $address = $customer['address2'];
  209. }
  210. $customer['address'] = $address;
  211. $customer['customer_contact'] = $customer['img'] = $customer['file'] = [];
  212. //详情
  213. $customer_info = BusinessOpportunityInfo::where('del_time',0)
  214. ->where('business_opportunity_id',$customer['id'])
  215. ->select('id','business_opportunity_id','contact_type','contact_info','data_id','file','type','name')
  216. ->get()->toArray();
  217. $emp_id = [];
  218. $emp_id[] = $customer['crt_id'];
  219. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  220. ->pluck('emp_name','id')
  221. ->toArray();
  222. $fileUploadService = new FileUploadService();
  223. foreach ($customer_info as $value){
  224. if($value['type'] == BusinessOpportunityInfo::type_one){
  225. $tmp = [
  226. 'id' => $value['contact_type'],
  227. 'type' => $value['contact_type'],
  228. 'type_title' => BusinessOpportunityInfo::$contact_type[$value['contact_type']] ?? "",
  229. 'info' => $value['contact_info']
  230. ];
  231. $customer['customer_contact'][] = $tmp;
  232. }elseif ($value['type'] == BusinessOpportunityInfo::type_five){
  233. $tmp = [
  234. 'url' => $value['file'],
  235. 'name' => $value['name'],
  236. 'show_url' => $fileUploadService->getFileShow($value['file']),
  237. ];
  238. $customer['img'][] = $tmp;
  239. }elseif ($value['type'] == BusinessOpportunityInfo::type_six){
  240. $tmp = [
  241. 'url' => $value['file'],
  242. 'name' => $value['name'],
  243. 'show_url' => $fileUploadService->getFileShow($value['file']),
  244. ];
  245. $customer['file'][] = $tmp;
  246. }
  247. }
  248. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  249. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  250. return [true, $customer];
  251. }
  252. public function BusinessOpportunityList($data,$user){
  253. $model = BusinessOpportunity::where('del_time',0)
  254. ->select('title','id','order_number','intention','customer_type','address1','address2','crt_id','crt_time','mark','relationship_level','is_company')
  255. ->orderby('id', 'desc');
  256. if(! empty($data['id'])) $model->where('id', $data['id']);
  257. if(isset($data['relationship_level'])) $model->where('relationship_level', $data['relationship_level']);
  258. if(isset($data['is_company'])) $model->where('is_company', $data['is_company']);
  259. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  260. if(! empty($data['time_type'])) {
  261. if($data['time_type'] == 1) {
  262. $start = strtotime('today');
  263. $end = strtotime('tomorrow') - 1;
  264. }elseif ($data['time_type'] == 2){
  265. $start = strtotime('this week',strtotime('today'));
  266. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  267. }
  268. if(! empty($start) && ! empty($end)) {
  269. $model->where('crt_time','>=',$start);
  270. $model->where('crt_time','<=',$end);
  271. }
  272. }
  273. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  274. if(isset($data['intention'])) $model->where('intention',$data['intention']);
  275. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  276. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  277. $model->where('crt_time','>=',$return[0]);
  278. $model->where('crt_time','<=',$return[1]);
  279. }
  280. if(! empty($data['contact_info'])){
  281. $customer_info = BusinessOpportunityInfo::where('del_time',0)
  282. ->where("type",BusinessOpportunityInfo::type_one)
  283. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  284. ->select('business_opportunity_id')
  285. ->get()->toArray();
  286. $model->whereIn('id',array_column($customer_info,'business_opportunity_id'));
  287. }
  288. $list = $this->limit($model,'',$data);
  289. $list = $this->fillData($list,$data);
  290. return [true, $list];
  291. }
  292. public function BusinessOpportunityRule(&$data, $user, $is_add = true){
  293. if(empty($data['title'])) return [false,'商机名称不能为空'];
  294. if($is_add){
  295. $data['order_number'] = (new OrderNoService())->createOrderNumber(BusinessOpportunity::$prefix);
  296. }else{
  297. if(empty($data['id'])) return [false,'ID不能为空'];
  298. }
  299. return [true, ''];
  300. }
  301. public function fillData($data,$ergs){
  302. if(empty($data['data'])) return $data;
  303. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  304. ->pluck('emp_name','id')
  305. ->toArray();
  306. //跟进记录
  307. $record_array = (new FollowUpRecordService())->getVisitDataOfTime(array_column($data['data'],'id'), FollowUpRecord::type_two);
  308. $customer_info = BusinessOpportunityInfo::where('del_time',0)
  309. ->whereIn('business_opportunity_id',array_column($data['data'],'id'))
  310. ->whereIn('type',[BusinessOpportunityInfo::type_one])
  311. ->select('contact_type as type','contact_info as info','business_opportunity_id','data_id')
  312. ->get()->toArray();
  313. //联系方式
  314. $customer_info_map = [];
  315. foreach ($customer_info as $value){
  316. $value['type_title'] = BusinessOpportunityInfo::$contact_type[$value['type']] ?? "";
  317. $customer_info_map[$value['business_opportunity_id']][] = $value;
  318. }
  319. $address_map = config('address');
  320. foreach ($data['data'] as $key => $value){
  321. $address_str = [];
  322. if(! empty($value['address1'])) {
  323. $tmp = json_decode($value['address1'],true);
  324. $this->findLabelsByValue($address_map,$tmp,$address_str);
  325. $tmp = implode(' ',$address_str);
  326. $tmp .= ' ' . $value['address2'];
  327. $address = $tmp;
  328. }else{
  329. $address = $value['address2'];
  330. }
  331. $data['data'][$key]['address'] = $address;
  332. $data['data'][$key]['is_company_title'] = $value['is_company'] ? '是' : '否';
  333. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  334. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  335. $record_tmp = $record_array[$value['id']] ?? "";
  336. $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";
  337. $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";
  338. $customer_tmp = $customer_info_map[$value['id']] ?? [];
  339. $data['data'][$key]['customer_detail'] = $customer_tmp;
  340. }
  341. return $data;
  342. }
  343. }