package com.zhyc.module.feed.controller;

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

import com.zhyc.common.utils.uuid.UUID;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
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.domain.SgFeedStatistic;
import com.zhyc.module.feed.service.ISgFeedStatisticService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;

/**
 * 饲喂量统计Controller
 *
 * @author HashMap
 * @date 2025-08-23
 */
@RestController
@RequestMapping("/feed/FeedStatistic")
@Transactional(rollbackFor = Exception.class)
public class SgFeedStatisticController extends BaseController {
    private final ISgFeedStatisticService sgFeedStatisticService;



    public SgFeedStatisticController(ISgFeedStatisticService sgFeedStatisticService) {
        this.sgFeedStatisticService = sgFeedStatisticService;
    }

    /**
     * 查询饲喂量统计列表
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedStatistic:list')")
    @GetMapping("/list")
    public TableDataInfo list(SgFeedStatistic sgFeedStatistic) {
        startPage();
        List<SgFeedStatistic> list = sgFeedStatisticService.selectSgFeedStatisticList(sgFeedStatistic);
        return getDataTable(list);
    }

    /**
     * 导出饲喂量统计列表
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedStatistic:export')")
    @Log(title = "饲喂量统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SgFeedStatistic sgFeedStatistic) {
        List<SgFeedStatistic> list = sgFeedStatisticService.selectSgFeedStatisticList(sgFeedStatistic);
        ExcelUtil<SgFeedStatistic> util = new ExcelUtil<>(SgFeedStatistic.class);
        util.exportExcel(response, list, "饲喂量统计数据");
    }

    /**
     * 获取饲喂量统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedStatistic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(sgFeedStatisticService.selectSgFeedStatisticById(id));
    }

    /**
     * 新增饲喂量统计
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedStatistic:add')")
    @Log(title = "饲喂量统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SgFeedStatistic sgFeedStatistic) {
        if (null == sgFeedStatistic.getFormulaId() && null == sgFeedStatistic.getFormulaBatchId()) {
            throw new RuntimeException("ERROR: 数据为空");
        }
        sgFeedStatistic.setUserId(getUserId());
        sgFeedStatistic.setDeptId(getDeptId());
        List<SgFeedStatistic> isExist = sgFeedStatisticService.selectSgFeedStatisticList(sgFeedStatistic);
        if (null != isExist && !isExist.isEmpty()) {
            throw new RuntimeException("WARNING: 数据重复");
        }
        // 设定唯一标识符主键
        sgFeedStatistic.setId(UUID.randomUUID().toString());
        /*
         * 初始化数据
         * 弃用: 此处不再需要初始化，前端选定批号后会触发预载, 此处再初始化会覆盖前端后续的自定义数据
         */
        // sgFeedStatisticService.getInitSgFeedStatistic(sgFeedStatistic);
        return toAjax(sgFeedStatisticService.insertSgFeedStatistic(sgFeedStatistic));
    }

    /**
     * 修改饲喂量统计
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedStatistic:edit')")
    @Log(title = "饲喂量统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SgFeedStatistic sgFeedStatistic) {
        return toAjax(sgFeedStatisticService.updateSgFeedStatistic(sgFeedStatistic));
    }

    /**
     * 删除饲喂量统计
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedStatistic:remove')")
    @Log(title = "饲喂量统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sgFeedStatisticService.deleteSgFeedStatisticByIds(ids));
    }

    @GetMapping("/init")
    public TableDataInfo getInitData(SgFeedStatistic sgFeedStatistic){
        sgFeedStatisticService.getInitSgFeedStatistic(sgFeedStatistic);
        // 兼容写法 - 实际只是为了返回一个 sgFeedStatistic
        List<SgFeedStatistic> list = new ArrayList<>();
        list.add(sgFeedStatistic);
        return getDataTable(list);
    }
}
