package com.jumeirah.common.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumeirah.common.entity.Stroke;
import com.jumeirah.common.enums.StrokeStatusEnum;
import com.jumeirah.common.mapper.StrokeMapper;
import com.jumeirah.common.param.McStrokePageParam;
import com.jumeirah.common.param.StrokePageParam;
import com.jumeirah.common.service.StrokeService;
import com.jumeirah.common.vo.McStrokeQueryVo;
import com.jumeirah.common.vo.StrokeDetailVo;
import com.jumeirah.common.vo.StrokeQueryVo;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.shiro.jwt.JwtToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 行程表 服务实现类
 *
 * @author wei
 * @since 2020-09-29
 */
@Slf4j
@Service
public class StrokeServiceImpl extends BaseServiceImpl<StrokeMapper, Stroke> implements StrokeService {

    @Autowired
    private StrokeMapper strokeMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveStroke(Stroke stroke) throws Exception {
        return super.save(stroke);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateStroke(Stroke stroke) throws Exception {
        return super.updateById(stroke);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteStroke(Long id) throws Exception {
        JwtToken jwtToken = (JwtToken) SecurityUtils.getSubject().getPrincipal();
        Stroke byId = super.getById(id);
        // 判断该记录是否属于此用户
        if (!byId.getUserId().equals(jwtToken.getUserId())) {
            return false;
        }

        return super.removeById(id);
    }

    @Override
    public boolean cancelStroke(Long id) throws Exception {
        JwtToken jwtToken = (JwtToken) SecurityUtils.getSubject().getPrincipal();
        Stroke byId = super.getById(id);
        // 判断该记录是否属于此用户
        if (!byId.getUserId().equals(jwtToken.getUserId())) {
            return false;
        }
        Stroke stroke = new Stroke();
        stroke.setId(id);
        stroke.setStatus(StrokeStatusEnum.CANCEL.getCode());
        return super.updateById(stroke);
    }

    @Override
    public StrokeDetailVo getStrokeById(Long id) throws Exception {
        return strokeMapper.getStrokeById(id);
    }

    @Override
    public Paging<StrokeQueryVo> getStrokePageList(StrokePageParam strokePageParam) throws Exception {
        Page<StrokeQueryVo> page = new PageInfo<>(strokePageParam, OrderItem.asc(getLambdaColumn(Stroke::getCreateTime)));

        JwtToken jwtToken = (JwtToken) SecurityUtils.getSubject().getPrincipal();

        IPage<StrokeQueryVo> iPage = strokeMapper.getStrokePageList(page, strokePageParam, jwtToken.getUserId());

        return new Paging<StrokeQueryVo>(iPage);
    }

    @Override
    public Paging<McStrokeQueryVo> getMcStrokePageList(McStrokePageParam mcStrokePageParam) {
        Page<StrokeQueryVo> page = new PageInfo<>(mcStrokePageParam, OrderItem.asc(getLambdaColumn(Stroke::getCreateTime)));
        IPage<McStrokeQueryVo> mcStrokePageList = strokeMapper.getMcStrokePageList(page, mcStrokePageParam);
        return new Paging<McStrokeQueryVo>(mcStrokePageList);
    }

}
