package com.yida.data.device.studentCard.controller;

import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.card.EduStudentCardFence;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.device.studentCard.service.EduStudentCardFenceService;
import com.yida.data.device.vo.studentCard.FenceListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 电子学生证-电子围栏主信息 Controller
 *
 * @author zhanghaijun
 * @date 2021-08-04 15:07:58
 */
@Api(tags = "学生证-围栏")
@Slf4j
@Validated
@RestController
@RequestMapping("/studentCard/fence")
@RequiredArgsConstructor
public class EduStudentCardFenceController {

    private final EduStudentCardFenceService eduStudentCardFenceService;

    @ApiOperation("前后台通用新增、编辑围栏")
    @PostMapping("/saveFence")
    public ResultBean saveFence(@RequestBody EduStudentCardFence fence) {
        eduStudentCardFenceService.saveFence(fence);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询围栏列表")
    @GetMapping("/listFence")
    public ResultBean<List<FenceListVO>> listFence(@ApiParam("学校id") @RequestParam(required = false) Long schoolId,
        @ApiParam("imei") @RequestParam(required = false) String imei,
        @ApiParam("创建人类型，0-学校，1-家长") Integer createType,
        @ApiParam("围栏名称") @RequestParam(required = false) String name) {
        if (schoolId == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (currentUser.getDeptType() == 0) {
                schoolId = currentUser.getDeptId();
            }
        }
        return ResultBean.buildSuccess(eduStudentCardFenceService.listFence(schoolId, imei, createType,
            name));
    }

    @ApiOperation("前后台通用删除围栏")
    @PostMapping("/delFenceBatch")
    public ResultBean delFenceBatch(@ApiParam("围栏id") @RequestBody List<Long> ids) {
        eduStudentCardFenceService.delFence(ids);
        return ResultBean.buildSuccess();
    }
}
