package com.djhk.uav.label.controller;


import com.djhk.common.core.controller.BaseController;
import com.djhk.common.core.domain.R;
import com.djhk.common.core.page.TableDataInfo;
import com.djhk.common.utils.DateUtils;
import com.djhk.common.utils.SecurityUtils;
import com.djhk.system.service.ISysUserService;
import com.djhk.uav.label.domain.CatalogueDto;
import com.djhk.uav.label.domain.FolderInfo;
import com.djhk.uav.label.domain.Plotting;
import com.djhk.uav.label.domain.PlottingQuery;
import com.djhk.uav.label.service.LabelService;
import com.djhk.uav.label.service.impl.AppFolderInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author jwh
 * @version 1.0
 * @description
 * @date 2023/7/26 11:03:55
 */
@RestController
@RequestMapping("/plot")
public class LabelController extends BaseController {

    @Autowired
    private LabelService labelService;
    @Autowired
    private AppFolderInfoServiceImpl appFolderInfoService;
//    @Resource
//    private RemoteUserService userService;
    @Autowired
    private ISysUserService userService;


    /**
     * 导出kml文件
     *
     *
     * @return
     */
    @GetMapping(value = "/downloadPlot")
    public R listCatalogue(@RequestParam List folderIds) {
        return labelService.downloadPlot(folderIds);
    }

    @PostMapping("/importPlot")
    public R uploadFile(@RequestParam("file") MultipartFile file){
        return labelService.uploadPlot(file);
    }

    /**
     * 添加标注目录
     *
     * @param folderInfo
     * @return
     */
    @PostMapping("/addDir")
    public R addCatalogue(@RequestBody FolderInfo folderInfo) {
        //web端目录
        folderInfo.setSource(1);
        folderInfo.setInit(0);
        int i = appFolderInfoService.insertFolderInfo(folderInfo);
        if (i > 0) {
            return R.ok(folderInfo.getId());
        }
        return R.fail("新增失败");
//        return labelService.addCatalogue(catalogueDto);
    }

    /**
     * 删除目录
     *
     * @param ids
     * @return
     */
    @GetMapping("/{ids}")
    public R delete(@PathVariable Integer[] ids) {
        return labelService.deleteCatalogue(ids);
    }

    /**
     * 修改目录名称
     *
     * @param folderInfo
     * @return
     */
    @PostMapping("/updateDir")
    public R updateCatalogue(@RequestBody FolderInfo folderInfo) {

        //web端目录
        folderInfo.setSource(1);
        folderInfo.setInit(0);
        return appFolderInfoService.updateFolderInfo(folderInfo);
//        return labelService.updateCatalogue(catalogueDto);
    }

    /**
     * 查询目录列表--不分页
     *
     * @param folderInfo
     * @return
     */
    @GetMapping("/listdir")
    public R listCatalogue(FolderInfo folderInfo) {
        List<FolderInfo> list = new ArrayList<>();
//        if (
//                StringUtils.isNotEmpty(folderInfo.getAppId())
//                        && folderInfo.getOrgId() != null
//        ){
//            startPage();
//            list = appFolderInfoService.selectFolderInfoList(folderInfo);
//        }
        startPage();
        list = appFolderInfoService.selectFolderInfoList(folderInfo);
        return R.ok(list);
    }

    /**
     * 修改目录锁定状态
     *
     * @param id
     * @param lockStatus
     */
    @GetMapping("/changeLockStatusDir")
    public R changeLockStatus(@RequestParam("id") Integer id, @RequestParam("lockStatus") Integer lockStatus) {
        return labelService.changeLockStatusCatalogue(id, lockStatus);
    }

    /**
     * 修改目录显示/隐藏状态
     *
     * @param id
     * @param showStatus
     * @return
     */
    @GetMapping("/changeShowStatusDir")
    public R changeShowStatus(@RequestParam("id") Integer id, @RequestParam("showStatus") Integer showStatus) {
        return labelService.changeShowStatusCatalogue(id, showStatus);
    }

    /**
     * 修改目录分享状态
     *
     * @param id
     * @param shareStatus
     * @return
     */
    @GetMapping("/changeShareStatusDir")
    public R changeShareStatus(@RequestParam("id") Integer id, @RequestParam("shareStatus") Integer shareStatus) {
        return labelService.changeShareStatusCatalogue(id, shareStatus);
    }


