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


import com.neusoft.smart.pos.dto.org.response.PostShopResponse;
import com.neusoft.smart.pos.dto.trade.response.GetBillSettlementRuleResponse;
import com.neusoft.smart.pos.dto.trade.response.GetBillSettlementRulesResponse;
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.RemoteBillRuleService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteShopService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service("billRuleService")
public class BillRuleService {
    
    
    @Autowired
    private RemoteBillRuleService remoteBillRuleService;
    
    @Autowired
    private RemoteShopService remoteShopService;
    
    /**
     * 集团规则查询方法
     *
     * @return
     */
    public GetBillSettlementRuleResponse queryBillSettlementRule(Integer ruleId) {
        val billSettlementRuleResponse = remoteBillRuleService.queryBillSettlementRule(ruleId);
        if(null == billSettlementRuleResponse || !ApplicationConstants.SUCCESS_CODE.equals(billSettlementRuleResponse.getCode())){
            throw new BusinessCommonException(billSettlementRuleResponse);
        }
    
        if(!StringUtils.isEmpty(billSettlementRuleResponse.getResult().getShopId())){
            if(0 != billSettlementRuleResponse.getResult().getShopId()){
                ResponseData<PostShopResponse> shopResponse = remoteShopService.getShopDetail(billSettlementRuleResponse.getResult().getShopId());
                billSettlementRuleResponse.getResult().setShopName(shopResponse.getResult().getName());
            }else{
                billSettlementRuleResponse.getResult().setShopName("");
            }
        }else{
        }
        return billSettlementRuleResponse.getResult();
    }
    
    /**
     * 集团规则查询方法
     *
     * @return
     */
    public GetBillSettlementRulesResponse queryBillSettlementRules() {
        val billSettlementRuleResponse = remoteBillRuleService.queryBillSettlementRules();
        if(null == billSettlementRuleResponse || !ApplicationConstants.SUCCESS_CODE.equals(billSettlementRuleResponse.getCode())){
            throw new BusinessCommonException(billSettlementRuleResponse);
        }
        billSettlementRuleResponse.getResult().getBillSettlementRules().forEach(billSettlementRule->{
            if(0 != billSettlementRule.getShopId()){
                ResponseData<PostShopResponse> shopResponse = remoteShopService.getShopDetail(billSettlementRule.getShopId());
                billSettlementRule.setShopName(shopResponse.getResult().getName());
            }else{
                billSettlementRule.setShopName("");
            }
        });
        return billSettlementRuleResponse.getResult();
    }
}
