package com.yfd.platform.modules.basedata.controller;


import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.basedata.domain.SubstationDevice;
import com.yfd.platform.modules.basedata.domain.SubstationModel;
import com.yfd.platform.modules.basedata.domain.SubstationPatroldevice;
import com.yfd.platform.modules.basedata.mapper.SubstationPatroldeviceMapper;
import com.yfd.platform.modules.basedata.service.ISubstationDeviceService;
import com.yfd.platform.modules.basedata.service.ISubstationModelService;
import com.yfd.platform.modules.patroltask.domain.TaskResult;
import com.yfd.platform.modules.patroltask.mapper.TaskResultMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 变电站-三维动态模型 前端控制器
 * </p>
 *
 * @author zhengsl
 * @since 2024-04-05
 */
@RestController
@RequestMapping("/basedata/substation-model")
@Api(value = "SubstationModelController", tags = "变电站关联三维模型")
public class SubstationModelController {
    @Resource
    private ISubstationModelService substationModelService;
    @Resource
    private TaskResultMapper taskResultMapper;
    @Resource
    private SubstationPatroldeviceMapper patroldeviceMapper;

    @GetMapping("/getModelListByType")
    @ApiOperation("根据类型查询模型对象列表")
    public ResponseResult getModelListByType(String stationid,String type) {
        LambdaQueryWrapper<SubstationModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SubstationModel::getStationId,stationid);
        queryWrapper.eq(SubstationModel::getType, type);
        queryWrapper.orderByAsc(SubstationModel::getObjname);
        List<Map<String, Object>> listMaps = substationModelService.listMaps(queryWrapper);
        return ResponseResult.successData(listMaps);
    }


    @PostMapping("/addModelObject")
    @ApiOperation("新增模型对象")
    public ResponseResult addModelObject(@RequestBody SubstationModel substationModel) {

        boolean ok = substationModelService.save(substationModel);
        if (ok) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

    @PostMapping("/updateModelObject")
    @ApiOperation("修改模型对象")
    public ResponseResult updateModelObject(@RequestBody SubstationModel substationModel) {
        boolean ok = false;
        if(StrUtil.isNotEmpty(substationModel.getId())){
            ok=substationModelService.updateById(substationModel);
        }
        if (ok) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

    @PostMapping("/deleteModelObject")
    @ApiOperation("删除模型对象")
    public ResponseResult deleteModelObject(String id) {
        if (StrUtil.isBlank(id)) {
            return ResponseResult.error("模型对象ID为空");
        }
        boolean ok = substationModelService.removeById(id);
        if (ok) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error("删除模型对象");
        }
    }

    @PostMapping("/setModelLine")
    @ApiOperation("设置航线点位坐标")
    public ResponseResult deleteModelObject(String id,String type, String index, String x, String y, String z) {
        if (StrUtil.isBlank(id)) {
            return ResponseResult.error("id为空");
        }
        SubstationModel substationModel = substationModelService.getById(id);
        JSONArray modelArray = new JSONArray();
        String linePoints = substationModel.getLinePoints();
        JSONArray jsonArray = JSONUtil.parseArray(linePoints);
        List<String> split = StrUtil.split(index, ",");
        List<Integer> indexList = split.stream().map(NumberUtil::parseInt).collect(Collectors.toList());
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            if (indexList.contains(i)) {
                if (StrUtil.isNotBlank(x)) {
                    jsonObject.putOpt("x", NumberUtil.parseInt(x));
                }
                if (StrUtil.isNotBlank(y)) {
                    jsonObject.putOpt("y", NumberUtil.parseInt(y));
                }
                if (StrUtil.isNotBlank(z)) {
                    jsonObject.putOpt("z", NumberUtil.parseInt(z));
                }

            }
            modelArray.add(jsonObject);
        }
        JSONArray modelArray1 = new JSONArray();
        String checkPoints = substationModel.getCheckPoints();
        JSONArray jsonArray1 = JSONUtil.parseArray(checkPoints);
        for (int i = 0; i < jsonArray1.size(); i++) {
            JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
            if (indexList.contains(i)) {
                if (StrUtil.isNotBlank(x)) {
                    jsonObject1.putOpt("x", NumberUtil.parseInt(x));
                }
                if (StrUtil.isNotBlank(y)) {
                    jsonObject1.putOpt("y", NumberUtil.parseInt(y));
                }
                if (StrUtil.isNotBlank(z)) {
                    jsonObject1.putOpt("z", NumberUtil.parseInt(z));
                }

            }
            modelArray1.add(jsonObject1);
        }
        LambdaUpdateWrapper<SubstationModel> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SubstationModel::getId, id).set(SubstationModel::getLinePoints, modelArray.toString()).set(SubstationModel::getCheckPoints, modelArray1.toString());
        substationModelService.update(updateWrapper);
        return ResponseResult.success();
    }

    @GetMapping("/getDevicelListByTask")
    @ApiOperation("根据任务查询任务对应的点位信息")
    public ResponseResult getDevicelListByTask(String stationid, String tasktodoid, String robotCode) {
        LambdaQueryWrapper<TaskResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskResult::getTaskTodoId, tasktodoid);
        queryWrapper.select(TaskResult::getDeviceId);
        queryWrapper.orderByAsc(TaskResult::getOrderNum);
        List<TaskResult> results = taskResultMapper.selectList(queryWrapper);
        List<Map<String, Object>> listMaps = new ArrayList<>();
        for (int i = 0; i < results.size(); i++) {
            TaskResult result = results.get(i);
            LambdaQueryWrapper<SubstationModel> modelWrapper = new LambdaQueryWrapper<>();
            modelWrapper.eq(SubstationModel::getStationId, stationid);
            modelWrapper.eq(SubstationModel::getType, "02");
            modelWrapper.like(SubstationModel::getObjinfo,result.getDeviceId());
            List<Map<String, Object>> list = substationModelService.listMaps(modelWrapper);
            if(list.size()>0){
                listMaps.add(list.get(0));
            }
        }

        LambdaQueryWrapper<SubstationPatroldevice> patrolqueryWrapper = new LambdaQueryWrapper<>();
        patrolqueryWrapper.eq(SubstationPatroldevice::getRobotsCode, robotCode);
        patrolqueryWrapper.select(SubstationPatroldevice::getPatroldeviceId);
        List<SubstationPatroldevice> patroldevices=patroldeviceMapper.selectList(patrolqueryWrapper);
        if(patroldevices.size()>0){
            String patroldeviceid=patroldevices.get(0).getPatroldeviceId();
            LambdaQueryWrapper<SubstationModel> modelWrapper1 = new LambdaQueryWrapper<>();
            modelWrapper1.eq(SubstationModel::getStationId,stationid);
            modelWrapper1.in(SubstationModel::getType, "04","06");
            modelWrapper1.like(SubstationModel::getObjinfo,patroldeviceid);
            List<Map<String, Object>> robots = substationModelService.listMaps(modelWrapper1);
            if(robots.size()>0){
                listMaps.add(robots.get(0));
            }
        }
        return ResponseResult.successData(listMaps);
    }
}
