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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springblade.common.utils.SpringContextHolder;
import org.springblade.common.utils.TenantIgnoreUtil;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.ldt.agent.entity.AgentRecord;
import org.springblade.ldt.agent.vo.AgentRecordVO;
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 org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;

import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-09-23
 */
@AllArgsConstructor
public class AgentRecordWrapper extends BaseEntityWrapper<AgentRecord, AgentRecordVO> {
	private static final ILoginUserService loginUserService = SpringUtil.getBean(ILoginUserService.class);
	private static final IShopService shopService = SpringUtil.getBean(IShopService.class);
	private static final TenantIgnoreUtil tenantIgnoreUtil = SpringUtil.getBean(TenantIgnoreUtil.class);
	private static final IUserService userService = SpringContextHolder.getBean(IUserService.class);

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

	@Override
	public AgentRecordVO entityVO(AgentRecord agentRecord) {
		AgentRecordVO agentRecordVO = Objects.requireNonNull(BeanUtil.copy(agentRecord, AgentRecordVO.class));

		//User createUser = UserCache.getUser(agentRecord.getCreateUser());
		//User updateUser = UserCache.getUser(agentRecord.getUpdateUser());
		//agentRecordVO.setCreateUserName(createUser.getName());
		//agentRecordVO.setUpdateUserName(updateUser.getName());

		//查询代理信息
		LoginUser agenterUser = (LoginUser) tenantIgnoreUtil.getById(loginUserService,agentRecordVO.getAccountId());
		if (agenterUser != null) {
			agentRecordVO.setAccountName(agenterUser.getNickName());
			agentRecordVO.setBalance(agenterUser.getBalance());
		} else {
			agentRecordVO.setAccountName(ObjectUtils.isNotEmpty(agentRecordVO.getAccountId()) ? agentRecordVO.getAccountId().toString() : null);
		}
		//查询业务员名字
		if (ObjectUtils.isNotEmpty(agentRecordVO.getSalesmanId())) {
			User user = userService.getById(agentRecordVO.getSalesmanId());
			if (ObjectUtils.isNotEmpty(user)) {
				agentRecordVO.setSalesmanName(user.getName());
			}
		}

		//获取推广数量
		agentRecordVO.setPromoteCount(tenantIgnoreUtil.count(shopService,
			new QueryWrapper<Shop>().lambda()
				.eq(Shop::getAgenter, agentRecordVO.getAccountId())));
		return agentRecordVO;
	}
}

