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.R;
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.AirspaceApply;
import com.djhk.uav.airspace.service.AirspaceService;
import com.djhk.uav.airspace.service.AirspaceWaypointService;
import com.djhk.uav.label.domain.Plotting;
import com.djhk.uav.label.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/airspace")
public class AirspaceController extends BaseController {
    @Resource
    private AirspaceService airspaceService;
//    @Resource
//    private RemoteUserService userService;
    @Resource
    private AirspaceWaypointService airspaceWaypointService;
    @Autowired
    private LabelService labelService;

    /**
     * 查询空域申请
     */
    @GetMapping("/list")
    public TableDataInfo getList(AirspaceApply airspaceApply)
    {
        startPage();
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
        LoginUser securityUser = SecurityUtils.getLoginUser();
        airspaceApply.setCreateBy(SecurityUtils.getUsername());
        airspaceApply.setDeptId(securityUser.getDeptId());
        List<AirspaceApply> list = airspaceService.selectAirspaceApplyList(airspaceApply);
        return getDataTable(list);
    }


    /**
     * 查询详情
     */
    @GetMapping(value = "/{airspaceApplyId}")
    public AjaxResult selectAirspaceApplyById(@PathVariable Long airspaceApplyId){
        AirspaceApply airspaceApply = airspaceService.selectAirspaceApplyById(airspaceApplyId);
//        List<AirspaceWaypoint> airspaceWaypoints = airspaceWaypointService.selectAirspaceWaypointList(airspaceApplyId);
//        airspaceApply.setAirlineWaypointList(airspaceWaypoints);
        return AjaxResult.success(airspaceApply);
    }


    /**
     * 新增
     */
//    @PostMapping
//    public AjaxResult add(@RequestBody AirspaceApply airspaceApply){
//        logger.info("airspaceApply数据为："+airspaceApply.toString());
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
//        Date nowDate = DateUtils.getNowDate();
//        airspaceApply.setCreateTime(nowDate);
//        airspaceApply.setCreateBy(sysUser.getUserName());
////        airspaceApply.setSysDeptNo(sysUser.getDeptId());
//        airspaceService.insertAirspaceApply(airspaceApply);
//        Long airspaceApplyId = airspaceService.selectAirspaceApplyId();
//        if (airspaceApply.getAirlineWaypointList() !=null && airspaceApply.getAirlineWaypointList().size()>0){
//            for (AirspaceWaypoint airspaceWaypoint : airspaceApply.getAirlineWaypointList()) {
//                airspaceWaypoint.setAirspaceApplyId(airspaceApplyId);
//                airspaceWaypointService.insertAirspaceWaypoint(airspaceWaypoint);
//            }
//        }
//        return AjaxResult.success();
//    }
    @PostMapping
    public AjaxResult add(@RequestBody AirspaceApply airspaceApply){
        logger.info("airspaceApply数据为："+airspaceApply.toString());
        Date nowDate = DateUtils.getNowDate();
        airspaceApply.setCreateTime(nowDate);
        LoginUser securityUser = SecurityUtils.getLoginUser();
        airspaceApply.setCreateBy(SecurityUtils.getUsername());
        airspaceApply.setDeptId(securityUser.getDeptId());
//        airspaceApply.setSysDeptNo(sysUser.getDeptId());
//        airspaceService.insertAirspaceApply(airspaceApply);
//        Long airspaceApplyId = airspaceService.selectAirspaceApplyId();
//        if (airspaceApply.getAirlineWaypointList() !=null && airspaceApply.getAirlineWaypointList().size()>0){
//            for (AirspaceWaypoint airspaceWaypoint : airspaceApply.getAirlineWaypointList()) {
//                airspaceWaypoint.setAirspaceApplyId(airspaceApplyId);
//                airspaceWaypointService.insertAirspaceWaypoint(airspaceWaypoint);
//            }
//        }
        Plotting plotting = labelService.selectPlotUuid();
        airspaceApply.setUuid(plotting.getUuid());
        return AjaxResult.success(airspaceService.insertAirspaceApply(airspaceApply));
    }

    /**
     * 修改
     */
//    @PutMapping
//    public  AjaxResult edit(@RequestBody AirspaceApply airspaceApply){
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
//        Date nowDate = DateUtils.getNowDate();
//        airspaceApply.setUpdateTime(nowDate);
//        airspaceApply.setUpdateBy(sysUser.getUserName());
//        airspaceWaypointService.deleteAirspaceWaypointById(airspaceApply.getId());
//        if (airspaceApply.getAirlineWaypointList().size()>0){
//            for (AirspaceWaypoint airspaceWaypoint : airspaceApply.getAirlineWaypointList()) {
//                airspaceWaypoint.setAirspaceApplyId(airspaceApply.getId());
//                airspaceWaypointService.insertAirspaceWaypoint(airspaceWaypoint);
//            }
//        }
//        return AjaxResult.success(airspaceService.updateAirspaceApply(airspaceApply));
//    }

    @PutMapping
    public  AjaxResult edit(@RequestBody AirspaceApply airspaceApply){
        Date nowDate = DateUtils.getNowDate();
        airspaceApply.setUpdateTime(nowDate);
        airspaceApply.setUpdateBy(SecurityUtils.getUsername());
        Plotting plotting = labelService.selectPlotUuid();
        airspaceApply.setUuid(plotting.getUuid());
        return AjaxResult.success(airspaceService.updateAirspaceApply(airspaceApply));
    }

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