package com.sqx.modules.riderStation.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.dao.UserDao;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.riderStation.dao.RiderStationDao;
import com.sqx.modules.riderStation.entity.RiderStation;
import com.sqx.modules.riderStation.service.RiderStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RiderStationServiceImpl extends ServiceImpl<RiderStationDao, RiderStation> implements RiderStationService {

    @Autowired
    private UserDao userDao;

    @Override
    public Result saveStation(RiderStation riderStation) {
        String stationName = riderStation.getStationName();
        LambdaQueryWrapper<RiderStation> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(RiderStation::getStationName,stationName);
        RiderStation station = this.getOne(wrapper);
        if (ObjectUtils.isNotEmpty(station)) {
            return Result.error("该站点已存在");
        }
        this.save(riderStation);
        return Result.success();
    }
    @Override
    public RiderStation stationById(Integer id){
        RiderStation result= baseMapper.selectById(id);
        return result;
    }

    @Override
    public Result selectStationList(Integer page, Integer size) {
        IPage<RiderStation> riderStationIPage = this.page(new Page<RiderStation>(page, size));
        PageUtils pageUtils = new PageUtils(riderStationIPage);
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result stationList() {
        List<RiderStation> list = this.list();
        return Result.success().put("data", list);
    }

    @Override
    public Result getRiderStationName(Integer userId) {
        UserEntity userEntity = userDao.selectById(userId);
        if (ObjectUtils.isEmpty(userEntity)) {
            return Result.error("用户不存在");
        }
        Integer riderStationId = userEntity.getRiderStationId();

        RiderStation riderStation = this.getById(riderStationId);

        return Result.success().put("data",riderStation);
    }

    @Override
    public Result updateStation(RiderStation riderStation) {
        Long id = riderStation.getId();
        String stationName = riderStation.getStationName();

        LambdaQueryWrapper<RiderStation> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(RiderStation::getStationName,stationName);
        RiderStation station = this.getOne(wrapper);

        if (ObjectUtils.isNotEmpty(station)) {
            Long id1 = station.getId();
            if (!id.equals(id1)) {
                return Result.error("所修改的站点已存在");
            }
        }
        this.updateById(riderStation);

        return Result.success();
    }

    @Override
    public Result removeStation(Integer id) {
        LambdaQueryWrapper<UserEntity> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UserEntity::getRiderStationId,id);
        List<UserEntity> entityList = userDao.selectList(wrapper);
        if (ObjectUtils.isNotEmpty(entityList)&&entityList.size()>0) {
            return Result.error("该站点下已存在骑手");
        }
        this.removeById(id);
        return Result.success();
    }

}
