Farmland.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\ApiController;
  4. use app\common\validate\IDMustBePositiveInt;
  5. class Farmland extends ApiController
  6. {
  7. /**
  8. * 列表
  9. *
  10. * @author 许祖兴 < zuxing.xu@lettered.cn>
  11. * @date 2020/7/3 16:16
  12. *
  13. * @return \think\response\Json
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\ModelNotFoundException
  16. * @throws \think\exception\DbException
  17. */
  18. public function getNearby()
  19. {
  20. $param = $this->request->param();
  21. $limit = 10;
  22. // 数据校验
  23. $valid = $this->validate($param, [
  24. 'page' => 'require',
  25. ]);
  26. // 错误
  27. if (true !== $valid){
  28. return $this->ApiJson(-1,$valid);
  29. }
  30. //
  31. $taxi = model('common/Farmland')
  32. ->where([ 'status' => 1])
  33. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  34. ->select();
  35. return $this->ApiJson(0,'获取成功', $taxi);
  36. }
  37. /**
  38. * 详情
  39. *
  40. * @author 许祖兴 < zuxing.xu@lettered.cn>
  41. * @date 2020/7/3 16:16
  42. *
  43. * @param $id
  44. * @return \think\response\Json
  45. * @throws \Lettered\Support\Exceptions\EvidentException
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. * @throws \think\exception\DbException
  49. */
  50. public function getDetailByID($id)
  51. {
  52. (new IDMustBePositiveInt())->valid();
  53. // 读取详情
  54. $farmland = model('common/Farmland')->findBy($id);
  55. // 读取模块
  56. $farmland['block'] = model('common/FarmlandBlock')->where(['farm_id' => $id])->select();
  57. if (!$farmland){
  58. return $this->ApiJson(-1, '不存在编号');
  59. }
  60. return $this->ApiJson(0, '获取信息', $farmland);
  61. }
  62. /**
  63. *
  64. * @author 许祖兴 < zuxing.xu@lettered.cn>
  65. * @date 2021/2/3 17:53
  66. *
  67. * @param $id
  68. * @return mixed
  69. * @throws \Lettered\Support\Exceptions\EvidentException
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. * @throws \think\exception\DbException
  73. */
  74. public function getFarmBlockVarietyByID($id)
  75. {
  76. (new IDMustBePositiveInt())->valid();
  77. // 读取详情
  78. $block = model('common/FarmlandBlock')->findBy($id);
  79. if (!$block){
  80. return $this->ApiJson(-1, "不存在");
  81. }
  82. // 读取品类
  83. $block['variety'] = model('common/FarmlandVariety')->where(['block_id' => $block['id']])->select();
  84. return $this->ApiJson(0, '获取信息', $block);
  85. }
  86. /**
  87. * 技能服务
  88. *
  89. * @author 许祖兴 < zuxing.xu@lettered.cn>
  90. * @date 2020/7/3 16:29
  91. *
  92. * @return \think\response\Json
  93. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  94. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  95. * @throws \GuzzleHttp\Exception\GuzzleException
  96. * @throws \Lettered\Support\Exceptions\FailedException
  97. */
  98. public function userServe()
  99. {
  100. $params = $this->request->param();
  101. // 参数校验
  102. $valid = $this->validate($params, [
  103. 'block_id|归属地块' => 'require',
  104. 'price|价格' => 'require',
  105. 'variety|品种' => 'require',
  106. 'date|承包到期' => 'require'
  107. ]);
  108. // 错误返回
  109. if(true !== $valid){
  110. return $this->ApiJson(-1, $valid);
  111. };
  112. // 检查服务是不是已经被购买了
  113. $block = model('common/FarmlandBlock')->findBy($params['block_id']);
  114. if (!$block){
  115. // 不存在
  116. return $this->ApiJson(-1, "该地块不存在...");
  117. }
  118. if ($block['status'] == 2){
  119. //return $this->ApiJson(-1, "农场已在服务中,无法购买了");
  120. }
  121. // 创建订单 -- 附加数据
  122. $params['order_no'] = get_order_no();
  123. $params['user_id'] = $this->auth->user()['id'];
  124. // 总价计算
  125. $params['total_price'] = round( $params['price'] * $params['month'] * (int)$params['area'],2);
  126. // 开始时间(付款成功那一刻开始算)-结束时间
  127. $params['end_at'] = strtotime(date($params['date'] . ' 23:59:59'));
  128. // halt($params);
  129. $orderId = model('common/FarmlandOrder')->storeBy($params);
  130. if ($orderId){
  131. // 创建订单详情
  132. foreach (str2arr($params['variety']) as $varietyId){
  133. $variety = model('common/FarmlandVariety')->findBy($varietyId);
  134. model('common/FarmlandOrderDetail')::create([
  135. 'order_id' => $orderId,
  136. 'variety_id' => $varietyId,
  137. 'name' => $variety['name']
  138. ], true);
  139. }
  140. // 创建对应支付记录
  141. $trade_no = get_order_no();
  142. model('common/OrderPaylog')->storeBy([
  143. 'out_trade_no' => $trade_no,
  144. 'total_price' => $params['total_price'],
  145. 'order_idx' => $orderId,
  146. 'ascription' => 'farmland' // 归属订单
  147. ]);
  148. // 返回支付单号
  149. return $this->ApiJson(0,'订单提交成功', $trade_no);
  150. }
  151. return $this->ApiJson(-1,'发生异常,请骚后重试...');
  152. }
  153. /**
  154. *
  155. * @author 许祖兴 < zuxing.xu@lettered.cn>
  156. * @date 2021/2/5 20:37
  157. *
  158. * @return mixed
  159. */
  160. public function userServeChange()
  161. {
  162. $params = $this->request->param();
  163. // 参数校验
  164. $valid = $this->validate($params, [
  165. 'order_id|订单' => 'require',
  166. 'variety_id|品种' => 'require',
  167. 'change_id|替换' => 'require'
  168. ]);
  169. // 错误返回
  170. if(true !== $valid){
  171. return $this->ApiJson(-1, $valid);
  172. }
  173. // 已经替换
  174. $detail = model('common/FarmlandOrderDetail')->findBy($params['change_id']);
  175. if (!$detail){
  176. return $this->ApiJson(-1, '错误的替换');
  177. }
  178. if ($detail['status'] == 4){
  179. return $this->ApiJson(-1, '已经替换,请勿重复操作');
  180. }
  181. // todo try catch
  182. // 查找
  183. $variety = model('common/FarmlandVariety')->findBy($params['variety_id']);
  184. // 写入新明细
  185. $result = model('common/FarmlandOrderDetail')::create([
  186. 'order_id' => $params['order_id'],
  187. 'variety_id' => $variety['id'],
  188. 'name' => $variety['name']
  189. ], true);
  190. if ($result){
  191. // 标记替换完成
  192. model('common/FarmlandOrderDetail')->updateBy($detail['id'],[
  193. 'status' => 4
  194. ]);
  195. }
  196. return $this->ApiJson(0,'更新', $params);
  197. }
  198. /**
  199. * 获取订单列表
  200. *
  201. * @return \think\response\Json
  202. * @throws \Lettered\Support\Exceptions\FailedException
  203. * @throws \think\db\exception\DataNotFoundException
  204. * @throws \think\db\exception\ModelNotFoundException
  205. * @throws \think\exception\DbException
  206. */
  207. public function order()
  208. {
  209. $param = $this->request->param();
  210. $limit = 10;
  211. // 数据校验
  212. $valid = $this->validate($param, [
  213. 'page' => 'require',
  214. ]);
  215. // 错误
  216. if (true !== $valid) {
  217. return $this->ApiJson(-1, $valid);
  218. }
  219. $orders = model('common/FarmlandOrder')
  220. ->where(['user_id' => $this->auth->user()['id']])
  221. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  222. ->order(['created_at' => 'desc'])
  223. ->select();
  224. return $this->ApiJson(0,'获取成功', $orders);
  225. }
  226. /**
  227. *
  228. * @author 许祖兴 < zuxing.xu@lettered.cn>
  229. * @date 2021/2/6 16:28
  230. *
  231. * @return mixed
  232. */
  233. public function orderShipping()
  234. {
  235. $user = $this->auth->user();
  236. $param = $this->request->param();
  237. if($this->request->isPost()){ // 提交数据
  238. // 参数校验
  239. $valid = $this->validate($param, [
  240. 'order_id|关联订单' => 'require',
  241. // 'count|数量' => 'require',
  242. 'username|收件人姓名' => 'require',
  243. 'mobile|收件人手机' => 'require',
  244. 'address|收件人地址' => 'require'
  245. ]);
  246. // 错误返回
  247. if(true !== $valid){
  248. return $this->ApiJson(-1, $valid);
  249. };
  250. // 查找订单
  251. $order = model('common/FarmlandOrder')->findBy($param['order_id']);
  252. if(!$order){
  253. return $this->ApiJson(-1, "订单不存在了!");
  254. }
  255. if($order['status'] !== 3){
  256. return $this->ApiJson(-1, "当前服务状态不接受采摘!");
  257. }
  258. // 检查有完成采摘的没有
  259. $picked = model('common/FarmlandOrderDetail')->where(['order_id' => $order['id'],'status' => 3])->count('id');
  260. if ($picked <= 0){
  261. return $this->ApiJson(-1, "亲,您还没有可采摘的农作物哟!!");
  262. }
  263. // 一个周申请
  264. $param['user_id'] = $user['id'];
  265. model('common/FarmlandShipping')->storeBy($param);
  266. push_socket_data('farmland',[
  267. 'id' => $order['id'],
  268. 'msg' => '有新的农田采摘申请等待处理,订单号:' . $order['order_no']
  269. ]);
  270. return $this->ApiJson(0,'提交成功,等待处理', $param);
  271. }
  272. // 查看申请记录
  273. return $this->ApiJson(0,'获取成功', $param);
  274. }
  275. /**
  276. *
  277. * @author 许祖兴 < zuxing.xu@lettered.cn>
  278. * @date 2021/2/5 16:25
  279. *
  280. * @param $id
  281. * @return mixed
  282. * @throws \think\db\exception\DataNotFoundException
  283. * @throws \think\db\exception\ModelNotFoundException
  284. * @throws \think\exception\DbException
  285. */
  286. public function orderDetail($id)
  287. {
  288. return $this->ApiJson(0,'获取成功', model('common/FarmlandOrderDetail')
  289. ->where(['order_id' => $id])->order(['status' => 'asc'])
  290. ->select());
  291. }
  292. public function orderDetailImage($id)
  293. {
  294. $param = $this->request->param();
  295. $limit = 10;
  296. // 数据校验
  297. $valid = $this->validate($param, [
  298. 'page' => 'require',
  299. ]);
  300. // 错误
  301. if (true !== $valid) {
  302. return $this->ApiJson(-1, $valid);
  303. }
  304. $orders = model('common/FarmlandOrderImage')
  305. ->where(['order_id' => $id])
  306. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  307. ->order(['created_at' => 'desc'])
  308. ->select();
  309. return $this->ApiJson(0,'获取成功', $orders);
  310. }
  311. }