package com.ruoyi.web.controller.app;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.app.model.DryLossRatio;
import com.ruoyi.app.service.IDryLossRatioService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 干损比例Controller
 * 
 * @author coede
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/app/ratio")
public class DryLossRatioController extends BaseController
{
    @Autowired
    private IDryLossRatioService dryLossRatioService;

    /**
     * 查询干损比例列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DryLossRatio dryLossRatio)
    {
        if(dryLossRatio.getStartTime() != null){
            dryLossRatio.setStartTime("2025-"+dryLossRatio.getStartTime()+" 00:00:00");
        }
        startPage();
        List<DryLossRatio> list = dryLossRatioService.selectDryLossRatioList(dryLossRatio);
        return getDataTable(list);
    }

    /**
     * 导出干损比例列表
     */
    @PreAuthorize("@ss.hasPermi('app:ratio:export')")
    @Log(title = "干损比例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DryLossRatio dryLossRatio)
    {
        List<DryLossRatio> list = dryLossRatioService.selectDryLossRatioList(dryLossRatio);
        ExcelUtil<DryLossRatio> util = new ExcelUtil<DryLossRatio>(DryLossRatio.class);
        util.exportExcel(response, list, "干损比例数据");
    }

    /**
     * 获取干损比例详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dryLossRatioService.selectDryLossRatioById(id));
    }

    /**
     * 新增干损比例
     */
    @PreAuthorize("@ss.hasPermi('app:ratio:add')")
    @Log(title = "干损比例", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DryLossRatio dryLossRatio)
    {
//       if(!dryLossRatio.getStartTime().before(dryLossRatio.getEndTime())){
//           return error("开始时间必须早于结束时间");
//       }
        ////拼凑成完整的时间字段且年份固定，数据库采用时间字段方便比较
        dryLossRatio.setStartTime("2025-"+dryLossRatio.getStartTime()+" 00:00:00");
        String ids = dryLossRatioService.checkTimeOverLap(dryLossRatio);
        if (ids != null && !ids.isEmpty())
        {
            return error("新增干损比例失败，已存在ID为：‘" + ids + "’列表中存在时间范围重叠的干损比例");
        }
        dryLossRatio.setCreateBy(getUsername());
        return toAjax(dryLossRatioService.insertDryLossRatio(dryLossRatio));
    }

    /**
     * 修改干损比例
     */
    @PreAuthorize("@ss.hasPermi('app:ratio:edit')")
    @Log(title = "干损比例", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DryLossRatio dryLossRatio)
    {
//        if(!dryLossRatio.getStartTime().before(dryLossRatio.getEndTime())){
//            return error("开始时间必须早于结束时间");
//        }
        //拼凑成完整的时间字段且年份固定，数据库采用时间字段方便比较
        dryLossRatio.setStartTime("2025-"+dryLossRatio.getStartTime()+" 00:00:00");
        String ids = dryLossRatioService.checkTimeOverLap(dryLossRatio);
        if (ids != null && !ids.isEmpty())
        {
            return error("修改干损比例，已存在ID为：‘" + ids + "’列表中存在时间范围重叠的干损比例");
        }
        dryLossRatio.setUpdateBy(getUsername());
        return toAjax(dryLossRatioService.updateDryLossRatio(dryLossRatio));
    }

    /**
     * 删除干损比例
     */
    @PreAuthorize("@ss.hasPermi('app:ratio:remove')")
    @Log(title = "干损比例", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dryLossRatioService.deleteDryLossRatioByIds(ids));
    }

    /**
     * 获取满足当前时间的干损比
     */
    @GetMapping(value = "/getFit")
    public AjaxResult getFit()
    {
        return success(dryLossRatioService.selectDryLossRatioNow());
    }

}
