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.page.TableDataInfo;
import com.djhk.common.enums.BusinessType;
import com.djhk.common.exception.ServiceException;
import com.djhk.common.utils.poi.ExcelUtil;
import com.djhk.system.service.ISysUserService;
import com.djhk.uav.uav.model.UavPilot;
import com.djhk.uav.uav.service.IUavPilotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 无人机基础数据比对模块 Controller
 *
 * @author jiachengshuai
 * @date 2022-08-30
 */
@RestController
@RequestMapping("/pilot")
public class UavPilotController extends BaseController
{
    @Autowired
    private IUavPilotService uavPilotService;
//    @Autowired
//    private RemoteUserService userService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询飞手管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UavPilot uavPilot)
    {
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
//        Long deptId = sysUser.getDeptId();
//        if (uavPilot.getOrgId() == null){
//            uavPilot.setOrgId(deptId);
//        }
        startPage();
        List<UavPilot> list = uavPilotService.selectUavPilotList(uavPilot);
        return getDataTable(list);
    }

//    @RequiresPermissions("uav:Pilot:list")
    @GetMapping("/selectPilotList")
    public AjaxResult selectPilotList(UavPilot uavPilot)
    {
        List<UavPilot> list = uavPilotService.selectUavPilotList(uavPilot);
        return AjaxResult.success(list);
    }

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

    /**
     * 导出飞手管理列表
     */
//    @RequiresPermissions("uav:Pilot:export")
    @Log(title = "飞手管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UavPilot uavPilot)
    {
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
//        Long deptId = sysUser.getDeptId();
//        uavPilot.setOrgId(deptId);
        List<UavPilot> list = uavPilotService.selectUavPilotList(uavPilot);
        ExcelUtil<UavPilot> util = new ExcelUtil<UavPilot>(UavPilot.class);
        util.exportExcel(response, list, "飞手信息");
    }

    @Log(title = "飞手管理导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        try {
            ExcelUtil<UavPilot> util = new ExcelUtil<UavPilot>(UavPilot.class);
            List<UavPilot> userList = util.importExcel(file.getInputStream());
            String message = uavPilotService.importData(userList, updateSupport);
            return success(message);
        } catch (ServiceException e) {
            return new AjaxResult(e.getCode(),e.getMessage());
        }
    }


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

    /**
     * 新增飞手管理
     */
//    @RequiresPermissions("uav:Pilot:add")
    @Log(title = "飞手管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UavPilot uavPilot)
    {
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
//        Long deptId = sysUser.getDeptId();
//        if(null == uavPilot.getOrgId()){
//            uavPilot.setOrgId(deptId);
//        }
        int result = uavPilotService.insertUavPilot(uavPilot);
        if(result == 1){
            return AjaxResult.success();
        }else{
            return AjaxResult.error("新增失败，飞手已在列表中");
        }
    }

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

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