package com.ruoyi.asset.controller;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.Folder2ZipUtils;
import com.ruoyi.common.utils.http.HttpHelper;
import com.ruoyi.common.utils.http.HttpUtils;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.asset.domain.TbLocation;
import com.ruoyi.asset.service.ITbLocationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 所属位置Controller
 * 
 * @author 原动力
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/asset/location")
public class TbLocationController extends BaseController
{
    @Autowired
    private ITbLocationService tbLocationService;

    /**
     * 查询所属位置列表
     */
    @PreAuthorize("@ss.hasPermi('asset:location:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbLocation tbLocation)
    {
        startPage();
        List<TbLocation> list = tbLocationService.selectTbLocationList(tbLocation);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('asset:location:list')")
    @PostMapping("/tree")
    public TableDataInfo tree(@RequestBody(required = false) TbLocation tbLocation) {
        startPage();
        List<TbLocation> tbLocations = tbLocationService.selectTree(tbLocation);
        return getDataTable(tbLocations);
    }

    @PreAuthorize("@ss.hasPermi('asset:location:list')")
    @GetMapping("/treeSelect")
    public AjaxResult treeSelect(@Parameter(name = "id") Long id) {
        List<TreeSelect> tbLocations = tbLocationService.selectTreeSelect(id);
        return success(tbLocations);
    }

    /**
     * 导出所属位置列表
     */
    @PreAuthorize("@ss.hasPermi('asset:location:export')")
    @Log(title = "所属位置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbLocation tbLocation)
    {
        List<TbLocation> list = tbLocationService.selectTbLocationList(tbLocation);
        ExcelUtil<TbLocation> util = new ExcelUtil<TbLocation>(TbLocation.class);
        util.exportExcel(response, list, "所属位置数据");
    }

    /**
     * 获取所属位置详细信息
     */
    @PreAuthorize("@ss.hasPermi('asset:location:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbLocationService.selectTbLocationById(id));
    }

    /**
     * 新增所属位置
     */
    @PreAuthorize("@ss.hasPermi('asset:location:add')")
    @Log(title = "所属位置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbLocation tbLocation)
    {
        return toAjax(tbLocationService.insertTbLocation(tbLocation));
    }

    /**
     * 修改所属位置
     */
    @PreAuthorize("@ss.hasPermi('asset:location:edit')")
    @Log(title = "所属位置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbLocation tbLocation)
    {
        return toAjax(tbLocationService.updateTbLocation(tbLocation));
    }

    /**
     * 删除所属位置
     */
    @PreAuthorize("@ss.hasPermi('asset:location:remove')")
    @Log(title = "所属位置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbLocationService.deleteTbLocationByIds(ids));
    }

    /**
     * 查看位置二维码
     */
    @GetMapping(value = "/code/{number}", produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] selectQrCode(@PathVariable("number") String locationNumber) {
        return tbLocationService.selectQrCodeByLocationNumber(locationNumber);
    }

    /**
     * 下载指定二维码
     */
    @PostMapping(value = "/download/code/{ids}")
    public void downloadZip(@PathVariable Long[] ids, HttpServletResponse response) {
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        String sourceName = tbLocationService.downloadQrCode(idList);
        Folder2ZipUtils.zip(sourceName, response);
    }
}
