package com.goafanti.member.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.common.dao.MemberOrderMapper;
import com.goafanti.common.enums.OrderState;
import com.goafanti.common.model.MemberOrder;

@Service
public class MemberOrderService {
	@Autowired
	private MemberOrderMapper memberOrderMapper;

	public MemberOrder getById(Long id) {
		return memberOrderMapper.selectByPrimaryKey(id);
	}

	public int update(MemberOrder mo) {
		if (OrderState.isPayed(OrderState.getStatus(mo.getOrderState()))) {
			Map<String, Object> umap = new HashMap<>();
			umap.put("lvl", mo.getMenberType());
			umap.put("userId", mo.getUserId());
			memberOrderMapper.updateUserLvl(umap);
		}
		return memberOrderMapper.updateByPrimaryKeySelective(mo);
	}

	public void updateExpiredUser() {
		List<String> uids = memberOrderMapper.selectExpired();
		if (!uids.isEmpty()) {
			Map<String, Object> umap = new HashMap<>();
			umap.put("lvl", 1);
			umap.put("list", uids);
			memberOrderMapper.updateBatchUserLvl(umap);
		}
	}
}
