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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.common.constant.Constants;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.enterprise.entity.Staff;
import org.springblade.enterprise.feign.IStaffClient;
import org.springblade.enterprise.vo.StaffVO;
import org.springblade.party.entity.Activity;

import org.springblade.party.entity.ActivityEnroll;
import org.springblade.party.entity.PartyMember;
import org.springblade.party.service.IActivityEnrollService;
import org.springblade.party.service.IPartyMemberService;
import org.springblade.party.vo.ActivityVO;

import org.springblade.system.expand.feign.IConfClient;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Objects;

/**
 * 党内活动表包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-04-19
 */
public class ActivityWrapper extends BaseEntityWrapper<Activity, ActivityVO>  {

	private IActivityEnrollService activityEnrollService;
	private IPartyMemberService partyMemberService;
	private IStaffClient staffClient;
	public static IConfClient confClient;

	static {
		confClient = SpringUtil.getBean(IConfClient.class);
	}

	private ActivityWrapper(IActivityEnrollService activityEnrollService,IPartyMemberService partyMemberService,IStaffClient staffClient){
		this.activityEnrollService = activityEnrollService;
		this.partyMemberService = partyMemberService;
		this.staffClient = staffClient;
	}
	public static ActivityWrapper build(IActivityEnrollService activityEnrollService,IPartyMemberService partyMemberService,IStaffClient staffClient) {
		return new ActivityWrapper(activityEnrollService,partyMemberService,staffClient);
	}
	@Override
	public ActivityVO entityVO(Activity activity) {
		ActivityVO activityVO = Objects.requireNonNull(BeanUtil.copy(activity, ActivityVO.class));
		activityVO.setImg(confClient.addPrefix(activityVO.getImg()));
		List<ActivityEnroll> activityEnrolls = activityEnrollService.getBaseMapper().selectList(new QueryWrapper<>(new ActivityEnroll()).lambda()
			.eq(ActivityEnroll::getActivityId, activity.getId()));

		activityVO.setEnrollCount(activityEnrolls.size());

		List<String> enrollPicList = new ArrayList<>();
		activityEnrolls.stream().forEach(activityEnroll -> {
			PartyMember partymember = partyMemberService.getById(activityEnroll.getMemberId());
			//目前物业系统app用户的微信头像没有在数字园区，只查询园区中党员的头像信息，待微服务重构后要实现园区住户党员的微信头像
			if(partymember != null&&partymember.getPersonType() == Constants.PARTY_MEMBER_TYPE_ENTERPRISESTAFF){
				Staff enterpriseStaff = new Staff();
				enterpriseStaff.setId(partymember.getPersonId());
				StaffVO enterpriseStaffVO = staffClient.detail(enterpriseStaff);
				if(enterpriseStaffVO != null){
					enrollPicList.add(enterpriseStaffVO.getAvatar());
				}
			}
		});
		if (StringUtils.hasText(activityVO.getContent())){
			activityVO.setContent(new String(Base64.getDecoder().decode(activityVO.getContent().getBytes(StandardCharsets.UTF_8))));
		}

		return activityVO;
	}

}
