/*
 *      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.gateway.mall_gateway.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.common.enums.AppConstant;
import org.springblade.common.enums.PointChannelType;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.DigestUtil;
import org.springblade.flow.mall.service.IAuditMallService;
import org.springblade.flow.shop.entity.ProcessAudit;
import org.springblade.flow.shop.entity.ShopEnum;
import org.springblade.gateway.mall_gateway.entity.vo.IndexStatisticVO;
import org.springblade.gateway.mall_gateway.service.IAppMallService;
import org.springblade.ldt.account.entity.Account;
import org.springblade.ldt.account.service.IAccountService;
import org.springblade.ldt.bills.entity.Bills;
import org.springblade.ldt.bills.service.IBillsService;
import org.springblade.ldt.channel.entity.PointChannel;
import org.springblade.ldt.channel.service.IPointChannelService;
import org.springblade.ldt.mall.dto.MallDTO;
import org.springblade.ldt.mall.entity.Mall;
import org.springblade.ldt.mall.mapper.MallMapper;
import org.springblade.ldt.mall.vo.MallVO;
import org.springblade.ldt.shop.entity.Audit;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IAuditService;
import org.springblade.ldt.shop.service.IShopService;
import org.springblade.ldt.user.entity.Member;
import org.springblade.ldt.user.service.IMemberService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.Optional;

/**
 * 服务实现类
 *
 * @author BladeX
 * @since 2021-08-30
 */
@Service
public class AppMallServiceImpl extends ServiceImpl<MallMapper, Mall> implements IAppMallService {

	@Resource
	private IAccountService accountService;
	@Resource
	private IShopService shopService;
	@Resource
	private IMemberService memberService;
	@Resource
	private IBillsService billsService;
	@Resource
	private IAuditService auditService;
	@Resource
	private IAuditMallService auditMallService;
	@Resource
	private IPointChannelService pointChannelService;

	@Override
	@Transactional
	public MallDTO submit(MallDTO mallDTO) {
		if (mallDTO.getId() == null) {
			Account account = accountService.getOne(Condition.getQueryWrapper(new Account()).lambda()
				.eq(Account::getPhone, mallDTO.getPersonTel())
				.eq(Account::getType, AppConstant.PLATFORM.MALL.name()));
			account = Optional.ofNullable(account).orElse(new Account());
			account.setPhone(mallDTO.getPersonTel());
			//手机号加密两次
			String hex1 = DigestUtil.md5Hex(account.getPhone());
			String secret = DigestUtil.hex(hex1);
			account.setSecret(secret);
			account.setType(AppConstant.PLATFORM.MALL.name());
			accountService.saveOrUpdate(account);
		}
		this.saveOrUpdate(mallDTO);

		return mallDTO;
	}

	@Override
	public Boolean mallAudit(Audit audit) {
		ProcessAudit processAudit = ProcessAudit.builder().audit(audit).build();
		return auditMallService.startProcess(processAudit);
	}

	@Override
	public IndexStatisticVO statistic(String mallId) {

		//门店数量
		int shopNum = shopService.count(Condition.getQueryWrapper(new Shop()).lambda()
			.eq(Shop::getMallId, mallId)
			.eq(Shop::getAuditStatus, ShopEnum.PASS.name()));

		//会员数量
		int memberNum = memberService.count(Condition.getQueryWrapper(new Member()).lambda()
			.eq(Member::getMallId, mallId)
			.eq(Member::getType,AppConstant.PLATFORM.MALL.name()));

		//今日新增会员
		int newMember = memberService.count(Condition.getQueryWrapper(new Member()).lambda().eq(Member::getMallId, mallId)
			.eq(Member::getType,AppConstant.PLATFORM.MALL.name())
			.ge(Member::getCreateTime, DateUtil.beginOfDay(new Date()))
			.le(Member::getCreateTime, DateUtil.endOfDay(new Date())));

		//交易笔数
		int transactionNum = billsService.count(Condition.getQueryWrapper(new Bills()).lambda()
			.eq(Bills::getMallId, mallId)
			.eq(Bills::getPayStatus, AppConstant.BillPayStatus.付款成功.name()));

		//今日营收
		QueryWrapper<Bills> queryWrapper = Condition.getQueryWrapper(new Bills());
		queryWrapper.select("IFNULL(sum(price), 0) as todayCharge").lambda()
			.eq(Bills::getMallId, mallId)
			.eq(Bills::getPayStatus, AppConstant.BillPayStatus.付款成功.name())
			.ge(Bills::getCreateTime, DateUtil.beginOfDay(new Date()))
			.le(Bills::getCreateTime, DateUtil.endOfDay(new Date()));

		Map<String, Object> map = billsService.getMap(queryWrapper);
		BigDecimal todayCharge = (BigDecimal) map.get("todayCharge");

		IndexStatisticVO indexStatisticVO = IndexStatisticVO.builder()
			.shopNum(shopNum)
			.memberNum(memberNum)
			.newMember(newMember)
			.todayCharge(todayCharge)
			.transactionNum(transactionNum)
			.build();

		return indexStatisticVO;
	}

	@Override
	public void shopManagementList(IPage<Object> page, MallVO mall) {
//		IPage<AuditVO> pages = page.setRecords(baseMapper.selectMallPage(page, mall));
	}


}
