package com.hai.service.impl;

import com.hai.common.utils.DateUtil;
import com.hai.dao.HighGoldRecMapper;
import com.hai.entity.HighGoldRec;
import com.hai.entity.HighGoldRecExample;
import com.hai.service.HighGoldRecService;
import com.hai.service.HighUserService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

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

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/3/27 10:03
 */
@Service("highGoldRecService")
public class HighGoldRecServiceImpl implements HighGoldRecService {

    @Resource
    private HighGoldRecMapper highGoldRecMapper;

    @Resource
    private HighUserService highUserService;

    @Override
    public void insertGoldRec(HighGoldRec highGoldRec) {
        highGoldRecMapper.insert(highGoldRec);
    }

    @Override
    public void updateGoldRec(HighGoldRec highGoldRec) {
        highGoldRecMapper.updateByPrimaryKey(highGoldRec);
    }

    @Override
    public List<HighGoldRec> getGoldRec(Map<String, Object> map) {
        HighGoldRecExample example = new HighGoldRecExample();
        HighGoldRecExample.Criteria criteria = example.createCriteria();


        if (MapUtils.getLong(map, "userId") != null) {
            criteria.andUserIdEqualTo(MapUtils.getLong(map, "userId"));
        }

        if (MapUtils.getLong(map, "goldType") != null) {
            criteria.andGoldTypeEqualTo(MapUtils.getLong(map, "goldType"));
        }

        if (MapUtils.getLong(map, "resType") != null) {
            criteria.andResTypeEqualTo(MapUtils.getInteger(map, "resType"));
        }
        if (MapUtils.getLong(map, "resId") != null) {
            criteria.andResIdEqualTo(MapUtils.getLong(map, "resId"));
        }

        if (MapUtils.getLong(map , "createTimeS") != null) {
            criteria.andCreateTimeBetween(new Date(MapUtils.getLong(map , "createTimeS")) , new Date(MapUtils.getLong(map , "createTimeE")));
        }
        if (MapUtils.getInteger(map, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
        } {
            criteria.andStatusEqualTo(1);
        }

        example.setOrderByClause("create_time desc");
        List<HighGoldRec> list = highGoldRecMapper.selectByExample(example);
        for (HighGoldRec highGoldRec : list) {
            highGoldRec.setHighUser(highUserService.findByUserId(highGoldRec.getUserId()));
        }
        return list;
    }

    @Override
    public List<Map<Long, Long>> queryGold(Map<String, Object> map) {

        return highGoldRecMapper.queryGold(map);
    }

    @Override
    public List<HighGoldRec> selectByExpiringSoon(Long userId , Integer day) {
        return highGoldRecMapper.selectByExpiringSoon(userId , day);
    }

    @Override
    public List<HighGoldRec> selectByExpiringSoonByAll(Integer day) {
        return highGoldRecMapper.selectByExpiringSoonByAll( day);
    }


}
