package com.fzm.mall.server.front.asset.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.asset.model.TranOrder;
import com.fzm.mall.server.front.asset.model.UserAsset;
import com.fzm.mall.server.front.asset.model.UserAssetRecord;
import com.fzm.mall.server.front.asset.mapper.UserAssetRecordMapper;
import com.fzm.mall.server.front.asset.model.vo.asset.UserAssetRecordVo;
import com.fzm.mall.server.front.asset.model.vo.asset.UserAssetVo;
import com.fzm.mall.server.front.asset.service.ITranOrderService;
import com.fzm.mall.server.front.asset.service.ITranService;
import com.fzm.mall.server.front.asset.service.IUserAssetRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.front.base.PageVO;
import io.netty.util.internal.StringUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户资产变更记录表 服务实现类
 * </p>
 *
 * @author tdz
 * @since 2021-05-17
 */
@Service
public class UserAssetRecordServiceImpl extends ServiceImpl<UserAssetRecordMapper, UserAssetRecord> implements IUserAssetRecordService {
    private static final List<Integer> resultPay = Arrays.asList(2, 4, 6, 8, 9, 11, 16, 17);
    private static final List<Integer> resultIncome = Arrays.asList(1, 3, 5, 7, 10, 22, 23);
    private static final List<Integer> transferGive = Arrays.asList(20, 21);
    @Autowired
    private ITranOrderService tranOrderService;


    /**
     * 分页查询用户资产记录
     */
    @Override
    public PageVO<UserAssetRecord> pageAssetRecord(final UserAssetRecordVo assetRecordVo) {
        String uid = assetRecordVo.getUid();
        String coin = assetRecordVo.getCoin();
        String skuId = assetRecordVo.getSkuId();
        List<UserAssetRecord> list = new ArrayList<>();
        QueryWrapper<UserAssetRecord> qw = new QueryWrapper<>();
        qw.eq("uid", uid);
        qw.eq("coin", coin);
        String payment = assetRecordVo.getPayment();
        if (StringUtil.isNullOrEmpty(skuId)) {
            //查的积分
            if ("pay".equals(payment)) {
                qw.in("type", resultPay);
            } else if ("income".equals(payment)) {
                qw.in("type", resultIncome);
            }
            list = list(qw);
        } else {
            //查的库存
            if ("delivery".equals(payment)) {
                qw.eq("type", 18);
                list=list(qw);
            } else if ("transferGive".equals(payment)) {
                //转赠
                qw.in("type", transferGive);
                list=list(qw);
            } else if ("transferOut".equals(payment) || StringUtil.isNullOrEmpty(payment)) {
                //转让从tran order查
                //转让转出
                List<UserAssetRecord> userAssetRecordsOut = buildTranAssetRecord("", "1", uid, skuId, 0);
                if (!CollectionUtils.isEmpty(userAssetRecordsOut)) {
                    userAssetRecordsOut.stream().forEach(i -> i.setType(-1));
                }
                //转让转入
                List<UserAssetRecord> userAssetRecordsIn = buildTranAssetRecord(uid, "1", "", skuId, 1);
                if (!CollectionUtils.isEmpty(userAssetRecordsIn)) {
                    userAssetRecordsIn.stream().forEach(i -> i.setType(-2));
                }
                list.addAll(userAssetRecordsOut);
                list.addAll(userAssetRecordsIn);
            }
            if(StringUtil.isNullOrEmpty(payment)){
                //全部
                list.addAll(list(qw));
            }
        }
        for (UserAssetRecord i : list) {
            i.setAmount(i.getAmount().setScale(2, BigDecimal.ROUND_DOWN));
        }
        PageVO<UserAssetRecord> pageVO = new PageVO<>();
        pageVO.init(Integer.valueOf(assetRecordVo.getPage()), Integer.valueOf(assetRecordVo.getSize()), list.size(), list);
        return pageVO;
    }

    @Override
    public UserAssetRecord getByUidAndcommentId(String uid, String commentId) {
        QueryWrapper<UserAssetRecord> q = new QueryWrapper<>();
        q.eq("uid", uid);
        q.eq("note", commentId);
        q.eq("type", 19);
        return getOne(q);
    }


    public List<UserAssetRecord> buildTranAssetRecord(String uid, String status, String seller, String skuId, Integer type) {
        List<UserAssetRecord> list = new ArrayList<>();
        //转让
        List<TranOrder> tranOrders = tranOrderService.getByUid(uid, status, seller, skuId);
        for (TranOrder s : tranOrders) {
            UserAssetRecord r = new UserAssetRecord();
            BeanUtils.copyProperties(s, r);
            r.setStatus(2);//成功
            if (type == 0) {
                //转让转出
                r.setOtherSide(s.getUid());
            } else {
                //转让转入
                r.setOtherSide(s.getSeller());
            }
            r.setAmount(BigDecimal.valueOf(s.getNumber()));
            list.add(r);
        }
        return list;
    }
}
