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.GoodsService;
import org.jeecg.modules.carrentGoods.entity.CarrentGoods;
import org.jeecg.modules.carrentGoods.service.ICarrentGoodsService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class GoodsServiceImpl implements GoodsService {
    /*************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;

    //车辆信息
    @Resource
    private ICarrentGoodsService carrentGoodsService;

    /*************************************************************************************/
    //商品信息-获取商品列表
    @Override
    public Result<?> queryGoodsList(String token, CarrentGoods goods, PageBean pageBean) {
        //返回信息
        String message = "获取商品列表失败!";
        //商品列表
        Page<CarrentGoods> pageList = null;
        //分页信息
        Page<CarrentGoods> page = null;

        try{
            //分页
            page = new Page<CarrentGoods>(pageBean.getPageNo(), pageBean.getPageSize());
            LambdaQueryChainWrapper<CarrentGoods> query = carrentGoodsService.lambdaQuery();

            //组装查询条件
            if(null != goods.getCartypeId()){
                //车辆类型
                query.eq(CarrentGoods::getCartypeId, goods.getCartypeId());
            }
            if(null != goods.getCategorytwoId()){
                //分类
                query.eq(CarrentGoods::getCategorytwoId, goods.getCategorytwoId());
            }
            if(null != goods.getGoodsTime()){
                //时间
                query.le(CarrentGoods::getGoodsTime, goods.getGoodsTime());
            }
            if(null != goods.getMileage()){
                //公里数
                query.between(CarrentGoods::getMileage, 0, goods.getMileage());
            }
            if(null != goods.getGoodsName()){
                //商品名称
                query.like(CarrentGoods::getGoodsName, goods.getGoodsName());
            }
            if(null != token){
                //权限验证
                HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
                //关联用户id
                query.eq(CarrentGoods::getUserId, hanHaiMember.getId());
            }

            //获取商品列表
            pageList = query.page(page);

            //判断执行结果
            if(null != pageList){
                message = "商品列表信息";
            }else {
                message = "商品列表信息为空";
            }

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

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

    //商品信息-根据id获取商品详情
    @Override
    public Result<?> queryGoodsById(String goodsId) {
        //返回信息
        String message = "获取商品详情失败！";
        //商品详情
        CarrentGoods one = null;

        try{
            //获取商品信息详情
            one = carrentGoodsService.getById(goodsId);

            //判断执行结果
            if(null != one){
                message = "商品详情信息";
            }else {
                message = "商品详情信息为空";
            }

            //返回执行结果
            return Result.OK(message, one);
        }catch (Exception e){
            //错误信息打印
            e.printStackTrace();
            return Result.error(message, one);
        }
    }

    //商品信息-车辆信息发布
    @Override
    public Result<?> addGoods(String token, CarrentGoods goods) {
        //返回信息
        String message = "车辆信息发布失败！";
        //执行结果
        boolean flag = false;
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        goods.setUserId(hanHaiMember.getId());

        try{
            //获取商品信息详情
            flag = carrentGoodsService.save(goods);

            //判断执行结果
            if(flag){
                message = "车辆信息发布成功";
            }else {
                message = "车辆信息发布失败";
            }

            //返回执行结果
            return Result.OK(message);
        }catch (Exception e){
            //错误信息打印
            e.printStackTrace();
            return Result.error(message);
        }
    }
}