    /**
     * 添加标注目录
     *
     * @param plotting
     * @return
     */
    @PostMapping("/addPlotWeb")
    public R addPlot(@RequestBody Plotting plotting) {
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
//        plotting.setOrgId(sysUser.getDeptId());
//        plotting.setCreateBy(sysUser.getUserName());
        plotting.setOrgId(SecurityUtils.getDeptId());
        plotting.setCreateBy(SecurityUtils.getUsername());
        plotting.setCreateTime(DateUtils.getNowDate());
        plotting.setDataType("GeoJSON");
        plotting.setOrder(0);
        plotting.setDelFlag("0");
        plotting.setName("申请空域");
        plotting.setAppId("fc4bcf44-83d3-45c9-b5be-e5f2d737aef9");
        plotting.setShowStatusWeb(0);
        plotting.setType(1);
        return labelService.addPlotWeb(plotting);
    }

    /**
     * 添加标注目录
     *
     * @param uuid
     * @return
     */
    @GetMapping("/selectPlotByUuid/{uuid}")
    public R selectPlotByUuid(@PathVariable String uuid) {
        return labelService.selectPlotByUuid(uuid);
    }


    /**
     * 删除目录
     *
     * @param ids
     * @return
     */
    @GetMapping("/deletePlot/{ids}")
    public R deletePlot(@PathVariable Integer[] ids) {
        return labelService.deletePlot(ids);
    }

    /**
     * 删除目录下的所有标绘
     *
     * @param ids
     * @return
     */
    @GetMapping("/deleteDirInner/{ids}")
    public R deleteDirInner(@PathVariable Integer[] ids) {
        return labelService.deleteDirInner(ids);
    }

    /**
     * 修改目录名称
     *
     * @param catalogueDto
     * @return
     */
    @PostMapping("/updatePlot")
    public R updatePlot(@RequestBody CatalogueDto catalogueDto) {
        return labelService.updatePlot(catalogueDto);
    }
    /**
     * 修改标绘数据ByUUID
     *
     * @param plotting
     * @return
     */
    @PostMapping("/updatePlotByUUID")
    public R updatePlotByUUID(@RequestBody Plotting plotting) {
        return labelService.updatePlotByUUID(plotting);
    }

    @PostMapping("/updateOrder")
    public R updateOrder(@RequestBody List<HashMap<String,Object>> list){
        R result = labelService.updateOrder(list);
        return result;
    }

    /**
     * 查询目录列表--不分页
     *
     * @param folderId
     * @return
     */
    @GetMapping("/listPlot")
    public TableDataInfo listPlot(@RequestParam(required = false) Integer folderId, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return labelService.listPlot(folderId,pageNum,pageSize);
    }
    /**
     * 查询标绘列表
     *
     * @return
     */
    @GetMapping("/plottings")
    public R plottings() {
        return labelService.listAllPlot();
    }

    /**
     * 修改标绘锁定状态
     *
     * @param id
     * @param lockStatus
     */
    @GetMapping("/changeLockStatusPlot")
    public R changeLockStatusPlot(@RequestParam("id") Integer id, @RequestParam("lockStatus") Integer lockStatus) {
        return labelService.changeLockStatusPlot(id, lockStatus);
    }

    /**
     * 修改标绘显示/隐藏状态
     *
     * @param id
     * @param showStatus
     * @return
     */
    @GetMapping("/changeShowStatusPlot")
    public R changeShowStatusPlot(@RequestParam("id") Integer id, @RequestParam("showStatus") Integer showStatus) {
        return labelService.changeShowStatusPlot(id, showStatus);
    }

    /**
     * 修改标绘分享状态
     *
     * @param id
     * @param shareStatus
     * @return
     */
    @GetMapping("/changeShareStatusPlot")
    public R changeShareStatusPlot(@RequestParam("id") Integer id, @RequestParam("shareStatus") Integer shareStatus) {
        return labelService.changeShareStatusPlot(id, shareStatus);
    }

    @GetMapping("/collection")
    public R plotCollection(@RequestParam Map<String,Object> params) {
        return labelService.plotCollection(params);
    }

    @GetMapping("/infoByUuid")
    public R<Plotting> infoByUuid(@RequestParam("uuid") String uuid) {
        return labelService.infoByUuid(uuid);
    }

    @GetMapping("/deleteByUuid")
    public R deleteByUuid(@RequestParam("uuid") String uuid) {
        return labelService.deleteByUuid(uuid);
    }

    @GetMapping("/count")
    public R count(PlottingQuery query) {
        return labelService.count(query);
    }

}
