package com.iformall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iformall.common.IdWorker;
import com.iformall.domain.po.WxUserDataRule;
import com.iformall.enums.EnumUserDataRuleType;
import com.iformall.mapper.WxUserDataRuleMapper;
import com.iformall.service.WxUserDataRuleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author gongbiao
 */
@Service
public class WxUserDataRuleServiceImpl implements WxUserDataRuleService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxUserDataRuleMapper wxUserDataRuleMapper;

    @Override
    public void modify(WxUserDataRule record) {
        Date date = new Date();
        if (record.getId() == null) {
            final IdWorker idWorker = IdWorker.get();
            record.setId(idWorker.nextId());
            record.setCreateTime(date);
            record.setUpdateTime(date);
            wxUserDataRuleMapper.insert(record);
        } else {
            if (EnumUserDataRuleType.ALL.getCode().equals(record.getType())) {
                wxUserDataRuleMapper.deleteById(record.getId());
            } else {
                record.setUpdateTime(date);
                wxUserDataRuleMapper.updateById(record);
            }

        }
    }

    @Override
    public WxUserDataRule findByUserId(Long userId) {
        WxUserDataRule wxUserDataRule = new WxUserDataRule();
        wxUserDataRule.setUserId(userId);
        return wxUserDataRuleMapper.selectOne(new QueryWrapper(wxUserDataRule));
    }

    @Override
    public List<WxUserDataRule> list(WxUserDataRule rule) {
       return wxUserDataRuleMapper.selectList(new QueryWrapper<>(rule)) ;
    }
}
