package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.dto.*;
import com.hc.business.mapper.OrderRefundLogMapper;
import com.hc.business.mapper.OrderRefundMapper;
import com.hc.business.mapper.OrdersMapper;
import com.hc.business.mapper.UserStatusMapper;
import com.hc.business.model.OrderRefund;
import com.hc.business.model.Orders;
import com.hc.business.service.OrderRefundService;
import com.hc.core.utils.tool.ResultUtils;
import com.hc.core.utils.tool.TimerUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 退费订单记录表(OrderRefund)表服务实现类
 *
 * @author makejava
 * @since 2024-01-20 10:54:50
 */

@Service("orderRefundService")
public class OrderRefundServiceImpl implements OrderRefundService {

    private final OrderRefundMapper orderRefundMapper;
    private final OrderRefundLogMapper orderRefundLogMapper;
    private final OrdersMapper ordersMapper;

    private final UserStatusMapper userStatusMapper;

    public OrderRefundServiceImpl(OrderRefundMapper orderRefundMapper, OrderRefundLogMapper orderRefundLogMapper, OrdersMapper ordersMapper, UserStatusMapper userStatusMapper) {

        this.orderRefundMapper = orderRefundMapper;
        this.orderRefundLogMapper = orderRefundLogMapper;
        this.ordersMapper = ordersMapper;
        this.userStatusMapper = userStatusMapper;
    }

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

    /**
     * 新增数据
     *
     * @param orderRefund 实例对象
     * @return 实例对象
     */
    @Override
    public OrderRefund insert(OrderRefund orderRefund) {
        this.orderRefundMapper.insert(orderRefund);
        return orderRefund;
    }

    /**
     * 修改数据
     *
     * @param orderRefund 实例对象
     * @return 实例对象
     */
    @Override
    public OrderRefund update(OrderRefund orderRefund) {
        this.orderRefundMapper.updateByPrimaryKeySelective(orderRefund);
        OrderRefund updateEntity = this.queryById(orderRefund.getRefundId());
        if(updateEntity!=null && (updateEntity.getStatus()==2 || updateEntity.getStatus()==3))
        {
            //判断是否要更新订单状态，1.如果是人员都退掉的话，那么更新为退费，否则是部分退费
            List<OrderRefundLogDto> orderRefundLogDtos = orderRefundLogMapper.getWaitingForRefundUser(updateEntity.getOrderId());
            //下面还有人
            Orders orders = new Orders();
            if(orderRefundLogDtos.stream().count()>0)
            {
                orders = ordersMapper.selectByPrimaryKey(orderRefund.getOrderId());
                orders.setOrderStatus(3);//还有人就是部分退费
            }else {
                orders = ordersMapper.selectByPrimaryKey(orderRefund.getOrderId());
                orders.setOrderStatus(2);//退费
            }

            //更新user_status的状态，让他不能再访问

            List<OrderRefundLogDto> refundLogList = orderRefundLogMapper.queryByRefundId(orderRefund.getRefundId());

            for (OrderRefundLogDto userST: refundLogList) {
                userStatusMapper.updateUserStatus(userST.getUserId(),orderRefund.getClassId(),orderRefund.getOrderId());
            }

            //更新主表状态
            this.ordersMapper.updateByPrimaryKey(orders);
        }
        return updateEntity;
    }

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

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


    /**
     * @Author ymnets
     * @Description 分页查询退费单
     * @return com.github.pagehelper.PageInfo
     **/
    @Override
    public PageInfo queryAll(int page, int size, Integer orderId,String orderNo,Integer status,Integer payType,Integer classId,Long begin,Long end) {
        PageHelper.startPage(page, size);
        List<OrderRefundEntityDto> orders = this.orderRefundMapper.queryAll(orderId, orderNo,status,payType,classId,begin,end);
        return new PageInfo(orders);
    }

    /**
     * @Author ymnets
     * @Description 查询退费单
     * @return com.github.pagehelper.PageInfo
     **/
    @Override
    public  List<OrderRefundEntityDto> queryAll(Integer orderId, String orderNo, Integer status, Integer payType, Integer classId, Long begin, Long end) {
        List<OrderRefundEntityDto> orders = this.orderRefundMapper.queryAll(orderId, orderNo,status,payType,classId,begin,end);
        return orders;
    }

    @Override
    public  List<OrderRefundAllDto> queryHighAll(Integer orderId, String orderNo, Integer status, Integer payType, Integer classId, Long begin, Long end) {
        List<OrderRefundAllDto> list = this.orderRefundMapper.queryHighAll(orderId, orderNo,status,payType,classId,begin,end);
        for (OrderRefundAllDto record : list) {
            record.setRefundTimeStr(TimerUtil.timeStamp2Date(record.getRefundTime()));
            record.setFincalTimeStr(TimerUtil.timeStamp2Date(record.getFincalTime()));
            record.setPayTimeStr(TimerUtil.timeStamp2Date(record.getPayTime()));
            record.setPayTypeName(ResultUtils.getTypeName(record.getPayType()));
            record.setSyncStatusStr(ResultUtils.getSyncStatusName(record.getSyncStatus()));
            record.setStatusName(ResultUtils.getCheckStatusName(record.getStatus()));
        }
        return list;
    }



    private List<OrderRefundDto> copyList(List<OrderRefund> records) {
        List<OrderRefundDto> list = new ArrayList<>();
        for (OrderRefund record : records) {
            list.add(copy(record));
        }
        return list;
    }

    private OrderRefundDto copy(OrderRefund record) {
        OrderRefundDto recordDto = new OrderRefundDto();
        BeanUtils.copyProperties(record, recordDto);

        recordDto.setRefundLogList(orderRefundLogMapper.queryByRefundId(record.getRefundId()));
        return recordDto;
    }

}
