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.api.dto.query.DevicePageDto;
import com.zcxk.charge.api.dto.query.DeviceQueryDto;
import com.zcxk.charge.api.vo.DeviceVo;
import com.zcxk.charge.web.common.Pagination;
import com.zcxk.charge.api.dto.DeviceDto;
import com.zcxk.charge.core.entity.DeviceEntity;
import com.zcxk.charge.core.entity.SceneEntity;
import com.zcxk.charge.web.advice.LogAnnotation;
import com.zcxk.charge.web.service.DeviceService;
import com.zcxk.charge.web.service.SceneService;
import com.zcxk.charge.web.util.OrgInfoUtil;
import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.common.util.BeanCopyUtils;
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 javax.validation.Valid;
import java.util.List;

/**
 * @description
 * @author  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 get(@ApiParam(value = "设备信息id") @RequestParam Long id){
         return AjaxMessage.success(deviceService.selectById(id));
    }

    @RequestMapping(value="selectPage" , method = RequestMethod.POST)
    @ApiOperation(value = "分页查询设备信息")
    public AjaxMessage selectPage(@RequestBody @Valid DevicePageDto dto){
        DeviceDto deviceDto = new DeviceDto();
        BeanCopyUtils.copyProperties(dto, deviceDto, DeviceDto.class);
        IPage<DeviceDto> iPage = new Page<>(dto.getPageNum(), dto.getPageSize());
        if (iPage.orders().size() == 0){
            OrderItem orderItem =new OrderItem();
            orderItem.setAsc(false);
            orderItem.setColumn("d.date_create");
            iPage.orders().add(orderItem);
        }
        Pagination<DeviceVo> pages = new Pagination<>(deviceService.selectPage(iPage,deviceDto));
        for(DeviceVo device : pages.getList()){
            device.setCompanyOrgName(orgInfoUtil.getOrgName(device.getCompanyOrgId()));
            device.setDeptOrgName(orgInfoUtil.getOrgName(device.getDeptOrgId()));
        }
        return AjaxMessage.success(pages);
    }

    @RequestMapping(value="selectList" , method = RequestMethod.POST)
    @ApiOperation(value = "查询设备信息")
    public AjaxMessage selectList(@RequestBody @Valid DevicePageDto dto){
        DeviceDto deviceDto = new DeviceDto();
        BeanCopyUtils.copyProperties(dto, deviceDto, DeviceDto.class);
        return AjaxMessage.success(deviceService.selectListByType(deviceDto));
    }

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

    @RequestMapping(value="selectListByScene" , method = RequestMethod.POST)
    @ApiOperation(value = "按场景查询设备信息(场景绑定设备信息)")
    public AjaxMessage selectListByScene(@RequestBody @Valid DeviceQueryDto dto){
        DeviceDto deviceDto = new DeviceDto();
        BeanCopyUtils.copyProperties(dto, deviceDto, DeviceDto.class);
        deviceDto.setEnableState(1);
        List<DeviceVo> deviceDtos = deviceService.selectListByScene(deviceDto);
        // 按场景ID,查询一级场景ID
        SceneEntity sceneEntity = sceneService.findParentSceneById(deviceDto.getSceneId().longValue());
        for (DeviceVo item: deviceDtos) {
            item.setSceneId(deviceDto.getSceneId());
            item.setParentSceneId(sceneEntity.getParentSceneId().intValue());
            item.setCompanyOrgName(orgInfoUtil.getOrgName(item.getCompanyOrgId()));
            item.setDeptOrgName(orgInfoUtil.getOrgName(item.getDeptOrgId()));
        }
        return AjaxMessage.success(deviceDtos);
    }

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

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

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

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