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

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.cache.DictBizCache;
import org.springblade.common.cache.DictCache;
import org.springblade.common.cache.ParamCache;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.enums.DictBizEnum;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.finance.entity.OrderGoods;
import org.springblade.modules.finance.service.IOrderGoodsService;
import org.springblade.modules.shopping.entity.Consign;
import org.springblade.modules.shopping.entity.ConsignConfig;
import org.springblade.modules.shopping.entity.Goods;
import org.springblade.modules.shopping.entity.GoodsAttributes;
import org.springblade.modules.shopping.service.IConsignConfigService;
import org.springblade.modules.shopping.service.IGoodsAttributesService;
import org.springblade.modules.shopping.service.IGoodsService;
import org.springblade.modules.shopping.vo.ConsignConfigVO;
import org.springblade.modules.shopping.vo.ConsignVO;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;

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

/**
 * 包装类,返回视图层所需的字段
 *
 * @author Chill
 */
public class ConsignWrapper extends BaseEntityWrapper<Consign, ConsignVO> {

	public static ConsignWrapper build() {
		return new ConsignWrapper();
	}
	public static IGoodsService goodsService;
	public static IUserService userService;
	public static IGoodsAttributesService goodsAttributesService;
	public static IConsignConfigService consignConfigService;

	static {
		goodsService = SpringUtil.getBean(IGoodsService.class);
		userService = SpringUtil.getBean(IUserService.class);
		goodsAttributesService =  SpringUtil.getBean(IGoodsAttributesService.class);
		consignConfigService = SpringUtil.getBean(IConsignConfigService.class);
	}

	@Override
	public ConsignVO entityVO(Consign entity) {
		ConsignVO vo = Objects.requireNonNull(BeanUtil.copy(entity, ConsignVO.class));
		vo.setConsignForDisplayName(DictBizCache.getValue(DictBizEnum.CONSIGN_FOR, entity.getConsignFor()));
		vo.setConsignStateDisplayName(DictBizCache.getValue(DictBizEnum.CONSIGN_STATE, entity.getConsignState()));
		vo.setIsDeliveryDisplayName(DictCache.getValue(DictEnum.YES_NO, entity.getIsDelivery()));
		vo.setIsSaleDisplayName(DictCache.getValue(DictEnum.YES_NO, entity.getIsSale()));
		Goods goods = goodsService.getById(entity.getGoodsId());
		User user = userService.getById(entity.getConsignUserId());
		if (goods != null){
			vo.setProductNo(goods.getProductNo());
			List<GoodsAttributes> list = goodsAttributesService.list(Wrappers.<GoodsAttributes>lambdaQuery().eq(GoodsAttributes::getGoodsId, vo.getId()));
			vo.setGoodsAttributes(GoodsAttributesWrapper.build().listVO(list));
			//商品规格项和值
			//vo.setSpecItems(apiService.specItemsList(vo.getId()));
			vo.setGoodsImg(goods.getGoodsImg());
			vo.setGallery(goods.getGallery());
			vo.setGoodsLabel(goods.getGoodsLabel());
			vo.setTextImg(goods.getTextImg());
		}
		if(user != null){
			vo.setConsignUserIdDisplayName(user.getName());
		}
		if (ObjectUtil.isNotNull(vo.getConsignConfigId())){
			ConsignConfig consignConfig = consignConfigService.getById(vo.getConsignConfigId());
			if (ObjectUtil.isNotNull(consignConfig)){
				vo.setConsignConfigDisplayName(consignConfig.getTitle());
			}
		}
		return vo;
	}

	/**
	 * 寄售溢价计算
	 * 	1、默认为1
	 * @param price 金额
	 * @param days 天数
	 * @return Double
	 */
	public Double consignPrice(Double price, Integer days){
		Double ratio = Double.valueOf(ParamCache.getValue(CommonConstant.DEFAULT_CONDIGN_RATIO_T1));;
		/*if(2 == days){
			ratio = Double.valueOf(ParamCache.getValue(CommonConstant.DEFAULT_CONDIGN_RATIO_T2));
		}else if(3 == days){
			ratio = Double.valueOf(ParamCache.getValue(CommonConstant.DEFAULT_CONDIGN_RATIO_T3));
		}else {
			ratio = Double.valueOf(ParamCache.getValue(CommonConstant.DEFAULT_CONDIGN_RATIO_T1));
		}*/
		double ratioPrice = NumberUtil.mul(price, ratio); //0.1 * 0.05 = 0.005
		return  new BigDecimal(price).add(new BigDecimal(ratioPrice))
			.setScale(2,BigDecimal.ROUND_DOWN).doubleValue();
	}

	/**
	 * 获取寄售手续费
	 *
	 * @param score
	 * @return Double
	 */
	public Double getScore(Double score){
		Double ratio = Double.valueOf(ParamCache.getValue(CommonConstant.DEFAULT_SCORE_RATIO));
		return NumberUtil.roundDown(NumberUtil.mul(score, ratio) , 2).doubleValue();
	}

	/**
	 * 获取寄售手续费
	 *
	 * @param consignPrice
	 * @return Double
	 */
	public Double getPrice(Double consignPrice){
		Double ratio = Double.valueOf(ParamCache.getValue(CommonConstant.DEFAULT_CONDIGN_RATIO_T1));
		double ratioPrice = NumberUtil.mul(consignPrice, ratio); //0.1 * 0.05 = 0.005
		return  new BigDecimal(consignPrice).add(new BigDecimal(ratioPrice))
			.setScale(2,BigDecimal.ROUND_DOWN).doubleValue();
	}

}
