package com.qs.mp.channel.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.mp.channel.domain.Channel;
import com.qs.mp.channel.domain.ChannelCommission;
import com.qs.mp.channel.domain.ChannelMoneyLog;
import com.qs.mp.channel.mapper.ChannelCommissionMapper;
import com.qs.mp.channel.service.IChannelCommissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qs.mp.channel.service.IChannelMoneyLogService;
import com.qs.mp.channel.service.IChannelService;
import com.qs.mp.common.enums.ChannelMoneyEnum;
import com.qs.mp.common.utils.DateUtils;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * <p>
 * 渠道佣金记录 服务实现类
 * </p>
 *
 * @author quanshu
 * @since 2022-03-02
 */
@Service
public class ChannelCommissionServiceImpl extends ServiceImpl<ChannelCommissionMapper, ChannelCommission> implements IChannelCommissionService {

	@Autowired
	private IChannelService channelService;

	@Autowired
	private IChannelMoneyLogService channelMoneyLogService;

	@Override
	public int getChannelCommAmtCnt(String channelNo, int days) {
		return getBaseMapper().getChannelCommAmtCnt(channelNo, days == 1?0:days);
	}

	@Override
	public int getChannelSaleAmtCnt(String channelNo, int days) {
		return getBaseMapper().getChannelSaleAmtCnt(channelNo, days == 1?0:days);
	}

	@Override
	public int getChannelTotalCommAmtCnt(String channelNo) {
		return getBaseMapper().getChannelTotalCommAmtCnt(channelNo);
	}

	@Override
	public int getChannelTotalSaleAmtCnt(String channelNo) {
		return getBaseMapper().getChannelTotalSaleAmtCnt(channelNo);
	}

	@Override
	public List<ChannelCommission> getChannelList(Wrapper<ChannelCommission> wrapper) {
		return getBaseMapper().getChannelList(wrapper);
	}

	@Override
	public void commEntry(Long channelId, Date bizDay) {
		Map<String, Object> sumCommMap = getMap(new QueryWrapper<ChannelCommission>()
				.select("IFNULL(SUM(comm_amt),0) as commAmt")
				.lambda().eq(ChannelCommission::getChannelId, channelId)
				.ge(ChannelCommission::getCreatedTime, bizDay)
				.lt(ChannelCommission::getCreatedTime, DateUtils.addDays(bizDay, 1)));
		int sumCommAmt = sumCommMap.get("commAmt") == null ? 0 : Integer.valueOf(String.valueOf(sumCommMap.get("commAmt")));
		Channel channel = channelService.getById(channelId);
		int cnt = channelMoneyLogService.count(new LambdaQueryWrapper<ChannelMoneyLog>()
				.eq(ChannelMoneyLog::getChannelId, channelId)
				.eq(ChannelMoneyLog::getBizTime, bizDay)
				.eq(ChannelMoneyLog::getType, ChannelMoneyEnum.COMMISSION));
		String strBizDay = DateUtils.dateTime(bizDay);
		Assert.isTrue(cnt == 0, "渠道" + channelId + " " + strBizDay + "的佣金收入已入账。此次入账任务忽略");
		ChannelMoneyLog moneyLog = new ChannelMoneyLog();
		moneyLog.setChannelId(channelId);
		moneyLog.setType(ChannelMoneyEnum.COMMISSION);
		moneyLog.setLogMoney(sumCommAmt);
		moneyLog.setLogText(strBizDay);
		moneyLog.setBizTime(bizDay);
		moneyLog.setRefId(null);
		channelMoneyLogService.changeMoney(moneyLog);
	}
}
