FarmPlotRunningController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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\FarmPlotRunning;
  8. use app\model\Member;
  9. use app\model\MemberInfo;
  10. use app\model\SysDept;
  11. use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
  12. use Illuminate\Database\Query\Builder as QueryBuilder;
  13. use support\exception\BusinessException;
  14. use support\Model;
  15. use support\Request;
  16. use support\Response;
  17. use function _PHPStan_cc8d35ffb\RingCentral\Psr7\str;
  18. class FarmPlotRunningController extends Curd
  19. {
  20. public function __construct()
  21. {
  22. $this->model = new FarmPlotRunning();
  23. }
  24. /**
  25. * @Desc 列表
  26. * @Author Gorden
  27. * @Date 2024/2/27 14:46
  28. *
  29. * @param Request $request
  30. * @return Response
  31. * @throws \support\exception\BusinessException
  32. */
  33. public function select(Request $request): Response
  34. {
  35. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  36. $order = $request->get('order', 'desc');
  37. $field = $field ?? 'farm_plot_running_addtimes';
  38. $query = $this->doSelect($where, $field, $order);
  39. return $this->doFormat($query, $format, $limit);
  40. }
  41. /**
  42. * 指定查询where条件,并没有真正的查询数据库操作
  43. * @param array $where
  44. * @param string|null $field
  45. * @param string $order
  46. * @return EloquentBuilder|QueryBuilder|Model
  47. */
  48. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  49. {
  50. $model = $this->model->with([
  51. 'plot' => function ($query) {
  52. $query->select('farm_plot_id', 'farm_plot_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. $item['farm_plot_id'] = $item['plot']['farm_plot_id'] ?? '';
  113. $item['farm_plot_name'] = $item['plot']['farm_plot_name'] ?? '';
  114. }
  115. return $items;
  116. }
  117. public function info(Request $request): Response
  118. {
  119. $plotRunningId = $request->get('farm_plot_running_id');
  120. if (!$plotRunningId) {
  121. return json_fail("参数异常");
  122. }
  123. $farmPlotRunning = FarmPlotRunning::with([
  124. 'plot' => function ($query) {
  125. $query->select('farm_plot_id', 'farm_plot_name', 'farm_plot_cover', 'join_farm_plot_dept_id', 'farm_plot_amount', 'farm_plot_cover', 'farm_plot_status', 'farm_plot_used_datetime', 'farm_plot_addtimes');
  126. },
  127. 'memberInfo' => function ($query) {
  128. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  129. }
  130. ])->where('farm_plot_running_id', $plotRunningId)
  131. ->select('farm_plot_running_id', 'join_running_farm_plot_id', 'join_farm_plot_running_member_id', 'farm_plot_running_json', 'farm_plot_running_process_json')
  132. ->first();
  133. if (!empty($farmPlotRunning->join_farm_plot_member_id)) {
  134. $member = Member::with([
  135. 'role' => function ($query) {
  136. $query->select('member_role_id', 'member_role_name');
  137. }
  138. ])->where('member_id', $farmPlotRunning->join_farm_plot_member_id)
  139. ->select('member_id', 'join_member_role_id')
  140. ->first();
  141. $farmPlotRunning->role_name = $member->role->member_role_name ?? '';
  142. }
  143. if (!empty($farmPlotRunning['plot']['join_farm_plot_dept_id'])) {
  144. $farmPlotRunning->dept_name = SysDept::where('dept_id', $farmPlotRunning['plot']['join_farm_plot_dept_id'])->value('dept_name');
  145. }
  146. if (!empty($farmPlotRunning['plot']['farm_plot_cover'])) {
  147. $farmPlotRunning->farm_plot_cover = getenv('STORAGE_DOMAIN') . $farmPlotRunning['plot']['farm_plot_cover'];
  148. }
  149. // $farmPlotRunning['member_info']['member_info_headimg'] = MemberService::getAvatarUrl($farmPlotRunning['member_info']['member_info_headimg'] ?? '');
  150. $harvestData = [];
  151. $processData = [];
  152. if (!empty($farmPlotRunning->farm_plot_running_json)) {
  153. $farmPlotRunningJson = json_decode($farmPlotRunning->farm_plot_running_json, true);
  154. if (isset($farmPlotRunningJson['harvest'])) {
  155. $harvest = $farmPlotRunningJson['harvest'];
  156. foreach ($harvest as &$item) {
  157. $harvestData[] = [
  158. 'url' => getenv('STORAGE_DOMAIN') . $item['media'][0]['url'],
  159. 'plant' => $item['plant'],
  160. 'status' => $item['status'],
  161. 'weight' => $item['weight'],
  162. 'datetime' => $item['datetime']
  163. ];
  164. }
  165. }
  166. }
  167. if (!empty($farmPlotRunning->farm_plot_running_process_json)) {
  168. $farmPlotRunningJson = json_decode($farmPlotRunning->farm_plot_running_process_json, true);
  169. if (isset($farmPlotRunningJson['process'])) {
  170. $process = $farmPlotRunningJson['process'];
  171. foreach ($process as &$item) {
  172. $processData[] = [
  173. 'url' => !empty($item['media'][0]) ? getenv('STORAGE_DOMAIN') . $item['media'][0]['url'] : '',
  174. 'content' => $item['content'],
  175. 'datetime' => $item['datetime']
  176. ];
  177. }
  178. }
  179. }
  180. $farmPlotRunning->harvest = $harvestData;
  181. $farmPlotRunning->process = $processData;
  182. return json_success('success', $farmPlotRunning);
  183. }
  184. /**
  185. * 添加
  186. * @param Request $request
  187. * @return Response
  188. * @throws BusinessException
  189. */
  190. public function insert(Request $request): Response
  191. {
  192. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  193. return json_fail($this->validateClass->getError());
  194. }
  195. try {
  196. $data = $this->insertInput($request);
  197. $this->doInsert($data);
  198. } catch (BusinessException $customException) {
  199. return json_fail($customException->getMessage());
  200. } catch (\Exception $e) {
  201. dump($e->getMessage());
  202. return json_fail('数据写入失败');
  203. }
  204. return json_success('success');
  205. }
  206. /**
  207. * 更新
  208. * @param Request $request
  209. * @return Response
  210. * @throws BusinessException
  211. */
  212. public function update(Request $request): Response
  213. {
  214. if ($this->validate && !$this->validateClass->scene('update')->check($request->post())) {
  215. return json_fail($this->validateClass->getError());
  216. }
  217. try {
  218. [$id, $data] = $this->updateInput($request);
  219. $this->doUpdate($id, $data);
  220. } catch (BusinessException $e) {
  221. return json_fail($e->getMessage());
  222. } catch (\Exception $e) {
  223. dump($e->getMessage());
  224. return json_fail('数据更新失败');
  225. }
  226. return json_success('success');
  227. }
  228. /**
  229. * @Desc 菜园维护
  230. * @Author Gorden
  231. * @Date 2024/10/17 13:14
  232. *
  233. * @param Request $request
  234. * @return Response
  235. */
  236. public function addProcess(Request $request)
  237. {
  238. $runningId = $request->post('farm_plot_running_id');
  239. if (!$runningId) {
  240. return json_fail("参数异常");
  241. }
  242. $running = FarmPlotRunning::where('farm_plot_running_id', $runningId)->first();
  243. $processJson = [];
  244. if (!empty($running->farm_plot_running_process_json)) {
  245. $processJson = json_decode($running->farm_plot_running_process_json, true);
  246. }
  247. try {
  248. $processJson['process'][] = [
  249. 'media' => [[
  250. 'type' => 'image',
  251. 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $request->post('url', '')),
  252. ]],
  253. 'content' => $request->post('content', ''),
  254. 'datetime' => date('Y-m-d H:i:s')
  255. ];
  256. $running->farm_plot_running_process_json = json_encode($processJson);
  257. $running->save();
  258. return json_success("success");
  259. } catch (\Exception $e) {
  260. return json_fail("维护操作失败");
  261. }
  262. }
  263. /**
  264. * @Desc 菜园维护
  265. * @Author Gorden
  266. * @Date 2024/10/17 13:14
  267. *
  268. * @param Request $request
  269. * @return Response
  270. */
  271. public function addHarvest(Request $request)
  272. {
  273. $runningId = $request->post('farm_plot_running_id');
  274. if (!$runningId) {
  275. return json_fail("参数异常");
  276. }
  277. $running = FarmPlotRunning::where('farm_plot_running_id', $runningId)->first();
  278. $runningJson = [];
  279. if (!empty($running->farm_plot_running_json)) {
  280. $runningJson = json_decode($running->farm_plot_running_json, true);
  281. }
  282. try {
  283. $runningJson['harvest'][] = [
  284. 'media' => [[
  285. 'type' => 'image',
  286. 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $request->post('url', '')),
  287. ]],
  288. 'plant' => $request->post('plant', ''),
  289. 'status' => $request->post('status', ''),
  290. 'weight' => $request->post('weight', ''),
  291. 'datetime' => date('Y-m-d H:i:s')
  292. ];
  293. $running->farm_plot_running_json = json_encode($runningJson);
  294. $running->save();
  295. return json_success("success");
  296. } catch (\Exception $e) {
  297. return json_fail("维护操作失败");
  298. }
  299. }
  300. }