package com.zhyc.module.dairyProducts.controller;

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.dairyProducts.domain.NpMilkInOutStore; // 引入实体类
import com.zhyc.module.dairyProducts.service.INpMilkInOutStoreService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@RestController
@RequestMapping("/milkInOutStore")
public class NpMilkInOutStoreController extends BaseController {

    @Autowired
    private INpMilkInOutStoreService service;

    /** 查询（日期区间）+ 动态列 */
    @PreAuthorize("@ss.hasPermi('milkInOutStore:list')")
    @GetMapping("/list")
    public TableDataInfo list(
            @RequestParam(required=false) Date datetimeStart,
            @RequestParam(required=false) Date datetimeEnd
    ) {
        startPage();
        // 修改：封装查询参数到实体对象，以便利用 BaseEntity 的 params 属性传递数据权限
        NpMilkInOutStore query = new NpMilkInOutStore();
        if (datetimeStart != null) {
            query.getParams().put("beginTime", datetimeStart);
        }
        if (datetimeEnd != null) {
            query.getParams().put("endTime", datetimeEnd);
        }

        List<Map<String, Object>> rows = service.selectWithDynamicColumns(query);
        return getDataTable(rows);
    }

    /** 导入 Excel */
    @Log(title="导入羊奶出入库", businessType=BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('milkInOutStore:import')")
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<Map<String,Object>> list = service.parseImportExcel(file);
        service.batchInsertFromRows(list);
        return AjaxResult.success("导入成功");
    }

    /** 导出 Excel */
    @PreAuthorize("@ss.hasPermi('milkInOutStore:export')")
    @Log(title="导出羊奶出入库", businessType=BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam(required=false) Date datetimeStart,
                       @RequestParam(required=false) Date datetimeEnd
    ) {
        // 修改：封装查询参数到实体对象
        NpMilkInOutStore query = new NpMilkInOutStore();
        if (datetimeStart != null) {
            query.getParams().put("beginTime", datetimeStart);
        }
        if (datetimeEnd != null) {
            query.getParams().put("endTime", datetimeEnd);
        }

        List<Map<String,Object>> rows = service.selectWithDynamicColumns(query);
        // 解决方案：强制转换并压制警告
        @SuppressWarnings("unchecked")
        ExcelUtil<Map<String,Object>> util = new ExcelUtil<>((Class<Map<String,Object>>) (Class<?>) Map.class);
        util.exportExcel(response, rows, "羊奶出入库数据");
    }

    /** 获取可选列定义（饲喂来源 + 销售去向） */
    @PreAuthorize("@ss.hasPermi('milkInOutStore:cols')")
    @GetMapping("/columns")
    public AjaxResult getColumns(){
        return AjaxResult.success(service.getAllColumnOptions());
    }
}