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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.cache.DictBizCache;
import org.springblade.common.cache.DictCache;
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.Order;
import org.springblade.modules.finance.entity.OrderConsign;
import org.springblade.modules.finance.service.IOrderConsignService;
import org.springblade.modules.finance.vo.OrderConsignVO;
import org.springblade.modules.finance.vo.OrderVO;
import org.springblade.modules.shopping.entity.Goods;
import org.springblade.modules.shopping.service.IGoodsService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

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

	public static OrderConsignWrapper build() {
		return new OrderConsignWrapper();
	}

	public static IGoodsService goodsService;

	public static IUserService userService;

	static {
		goodsService = SpringUtil.getBean(IGoodsService.class);
		userService = SpringUtil.getBean(IUserService.class);
	}

	@Override
	public OrderConsignVO entityVO(OrderConsign entity) {
		OrderConsignVO vo = Objects.requireNonNull(BeanUtil.copy(entity, OrderConsignVO.class));
		vo.setConsignTypeDisplayName(DictBizCache.getValue(DictBizEnum.CONSIGN_TYPE, entity.getConsignType()));
		vo.setIsConfirmDisplayName(DictBizCache.getValue(DictBizEnum.IS_CONFIRM, entity.getIsConfirm()));
		Goods goods = goodsService.getById(entity.getGoodsId());
		if (goods != null){
			vo.setProductNo(goods.getProductNo());
		}
		List<Long> ids = new ArrayList<>();
		if(entity.getConsignUserId() == 0 || entity.getConsignUserId() == null){
			vo.setConsignUserIdDisplayName("系统");
			vo.setConsignRealName("系统");
		}else{
			ids.add(entity.getConsignUserId());
		}
		ids.add(entity.getPayUserId());
		List<User> list = userService.listByIds(ids);
		if(list.size() == 1){
			vo.setPayUserIdDisplayName(list.get(0).getName());
			vo.setPayRealName(list.get(0).getRealName());
		}else{
			list.forEach(user -> {
				if(Objects.equals(user.getId(), vo.getPayUserId())) {
					vo.setPayUserIdDisplayName(user.getName());
					vo.setPayRealName(user.getRealName());
				}
				if(Objects.equals(user.getId(), vo.getConsignUserId())) {
					vo.setConsignUserIdDisplayName(user.getName());
					vo.setConsignRealName(user.getRealName());
				}
			});
		}
		return vo;
	}


}
