package com.iformall.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.po.WxCouponActionLog;
import com.iformall.domain.vo.WxCouponActionLogVo;
import com.iformall.exception.MallinkException;
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;
import org.springframework.util.CollectionUtils;

@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.selectById(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.insert(record);
        } else {
            wxCouponActionLogMapper.updateById(record);
        }
    }

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

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

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

    @Override
    public PageInfo<WxCouponActionLogVo> getActionLog(Map<String, Object> params, Integer pageNum, Integer pageSize) {
        Object channelType = params.get("channelType");
        if (CollectionUtils.isEmpty(params) || Objects.isNull(channelType)) {
            throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR);
        }
        params.put("sortColumns", BaseEntity.SortField.CreateTime_DESC) ;
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> wxCouponActionLogMapper.getActionLog(params));
    }

}
