package com.iformall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxMerchantSubsidy;
import com.iformall.domain.vo.WxMerchantSubsidyVo;

import com.iformall.mapper.WxMerchantSubsidyMapper;
import com.iformall.service.ExcelService;
import com.iformall.service.WxMerchantSubsidyService;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iformall.common.IdWorker;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

import java.util.List;
import java.util.Map;


@Service
public class WxMerchantSubsidyServiceImpl implements WxMerchantSubsidyService {
	
	@Autowired
    WxMerchantSubsidyMapper wxMerchantSubsidyMapper;

    @Autowired
    ExcelService excelService;

    @Override
    public Map<String, Object> sumForSubsidy(WxMerchantSubsidy record) {
        return wxMerchantSubsidyMapper.sumVoList(record);
    }

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

    @Override
    public WxMerchantSubsidy getById(Long id) {
        return wxMerchantSubsidyMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultData saveOrUpdate(WxMerchantSubsidy record) {
        if (record.getId() == null) {
            //record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
            wxMerchantSubsidyMapper.insertSelective(record);
        } else {
            int num = wxMerchantSubsidyMapper.updateByPrimaryKeySelective(record);
            return new ResultData(num);
        }
        return new ResultData();
    }

    @Override
    public ResultData update(WxMerchantSubsidy record) {
        String idStr = record.getIdStr();
        if (StringUtils.isEmpty(idStr)) {
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR.getCode(), "结单数据没有传入");
        }
        wxMerchantSubsidyMapper.updateByCond(record);
        return new ResultData(Result.SUCCESS, "操作成功");
    }

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

      @Override
    public void exportData(WxMerchantSubsidy wxMerchantSubsidy, HttpServletRequest request, HttpServletResponse response) {
        List<WxMerchantSubsidyVo> list = wxMerchantSubsidyMapper.findVoList(wxMerchantSubsidy);
        excelService.exportExcel(list,null,"补贴记录",WxMerchantSubsidyVo.class,"补贴记录.xls",response);
    }


}
