/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.api.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.common.cache.ParamCache;
import org.springblade.common.constant.CacheBizConstant;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.sms.model.SmsCode;
import org.springblade.core.sms.model.SmsData;
import org.springblade.core.tool.api.R;
import org.springblade.modules.desk.entity.Notice;
import org.springblade.modules.desk.service.INoticeService;
import org.springblade.modules.platform.entity.Ads;
import org.springblade.modules.platform.entity.Article;
import org.springblade.modules.platform.entity.Payments;
import org.springblade.modules.platform.service.IAdsService;
import org.springblade.modules.platform.service.IArticleService;
import org.springblade.modules.platform.service.IPaymentsService;
import org.springblade.modules.resource.builder.sms.SmsBuilder;
import org.springblade.modules.resource.utils.SmsUtil;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static org.springblade.modules.resource.utils.SmsUtil.PARAM_KEY;
import static org.springblade.modules.resource.utils.SmsUtil.SEND_SUCCESS;

/**
 * 控制器
 *
 * @author xuwei
 * @since 2022-02-12
 */
@RestController
@AllArgsConstructor
@RequestMapping(CommonConstant.API_URL)
@Api(value = "公共接口", tags = "01.公共接口")
public class CommonController extends BladeController {

	private final IPaymentsService paymentsService;
	private final IAdsService adsService;
	private final IArticleService articleService;
	private final SmsBuilder smsBuilder;
	private final BladeRedis bladeRedis;
	private final INoticeService noticeService;

	@GetMapping("/payments-list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "查询支付方式")
	public R<List<Payments>> paymentsList() {
		List<Payments> list =  bladeRedis.get(CacheBizConstant.CACHE_DEFAULT_PAYMENTS);
		if (list == null || list.size() == 0){
			Payments payments = new Payments();
			payments.setIsEnabled(2);
			payments.setPayFor(4);//APP端
			list = paymentsService.list(Condition.getQueryWrapper(payments).lambda().orderByAsc(Payments::getSort));
			bladeRedis.setEx(CacheBizConstant.CACHE_DEFAULT_PAYMENTS,list, CacheBizConstant.CACHE_TIME);
		}
		return R.data(list);
	}

	@GetMapping("/ads-list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "查询轮播数据", notes = "传入类型和条数,类型为字典项ads_type，1应用首页，2商城首页，3推荐品牌，4抢购专区，5新品推荐，6人气推荐")
	public R<List<Ads>> adsList(@ApiParam(value = "类型", required = true)  @RequestParam Integer type,
								@ApiParam(value = "条数，默认条数由系统配置", required = false)  @RequestParam(required = false) Integer limit) {
		if (ObjectUtil.isNull(limit)) {
			limit = Integer.valueOf(ParamCache.getValue(CommonConstant.DEFAULT_ADS_LIMIT));
		}
		return R.data(adsService.selectAdsList(type, limit));
	}

	@SneakyThrows
	@PostMapping("/common/send-validate")
	@ApiOperation(value = "短信发送", notes = "传入手机号")
	@ApiOperationSupport(order = 3)
	public R sendValidate(@RequestParam String phone) {
		Map<String, String> params = SmsUtil.getValidateParams();
		SmsCode smsCode = smsBuilder.noAuthTemplate().sendValidate(new SmsData(params).setKey(PARAM_KEY), phone);
		return smsCode.isSuccess() ? R.data(smsCode, SEND_SUCCESS) : R.fail("发送频率过高，请稍后再试");
	}


	@GetMapping("/ads-detail")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "查询轮播详情")
	public R<Ads> adsDetail(@ApiParam(value = "广告Id", required = true)  @RequestParam Long id) {
		return R.data(adsService.getById(id));
	}

	@GetMapping("/article-detail")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "查询文章详情")
	public R<Article> articleDetail(@ApiParam(value = "文章Id", required = true)  @RequestParam Long id) {
		return R.data(articleService.getById(id));
	}

	@GetMapping("/notice-detail")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "查询通知公告详情")
	public R<Notice> noticeDetail(@ApiParam(value = "文章Id", required = true)  @RequestParam Long id) {
		return R.data(noticeService.getById(id));
	}

	@ApiOperation("获取通知公告")
	@ApiOperationSupport(order = 6)
	@GetMapping("notice")
	public R<List<Notice>> getNotice(@ApiParam(value = "类型 1-小程序公告 2-商家端公告", required = true)  @RequestParam Integer type) {
		IPage<Notice> page = noticeService.page(Condition.getPage(new Query()), Wrappers.lambdaQuery(Notice.class)
				.eq(Notice::getCategory, type).eq(BaseEntity::getStatus, 1));
		return R.data(page.getRecords());
	}

}
