CompanyService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2018/3/22
  6. * Time: 下午6:00
  7. */
  8. namespace App\Modules\Channel\Services;
  9. use App\Modules\Channel\Models\Company;
  10. use DB;
  11. class CompanyService
  12. {
  13. /**
  14. * 添加一个公司
  15. * @param array $data
  16. * @return mixed
  17. */
  18. public static function addCompany($data = [])
  19. {
  20. return Company::create($data);
  21. }
  22. /**
  23. * 更新公司
  24. * @param $id
  25. * @param array $data [name, is_important, distribution_manages_id]
  26. * @return mixed
  27. */
  28. public static function updateCompany($id, $data = [])
  29. {
  30. \Log::info('updateCompany---------------------------------');
  31. \Log::info($data);
  32. $company = self::getCompany($id);
  33. if ($company) {
  34. if (isset($data['name']) && $data['name']) {
  35. $company->name = $data['name'];
  36. }
  37. if (isset($data['is_important']) && is_numeric($data['is_important'])) {
  38. $company->is_important = $data['is_important'];
  39. }
  40. if (isset($data['distribution_manages_id']) && is_numeric($data['distribution_manages_id'])) {
  41. $company->distribution_manages_id = $data['distribution_manages_id'];
  42. }
  43. if (isset($data['fans_gender']) && $data['fans_gender']) {
  44. $company->fans_gender = $data['fans_gender'];
  45. }
  46. if (isset($data['city']) && $data['city']) {
  47. $company->city = $data['city'];
  48. }
  49. $company->save();
  50. }
  51. return self::getCompany($id);
  52. }
  53. /**
  54. * 根据ID查询公司
  55. * @param $id
  56. * @return mixed
  57. */
  58. public static function getCompany($id)
  59. {
  60. $sqlObj = Company::select(
  61. 'companies.id',
  62. 'companies.name',
  63. 'companies.city',
  64. 'companies.fans_gender',
  65. 'companies.is_important',
  66. 'companies.distribution_manages_id',
  67. 'companies.created_at',
  68. 'companies.updated_at',
  69. DB::raw('IFNULL(distribution_manages.nickname, "") as distribution_manages_nickname')
  70. )
  71. ->leftjoin('distribution_manages', 'distribution_manages.id', '=', 'companies.distribution_manages_id')
  72. ->where('companies.id', $id);
  73. return $sqlObj->first();
  74. //return Company::find($id);
  75. }
  76. /**
  77. * 根据名称查找公司
  78. * @param $name
  79. * @return mixed
  80. */
  81. public static function findByName($name)
  82. {
  83. return Company::where('name', $name)->first();
  84. }
  85. /**
  86. * 通过 distribution_manages_id 获取商务下的公司数量
  87. * @param $distribution_manages_id
  88. * @return mixed
  89. */
  90. public static function getCompanyCountByManageId($distribution_manages_id)
  91. {
  92. return Company::where('distribution_manages_id', $distribution_manages_id)->count();
  93. }
  94. /**
  95. * 获取公司列表
  96. * @param array $data
  97. * @param bool $isAll
  98. * @return mixed
  99. */
  100. public static function getList($data = [], $isAll = false, $manager_id = '')
  101. {
  102. $search_object = Company::select(
  103. 'companies.id',
  104. 'companies.name',
  105. 'companies.city',
  106. 'companies.is_important',
  107. 'companies.distribution_manages_id',
  108. 'companies.fans_gender',
  109. 'companies.created_at',
  110. 'companies.updated_at',
  111. DB::raw('IFNULL(distribution_manages.nickname, "") as distribution_manages_nickname')
  112. )
  113. ->leftjoin('distribution_manages', 'distribution_manages.id', '=', 'companies.distribution_manages_id')
  114. ->orderBy('created_at', 'desc');
  115. // $search_object = Company::orderBy('created_at', 'desc');
  116. if ($manager_id) {
  117. $search_object->where('distribution_manages.id', $manager_id);
  118. }
  119. if (isset($data['search_name']) && $data['search_name']) {
  120. $search_object->where(function ($query) use ($data) {
  121. $query->where('companies.name', 'like', "%" . $data['search_name'] . "%");
  122. });
  123. }
  124. if (isset($data['is_important']) && is_numeric($data['is_important'])) {
  125. $search_object->where('companies.is_important', $data['is_important']);
  126. }
  127. if (isset($data['distribution_manages_id']) && is_numeric($data['distribution_manages_id'])) {
  128. $search_object->where('companies.distribution_manages_id', $data['distribution_manages_id']);
  129. }
  130. if ($isAll) {
  131. return $search_object->get();
  132. } else {
  133. return $search_object->paginate();
  134. }
  135. }
  136. public static function getCompanyServiceAccounts($company_id)
  137. {
  138. return Company::leftjoin('channel_users', 'channel_users.company_id', '=', 'companies.id')
  139. ->leftjoin('distribution_channels', 'distribution_channels.channel_user_id', '=', 'channel_users.id')
  140. ->leftjoin('official_accounts', 'official_accounts.distribution_channel_id', '=', 'distribution_channels.id')
  141. ->select(['official_accounts.id', 'official_accounts.nickname as official_account_name', 'distribution_channels.id as distribution_channel_id', 'distribution_channels.nickname as channel_name'])
  142. ->where('companies.id', $company_id)
  143. ->get();
  144. }
  145. }