package com.sy.coinage.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sy.coinage.app.req.AppStockReq;
import com.sy.coinage.app.vo.AppStockVO;
import com.sy.coinage.core.annotation.ActiveFastJsonProfile;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.annotation.LoginNotRequired;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.req.IdReq;
import com.sy.coinage.core.util.Arith;
import com.sy.coinage.core.vo.CacheUserVO;
import com.sy.coinage.core.vo.OptVO;
import com.sy.coinage.sys.model.Dept;
import com.sy.coinage.sys.service.IDeptService;
import com.sy.coinage.sys.service.IDictService;
import com.sy.coinage.workshop.model.BaseInfo;
import com.sy.coinage.workshop.model.Product;
import com.sy.coinage.workshop.model.Stock;
import com.sy.coinage.workshop.req.StockListReq;
import com.sy.coinage.workshop.service.IBaseInfoService;
import com.sy.coinage.workshop.service.IProductService;
import com.sy.coinage.workshop.service.IStockService;
import com.sy.coinage.workshop.vo.ShopWareStatusVO;
import com.sy.coinage.workshop.vo.StockHeaderVO;
import com.sy.coinage.workshop.vo.StockVO;
import com.sy.coinage.workshop.vo.WareStatusVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * -实时库存表Controller
 */
@Api(tags = "APP-实时库存表接口")
@RestController
@RequestMapping(value = "/app/stock")
public class AppStockController extends BaseController {

    @Autowired
    private IStockService stockService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IProductService productService;

    @ApiOperation(value = "app获取实时库存")
    @LoginNotRequired
    @PostMapping("/get")
    public AppStockVO get(@RequestBody @Valid AppStockReq req) {
        Dept dept = this.deptService.getById(req.getDeptId());
        AppStockVO data = new AppStockVO();
        Product p = this.productService.getByCache(req.getSecuritiesId());
        data.setProduct(p);
        LambdaQueryWrapper<Stock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Stock::getWareId, Integer.parseInt(dept.getRemark()));
        queryWrapper.eq(Stock::getSecuritiesId, req.getSecuritiesId());
        queryWrapper.orderByDesc(Stock::getId);
        List<Stock> list = this.stockService.list(queryWrapper);
        Integer total = 0;
        total = total + this.stockService.getProductStockTotal(Integer.parseInt(dept.getRemark()), 1, req.getSecuritiesId(), p);
        total = total + this.stockService.getProductStockTotal(Integer.parseInt(dept.getRemark()), 2, req.getSecuritiesId(), p);
        data.setTotal(total);
        return data;
    }


}
