package com.qmth.sop.server.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.result.SysDeviceResult;
import com.qmth.sop.business.entity.SysDevice;
import com.qmth.sop.business.entity.TBTask;
import com.qmth.sop.business.service.SysDeviceService;
import com.qmth.sop.business.service.TBTaskService;
import com.qmth.sop.business.templete.execute.AsyncSysDeviceImportService;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.DeviceStatusEnum;
import com.qmth.sop.common.enums.InOutTypeEnum;
import com.qmth.sop.common.enums.LogTypeEnum;
import com.qmth.sop.common.enums.TaskTypeEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.Map;
import java.util.Objects;

/**
 * 设备配置表 控制器
 * 
 * @author: shudonghui
 * @date: 2023-08-24 17:32:07
 * @version: 1.0
 * @email: shudonghui@qmth.com.cn
 * @Company: www.qmth.com.cn
 */
@Api(tags = "设备配置表 Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX+SystemConstant.PREFIX_URL_DEVICE)
public class SysDeviceController {


    @Resource
    SysDeviceService sysDeviceService;

    @Resource
    private TBTaskService tbTaskService;

    @Resource
    AsyncSysDeviceImportService asyncSysDeviceImportService;

    
    @ApiOperation(value = "设备配置表查询接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "设备配置表查询结果", response = SysDeviceResult.class)})
    public Result query(
                @ApiParam(value = "设备供应商", required = false) @RequestParam(required = false) Long supplierId,
                @ApiParam(value = "品牌", required = false) @RequestParam(required = false) String brand,
                @ApiParam(value = "型号", required = false) @RequestParam(required = false) String model,
                @ApiParam(value = "设备编号", required = false) @RequestParam(required = false) String deviceCode,
                @ApiParam(value = "设备序列号", required = false) @RequestParam(required = false) String serialNo,
                @ApiParam(value = "运行状态", required = false) @RequestParam(required = false) DeviceStatusEnum status,
                @ApiParam(value = "出库/入库", required = false) @RequestParam(required = false) InOutTypeEnum bound,
                @ApiParam(value = "启用/禁用", required = false) @RequestParam(required = false) Boolean enable,
                @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<SysDeviceResult> resultIPage = sysDeviceService.query(new Page<>(pageNumber, pageSize),supplierId,brand,model,deviceCode,serialNo,status,bound,enable);

        return ResultUtil.ok(resultIPage);
    }

    
    @ApiOperation(value = "设备配置表新增修改接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result save(@Valid @ApiParam(value = "设备配置表信息", required = true) @RequestBody(required = true) SysDevice sysDevice, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(sysDeviceService.saveSysDevice(sysDevice));
    }

    
    @ApiOperation(value = "设备配置表获取单个接口")
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "设备配置表信息", response = SysDevice.class)})
    public Result get(@ApiParam(value = "设备配置表id", required = true) @RequestParam long id) {
        return ResultUtil.ok(sysDeviceService.getById(id));
    }

    //批量禁用设备
    
    @ApiOperation(value = "批量禁用设备接口")
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.DELETE)
    public Result disable(@ApiParam(value = "设备配置表id", required = true) @RequestParam(required = true) long[] ids) {
        return ResultUtil.ok(sysDeviceService.disable(ids));
    }

    //批量启用设备
    
    @ApiOperation(value = "批量启用设备接口")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UN_KNOW)
    public Result enable(@ApiParam(value = "设备配置表id", required = true) @RequestParam(required = true) long[] ids) {
        return ResultUtil.ok(sysDeviceService.enable(ids));
    }

    //批量删除设备
    
    @ApiOperation(value = "批量删除设备接口")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.DELETE)
    public Result delete(@ApiParam(value = "设备配置表id", required = true) @RequestParam(required = true) long[] ids) {
        return ResultUtil.ok(sysDeviceService.delete(ids));
    }

    @ApiOperation(value = "设备导入")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.IMPORT)
    public Result importUserArchives(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file) throws Exception {
        Map<String, Object> map = tbTaskService.saveTask(file, TaskTypeEnum.DEVICE_IMPORT);
        asyncSysDeviceImportService.importTask(map);
        TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
        return ResultUtil.ok(tbTask.getId());
    }

    @ApiOperation(value = "设备配置表列表接口")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "设备配置表列表", response = SysDevice.class)})
    public Result list(@ApiParam(value = "查询条件", required = false) @RequestParam(required = false) Boolean enable) {
        return ResultUtil.ok(sysDeviceService.list(new QueryWrapper<SysDevice>().lambda().eq(Objects.nonNull(enable),SysDevice::getEnable, enable)));
    }
}