package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.dto.OrderUserLogDto;
import com.hc.business.dto.OrdersAllUserDto;
import com.hc.business.mapper.OrderUserLogMapper;
import com.hc.business.model.OrderUserLog;
import com.hc.business.service.OrderUserLogService;
import com.hc.core.utils.tool.ResultUtils;
import com.hc.core.utils.tool.TimerUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

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

@Service("orderUserLogService")
public class OrderUserLogServiceImpl implements OrderUserLogService {

    private final OrderUserLogMapper orderUserLogMapper;

    public OrderUserLogServiceImpl(OrderUserLogMapper orderUserLogMapper) {
        this.orderUserLogMapper = orderUserLogMapper;
    }

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

    /**
     * 新增数据
     *
     * @param orderUserLog 实例对象
     * @return 实例对象
     */
    @Override
    public OrderUserLog insert(OrderUserLog orderUserLog) {
        Integer orderId = orderUserLog.getOrderId();
        Integer reUserId = orderUserLog.getReUserId();
        List<OrderUserLog> 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<OrderUserLog> queryListByOrderId(Integer orderId) {
        return orderUserLogMapper.queryByOrderId(orderId);
    }

    @Override
    public List<OrderUserLogDto> queryUserLogByOrderId(Integer orderId) {
        return orderUserLogMapper.queryUserLogByOrderId(orderId);
    }

    /**
     * 修改数据
     *
     * @param orderUserLog 实例对象
     * @return 实例对象
     */
    @Override
    public OrderUserLog update(OrderUserLog 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);
    }

    @Override
    public List<OrdersAllUserDto> queryAllOrdersUser(Integer orderId,String orderNo,Integer status,Integer payType,Integer isPay,Integer classId,Long begin,Long end) {
        List<OrdersAllUserDto> users = this.orderUserLogMapper.queryAllOrdersUser(orderId, orderNo,status,payType,isPay,classId,begin,end);
        for (OrdersAllUserDto record : users) {
            record.setOrderStatusName(ResultUtils.getOrderStatusName(record.getOrderStatus()));
            record.setTypeName(ResultUtils.getTypeName(record.getType()));
            record.setPayTimeStr(TimerUtil.timeStamp2Date(record.getPayTime()));
        }
        return users;
    }

}