package com.hai.service.impl;

import com.hai.dao.HighUserCardMapper;
import com.hai.entity.HighUserCard;
import com.hai.entity.HighUserCardExample;
import com.hai.service.HighUserCardService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("highUserCardService")
public class HighUserCardServiceImpl implements HighUserCardService {

    @Resource
    private HighUserCardMapper highUserCardMapper;

    @Override
    public void editCard(HighUserCard highUserCard) {
        if(highUserCard.getId() == null){
            highUserCard.setCreateTime(new Date());
            highUserCard.setUpdateTime(new Date());
            highUserCard.setStatus(1);
            highUserCardMapper.insert(highUserCard);
        } else {
            highUserCard.setUpdateTime(new Date());
            highUserCardMapper.updateByPrimaryKey(highUserCard);
        }
    }

    @Override
    public HighUserCard getDetailById(Long id) {
        return highUserCardMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<HighUserCard> getListByUser(Long userId, Integer type) {
        HighUserCardExample example = new HighUserCardExample();
        HighUserCardExample.Criteria criteria = example.createCriteria().andUserIdEqualTo(userId).andStatusNotEqualTo(0);
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        example.setOrderByClause("create_time desc");
        return highUserCardMapper.selectByExample(example);
    }

    @Override
    public HighUserCard getDetailByUserCardNo(Long userId, String cardNo) {
        HighUserCardExample example = new HighUserCardExample();
        example.createCriteria().andUserIdEqualTo(userId).andCardNoEqualTo(cardNo).andStatusNotEqualTo(0);
        List<HighUserCard> list = highUserCardMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Boolean isBindHtlCard(Long userId) {
        HighUserCardExample example = new HighUserCardExample();
        example.createCriteria().andUserIdEqualTo(userId).andStatusNotEqualTo(0);
        List<HighUserCard> list = highUserCardMapper.selectByExample(example);
        if (list.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public HighUserCard getUserHtlCardNo(Long userId) {
        HighUserCardExample example = new HighUserCardExample();
        example.createCriteria().andUserIdEqualTo(userId).andStatusNotEqualTo(0);
        List<HighUserCard> list = highUserCardMapper.selectByExample(example);
        if(list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
