package com.njcn.relational.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.njcn.relational.pojo.po.SGConSubstationB;
import com.njcn.relational.service.SGConSubstationBService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/substation")
@RequiredArgsConstructor
public class SGConSubstationBController {

    private final SGConSubstationBService substationBService;

    /**
     * 新增变电站
     */
    @PostMapping("/add")
    public Boolean add(@RequestBody SGConSubstationB entity) {
        return substationBService.save(entity);
    }

    /**
     * 根据ID删除
     */
    @DeleteMapping("/remove/{id}")
    public Boolean remove(@PathVariable String id) {
        return substationBService.removeById(id);
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/batch")
    public Boolean batchRemove(@RequestBody List<String> idList) {
        return substationBService.removeByIds(idList);
    }

    /**
     * 修改变电站
     */
    @PutMapping("/update")
    public Boolean update(@RequestBody SGConSubstationB entity) {
        return substationBService.updateById(entity);
    }

    /**
     * 根据ID单条查询
     */
    @GetMapping("/get/{id}")
    public SGConSubstationB getById(@PathVariable String id) {
        return substationBService.getById(id);
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public IPage<SGConSubstationB> page(
            @RequestParam(defaultValue = "1") long pageNum,
            @RequestParam(defaultValue = "10") long pageSize,
            SGConSubstationB query
    ) {
        Page<SGConSubstationB> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SGConSubstationB> wrapper = new LambdaQueryWrapper<>();

        // 常用查询条件示例
        wrapper.like(query.getName() != null, SGConSubstationB::getName, query.getName());
        wrapper.eq(query.getDccId() != null, SGConSubstationB::getDccId, query.getDccId());
        wrapper.eq(query.getPgId() != null, SGConSubstationB::getPgId, query.getPgId());
        wrapper.eq(query.getTopAcVoltageType() != null, SGConSubstationB::getTopAcVoltageType, query.getTopAcVoltageType());
        wrapper.eq(query.getOperateState() != null, SGConSubstationB::getOperateState, query.getOperateState());

        return substationBService.page(page, wrapper);
    }

    /**
     * 不分页全量列表
     */
    @GetMapping("/list")
    public List<SGConSubstationB> list(SGConSubstationB query) {
        LambdaQueryWrapper<SGConSubstationB> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(query.getName() != null, SGConSubstationB::getName, query.getName());
        wrapper.eq(query.getDccId() != null, SGConSubstationB::getDccId, query.getDccId());
        return substationBService.list(wrapper);
    }
}