MonthlyWelfare.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. namespace app\store\controller\members;
  3. use app\common\enum\Setting as SettingEnum;
  4. use app\common\library\storage\Driver as StorageDriver;
  5. use app\common\service\Export;
  6. use app\store\controller\Controller;
  7. use app\store\model\Coupon as CouponModel;
  8. use app\store\model\member\MemberCardsAction;
  9. use app\store\model\member\MemberCooperationCoupon;
  10. use app\store\model\member\MemberWelfare;
  11. use app\store\model\member\MemberWelfare as MemberWelfareModel;
  12. use app\store\model\member\MemberWelfareDraw;
  13. use app\store\model\Setting as SettingModel;
  14. use app\store\model\UploadFile;
  15. use think\facade\Validate;
  16. use app\common\service\Export as ExportService;
  17. /**
  18. * 月度会员福利
  19. * Class MonthlyWelfare
  20. * @package app\store\controller\members
  21. */
  22. class MonthlyWelfare extends Controller
  23. {
  24. /**
  25. * 添加会员福利(会员专享券)
  26. * @return array|string
  27. */
  28. public function add()
  29. {
  30. // 新增记录
  31. $model = new MemberWelfareModel;
  32. $param = $this->postForm();
  33. $param['audit_user'] = $this->store['user']['real_name']?:'';
  34. $param['audit_admin_id'] = $this->store['user']['store_user_id']?:'';
  35. $param['number'] = str_replace('.','',microtime(true)); // 生成活动编号
  36. if ($param['target_type'] == 1 && empty($param['coupon_id'])){
  37. return $this->renderError('请完善福利内容');
  38. }
  39. if ($param['target_type'] == 2 && $param['deploy_type']==1 && empty($param['codes_file'])){
  40. return $this->renderError('请完善福利内容');
  41. }
  42. if ($param['target_type'] == 2 && $param['deploy_type']==2 && empty($param['welfare_name'])){
  43. return $this->renderError('请完善福利内容');
  44. }
  45. $etypes = $param['exchange_type']??[];
  46. if (is_array($etypes)){
  47. $etypes = array_map(function($v){
  48. return intval($v);
  49. },$etypes);
  50. }else{
  51. $etypes = [];
  52. }
  53. $param['exchange_type'] = json_encode($etypes);
  54. $send_times = between_time_format($param['send_times']);
  55. $param['start_time'] = date('Y-m-01',$send_times['start_time']);
  56. $nextM = mktime(0,0,0,date('m',$send_times['end_time']) +1,1,date('Y',$send_times['end_time']));
  57. $param['end_time'] = date('Y-m-d',$nextM - 1 );
  58. //$param['end_time'] = date('Y-m-d',$send_times['end_time']);
  59. if($param['target_type'] == 2){//异业福利才有使用有效期
  60. $use_times = between_time_format($param['use_times']);
  61. $param['use_start_time'] = date('Y-m-d',$use_times['start_time']);
  62. $param['use_end_time'] = date('Y-m-d',$use_times['end_time']);
  63. }else{
  64. $param['use_start_time']=$param['use_end_time'] = null;
  65. }
  66. unset($param['send_times'],$param['use_times']);
  67. //unset($param['use_times']);
  68. //dd($param);
  69. $flag = $model->addOne($param);
  70. if ($flag == 1) {
  71. return $this->renderSuccess('添加成功');
  72. }
  73. return $this->renderError($model->getError() ?: '添加失败');
  74. }
  75. //store/upload/image
  76. //{"status":200,"message":"图片上传成功","data":{"fileInfo":{"group_id":0,"channel":10,"storage":"aliyun","domain":"http:\/\/img-test.cookhome.club","file_name":"default_unit_logo2.png","file_path":"10001\/20220525\/d3076d109c83f3acc07c31f189407c7c.png","file_size":1923,"file_ext":"png","file_type":10,"store_id":10001,"create_time":"2022-05-25 16:57:23","update_time":"2022-05-25 16:57:23","file_id":"10866"}}}
  77. /**
  78. * 会员专享券详情
  79. * @param int $id
  80. * @return
  81. */
  82. public function detail(int $id)
  83. {
  84. $detail = MemberWelfareModel::detail($id,['file','coupon','urls']);
  85. return $this->renderSuccess(compact('detail'));
  86. }
  87. /**
  88. * 会员专享券编辑
  89. * @return array|string
  90. */
  91. public function update()
  92. {
  93. // 新增记录
  94. $param = $this->postForm();
  95. $model = MemberWelfareModel::detail($param['id']);
  96. if ($model->audit_status == 1){
  97. return $this->renderError( '不支持此操作');
  98. }
  99. if ($param['target_type'] == 1 && empty($param['coupon_id'])){
  100. return $this->renderError('请完善福利内容');
  101. }
  102. if ($param['target_type'] == 2 && $param['deploy_type']==1 && empty($param['codes_file'])){
  103. return $this->renderError('请完善福利内容');
  104. }
  105. if ($param['target_type'] == 2 && $param['deploy_type']==2 && empty($param['welfare_name'])){
  106. return $this->renderError('请完善福利内容');
  107. }
  108. $etypes = $param['exchange_type']??[];
  109. if (is_array($etypes)){
  110. $etypes = array_map(function($v){
  111. return intval($v);
  112. },$etypes);
  113. }else{
  114. $etypes = [];
  115. }
  116. $param['exchange_type'] = json_encode($etypes);
  117. $param['audit_user'] = $this->store['user']['real_name']?:'';
  118. $param['audit_admin_id'] = $this->store['user']['store_user_id']?:'';
  119. $flag = $model->edit($param);
  120. if ($flag == 1) {
  121. return $this->renderSuccess('修改成功');
  122. }
  123. return $this->renderError($model->getError() ?: '修改失败');
  124. }
  125. /**
  126. * 会员专享券审核
  127. * @param
  128. * @return array|bool
  129. */
  130. public function audit()
  131. {
  132. // 确认审核
  133. $postData = $this->postForm();
  134. $model = MemberWelfareModel::detail($postData['id']);
  135. if(empty($model)){
  136. return $this->renderError('找不到');
  137. }
  138. $res ='操作成功';
  139. if($model->audit_status == 1){
  140. return $this->renderError( '已审核通过,无法再操作');
  141. }
  142. $postData['audit_time'] = Date("Y-m-d H:i:s",time());
  143. $postData['audit_user'] = $this->store['user']['real_name']?:'';
  144. $postData['audit_admin_id'] = $this->store['user']['store_user_id']?:'';
  145. $flag = $model->audit($postData);
  146. if ($flag) {
  147. if ($postData['audit_status']==1 && $model->deploy_type==1 && !empty($model['codes_file'])){//导入第三方券码
  148. $file = UploadFile::detail(intval($model['codes_file']));
  149. if ($postData['audit_status'] == 1 && $model['target_type'] == 2 && $file){
  150. //审核通过导入券码
  151. $flagPath = $this->importCodes($model->id,$file->file_path);
  152. if ($flagPath === false){
  153. log_record(__METHOD__.'导入券码失败','error');
  154. return $this->renderError( '系统繁忙,请稍后再试');
  155. }
  156. }
  157. }
  158. if($postData['audit_status']==1 && $model['target_type'] == 2 && $model->deploy_type==2){//自制券码
  159. $code = random_character(6);
  160. $datas = [];
  161. for ($i = 1; $i <= $model->amount; $i++) {
  162. $datas[] = [
  163. 'coupon_id' => 0,
  164. 'coupon_name' => $model->welfare_name ?? '',
  165. 'create_time' => time(),
  166. 'update_time' => time(),
  167. 'welfare_id' => $postData['id'],
  168. 'code' => $code,
  169. 'provider' => $model->provider ?? '',
  170. ];
  171. }
  172. (new MemberCooperationCoupon())->insertAll($datas);
  173. }
  174. return $this->renderSuccess($postData,$res);
  175. }
  176. return $this->renderError($model->getError() ?: '操作失败');
  177. }
  178. /**
  179. * 会员专享券行动日志
  180. * @param int $id
  181. * @return array
  182. */
  183. public function actionsList(int $id)
  184. {
  185. $model = new MemberCardsAction();
  186. $list = $model->getLists($id,2);
  187. return $this->renderSuccess(compact('list'));
  188. }
  189. /**
  190. * 会员福利列表
  191. * @return array
  192. */
  193. public function welfareLists(){
  194. $model = new MemberWelfareModel;
  195. $params = $this->request->param();
  196. $list = $model->lists($params);
  197. return $this->renderSuccess(compact('list'));
  198. }
  199. /**
  200. * 编辑序号
  201. * @return array
  202. */
  203. public function editOrder(){
  204. $postData = $this->postForm();
  205. $data['id'] = $postData['id'];
  206. $data['order_at'] = $postData['order_at'];
  207. $model = new MemberWelfareModel;
  208. $flag = $model->editOrder($data);
  209. if ($flag == true){
  210. return $this->renderSuccess();
  211. }
  212. return $this->renderError($model->getError() ?: '操作失败');
  213. }
  214. /**
  215. * 列表删除福利
  216. * @param $id
  217. * @return array
  218. */
  219. public function delWelfare($id){
  220. $data['id'] = $id;
  221. $model = new MemberWelfareModel;
  222. $flag = $model->del($data);
  223. if ($flag == true){
  224. return $this->renderSuccess();
  225. }
  226. return $this->renderError($model->getError() ?: '操作失败');
  227. }
  228. /**
  229. * @param $id
  230. * @param $codesFileUrl
  231. * @return bool
  232. * @throws \PhpOffice\PhpSpreadsheet\Exception
  233. * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  234. * @throws \think\Exception
  235. * @throws \think\db\exception\DataNotFoundException
  236. * @throws \think\db\exception\DbException
  237. * @throws \think\db\exception\ModelNotFoundException
  238. */
  239. private function importCodes($id,$codesFileUrl): bool
  240. {
  241. $config = SettingModel::getItem(SettingEnum::STORAGE);
  242. $storage = new StorageDriver($config);
  243. $localFile = $storage->download($codesFileUrl);
  244. $flag = Export::importCooperationCodes($id,$localFile);
  245. if (file_exists($localFile)){
  246. unlink($localFile);//处理完删除文件 todo
  247. }
  248. return $flag;
  249. }
  250. /**
  251. * 会员专享券列表select
  252. * @return array
  253. */
  254. public function couponlist(){
  255. $params = $this->request->get();
  256. $model = new CouponModel;
  257. $list = $model->getMemTypeList($params);
  258. return $this->renderSuccess(compact('list'));
  259. }
  260. /**
  261. * 异业会员福利领取记录
  262. * @return array
  263. */
  264. public function getDiffWelfareDrawRecords(){
  265. $model = new MemberWelfareModel;
  266. $params = $this->request->param();
  267. $list = $model->listsDiff($params);
  268. return $this->renderSuccess(compact('list'));
  269. }
  270. /**
  271. * 异业会员福利领取记录导出
  272. * @return array
  273. * @Time: 2021/10/15 13:43
  274. */
  275. public function getDiffwDrawExport()
  276. {
  277. $param = $this->request->param();
  278. /* if(!isset($param['ids']) || empty($param['ids'])){
  279. return $this->renderError('请勾选兑换码后再导出');
  280. }*/
  281. $model = new MemberWelfareModel;
  282. $data = $model->diffwDrawExportData($param);
  283. if (!$data['data'] || !count($data['data'])){
  284. return $this->renderError('没有数据哟');
  285. }
  286. $res = ExportService::export($data['data'],$data['header'],$data['filename'],'兑换码列表');
  287. return $this->renderSuccess($res,'导出成功');
  288. }
  289. /**
  290. * 异业福利领取明细
  291. * @return array
  292. */
  293. public function getDiffWelDrawDetails(){
  294. $params = $this->request->get();
  295. $validate = Validate::rule('targetId', 'require|number|gt:0')
  296. ->rule([
  297. /* 'checkStatus' => 'require|number|between:0,1',
  298. 'hasExpired' => 'require|number|between:0,1',*/
  299. 'mobile' => 'number',
  300. ]);
  301. if (!$validate->check($params)) {
  302. return $this->renderError($validate->getError());
  303. }
  304. $model = new MemberWelfareDraw();
  305. $list = $model->getLists($params);
  306. return $this->renderSuccess(compact('list'));
  307. }
  308. /**
  309. * 异业福利领取明细导出
  310. * @return array
  311. * @Time: 2021/10/15 13:43
  312. */
  313. public function getDiffwDrawDetExport()
  314. {
  315. $params = $this->request->param();
  316. $validate = Validate::rule('targetId', 'require|number|gt:0')
  317. ->rule([
  318. /* 'checkStatus' => 'require|number|between:0,1',
  319. 'hasExpired' => 'require|number|between:0,1',*/
  320. 'mobile' => 'number',
  321. ]);
  322. if (!$validate->check($params)) {
  323. return $this->renderError($validate->getError());
  324. }
  325. $model = new MemberWelfareDraw;
  326. $data = $model->diffwDrawExportData($params);
  327. if (!$data['data'] || !count($data['data'])){
  328. return $this->renderError('没有数据哟');
  329. }
  330. $res = ExportService::export($data['data'],$data['header'],$data['filename'],'异业福利领取明细列表');
  331. return $this->renderSuccess($res,'导出成功');
  332. }
  333. }