package com.zhyc.module.feed.controller;

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

import com.zhyc.module.feed.domain.SgFeedPlan;
import org.springframework.security.access.prepost.PreAuthorize;
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.zhyc.common.annotation.Log;
import com.zhyc.common.core.controller.BaseController;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.common.enums.BusinessType;
import com.zhyc.module.feed.service.ISgFeedPlanService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;

/**
 * 饲喂计划Controller
 *
 * @author HashMap
 * @date 2025-08-14
 */
@RestController
@RequestMapping("/feed/FeedPlan")
public class SgFeedPlanController extends BaseController {
    private final ISgFeedPlanService sgFeedPlanService;

    public SgFeedPlanController(ISgFeedPlanService sgFeedPlanService) {
        this.sgFeedPlanService = sgFeedPlanService;
    }

    /**
     * 查询饲喂计划列表
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedPlan:list')")
    @GetMapping("/list")
    public TableDataInfo list(SgFeedPlan sgFeedPlan) {
        startPage();
        List<SgFeedPlan> list = sgFeedPlanService.selectSgFeedPlanList(sgFeedPlan);
        return getDataTable(list);
    }

    /**
     * 导出饲喂计划列表
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedPlan:export')")
    @Log(title = "饲喂计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SgFeedPlan sgFeedPlan) {
        List<SgFeedPlan> list = sgFeedPlanService.selectSgFeedPlanList(sgFeedPlan);
        ExcelUtil<SgFeedPlan> util = new ExcelUtil<>(SgFeedPlan.class);
        util.exportExcel(response, list, "饲喂计划数据");
    }

    /**
     * 获取饲喂计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedPlan:query')")
    @GetMapping(value = "/{createDate}")
    public AjaxResult getInfo(@PathVariable("createDate") Date createDate) {
        return success(sgFeedPlanService.selectSgFeedPlanByCreateDate(createDate));
    }

    /**
     * 新增饲喂计划
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedPlan:add')")
    @Log(title = "饲喂计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SgFeedPlan sgFeedPlan) {
        if (null == sgFeedPlan) {
            throw new RuntimeException("数据为空");
        }
        sgFeedPlan.setDeptId(getDeptId());
        sgFeedPlan.setUserId(getUserId());
        sgFeedPlan.setCreateDate(new Date());
        // 计算其他字段值
        setPlan(sgFeedPlan);
        // 通知配料清单刷新数据
        SgFeedListController.refresh = true;
        return toAjax(sgFeedPlanService.insertSgFeedPlan(sgFeedPlan));
    }

    /**
     * 修改饲喂计划
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedPlan:edit')")
    @Log(title = "饲喂计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SgFeedPlan sgFeedPlan) {
        // 根据修改后的值重新计算
        setPlan(sgFeedPlan);
        // 通知配料清单刷新数据
        SgFeedListController.refresh = true;
        return toAjax(sgFeedPlanService.updateSgFeedPlan(sgFeedPlan));
    }

    /**
     * 删除饲喂计划
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedPlan:remove')")
    @Log(title = "饲喂计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{createDates}")
    public AjaxResult remove(@PathVariable Date[] createDates) {
        // 通知配料清单刷新数据
        SgFeedListController.refresh = true;
        return toAjax(sgFeedPlanService.deleteSgFeedPlanByCreateDates(createDates));
    }

    /**
     * 设定计划值
     * 用于添加和修改
     */
    private void setPlan(SgFeedPlan sgFeedPlan) {
        // 根据羊舍ID获取羊只数量
        int countByFoldId = sgFeedPlanService.getSheepCountByFoldId(sgFeedPlan.getSheepHouseId());
        sgFeedPlan.setSheepCount(countByFoldId);
        // 计算总计划量
        sgFeedPlan.setPlanFeedTotal(countByFoldId * sgFeedPlan.getPlanDailySize());
        // 计算早中晚计划量
        sgFeedPlan.setPlanMorningSize(sgFeedPlan.getPlanFeedTotal() * (sgFeedPlan.getRatioMorning() / 100));
        sgFeedPlan.setPlanNoonSize(sgFeedPlan.getPlanFeedTotal() * (sgFeedPlan.getRatioNoon() / 100));
        sgFeedPlan.setPlanAfternoonSize(sgFeedPlan.getPlanFeedTotal() * (sgFeedPlan.getRatioAfternoon() / 100));
    }
}
