package com.ruoyi.web.v2.v1.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.app.domain.Pigpen;
import com.ruoyi.app.mapper.PigpenMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.v2.v1.entity.JsProduceRecall;
import com.ruoyi.web.v2.v1.entity.JsReport;
import com.ruoyi.web.v2.v1.service.IJsReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * <p>
 * v2.0完工报告 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-06-11
 */
@RestController
@RequestMapping("/js-report")
@Api("2.0完工报告")
public class JsReportController {

    @Autowired
    private IJsReportService reportService;
    @Autowired
    private PigpenMapper pigpenMapper;

    @ApiOperation("完工报告添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody JsReport jsReport) {
        jsReport.setCreateTime(new Date());
        Pigpen pigpen = new Pigpen();
        List<Pigpen> pigpens = pigpenMapper.selectPigpenList(pigpen);
        if (pigpens.size() != 0) {
            for (Pigpen pigpen1 : pigpens) {
                pigpen1.setIsUse(0);
                pigpenMapper.updatePigpen(pigpen1);
            }
        }
        return success(reportService.save(jsReport));
    }

    @ApiOperation("完工报告分页")
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize
            , @RequestParam(value = "startTime", required = false) String startTime
            , @RequestParam(value = "endTime", required = false) String endTime) {
        QueryWrapper<JsReport> queryWrapper = new QueryWrapper<>();
        if ("".equals(startTime) || null == startTime) {

        } else {
            queryWrapper.between("create_time", startTime + " 00:00:00", endTime + " 23:59:59");
        }
        Page<JsReport> page = new Page<>(pageNum, pageSize);
        return success(reportService.page(page,queryWrapper));
    }
}
