package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.OrderService;
import org.jeecg.modules.carrentGoods.entity.CarrentGoods;
import org.jeecg.modules.carrentGoods.service.ICarrentGoodsService;
import org.jeecg.modules.carrentOrder.entity.CarrentOrder;
import org.jeecg.modules.carrentOrder.service.ICarrentOrderService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.springframework.stereotype.Service;

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

@Service
public class OrderServiceImpl implements OrderService {

    /*************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;

    //预约信息
    @Resource
    private ICarrentOrderService carrentOrderService;

    //车辆信息
    @Resource
    private ICarrentGoodsService carrentGoodsService;
    /*************************************************************************************/
    //商品预约-查询预约列表
    @Override
    public Result<?> queryOrderList(String token, String typeId, PageBean pageBean) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        //分页
        Page<CarrentOrder> page = new Page<CarrentOrder>(pageBean.getPageNo(), pageBean.getPageSize());
        LambdaQueryChainWrapper<CarrentOrder> query = carrentOrderService
                .lambdaQuery();

        //组装查询条件
        query.eq(CarrentOrder::getUserId, hanHaiMember.getId());
        if(null != typeId){
            //车辆类型
            query.eq(CarrentOrder::getTypeId, typeId);
        }

        //获取收藏表信息
        Page<CarrentOrder> pageList = query.page(page);

        //将车辆信息放入列表中
        CarrentGoods carrentGoods;
        for (CarrentOrder record : pageList.getRecords()) {
            carrentGoods = carrentGoodsService.getById(record.getGoodsId());
            record.setCarrentGoods(carrentGoods);
        }

        return Result.OK("预约列表", pageList);
    }

    //商品预约-提交预约信息
    @Override
    public Result<?> addOrder(String token, CarrentOrder carrentOrder) {

        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        carrentOrder.setUserId(hanHaiMember.getId());

        //返回信息
        String message = "预约信息提交失败";
        //执行结果
        boolean result = false;

        try{
            //执行预约信息提交
            result = carrentOrderService.save(carrentOrder);

            //判断执行结果
            if(result){
                message = "预约信息提交成功！";
            }else {
                message = "预约信息提交失败！";
            }

            //返回执行结果
            return Result.OK(message);

        }catch (Exception e){
            //错误信息打印
            e.printStackTrace();
            return Result.error(message);
        }

    }

}
