package com.jdc.jdcproject.controller;

import com.jdc.jdcproject.entity.DicekeMiningloss;
import com.jdc.jdcproject.service.IDicekeMininglossService;
import com.jdc.jdcproject.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.jdc.jdcproject.entity.VO.DicekeMininglossVo;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 采剥与贫损表; 前端控制器
 * </p>
 *
 * @author haoyanlu
 * @since 2025-04-26
 */
@RestController
@RequestMapping("/dicekeMiningloss")
public class DicekeMininglossController {

    @Autowired
    private IDicekeMininglossService dicekeMininglossService;

    @Operation(summary = "查询损失和贫化报表")
    @GetMapping("findAllMininggloss")
    public Result findAllLoss(){
        List<DicekeMininglossVo> dicekeMininglossVoList = dicekeMininglossService.findAllLoss();
        System.out.println(dicekeMininglossVoList.toString());
        return Result.successResult().data("dicekeMininglossVoList", dicekeMininglossVoList);
    }


    @Operation(summary = "通过Month查询损失和贫化报表")
    @GetMapping("findLossByMonth")
    public Result findLossByMonth(@RequestParam(required = false) LocalDate Month,
                                  @RequestParam(required = false) LocalDate endMonth) {

        if(Month != null && endMonth != null){  //两个时间段内查询
            List<DicekeMininglossVo> LossByMonthRangeVoList = dicekeMininglossService.getLossByMonthRange(Month,endMonth);
            System.out.println(LossByMonthRangeVoList.toString());
            return Result.successResult().data("LossMonthRangeList", LossByMonthRangeVoList);
        } else if (Month != null){  //Month存在 通过获取year年和month月查询此月报表
            List<DicekeMininglossVo> LossByMonthVoList = dicekeMininglossService.getLossByMonth(Month);
            System.out.println(LossByMonthVoList.toString());
            return Result.successResult().data("LossMonthVoList", LossByMonthVoList);
        } else {
            return null;
        }
    }

    @Operation(summary = "修改损失和贫化信息")
    @PostMapping("updatedicekeMiningloss")
    public Result updateLoss(@RequestBody DicekeMiningloss dicekeMiningloss){
        boolean updateflag = dicekeMininglossService.updateById(dicekeMiningloss);
        if (updateflag){
            return Result.successResult();
        } else {
            return Result.errorResult();
        }
    }

    @Operation(summary = "删除损失和贫化信息")
    @DeleteMapping("deletedicekeMiningloss/{lossID}")
    public Result deleteLoss(@PathVariable String lossID){
        boolean updateflag = dicekeMininglossService.removeById(lossID);
        if(updateflag){
            return Result.successResult();
        } else {
            return Result.errorResult();
        }
    }

    @Operation(summary = "根据id查询损失和贫化信息")
    @GetMapping("finddicekeMininglossById/{lossID}")
    public Result findLossByID(@PathVariable String lossID){
        DicekeMiningloss lossById = dicekeMininglossService.getById(lossID);
        return Result.successResult().data("dicekeMininglossServiceById",lossById);
    }

    @Operation(summary = "新增损失和贫化信息")
    @PostMapping("savedicekeMiningloss")
    public Result saveLoss(@RequestBody DicekeMininglossVo dicekeMininglossVo){

        int savedml = dicekeMininglossService.savedml(dicekeMininglossVo);

        if(savedml == 1) {
            return Result.errorResult().data("平盘不存在",savedml);
        } else if (savedml == 2) {
            return Result.errorResult().data("电铲不存在",savedml);
        } else {
            return Result.successResult();
        }

    }

}