package com.gci.boot.modules.gis.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.annotation.Query.handle.QueryGenerator;
import com.gci.boot.annotation.permission.DataPermission;
import com.gci.boot.common.validated.SaveValidated;
import com.gci.boot.common.validated.UpdateValidated;
import com.gci.boot.common.vo.Result;
import com.gci.boot.modules.basedata.entity.TbVehicle;
import com.gci.boot.modules.gis.entity.TbGisFenceInfo;
import com.gci.boot.modules.gis.entity.TbGisFencePlan;
import com.gci.boot.modules.gis.service.ITbGisFenceInfoService;
import com.gci.boot.modules.gis.service.ITbGisFencePlanService;
import com.gci.boot.modules.gis.service.ITbGisFenceVehicleService;
import com.gci.boot.modules.gis.vo.GisProjectTreeVo;
import com.gci.boot.modules.gis.vo.GisVehicleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 围栏方案表 前端控制器
 * </p>
 *
 * @author 2464
 * @since 2023-08-30
 */
@RestController
@RequestMapping("/gps/gisFencePlan")
public class TbGisFencePlanController {
    @Autowired
    private ITbGisFencePlanService tbGisFencePlanService;
    @Autowired
    private ITbGisFenceVehicleService gisFenceVehicleService;
    @Autowired
    private ITbGisFenceInfoService gisFenceInfoService;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    @DataPermission(item = "tb_gis_fence_plan.projectId",sqlIds = {"com.gci.boot.modules.gis.mapper.TbGisFencePlanMapper.selectPage"})
    public Result<?> queryPageList(TbGisFencePlan tbGisFencePlan,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        Page<TbGisFencePlan> page = new Page<TbGisFencePlan>(pageNo, pageSize);
        LambdaQueryWrapper<TbGisFencePlan> lambdaQueryWrapper = QueryGenerator.initQueryWrapper(TbGisFencePlan.class, tbGisFencePlan);
        IPage<TbGisFencePlan> pageList = tbGisFencePlanService.page(page, lambdaQueryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Validated({SaveValidated.class}) TbGisFencePlan tbGisFencePlan) {
        tbGisFencePlanService.saveItem(tbGisFencePlan);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     */
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody @Validated({UpdateValidated.class})  TbGisFencePlan tbGisFencePlan) {
        tbGisFencePlanService.updateItem(tbGisFencePlan);
        return Result.ok("编辑成功！");
    }

    /**
     * 启用
     */
    @GetMapping(value = "/isValid")
    public Result<?> isValid(@RequestParam("id") String id) {
        tbGisFencePlanService.isValid(id);
        return Result.ok("启用成功！");
    }

    /**
     * 禁用
     */
    @GetMapping(value = "/unValid")
    public Result<?> unValid(@RequestParam("id") String id) {
        tbGisFencePlanService.unValid(id);
        return Result.ok("禁用成功！");
    }

    /**
     * 添加车辆
     */
    @PostMapping(value = "/addVehicle")
    public Result<?> addVehicle(@RequestBody GisVehicleVo vo) {
        tbGisFencePlanService.saveVehicle(vo);
        return Result.ok("关联成功！");
    }


    /**
     * 解绑车辆
     */
    @PostMapping(value = "/delVehicle")
    public Result<?> delVehicle(@RequestBody GisVehicleVo vo) {
        tbGisFencePlanService.delVehicle(vo);
        return Result.ok("取消关联成功！");
    }

    /**
     * 获取已绑定车辆
     */
    @GetMapping("/getPlanVehicleList")
    public Result<?>  getPlanVehicleList (@RequestParam("id") String id,
                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<TbVehicle> page = new Page<>(pageNo,pageSize);
        Page<TbVehicle> list = tbGisFencePlanService.getPlanVehicleList(id, page);
        return Result.ok(list);
    }

    /**
     * 获取项目内车辆
     */
    @GetMapping("/getProjectVehicleList")
    public Result<?>  getProjectVehicleList (@RequestParam("id") String id) {
        List<GisProjectTreeVo> list = tbGisFencePlanService.getProjectVehicleList(id);
        return Result.ok(list);
    }

    /**
     * 获取方案内围栏信息列表
     */
    @GetMapping("/getPlanFenceInfoList")
    public Result<?>  getPlanFenceInfoList (@RequestParam("id") String id) {
        List<TbGisFenceInfo> list = tbGisFencePlanService.getPlanFenceInfoList(id);
        return Result.ok(list);
    }

    /**
     * 手动更新所有围栏信息
     */
    @GetMapping("/updateAll")
    public Result<?> updateAll () {
        tbGisFencePlanService.updateAll();
        return Result.ok();
    }

    /**
     * 删除
     */
//    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        tbGisFencePlanService.removeById(id);
        return Result.ok("删除成功！");
    }

    /**
     * 批量删除
     */
//    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        tbGisFencePlanService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }
}
