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

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.warehouseBusiness.domain.StorageChargeInsert;
import com.ruoyi.warehouseBusiness.domain.TWareHouseExcelItems;
import com.ruoyi.warehouseBusiness.domain.TWarehouseBills;
import com.ruoyi.warehouseBusiness.excel.StorageDetail;
import com.ruoyi.warehouseBusiness.excel.StorageFee;
import com.ruoyi.warehouseBusiness.service.ITWarehouseBillsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 仓储费计算Controller
 *
 * @author ruoyi
 * @date 2020-12-11
 */
@RestController
@RequestMapping("/warehouseBusiness/storageFeeCalculation")
public class TStorageFeeCalculationController extends BaseController {
    @Autowired
    private ITWarehouseBillsService itWarehouseBillsService;


    /**
     * 查询入库详情主表列表
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:storageFeeCalculation:list')")
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/list")
    public TableDataInfo list(TWarehouseBills tWarehouseBills) {
        startPage();
        tWarehouseBills.setfBilltype("JSCCF");
        List<Map<String, Object>> list = itWarehouseBillsService.selectWarehouseBusinessList(tWarehouseBills);
        return getDataTable(list);
    }

    /**
     * 撤销入库信息
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:storageFeeCalculation:edit')")
    @Log(title = "详情主表", businessType = BusinessType.UPDATE)
    @GetMapping("/withdrawById/{fId}")
    @RepeatSubmit
    public AjaxResult withdrawById(@PathVariable("fId") Long fId) {
        String billsType = "JSCCFRevoke";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.revokeTWarehousebillsById(fId, loginUser, billsType);
    }

    /**
     * 导出入库详情主表列表
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:storageFeeCalculation:export')")
    @Log(title = "详情主表", businessType = BusinessType.EXPORT)
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/export")
    public AjaxResult export(TWarehouseBills tWarehouseBills) throws Exception {
        tWarehouseBills.setfBilltype("JSCCF");
        List<StorageFee> list = itWarehouseBillsService.storageFeeExport(tWarehouseBills);
        ExcelUtil<StorageFee> util = new ExcelUtil<StorageFee>(StorageFee.class);
        return util.exportExcel(list, "仓储费计算");
    }

    /**
     * 获取入库仓库详细信息
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:storageFeeCalculation:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId) {
        return itWarehouseBillsService.selectStorageFeeById(fId);
    }

    /**
     * 新增库存费计算
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:storageFeeCalculation:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @RepeatSubmit
    public AjaxResult add(@RequestParam("warehouseBills") String warehouseBills,
                          @RequestParam("warehouseItems") String warehouseItems,
                          @RequestParam("warehouseFees") String warehouseFees) {
        if (StringUtils.isNull(warehouseBills) || "{}".equals(warehouseBills)) {
            return AjaxResult.error("找不到主表信息");
        }
        String billsType = "JSCCF";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.insertStorageFee(warehouseBills, warehouseItems, warehouseFees, loginUser, billsType);
    }
    /**
     * 新增库存费批量计算
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:storageFeeCalculation:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addList")
    @RepeatSubmit
    public AjaxResult addList(@RequestBody List<StorageChargeInsert> storageChargeInsert) {

        String billsType = "JSCCF";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.insertStorageFeeList(storageChargeInsert,loginUser,billsType);
    }

    /**
     * 库存费计算
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:storageFeeCalculation:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/calculateFees")
    @RepeatSubmit
    public AjaxResult calculateFees(@RequestBody TWarehouseBills tWarehouseBills) {
        String billsType = "JSCCF";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.calculateStorageFees(tWarehouseBills, loginUser, billsType);
    }
    /**
     * 出库库存费批量计算
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:storageFeeCalculation:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/calculateFeesList")
    @RepeatSubmit
    public AjaxResult calculateFeesList(@RequestBody List<TWarehouseBills> tWarehouseBillsList) {
        String billsType = "JSCCF";
        // 获取当前的用户
        if (CollectionUtils.isEmpty(tWarehouseBillsList)){
            return AjaxResult.error("请选择条件后再计算仓储费");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.calculateStorageFeesList(tWarehouseBillsList, loginUser, billsType);
    }

    /**
     * 删除入库详情主表
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:storageFeeCalculation:remove')")
    @Log(title = "详情主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds) {
        return itWarehouseBillsService.deleteTWarehousebillsByIds(fIds);
    }

    @PreAuthorize("@ss.hasPermi('warehouseBusiness:storageFeeCalculation:remove')")
    @Log(title = "详情主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/queryRemove/{fId}")
    public AjaxResult queryRemove(@PathVariable("fId") Long fId) {
        return itWarehouseBillsService.queryRemove(fId);
    }

    /**
     * 导出入库详情主表列表
     */
    @GetMapping("/exportItems/{fId}")
    public AjaxResult exportItems(@PathVariable("fId") Long fId) {
        List<TWareHouseExcelItems> list = itWarehouseBillsService.selectTWarehousebillsItemsList(fId);
        ExcelUtil<TWareHouseExcelItems> util = new ExcelUtil<TWareHouseExcelItems>(TWareHouseExcelItems.class);
        return util.exportExcel(list, "计算仓储费");
    }

    /**
     * 导出仓储详情
     */
    @GetMapping("/exportDetail/{fId}")
    public AjaxResult exportDetail(@PathVariable("fId") Long fId) {
        List<StorageDetail> list = itWarehouseBillsService.selectStorageDetail(fId);
        ExcelUtil<StorageDetail> util = new ExcelUtil<>(StorageDetail.class);
        return util.exportExcel(list, "仓储费明细");
    }

}
