CompanyService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: michaelwu
  5. * Date: 2019/07/09
  6. * Time: 11:22
  7. */
  8. namespace App\Services\Crm;
  9. use App\Exceptions\ResponseException;
  10. use App\Models\Company;
  11. use App\Models\Jobs;
  12. use App\Models\JobsContact;
  13. use App\Repositories\CompanyRepository;
  14. use App\Repositories\JobsRepository;
  15. use App\Repositories\TplRepository;
  16. use App\Repositories\CompanyTplRepository;
  17. use App\Repositories\MemberLogRepository;
  18. use App\Services\Common\TaskService;
  19. use App\Services\Common\SmsService;
  20. use App\Services\Common\PmsService;
  21. use App\Models\AuditReason;
  22. use Aix\Sms\Contracts\Smser;
  23. use Exception;
  24. use Illuminate\Support\Facades\Cache;
  25. use Illuminate\Support\Facades\DB;
  26. use Illuminate\Validation\Rule;
  27. class CompanyService
  28. {
  29. protected $companyRepository;
  30. protected $jobsRepository;
  31. protected $taskService;
  32. protected $smsService;
  33. protected $pmsService;
  34. protected $tplRepository;
  35. protected $companyTplRepository;
  36. protected $memberLogRepository;
  37. /**
  38. * CompanyService constructor.
  39. */
  40. public function __construct(CompanyRepository $companyRepository, JobsRepository $jobsRepository, TaskService $taskService, SmsService $smsService, PmsService $pmsService, TplRepository $tplRepository, CompanyTplRepository $companyTplRepository, MemberLogRepository $memberLogRepository)
  41. {
  42. $this->companyRepository = $companyRepository;
  43. $this->jobsRepository = $jobsRepository;
  44. $this->taskService = $taskService;
  45. $this->smsService = $smsService;
  46. $this->pmsService = $pmsService;
  47. $this->tplRepository = $tplRepository;
  48. $this->companyTplRepository = $companyTplRepository;
  49. $this->memberLogRepository = $memberLogRepository;
  50. }
  51. public function getCompany($company_id)
  52. {
  53. return $this->companyRepository->getCompanyInfo(['id'=>$company_id]);
  54. }
  55. public function getTpl($tpl_id)
  56. {
  57. return $this->tplRepository->find($tpl_id);
  58. }
  59. public function getCompanyTpl($where)
  60. {
  61. return $this->companyTplRepository->getTpl($where);
  62. }
  63. //修改企业信息
  64. public function companySave($data)
  65. {
  66. if(empty($data['address'])){
  67. return ['status'=>0,'msg'=>'联系地址不能为空'];
  68. }
  69. $apiAddress = apiAddress($data['address']);
  70. if (!$apiAddress) {
  71. return ['status'=>0,'msg'=>'联系地址定位不准确'];
  72. }
  73. $id = $data['id'];
  74. $companyInfo = $this->companyRepository->find($id);
  75. if (!$companyInfo) {
  76. return ['status'=>0,'msg'=>'企业会员不存在'];
  77. }
  78. $companyInfo->uid = $companyInfo->id;
  79. ///// 公司名称、联系人、手机号码、固定电话、电子邮箱、通讯地址、企业介绍
  80. if ($companyInfo->companyname != $data['companyname']) {
  81. //判断企业名称是否可以相同
  82. if (config('aix.companyset.comset.other_set.company_repeat') != 1) {
  83. if (!$this->companyRepository->checkUniaue('companyname', $data['companyname'], $companyInfo->id)) {
  84. return ['status'=>0,'msg'=>'公司名称已存在'];
  85. }
  86. }
  87. }
  88. //判断联系人信息是否修改
  89. if (!$data['contact']) {
  90. return ['status'=>0,'msg'=>'联系人信息不能为空!'];
  91. }
  92. //判断手机号码或者固定电话是否填写,至少二选一
  93. if (!$data['telephone'] && !$data['landline_tel']) {
  94. return ['status'=>0,'msg'=>'手机号码、固定电话至少二选一!'];
  95. }
  96. if ($companyInfo->mobile != $data['telephone'] && $data['telephone']) {
  97. //判断电话是否已存在
  98. if (!$this->companyRepository->checkUniaue('mobile', $data['telephone'], $companyInfo->id)) {
  99. return ['status'=>0,'msg'=>'手机号码已存在!'];
  100. }
  101. }
  102. //判断固定电话格式
  103. if ($data['landline_tel'] && !preg_match("/^(([0\+]\d{1,9}-)?(\d{2,6})-)(\d{1,12})(-(\d{1,8}))?$/", $data['landline_tel'])) {
  104. return ['status'=>0,'msg'=>'固定电话格式不正确!'];
  105. }
  106. //判断企业介绍是否填写
  107. if (!$data['contents']) {
  108. return ['status'=>0,'msg'=>'企业介绍不能为空!'];
  109. }
  110. //判断邮箱是否填写
  111. if (!$data['email']) {
  112. return ['status'=>0,'msg'=>'电子邮箱不能为空!'];
  113. }
  114. //判断邮件是否重复
  115. if (!$this->companyRepository->checkUniaue('email', $data['email'], $companyInfo->id)) {
  116. return ['status'=>0,'msg'=>'电子邮箱已存在!'];
  117. }
  118. DB::beginTransaction();
  119. try {
  120. //修改企业会员信息
  121. $updateData['companyname'] = $data['companyname'];
  122. $updateData['contents'] = $data['contents'];
  123. $updateData['address'] = $data['address'];
  124. $updateData['contact'] = $data['contact'];
  125. $updateData['mobile'] = $data['telephone'];
  126. $updateData['landline_tel'] = $data['landline_tel'];
  127. $updateData['map_x'] = $apiAddress['lng'];
  128. $updateData['map_y'] = $apiAddress['lat'];
  129. $updateData['email'] = $data['email'];
  130. $com_rst = $this->companyRepository->companySave($updateData, $id);
  131. if (!$com_rst) {
  132. return ['status'=>0,'msg'=>'操作失败!'];
  133. }
  134. //修改职位信息并推到搜索引擎
  135. $updateJob['company_name'] = $data['companyname'];
  136. $updateJob['map_x'] = $apiAddress['lng'];
  137. $updateJob['map_y'] = $apiAddress['lat'];
  138. $jid = $this->jobsRepository->findWhere(['company_id'=>$id])->pluck('id')->toArray();
  139. if ($this->jobsRepository->modifyData($jid, $updateJob)) {
  140. $condition1 = [['whereIn','id', $jid]];
  141. event_search_update(Jobs::class, $condition1, 'update');
  142. }
  143. //修改职位的联系信息
  144. $contactInfo = [];
  145. if($jid){
  146. $contactInfo['telephone'] = $data['telephone'];
  147. $contactInfo['email'] = $data['email'];
  148. $contactInfo['contact'] = $data['contact'];
  149. $contactInfo['qq'] = $companyInfo->qq;
  150. $contactInfo['landline_tel'] = $data['landline_tel'];
  151. $contactInfo['address'] = $data['address'];
  152. $contactInfo['contact_show'] = $companyInfo->contact_show;
  153. $contactInfo['telephone_show'] = $companyInfo->telephone_show;
  154. $contactInfo['email_show'] = $companyInfo->email_show;
  155. $contactInfo['landline_tel_show'] = $companyInfo->landline_tel_show;
  156. JobsContact::whereIn('job_id',$jid)->update($contactInfo);
  157. }
  158. //企业信息推到搜索引擎上
  159. event_search_update(Company::class, (string)$id, 'update');
  160. DB::commit();
  161. } catch (\Exception $e) {
  162. DB::rollback();
  163. return ['status'=>0,'msg'=>'操作失败!'];
  164. }
  165. //增加操作记录
  166. $this->taskService->doTask(27, $id, 1);
  167. $this->memberLogRepository->createLog($companyInfo,1004,"");
  168. return ['status'=>1,'msg'=>'操作成功!'];
  169. }
  170. public function auditCompany($params)
  171. {
  172. $ids = $params['id'];
  173. $id = explode(',', $ids);
  174. $reason = $params['reason'];
  175. $audit = $params['audit'];
  176. $audit_username = $params['audit_username'];
  177. if ($this->companyRepository->updateCompanies([],['id'=>$id], ['audit'=>$audit])) {
  178. event_search_update(Company::class, [['whereIn','id', $id]], 'update');
  179. //获取企业职位信息
  180. $jid = $this->jobsRepository->findWhereIn('company_id', $id, ['id']);
  181. if ($jid) {
  182. $jids = array_column($jid->toArray(), 'id');
  183. //修改职位中企业状态字段
  184. if ($this->jobsRepository->modifyCom($jids, ['company_audit'=>$audit])) {
  185. $condition1 = [['whereIn','id', $jids]];
  186. event_search_update(Jobs::class, $condition1, 'update');
  187. }
  188. }
  189. //如果认证状态为通过(1)完成营业招聘审核
  190. if ($audit == 1) {
  191. foreach ($id as $key => $val) {
  192. $this->taskService->doTask(30, $val, 1);
  193. }
  194. }
  195. switch ($audit) {
  196. case 1:
  197. $html = "企业通过审核";
  198. $alias =Smser::TEMPLATE_SMS_LICENSEALLOW;
  199. break;
  200. case 3:
  201. $html = "企业未通过审核";
  202. $alias=Smser::TEMPLATE_SMS_LICENSENOTALLOW;
  203. break;
  204. }
  205. foreach ($id as $key => $val) {
  206. $companyInfo = $this->companyRepository->find($val, ['mobile']);
  207. if ($companyInfo->mobile) {
  208. $this->smsService->sendSms($companyInfo->mobile, $alias, ['sitename'=>config('aix.system.site.site.site_name'),'sitedomain'=>config('aix.system.site.site.site_domain')]);
  209. }
  210. }
  211. $auditData= [];
  212. $auditData['ids'] = $id;
  213. $auditData['status'] = $audit;
  214. $auditData['type'] = 8;
  215. $auditData['reason'] = $reason;
  216. $auditData['audit_man'] = $audit_username;
  217. AuditReason::addData($auditData);
  218. //站内信
  219. switch ($audit) {
  220. case 1:
  221. $html = "企业通过审核";
  222. break;
  223. case 3:
  224. $html = "企业未通过审核";
  225. break;
  226. }
  227. foreach ($id as $key => $val) {
  228. $com = $this->companyRepository->findWhere(['id'=>$val], ['*']);
  229. $insert_data[$key] = array(
  230. 'utype' => 1,
  231. 'msgtype' => 1,
  232. 'msgfromuid' => 0,
  233. 'msgfrom' => $audit_username,
  234. 'msgtoname' =>$com[0]->username,
  235. 'msgtouid' => $val,
  236. 'message' => $html.'【备注】'.$reason,
  237. 'new' => 1,
  238. 'created_at' =>date('Y-m-d H:i:s', time()),
  239. 'updated_at' =>date('Y-m-d H:i:s', time()),
  240. );
  241. }
  242. $this->pmsService->addBatchPms($insert_data);
  243. return ['status'=>10,'msg'=>'设置成功!'];
  244. } else {
  245. return ['status'=>0,'msg'=>'设置失败!'];
  246. }
  247. }
  248. }