Join.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\controller\relations;
  13. use app\api\controller\Controller;
  14. use app\api\model\ApplySeller;
  15. use app\api\model\Order;
  16. use app\api\model\OrderGoods;
  17. use app\api\model\Setting as SettingModel;
  18. use app\api\model\shop\Shops;
  19. use app\api\model\ShopsDailySalesSt;
  20. use app\api\model\ShopsMonthlyBonusSt;
  21. use app\api\model\StaffMobile;
  22. use app\api\model\StoreDailySalesSt;
  23. use app\api\model\User;
  24. use app\api\model\user\BonusHistory;
  25. use app\common\model\GoodsSalesRank;
  26. use app\common\model\user\CommissionsDetail as CommissionDetailModel;
  27. use app\common\service\commission\GiveOutCommission;
  28. use app\common\service\commission\RecordWaitCommission;
  29. use app\console\service\DailyBonus as DailyBonusService;
  30. use app\console\service\MonthlyBonusClear;
  31. use app\store\model\ChannelSaleVolumeTj;
  32. use app\store\model\user\CommerCountTj;
  33. use think\facade\Queue;
  34. /**
  35. * 页面控制器
  36. * Class Index
  37. * @package app\api\controller
  38. */
  39. class Join extends Controller
  40. {
  41. /**
  42. * 记录员工手机号
  43. * @return array|\think\response\Json
  44. * @throws \app\common\exception\BaseException
  45. */
  46. public function recordStaffMobile(){
  47. $mobile = strval(request()->post('mobile',''));
  48. $shopId = intval(request()->post('shopId',0));
  49. if (!$shopId)return $this->renderError('参数错误');
  50. $mobiles = explode(',',$mobile);
  51. /* try {
  52. $flag = $this->validate(['mobile'=>$mobile],['mobile'=>'mobile']);
  53. } catch (\think\exception\ValidateException $e) {
  54. $msg = $e->getError();
  55. Log::error('flag::'.$e->getError());
  56. return $this->renderSuccess($msg);
  57. }*/
  58. $msg = '成功';
  59. if (!$mobiles || !count($mobiles)){
  60. return $this->renderSuccess($msg);
  61. }
  62. $errMsg = '';
  63. foreach ($mobiles as $mob){
  64. $flag = StaffMobile::recordMobile($mob,$shopId);
  65. if ($flag == false){
  66. $errMsg .= $mob.'添加失败';
  67. }
  68. }
  69. if ($errMsg){
  70. $msg = $errMsg;
  71. //$msg = '请检查店员手机号是否正确';
  72. //return $this->renderError($msg);
  73. }
  74. return $this->renderSuccess($msg);
  75. }
  76. /**
  77. * 加入店员
  78. * @return array|\think\response\Json
  79. */
  80. public function addNewStaff()
  81. {
  82. $managerId = intval(request()->post('managerId',0));
  83. $shop_name = User::getShopInfo($managerId);
  84. //$mobile = strval(request()->post('mobile',''));
  85. $msg = '成功';
  86. $f = User::updateShopId($managerId);
  87. if ($f == false){
  88. $msg = '请核对手机号码';
  89. return $this->renderError($msg);
  90. }
  91. return $this->renderSuccess(['shop_name'=>$shop_name]);//compact('data')
  92. }
  93. /**
  94. * 申请成为推荐官
  95. * @return array|\think\response\Json
  96. * @throws \app\common\exception\BaseException
  97. */
  98. public function applyBeSeller(){
  99. $model = new ApplySeller();
  100. if (!$model->addNew($this->request->post())){
  101. return $this->renderError($model->getError());
  102. }
  103. return $this->renderSuccess('申请成功');
  104. }
  105. /**
  106. * 获取推荐官的等级说明
  107. * @author: zjwhust
  108. * @Time: 2021/12/11 11:03
  109. */
  110. public function getDistributorList(){
  111. $distributor_grade = SettingModel::getItem('distributor_grade');//分销员推广佣金配置
  112. return $this->renderSuccess($distributor_grade);
  113. }
  114. /**
  115. * 测试用,订单待结算佣金加入队列接口,异常情况的订单也可用
  116. * @return array|\think\response\Json
  117. */
  118. public function testRecordWaitCommission(){
  119. $orderId = intval($this->request->post('orderId',0));
  120. if (!$orderId){
  121. return $this->renderError('orderId不对');
  122. }
  123. $jobHandlerClassName = 'app\job\recordWaitCommissionsDetail';
  124. $jobQueueName = "orderWaitCommissionsQueue";
  125. //数组数据
  126. $orderData = [
  127. 'orderId' => $orderId,//uniqid()
  128. ];
  129. $isPushed = Queue::push($jobHandlerClassName, $orderData ,$jobQueueName);
  130. //$isPushed = Queue::later(5, $jobHandlerClassName, $orderData, $jobQueueName);
  131. if( $isPushed !== false ){
  132. $msg = date('Y-m-d H:i:s') . " 队列添加成功";
  133. }else{
  134. $msg = '队列添加失败';
  135. }
  136. return $this->renderSuccess($msg);
  137. }
  138. //测试用,写待分佣记录
  139. public function testWaitCommission($orderId){
  140. $orderId = intval($orderId);
  141. if($orderId < 1){
  142. return $this->renderError('参数有误');
  143. }
  144. $o = new RecordWaitCommission();
  145. $flag = $o->waitGiveOutCommission($orderId);
  146. $msg = $flag?'success':'failed';
  147. return $this->renderSuccess($msg);
  148. }
  149. //测试用,收货后分佣结算
  150. public function testConfirmGoodsCommission($orderId)
  151. {
  152. $orderId = intval($orderId);
  153. if($orderId < 1){
  154. return $this->renderError('参数有误');
  155. }
  156. $con = new GiveOutCommission();
  157. $con->testConfirmOrderCommission($orderId);
  158. return $this->renderSuccess('成功');
  159. }
  160. /**
  161. * 每日计算奖励金接口
  162. * @return array|\think\response\Json
  163. * @throws \think\db\exception\DataNotFoundException
  164. * @throws \think\db\exception\DbException
  165. * @throws \think\db\exception\ModelNotFoundException
  166. */
  167. public function dailyBonusClear(){
  168. set_time_limit(120);
  169. $service = new DailyBonusService;
  170. //v1.3.6版本奖励金区分门店和角色
  171. $service->calBonusMoney36($this->storeId);
  172. return $this->renderSuccess('成功');
  173. }
  174. /**
  175. * 每月20日结算奖励金接口
  176. * @return array|\think\response\Json
  177. * @throws \think\db\exception\DataNotFoundException
  178. * @throws \think\db\exception\DbException
  179. * @throws \think\db\exception\ModelNotFoundException
  180. */
  181. public function monthlyBonusClear(){
  182. $debug = boolval($this->request->get('debug',false));
  183. $service = new MonthlyBonusClear();
  184. if ($debug == true){
  185. //$service->clearBonusMoneyTest($this->storeId);
  186. $service->clearBonusMoney36Test($this->storeId);
  187. }else{
  188. //$service->clearBonusMoney($this->storeId);
  189. $service->clearBonusMoney36($this->storeId);
  190. }
  191. return $this->renderSuccess('成功');
  192. }
  193. /**
  194. * 测试用-清除当月的奖励金结算状态
  195. * @return array|\think\response\Json
  196. */
  197. public function monthlyBonusClearFlush(){
  198. if (env('APP_DEBUG') == true){
  199. $yearMonth = intval(date('Ym'));
  200. BonusHistory::where('year_month',$yearMonth)->update(['clearing_status'=>0]);
  201. }
  202. return $this->renderSuccess('成功清除本月奖励金结算状态');
  203. }
  204. /**
  205. * 每日统计门店销售数据
  206. * @return array|\think\response\Json
  207. * @throws \think\db\exception\DataNotFoundException
  208. * @throws \think\db\exception\DbException
  209. * @throws \think\db\exception\ModelNotFoundException
  210. */
  211. public function dailyShopSalesStatistics(){
  212. $flag = $this->request->get('flag',0);//1:统计今天的,供测试使用
  213. $shops = Shops::where('is_delete',0)->field('shop_id,province_id,city_id,region_id')->select();
  214. $m = new ShopsDailySalesSt();
  215. //$orderM = new OrderGoods();
  216. $orderM = new Order();
  217. $to = strtotime(date("Y-m-d"),time());
  218. if($flag == 1 && env('APP_DEBUG')){
  219. $from = $to;
  220. $dayMid = $to + 43200;
  221. $to = $to + 86399;
  222. }else{
  223. $from = $to - 86400;
  224. $dayMid = $to - 43200;
  225. $to -= 1;
  226. }
  227. $now = time();
  228. $datas = [];
  229. foreach ($shops as $shop){
  230. $temp = [];
  231. $tj = $orderM->dailySalesTj($shop['shop_id'],$from,$to);
  232. $temp['total_price'] = $tj['total_price'];
  233. $temp['total_goods_price'] = $tj['total_goods_price'];
  234. $temp['total_pay_price'] = $tj['total_pay_price'];
  235. $temp['coupon_money'] = $tj['coupon_money'];
  236. $temp['order_count'] = $tj['order_count'];
  237. $temp['express_price'] = $tj['express_price'];
  238. $temp['boss_comm_money'] = $tj['boss_comm_money'];
  239. $temp['mg_comm_money'] = $tj['mg_comm_money'];
  240. $temp['seller_comm_money'] = $tj['seller_comm_money'];
  241. $temp['shop_id'] = $shop['shop_id'];
  242. $temp['st_time'] = $dayMid;
  243. $temp['create_time'] = $now;
  244. $temp['update_time'] = $now;
  245. $datas[] = $temp;
  246. }
  247. //批量插入,当门店过多时,需优化成分批插入
  248. $m->where('st_time',$dayMid)->delete();
  249. $m->insertAll($datas);
  250. return $this->renderSuccess();
  251. }
  252. /**
  253. * 每月21号统计门店月奖励金
  254. * @return array|\think\response\Json
  255. */
  256. public function monthlyShopBonusStatistics(){
  257. $flag = $this->request->get('flag',0);//1:统计本月的,供测试使用
  258. $shops = Shops::where('is_delete',0)->field('shop_id,province_id,city_id,region_id,boss_user_id,manager_user_id')->select();
  259. $m = new ShopsMonthlyBonusSt();
  260. if($flag == 1 && env('APP_DEBUG')){
  261. $from = strtotime(date('Y-m-01 00:00:00',time()));//本月开始时间
  262. $to = strtotime(date('Y-m-01 00:00:00',strtotime("+1 month",time()))) - 1;
  263. $year = date('Y');
  264. $month = intval(date('m'));
  265. }else{
  266. $to = strtotime(date('Y-m-01 00:00:00',time())) - 1;
  267. $from = strtotime(date('Y-m-01',$to));
  268. $year = date("Y", strtotime("-1 month"));
  269. $month = intval(date("m", strtotime("-1 month")));
  270. }
  271. $bonusM = new BonusHistory();
  272. $datas = [];
  273. $stYearMonth = date('Ym',$from);
  274. $now = time();
  275. foreach ($shops as $shop){
  276. $data = $bonusM->getRoleBonus($shop,$from,$to,$year,$month);
  277. $data['shop_id'] = $shop['shop_id'];
  278. $data['st_year'] = $year;
  279. $data['st_month'] = $month;
  280. $data['st_year_month'] = $stYearMonth;
  281. $data['create_time'] = $now;
  282. $data['update_time'] = $now;
  283. $datas[] = $data;
  284. }
  285. $m->where('st_year_month',$stYearMonth)->delete();
  286. $m->insertAll($datas);
  287. return $this->renderSuccess();
  288. }
  289. /**
  290. * 每日统计商城销售数据
  291. * @return array|\think\response\Json
  292. * @throws \think\db\exception\DataNotFoundException
  293. * @throws \think\db\exception\DbException
  294. * @throws \think\db\exception\ModelNotFoundException
  295. */
  296. public function dailyStoreSalesStatistics(){
  297. $flag = $this->request->get('flag',0);//1:统计今天的,供测试使用
  298. $m = new StoreDailySalesSt();
  299. $orderM = new Order();
  300. $to = strtotime(date("Y-m-d"),time());
  301. if($flag == 1 && env('APP_DEBUG')){
  302. $from = $to;
  303. $to = $to + 86399;
  304. }else{
  305. $from = $to - 86400;
  306. $to -= 1;
  307. }
  308. $dayMid = date('Ymd',$to);
  309. $tj = $orderM->dailyStoreSaleVolumeSt($from,$to);
  310. $m->addOne($tj,$dayMid);
  311. //商品销量排行昨日统计数据添加
  312. (new GoodsSalesRank)->addGoodsSalesRankDaily();
  313. return $this->renderSuccess();
  314. }
  315. /**
  316. * 测试-统计渠道销售额
  317. * @return array|\think\response\Json
  318. */
  319. public function testGenChannelSaleVolume(){
  320. $now = time() + 86400;
  321. //推荐官销售数据统计
  322. $csvTj = new ChannelSaleVolumeTj();
  323. $csvTj->day($now);
  324. $csvTj->week($now);
  325. $csvTj->month($now);
  326. $commerTj = new CommerCountTj();
  327. $commerTj->day($now);
  328. $commerTj->week($now);
  329. $commerTj->month($now);
  330. return $this->renderSuccess();
  331. }
  332. }