package com.liquidnet.service.stone.service.impl;

import com.github.pagehelper.PageInfo;
import com.liquidnet.commons.lang.util.CurrentUtil;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.base.UserPathDto;
import com.liquidnet.service.stone.entity.StoneScoreItems;
import com.liquidnet.service.stone.entity.StoneScoreLogs;
import com.liquidnet.service.stone.service.IStoneScoreOrderService;
import com.liquidnet.service.stone.util.ObjectUtils;
import com.liquidnet.service.stone.util.StoneRedisUtils;
import com.liquidnet.service.stone.vo.StoneItemListVo;
import com.liquidnet.service.stone.vo.StoneLogsListVo;
import com.liquidnet.service.stone.vo.StoneOrderListVo;
import com.liquidnet.service.stone.vo.StoneScoreItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 积分订单表 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2021-10-19
 */
@Service
@Slf4j
public class StoneScoreOrderServiceImpl implements IStoneScoreOrderService {

    @Autowired
    StoneRedisUtils stoneRedisUtils;

    @Override
    public ResponseDto<String> order(String itemId, Integer number) {
        //判断状态 库存
        String uid = CurrentUtil.getCurrentUid();
        StoneScoreItems data = stoneRedisUtils.getItemData(itemId);
        if(data==null){
            return ResponseDto.failure("参数异常");
        }
        if(stoneRedisUtils.getStoneGeneralSurplus(itemId)<=0){
            return ResponseDto.failure("该商品库存不足");
        }
        StoneScoreItemVo vo = StoneScoreItemVo.getNew().copy(data);
        if(vo.getStatus()!=1){
            return ResponseDto.failure("该商品不可下单");
        }

        //获取积分
        int needScore = data.getScore().intValue();
        if(stoneRedisUtils.getUserScoreRest(uid)<needScore){
            return ResponseDto.failure("剩余积分不足");
        }
        //获取限购
        int buyCount = stoneRedisUtils.getItemLimit(uid,itemId);
        if((buyCount+number)>data.getLimitCount() && data.getLimitCount()>0){
            return ResponseDto.failure("超出限购数量");
        }
        stoneRedisUtils.addItemLimit(uid,itemId,number);
        //下单
        return null;
    }

    @Override
    public PageInfo<List<StoneOrderListVo>> orderList(Integer page) {
//        String uid = CurrentUtil.getCurrentUid();
//        PageInfo<List<StoneOrderListVo>> mPageInfo;
//        int size = 40;
//        if (page == null || page == 1 || page == 0) {
//            page = 1;
//        }
//        List<StoneScoreLogs> voList;
//        long count;
//        if (page == 1) {
//            voList = stoneRedisUtils.getLogsList(uid);
//            count = voList.size();
//        } else {
//            HashMap<String, Object> info = stoneMongoUtils.itemListPage(page, size);
//            try {
//                voList = (List<StoneScoreLogs>) info.get("total");
//                count = (long) info.get("data");
//            } catch (Exception e) {
//                voList = new ArrayList();
//                count = 0;
//            }
//        }
//        List<StoneLogsListVo> returnVoList = ObjectUtils.getStoneLogsListVoArrayList();
//        for (int i = 0; i < voList.size(); i++) {
//            StoneScoreLogs vo = voList.get(i);
//            StoneLogsListVo returnVo = StoneLogsListVo.getNew().copy(vo);
//            returnVoList.add(returnVo);
//        }
//        mPageInfo = new PageInfo(returnVoList);
//        mPageInfo.setTotal(count);
//        log.info(UserPathDto.setData("积分日志列表", "", voList));
//        return mPageInfo;
        return null;
    }
}
