package com.gxwebsoft.common.system.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.Company;
import com.gxwebsoft.common.system.entity.Order;
import com.gxwebsoft.common.system.entity.OrderGoods;
import com.gxwebsoft.common.system.mapper.OrderMapper;
import com.gxwebsoft.common.system.param.OrderParam;
import com.gxwebsoft.common.system.service.CompanyService;
import com.gxwebsoft.common.system.service.MenuService;
import com.gxwebsoft.common.system.service.OrderGoodsService;
import com.gxwebsoft.common.system.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * 订单Service实现
 *
 * @author 科技小王子
 * @since 2024-10-16 12:32:52
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private MenuService menuService;
    @Resource
    private OrderGoodsService orderGoodsService;
    @Resource
    private CompanyService companyService;

    @Override
    public PageResult<Order> pageRel(OrderParam param) {
        PageParam<Order, OrderParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        List<Order> list = baseMapper.selectPageRel(page, param);
        if (param.getSceneType() != null && ObjectUtil.isNotEmpty(param.getSceneType().equals("showOrderGoods"))) {
          list.forEach(d -> {
            d.setOrderGoods(orderGoodsService.list(new LambdaQueryWrapper<OrderGoods>().eq(OrderGoods::getOrderId, d.getOrderId())));
          });
        }
        return new PageResult<>(list, page.getTotal());
    }

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

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

  @Override
  public void paySuccess(Order order) {
    System.out.println("order = " + order);
    // 安装插件
    menuService.cloneMenu(order.getMenuParam());
    order.setPayStatus(true);
    // 实际支付金额
    if (order.getPayType().equals(0)) {
      order.setPayPrice(order.getPayPrice());
    }
    order.setPayTime(DateUtil.date());
    order.setStartTime(System.currentTimeMillis());
    order.setExpirationTime(DateUtil.offsetMonth(DateUtil.date(), order.getMonth()));
    updateById(order);
//    orderGoodsService.update(new LambdaUpdateWrapper<OrderGoods>().eq(OrderGoods::getOrderId, order.getOrderId())
//      .set(OrderGoods::getPayStatus, 1)
//      .set(OrderGoods::getPayTime, DateUtil.date())
//      .set(OrderGoods::getExpirationTime, DateUtil.offsetMonth(DateUtil.date(), order.getMonth()))
//    );

    final List<OrderGoods> list = orderGoodsService.list(new LambdaQueryWrapper<OrderGoods>().eq(OrderGoods::getOrderId, order.getOrderId()));
    if (!CollectionUtils.isEmpty(list)) {
      list.forEach(d -> {
        d.setPayStatus(true);
        d.setPayTime(DateUtil.date());
        d.setExpirationTime(DateUtil.offsetMonth(DateUtil.date(), order.getMonth()));
      });
      // 更新订单商品状态
      orderGoodsService.updateBatchById(list);
      // 累计销量
      Company company = companyService.getById(list.get(0).getItemId());
      company.setBuys(company.getBuys() + list.size());
      System.out.println("company = " + company);
      companyService.updateById(company);
    }
  }

}
