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


import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.member.request.MemberRechargeRequest;
import com.neusoft.smart.pos.dto.member.request.MemberRefundRequest;
import com.neusoft.smart.pos.dto.member.request.MemberTransactionRequest;
import com.neusoft.smart.pos.dto.member.response.DetailedMemberResponse;
import com.neusoft.smart.pos.dto.org.response.PostShopResponse;
import com.neusoft.smart.pos.dto.promotion.request.GetPreferentialPriceRequest;
import com.neusoft.smart.pos.dto.promotion.response.GetPreferentialPriceResponse;
import com.neusoft.smart.pos.dto.trade.request.*;
import com.neusoft.smart.pos.dto.trade.response.*;
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.framework.task.TaskExecutor;
import com.neusoft.smart.pos.framework.utils.BeanUtil;
import com.neusoft.smart.pos.framework.utils.DateUtils;
import com.neusoft.smart.pos.gateway.constants.MemberErrorCode;
import com.neusoft.smart.pos.gateway.service.feignService.*;
import com.neusoft.smart.pos.gateway.task.RecordOrderTask;
import com.neusoft.smart.pos.redis.bean.DeviceInfo;
import com.neusoft.smart.pos.utils.PosDeviceRedisUtils;
import com.neusoft.smart.pos.utils.PosMemberRedisUtils;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("billService")
public class BillService {
    
    
    @Autowired
    private RemoteBillService remoteBillService;
    
    @Autowired
    private RemoteShopService remoteShopService;
    
    /**
     * 订单信息创建方法
     *
     * @return
     */
    public GetNoAcceptBillsResponse queryNoAcceptBills(String billNumber,String rebuild) {
        //1.根据集团及门店ID查询门店列表信息
        ResponseData<List<PostShopResponse>> shopListResponse = remoteShopService.queryShopListByCurrentUser();
    
        if(null == shopListResponse || !ApplicationConstants.SUCCESS_CODE.equals(shopListResponse.getCode())){
            throw new BusinessCommonException(shopListResponse);
        }
        List<Integer> shopIds = new ArrayList<>();
        Integer orgId = 0;
        if(null != shopListResponse.getResult() && shopListResponse.getResult().size() > 0){
            orgId = shopListResponse.getResult().get(0).getOrg();
            shopListResponse.getResult().forEach(shop->{
                shopIds.add(shop.getId());
            });
        }
    
        val noAcceptBillsResponse = remoteBillService.queryNoAcceptBills(orgId,shopIds,billNumber,rebuild);
        //2.判断返回信息是否错误
        if(null == noAcceptBillsResponse || !ApplicationConstants.SUCCESS_CODE.equals(noAcceptBillsResponse.getCode())){
            throw new BusinessCommonException(noAcceptBillsResponse);
        }
    
        //3.整理返回数据信息
        for(int i =0 ;i<noAcceptBillsResponse.getResult().getBillList().size();i++){
            for(int j=0;j<shopListResponse.getResult().size();j++){
                if(noAcceptBillsResponse.getResult().getBillList().get(i).getShopId().toString().equals(shopListResponse.getResult().get(j).getId().toString())){
                    noAcceptBillsResponse.getResult().getBillList().get(i).setShopName(shopListResponse.getResult().get(j).getName());
                    break;
                }
            }
        }
        return noAcceptBillsResponse.getResult();
    }
    
    /**
     * 已归档账单信息查询方法
     *
     * @return
     */
    public GetAcceptBillsResponse queryAcceptBills(List<Integer> shopIds,String startTime,String endTime) {
        //1.根据集团及门店ID查询门店列表信息
        ResponseData<List<PostShopResponse>> shopListResponse = remoteShopService.queryShopListByCurrentUser();
        
        if(null == shopListResponse || !ApplicationConstants.SUCCESS_CODE.equals(shopListResponse.getCode())){
            throw new BusinessCommonException(shopListResponse);
        }
        Integer orgId = 0;
        if(null != shopListResponse.getResult() && shopListResponse.getResult().size() > 0){
            orgId = shopListResponse.getResult().get(0).getOrg();
            if(null == shopIds || shopIds.size() == 0){
                shopListResponse.getResult().forEach(shop->{
                    shopIds.add(shop.getId());
                });
            }
        }
        
        val acceptBillsResponse = remoteBillService.queryAcceptBills(orgId,shopIds,startTime,endTime);
        //2.判断返回信息是否错误
        if(null == acceptBillsResponse || !ApplicationConstants.SUCCESS_CODE.equals(acceptBillsResponse.getCode())){
            throw new BusinessCommonException(acceptBillsResponse);
        }
        
        //3.整理返回数据信息
        for(int i =0 ;i<acceptBillsResponse.getResult().getBillMonthList().size();i++){
            for(int z=0;z<acceptBillsResponse.getResult().getBillMonthList().get(i).getBillList().size();z++){
                for(int j=0;j<shopListResponse.getResult().size();j++){
                    if(acceptBillsResponse.getResult().getBillMonthList().get(i).getBillList().get(z).getShopId().toString().equals(shopListResponse.getResult().get(j).getId().toString())){
                        acceptBillsResponse.getResult().getBillMonthList().get(i).getBillList().get(z).setShopName(shopListResponse.getResult().get(j).getName());
                        break;
                    }
                }
            }
        }
        return acceptBillsResponse.getResult();
    }
    
    /**
     * 商户未结算账单信息查询方法
     * @return
     */
    public GetBillDetailsResponse queryBillDetails(GetBillDetailsRequest billDetailsRequest){
        val billDetailsResponseData = remoteBillService.queryBillDetails(billDetailsRequest.getShopId(),
                billDetailsRequest.getBillNumber(),
                billDetailsRequest.getOffset(),
                billDetailsRequest.getLimit(),
                billDetailsRequest.getIsPage());
        ResponseData<PostShopResponse> shopResponse = remoteShopService.getShopDetail(billDetailsRequest.getShopId());
        //2.判断返回信息是否错误
        if(null == billDetailsResponseData || !ApplicationConstants.SUCCESS_CODE.equals(billDetailsResponseData.getCode())){
            throw new BusinessCommonException(billDetailsResponseData);
        }
        //3.整理返回数据信息
        for(int i =0 ;i<billDetailsResponseData.getResult().getPaginationExtendBean().getRows().size();i++){
            billDetailsResponseData.getResult().getPaginationExtendBean().getRows().get(i).setShopName(shopResponse.getResult().getName());
        }
        return billDetailsResponseData.getResult();
    }
    
}
