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.Line;
import com.njcn.relational.pojo.po.UserReportPO;
import com.njcn.relational.service.LineService;
import com.njcn.relational.service.UserLedgerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/userLedger")
@RequiredArgsConstructor
@Slf4j
public class UserLedgerController {

    private final LineService lineService;
    private final UserLedgerService userLedgerService;

    /**
     * 监测点分页查询 (Line, level=6)
     */
    @GetMapping("/line/page")
    public IPage<Line> linePage(
            @RequestParam(defaultValue = "1") long pageNum,
            @RequestParam(defaultValue = "10") long pageSize,
            Line query
    ) {
        Page<Line> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Line> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Line::getLevel, 6);
        wrapper.eq(Line::getState, 1);
        wrapper.like(query.getName() != null, Line::getName, query.getName());
        wrapper.orderByAsc(Line::getSort);
        return lineService.page(page, wrapper);
    }

    /**
     * 变电站分页查询 (Line, level=3)
     */
    @GetMapping("/station/page")
    public IPage<Line> stationPage(
            @RequestParam(defaultValue = "1") long pageNum,
            @RequestParam(defaultValue = "10") long pageSize,
            Line query
    ) {
        Page<Line> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Line> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Line::getLevel, 3);
        wrapper.eq(Line::getState, 1);
        wrapper.like(query.getName() != null, Line::getName, query.getName());
        wrapper.orderByAsc(Line::getSort);
        return lineService.page(page, wrapper);
    }

    /**
     * 新能源场站分页查询 (UserReportPO)
     */
    @GetMapping("/report/page")
    public IPage<UserReportPO> reportPage(
            @RequestParam(defaultValue = "1") long pageNum,
            @RequestParam(defaultValue = "10") long pageSize,
            UserReportPO query
    ) {
        Page<UserReportPO> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UserReportPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserReportPO::getState, 1);
        wrapper.like(query.getProjectName() != null, UserReportPO::getProjectName, query.getProjectName());
        wrapper.like(query.getSubstation() != null, UserReportPO::getSubstation, query.getSubstation());
        wrapper.like(query.getCity() != null, UserReportPO::getCity, query.getCity());
        wrapper.orderByDesc(UserReportPO::getCreateTime);
        return userLedgerService.page(page, wrapper);
    }

}
