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.poi.ExcelUtil;
import com.djhk.system.service.ISysUserService;
import com.djhk.uav.airspace.domain.UavOperator;
import com.djhk.uav.uav.service.IUavOperatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 飞手管理 Controller
 *
 * @author jiachengshuai
 * @date 2022-08-30
 */
@RestController
@RequestMapping("/operator")
public class UavOperatorController extends BaseController
{
    @Autowired
    private IUavOperatorService uavOperatorService;
//    @Autowired
//    private RemoteUserService userService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询飞手管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UavOperator uavOperator)
    {
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();
        if (uavOperator.getDeptId() == null){
            uavOperator.setDeptId(deptId);
        }
        startPage();
        List<UavOperator> list = uavOperatorService.selectUavOperatorList(uavOperator);
        return getDataTable(list);
    }

//    @RequiresPermissions("uav:operator:list")
    @GetMapping("/selectOperatorList")
    public AjaxResult selectOperatorList(UavOperator uavOperator)
    {
        List<UavOperator> list = uavOperatorService.selectUavOperatorList(uavOperator);
        return AjaxResult.success(list);
    }

    /**
     * 导出飞手管理列表
     */
//    @RequiresPermissions("uav:operator:export")
/*    @Log(title = "飞手管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UavOperatorExportVo uavOperatorExportVo)
    {
         SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();
        uavOperatorExportVo.setOrgIds(Arrays.asList(ServletUtils.urlDecode(String.valueOf(deptId)).split(",")).stream().map(x -> Long.parseLong(x)).collect(Collectors.toList()));
        List<UavOperator> list = uavOperatorService.selectUavOperatorExportList(uavOperatorExportVo);
        ExcelUtil<UavOperator> util = new ExcelUtil<UavOperator>(UavOperator.class);
        util.exportExcel(response, list, "飞手数据");
    }*/

    /**
     * 导出飞手管理列表
     */
//    @RequiresPermissions("uav:operator:export")
//    @Log(title = "飞手管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UavOperator uavOperator)
    {
         SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();
        uavOperator.setDeptId(deptId);
        List<UavOperator> list = uavOperatorService.selectUavOperatorList(uavOperator);
        ExcelUtil<UavOperator> util = new ExcelUtil<UavOperator>(UavOperator.class);
        util.exportExcel(response, list, "飞手数据");
    }

    /**
     * 获取飞手管理详细信息
     */
//    @RequiresPermissions("uav:operator:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(uavOperatorService.selectUavOperatorById(id));
    }

    /**
     * 新增飞手管理
     */
//    @RequiresPermissions("uav:operator:add")
//    @Log(title = "飞手管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UavOperator uavOperator)
    {
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        Long deptId = sysUser.getDeptId();
        if(null == uavOperator.getDeptId()){
            uavOperator.setDeptId(deptId);
        }
        int result = uavOperatorService.insertUavOperator(uavOperator);
        if(result == 1){
            return AjaxResult.success();
        }else{
            return AjaxResult.error("新增失败，飞手已在列表中");
        }
    }

    /**
     * 修改飞手管理
     */
//    @RequiresPermissions("uav:operator:edit")
//    @Log(title = "飞手管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UavOperator uavOperator)
    {
        int result = uavOperatorService.updateUavOperator(uavOperator);
        if(result == 1){
            return AjaxResult.success();
        }else{
            return AjaxResult.error("修改失败，飞手已在列表中");
        }
    }

    /**
     * 删除飞手管理
     */
//    @RequiresPermissions("uav:operator:remove")
//    @Log(title = "飞手管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(uavOperatorService.deleteUavOperatorByIds(ids));
    }
}
