package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.ZccmOrderUserLogMapper;
import com.hc.business.model.ZccmOrderUserLog;
import com.hc.business.service.ZccmOrderUserLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * (ZccmOrderUserLog)表服务实现类
 *
 * @author java
 * @since 2020-12-03 09:25:11
 */

@Service("orderUserLogService")
public class ZccmOrderUserLogServiceImpl implements ZccmOrderUserLogService {

    private final ZccmOrderUserLogMapper orderUserLogMapper;

    public ZccmOrderUserLogServiceImpl(ZccmOrderUserLogMapper orderUserLogMapper) {
        this.orderUserLogMapper = orderUserLogMapper;
    }

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

    /**
     * 新增数据
     *
     * @param orderUserLog 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmOrderUserLog insert(ZccmOrderUserLog orderUserLog) {
        Integer orderId = orderUserLog.getOrderId();
        Integer reUserId = orderUserLog.getReUserId();
        List<ZccmOrderUserLog> orderUserLogs
                = this.orderUserLogMapper.queryByUserIdAndOrderId(orderId, reUserId);
        if (!CollectionUtils.isEmpty(orderUserLogs)) {
//            throw new ResponseException(InfoEnum.LOGIN_REGISTER_RE);
            return orderUserLogs.get(0);
        }
        orderUserLog.setCreateTime(System.currentTimeMillis());
        this.orderUserLogMapper.insert(orderUserLog);
        return orderUserLog;
    }

    @Override
    public List<ZccmOrderUserLog> queryListByOrderId(Integer orderId) {
        return orderUserLogMapper.queryByOrderId(orderId);
    }

    /**
     * 修改数据
     *
     * @param orderUserLog 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmOrderUserLog update(ZccmOrderUserLog orderUserLog) {
        this.orderUserLogMapper.updateByPrimaryKeySelective(orderUserLog);
        return this.queryById(orderUserLog.getOrderLogId());
    }

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

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