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

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.annotation.Log;
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.reportManagement.domain.SingleAnalysisExcel;
import com.ruoyi.reportManagement.domain.SingleAnalysisFeesExcel;
import com.ruoyi.reportManagement.domain.TWhgenleg;
import com.ruoyi.reportManagement.dto.TWarehousebill;
import com.ruoyi.reportManagement.excel.Whgenleg;
import com.ruoyi.reportManagement.service.ITWhgenlegService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库存总账Controller
 *
 * @author ruoyi
 * @date 2020-12-23
 */
@RestController
@RequestMapping("/warehouseBusiness/whgenleg")
public class TWhgenlegController extends BaseController {
    @Autowired
    private ITWhgenlegService tWhgenlegService;

    /**
     * 出库、调拨、货转查询库存总账列表
     */
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/list")
    public TableDataInfo list(TWhgenleg tWhgenleg) {
        startPage();
        List<Map<String, Object>> list = tWhgenlegService.selectInventoryList(tWhgenleg);
        return getDataTable(list);
    }

    /**
     * 查询库存总账列表
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:whgenleg:list')")
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/detailsList")
    public AjaxResult detailsList(TWhgenleg tWhgenleg) {
        return tWhgenlegService.selectWhgenlegDetailsList(tWhgenleg);
    }

    /**
     * 导出库存总账列表
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:whgenleg:export')")
    @Log(title = "库存总账", businessType = BusinessType.EXPORT)
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/export")
    public AjaxResult export(TWhgenleg tWhgenleg) throws Exception {
        List<Whgenleg> list = tWhgenlegService.whgenlegExport(tWhgenleg);
        ExcelUtil<Whgenleg> util = new ExcelUtil<Whgenleg>(Whgenleg.class);
        return util.exportExcel(list, "库存总账");
    }

    /**
     * 获取库存总账详细信息
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:whgenleg:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId) {
        if (StringUtils.isNull(fId)) {
            return AjaxResult.error("未获取到库存总账信息");
        }
        return AjaxResult.success(tWhgenlegService.selectTWhgenlegById(fId));
    }

    /**
     * 获取单票分析信息
     */
    @GetMapping(value = "/singleAnalysis")
    public AjaxResult singleAnalysis(SingleAnalysisExcel singleAnalysisExcel) {
        if (StringUtils.isNull(singleAnalysisExcel.getfCorpid()) || StringUtils.isEmpty(singleAnalysisExcel.getfMblno())) {
            return AjaxResult.error("未找到查询条件");
        }
        List<SingleAnalysisFeesExcel> feesList = tWhgenlegService.selectSingleAnalysisFees(singleAnalysisExcel);
        List<SingleAnalysisExcel> singleAnalysisExcels = tWhgenlegService.selectSingleAnalysis(singleAnalysisExcel);
        Map<String, Object> map = new HashMap<>();
        map.put("singleAnalysis", singleAnalysisExcels);
        map.put("feeList", feesList);
        String goodsName = "";
        if (StringUtils.isNotEmpty(singleAnalysisExcels)) {
            goodsName = singleAnalysisExcels.get(0).getGoodsName();
        }
        map.put("goodsName", goodsName);
        return AjaxResult.success(map);
    }

