package org.jeecg.modules.device.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.device.entity.DeviceLocation;
import org.jeecg.modules.device.service.IDeviceLocationService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

import static org.jeecg.modules.device.service.impl.DeviceLocationServiceImpl.buildTree;


/**
 * @Description: 设备位置表
 * @Author: jeecg-boot
 * @Date:   2025-02-12
 * @Version: V1.0
 */
@Api(tags="设备位置表")
@RestController
@RequestMapping("/device/deviceLocation")
@Slf4j
public class DeviceLocationController extends JeecgController<DeviceLocation, IDeviceLocationService> {
	@Autowired
	private IDeviceLocationService deviceLocationService;

	/**
	 * 分页列表查询
	 *
	 * @param deviceLocation
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "设备位置表-分页列表查询")
	@ApiOperation(value="设备位置表-分页列表查询", notes="设备位置表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<DeviceLocation>> queryPageList(DeviceLocation deviceLocation,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		LambdaQueryWrapper<DeviceLocation> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(StringUtils.isNotBlank(deviceLocation.getTitle()),DeviceLocation::getTitle,deviceLocation.getTitle());
		queryWrapper.like(ObjectUtil.isNotNull(deviceLocation.getLatitude()),DeviceLocation::getLatitude,deviceLocation.getLatitude());
		queryWrapper.like(ObjectUtil.isNotNull(deviceLocation.getLongitude()),DeviceLocation::getLongitude,deviceLocation.getLongitude());
		queryWrapper.eq(DeviceLocation::getDeleteFlag,0);
		Page<DeviceLocation> page = new Page<DeviceLocation>(pageNo, pageSize);
		IPage<DeviceLocation> pageList = deviceLocationService.page(page, queryWrapper);
		pageList.setRecords(buildTree(pageList.getRecords()));
		return Result.OK(pageList);
	}

	/**
	 * 获取全部位置
	 * @return
	 */
	@GetMapping(value = "/list/all")
	@ApiOperation(value="设备位置树", notes="设备位置树")
	public Result<List<DeviceLocation>> queryAllList() {
		List<DeviceLocation> pageList = deviceLocationService.list();
		return Result.OK(buildTree(pageList));
	}

	/**
	 *   添加
	 *
	 * @param deviceLocation
	 * @return
	 */
	@AutoLog(value = "设备位置表-添加")
	@ApiOperation(value="设备位置表-添加", notes="设备位置表-添加")
	//@RequiresPermissions("org.jeecg.modules:bu_device_location:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody DeviceLocation deviceLocation) {
		deviceLocationService.save(deviceLocation);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param deviceLocation
	 * @return
	 */
	@AutoLog(value = "设备位置表-编辑")
	@ApiOperation(value="设备位置表-编辑", notes="设备位置表-编辑")
	//@RequiresPermissions("org.jeecg.modules:bu_device_location:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DeviceLocation deviceLocation) {
		deviceLocationService.updateById(deviceLocation);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "设备位置表-通过id删除")
	@ApiOperation(value="设备位置表-通过id删除", notes="设备位置表-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:bu_device_location:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		deviceLocationService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "设备位置表-批量删除")
	@ApiOperation(value="设备位置表-批量删除", notes="设备位置表-批量删除")
	//@RequiresPermissions("org.jeecg.modules:bu_device_location:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.deviceLocationService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "设备位置表-通过id查询")
	@ApiOperation(value="设备位置表-通过id查询", notes="设备位置表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<DeviceLocation> queryById(@RequestParam(name="id",required=true) String id) {
		DeviceLocation deviceLocation = deviceLocationService.getById(id);
		if(deviceLocation==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(deviceLocation);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param deviceLocation
    */
    //@RequiresPermissions("org.jeecg.modules:bu_device_location:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DeviceLocation deviceLocation) {
        return super.exportXls(request, deviceLocation, DeviceLocation.class, "设备位置表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("bu_device_location:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DeviceLocation.class);
    }

}
