package com.yida.data.attendance.controller;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yida.data.attendance.service.EduAttendanceDeviceGroupService;
import com.yida.data.attendance.service.EduDeviceGroupService;
import com.yida.data.attendance.vo.DeviceOrDeviceGroupVO;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.attendance.EduAttendanceDevice;
import com.yida.data.common.core.entity.attendance.EduAttendanceDeviceGroup;
import com.yida.data.common.core.entity.attendance.EduDeviceGroup;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.log.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Arrays;
import java.util.Collections;
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-06-16 15:27:11
 */

@Api(tags = "考勤-设备分组")
@Slf4j
@Validated
@RestController
@RequestMapping("/deviceGroup")
@RequiredArgsConstructor
public class AttendanceDeviceGroupController {

    private final EduAttendanceDeviceGroupService eduAttendanceDeviceGroupService;
    private final EduDeviceGroupService eduDeviceGroupService;


    @ApiOperation("根据name分页查询分组")
    @GetMapping("/listDevicePage")
    public ResultBean<IPage<EduAttendanceDeviceGroup>> listDeviceGroupPage(@ApiParam("分组名称") @RequestParam String name,
        @ApiParam("当前页码") @RequestParam(defaultValue = "1") Long current,
        @ApiParam("页面大小") @RequestParam(defaultValue = "10") Long size,
        @ApiParam("学校id") @RequestParam(required = false) Long schoolId) throws NoSuchMethodException {

        return ResultBean.buildSuccess(eduAttendanceDeviceGroupService.selectGroup(name, current, size, schoolId));
    }


    @ApiOperation("全部查询")
    @GetMapping("/listDevice")
    public ResultBean<List<EduAttendanceDeviceGroup>> listDeviceGroup() {

        return ResultBean.buildSuccess(eduAttendanceDeviceGroupService.selectGroupAll());
    }


    @ApiOperation("新增或修改分组")
    @PostMapping("/insertGroup")
    @OperationLog(module = ModuleName.DEVICE, methods = "新增分组", type = OperationLogTypeEnum.SAVE)
    public ResultBean insertGroup(@ApiParam("分组详细") @RequestBody EduAttendanceDeviceGroup eduAttendanceDeviceGroup) {

        eduAttendanceDeviceGroupService.saveOrUpdate(eduAttendanceDeviceGroup);
        return ResultBean.buildSuccess();
    }


    @ApiOperation("删除分组")
    @GetMapping("/delGroup")
    @OperationLog(module = ModuleName.DEVICE, methods = "删除分组", type = OperationLogTypeEnum.DELETE)
    public ResultBean delGroup(@RequestParam Long id) {
        eduAttendanceDeviceGroupService.delGroup(id);
        return ResultBean.buildSuccess();
    }


    @ApiOperation("查询设备与设备组列表")
    @GetMapping("/listDeviceAndDeviceGroup")
    public ResultBean<List<DeviceOrDeviceGroupVO>> listDeviceAndDevice(String keyword) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        Long schoolId = null;
        if (currentUser.getDeptType() == 0) {
            schoolId = currentUser.getDeptId();
        }
        return ResultBean.buildSuccess(eduAttendanceDeviceGroupService.listDeviceAndDevice(keyword, schoolId));
    }

    @ApiOperation("根据设备组查询设备")
    @GetMapping("/listDeviceByGroup")
    public ResultBean<List<EduAttendanceDevice>> listDeviceByGroup(Long[] groupIds) {
        return ResultBean.buildSuccess(eduAttendanceDeviceGroupService.listDeviceByGroup(Arrays.asList(groupIds)));
    }

    @ApiOperation("查询设备组列表")
    @GetMapping("/listDeviceGroupById")
    public ResultBean<List<EduAttendanceDeviceGroup>> listDeviceGroupById(Long[] ids) {
        return ResultBean.buildSuccess(ArrayUtil.isNotEmpty(ids) ? eduAttendanceDeviceGroupService.listByIds(Arrays.asList(ids)) :
            Collections.EMPTY_LIST);
    }

    @ApiOperation("批量分组")
    @PostMapping("/batchGroup")
    @OperationLog(module = ModuleName.DEVICE, methods = "批量分组", type = OperationLogTypeEnum.UPDATE)
    public ResultBean batchGroup(@ApiParam("批量分组") @RequestBody List<EduDeviceGroup> list) {
        eduDeviceGroupService.insertGroup(list);
        return ResultBean.buildSuccess();
    }

}
