UserServiceLogic.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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\adminapi\logic\agricultural_machinery;
  15. use app\common\enum\PayEnum;
  16. use app\common\enum\YesNoEnum;
  17. use app\common\logic\BaseLogic;
  18. use app\common\model\agricultural_machinery\UserService;
  19. use app\common\model\article\ArticleCate;
  20. use app\common\model\user\User;
  21. use app\common\model\agricultural_machinery\ServiceCategory as AgriculturalMachineryServiceCategory;
  22. use app\common\model\recharge\RechargeOrder;
  23. use app\common\model\supply_demand\SupplyDemandInfo;
  24. use app\common\model\supply_demand\SupplyDemandCate;
  25. use app\common\model\ServiceCharge;
  26. use think\facade\Db;
  27. /**
  28. * 服务管理逻辑
  29. * Class ArticleCateLogic
  30. * @package app\adminapi\logic\article
  31. */
  32. class UserServiceLogic extends BaseLogic
  33. {
  34. /**
  35. * @notes 添加农耕分类
  36. * @param array $params
  37. * @author heshihu
  38. * @date 2022/2/18 10:17
  39. */
  40. public static function add(array $params,$type_name)
  41. {
  42. Db::startTrans();
  43. try {
  44. $where['user_id'] = $params['user_id'];
  45. $where['type'] = $params['type'];
  46. $userService = UserService::where($where)->findOrEmpty();
  47. if(!$userService->isEmpty()){
  48. Db::rollback();
  49. self::setError('当前用户存在'.$type_name.'服务信息,无需添加');
  50. return ['code'=>0,'data'=>[]];
  51. }
  52. $result = UserService::create([
  53. 'user_id' => $params['user_id'],
  54. 'type' => $params['type'] ?? 1,
  55. 'name' => $params['name'],
  56. 'mobile' => $params['mobile'],
  57. 'agricultural_machinery_model' =>$params['agricultural_machinery_model'],
  58. 'images' => $params['images'],
  59. 'cate_id' => ','.$params['cate_id'].',',
  60. 'area_id' => ','.$params['area_id'].',',
  61. 'money' => $params['money'],
  62. 'content'=>$params['content']?? '',
  63. 'order_id'=>0,
  64. ]);
  65. if($result){
  66. $service_fee = ServiceCharge::find(1);
  67. $order_money = 0;
  68. switch ($params['type']){
  69. case 1 :
  70. $order_money =$service_fee['agricultural_machinery_service_fee'];
  71. break;
  72. case 2 :
  73. $order_money =$service_fee['bake_service_fee'];
  74. break;
  75. case 3 :
  76. $order_money =$service_fee['air_control_service_fee'];
  77. break;
  78. }
  79. $data = [
  80. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  81. 'user_id' => $params['user_id'],
  82. 'pay_way'=> 4,
  83. 'pay_time'=>time(),
  84. 'service_id'=>$result['id'],
  85. 'pay_status' => PayEnum::ISPAID,
  86. 'order_amount' => $order_money,
  87. ];
  88. $order = RechargeOrder::create($data);
  89. $user_service_info = UserService::find($result['id']);
  90. $user_service_info->order_id = $order['id'];
  91. $user_service_info->status = 1;
  92. $user_service_info->expiration_time = time()+365*24*60*60;
  93. $user_service_info->save();
  94. $order_info = [
  95. 'order_id' => (int)$order['id'],
  96. 'order_sn' => $order['sn'],
  97. 'from' => 'service',
  98. 'result'=>$result
  99. ];
  100. }
  101. Db::commit();
  102. return ['code'=>1,'data'=>$order_info];
  103. } catch (\Exception $e) {
  104. Db::rollback();
  105. self::setError($e->getMessage());
  106. return ['code'=>0,'data'=>[]];
  107. }
  108. }
  109. /**
  110. * @notes 删除农耕分类
  111. * @param array $params
  112. * @author heshihu
  113. * @date 2022/2/21 17:52
  114. */
  115. public static function delete(array $params)
  116. {
  117. UserService::destroy($params['id']);
  118. }
  119. /**
  120. * @notes 查看服务商详情
  121. * @param $params
  122. * @return array
  123. * @author heshihu
  124. * @date 2022/2/21 17:54
  125. */
  126. public static function detail($params) : array
  127. {
  128. return UserService::findOrEmpty($params['id'])->toArray();
  129. }
  130. /**
  131. * @notes 更改服务商状态
  132. * @param array $params
  133. * @return bool
  134. * @author heshihu
  135. * @date 2022/2/21 18:04
  136. */
  137. public static function updateStatus(array $params)
  138. {
  139. UserService::update([
  140. 'id' => $params['id'],
  141. 'status' => $params['status']
  142. ]);
  143. return true;
  144. }
  145. /**
  146. * @notes 服务商审核状态
  147. * @param array $params
  148. * @return bool
  149. * @author heshihu
  150. * @date 2022/2/21 18:04
  151. */
  152. public static function auditUserService(array $params)
  153. {
  154. Db::startTrans();
  155. try {
  156. $user_service_info = UserService::find( $params['id']);
  157. if(empty($user_service_info)){
  158. Db::rollback();
  159. self::setError('传入的服务信息错误!');
  160. return false;
  161. }
  162. if($user_service_info['status']<>1){
  163. Db::rollback();
  164. self::setError('当前服务已过期或者未支付,无法审核!');
  165. return false;
  166. }
  167. $updateDate['id'] = $params['id'];
  168. $updateDate['audit_status'] = $params['audit_status'];
  169. $updateDate['audit_time'] = time();
  170. if ($params['audit_status'] == 2) {
  171. $updateDate['remark'] = $params['remark'];
  172. }
  173. UserService::update($updateDate);
  174. if ($params['audit_status'] == 2) {
  175. //退款
  176. }
  177. Db::commit();
  178. return true;
  179. } catch (\Exception $e) {
  180. Db::rollback();
  181. self::setError($e->getMessage());
  182. return false;
  183. }
  184. }
  185. /**
  186. * @notes 有效用户数据
  187. * @return array
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\DbException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. * @author 段誉
  192. * @date 2022/10/13 10:53
  193. */
  194. public static function getValidUserData()
  195. {
  196. $list = User::where(['is_disable' => YesNoEnum::NO])
  197. ->field('id,sn,nickname,account')
  198. ->order([ 'id' => 'desc'])
  199. ->select()
  200. ->toArray();
  201. return $list;
  202. }
  203. public static function getServiceContent(){
  204. $info = ServiceCharge::field('agricultural_service,bake_service,air_service')->find(1)->toArray();
  205. return $info;
  206. }
  207. public static function saveServiceContent($post){
  208. if(isset($post['agricultural_service']) && !empty($post['agricultural_service'])){
  209. $saveData['agricultural_service'] = $post['agricultural_service'];
  210. }
  211. if(isset($post['bake_service']) && !empty($post['bake_service'])){
  212. $saveData['bake_service'] = $post['bake_service'];
  213. }
  214. if(isset($post['air_service']) && !empty($post['air_service'])){
  215. $saveData['air_service'] = $post['air_service'];
  216. }
  217. $saveData['id'] = 1;
  218. ServiceCharge::update($saveData);
  219. return true;
  220. }
  221. public static function getServiceList(){
  222. // //农资供应
  223. $sdswhere = [];
  224. $sdswhere[]=['sdi.status','=',2];
  225. $sdswhere[]=['sdi.type','=',1];
  226. $SupplyInfo = SupplyDemandInfo::alias('sdi')
  227. ->leftJoin('supply_demand_cate sdc','sdi.cate_id = sdc.id')->field('sdi.cate_id,sdc.name,sum(number) total_num')->where($sdswhere)->group('cate_id')->select()->toArray();
  228. $cate_name_arr = array_column($SupplyInfo,'name');
  229. $value_arr = array_column($SupplyInfo,'total_num');
  230. $middle['cate_name'] = $cate_name_arr;
  231. $middle['cate_value'] = $value_arr;
  232. $uswhere = [];
  233. $uswhere[]=['status','=',1];
  234. $UserServiceInfo = UserService::field('type,count(id) total_num')->where($uswhere)->group('type')->order('type asc')->select()->toArray();
  235. $type_arr =[1,2,3];
  236. $utype_arr = array_column($UserServiceInfo,'type');
  237. $user_service_value = [];
  238. foreach($type_arr as $tv){
  239. $data=[];
  240. if(in_array($tv,$utype_arr)){
  241. foreach($UserServiceInfo as $usv){
  242. if($tv == $usv['type']){
  243. $data['value'] = $usv['total_num'];
  244. }
  245. }
  246. }else{
  247. $data['value'] = 0;
  248. }
  249. switch ($tv){
  250. case 1:
  251. $type_name = '农机服务';
  252. break;
  253. case 2:
  254. $type_name = '烘干服务';
  255. break;
  256. case 3:
  257. $type_name = '飞防服务';
  258. break;
  259. }
  260. $data['name'] = $type_name;
  261. $user_service_value[]=$data;
  262. }
  263. $middle['user_service_value'] = $user_service_value;
  264. //
  265. $sdswhere = [];
  266. $sdswhere[]=['sdi.status','=',2];
  267. $sdswhere[]=['sdi.type','=',1];
  268. $nowYear = date('Y');
  269. $nowMonth = date('n');
  270. $date_arr = getMonthFirstAndLastDay($nowYear,$nowMonth);
  271. $lastDate = $date_arr['lastDay'];
  272. $firstDate = $date_arr['firstDay'];
  273. $sdswhere[]=['sdi.create_time','between',[strtotime($firstDate),strtotime($lastDate.' 23:59:59')]];
  274. $SupplyInfo = SupplyDemandInfo::alias('sdi')
  275. ->leftJoin('supply_demand_cate sdc','sdi.cate_id = sdc.id')->field('sdi.cate_id,sdc.name,sum(number) total_num')
  276. ->where($sdswhere)->group('cate_id')->order('total_num desc')->limit(6)->select()->toArray();
  277. $cate_name_arr = array_column($SupplyInfo,'name');
  278. $value_arr = array_column($SupplyInfo,'total_num');
  279. $cate_id_arr = array_column($SupplyInfo,'cate_id');
  280. foreach($SupplyInfo as &$siv){
  281. unset($siv['cate_id']);
  282. $siv['value'] = $siv['total_num'];
  283. unset($siv['total_num']);
  284. }
  285. $cwhere=[];
  286. $cwhere[]=['id','in',$cate_id_arr];
  287. $cateList = SupplyDemandCate::where($cwhere)->field('id,pid')->select()->toArray();
  288. $pcate_arr = array_column($cateList,'pid');
  289. $pcate_arr = array_unique($pcate_arr);
  290. $pwhere=[];
  291. $pwhere[]=['id','in',$pcate_arr];
  292. $pcateList = SupplyDemandCate::where($pwhere)->field('id,name')->select()->toArray();
  293. // $first_cate_name = [];
  294. // if($pcateList){
  295. // $first_cate_name =array_column($pcateList,'name');
  296. // }
  297. $middle['SupplyDemandInfo'] = $SupplyInfo;
  298. $middle['supply_cate_name'] = $cate_name_arr;
  299. $middle['supply_cate_value'] = $value_arr;
  300. $middle['supply_first_cate_name'] = $pcateList;
  301. return $middle;
  302. }
  303. }