ServiceLogic.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\api\logic;
  15. use app\common\logic\BaseLogic;
  16. use app\common\enum\{LoginEnum, PayEnum};
  17. use app\common\service\{
  18. ConfigService,
  19. };
  20. use app\common\model\agricultural_machinery\UserService;
  21. use app\common\model\agricultural_machinery\ServiceCategory;
  22. use app\common\model\agricultural_machinery\ServiceScanLog;
  23. use app\common\model\agricultural_machinery\MechanicalCate;
  24. use app\common\model\ServiceCharge;
  25. use app\common\model\asset\AssetArea;
  26. use app\common\model\recharge\RechargeOrder;
  27. use app\common\model\user\{User, UserAuth};
  28. use think\facade\{Db, Config};
  29. /**
  30. * 服务商逻辑
  31. * Class LoginLogic
  32. * @package app\api\logic
  33. */
  34. class ServiceLogic extends BaseLogic
  35. {
  36. /**
  37. * @notes 入驻
  38. * @param array $params
  39. * @return bool
  40. * @author 段誉
  41. * @date 2022/9/7 15:37
  42. */
  43. public static function Settled(array $params)
  44. {
  45. Db::startTrans();
  46. try {
  47. $service_fee = ServiceCharge::find(1);
  48. $order_money = 0;
  49. $flag = false ;
  50. switch ($params['type']){
  51. case 1 :
  52. $flag = true ;
  53. $order_money =$service_fee['agricultural_machinery_service_fee'];
  54. break;
  55. case 2 :
  56. $flag = true ;
  57. $order_money =$service_fee['bake_service_fee'];
  58. break;
  59. case 3 :
  60. $flag = true ;
  61. $order_money =$service_fee['air_control_service_fee'];
  62. break;
  63. }
  64. $result = UserService::create([
  65. 'user_id' => $params['user_id'],
  66. 'type' => $params['type'] ?? 1,
  67. 'name' => $params['name'],
  68. 'mobile' => $params['mobile'],
  69. 'agricultural_machinery_model' =>$params['agricultural_machinery_model'],
  70. 'images' => $params['images']??'',
  71. 'driving_image' => $params['driving_image']??'',
  72. 'driver_image' => $params['driver_image']??'',
  73. 'agricultural_image' => $params['agricultural_image']??'',
  74. 'cate_id' => ','.$params['cate_id'].',',
  75. 'area_id' => ','.$params['area_id'].',',
  76. 'mechanical_cate_id' => ','.$params['mechanical_cate_id'].',',
  77. 'money' => $params['money'],
  78. 'content'=>$params['content']??'',
  79. 'order_id'=>0,
  80. ]);
  81. if($result){
  82. $data = [
  83. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  84. 'user_id' => $params['user_id'],
  85. 'service_id'=>$result['id'],
  86. 'pay_status' => PayEnum::UNPAID,
  87. 'order_amount' => $order_money,
  88. ];
  89. $order = RechargeOrder::create($data);
  90. $user_service_info = UserService::find($result['id']);
  91. $user_service_info->order_id = $order['id'];
  92. $user_service_info->save();
  93. if($flag){
  94. switch ($params['type']){
  95. case 1 :
  96. $updateData['agricultural_id'] =(int)$order['id'];
  97. $updateData['agricultural_status'] =1;
  98. break;
  99. case 2 :
  100. $updateData['bake_id'] =(int)$order['id'];
  101. $updateData['bake_status'] =1;
  102. break;
  103. case 3 :
  104. $updateData['air_id'] =(int)$order['id'];
  105. $updateData['air_status'] =1;
  106. break;
  107. }
  108. $updateData['id'] = $params['user_id'];
  109. User::update($updateData);
  110. }
  111. $order_info = [
  112. 'order_id' => (int)$order['id'],
  113. 'order_sn' => $order['sn'],
  114. 'order_money'=>$order_money,
  115. 'from' => 'service',
  116. 'result'=>$result
  117. ];
  118. }
  119. Db::commit();
  120. return ['code'=>1,'data'=>$order_info];
  121. } catch (\Exception $e) {
  122. Db::rollback();
  123. self::setError($e->getMessage());
  124. return ['code'=>0,'data'=>[]];
  125. }
  126. }
  127. /**
  128. * @notes
  129. * @param $params
  130. * @return array|false
  131. * @author 段誉
  132. * @date 2022/9/6 19:26
  133. */
  134. public static function getInfo($params,$user_id)
  135. {
  136. try {
  137. $where = [];
  138. $where[]=['user_id','=',$user_id];
  139. $where[]=['type','=',$params['type']];
  140. $serviceInfo = UserService::where($where)->append(['type_desc','cate_desc','area_desc','images','user','orderInfo'])->findOrEmpty();
  141. if($serviceInfo->isEmpty()){
  142. $status = -1;
  143. }else{
  144. if($serviceInfo['status'] == 0){
  145. $status = 0;
  146. }else if($serviceInfo['status'] == 2){
  147. $status = 2;
  148. }else{
  149. $status = 1;
  150. }
  151. }
  152. return [
  153. 'status' => $status,
  154. 'info' => $serviceInfo,
  155. ];
  156. } catch (\Exception $e) {
  157. self::setError($e->getMessage());
  158. return false;
  159. }
  160. }
  161. /**
  162. * @notes 地址列表
  163. * @author 段誉
  164. * @date 2022/9/16 17:56
  165. */
  166. public static function getsAreaLists($get)
  167. {
  168. $where=[];
  169. $where[]=['status','=',1];
  170. if(isset($get['title'])){
  171. $where[]=['title','like','%'.$get['title'].'%'];
  172. }
  173. $area_list = AssetArea::where($where)->field('id,id value,level,pid,title,title label')->order('sort desc,id asc')->select()->toArray();
  174. if($get['is_tree'] ==1){
  175. $treeList = linear_to_tree($area_list, 'children');
  176. if (empty($treeList) && !empty($area_list)) {
  177. foreach ($area_list as &$v) {
  178. $v['children'] = [];
  179. }
  180. return $area_list;
  181. }
  182. return $treeList;
  183. }else{
  184. return $area_list;
  185. }
  186. }
  187. /**
  188. * @notes 地址列表
  189. * @author 段誉
  190. * @date 2022/9/16 17:56
  191. */
  192. public static function getsCateLists($get)
  193. {
  194. $where=[];
  195. $where[]=['type','=',$get['type']];
  196. $where[]=['status','=',1];
  197. if(isset($get['name'])){
  198. $where[]=['name','like','%'.$get['name'].'%'];
  199. }
  200. $cate_list = ServiceCategory::where($where)->field('id,name')->order('sort desc,id asc')->select()->toArray();
  201. return $cate_list;
  202. }
  203. public static function getsMechanicalCateLists($get){
  204. $where=[];
  205. if(isset($get['name'])){
  206. $where[]=['name','like','%'.$get['name'].'%'];
  207. }
  208. $cate_list = MechanicalCate::where($where)->field('id,name')->order('sort desc,id asc')->select()->toArray();
  209. return $cate_list;
  210. }
  211. /**
  212. * @notes 续费订单
  213. * @param array $params
  214. * @return bool
  215. * @author 段誉
  216. * @date 2022/9/7 15:37
  217. */
  218. public static function Renew(array $params)
  219. {
  220. Db::startTrans();
  221. try {
  222. $where['user_id'] = $params['user_id'];
  223. $where['type'] = $params['type'];
  224. $info = UserService::where($where)->findOrEmpty();
  225. $order_info = [];
  226. if($info){
  227. if($info['expiration_time'] > time()){
  228. Db::rollback();
  229. self::setError('服务未到期,无需进行续费操作');
  230. return ['code'=>0,'data'=>[]];
  231. }
  232. $service_fee = ServiceCharge::find(1);
  233. $order_money = 0;
  234. switch ($params['type']){
  235. case 1 :
  236. $order_money =$service_fee['agricultural_machinery_service_fee'];
  237. break;
  238. case 2 :
  239. $order_money =$service_fee['bake_service_fee'];
  240. break;
  241. case 3 :
  242. $order_money =$service_fee['air_control_service_fee'];
  243. break;
  244. }
  245. $data = [
  246. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  247. 'user_id' => $params['user_id'],
  248. 'service_id'=>$info['id'],
  249. 'pay_status' => PayEnum::UNPAID,
  250. 'order_amount' => $order_money,
  251. ];
  252. $order = RechargeOrder::create($data);
  253. $info->order_id = $order['id'];
  254. $info->status = 0;
  255. $info->save();
  256. $order_info = [
  257. 'order_id' => (int)$order['id'],
  258. 'order_sn' => $order['sn'],
  259. 'from' => 'service',
  260. ];
  261. }
  262. Db::commit();
  263. return ['code'=>1,'data'=>$order_info];
  264. } catch (\Exception $e) {
  265. Db::rollback();
  266. self::setError($e->getMessage());
  267. return ['code'=>0,'data'=>[]];
  268. }
  269. }
  270. public static function updateServiceStatus(){
  271. $where = [];
  272. $where[]=['status','=' ,1];
  273. $where[]=['is_auto_upd','=', 0];
  274. $where[]=['expiration_time','<', time()];
  275. $list = UserService::where($where)->select()->toArray();
  276. $remark = '';
  277. foreach($list as $k=>$v){
  278. $updatewhere['id'] = $v['id'];
  279. $updateData['status'] = 2;
  280. $updateData['is_auto_upd'] = 1;
  281. UserService::where($updatewhere)->update($updateData);
  282. $remark .='id='.$v['id'].'~已到期,状态更新~';
  283. }
  284. if(empty($remark)){
  285. $remark = '无更新数据';
  286. }
  287. return $remark;
  288. }
  289. public static function getServiceContentInfo(){
  290. $where['id'] = 1;
  291. $info = ServiceCharge::field('agricultural_service,bake_service,air_service')->find(1)->toArray();
  292. return $info;
  293. }
  294. public static function getUserServiceInfo($user_id){
  295. $info = User::field('id,nickname,mobile,agricultural_status,agricultural_id,bake_status,bake_id,air_status,air_id')
  296. ->with(['agricultural','bake','air'])->find($user_id)->toArray();
  297. return $info;
  298. }
  299. public static function getServiceDisclaimer(){
  300. $data['disclaimer'] = ConfigService::get('website', 'disclaimer');
  301. return $data;
  302. }
  303. /**
  304. * @notes 插入浏览日志
  305. * @param array $params
  306. * @return bool
  307. * @author 段誉
  308. * @date 2022/9/7 15:37
  309. */
  310. public static function addLog(array $params)
  311. {
  312. try {
  313. $data = [
  314. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  315. 'user_id' => $params['user_id'],
  316. 'type' => $params['log_type'],
  317. 'service_user_id' => $params['service_user_id']??0,
  318. ];
  319. ServiceScanLog::create($data);
  320. return true;
  321. } catch (\Exception $e) {
  322. self::setError($e->getMessage());
  323. return false;
  324. }
  325. }
  326. }