package com.zhyc.module.dairyProducts.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
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 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.common.core.page.TableDataInfo;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.module.dairyProducts.domain.NpMilkProdClasses;
import com.zhyc.module.dairyProducts.service.INpMilkProdClassesService;

@RestController
@RequestMapping("/milkProdclasses/milkProdclasses")
public class NpMilkProdClassesController extends BaseController {

    @Autowired
    private INpMilkProdClassesService npMilkProdClassesService;

    @PreAuthorize("@ss.hasPermi('milkProdclasses:milkProdclasses:list')")
    @GetMapping("/list")
    public TableDataInfo list(
            @RequestParam(required = false) Date datetimeStart,
            @RequestParam(required = false) Date datetimeEnd,
            @RequestParam(required = false) List<String> allEarNumbers,
            @RequestParam(required = false) String factory,
            @RequestParam(required = false) Integer classes) {
        startPage();
        NpMilkProdClasses params = new NpMilkProdClasses();
        params.setAllEarNumbers(allEarNumbers);
        params.setFactory(factory);
        params.setClasses(classes);

        List<NpMilkProdClasses> list = npMilkProdClassesService
                .selectNpMilkProdClassesList(params, datetimeStart, datetimeEnd);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('milkProdclasses:milkProdclasses:list')")
    @GetMapping("/search_ear_numbers")
    public AjaxResult searchEarNumbers(@RequestParam("query") String query) {
        try {
            List<String> earNumbers = npMilkProdClassesService.searchEarNumbers(query);
            return success(earNumbers);
        } catch (Exception e) {
            return error("搜索耳号失败：" + e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('milkProdclasses:milkProdclasses:import')")
    @Log(title = "班次产奶", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) {
        try {
            ExcelUtil<NpMilkProdClasses> util = new ExcelUtil<>(NpMilkProdClasses.class);
            List<NpMilkProdClasses> list = util.importExcel(file.getInputStream());

            // === 修改开始：循环注入当前用户和部门ID ===
            Long userId = getUserId();
            Long deptId = getDeptId();
            for (NpMilkProdClasses item : list) {
                item.setUserId(userId);
                item.setDeptId(deptId);
            }
            // === 修改结束 ===

            int rows = npMilkProdClassesService.importMilkProdClasses(list);
            return success("成功导入 " + rows + " 行数据");
        } catch (Exception e) {
            return error("导入失败：" + e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('milkProdclasses:milkProdclasses:export')")
    @Log(title = "班次产奶", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam(required = false) Date datetimeStart,
                       @RequestParam(required = false) Date datetimeEnd,
                       @RequestParam(required = false) List<String> allEarNumbers,
                       @RequestParam(required = false) String factory,
                       @RequestParam(required = false) Integer classes) {

        NpMilkProdClasses params = new NpMilkProdClasses();
        params.setAllEarNumbers(allEarNumbers);
        params.setFactory(factory);
        params.setClasses(classes);

        List<NpMilkProdClasses> list = npMilkProdClassesService.selectNpMilkProdClassesList(
                params, datetimeStart, datetimeEnd);

        ExcelUtil<NpMilkProdClasses> util = new ExcelUtil<>(NpMilkProdClasses.class);
        util.exportExcel(response, list, "班次产奶数据");
    }
}