/*
 *      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.common_gateway.wrapper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ObjectUtils;
import org.springblade.common.enums.AppConstant;
import org.springblade.common.enums.OrderType;
import org.springblade.common.utils.SpringContextHolder;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.gateway.common_gateway.entity.vo.AppBillsVO;
import org.springblade.ldt.bills.entity.BalanceBills;
import org.springblade.ldt.bills.entity.Bills;
import org.springblade.ldt.bills.service.IBalanceBillsService;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IShopService;
import org.springblade.ldt.user.entity.LoginUser;
import org.springblade.ldt.user.service.ILoginUserService;

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

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-08-31
 */
public class AppBillsWrapper extends BaseEntityWrapper<Bills, AppBillsVO>  {

	private ILoginUserService loginUserService;
	private IShopService shopService;
	private final static IBalanceBillsService balanceBillsService = SpringContextHolder.getBean(IBalanceBillsService.class);

	public AppBillsWrapper(ILoginUserService loginUserService) {
		this.loginUserService = loginUserService;
	}

	public AppBillsWrapper(IShopService shopService) {
		this.shopService = shopService;
	}

	public static AppBillsWrapper build(ILoginUserService loginUserService) {
		return new AppBillsWrapper(loginUserService);
 	}

	public static AppBillsWrapper build(IShopService shopService) {
		return new AppBillsWrapper(shopService);
	}

	@Override
	public AppBillsVO entityVO(Bills bills) {
		AppBillsVO appBillsVO = Objects.requireNonNull(BeanUtil.copy(bills, AppBillsVO.class));

		if (loginUserService!=null) {
			LoginUser loginUser = loginUserService.getById(appBillsVO.getPayId());
			if (loginUser!=null) {
				appBillsVO.setNickName(loginUser.getNickName());
				appBillsVO.setAvatar(loginUser.getAvatar());
				appBillsVO.setPhone(loginUser.getPhone());
				appBillsVO.setSex(loginUser.getSex());
			}
		}
		if (shopService != null) {
			Shop shop = shopService.getById(appBillsVO.getReceiveId());
			if (shop != null) {
				appBillsVO.setReceiverName(shop.getName());
				appBillsVO.setReceiverLogo(shop.getCover());
			}
		}
		//查询积分账单
		BalanceBills balanceBills = balanceBillsService.getOne(
			Wrappers.<BalanceBills>lambdaQuery()
				.eq(BalanceBills::getTradeNo, appBillsVO.getId()).eq(BalanceBills::getType, OrderType.SHOP_SEND.name())
				.eq(BalanceBills::getPayStatus, AppConstant.BillPayStatus.付款成功.name())
				.last("limit 0,1"));
		if(ObjectUtils.isNotEmpty(balanceBills)){
			appBillsVO.setSendPoint(Optional.ofNullable(balanceBills.getPrice()).orElse(BigDecimal.ZERO).doubleValue());
		}
		return appBillsVO;
	}

}
