package org.springblade.modules.ldt.billrecord.wrapper;

import com.alibaba.fastjson.support.geo.Point;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.common.constant.SystemConstant;
import org.springblade.common.enums.PayWay;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.ldt.billrecord.entity.BillRecord;
import org.springblade.modules.ldt.billrecord.vo.BillRecordVO;
import org.springblade.modules.ldt.loginuser.entity.LoginUser;
import org.springblade.modules.ldt.loginuser.service.ILoginUserService;
import org.springblade.modules.ldt.pointdetail.entity.PointDetail;
import org.springblade.modules.ldt.pointdetail.service.IPointDetailService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * Created By lidexi in 2021/8/9
 **/
public class BillRecordWrapper extends BaseEntityWrapper<BillRecord, BillRecordVO> {

	private ILoginUserService loginUserService;
	private IPointDetailService pointDetailService;


	private BillRecordWrapper(ILoginUserService loginUserService,IPointDetailService pointDetailService){
		this.loginUserService = loginUserService;
		this.pointDetailService = pointDetailService;
	}
	public static BillRecordWrapper build(ILoginUserService loginUserService,IPointDetailService pointDetailService){
		return new BillRecordWrapper(loginUserService,pointDetailService);
	}
	@Override
	public BillRecordVO entityVO(BillRecord billRecord) {
		BillRecordVO billRecordVO = Objects.requireNonNull(BeanUtil.copy(billRecord, BillRecordVO.class));
		LoginUser loginUser = loginUserService.getById(billRecord.getUserId());
		if(billRecord.getUserId() == null){
			if(billRecord.getPayWay().equals(PayWay.ALIPAY.getName())){
				billRecordVO.setPayWay(PayWay.ALIPAY.getName());
				billRecordVO.setUserName("支付宝用户");
			}else if(billRecord.getPayWay().equals(PayWay.WECHAT.getName())){
				billRecordVO.setPayWay(PayWay.WECHAT.getName());
				billRecordVO.setUserName("微信用户");
			}

		}
		if(loginUser != null){
			billRecordVO.setUserAvator(loginUser.getAvatar());
			billRecordVO.setUserName(loginUser.getNickName());

			List<PointDetail> pointDetails = pointDetailService.getBaseMapper().selectList(new QueryWrapper<>(new PointDetail()).lambda()
				.eq(PointDetail::getShopId, billRecord.getShopId())
				.eq(PointDetail::getType, SystemConstant.PointDetailType.SHOP_SEND.getValue())
				.eq(PointDetail::getUserId, billRecord.getUserId())
				.eq(PointDetail::getBillRecordId, billRecord.getId()));

			if(pointDetails.size()>0){
				billRecordVO.setPoint(pointDetails.get(0).getPoint());
				billRecordVO.setPointValue(pointDetails.get(0).getPointValue());
			}
			if(billRecord.getRealPayAmount().compareTo(BigDecimal.ZERO) == 0){
				billRecordVO.setPayWay("积分支付");
			}else if(billRecord.getRealPayAmount().compareTo(billRecord.getPrice()) == 0){
				billRecordVO.setPayWay("现金支付");
			}else{
				billRecordVO.setPayWay("混合支付");
			}
			//通兑应收
			List<PointDetail> pointDetails1 = pointDetailService.getBaseMapper().selectList(new QueryWrapper<>(new PointDetail()).lambda()
				.eq(PointDetail::getShopId, billRecord.getShopId())
				.eq(PointDetail::getType, SystemConstant.PointDetailType.USER_PAY.getValue())
				.eq(PointDetail::getUserId, billRecord.getUserId())
				.eq(PointDetail::getBillRecordId, billRecord.getId()));

			if(pointDetails1.size()>0){
				BigDecimal tongdui = pointDetails1.stream().map(PointDetail::getPointValue).reduce(
					BigDecimal.ZERO, BigDecimal::add
				);
				billRecordVO.setTongduiAccount(tongdui);
			}
		}
		return billRecordVO;
	}
}
