package com.sqx.modules.driver.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.driver.entity.Driver;
import com.sqx.modules.driver.entity.DriverRoute;
import com.sqx.modules.driver.service.DriverRouteService;
import com.sqx.modules.driver.service.DriverService;
import com.sqx.modules.orders.entity.Orders;
import com.sqx.modules.orders.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@Api(value = "司机发布-用户端", tags = {"司机发布-用户端"})
@RequestMapping("/app/driverRoute")
public class AppDriverRouteController {

    @Autowired
    private DriverRouteService driverRouteService;
    @Autowired
    private OrdersService ordersService;

    @Login
    @PostMapping("/insertDriverRoute")
    @ApiOperation("司机发布")
    public Result insertDriverRoute(@RequestBody DriverRoute driverRoute){
        return driverRouteService.insertDriverRoute(driverRoute);
    }

    @Login
    @PostMapping("/updateDriverRoute")
    @ApiOperation("司机修改")
    public Result updateDriverRoute(@RequestBody DriverRoute driverRoute,@RequestAttribute Long userId){
        driverRouteService.updateById(driverRoute);
        return Result.success();
    }

    @Login
    @PostMapping("/deleteDriverRoute")
    @ApiOperation("删除发布")
    public Result deleteDriverRoute(Long driverRouteId,@RequestAttribute Long userId){
        int count = ordersService.count(new QueryWrapper<Orders>().eq("driver_route_id", driverRouteId).in("status", 3, 4));
        if(count>0){
            return Result.error("当前有正在进行中的订单，请完成后进行操作");
        }
        DriverRoute driverRoute = driverRouteService.getById(driverRouteId);
        driverRoute.setStatus(2);
        driverRouteService.updateById(driverRoute);
        return Result.success();
    }


    @Login
    @GetMapping("/selectDriverRouteListByUserId")
    @ApiOperation("查询自己发布")
    public Result selectDriverRouteByUserId(Integer page,Integer limit,@RequestAttribute Long userId){
        return Result.success().put("data",driverRouteService.selectDriverRouteListByUserId(page,limit,userId));
    }

    @Login
    @GetMapping("/selectDriverRouteById")
    @ApiOperation("查询行程详情")
    public Result selectDriverRouteById(Long driverRouteId){
        return Result.success().put("data",driverRouteService.getById(driverRouteId));
    }


    @Login
    @GetMapping("/selectDriverRouteList")
    @ApiOperation("查询发布列表")
    public Result selectDriverRouteList(Integer page,Integer limit,String shipProvince,String shipCity,String shipDistrict,String shipAddress,
                                        String deliveryProvince,String deliveryCity, String deliveryDistrict,String deliveryAddress,
                                        String userName,String phone,Long ordersId,Long userId,Integer status,String driverName,Integer isHighSpeedPrice){
        return driverRouteService.selectDriverRouteList(page, limit, shipProvince, shipCity, shipDistrict, shipAddress, deliveryProvince,
                deliveryCity, deliveryDistrict, deliveryAddress, userName, phone,ordersId,userId,status,driverName,isHighSpeedPrice);
    }

    @Login
    @PostMapping("/insertMessageByDriver")
    @ApiOperation("邀请司机接单")
    public Result insertMessageByDriver(Long ordersId,Long driverRouteId){
        return driverRouteService.insertMessageByDriver(ordersId, driverRouteId);
    }



}