package com.iformall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.domain.po.WxQuestion;
import com.iformall.domain.po.WxQuestionConfig;
import com.iformall.domain.po.WxQuestionLog;
import com.iformall.mapper.WxQuestionConfigMapper;
import com.iformall.mapper.WxQuestionLogMapper;
import com.iformall.mapper.WxQuestionMapper;
import com.iformall.service.WxQuestionService;
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;

@Service
public class WxQuestionServiceImpl implements WxQuestionService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    WxQuestionMapper wxQuestionMapper;

    @Autowired
    WxQuestionConfigMapper wxQuestionConfigMapper;

    @Autowired
    WxQuestionLogMapper wxQuestionLogMapper;

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

    @Override
    public WxQuestion getById(Long id) {
        return wxQuestionMapper.selectById(id);
    }

    @Override
    public void saveOrUpdate(WxQuestion record) {
        if (record.getId() == null) {
            //record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
            wxQuestionMapper.insert(record);
        } else {
            wxQuestionMapper.updateById(record);
        }
    }

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

    @Override
    public List<WxQuestion> findList(WxQuestion wxQuestion) {
        return wxQuestionMapper.findList(wxQuestion);
    }

    @Override
    public List<WxQuestionConfig> findConfigList(WxQuestionConfig wxQuestionConfig) {
        return wxQuestionConfigMapper.findList(wxQuestionConfig);
    }

    @Override
    public List<WxQuestionLog> findLogList(WxQuestionLog wxQuestionLog) {
        return wxQuestionLogMapper.findListWithAnswer(wxQuestionLog);
    }

    @Override
    public void saveOrUpdateConfig(WxQuestionConfig record) {
        if (record.getId() == null) {
            final IdWorker idWorker = IdWorker.get();
            record.setId(idWorker.nextId());
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            wxQuestionConfigMapper.insert(record);
        } else {
            record.setUpdateTime(new Date());
            wxQuestionConfigMapper.updateById(record);
        }
    }

    @Override
    public void saveOrUpdateLog(WxQuestionLog record) {
        if (record.getId() == null) {
            final IdWorker idWorker = IdWorker.get();
            record.setId(idWorker.nextId());
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            wxQuestionLogMapper.insert(record);
        } else {
            record.setUpdateTime(new Date());
            wxQuestionLogMapper.updateById(record);
        }
    }

}
