/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.Json;
import com.yami.shop.distribution.comment.dao.DistributionBindSetMapper;
import com.yami.shop.distribution.comment.dao.DistributionUserBindMapper;
import com.yami.shop.distribution.comment.model.DistributionBindSet;
import com.yami.shop.distribution.comment.model.DistributionUserBind;
import com.yami.shop.distribution.comment.param.RangeTimeParam;
import com.yami.shop.distribution.comment.service.DistributionUserBindService;
import com.yami.shop.distribution.comment.vo.DistributionBindSceneVO;
import com.yami.shop.distribution.comment.vo.DistributionBindSetRobVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 *
 * @author lgh on 2019/04/01.
 */
@Service
public class DistributionUserBindServiceImpl extends ServiceImpl<DistributionUserBindMapper, DistributionUserBind> implements DistributionUserBindService {

    @Autowired
    private DistributionUserBindMapper distributionUserBindMapper;

    @Autowired
    private DistributionBindSetMapper distributionBindSetMapper;

    @Override
    public IPage<DistributionUserBind> distributionMsgsAndUserPage(Page page, DistributionUserBind distributionUserBind, RangeTimeParam bindTime, RangeTimeParam invalidTime, String userName, String parentName) {
        return distributionUserBindMapper.distributionMsgsAndUserPage(page,distributionUserBind,bindTime,invalidTime,userName,parentName);
    }

    @Override
    public Long getParentDistributionIdByShopIdAndUserId(Long shopId, String userId,Integer state) {
        return distributionUserBindMapper.getParentDistributionIdByShopIdAndUserId(shopId,userId,state);
    }

    @Override
    public boolean bindingUser(String userId, Long distributionUserId,Long shopId) {
        //获取分销设置
        DistributionBindSet distributionBindSet=distributionBindSetMapper.selectOne(new LambdaQueryWrapper<DistributionBindSet>()
                .eq(DistributionBindSet::getShopId,shopId)
        );
        //json转化
        distributionBindSet.setRob(Json.parseObject(distributionBindSet.getRobJson(), DistributionBindSetRobVO.class));
        distributionBindSet.setBindScene(Json.parseObject(distributionBindSet.getBindSceneJson(), DistributionBindSceneVO.class));

        //获取该用户生效的绑定数据
        DistributionUserBind distributionUserBind =  distributionUserBindMapper.selectOne(new LambdaQueryWrapper<DistributionUserBind>()
                .eq(DistributionUserBind::getShopId, Constant.PLATFORM_SHOP_ID)
                .eq(DistributionUserBind::getState,1)
                .eq(DistributionUserBind::getUserId,userId)
        );

        if(distributionUserBind == null){
            //如果该用户没绑定人,则进行关系绑定
            //获取到有没有 该用户与分销员的预绑定
            System.err.println("该用户没绑定人");
            distributionUserBind =distributionUserBindMapper.selectOne(new LambdaQueryWrapper<DistributionUserBind>()
                    .eq(DistributionUserBind::getShopId,Constant.PLATFORM_SHOP_ID)
                    .eq(DistributionUserBind::getState,0)
                    .eq(DistributionUserBind::getUserId,userId)
            );
            if(distributionUserBind != null){
                //有预绑定数据直接修改状态
                distributionUserBind.setState(1);
                distributionUserBindMapper.updateById(distributionUserBind);
            }
            distributionUserBind=new DistributionUserBind();
            distributionUserBind.setDistributionUserId(distributionUserId);
            distributionUserBind.setShopId(shopId);
            distributionUserBind.setUserId(userId);
            distributionUserBind.setBindTime(new Date());
            //插入绑定记录
            distributionUserBindMapper.insert(distributionUserBind);
            return true;
        }else{
            if(Objects.equals(distributionUserBind.getDistributionUserId(),distributionUserId)){
                System.err.println("绑定人相同,不处理");
                //绑定人相同,不处理
                return false;
            }
            //该用户有分销员绑定,获取绑定设置的保护期设置
            if(distributionBindSet.getProtectDay() == -1){
                //永久保护,退出不处理
                System.err.println("永久保护,不处理");
                return false;
            }else{
                //获取设置的保护期限  计算用户是否还在保护期内
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(distributionUserBind.getBindTime());
                calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + distributionBindSet.getProtectDay());
                if(calendar.getTime().after(new Date())){
                    System.err.println("用户处于保护期内,无法绑定");
                    //用户处于保护期内,无法绑定
                    return false;
                }else{
                    //用户不在保护期内,进行抢客操作
                    System.err.println("用户不在保护期内,进行抢客操作");
                    //设置该绑定客户记录为失效状态,失效原因 抢客
                    distributionUserBind.setUpdateTime(new Date());
                    distributionUserBind.setInvalidReason(1);
                    distributionUserBind.setInvalidTime(new Date());
                    distributionUserBind.setState(-1);
                    distributionUserBindMapper.updateById(distributionUserBind);

                    //插入新的绑定记录
                    distributionUserBind = new DistributionUserBind();
                    distributionUserBind.setState(1);
                    distributionUserBind.setBindTime(new Date());
                    distributionUserBind.setUserId(userId);
                    distributionUserBind.setShopId(shopId);
                    distributionUserBind.setUpdateTime(new Date());
                    distributionUserBind.setDistributionUserId(distributionUserId);
                    distributionUserBindMapper.insert(distributionUserBind);
                    return true;
                }
            }
        }
    }
}
