package com.djhk.uav.airspace.controller;



import com.djhk.common.core.controller.BaseController;
import com.djhk.common.core.domain.AjaxResult;
import com.djhk.common.core.domain.model.LoginUser;
import com.djhk.common.core.page.TableDataInfo;
import com.djhk.common.utils.DateUtils;
import com.djhk.common.utils.SecurityUtils;
import com.djhk.uav.airspace.domain.AirplaneApply;
import com.djhk.uav.airspace.service.AirplaneService;
import com.djhk.uav.airspace.service.AirspaceWaypointService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

import static com.djhk.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/airplane")
@Slf4j
public class AirplaneController extends BaseController {
    @Resource
    private AirplaneService airplaneService;
//    @Resource
//    private RemoteUserService userService;
    @Resource
    private AirspaceWaypointService airplaneWaypointService;

    /**
     * 查询飞行申请
     */
    @GetMapping("/list")
    public TableDataInfo getList(AirplaneApply airplaneApply)
    {
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (airplaneApply.getDeptId() == null) {
            airplaneApply.setDeptId(loginUser.getDeptId());
        }
        startPage();
        List<AirplaneApply> list = airplaneService.selectAirplaneApplyList(airplaneApply);
        return getDataTable(list);
    }


    /**
     * 查询详情
     */
    @GetMapping(value = "/{airplaneApplyId}")
    public AjaxResult selectAirplaneApplyById(@PathVariable Long airplaneApplyId){
        AirplaneApply airplaneApply = airplaneService.selectAirplaneApplyById(airplaneApplyId);
        return AjaxResult.success(airplaneApply);
    }

//    @PostMapping("/export")
//    public void export(HttpServletResponse response, AirplaneApply airplaneApply)
//    {
////        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
////        Long deptId = sysUser.getDeptId();
////        airplaneApply.setDept(deptId);
////        List<UavOperator> list = uavOperatorService.selectUavOperatorList(uavOperator);
////        ExcelUtil<UavOperator> util = new ExcelUtil<UavOperator>(UavOperator.class);
////        util.exportExcel(response, list, "申请空域数据");
//
//        List<AirplaneApplyExport> list = airplaneService.exportAirplaneApplyList(airplaneApply);
//        ExcelUtil<AirplaneApplyExport> util = new ExcelUtil<AirplaneApplyExport>(AirplaneApplyExport.class);
//        util.exportExcel(response, list, "申请空域数据");
//    }


    /**
     * 导出
     * @param response
     * @param
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, Long id) throws Exception {
//        List<AirplaneApplyExport> list = airplaneService.exportAirplaneApplyList(airplaneApply);
//        ExcelUtil<AirplaneApplyExport> util = new ExcelUtil<AirplaneApplyExport>(AirplaneApplyExport.class);
//        util.exportExcel(response, list, "申请空域数据")

        airplaneService.exportAirplaneApply(response,id);
    }


    /**
     * 新增
     */
    @PostMapping
    public AjaxResult add(@RequestBody AirplaneApply airplaneApply){
        logger.info("airplaneApply数据为："+airplaneApply.toString());
        Date nowDate = DateUtils.getNowDate();
        airplaneApply.setCreateTime(nowDate);
        LoginUser securityUser = SecurityUtils.getLoginUser();
        airplaneApply.setCreateBy(SecurityUtils.getUsername());
        airplaneApply.setDeptId(securityUser.getDeptId());
        if (airplaneApply.getSts()==null){
            airplaneApply.setSts("0");
        }
        airplaneService.insertAirplaneApply(airplaneApply);
        return AjaxResult.success();
    }

    /**
     * 修改
     */
    @PutMapping
    public  AjaxResult edit(@RequestBody AirplaneApply airplaneApply){
        Date nowDate = DateUtils.getNowDate();
        airplaneApply.setUpdateTime(nowDate);
        airplaneApply.setUpdateBy(SecurityUtils.getUsername());
        LoginUser securityUser = SecurityUtils.getLoginUser();
        if ( (SecurityUtils.getUsername() == airplaneApply.getCreateBy())&& (airplaneApply.getSts().equals("5") || airplaneApply.getSts().equals("4") )){
            return AjaxResult.success(airplaneService.updateAirplaneApply(airplaneApply));
        }else if (securityUser.getDeptId() == 1L){
            return AjaxResult.success(airplaneService.updateAirplaneApply(airplaneApply));
        }else {
            return AjaxResult.error("该状态下禁止修改！");
        }
    }

    /**
     * 删除
     */
    @DeleteMapping(value = "/{airplaneApplyIds}")
    public  AjaxResult delete(@PathVariable Long[] airplaneApplyIds){
        return AjaxResult.success(airplaneService.deleteAirplaneApplyByIds(airplaneApplyIds));
    }
    
}
