package com.iformall.service.impl;

import java.util.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.domain.po.WxCouponActionLog;
import com.iformall.mapper.WxCouponActionLogMapper;
import com.iformall.service.WxCouponActionLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iformall.common.IdWorker;

@Service
public class WxCouponActionLogServiceImpl implements WxCouponActionLogService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    WxCouponActionLogMapper wxCouponActionLogMapper;


    @Override
    public PageInfo<WxCouponActionLog> listAsPage(WxCouponActionLog record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxCouponActionLogMapper.findList(record));
    }

    @Override
    public WxCouponActionLog getById(Long id) {
        return wxCouponActionLogMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(WxCouponActionLog record) {
        if (record.getId() == null) {
            //record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
            wxCouponActionLogMapper.insertSelective(record);
        } else {
            wxCouponActionLogMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void deleteById(Long id) {
        wxCouponActionLogMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void addOne(String tenantId, Long couponId, Long coupon_order_id, int channelType, Long channelId) {
        WxCouponActionLog actionLog = new WxCouponActionLog();
        actionLog.setTenantId(tenantId);
        actionLog.setCouponId(couponId);
        actionLog.setChannelId(channelId);
        actionLog.setCouponOrderId(coupon_order_id);
        actionLog.setChannelType(channelType);
        actionLog.setCreateTime(new Date());
        saveOrUpdate(actionLog);
    }

    @Override
    public int getCountByChannelId(String tenantId, int channelType, Long channelId) {
        return wxCouponActionLogMapper.getCountByChannelId(tenantId, channelType, channelId);
    }

}
