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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.common.Constants;
import org.springblade.community.staff.entity.EnterpriseStaff;
import org.springblade.community.staff.service.IEnterpriseStaffService;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.party.activity.entity.Activity;
import org.springblade.party.activity.vo.ActivityVO;
import org.springblade.party.activityenroll.entity.ActivityEnroll;
import org.springblade.party.activityenroll.service.IActivityEnrollService;
import org.springblade.party.member.entity.Member;
import org.springblade.party.member.service.IMemberService;
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-01
 */
public class ActivityWrapper extends BaseEntityWrapper<Activity, ActivityVO>  {

	private IActivityEnrollService activityEnrollService;
	private IMemberService memberService;
	private IEnterpriseStaffService enterpriseStaffService;

	private ActivityWrapper(IActivityEnrollService activityEnrollService,IMemberService memberService,IEnterpriseStaffService enterpriseStaffService){
		this.activityEnrollService = activityEnrollService;
		this.memberService = memberService;
		this.enterpriseStaffService = enterpriseStaffService;
	}
	public static ActivityWrapper build(IActivityEnrollService activityEnrollService,IMemberService memberService,IEnterpriseStaffService enterpriseStaffService) {
		return new ActivityWrapper(activityEnrollService,memberService,enterpriseStaffService);
 	}

	@Override
	public ActivityVO entityVO(Activity activity) {
		ActivityVO activityVO = Objects.requireNonNull(BeanUtil.copy(activity, ActivityVO.class));

		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 -> {
			Member member = memberService.getById(activityEnroll.getMemberId());
			//目前物业系统app用户的微信头像没有在数字社区，只查询园区中党员的头像信息，待微服务重构后要实现社区住户党员的微信头像
			if(member != null&&member.getPersonType() == Constants.PARTY_MEMBER_TYPE_ENTERPRISESTAFF){
				EnterpriseStaff enterpriseStaff = enterpriseStaffService.getById(member.getPersonId());
				if(enterpriseStaff != null){
					enrollPicList.add(enterpriseStaff.getAvatar());
				}
			}
		});
		if (StringUtils.hasText(activityVO.getContent())){
			activityVO.setContent(new String(Base64.getDecoder().decode(activityVO.getContent().getBytes(StandardCharsets.UTF_8))));
		}

		return activityVO;
	}

}
