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

import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.org.request.PostOrgRequest;
import com.neusoft.smart.pos.dto.org.response.PostOrgResponse;
import com.neusoft.smart.pos.dto.trade.request.PostSettlementRequest;
import com.neusoft.smart.pos.framework.constants.ApplicationConstants;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteMemberService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteMenuService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteOrgService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteTradeSettlementService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrgService {
	
	@Autowired
	private RemoteOrgService remoteOrgService;
	
	@Autowired
	private RemoteTradeSettlementService remoteTradeSettlementService;
	
	@Autowired
	private RemoteMemberService remoteMemberService;
	
	/**
	 * 集团信息保存接口
	 *
	 * @param orgRequest
	 * @return
	 */
	public PostOrgResponse saveOrg(PostOrgRequest orgRequest) {
		//1.调用集团门店管理服务，新建集团信息
		ResponseData<PostOrgResponse> responseData = remoteOrgService.orgInsert(orgRequest);
		val orgResponse = responseData.getResult();
		
		//2.添加集团结算信息，默认开通现金支付
		if (ApplicationConstants.SUCCESS_CODE.equals(responseData.getCode()) && null != orgResponse) {
			val settlementRequest = new PostSettlementRequest();
			settlementRequest.setOrgId(orgResponse.getId());
			settlementRequest.setMchName(orgResponse.getName());
			settlementRequest.setShopId(0);
			settlementRequest.setSettlementType(DictionaryConstant.SETTLEMENT_TYPE_JT.getCode());
			remoteTradeSettlementService.saveSettlement(settlementRequest);
			
			//添加会员业务类型 FIXME 临时都默认为普通业务 食堂相关手动修改数据库
			remoteMemberService.createBusiness(orgResponse.getId(), 1);
		} else {
			throw new BusinessCommonException(responseData);
		}
		return responseData.getResult();
	}
	
}
