package com.gxwebsoft.shop.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.shop.mapper.ShopOrderMapper;
import com.gxwebsoft.shop.service.ShopOrderService;
import com.gxwebsoft.shop.entity.ShopOrder;
import com.gxwebsoft.shop.param.ShopOrderParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单Service实现
 *
 * @author 科技小王子
 * @since 2024-09-10 21:05:07
 */
@Service
public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder> implements ShopOrderService {
    @Resource
    private ShopOrderService shopOrderService;

    @Override
    public PageResult<ShopOrder> pageRel(ShopOrderParam param) {
        PageParam<ShopOrder, ShopOrderParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        List<ShopOrder> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<ShopOrder> listRel(ShopOrderParam param) {
        List<ShopOrder> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<ShopOrder, ShopOrderParam> page = new PageParam<>();
        page.setDefaultOrder("create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public ShopOrder getByIdRel(Integer orderId) {
        ShopOrderParam param = new ShopOrderParam();
        param.setOrderId(orderId);
        return param.getOne(baseMapper.selectListRel(param));
    }

    /**
     * 支付成功后的事务处理
     * @param order
     */
    @Override
    public void onPaySuccess(ShopOrder order) {
        System.out.println("支付成功后的事务处理 = " + order);
        order.setPayStatus(true);
        order.setPayTime(DateUtil.date());
        shopOrderService.updateById(order);
    }

}
