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.WxPrepayment;
import com.iformall.domain.po.WxPrepaymentHistory;
import com.iformall.mapper.WxPrepaymentHistoryMapper;
import com.iformall.mapper.WxPrepaymentMapper;
import com.iformall.service.WxPrepaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author gongbiao
 */
@Service
public class WxPrepaymentServiceImpl implements WxPrepaymentService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxPrepaymentMapper wxPrepaymentMapper;

    @Autowired
    WxPrepaymentHistoryMapper wxPrepaymentHistoryMapper;

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

    @Override
    public PageInfo<WxPrepaymentHistory> listHistoryAsPage(WxPrepaymentHistory record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxPrepaymentHistoryMapper.findList(record));
    }

    @Override
    public void payFirst(WxPrepayment wxPrepayment) {
        final IdWorker idWorker = IdWorker.get();
        wxPrepayment.setId(idWorker.nextId());
        wxPrepayment.setTotalAmount(wxPrepayment.getLastPayAmount());
        wxPrepayment.setRemainAmount(wxPrepayment.getLastPayAmount());
        wxPrepaymentMapper.insert(wxPrepayment);
        addPrepaymentHistory(wxPrepayment);
    }

    @Override
    public void payAgain(WxPrepayment record) {
        WxPrepayment wxPrepayment = wxPrepaymentMapper.selectById(record.getId());
        long remainAmout = wxPrepayment.getRemainAmount() + record.getLastPayAmount();
        long totalAmount = wxPrepayment.getTotalAmount() + record.getLastPayAmount();
        wxPrepayment.setRemainAmount(remainAmout);
        wxPrepayment.setTotalAmount(totalAmount);
        wxPrepaymentMapper.updateById(wxPrepayment);
        addPrepaymentHistory(wxPrepayment);
    }

    private void addPrepaymentHistory(WxPrepayment wxPrepayment) {
        WxPrepaymentHistory wxPrepaymentHistory = new WxPrepaymentHistory();
        final IdWorker idWorker = IdWorker.get();
        wxPrepaymentHistory.setId(idWorker.nextId());
        wxPrepaymentHistory.setTenantId(wxPrepayment.getTenantId());
        wxPrepaymentHistory.setMerchantId(wxPrepayment.getMerchantId());
        String lastPayAmount = new BigDecimal(wxPrepayment.getLastPayAmount()).divide(new BigDecimal(100)).setScale(2).toPlainString();
        wxPrepaymentHistory.setAmountDetail("+" + lastPayAmount + "元");
        wxPrepaymentHistory.setRemainAmount(wxPrepayment.getRemainAmount());
        wxPrepaymentHistory.setOperationType(wxPrepayment.getOperationType());
        wxPrepaymentHistory.setOperator(wxPrepayment.getOperator());
        wxPrepaymentHistoryMapper.insert(wxPrepaymentHistory);
    }

}
