package com.sqx.modules.emergencyContact.controller;

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.app.service.UserService;
import com.sqx.modules.driver.entity.Driver;
import com.sqx.modules.emergencyContact.entity.EmergencyContact;
import com.sqx.modules.emergencyContact.service.EmergencyContactService;
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/emergency")
public class EmergencyContactController {

    @Autowired
    private EmergencyContactService emergencyService;
    @Autowired
    private UserService userService;

    @Login
    @PostMapping("/insertEmergency")
    @ApiOperation("新增紧急联系人")
    public Result insertEmergency(@RequestAttribute("userId")Long userId, EmergencyContact emergencyContact){
        emergencyContact.setUserId(userId);
        emergencyService.insert(emergencyContact);
        return Result.success();
    }

    @Login
    @PostMapping("/updateEmergency")
    @ApiOperation("修改紧急联系人")
    public Result updateEmergency(@RequestAttribute("userId")Long userId, EmergencyContact emergencyContact){
        emergencyContact.setUserId(userId);
        return emergencyService.updateEmergency(emergencyContact);
    }

    @PostMapping("/deleteEmergency")
    @ApiOperation("删除紧急联系人")
    public Result deleteEmergency(Long id){
        return emergencyService.deleteEmergency(id);
    }

    @Login
    @GetMapping("/selectDriverList")
    @ApiOperation("查询紧急联系人")
    public Result selectEmergencyList(@RequestAttribute("userId")Long userId){
        return Result.success().put("data",emergencyService.list(new QueryWrapper<EmergencyContact>().eq("user_id",userId)));
    }

    @Login
    @PostMapping("/inviter")
    @ApiOperation("查询邀请列表")
    public Result selectInviterList(@RequestAttribute("userId") Long userId ,Integer page, Integer limit){
        /*List<UserEntity> userEntities = userService.selectInviterList(userId);
        for (UserEntity user: userEntities){
            int size = ordersService.list(new QueryWrapper<Orders>().eq("user_id", user.getUserId()).eq("status", 5)).size();
            if (size > 0){
                user.setFirstOrder(1);
                userService.updateById(user);
            }
        }*/
        return Result.success().put("data",userService.selectInviterList(page,limit,userId));
    }
}
