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


import com.neusoft.smart.pos.dto.trade.request.GetMemberOrdersRequest;
import com.neusoft.smart.pos.dto.trade.response.GetMemberOrdersResponse;
import com.neusoft.smart.pos.framework.constants.ApplicationConstants;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteMchOrderService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteShopService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("mchOrderService")
public class MchOrderService {
    
    @Autowired
    private RemoteMchOrderService remoteMchOrderService;
    
    @Autowired
    private RemoteShopService remoteShopService;
    
    /**
     * 订单信息创建方法
     *
     * @return
     */
    public GetMemberOrdersResponse queryMemberOrders(GetMemberOrdersRequest memberOrdersRequest) {
        //1.查询商户订单列表
        val memberOrdersResponseData = remoteMchOrderService.queryMemberOrders(memberOrdersRequest.getMcode(),
                memberOrdersRequest.getSign(),
                memberOrdersRequest.getMemberCardNo(),
                memberOrdersRequest.getOrderType(),
                memberOrdersRequest.getStartTime(),
                memberOrdersRequest.getEndTime(),
                memberOrdersRequest.getOffset(),
                memberOrdersRequest.getLimit(),
                memberOrdersRequest.getIsPage());
        if (null == memberOrdersResponseData || !ApplicationConstants.SUCCESS_CODE.equals(memberOrdersResponseData.getCode())) {
            throw new BusinessCommonException(memberOrdersResponseData);
        }
        //2.处理门店名称信息
        if(null != memberOrdersResponseData.getResult().getPaginationExtendBean().getRows()
                &&memberOrdersResponseData.getResult().getPaginationExtendBean().getRows().size() >0){
            Integer orgId = memberOrdersResponseData.getResult().getPaginationExtendBean().getRows().get(0).getOrgId();
            val shopListResponse = remoteShopService.queryShopListByOrgId(orgId,0);
            for(int i =0 ;i<memberOrdersResponseData.getResult().getPaginationExtendBean().getRows().size();i++){
                for(int j=0;j<shopListResponse.getResult().size();j++){
                    if(memberOrdersResponseData.getResult().getPaginationExtendBean().getRows().get(i).getShopId().toString().equals(shopListResponse.getResult().get(j).getId().toString())){
                        memberOrdersResponseData.getResult().getPaginationExtendBean().getRows().get(i).setShopName(shopListResponse.getResult().get(j).getName());
                        break;
                    }
                }
            }
        }
        
        return memberOrdersResponseData.getResult();
    }
    
    
}
