package com.fjhx.controller.stock;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.base.Condition;
import com.fjhx.entity.stock.ManualOutDto;
import com.fjhx.enums.stock.OutTypeEnum;
import com.fjhx.params.stock.StockChangeDto;
import com.fjhx.service.stock.StockJournalService;
import com.fjhx.service.stock.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.util.Map;

/**
 * @author 手动出库
 */
@RestController
@RequestMapping("/manualOut")
public class ManualOutStockController {

    @Autowired
    private StockService stockService;

    @Autowired
    private StockJournalService stockJournalService;

    @PostMapping("page")
    public R page(@RequestBody Condition condition) {
        Page<Map<String, Object>> page = stockJournalService.getManualOutPage(condition);
        return R.success(page);
    }

    @PostMapping("/add")
    public R add(@RequestBody ManualOutDto manualOutDto) {
        StockChangeDto stockChangeDto = new StockChangeDto();
        stockChangeDto.setDefaultBusinessId(IdWorker.getId());
        stockChangeDto.setDefaultWarehouseId(manualOutDto.getWarehouseId());
        stockChangeDto.setDefaultRemarks(manualOutDto.getRemarks());
        stockChangeDto.setTypeEnum(OutTypeEnum.getEnumByKey(manualOutDto.getType()));
        stockChangeDto.setChangeDetailsList(manualOutDto.getChangeProductList());

        stockService.changeQuantity(stockChangeDto);
        return R.success();
    }

}
