package com.iamberry.wechat.service.integral;

import com.iamberry.wechat.core.entity.integral.IntegralProduct;
import com.iamberry.wechat.core.entity.integral.IntegralProductConvert;
import com.iamberry.wechat.core.entity.integral.IntegralProductGrade;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.face.integral.IntegralProductGradeService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.service.mapper.IntegralProductConvertMapper;
import com.iamberry.wechat.service.mapper.IntegralProductGradeMapper;
import com.iamberry.wechat.service.mapper.IntegralProductMapper;
import com.iamberry.wechat.service.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  积分商品兑换对应等级接口
 * @author Administrator
 * @Date 2018-04-28
 */
@Service
public class IntegralProductGradeServiceImpl  implements IntegralProductGradeService {
    @Autowired
    private IntegralProductGradeMapper integralProductGradeMapper;
    @Autowired
    private IntegralProductMapper integralProductMapper;
    @Autowired
    private IntegralProductConvertMapper integralProductConvertMapper;
    @Autowired
    private MemberService memberService;

    /**
     * 获取集合
     * @param  integralProductGrade
     * @return List
     */
    @Override
    public List<IntegralProductGrade> getIntegralProductGradeList(IntegralProductGrade  integralProductGrade){
        return  integralProductGradeMapper.getIntegralProductGradeList(integralProductGrade);
    }
    /**
     * 查询单条数据
     * @param  id
     * @return  integralProductGrade
     */
    @Override
    public  IntegralProductGrade  getIntegralProductGradeById(Integer  id){
        return  integralProductGradeMapper.getIntegralProductGradeById(id);
    }
    /**
     * 增加数据
     * @param  integralProductGrade
     * @return Integer
     */
    @Override
    public  Integer  save(IntegralProductGrade  integralProductGrade){
        return  integralProductGradeMapper.save(integralProductGrade);
    }
    /**
     * 修改数据
     * @param  integralProductGrade
     * @return Integer
     */
    @Override
    public  Integer  update(IntegralProductGrade  integralProductGrade){
        return  integralProductGradeMapper.update(integralProductGrade);
    }
    /**
     * 删除数据
     * @param  id
     * @return Integer
     */
    @Override
    public  Integer  delete(Integer  id){
        return  integralProductGradeMapper.delete(id);
    }

    @Override
    public Map<String,Object> getIsExchange(Integer userId, Integer integralProductId) {
        Map<String,Object> map = new HashMap<>();
        map.put("isExchange",false);
        /*判断状态*/
        IntegralProduct integralProduct = integralProductMapper.getIntegralProductById(integralProductId);
        if(integralProduct.getIntegralProductStatus() != 1 ){
            map.put("noExchangeInfo","查询产品信息失败");
            return map;
        }

        if(integralProduct.getIntegralProductStatus() != 1){
            map.put("noExchangeInfo","产品已下架");
            return map;
        }

        /*判断数量*/
        if(integralProduct.getIntegralProductNum() - integralProduct.getIntegralProductRemainingNum() < 1){
            map.put("noExchangeInfo","产品已售空");
            return map;
        }
        /*判断积分是否足够*/
        IntegralProductConvert integralProductConvert = integralProductConvertMapper.getIntegralProductConvertByProductId(integralProductId);
        Member member = memberService.getMemberByUserId(userId);
        if(member.getUserSurplusIntegral() - integralProductConvert.getConvertDfbi() < 0){
            map.put("noExchangeInfo","会员可用积分不足");
            return map;
        }

        /*获取用户当前等级*/
        Integer rankRuleId = memberService.getUserGrad(userId);
        boolean isRank = false;
        IntegralProductGrade integralProductGrade = new IntegralProductGrade();
        integralProductGrade.setIntegralProductId(integralProductId);
        List<IntegralProductGrade> integralProductGradeList = integralProductGradeMapper.getIntegralProductGradeList(integralProductGrade);
        if(integralProductGradeList != null && integralProductGradeList.size() >0){
            for (IntegralProductGrade ipg:integralProductGradeList) {
                if(rankRuleId == ipg.getRankId()){
                    isRank = true;
                }
            }
        }else{
            isRank = true;
        }

        if(!isRank){
            map.put("noExchangeInfo","会员可用积分不足");
            return map;
        }

        map.put("isExchange",true);
        map.put("noExchangeInfo","");
        return map;
    }



}
