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.gis.entity.TbGisSpeedingThreshold;
import com.gci.boot.modules.gis.service.ITbGisSpeedingThresholdService;
import com.gci.boot.modules.projects.entity.Projects;
import com.gci.boot.modules.projects.service.IProjectsService;
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-12-25
 */
@RestController
@RequestMapping("/gis/speedingThreshold")
public class TbGisSpeedingThresholdController {
    @Autowired
    private ITbGisSpeedingThresholdService iTbGisSpeedingThresholdService;
    @Autowired
    private IProjectsService projectsService;

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

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

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

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

    /**
     * 作废
     */
    @DeleteMapping(value = "/cancel")
    public Result<?> cancel(@RequestParam(name = "id", required = true) String id) {
        iTbGisSpeedingThresholdService.cancel(id);
        return Result.ok("禁用成功！");
    }

    /**
     * 恢复
     */
    @DeleteMapping(value = "/unCancel")
    public Result<?> unCancel(@RequestParam(name = "id", required = true) String id) {
        iTbGisSpeedingThresholdService.unCancel(id);
        return Result.ok("启用成功！");
    }

    /**
     * 批量删除
     */
    // @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        iTbGisSpeedingThresholdService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }
    @GetMapping("/test")
    public Result<?> test() {
        List<Projects> list = projectsService.list();
        for (Projects projects : list) {
            TbGisSpeedingThreshold tbGisSpeedingThreshold = new TbGisSpeedingThreshold();
            tbGisSpeedingThreshold.setThresholdSpeed(50);
            tbGisSpeedingThreshold.setProjectId(projects.getId());
            tbGisSpeedingThreshold.setThresholdName(projects.getProjectName());
            tbGisSpeedingThreshold.setIsValid(1);
            tbGisSpeedingThreshold.setThresholdType("1");
            iTbGisSpeedingThresholdService.save(tbGisSpeedingThreshold);
        }
        return Result.ok();
    }
    @GetMapping("/test2")
    public Result<?> test2() {
        iTbGisSpeedingThresholdService.rel();
        return Result.ok();
    }
}