package com.xinelu.web.controller.familydoctor;

import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.R;
import com.xinelu.familydoctor.entity.DeviceBindResident;
import com.xinelu.familydoctor.service.DeviceBindResidentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gaoyu
 * @description 设备绑定控制器
 * @date 2023-9-25 16:54
 */
@Api(tags = {"设备绑定控制器"})
@RestController
@RequestMapping("/fd/device")
public class DeviceBindResidentController extends BaseController {
    @Resource
    private DeviceBindResidentService deviceBindResidentService;

    @ApiOperation("绑定设备")
    @PostMapping("binding")
    public R<?> bind(@RequestBody DeviceBindResident entity) {
        if (deviceBindResidentService.repeatBind(entity)) {
            return R.fail("设备【" + entity.getSn() + "】已绑定");
        }
        int row = deviceBindResidentService.bindDevice(entity);
        if (row > 0) {
            return R.ok();
        } else {
           return R.fail("绑定失败");
        }
    }

    @ApiOperation("已绑定的设备")
    @GetMapping("bound/{identity}")
    @ApiImplicitParam(name = "identity", value = "身份证号", required = true)
    public R<List<DeviceBindResident>> boundDevice(@PathVariable String identity) {
        List<DeviceBindResident> list = deviceBindResidentService.boundDevice(identity);
        return R.ok(list);
    }

    @ApiOperation("解绑设备")
    @PostMapping("unbind")
    public R<?> unbindDevice(@RequestBody DeviceBindResident entity) {
        int row = deviceBindResidentService.unbindDevice(entity);
        if (row > 0) {
            return R.ok();
        } else {
            return R.fail("解绑失败");
        }
    }
}
