package com.djhk.uav.label.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.djhk.common.core.domain.R;
import com.djhk.common.core.page.TableDataInfo;
import com.djhk.uav.label.domain.CatalogueDto;
import com.djhk.uav.label.domain.Plotting;
import com.djhk.uav.label.domain.PlottingQuery;
import com.djhk.uav.label.mapper.PlottingMappers;
import com.djhk.uav.label.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
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 14:44:10
 */
@Service
public class LabelServiceImpl implements LabelService {


//    @Resource
//    private LabelClient labelClient;
    @Autowired
    private PlottingMappers plottingMapper;

    @Override
    public R downloadPlot(List folderIds) {
//        return labelClient.downloadPlot(folderIds);
        return R.ok();
    }

    @Override
    public R uploadPlot(MultipartFile file) {
//        return labelClient.uploadFile(file);
        return R.ok();
    }
    @Override
    public R addCatalogue(CatalogueDto addCatalogueDto) {
//        return labelClient.addCatalogue(addCatalogueDto);
        return R.ok();
    }

    @Override
    public R updateCatalogue(CatalogueDto catalogueDto) {
//        return labelClient.updateCatalogue(catalogueDto);
        return R.ok();
    }

    @Override
    public R deleteCatalogue(Integer[] ids) {
//        return labelClient.deleteCatalogue(ids[0]);
        return R.ok();
    }

    @Override
    public R listCatalogue(CatalogueDto catalogueDto) {
//        Object s = labelClient.listCatalogue(catalogueDto);
//        return (R) s;
        return R.ok();
    }


    @Override
    public R changeLockStatusCatalogue(Integer id, Integer lockStatus) {
//        return labelClient.changeLockStatusCatalogue(id, lockStatus);
        return R.ok();
    }

    @Override
    public R changeShowStatusCatalogue(Integer id, Integer showStatus) {
//        int pageNum = 1;
//        int pageSize = 9999;
//        TableDataInfo dataInfo = labelClient.listPlot(id,pageNum,pageSize);
//        if (dataInfo.getRows().size() == 0){
//            return R.fail("当前目录下没有标绘");
//        }
//        List<JSONObject> plotList = (List<JSONObject>) dataInfo.getRows();
//        for (JSONObject plot : plotList) {
//            labelClient.changeShowStatusPlot((Integer) plot.get("id"), showStatus);
//        }
//        return labelClient.changeShowStatusCatalogue(id, showStatus);
        return R.ok();
    }

    @Override
    public R changeShareStatusCatalogue(Integer id, Integer shareStatus) {
//        return labelClient.changeShareStatusCatalogue(id, shareStatus);
        return R.ok();
    }

    @Override
    public R addPlotWeb(Plotting plotting) {
        List<Plotting>  plottingList = new ArrayList<>();
        plottingList.add(plotting);
        plottingMapper.batchInsertPlotting(plottingList);
        return R.ok("新增成功");


//        return labelClient.addPlotWeb(plotting);
    }

    @Override
    public R selectPlotByUuid(String uuid) {
        List<Plotting>  plottingList = new ArrayList<>();
        plottingList.add(plottingMapper.selectPlottingByUUId(uuid));
        return R.ok(plottingList);
    }

    @Override
    public Plotting selectPlotUuid() {
        return plottingMapper.selectPlottingUUId();
    }

    @Override
    public R updatePlot(CatalogueDto catalogueDto) {
//        return labelClient.updatePlot(catalogueDto);
        return R.ok();
    }
    @Override
    public R updatePlotByUUID(Plotting plotting) {
        plottingMapper.updatePlottingByUUId(plotting);
        return R.ok("修改成功");
//        return labelClient.updatePlotByUUID(catalogueDto);
    }

    @Override
    public R updateOrder(List list) {
        Map map = new HashMap();
        //二级目录移动
        if (list.size() == 1){
            JSONObject obj = (JSONObject)JSONObject.toJSON(list.get(0));
            List<Map<String,Object>> result = (List<Map<String,Object>>)obj.get("children");
            List listArr = new ArrayList();
            for (int i = 0; i < result.size(); i++) {
                Map mapArr = new HashMap();
                mapArr.put("id",result.get(i).get("id"));
                mapArr.put("order",i);
                listArr.add(mapArr);
            }
            map.put("id",obj.get("nodeId"));
            map.put("targetFolderId",obj.get("id"));
            map.put("orders",listArr);
//            return labelClient.updateSecOrder(map);
            return R.ok();
        }else {//文件夹移动
            List listArr = new ArrayList();
            JSONObject object = (JSONObject)JSONObject.toJSON(list.get(0));
            for (int i = 0; i < list.size(); i++) {
                JSONObject obj = (JSONObject)JSONObject.toJSON(list.get(i));
                Map mapArr = new HashMap();
                mapArr.put("id",obj.get("id"));
                mapArr.put("order",i);
                listArr.add(mapArr);
            }
            map.put("id",object.get("id"));
            map.put("orders",listArr);
//            return labelClient.updateOrder(map);
            return R.ok();
        }

    }

    @Override
    public R deletePlot(Integer[] ids) {
        System.out.println(ids);
//        return labelClient.deletePlot(ids);
        return R.ok();
    }

    @Override
    public TableDataInfo listPlot(Integer folderId,Integer pageNum,Integer pageSize) {
//        Object s = labelClient.listPlot(folderId,pageNum,pageSize);
//        return (TableDataInfo) s;
        return null;
    }

    @Override
    public R listAllPlot() {
//        Object s = labelClient.listAllPlot();
//        return (R) s;
        return R.ok();
    }

    @Override
    public R changeLockStatusPlot(Integer id, Integer lockStatus) {
//        return labelClient.changeLockStatusPlot(id, lockStatus);
        return R.ok();
    }

    @Override
    public R changeShowStatusPlot(Integer id, Integer showStatus) {
//        return labelClient.changeShowStatusPlot(id, showStatus);
        return R.ok();
    }

    @Override
    public R changeShareStatusPlot(Integer id, Integer shareStatus) {
//        return labelClient.changeShareStatusPlot(id, shareStatus);
        return R.ok();
    }

    @Override
    public R plotCollection(Map params) {
//        return labelClient.plotCollection(params);
        return R.ok();
    }

    @Override
    public R deleteDirInner(Integer[] ids) {
//        return labelClient.deleteDirInner(ids);
        return R.ok();
    }
    @Override
    public R<Plotting> infoByUuid(String uuid) {
//        return labelClient.infoByUuid(uuid);
        return R.ok();
    }

    @Override
    public R deleteByUuid(String uuid) {
//        return labelClient.deleteByUuid(uuid);
        return R.ok();
    }

    @Override
    public R count(PlottingQuery query) {
//        return labelClient.count(query);
        return R.ok();
    }
}
