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.MallUserInfo;
import com.iformall.domain.po.WxBillAction;
import com.iformall.mapper.WxBillActionMapper;
import com.iformall.service.WxBillActionService;
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;

/**
 * @author gongbiao
 */
@Service
public class WxBillActionServiceImpl implements WxBillActionService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxBillActionMapper wxBillActionMapper;

    @Override
    public List<WxBillAction> find(WxBillAction record) {
        return wxBillActionMapper.findBillActionList(record);
    }

    @Override
    public PageInfo<WxBillAction> listAsPage(WxBillAction record, Integer pageIndex, Integer pageSize) {
        logger.info("查询账单操作记录列表");
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxBillActionMapper.findList(record));
    }

    @Override
    public int save(WxBillAction action) {
        final IdWorker idWorker = IdWorker.get();
        action.setId(idWorker.nextId());
        Date date = new Date();
        action.setCreatetime(date);
        action.setUpdatetime(date);
        return wxBillActionMapper.insert(action);
    }


    @Override
    public int modifyBill(WxBillAction action, MallUserInfo user) {
        final IdWorker idWorker = IdWorker.get();
        WxBillAction wxBillAction = new WxBillAction();
        wxBillAction.setId(idWorker.nextId());
        wxBillAction.setBillId(action.getBillId());
        wxBillAction.setDetails(action.getDetails());
        wxBillAction.setAction(action.getAction());
        wxBillAction.setUserId(user.getId());
        wxBillAction.setUserName(user.getName());
        wxBillAction.setPhone(user.getPhone());
        wxBillAction.setTenantId(user.getTenantId());
        wxBillAction.setPayDate(action.getPayDate());
        Date date = new Date();
        wxBillAction.setCreatetime(date);
        wxBillAction.setUpdatetime(date);
        return wxBillActionMapper.insert(wxBillAction);
    }

}
