package com.gic.haoban.manage.service.service.impl;

import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gic.commons.util.EntityUtil;
import com.gic.haoban.common.utils.StringUtil;
import com.gic.haoban.manage.api.dto.MemberUnionidRelatedDTO;
import com.gic.haoban.manage.service.dao.mapper.MemberUnionidRelatedMapper;
import com.gic.haoban.manage.service.entity.MemberUnionidRelated;
import com.gic.haoban.manage.service.service.MemberUnionRelatedService;

@Service
public class MemberUnionRelatedServiceImpl implements MemberUnionRelatedService {

	@Autowired
	private MemberUnionidRelatedMapper mapper;
	
	@Override
	public String addMemberUnionidRelated(MemberUnionidRelatedDTO dto) {
	    String wxEnterpriseId = dto.getWxEnterpriseId() ;
	    String staffId = dto.getStaffId() ;
	    String selfExternalUserId = dto.getSelfExternalUserid() ;
	    MemberUnionidRelated oldDTO = this.mapper.getBySelfExternalUserId(staffId, wxEnterpriseId, selfExternalUserId) ;
	    if(null != oldDTO) {
	        return oldDTO.getMemberUnionidRelatedId() ;
	    }
		dto.setStatusFlag(1);
		dto.setCreateTime(new Date());
		dto.setUpdateTime(new Date());
		dto.setMemberUnionidRelatedId(StringUtil.randomUUID());
		String unionid = dto.getUnionid() ;
		// unionid不保存,但需要传递
		dto.setUnionid(null);
		mapper.insert(EntityUtil.changeEntityByJSON(MemberUnionidRelated.class, dto));
		dto.setUnionid(unionid);
		return dto.getMemberUnionidRelatedId();
	}

	@Override
	public MemberUnionidRelatedDTO getBySelfExternalUserId(String wxEnterpriseId, String selfExternalUserId) {
		MemberUnionidRelated unionidRelated = mapper.getBySelfExternalUserId(null,wxEnterpriseId, selfExternalUserId);
		return EntityUtil.changeEntityNew(MemberUnionidRelatedDTO.class, unionidRelated);
	}
}
