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.SGConSubstationBind;
import com.njcn.relational.service.impl.SGConSubstationCServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

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

    private final SGConSubstationCServiceImpl substationCService;

    /**
     * 新增多系统映射记录
     */
    @PostMapping("/add")
    public Boolean add(@RequestBody SGConSubstationBind entity) {
        // 新增默认未删除
        entity.setIsDelete(0);
        return substationCService.save(entity);
    }

    /**
     * 根据联合主键查询
     */
    @GetMapping("/get")
    public SGConSubstationBind get(
            @RequestParam String dccId,
            @RequestParam String dcloudId
    ) {
        return substationCService.getByUnionKey(dccId, dcloudId);
    }

    /**
     * 修改映射记录
     */
    @PutMapping("/update")
    public Boolean update(@RequestBody SGConSubstationBind entity) {
        return substationCService.updateById(entity);
    }

    /**
     * 逻辑删除（软删）
     */
    @DeleteMapping("/logicRemove")
    public Boolean logicRemove(
            @RequestParam String dccId,
            @RequestParam String dcloudId
    ) {
        return substationCService.logicRemoveByUnionKey(dccId, dcloudId);
    }

    /**
     * 分页查询映射关系
     */
    @GetMapping("/page")
    public IPage<SGConSubstationBind> page(
            @RequestParam(defaultValue = "1") long pageNum,
            @RequestParam(defaultValue = "10") long pageSize,
            SGConSubstationBind query
    ) {
        Page<SGConSubstationBind> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SGConSubstationBind> wrapper = new LambdaQueryWrapper<>();
        // 默认只查正常未删除数据
        wrapper.eq(SGConSubstationBind::getIsDelete, 0);

        wrapper.eq(query.getDccId() != null, SGConSubstationBind::getDccId, query.getDccId());
        wrapper.eq(query.getDcloudId() != null, SGConSubstationBind::getDcloudId, query.getDcloudId());
        wrapper.like(query.getDcloudName() != null, SGConSubstationBind::getDcloudName, query.getDcloudName());
        wrapper.eq(query.getDcloudVoltagelevel() != null, SGConSubstationBind::getDcloudVoltagelevel, query.getDcloudVoltagelevel());
        wrapper.eq(query.getD5000Id() != null, SGConSubstationBind::getD5000Id, query.getD5000Id());
        wrapper.eq(query.getOmsId() != null, SGConSubstationBind::getOmsId, query.getOmsId());
        wrapper.eq(query.getStatus() != null, SGConSubstationBind::getStatus, query.getStatus());

        return substationCService.page(page, wrapper);
    }

    /**
     * 不分页列表
     */
    @GetMapping("/list")
    public List<SGConSubstationBind> list(SGConSubstationBind query) {
        LambdaQueryWrapper<SGConSubstationBind> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SGConSubstationBind::getIsDelete, 0);
        wrapper.eq(query.getDccId() != null, SGConSubstationBind::getDccId, query.getDccId());
        return substationCService.list(wrapper);
    }
}