package com.gree.mall.miniapp.logic.order;

import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.order.OrderDetailBean;
import com.gree.mall.miniapp.bean.order.OrderRefundDetailBean;
import com.gree.mall.miniapp.enums.OrderStatusEnum;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.OrderDetail;
import com.gree.mall.miniapp.plus.entity.OrderInfo;
import com.gree.mall.miniapp.plus.entity.OrderRefund;
import com.gree.mall.miniapp.plus.service.OrderDetailService;
import com.gree.mall.miniapp.plus.service.OrderRefundService;
import com.gree.mall.miniapp.enums.OrderStatusEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderRefundLogic {

    @Autowired
    OrderRefundService orderRefundService;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    CommonLogic commonLogic;


    /**
     * 订单列表
     * @return
     */
    public IPage<OrderRefundDetailBean> list(String orderId, String orderRefundId, String userName, String phone, String orderStatus,
                                             String startCreateTime, String endCreateTime, String startExamineTime, String endExamineTime,
                                             Integer pageNum, Integer pageSize){

        IPage page = orderRefundService.lambdaQuery().like(StringUtils.isNotBlank(orderId), OrderRefund::getOrderId, orderId)
                .like(StringUtils.isNotBlank(orderRefundId), OrderRefund::getOrderRefundId, orderRefundId)
                .like(StringUtils.isNotBlank(userName), OrderRefund::getUserName, userName)
                .like(StringUtils.isNotBlank(phone), OrderRefund::getPhone, phone)
                .eq(StringUtils.isNotBlank(orderStatus), OrderRefund::getOrderStatus, orderStatus)
                .between(StringUtils.isNotBlank(startCreateTime) && StringUtils.isNotBlank(endCreateTime), OrderRefund::getCreateTime, startCreateTime, endCreateTime)
                .between(StringUtils.isNotBlank(startExamineTime) && StringUtils.isNotBlank(endExamineTime), OrderRefund::getExamineTime, startExamineTime, endExamineTime)
                .orderByDesc(OrderRefund::getCreateTime).page(new Page<>(pageNum, pageSize));

        List<OrderRefundDetailBean> list = new ArrayList<>();
        for(Object o : page.getRecords()){
            OrderInfo orderInfo = (OrderInfo)o;
            OrderRefundDetailBean detailBean = new OrderRefundDetailBean();
            BeanUtils.copyProperties(orderInfo,detailBean);
            //查询订单详情
            List<OrderDetail> orderDetails = orderDetailService.lambdaQuery().eq(OrderDetail::getOrderId, orderInfo.getOrderId()).list();
            detailBean.setOrderDetails(orderDetails);
            list.add(detailBean);
        }
        page.setRecords(list);
        return page;
    }

    /**
     * 审核
     */
    public void examine(String orderRefundId,String examineStatus){
        orderRefundService.lambdaUpdate()
                .set(OrderRefund::getExamineStatus,examineStatus)
                .set(OrderRefund::getExamineTime,new Date())
                .set(OrderRefund::getOrderStatus,OrderStatusEnum.DSJSH.toString())
                .eq(OrderRefund::getOrderRefundId,orderRefundId).update();
    }

    /**
     * 备注
     */
    public void remark(String orderRefundId,String remark){
        orderRefundService.lambdaUpdate().set(OrderRefund::getRemark,remark).eq(OrderRefund::getOrderRefundId,orderRefundId).update();
    }

}
