package com.djhk.uav.uav.controller;


import com.djhk.common.annotation.Log;
import com.djhk.common.core.controller.BaseController;
import com.djhk.common.core.domain.AjaxResult;
import com.djhk.common.core.domain.entity.SysUser;
import com.djhk.common.core.page.TableDataInfo;
import com.djhk.common.enums.BusinessType;
import com.djhk.common.utils.SecurityUtils;
import com.djhk.common.utils.ServletUtils;
import com.djhk.common.utils.poi.ExcelUtil;
import com.djhk.system.service.ISysUserService;
import com.djhk.uav.uav.domain.ThreedModelInfo;
import com.djhk.uav.uav.domain.ThreedModelInfoVo;
import com.djhk.uav.uav.service.IThreedModelInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 模型Controller
 * 
 * @author ruoyi
 * @date 2023-07-27
 */
@RestController
@RequestMapping("/model")
public class ThreedModelInfoController extends BaseController
{
    @Autowired
    private IThreedModelInfoService threedModelInfoService;
//    @Autowired
//    private RemoteUserService userService;
    @Autowired
    private ISysUserService userService;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 查询模型列表
     */
    // @RequiresPermissions("uav:model:list")
    @GetMapping("/list")
    public TableDataInfo list(ThreedModelInfoVo threedModelInfoVo)
    {
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();
        threedModelInfoVo.setOrgIds(Arrays.asList(ServletUtils.urlDecode(String.valueOf(deptId)).split(",")).stream().map(x -> Long.parseLong(x)).collect(Collectors.toList()));
        startPage();
        List<ThreedModelInfo> list = threedModelInfoService.selectThreedModelInfoList(threedModelInfoVo);
        return getDataTable(list);
    }

    /**
     * 导出模型列表
     */
    // @RequiresPermissions("uav:model:export")
    @Log(title = "模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ThreedModelInfoVo threedModelInfoVo)
    {
        List<ThreedModelInfo> list = threedModelInfoService.selectThreedModelInfoList(threedModelInfoVo);
        ExcelUtil<ThreedModelInfo> util = new ExcelUtil<ThreedModelInfo>(ThreedModelInfo.class);
        util.exportExcel(response, list, "模型数据");
    }

    /**
     * 获取模型详细信息
     */
    //   @RequiresPermissions("uav:model:query")
    @GetMapping(value = "/{modelId}")
    public AjaxResult getInfo(@PathVariable("modelId") Long modelId)
    {
        return AjaxResult.success(threedModelInfoService.selectThreedModelInfoByModelId(modelId));
    }

    /**
     * 新增模型
     */
    // @RequiresPermissions("uav:model:add")
    @Log(title = "模型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ThreedModelInfo threedModelInfo)
    {
         SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();
        threedModelInfo.setDeptId(deptId);
        return toAjax(threedModelInfoService.insertThreedModelInfo(threedModelInfo));
    }

    /**
     * 修改模型
     */
    //@RequiresPermissions("uav:model:edit")
    @Log(title = "模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ThreedModelInfo threedModelInfo)
    {
        return toAjax(threedModelInfoService.updateThreedModelInfo(threedModelInfo));
    }

    /**
     * 删除模型
     */
    //@RequiresPermissions("uav:model:remove")
    @Log(title = "模型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{modelIds}")
    public AjaxResult remove(@PathVariable Long[] modelIds)
    {
        return toAjax(threedModelInfoService.deleteThreedModelInfoByModelIds(modelIds));
    }

    /**
     * 新增模型
     */
    // @RequiresPermissions("uav:model:add")
    @Log(title = "模型", businessType = BusinessType.INSERT)
    @PostMapping("/setUploadFileId")
    public AjaxResult add(@RequestParam("modelId") Long modelId, @RequestParam("sysFileId") Long sysFileId)
    {
        ThreedModelInfo threedModelInfo=new ThreedModelInfo();
        threedModelInfo.setModelId(modelId);
        threedModelInfo.setModelSts("0");
        threedModelInfo.setDealSts("2");
        threedModelInfo.setModelFile(sysFileId);
        return toAjax(threedModelInfoService.updateThreedModelInfo(threedModelInfo));
    }

    /**
     * 部署模型
     */
    // @RequiresPermissions("uav:model:add")
    @Log(title = "模型", businessType = BusinessType.INSERT)
    @PostMapping("/deployUpload")
    public AjaxResult deployUpload(@RequestParam("modelId") Long modelId, @RequestParam("path") String path)
    {
        ThreedModelInfo threedModelInfo=new ThreedModelInfo();
        threedModelInfo.setModelId(modelId);
        threedModelInfo.setModelSts("1");
        threedModelInfo.setDealSts("3");
        threedModelInfo.setPath(path);
        return toAjax(threedModelInfoService.updateThreedModelInfo(threedModelInfo));
    }

    /**
     * 模型部署中状态
     */
    // @RequiresPermissions("uav:model:add")
    @Log(title = "模型", businessType = BusinessType.INSERT)
    @PostMapping("/deployUploading")
    public AjaxResult deployUploading(@RequestParam("modelId") Long modelId,@RequestParam("path") String dealSts)
    {
        ThreedModelInfo threedModelInfo=new ThreedModelInfo();
        threedModelInfo.setModelId(modelId);
        threedModelInfo.setModelSts("1");
        threedModelInfo.setDealSts(dealSts);
        return toAjax(threedModelInfoService.updateThreedModelInfo(threedModelInfo));
    }

    /**
     * 查询可展示所有模型列表
     */
    // @RequiresPermissions("uav:model:list")
    @GetMapping("/all")
    public AjaxResult allList()
    {
        List<ThreedModelInfo> list = threedModelInfoService.selectThreedModelInfoAllList();
        return AjaxResult.success(list);
    }
}
