package com.zcxk.charge.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.charge.web.common.Pagination;
import com.zcxk.charge.api.dto.DeviceParmInfoDto;
import com.zcxk.charge.api.dto.RequestParmDto;
import com.zcxk.charge.core.entity.DeviceParmEntity;
import com.zcxk.charge.web.service.DeviceParmService;
import com.zcxk.core.common.pojo.AjaxMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description
 * @author  wangli
 * @data 2020-11-17 11:17
 */
@RestController
@RequestMapping("/deviceParm")
@Api(tags = "设备参数管理")
public class DeviceParmController {

    @Autowired
    private DeviceParmService deviceParmService;

    @RequestMapping(value="get" , method = RequestMethod.GET)
    @ApiOperation(value = "查询单个设备参数信息")
    public AjaxMessage<DeviceParmEntity> get(@ApiParam(value = "设备参数信息id") @RequestParam Integer id){
        return AjaxMessage.success(deviceParmService.selectById(id));
    }

    @RequestMapping(value="getBySceneDeviceId" , method = RequestMethod.GET)
    @ApiOperation(value = "查询单个设备参数信息(参数设置页面)")
    public AjaxMessage<List<DeviceParmEntity>> getBySceneDeviceId(@ApiParam(value = "场景id",required = true) @RequestParam Integer sceneId,@ApiParam(value = "设备id",required = true) @RequestParam Integer id){
        return AjaxMessage.success(deviceParmService.selectByDeviceId(sceneId,id));
    }

    @RequestMapping(value="batchEdit" , method = RequestMethod.POST)
    @ApiOperation(value = "批量保存设备参数信息(参数设置页面)")
    public AjaxMessage<Integer> batchEdit( @ApiParam(value = "设备参数信息") @RequestBody List<DeviceParmEntity> deviceParmEntities){
        deviceParmService.saveOrUpdateBatch(deviceParmEntities);
        return AjaxMessage.success(1 );
    }
    @RequestMapping(value="getBySceneAndDeviceId" , method = RequestMethod.GET)
    @ApiOperation(value = "查询单个设备参数信息(场景参数绑定页面)")
    public AjaxMessage getBySceneAndDeviceId(@ApiParam(value = "一级场景id",required = true) @RequestParam Integer parentSceneId, @ApiParam(value = "场景id",required = true) @RequestParam Integer sceneId, @ApiParam(value = "设备id",required = true) @RequestParam Integer deviceId){
        return AjaxMessage.success(deviceParmService.selectBySceneAndDeviceId(parentSceneId,sceneId,deviceId));
    }
    @RequestMapping(value="batchSaveEdit" , method = RequestMethod.POST)
    @ApiOperation(value = "批量保存设备参数信息(场景参数绑定页面)")
    public AjaxMessage<Integer> batchSaveEdit(
            @ApiParam(value = "设备参数信息") @RequestBody RequestParmDto requestParmDto) {
        deviceParmService.batchSaveEdit(requestParmDto.getSceneId(), requestParmDto.getDeviceId(), requestParmDto.getDeviceParmInfos());
        return AjaxMessage.success(1);
    }

    @RequestMapping(value="selectPage" , method = RequestMethod.GET)
    @ApiOperation(value = "分页查询设备参数信息")
    public AjaxMessage<Pagination<DeviceParmEntity>> selectPage(
            @ApiParam(value = "设备参数信息id") @RequestParam(required = false) Integer id,
            @ApiParam(value = "页数,非必传，默认第一页",  defaultValue = "1") @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @ApiParam(value = "条数，非必传，默认10条",  defaultValue = "10") @RequestParam(required = false, defaultValue = "10") Integer pageSize
    ){
        DeviceParmEntity deviceDto = new DeviceParmEntity();
        IPage<DeviceParmEntity> iPage = new Page<>(pageNum, pageSize);
        if(iPage.orders().size() == 0){
            OrderItem orderItem =new OrderItem();
            orderItem.setAsc(false);
            orderItem.setColumn("d.date_create");
            iPage.orders().add(orderItem);
        }
        iPage = deviceParmService.selectPage(iPage,deviceDto);
        Pagination<DeviceParmEntity> pages = new Pagination<>(iPage);
        return AjaxMessage.success(pages);
    }

    @RequestMapping(value="add" , method = RequestMethod.POST)
    @ApiOperation(value = "新增设备参数信息")
    public AjaxMessage<Integer> add(@ApiParam(value = "设备参数信息") @RequestBody List<DeviceParmEntity> deviceParmEntities){
        return AjaxMessage.success( deviceParmService.batchInsert(deviceParmEntities));
    }

    @RequestMapping(value="edit" , method = RequestMethod.POST)
    @ApiOperation(value = "修改设备参数信息")
    public AjaxMessage<Integer> edit( @ApiParam(value = "设备参数信息") @RequestBody DeviceParmEntity deviceParmEntity){
        return AjaxMessage.success(deviceParmService.update(deviceParmEntity));
    }

    @RequestMapping(value="delete" , method = RequestMethod.GET)
    @ApiOperation(value = "单个删除设备参数信息")
    public AjaxMessage<Integer> delete( @ApiParam(value = "设备参数信息id") @RequestParam Integer id){
        return AjaxMessage.success(deviceParmService.delete(id));
    }

}
