CompanyRepository.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace App\Repositories;
  3. use App\Exceptions\ResponseException;
  4. use App\Models\Category;
  5. use App\Models\Company;
  6. use App\Models\MembersLog;
  7. use App\Validators\Rules\MobileRule;
  8. use Illuminate\Container\Container as Application;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\Hash;
  11. use Illuminate\Support\Facades\Log;
  12. use Illuminate\Support\Facades\Schema;
  13. use Prettus\Repository\Eloquent\BaseRepository;
  14. use Prettus\Repository\Criteria\RequestCriteria;
  15. use Illuminate\Support\Facades\DB;
  16. class CompanyRepository extends BaseRepository
  17. {
  18. /**
  19. * Specify Model class name
  20. *
  21. * @return string
  22. */
  23. protected $membersSetmealLogRepository;
  24. public function __construct(Application $app,MembersSetmealLogRepository $membersSetmealLogRepository)
  25. {
  26. $this->membersSetmealLogRepository=$membersSetmealLogRepository;
  27. parent::__construct($app);
  28. }
  29. public function model()
  30. {
  31. return Company::class;
  32. }
  33. /**
  34. * Boot up the repository, pushing criteria
  35. */
  36. public function boot()
  37. {
  38. $this->pushCriteria(app(RequestCriteria::class));
  39. }
  40. /**
  41. * @param array $column
  42. * @return mixed
  43. */
  44. public function getCompanyColumn($id, $column = ['*'])
  45. {
  46. return $this->model->where('id', $id)->select($column)->first()->toArray();
  47. }
  48. public function auth()
  49. {
  50. return $this->find(auth('web-company')->user()->id, ['audit']);
  51. }
  52. public function getCompanyInfoByID($id)
  53. {
  54. $companyInfo = $this->model->find($id, ['*']);
  55. if (!$companyInfo) {
  56. throw new ResponseException('参数错误', '400');
  57. }
  58. $tag = Category::categoryType('AIX_jobtag');//这个要改
  59. $companyInfo->tag1 = implode(',', $companyInfo->tag);
  60. foreach ($companyInfo->tag as $val) {
  61. if ($val) {
  62. $companyInfo->tag_cn.=$tag[$val].',';
  63. }
  64. }
  65. $landline = $companyInfo->landline_tel?explode('-', $companyInfo->landline_tel):array();
  66. if (is_array($landline)) {
  67. $companyInfo->landline_first = isset($landline[0]) ? ($landline[0] == 0 ? '' : $landline[0]) : "";
  68. $companyInfo->landline_next = isset($landline[1]) ? ($landline[1] == 0 ? '' : $landline[1]) : "";
  69. $companyInfo->landline_last = isset($landline[2]) ? ($landline[2] == 0 ? '' : $landline[2]) : "";
  70. }
  71. return $companyInfo;
  72. }
  73. /**
  74. * 企业保存
  75. * @param $data
  76. * @return mixed
  77. * @throws \Prettus\Validator\Exceptions\ValidatorException
  78. */
  79. public function companySave($data, $id)
  80. {
  81. return $this->update($data, $id);
  82. }
  83. /**
  84. * @param array $data 需要修改的字段
  85. * @return mixed
  86. * @throws \Prettus\Validator\Exceptions\ValidatorException
  87. */
  88. public function save($data, $id)
  89. {
  90. return $this->update($data, $id);
  91. }
  92. public function modifyJobsNum($uid, $type, $count)
  93. {
  94. switch ($type) {
  95. case 1:
  96. $increment = 'increment';
  97. break;
  98. case 2:
  99. $increment = 'decrement';
  100. break;
  101. }
  102. return $this->model->where('id', $uid)->$increment('jobs', $count);
  103. }
  104. public function verifyCode($data, $id)
  105. {
  106. return $this->update($data, $id);
  107. }
  108. /**
  109. * @param $account string
  110. * @return null|Company
  111. */
  112. public function getCompanyByAccount($account)
  113. {
  114. if (validator_check($account, 'email')) {
  115. return $this->model->where('email', $account)->first();
  116. }
  117. if (validator_check($account, new MobileRule())) {
  118. return $this->model->where('mobile', $account)->first();
  119. }
  120. return $this->model->where('username', $account)->orWhere('organization_code', $account)->first();
  121. }
  122. //获取企业信息
  123. public function getCompanyInfo($where)
  124. {
  125. return $this->model->where($where)->first();
  126. }
  127. //获取指定行业的企业
  128. public function getTradeCompanies($where, $trades = array(), $field = '*', $order = 'refresh_time desc', $limit = '10')
  129. {
  130. if ($trades) {
  131. return $this->model->select(DB::raw($field))->where($where)->whereIn('trade', $trades)->orderByRaw($order)->limit($limit)->get();
  132. } else {
  133. return $this->model->select(DB::raw($field))->where($where)->orderByRaw($order)->limit($limit)->get();
  134. }
  135. }
  136. public function updataSmsNum($uid, $type, $num)
  137. {
  138. if ($type == 1) {
  139. $action = 'increment';
  140. } else {
  141. $action = 'decrement';
  142. }
  143. return $this->model->where('id', $uid)->$action('sms_num', $num);
  144. }
  145. public function SetSmsNum($uid,$vaule)
  146. {
  147. return $this->model->where('id', $uid)->update(['sms_num'=> $vaule]);
  148. }
  149. public function updateLoginStatus($company)
  150. {
  151. $company->last_login_ip=ip2long(request()->ip);
  152. $company->last_login_time=time();
  153. $company->refresh_time=time();
  154. $company->save();
  155. }
  156. /**
  157. * 检查字段惟一
  158. * @param $key
  159. * @param $value
  160. * @param int $id 大于0表示排除自身
  161. * @return bool
  162. */
  163. public function checkUniaue($key, $value, $id = 0)
  164. {
  165. if (!Schema::hasColumn($this->model->getTable(), $key)) {
  166. return true;
  167. }
  168. if ($this->model->withTrashed()->where($key, $value)
  169. ->when($id>0, function ($query) use ($id) {
  170. return $query->where('id', '<>', $id);
  171. })->first()) {
  172. return false;
  173. }
  174. return true;
  175. }
  176. public function changeEmail($id, $email)
  177. {
  178. $this->model->where('id', $id)->update(['email'=>$email, 'email_audit'=>1]);
  179. }
  180. /**
  181. * 重置密码
  182. * @param $type :mobile, email
  183. * @param $value :对应type
  184. * @param $password
  185. */
  186. public function resetPassword($type, $value, $password)
  187. {
  188. $this->model->where($type, $value)->update(['password'=>Hash::make($password)]);
  189. return $this->model->where($type, $value)->first();
  190. }
  191. public function getLastCompanyCount($where)
  192. {
  193. return $this->model->where($where)->where('created_at', '>=', date('Y-m-01 00:00:00', strtotime('-1 month')))->where('created_at', '<=', date("Y-m-d 23:59:59", strtotime(-date('d').'day')))->count();
  194. }
  195. public function getNextCompanyCount($where)
  196. {
  197. return $this->model->where($where)->where('created_at', '>=', date('Y-m-01 00:00:00', strtotime(date("Y-m-d"))))->where('created_at', '<=', date('Y-m-d 23:59:59', strtotime(date('Y-m-01', strtotime(date("Y-m-d")))." +1 month -1 day")))->count();
  198. }
  199. public function getCompanyCount($where)
  200. {
  201. return $this->model->where($where)->count();
  202. }
  203. public function getCompanyNumByTime($where, $time_condition)
  204. {
  205. return $this->model->where($where)->where(function ($query) use ($time_condition) {
  206. if ($time_condition) {
  207. $query->Where([['last_login_time', '>=',strtotime($time_condition[0])],['last_login_time', '<=',strtotime($time_condition[1])]])->orWhere([['refresh_time', '>=',strtotime($time_condition[0])],['refresh_time', '<=',strtotime($time_condition[1])]]);
  208. }
  209. })->count();
  210. }
  211. public function getAuditCount($where)
  212. {
  213. return $this->model->where($where)->count();
  214. }
  215. public function getCompanyies($where = array(), $whereIn = array(), $order = array())
  216. {
  217. $rst = $this->model->where($where);
  218. if ($whereIn) {
  219. foreach ($whereIn as $k => $v) {
  220. $rst->whereIn($k, $v);
  221. }
  222. }
  223. if ($order && is_array($order)) {
  224. foreach ($order as $k => $v) {
  225. $rst->orderBy($k, $v);
  226. }
  227. }
  228. return $rst->get();
  229. }
  230. public function getCompanyNumsByGroup($where, $fields, $group_by = '')
  231. {
  232. $rst = $this->model->select(DB::raw($fields))->where($where);
  233. if ($group_by) {
  234. $rst->groupBy($group_by);
  235. }
  236. return $rst->get();
  237. }
  238. //短信数量--
  239. public function SetSmsReduce($user_id){
  240. $user=$this->model->find($user_id);
  241. $isOk=$this->model->where('id',$user->id)->update(['sms_num'=>$user->sms_num-1]);
  242. $this->membersSetmealLogRepository->createLog($user,"扣除了短信",$user->sms_num-1);
  243. return $isOk;
  244. }
  245. //批量修改企业信息
  246. public function updateCompanies($where, $whereIn, $data)
  247. {
  248. $rst = $this->model->where($where);
  249. if ($whereIn) {
  250. foreach ($whereIn as $k => $v) {
  251. $rst->whereIn($k, $v);
  252. }
  253. }
  254. return $rst->update($data);
  255. }
  256. }