package com.ygtx.gxt.controller;

import java.util.ArrayList;
import java.util.List;

import com.ygtx.common.constant.HttpStatus;
import com.ygtx.common.core.controller.BaseController;
import com.ygtx.common.core.page.PageDomain;
import com.ygtx.common.core.page.TableSupport;
import com.ygtx.gxt.domain.GxtWorkOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ygtx.gxt.domain.GxtMonthRuntime;
import com.ygtx.gxt.service.IGxtMonthRuntimeService;
import com.ygtx.common.core.page.TableDataInfo;

/**
 * 月度场站运行天数工单Controller
 * 
 * @author ruoyi
 * @date 2025-01-16
 */
@RestController
@RequestMapping("/gxt/monthRuntime")
public class GxtMonthRuntimeController extends BaseController
{
    @Autowired
    private IGxtMonthRuntimeService gxtMonthRuntimeService;

    /**
     * 查询月度场站运行天数工单列表
     */
    @PreAuthorize("@ss.hasPermi('gxt:monthRuntime:list')")
    @GetMapping("/list")
    public TableDataInfo list(GxtMonthRuntime gxtMonthRuntime)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<GxtMonthRuntime> list = gxtMonthRuntimeService.selectGxtMonthRuntimeList(gxtMonthRuntime);
        Integer total = list.size();
        List<GxtMonthRuntime> result = new ArrayList<>();
        if (pageNum!=null && pageSize!=null) {
            int from = (pageNum-1) * pageSize;
            int to = pageNum * pageSize;
            if (to>=total) {
                result.addAll(list.subList(from,total));
            }else {
                result.addAll(list.subList(from, to));
            }
        }
        TableDataInfo dataInfo = new TableDataInfo();
        dataInfo.setCode(HttpStatus.SUCCESS);
        dataInfo.setMsg("查询成功");
        dataInfo.setTotal(total);
        dataInfo.setRows(result);
        return dataInfo;
    }

    @PreAuthorize("@ss.hasPermi('gxt:monthRuntime:query')")
    @GetMapping("/getOrders")
    public TableDataInfo getOrders(@RequestParam Integer itemNum,@RequestParam Integer itemSize,@RequestParam Long pid,@RequestParam Integer fromDays,@RequestParam Integer toDays,@RequestParam boolean isStop) {
        List<GxtWorkOrder> list = gxtMonthRuntimeService.getOrders(pid,fromDays,toDays,isStop);
        Integer total = list.size();
        List<GxtWorkOrder> result = new ArrayList<>();
        if (itemNum!=null && itemSize!=null) {
            int from = (itemNum-1) * itemSize;
            int to = itemNum * itemSize;
            if (to>=total) {
                result.addAll(list.subList(from,total));
            }else {
                result.addAll(list.subList(from, to));
            }
        }
        TableDataInfo dataInfo = new TableDataInfo();
        dataInfo.setCode(HttpStatus.SUCCESS);
        dataInfo.setMsg("查询成功");
        dataInfo.setTotal(total);
        dataInfo.setRows(result);
        return dataInfo;
    }
}