package org.jeecg.modules.apiService.impl;

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.ApplyService;
import org.jeecg.modules.carrentApply.entity.CarrentApply;
import org.jeecg.modules.carrentApply.service.ICarrentApplyService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ApplyServiceImpl implements ApplyService {

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

    //新闻信息
    @Resource
    private ICarrentApplyService carrentApplyService;
    /*************************************************************************************/

    //寻车申请-申请列表
    @Override
    public Result<?> queryApplyList(String token, PageBean pageBean) {
        //返回信息
        String message = "寻车申请列表查询失败!";
        //新闻列表
        Page<CarrentApply> pageList = null;
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //分页
            Page<CarrentApply> page = new Page<CarrentApply>(pageBean.getPageNo(), pageBean.getPageSize());
            //获取寻车申请列表
            pageList = carrentApplyService
                    .lambdaQuery()
                    .eq(CarrentApply::getUserId, hanHaiMember.getId())
                    .page(page);

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

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

    //寻车申请-添加申请
    @Override
    public Result<?> addApply(String token, CarrentApply carrentApply) {
        //返回信息
        String message = "添加申请失败";
        //执行结果
        boolean result = false;
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        carrentApply.setUserId(hanHaiMember.getId());

        try{
            //执行寻车申请添加
            result = carrentApplyService.save(carrentApply);

            //判断执行结果
            if(result){
                message = "添加申请成功！";
            }else {
                message = "添加申请失败！";
            }

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

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