package com.neusoft.smart.pos.member.service;

import com.neusoft.smart.pos.member.persistent.domain.MemberBusinessDict;
import com.neusoft.smart.pos.member.persistent.domain.MemberBusinessDictExample;
import com.neusoft.smart.pos.member.persistent.mapper.MemberBusinessDictMapper;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberBusinessService {
	
	@Autowired
	private MemberBusinessDictMapper memberBusinessDictMapper;
	
	public void create(Integer orgId, Integer businessDict){
		MemberBusinessDict memberBusinessDict = new MemberBusinessDict();
		memberBusinessDict.setBusiness(businessDict);
		memberBusinessDict.setOrgId(orgId);
		memberBusinessDictMapper.insertSelective(memberBusinessDict);
	}
	
	public Integer getBusiness() {
		return getBusiness(RequestHeaderUtil.getOrgId());
	}

	public Integer getBusiness(Integer orgId) {
		try {
			Integer tempOrg = (null == orgId ? RequestHeaderUtil.getOrgId() : orgId);
			MemberBusinessDictExample memberBusinessDictExample = new MemberBusinessDictExample();
			memberBusinessDictExample.createCriteria().andOrgIdEqualTo(tempOrg);
			List<MemberBusinessDict> businessDicts = memberBusinessDictMapper.selectByExample(memberBusinessDictExample);
			if (businessDicts.isEmpty()) {
				return 1;
			} else {
				return businessDicts.get(0).getBusiness();
			}
		} catch (Exception e) {
			return 1;
		}
	}

}
