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.WxCardInfo;
import com.iformall.domain.vo.WxCardVo;
import com.iformall.mapper.WxCardInfoMapper;
import com.iformall.service.ExcelService;
import com.iformall.service.WxCardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Service
public class WxCardInfoServiceImpl implements WxCardInfoService {
	
	@Autowired
    WxCardInfoMapper wxCardInfoMapper;

    @Autowired
    ExcelService excelService;

    @Override
    public PageInfo<WxCardVo> listAsPage(WxCardVo record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxCardInfoMapper.findVoList(record));
    }

    @Override
    public WxCardInfo getById(Long id) {
        return wxCardInfoMapper.selectById(id);
    }

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

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

    @Override
    public void exportData(WxCardVo wxCardVo, HttpServletRequest request, HttpServletResponse response) {
        List<WxCardVo> list = wxCardInfoMapper.findVoList(wxCardVo);
        excelService.exportExcel(list, null, "卡订单", WxCardVo.class, "卡订单.xlsx", response, false);

    }
}