    /**
     * 导出单票分析信息
     */
    @GetMapping(value = "/exportSingleAnalysis")
    public AjaxResult exportSingleAnalysis(SingleAnalysisExcel singleAnalysisExcel) {
        if (StringUtils.isNull(singleAnalysisExcel.getfCorpid()) || StringUtils.isEmpty(singleAnalysisExcel.getfMblno())) {
            return AjaxResult.error("未找到查询条件信息");
        }
        List<SingleAnalysisExcel> list = tWhgenlegService.selectSingleAnalysis(singleAnalysisExcel);
        if (StringUtils.isEmpty(list)) {
            return AjaxResult.error("数据为空");
        }
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(list)) {
            map.put("mblno", list.get(0).getfMblno());
            map.put("corpName", list.get(0).getCorpName());
            map.put("goodsName", list.get(0).getGoodsName());
        }
        ExcelUtil<SingleAnalysisExcel> util = new ExcelUtil<SingleAnalysisExcel>(SingleAnalysisExcel.class);
        return util.singleAnalysisExportExcel(list, map, "单票分析");
    }

    /**
     * 导出单票分析费用信息
     */
    @GetMapping(value = "/exportSingleAnalysisFees")
    public AjaxResult exportSingleAnalysisFees(SingleAnalysisExcel singleAnalysisExcel) {
        if (StringUtils.isNull(singleAnalysisExcel.getfCorpid()) || StringUtils.isEmpty(singleAnalysisExcel.getfMblno())) {
            return AjaxResult.error("未找到查询条件信息");
        }
        List<SingleAnalysisFeesExcel> list = tWhgenlegService.selectSingleAnalysisFees(singleAnalysisExcel);
        if (StringUtils.isEmpty(list)) {
            return AjaxResult.error("数据为空");
        }
        ExcelUtil<SingleAnalysisFeesExcel> util = new ExcelUtil<SingleAnalysisFeesExcel>(SingleAnalysisFeesExcel.class);
        return util.singleAnalysisFeesExportExcel(list, "单票分析费用");
    }

    /**
     * 新增库存总账
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:whgenleg:add')")
    @Log(title = "库存总账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TWhgenleg tWhgenleg) {
        return toAjax(tWhgenlegService.insertTWhgenleg(tWhgenleg));
    }

    /**
     * 修改库存总账
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:whgenleg:edit')")
    @Log(title = "库存总账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWhgenleg tWhgenleg) {
        return toAjax(tWhgenlegService.updateTWhgenleg(tWhgenleg));
    }

    /**
     * 删除库存总账
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:whgenleg:remove')")
    @Log(title = "库存总账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fAccyears}")
    public AjaxResult remove(@PathVariable Long[] fAccyears) {
        return toAjax(tWhgenlegService.deleteTWhgenlegByIds(fAccyears));
    }

    /**
     * 统计图  商品 库存 结余 毛重 / 总库容
     */
    // @PreAuthorize("@ss.hasPermi('warehouseBusiness:whgenleg:list')")
    @GetMapping("/goodsList/{fId}")
    public AjaxResult goodsList(@PathVariable("fId") Long fId) {
        return AjaxResult.success(tWhgenlegService.selectGoodsList(fId));
    }

    /**
     * 统计图  仓库 毛重 / 库容
     */
    // @PreAuthorize("@ss.hasPermi('warehouseBusiness:whgenleg:list')")
    @GetMapping("/wareHouseList")
    public TableDataInfo wareHouseList() {
        startPage();
        // List<Map<String, Object>> list = tWhgenlegService.selectWareHouseList();
        List<Map<String, Object>> list = tWhgenlegService.selectWareHouseLists();
        return getDataTable(list);
    }

    /**
     * 统计图  客户   毛重 / 库容
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:whgenleg:list')")
    @GetMapping("/corpsList/{fId}")
    public AjaxResult corpsList(@PathVariable("fId") Long fId) {
        return AjaxResult.success(tWhgenlegService.selectCorpsList(fId));
    }

    /**
     * 当天 往前推一周  每日入库数量
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:whgenleg:list')")
    @GetMapping("/dateFQtyblcList")
    public TableDataInfo dateFQtyblcList() {
        startPage();
        List<Map<String, Object>> list = tWhgenlegService.selectDateFQtyblcListList();
        return getDataTable(list);
    }

    /**
     * 明细帐
     */
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/detailed/list")
    public TableDataInfo detailedList(TWarehousebill tWarehousebill) {
        startPage();
        List<Map<String, Object>> list = tWhgenlegService.selectdetailedList(tWarehousebill);
        return getDataTable(list);
    }

    /**
     * 根据客户id或者提单号查询库存不为0的仓库
     */
    //@PreAuthorize("@ss.hasPermi('warehouseBusiness:whgenleg:list')")
    @GetMapping("/getWarehouse")
    public AjaxResult getWarehouse(TWhgenleg tWhgenleg) {
        if (tWhgenleg.getfCorpid() == null) {
            return AjaxResult.error("请选择客户后再选择仓库");
        }
        if (StringUtils.isNull(tWhgenleg.getfMblno())) {
            return AjaxResult.error("请输入提单号后再选择仓库");
        }
        List<Map<String, Object>> list = tWhgenlegService.getWarehouse(tWhgenleg);
        return AjaxResult.success(list);
    }

    /**
     * 网站查询库存总账列表
     */
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/whgenlegList")
    public TableDataInfo websiteWhGenLegList(TWhgenleg tWhgenleg) {
        startPage();
        List<Map<String, Object>> list = tWhgenlegService.selectInventoryMapList(tWhgenleg);
        return getDataTable(list);
    }

    /**
     * app查询库存总账列表
     */
    //@DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/appWhGenLegList")
    public TableDataInfo appWhGenLegList(TWhgenleg tWhgenleg, Integer pageNum, Integer pageSize) {
        List<Map<String, Object>> list = tWhgenlegService.appWhGenLegList(tWhgenleg, pageNum, pageSize);
        return getDataTable(list);
    }

}
