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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.driver.dao.DriverDao;
import com.sqx.modules.driver.entity.Driver;
import com.sqx.modules.driver.service.DriverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@Slf4j
@Service
public class DriverServiceImp extends ServiceImpl<DriverDao, Driver> implements DriverService {

    private ReentrantReadWriteLock reentrantReadWriteLock=new ReentrantReadWriteLock(true);

    @Override
    public Result insertDriver(Driver driver){
        reentrantReadWriteLock.writeLock().lock();
        try {
            driver.setStatus(1);
            driver.setCreateTime(DateUtils.format(new Date()));
            Driver driver1 = selectDriverByUserId(driver.getUserId());
            if(driver1!=null){
                driver.setDriverId(driver1.getDriverId());
                baseMapper.updateById(driver);
            }else{
                driver.setSeatNum(4);
                driver.setIsFriends("2");
                driver.setIsCityOrders(1);
                driver.setIsProvinceOrders(1);
                driver.setOrdersKm(10);
                driver.setOrdersStartTime("全天");
                driver.setOrdersType(1);
                driver.setMusicType(2);
                baseMapper.insert(driver);
            }
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            log.error("提交司机认证出错："+e.getMessage(),e);
        }finally {
            reentrantReadWriteLock.writeLock().unlock();
        }
        return Result.error("系统繁忙，请稍后再试！");
    }

    @Override
    public Driver selectDriverByUserId(Long userId){
        return baseMapper.selectOne(new QueryWrapper<Driver>().eq("user_id",userId));
    }

    @Override
    public Result updateDriver(Driver driver){
        baseMapper.updateById(driver);
        return Result.success();
    }

    @Override
    public Result deleteDriver(Long driverId){
        baseMapper.deleteById(driverId);
        return Result.success();
    }

    @Override
    public Result selectDriverList(Integer page,Integer limit,String driverName,String userName,String phone,Integer status){
        return Result.success().put("data",new PageUtils(baseMapper.selectDriverList(new Page<>(page,limit),driverName,userName,phone,status)));
    }

    @Override
    public Result auditDriver(String ids,Integer status,String auditContent){
        for (String id:ids.split(",")){
            Driver driver = baseMapper.selectById(Long.parseLong(id));
            if(driver.getStatus()==1){
                driver.setStatus(status);
                driver.setAuditContent(auditContent);
                baseMapper.updateById(driver);
            }
        }
        return Result.success();
    }



}
