package com.zcxk.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.dto.DeviceDto;
import com.zcxk.entity.DeviceEntity;
import com.zcxk.entity.SceneEntity;
import com.zcxk.logAdvice.LogAnnotation;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.DeviceService;
import com.zcxk.service.SceneService;
import com.zcxk.util.OrgInfoUtil;
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.ArrayList;
import java.util.List;

/**
 * @description
 * @auto wangli
 * @data 2020-11-16 11:05
 */
@RestController
@RequestMapping("/device")
@Api(tags = "设备信息管理")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private OrgInfoUtil orgInfoUtil;
    @Autowired
    private SceneService sceneService;


    @RequestMapping(value="get" , method = RequestMethod.GET)
    @ApiOperation(value = "查询单个设备信息")
    public AjaxMessage<DeviceDto> get( @ApiParam(value = "设备信息id") @RequestParam Long id){
         return new AjaxMessage<>(ResultStatus.OK, deviceService.selectById(id));
    }

    @RequestMapping(value="selectPage" , method = RequestMethod.GET)
    @ApiOperation(value = "分页查询设备信息")
    public AjaxMessage<Pagination<DeviceDto>> selectPage(
            @ApiParam(value = "设备编号/名称") @RequestParam(required = false)  String condition,
            @ApiParam(value = "所属公司") @RequestParam(required = false)  Integer companyOrgId,
            @ApiParam(value = "所属部门") @RequestParam(required = false)  Integer deptOrgId,
            @ApiParam(value = "所属场景id") @RequestParam(required = false)  Integer sceneId,
            @ApiParam(value = "设备状态") @RequestParam(required = false) Integer enableState,
            @ApiParam(value = "页数,非必传，默认第一页",  defaultValue = "1") @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @ApiParam(value = "条数，非必传，默认10条",  defaultValue = "10") @RequestParam(required = false, defaultValue = "10") Integer pageSize
    ){
        DeviceDto deviceDto = new DeviceDto();
        deviceDto.setCondition(condition);
        deviceDto.setCompanyOrgId(companyOrgId);
        deviceDto.setDeptOrgId(deptOrgId);
        deviceDto.setSceneId(sceneId);
        deviceDto.setEnableState(enableState);

        IPage<DeviceDto> 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 = deviceService.selectPage(iPage,deviceDto);

        Pagination<DeviceDto> pages = new Pagination<>(iPage);
        for(DeviceDto device : pages.getList()){
            device.setCompanyOrgName(orgInfoUtil.getOrgName(device.getCompanyOrgId()));
            device.setDeptOrgName(orgInfoUtil.getOrgName(device.getDeptOrgId()));
        }
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }

    @RequestMapping(value="selectList" , method = RequestMethod.GET)
    @ApiOperation(value = "查询设备信息")
    public AjaxMessage<List<DeviceDto>> selectList(
            @ApiParam(value = "设备编号/名称") @RequestParam(required = false)  String condition,
            @ApiParam(value = "所属公司") @RequestParam(required = false)  Integer companyOrgId,
            @ApiParam(value = "所属部门") @RequestParam(required = false)  Integer deptOrgId,
            @ApiParam(value = "所属场景id") @RequestParam(required = false)  Integer sceneId,
            @ApiParam(value = "场景查询范围：1 不查询子级；0 查询子级",  defaultValue = "0") @RequestParam(required = false,defaultValue = "0")  Integer searchType,
            @ApiParam(value = "设备状态") @RequestParam(required = false) Integer enableState
    ){
        DeviceDto deviceDto = new DeviceDto();
        deviceDto.setCondition(condition);
        deviceDto.setCompanyOrgId(companyOrgId);
        deviceDto.setDeptOrgId(deptOrgId);
        deviceDto.setSceneId(sceneId);
        deviceDto.setEnableState(enableState);
        deviceDto.setSearchType(searchType);
        return new AjaxMessage<>(ResultStatus.OK, deviceService.selectListByType(deviceDto));
    }

    @RequestMapping(value="selectListBySceneId" , method = RequestMethod.GET)
    @ApiOperation(value = "根据场景查询设备信息（场景设备联动）")
    public AjaxMessage<List<DeviceDto>> selectListBySceneId(
            @ApiParam(value = "所属场景id") @RequestParam(required = false)  Integer sceneId
    ){
        DeviceDto deviceDto = new DeviceDto();
        deviceDto.setSceneId(sceneId);
        return new AjaxMessage<>(ResultStatus.OK, deviceService.selectListBySceneId(deviceDto));
    }

    @RequestMapping(value="selectListByScene" , method = RequestMethod.GET)
    @ApiOperation(value = "按场景查询设备信息(场景绑定设备信息)")
    public AjaxMessage<List<DeviceDto>> selectListByScene(
            @ApiParam(value = "设备编号/名称") @RequestParam(required = false)  String condition,
            @ApiParam(value = "所属公司") @RequestParam(required = false)  Integer companyOrgId,
            @ApiParam(value = "所属部门") @RequestParam(required = false)  Integer deptOrgId,
            @ApiParam(value = "所属场景id",required = true) @RequestParam(required = true)  Integer sceneId
    ){
        DeviceDto deviceDto = new DeviceDto();
        deviceDto.setCondition(condition);
        deviceDto.setCompanyOrgId(companyOrgId);
        deviceDto.setDeptOrgId(deptOrgId);
        deviceDto.setEnableState(1);
        deviceDto.setSceneId(sceneId);
        List<DeviceDto> deviceDtos = new ArrayList<>();
        deviceDtos = deviceService.selectListByScene(deviceDto);
        //按场景ID,查询一级场景ID
        SceneEntity sceneEntity = sceneService.findParentSceneById(sceneId.longValue());
        for(DeviceDto item: deviceDtos)
        {
            item.setSceneId(sceneId);
            item.setParentSceneId(sceneEntity.getParentSceneId().intValue());
            item.setCompanyOrgName(orgInfoUtil.getOrgName(item.getCompanyOrgId()));
            item.setDeptOrgName(orgInfoUtil.getOrgName(item.getDeptOrgId()));
        }
        return new AjaxMessage<>(ResultStatus.OK, deviceDtos);
    }

    @RequestMapping(value="add" , method = RequestMethod.POST)
    @ApiOperation(value = "新增设备信息")
    @LogAnnotation(module = "【设备管理】新增")
    public AjaxMessage<Integer> add( @ApiParam(value = "设备信息") @RequestBody DeviceEntity deviceEntity){
        return new AjaxMessage<>(ResultStatus.OK, deviceService.add(deviceEntity));
    }

    @RequestMapping(value="edit" , method = RequestMethod.POST)
    @ApiOperation(value = "修改设备信息")
    @LogAnnotation(module = "【设备管理】修改")
    public AjaxMessage<Integer> edit( @ApiParam(value = "设备信息") @RequestBody DeviceEntity deviceEntity){
        return new AjaxMessage<>(ResultStatus.OK, deviceService.update(deviceEntity));
    }

    @RequestMapping(value="delete" , method = RequestMethod.DELETE)
    @ApiOperation(value = "单个删除设备信息")
    @LogAnnotation(module = "【设备管理】删除")
    public AjaxMessage<Integer> delete( @ApiParam(value = "设备信息id") @RequestParam Long id){
        return new AjaxMessage<>(ResultStatus.OK, deviceService.delete(id));
    }

    @RequestMapping(value="batchDelete" , method = RequestMethod.DELETE)
    @ApiOperation(value = "批量删除设备信息")
    @LogAnnotation(module = "【设备管理】批量删除")
    public AjaxMessage<Integer> batchDelete( @ApiParam(value = "设备信息id") @RequestParam List<Long> ids){
        return new AjaxMessage<>(ResultStatus.OK, deviceService.batchDelete(ids));
    }
}
