package com.fjhx.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.constants.StockJournalTypeConstant;
import com.fjhx.service.StockExService;
import com.fjhx.service.StockService;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 库存 前端控制器
 */
@RestController
@RequestMapping("/stock")
public class StockController {

    @Autowired
    private StockService stockService;

    @Autowired
    private StockExService stockExService;

    /**
     * 入库
     */
    @PostMapping("/in")
    public R in(@RequestBody Map<String, String> condition) {
        stockService.inChangeQuantity(
                Long.valueOf(condition.get("goodsId")),
                Long.valueOf(condition.get("warehouseId")),
                new BigDecimal(condition.get("quantity")),
                Integer.valueOf(condition.get("detailsType")),
                condition.get("remarks")
        );
        return R.success();
    }

    /**
     * 出库
     */
    @PostMapping("/out")
    public R out(@RequestBody Map<String, String> condition) {
        stockService.outChangeQuantity(
                Long.valueOf(condition.get("goodsId")),
                Long.valueOf(condition.get("warehouseId")),
                new BigDecimal(condition.get("quantity")),
                Integer.valueOf(condition.get("detailsType")),
                condition.get("remarks")
        );
        return R.success();
    }

    /**
     * 入库分页记录
     */
    @PostMapping("getInPage")
    public R getInPage(@RequestBody Map<String, String> condition) {
        condition.put("type", StockJournalTypeConstant.IN.toString());
        Page<Map<String, Object>> inOutPage = stockExService.getInOutPage(condition);
        return R.success(inOutPage);
    }

    /**
     * 出库分页记录
     */
    @PostMapping("getOutPage")
    public R getOutPage(@RequestBody Map<String, String> condition) {
        condition.put("type", StockJournalTypeConstant.OUT.toString());
        Page<Map<String, Object>> inOutPage = stockExService.getInOutPage(condition);
        return R.success(inOutPage);
    }

    /**
     * 出入库分页记录
     */
    @PostMapping("getInOutPage")
    public R getInOutPage(@RequestBody Map<String, String> condition) {
        condition.put("type", null);
        Page<Map<String, Object>> inOutPage = stockExService.getInOutPage(condition);
        return R.success(inOutPage);
    }

    /**
     * 获取库存详情
     */
    @PostMapping("/page")
    public R page(@RequestBody Map<String, String> condition) {
        Page<Map<String, Object>> page = stockExService.getPage(condition);
        return R.success(page);
    }

}
