package com.iformall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.IdWorker;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


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

    @Autowired
    ExcelService excelService;

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

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

    @Override
    public WxMerchantSubsidy getById(Long id) {
        return wxMerchantSubsidyMapper.selectById(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.insert(record);
        } else {
            int num = wxMerchantSubsidyMapper.updateById(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.deleteById(id);
    }

    @Override
    public void exportData(WxMerchantSubsidyVo wxMerchantSubsidy, HttpServletRequest request, HttpServletResponse response) {
        List<WxMerchantSubsidyVo> list = wxMerchantSubsidyMapper.findVoList(wxMerchantSubsidy);
        Map<String, Long> summary = summary(wxMerchantSubsidy);
        Long subsidy = summary.get("subsidy");
        String subsidyStr = new BigDecimal(subsidy).divide(new BigDecimal(100)).toPlainString();
        WxMerchantSubsidyVo merchantSubsidy = new WxMerchantSubsidyVo();
        merchantSubsidy.setSubsidyStr(subsidyStr);
        merchantSubsidy.setSource(-1);
        merchantSubsidy.setType(-1);
        merchantSubsidy.setFreeze(-1);
        merchantSubsidy.setStatus(-1);
        list.add(merchantSubsidy);
        excelService.exportExcel(list, null, "结算记录", WxMerchantSubsidyVo.class, "结算记录.xlsx", response, false);
    }

    @Override
    public Map<String, Long> summary(WxMerchantSubsidyVo wxMerchantSubsidy) {
        Long subsidy = wxMerchantSubsidyMapper.findVoList(wxMerchantSubsidy).stream().collect(Collectors.summingLong(WxMerchantSubsidy::getSubsidy));
        Map<String, Long> data = new HashMap<>();
        data.put("subsidy", subsidy);
        return data;
    }


}
