package com.bjtdba.userCenter.service.impl;

import com.bjtdba.userCenter.dao.*;
import com.bjtdba.userCenter.service.UserInviteService;
import com.bjtdba.userCenter.vo.InviteProfitVo;
import com.bjtdba.userCenter.vo.UserInviteVo;
import com.bjtdba.utils.ResponseUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserInviteServiceImpl implements UserInviteService {

    /**
     * 金额记录
     */
    @Autowired
    private UserBillDao userBillDao;
    /**
     * 用户
     */
    @Autowired
    private UserDao userDao;

    /**
     * 大仓用户
     */
    @Autowired
    private DaCangUserDao daCangUserDao;
    /**
     *  商户
     */
    @Autowired
    private MerchantAnnualCostOrderDao merchantAnnualCostOrder;
    @Autowired
    private MerchantDao merchantDao;
    @Autowired
    private MerchantIntentionDao merchantIntentionDao;
    /**
     * 渠道商
     */
    @Autowired
    private ChannelUserDao channelUserDao;

    /**
     * 当前收益= 平台可结算-已提现的值(审核中、已提现)
     * 未结算 = status 0 待定
     * 昨日收益 = 昨天已结算+昨天未结算+create_time
     * 累计收益？已结算+未结算 status 0待定 1有效   pm =1
     * 累计提现：rrx_user_extract +用户ID
     * @param uid
     * @return
     */
    @Override
    public Object userInviteProfit(Integer uid) {
        Map<String, Object> data = new HashMap<>();
        //查询平台可结算全部金额 rrx_user_bill

        //查询用户已经提现多少钱（审核中、已提现两个状态）rrx_user_extract

        //未结算 = status 0 待定 rrx_user_bill  select sum(number) numberhe FROM rrx_user_bill where pm =1 and status in (0) and commission_type in(3,10,9,1) and uid = "930" ;
        BigDecimal UserUnsettledInviteProfit = userBillDao.selectUserUnsettledInviteProfit(uid,new int[]{0}, new int[]{1,2,3,9,10},null);
        //昨日收益 = 昨天已结算+昨天未结算+create_time
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,-24);
        String yesterdayDate=dateFormat.format(calendar.getTime());
        BigDecimal UserYesterdayInviteProfit = userBillDao.selectUserUnsettledInviteProfit(uid,new int[]{1}, new int[]{1,2,3,4,6,7,8,9,10},yesterdayDate);
        //累计收益 rrx_user_extract
        BigDecimal numberSun = userBillDao.selectUserUnsettledInviteProfit(uid,new int[]{1}, new int[]{1,2,3,4,6,7,8,9,10}, null);
        //累计提现：rrx_user_extract +用户ID

        data.put("UserUnsettledInviteProfit",UserUnsettledInviteProfit);
        data.put("UserYesterdayInviteProfit",UserYesterdayInviteProfit);
        data.put("numberSun",numberSun);
        return  ResponseUtil.ok(data);
    }

    /**
     * 邀请
     */
    public Object userInviteList(Integer pageNum, Integer pageSize, Integer uid, String nickname){
        Map<String, Object> data = new HashMap<>();
        //1、查看多少用户（伙伴）select u.uid,u.nickname,u.promoter_time FROM rrx_user u where  spread_uid = "57"
        List<UserInviteVo> userInviteVoList = userDao.selectUserByUidandNickname(uid,nickname);
        int userCount = userInviteVoList.size();
        int merchantCount = 0;
        //2、查看是否是大仓会员
        for(int i = 0;i <userInviteVoList.size();i++){
            //if(daCangUserDao.selectUserByUid(userInviteVoList.get(i).getUid()) != null){}
            Integer InviteUid = userInviteVoList.get(i).getUid();
            String type = daCangUserDao.selectUserByUid(InviteUid);
            if("1".equals(type)){
                //merchantCount ++;
                userInviteVoList.get(i).setStatusType(userInviteVoList.get(i).getStatusType()+"、金卡会员");
            }else if("2".equals(type)){
                //merchantCount ++;
                userInviteVoList.get(i).setStatusType(userInviteVoList.get(i).getStatusType()+"、银卡会员");
            }
            /**
             * 3、商户：1、select * from rrx_merchant_annual_cost_order where uid = "1824"
             *         2、select * from rrx_merchant where uid = "1824"
             *         3、select * from rrx_merchant_intention where uid = "1824"
             */
            if(1 == merchantAnnualCostOrder.selectUserMerchantByUid(InviteUid) ||
                    1 == merchantDao.selectUserMerchantByUid(InviteUid)||
                    1 == merchantIntentionDao.selectUserMerchantByUid(InviteUid)){
                //merchantCount ++;
                userInviteVoList.get(i).setStatusType(userInviteVoList.get(i).getStatusType()+"、商户");
            }
            //4、渠道商
            if("null".equals(channelUserDao.selectUserChannelByUid(InviteUid))){
                //merchantCount ++;
                userInviteVoList.get(i).setStatusType(userInviteVoList.get(i).getStatusType()+"、渠道商");
            }
            //判断非伙伴的话，商户+1
            String[] split = userInviteVoList.get(i).getStatusType().split("、");
            if(split.length>1){
                merchantCount ++;
            }
        }
        if (!StringUtils.isEmpty(nickname) && (nickname.equals("商户") || nickname.equals("伙伴"))) {
            Iterator<UserInviteVo> iterator = userInviteVoList.iterator();
            while (iterator.hasNext()) {
                UserInviteVo item = iterator.next();
                if (!item.getStatusType().contains(nickname)) {
                    iterator.remove();
                }
            }
        }
        int total = userInviteVoList.size();
        PageInfo<UserInviteVo> pageInfo = null;
        if(total/pageSize+1>= pageNum){
            Page page = new Page(pageNum, pageSize);
            page.setTotal(total);
            //计算当前需要显示的数据下标起始值
            int startIndex = (pageNum - 1) * pageSize;
            int endIndex = Math.min(startIndex + pageSize,total);
            //从链表中截取需要显示的子链表，并加入到Page
            page.addAll(userInviteVoList.subList(startIndex,endIndex));
            pageInfo = new PageInfo<>(page);
        }else{
            Page page = new Page(0, 0);
            page.setTotal(0);
            List<UserInviteVo> a = new ArrayList<>();
            pageInfo = new PageInfo<>(a);
        }
        data.put("userCount",userCount);//伙伴
        data.put("MerchantCount",merchantCount);//商户
        data.put("userInviteVoList",pageInfo);
        return ResponseUtil.ok(data);
    }

    public Object userInviteProfitList(Integer pageNum,Integer pageSize,Integer uid,Integer commission_type){//0分享 8奖励收益
        Map<String, Object> data = new HashMap<>();
        //String status = "0";
        int[] statusList ={0};
        BigDecimal numberSun = userBillDao.selectUserUnsettledInviteProfit(uid, new int[]{1}, new int[]{1,2,3,4,6,7,8,9,10},null);//累计收益
        BigDecimal UserUnsettledInviteProfit = userBillDao.selectUserUnsettledInviteProfit(uid,new int[]{0}, new int[]{1,2,3,9,10},null);//未结算
        //分享收益  and commission_type in(3,10,9,1)
        BigDecimal shareProfit = userBillDao.selectUserUnsettledInviteProfit(uid, new int[]{1}, new int[]{3,10},null);//
        //奖励收益
        BigDecimal rewardProfit = userBillDao.selectUserUnsettledInviteProfit(uid,new int[]{1}, new int[]{8}, null);//
        //收益类型列表+分页
        PageHelper.startPage(pageNum,pageSize);
        List<InviteProfitVo> inviteProfitVoList = null;
        //List<Map> mapList= null;
        if(commission_type==null){
            inviteProfitVoList= userBillDao.selectUserUnsettledInviteProfitList(uid, new int[]{1}, new int[]{1,2,3,4,6,7,8,9,10});//累计所有收益

        }else if(commission_type==0){
            //分享收益  and commission_type in(3,10,9,1)
            inviteProfitVoList = userBillDao.selectUserUnsettledInviteProfitList(uid, new int[]{1}, new int[]{1,2,3,4,6,7,8,9,10});//分享
        }else if(commission_type==8){
            //奖励收益
            inviteProfitVoList = userBillDao.selectUserUnsettledInviteProfitList(uid, new int[]{1}, new int[]{8});//奖励列表
        }
        PageInfo<InviteProfitVo> pageInfo = new PageInfo<InviteProfitVo>(inviteProfitVoList);
        data.put("numberSun",numberSun);
        data.put("UserUnsettledInviteProfit",UserUnsettledInviteProfit);
        data.put("shareProfit",shareProfit);
        data.put("rewardProfit",rewardProfit);
        data.put("pageInfo",pageInfo);
        return ResponseUtil.ok(data);
    }




}
