package com.zhyc.module.feed.controller;

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

import com.zhyc.module.feed.service.impl.SgFeedListServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
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.SgFeedList;
import com.zhyc.module.feed.service.ISgFeedListService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;

/**
 * 配料清单Controller
 *
 * @author HashMap
 * @date 2025-08-19
 */
@RestController
@RequestMapping("/feed/FeedList")
public class SgFeedListController extends BaseController {
    private final ISgFeedListService sgFeedListService;


    public static boolean refresh = true;

    @Autowired
    public SgFeedListController(ISgFeedListService sgFeedListService) {
        this.sgFeedListService = sgFeedListService;
    }

    /**
     * 查询配料清单列表
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedList:list')")
    @GetMapping("/list")
    @Transactional(rollbackFor = Exception.class)
    public TableDataInfo list(SgFeedList sgFeedList) {
        /*
        刷新缓存
        当配方管理表出现更新 或 饲喂计划表出现增删改时会将refresh置为true 通知此处进行刷新
         */
        if (refresh) {
            sgFeedListService.SyncFeedList();
            refresh = false;
        }
        startPage();
        List<SgFeedList> list = sgFeedListService.selectSgFeedListList(sgFeedList);
        // 用 map 中已有的数据替换 list 中的元素
        List<SgFeedList> replacedList = new ArrayList<>();
         for (SgFeedList item : list) {
         String key = item.getFormulaId() + "_" + item.getFormulaBatchId() + "_" + item.getDeployDate();
         // 从缓存中取出完整对象
         SgFeedList itemInCache = SgFeedListServiceImpl.getSgFeedListMap().getOrDefault(key, item);
         // 将数据库查询的基本信息替换掉缓存中去除的内容 - 前端展示与修改需要
         itemInCache.setId(item.getId());
         itemInCache.setFormulaBatchId(item.getFormulaBatchId());
         itemInCache.setFormulaId(item.getFormulaId());
         itemInCache.setZookeeper(item.getZookeeper());
         itemInCache.setDeployDate(item.getDeployDate());
         // 替换为 map 中的对象
         replacedList.add(itemInCache);
         }
        return getDataTable(replacedList);
    }

    /**
     * 导出配料清单列表
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedList:export')")
    @Log(title = "配料清单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SgFeedList sgFeedList) {
        List<SgFeedList> list = sgFeedListService.selectSgFeedListList(sgFeedList);
        ExcelUtil<SgFeedList> util = new ExcelUtil<>(SgFeedList.class);
        util.exportExcel(response, list, "配料清单数据");
    }

    /**
     * 获取配料清单详细信息
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedList:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sgFeedListService.selectSgFeedListById(id));
    }

    /**
     * 新增配料清单
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedList:add')")
    @Log(title = "配料清单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SgFeedList sgFeedList) {
        sgFeedList.setDeptId(getDeptId());
        sgFeedList.setUserId(getUserId());
        return toAjax(sgFeedListService.insertSgFeedList(sgFeedList));
    }

    /**
     * 修改配料清单
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedList:edit')")
    @Log(title = "配料清单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SgFeedList sgFeedList) {

        return toAjax(sgFeedListService.updateSgFeedList(sgFeedList));
    }

    /**
     * 删除配料清单
     */
    @PreAuthorize("@ss.hasPermi('feed:FeedList:remove')")
    @Log(title = "配料清单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sgFeedListService.deleteSgFeedListByIds(ids));
    }

}
