package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.ZccmUserRefundHistoryMapper;
import com.hc.business.model.ZccmUserRefundHistory;
import com.hc.business.service.ZccmUserRefundHistoryService;
import com.hc.core.utils.exception.ResponseException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 用户退款记录表(ZccmUserRefundHistory)表服务实现类
 *
 * @author java
 * @since 2020-12-10 11:31:43
 */

@Service("userRefundHistoryService")
public class ZccmUserRefundHistoryServiceImpl implements ZccmUserRefundHistoryService {

    private final ZccmUserRefundHistoryMapper userRefundHistoryMapper;

    public ZccmUserRefundHistoryServiceImpl(ZccmUserRefundHistoryMapper userRefundHistoryMapper) {
        this.userRefundHistoryMapper = userRefundHistoryMapper;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ZccmUserRefundHistory queryById(Integer id) {
        return this.userRefundHistoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增数据
     *
     * @param userRefundHistory 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmUserRefundHistory insert(ZccmUserRefundHistory userRefundHistory) {
        String orderNo = userRefundHistory.getOrderNo();
        ZccmUserRefundHistory userRefundHistory1
                = this.userRefundHistoryMapper.queryByOrderNo(orderNo);
        if (!Objects.isNull(userRefundHistory1)) {
            throw new ResponseException(-1, "退款已提交请不要重复操作");
        }
        userRefundHistory.setCreateTime(System.currentTimeMillis());
        this.userRefundHistoryMapper.insert(userRefundHistory);
        return userRefundHistory;
    }

    /**
     * 修改数据
     *
     * @param userRefundHistory 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmUserRefundHistory update(ZccmUserRefundHistory userRefundHistory) {
        this.userRefundHistoryMapper.updateByPrimaryKeySelective(userRefundHistory);
        return this.queryById(userRefundHistory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userRefundHistoryMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(this.userRefundHistoryMapper::selectAll);
    }
}