FarmPlotController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace app\admin\controller\life;
  3. use app\admin\service\member\MemberService;
  4. use app\admin\validate\life\FarmLandValidate;
  5. use app\controller\Curd;
  6. use app\model\FarmPlot;
  7. use app\model\Member;
  8. use app\model\MemberInfo;
  9. use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
  10. use Illuminate\Database\Query\Builder as QueryBuilder;
  11. use support\exception\BusinessException;
  12. use support\Model;
  13. use support\Request;
  14. use support\Response;
  15. class FarmPlotController extends Curd
  16. {
  17. public function __construct()
  18. {
  19. $this->model = new FarmPlot();
  20. }
  21. /**
  22. * @Desc 列表
  23. * @Author Gorden
  24. * @Date 2024/2/27 14:46
  25. *
  26. * @param Request $request
  27. * @return Response
  28. * @throws \support\exception\BusinessException
  29. */
  30. public function select(Request $request): Response
  31. {
  32. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  33. $order = $request->get('order', 'desc');
  34. $field = $field ?? 'farm_plot_addtimes';
  35. $query = $this->doSelect($where, $field, $order);
  36. return $this->doFormat($query, $format, $limit);
  37. }
  38. /**
  39. * 指定查询where条件,并没有真正的查询数据库操作
  40. * @param array $where
  41. * @param string|null $field
  42. * @param string $order
  43. * @return EloquentBuilder|QueryBuilder|Model
  44. */
  45. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  46. {
  47. $model = $this->model->with([
  48. 'dept' => function ($query) {
  49. $query->select('dept_id', 'dept_name');
  50. },
  51. 'user' => function ($query) {
  52. $query->select('user_id', 'user_name');
  53. },
  54. 'member' => function ($query) {
  55. $query->select('member_id', 'member_mobile', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer');
  56. },
  57. 'memberCert' => function ($query) {
  58. $query->select('join_cert_member_id', 'member_cert_name');
  59. }
  60. ]);
  61. foreach ($where as $column => $value) {
  62. if (is_array($value)) {
  63. if ($value[0] === 'like' || $value[0] === 'not like') {
  64. $model = $model->where($column, $value[0], "%$value[1]%");
  65. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  66. $model = $model->where($column, $value[0], $value[1]);
  67. } elseif ($value[0] == 'in' && !empty($value[1])) {
  68. $valArr = $value[1];
  69. if (is_string($value[1])) {
  70. $valArr = explode(",", trim($value[1]));
  71. }
  72. $model = $model->whereIn($column, $valArr);
  73. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  74. $valArr = $value[1];
  75. if (is_string($value[1])) {
  76. $valArr = explode(",", trim($value[1]));
  77. }
  78. $model = $model->whereNotIn($column, $valArr);
  79. } elseif ($value[0] == 'null') {
  80. $model = $model->whereNull($column);
  81. } elseif ($value[0] == 'not null') {
  82. $model = $model->whereNotNull($column);
  83. } elseif ($value[0] !== '' || $value[1] !== '') {
  84. $model = $model->whereBetween($column, $value);
  85. }
  86. } else {
  87. $model = $model->where($column, $value);
  88. }
  89. }
  90. if ($field) {
  91. $model = $model->orderBy($field, $order);
  92. }
  93. return $model;
  94. }
  95. /**
  96. * @Desc 查询后数据处理
  97. * @Author Gorden
  98. * @Date 2024/3/22 10:56
  99. *
  100. * @param $items
  101. * @return mixed
  102. */
  103. public function afterQuery($items)
  104. {
  105. foreach ($items as &$item) {
  106. if (!empty($item['farm_plot_cover'])) {
  107. $item['farm_plot_cover'] = getenv('STORAGE_DOMAIN') . $item['farm_plot_cover'];
  108. }
  109. $mobile = $item['member']['member_mobile'] ?? '';
  110. $certName = $item['memberCert']['member_cert_name'] ?? '';
  111. $item['member_name'] = MemberService::getMemberCertName($mobile, $certName, '');
  112. }
  113. return $items;
  114. }
  115. public function info(Request $request): Response
  116. {
  117. $plotId = $request->get('plot_id');
  118. if (!$plotId) {
  119. return json_fail("参数异常");
  120. }
  121. $farmPlot = FarmPlot::with([
  122. 'memberInfo' => function ($query) {
  123. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  124. }
  125. ])->where('farm_plot_id', $plotId)
  126. ->select('farm_plot_id', 'join_farm_plot_member_id')
  127. ->first();
  128. if (!empty($farmPlot->join_farm_plot_member_id)) {
  129. $member = Member::with([
  130. 'role' => function ($query) {
  131. $query->select('member_role_id', 'member_role_name');
  132. }
  133. ])->where('member_id', $farmPlot->join_farm_plot_member_id)
  134. ->select('member_id', 'join_member_role_id')
  135. ->first();
  136. $farmPlot->role_name = $member->role->member_role_name ?? '';
  137. }
  138. // $farmPlot['memberInfo']['member_info_headimg'] = MemberService::getAvatarUrl($farmPlot['memberInfo']['member_info_headimg'] ?? '');
  139. return json_success('success', $farmPlot);
  140. }
  141. /**
  142. * 添加
  143. * @param Request $request
  144. * @return Response
  145. * @throws BusinessException
  146. */
  147. public function insert(Request $request): Response
  148. {
  149. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  150. return json_fail($this->validateClass->getError());
  151. }
  152. try {
  153. $data = $this->insertInput($request);
  154. $data['farm_plot_id'] = 'FP' . date('ymdHi') . random_string(4, 'up');
  155. $data['farm_plot_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['farm_plot_cover']);
  156. $this->doInsert($data);
  157. } catch (BusinessException $customException) {
  158. return json_fail($customException->getMessage());
  159. } catch (\Exception $e) {
  160. dump($e->getMessage());
  161. return json_fail('数据写入失败');
  162. }
  163. return json_success('success');
  164. }
  165. /**
  166. * 更新
  167. * @param Request $request
  168. * @return Response
  169. * @throws BusinessException
  170. */
  171. public function update(Request $request): Response
  172. {
  173. if ($this->validate && !$this->validateClass->scene('update')->check($request->post())) {
  174. return json_fail($this->validateClass->getError());
  175. }
  176. try {
  177. [$id, $data] = $this->updateInput($request);
  178. $data['farm_plot_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['farm_plot_cover']);
  179. $this->doUpdate($id, $data);
  180. } catch (BusinessException $e) {
  181. return json_fail($e->getMessage());
  182. } catch (\Exception $e) {
  183. dump($e->getMessage());
  184. return json_fail('数据更新失败');
  185. }
  186. return json_success('success');
  187. }
  188. }