package com.aucma.production.controller;

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

import com.aucma.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.multipart.MultipartFile;
import com.aucma.common.annotation.Log;
import com.aucma.common.core.controller.BaseController;
import com.aucma.common.core.domain.AjaxResult;
import com.aucma.common.enums.BusinessType;
import com.aucma.production.domain.MrpComponentWeeklyReq;
import com.aucma.production.service.IMrpComponentWeeklyReqService;
import com.aucma.common.utils.poi.ExcelUtil;
import com.aucma.common.core.page.TableDataInfo;

/**
 * 子件物料周需求Controller
 *
 * @author Yinq
 * @date 2026-01-05
 */
@RestController
@RequestMapping("/production/req" )
public class MrpComponentWeeklyReqController extends BaseController {
    @Autowired
    private IMrpComponentWeeklyReqService mrpComponentWeeklyReqService;

    /**
     * 查询子件物料周需求列表
     */
    @PreAuthorize("@ss.hasPermi('production:req:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(MrpComponentWeeklyReq mrpComponentWeeklyReq) {
        startPage();
        List<MrpComponentWeeklyReq> list = mrpComponentWeeklyReqService.selectMrpComponentWeeklyReqList(mrpComponentWeeklyReq);
        return getDataTable(list);
    }

    /**
     * 导出子件物料周需求列表
     */
    @PreAuthorize("@ss.hasPermi('production:req:export')" )
    @Log(title = "子件物料周需求" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, MrpComponentWeeklyReq mrpComponentWeeklyReq) {
        List<MrpComponentWeeklyReq> list = mrpComponentWeeklyReqService.selectMrpComponentWeeklyReqList(mrpComponentWeeklyReq);
        ExcelUtil<MrpComponentWeeklyReq> util = new ExcelUtil<MrpComponentWeeklyReq>(MrpComponentWeeklyReq. class);
        util.exportExcel(response, list, "子件物料周需求数据" );
    }

    /**
     * 获取子件物料周需求详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:req:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return success(mrpComponentWeeklyReqService.selectMrpComponentWeeklyReqById(id));
    }

    /**
     * 新增子件物料周需求
     */
    @PreAuthorize("@ss.hasPermi('production:req:add')" )
    @Log(title = "子件物料周需求" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MrpComponentWeeklyReq mrpComponentWeeklyReq) {
        mrpComponentWeeklyReq.setCreatedAt(DateUtils.getNowDate());
        return toAjax(mrpComponentWeeklyReqService.insertMrpComponentWeeklyReq(mrpComponentWeeklyReq));
    }

    /**
     * 修改子件物料周需求
     */
    @PreAuthorize("@ss.hasPermi('production:req:edit')" )
    @Log(title = "子件物料周需求" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MrpComponentWeeklyReq mrpComponentWeeklyReq) {
        mrpComponentWeeklyReq.setUpdatedAt(DateUtils.getNowDate());
        return toAjax(mrpComponentWeeklyReqService.updateMrpComponentWeeklyReq(mrpComponentWeeklyReq));
    }

    /**
     * 删除子件物料周需求
     */
    @PreAuthorize("@ss.hasPermi('production:req:remove')" )
    @Log(title = "子件物料周需求" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mrpComponentWeeklyReqService.deleteMrpComponentWeeklyReqByIds(ids));
    }

    /**
     * 批量更新子件物料周需求
     */
    @PreAuthorize("@ss.hasPermi('production:req:edit')" )
    @Log(title = "子件物料周需求" , businessType = BusinessType.UPDATE)
    @PutMapping("/batchUpdate" )
    public AjaxResult batchUpdate(@RequestBody List<MrpComponentWeeklyReq> list) {
        int count = 0;
        for (MrpComponentWeeklyReq item : list) {
            item.setUpdatedAt(DateUtils.getNowDate());
            count += mrpComponentWeeklyReqService.updateMrpComponentWeeklyReq(item);
        }
        return success("成功更新 " + count + " 条数据");
    }

    /**
     * 导入子件物料周需求（异步导入，不阻塞前端）
     * @param file Excel文件
     * @param updateSupport 是否更新已存在数据
     * @param weekStartDate 周一日期（yyyy-MM-dd格式）
     */
    @PreAuthorize("@ss.hasPermi('production:req:import')" )
    @Log(title = "子件物料周需求" , businessType = BusinessType.IMPORT)
    @PostMapping("/importData" )
    public AjaxResult importData(MultipartFile file, boolean updateSupport, String weekStartDate) throws Exception {
        ExcelUtil<MrpComponentWeeklyReq> util = new ExcelUtil<MrpComponentWeeklyReq>(MrpComponentWeeklyReq.class);
        List<MrpComponentWeeklyReq> list = util.importExcel(file.getInputStream());
        
        int total = list.size();
        System.out.println("========== 开始异步导入子件物料周需求 ==========");
        System.out.println("weekStartDate: " + weekStartDate);
        System.out.println("数据量: " + total + " 条");
        
        // 异步导入，立即返回
        mrpComponentWeeklyReqService.importDataAsync(list, updateSupport, getUsername(), weekStartDate);
        
        return success("已提交后台异步导入任务，共 " + total + " 条数据，请稍后刷新页面查看结果。导入进度请查看控制台日志。");
    }

    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate" )
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<MrpComponentWeeklyReq> util = new ExcelUtil<MrpComponentWeeklyReq>(MrpComponentWeeklyReq.class);
        util.importTemplateExcel(response, "子件物料周需求模板" );
    }
}
