package com.ruoyi.web.controller.warehouse.warehouseBusiness;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.warehouseBusiness.domain.TWarehouseBills;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsitems;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsitemsNew;
import com.ruoyi.warehouseBusiness.excel.StockTransfer;
import com.ruoyi.warehouseBusiness.service.ITWarehouseBillsService;
import com.ruoyi.warehouseBusiness.service.ITWarehousebillsitemsService;
import com.ruoyi.warehouseBusiness.service.TWarehousebillsitemsNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 仓储分析 Controller
 */
@RestController
@RequestMapping("/warehousing/analysis")
public class TWarehouseAnalysisController extends BaseController {

    @Autowired
    private TWarehousebillsitemsNewService tWarehousebillsitemsNewService;

    // 从主查询仓储分析全部数据
    @PreAuthorize("@ss.hasPermi('Warehousing:analysis:list')")
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/list")
    public TableDataInfo list(TWarehousebillsitemsNew tWarehousebillsitemsNew){
        startPage();
        if(tWarehousebillsitemsNew.getType().equals("1")){
            tWarehousebillsitemsNew.setfBilltype("SJRK");
        }else{
            tWarehousebillsitemsNew.setfBilltype("SJCK");
        }
        List<TWarehousebillsitemsNew> list = tWarehousebillsitemsNewService.selectWarehouseBillItemsList(tWarehousebillsitemsNew);
        return getDataTable(list);
    }

    // 导出仓储分析数据
    @PreAuthorize("@ss.hasPermi('Warehousing:analysis:export')")
    @Log(title = "仓储分析数据",businessType = BusinessType.EXPORT)
    @DataScope(deptAlias = "d",userAlias = "u")
    @GetMapping("/export")
    public AjaxResult export(TWarehousebillsitemsNew tWarehousebillsitemsNew) throws Exception {
        String exportName = null;
        if (tWarehousebillsitemsNew.getType().equals("1")) {
            tWarehousebillsitemsNew.setfBilltype("SJRK");
            exportName = "在库";
        } else {
            tWarehousebillsitemsNew.setfBilltype("SJCK");
            exportName = "出库";
        }
        List<TWarehousebillsitemsNew> list = tWarehousebillsitemsNewService.selectWarehouseBillItemsList(tWarehousebillsitemsNew);
        ExcelUtil<TWarehousebillsitemsNew> util = new ExcelUtil<>(TWarehousebillsitemsNew.class);
        return util.exportExcel(list,exportName);
    }

    /**
     * 获取入库仓库详细信息
     */
    @PreAuthorize("@ss.hasPermi('Warehousing:analysis:query')")
    @GetMapping(value = "/{fId}")
    @RepeatSubmit
    public AjaxResult getInfo(@PathVariable("fId") Long fId) {
        return AjaxResult.success(tWarehousebillsitemsNewService.selectWarehouseBillItemesById(fId));
    }
}
