package org.springblade.modules.member.controller.pc;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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 org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.member.dto.MemberDTO;
import org.springblade.modules.member.dto.MemberStatisticInfo;
import org.springblade.modules.member.entity.MemberEntity;
import org.springblade.modules.member.service.IMemberInfoService;
import org.springblade.modules.member.vo.MemberVO;
import org.springblade.modules.member.wrapper.MemberInfoWrapper;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("/member_info")
@Api(value = "会员信息管理模块", tags = "会员信息管理模块")
public class MemberInfoController extends BladeController {

	private final IMemberInfoService memberInfoService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入member_info")
	public R<MemberVO> detail(MemberEntity member_info) {
		MemberEntity detail = memberInfoService.getOne(Condition.getQueryWrapper(member_info));
		if (Func.isNotEmpty(detail)) {
			return R.data(MemberInfoWrapper.build().entityVO(detail));
		} else {
			return R.data(null);
		}
	}

	/**
	 * 分页 代码自定义代号
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入member_info")
	public R<IPage<MemberVO>> list(@RequestParam Map<String, Object> MemberInfo, Query query) {
		String shopId = (String) MemberInfo.get("shopId");//门店
		String cardStatus = (String) MemberInfo.get("cardStatus");//卡状态
		String cardType = (String) MemberInfo.get("cardType");//卡类型
		String memberInfo = (String) MemberInfo.get("memberInfo");//会员卡号/微信号/姓名/手机号/标签
		MemberInfo.remove("shopId");
		MemberInfo.remove("cardStatus");
		MemberInfo.remove("cardType");
		MemberInfo.remove("memberInfo");

		QueryWrapper<MemberEntity> wrapper = Condition.getQueryWrapper(MemberInfo, MemberEntity.class);
		if (Func.isNotEmpty(memberInfo)) {
			wrapper.like("real_name", memberInfo).or().like("member_code", memberInfo).or().like("phone", memberInfo);
		}
		if (Func.isNotEmpty(shopId)) {
			wrapper.eq("filing_shop_id", shopId);
		}

		IPage<MemberEntity> pages = memberInfoService.page(Condition.getPage(query), wrapper);
		return R.data(MemberInfoWrapper.build().pageVO(pages));
	}

	/**
	 * 新增 代码自定义代号
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入member_info")
	public R save(@Valid @RequestBody MemberEntity member_info) {
		return R.status(memberInfoService.save(member_info));
	}

	/**
	 * 修改 代码自定义代号
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入member_info")
	public R update(@Valid @RequestBody MemberEntity member_info) {
		if (Func.isEmpty(member_info.getRealName())) {
			return R.fail("会员姓名不能为空！");
		}
		if (Func.isEmpty(member_info.getPhone())) {
			return R.fail("联系方式不能为空！");
		}

		return R.status(memberInfoService.updateById(member_info));
	}

	/**
	 * 新增或修改 代码自定义代号
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入member_info")
	public R submit(@Valid @RequestBody MemberDTO memberInfoDTO) {
		if (Func.isEmpty(memberInfoDTO.getRealName())) {
			return R.fail("会员姓名不能为空！");
		}
		if (Func.isEmpty(memberInfoDTO.getPhone())) {
			return R.fail("联系方式不能为空！");
		}

		//新增的时候判断，电话是否已存在
		if (Func.isEmpty(memberInfoDTO.getId())) {//id为空则为新增
			Boolean memPhone = memberInfoService.getMemPhone(memberInfoDTO.getPhone());
			if (memPhone) {
				return R.fail("新增失败！该手机号已存在！");
			}
		}

		MemberEntity memberInfoEntity = memberInfoService.setMemberInfoEntity(memberInfoDTO);

		return R.status(memberInfoService.saveOrUpdate(memberInfoEntity));
	}


	/**
	 * 删除 代码自定义代号
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(memberInfoService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 统计会员信息
	 *
	 * @return
	 */
	@GetMapping("/statistic_info")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "统计会员信息", notes = "")
	public R getStatisticMemInfo() {

		MemberStatisticInfo memberStatisticInfo = new MemberStatisticInfo();

		//今日会员生日人数
		List<MemberEntity> today = memberInfoService.getBirMemByDate("today");
		memberStatisticInfo.setBirTodayMemNum(today.size());

		//本月会员生日人数
		List<MemberEntity> month = memberInfoService.getBirMemByDate("month");
		memberStatisticInfo.setBirMonthMemNum(month.size());

		//本月还有的会员生日人数（剩余的）monthSurplus
		List<MemberEntity> monthSurplus = memberInfoService.getBirMemByDate("monthSurplus");
		memberStatisticInfo.setBirMonthSurplusMemNum(monthSurplus.size());

		//平均每日增长多少位会员
		memberStatisticInfo.setAverageDailyIncrease(memberInfoService.averageDailyIncrease(null));

		return R.data(memberStatisticInfo);
	}

	/**
	 * 根据时间段获取会员信息
	 *
	 * @param dateType 今日（today），近7天（rec7day），本月（month）
	 * @return
	 */
	@GetMapping("get_mem_info_by_date")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "根据时间段获取会员信息", notes = "")
	public R getMemInfoByDate(String dateType) {
		List<MemberEntity> birMemByDate = memberInfoService.getBirMemByDate(dateType);
		return R.data(MemberInfoWrapper.build().listVO(birMemByDate));
	}

	/**
	 * 根据会员卡号/手机号/微信卡号获取推荐人
	 *
	 * @param phone
	 * @return
	 */
	@GetMapping("referral_by_phone")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "根据会员卡号/手机号/微信卡号获取推荐人", notes = "")
	public R getReferralByPhone(String phone) {

		QueryWrapper<MemberEntity> wrapper = new QueryWrapper<>();
		wrapper.eq("phone", phone).or().eq("member_code", phone);//会员卡号/手机号
		wrapper.last("limit 1");
		MemberVO memberInfo = MemberInfoWrapper.build().entityVO(memberInfoService.getOne(wrapper));
		return R.data(memberInfo);
	}

}
