package com.gree.mall.manager.logic.goods;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.listvo.goods.GoodsMaterialStockAccVO;
import com.gree.mall.manager.commonmapper.CommonMapper;
import com.gree.mall.manager.enums.material.DirectFlagEnum;
import com.gree.mall.manager.enums.material.GoodsMaterialStockAccBillTypeEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.GoodsMaterial;
import com.gree.mall.manager.plus.entity.GoodsMaterialStockAcc;
import com.gree.mall.manager.plus.entity.GoodsMaterialStorage;
import com.gree.mall.manager.plus.service.GoodsMaterialService;
import com.gree.mall.manager.plus.service.GoodsMaterialStockAccService;
import com.gree.mall.manager.plus.service.GoodsMaterialStorageService;
import com.gree.mall.manager.plus.service.StorageService;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 商品物料库存三级台账
 * @author ：lijh
 * @description：TODO
 * @date ：2024/2/18 10:25
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class GoodsMaterialStockAccLogic {
    
    private final CommonLogic commonLogic;
    private final GoodsMaterialService goodsMaterialService;
    private final CommonMapper commonMapper;
    private final GoodsMaterialStockAccService goodsMaterialStockAccService;
    private final GoodsMaterialStorageService goodsMaterialStorageService;
    private final StorageService storageService;

    /**
     * 库存三级帐列表
     * @param zfireParamBean
     * @return
     */
    public IPage<GoodsMaterialStockAccVO> list(ZfireParamBean zfireParamBean){
        AdminUserCom adminUser = commonLogic.getAdminUser();
        FieldUtils.supplyParam(zfireParamBean,GoodsMaterialStockAccVO.class,adminUser);
        IPage<GoodsMaterialStockAccVO> list = commonMapper.goodsMaterialStockAccList(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
        return list;
    }


    /**
     * 新增三级帐
     * @param goodsMaterialId 物料id
     * @param price 变动价格
     * @param changeQty 变动数量
     * @param directFlagEnum 变动方向 ADD=增加 SUB=减少
     * @param refBillNo 相关单号
     * @param refBillType 相关单据类型
     */
    public void add(String goodsMaterialId, BigDecimal price, Integer changeQty, DirectFlagEnum directFlagEnum,  String refBillNo,
                    GoodsMaterialStockAccBillTypeEnum refBillType,String storageId,String storageName){

        GoodsMaterialStorage goodsMaterialStorage = goodsMaterialStorageService.lambdaQuery()
                .eq(GoodsMaterialStorage::getGoodsMaterialId, goodsMaterialId)
                .eq(GoodsMaterialStorage::getStorageId, storageId).last("limit 1").one();




        GoodsMaterial goodsMaterial = goodsMaterialService.getById(goodsMaterialId);
        if(goodsMaterial == null){
            throw new RemoteServiceException("物料id不存在");
        }
        GoodsMaterialStockAcc goodsMaterialStockAcc = new GoodsMaterialStockAcc()
                .setGoodsMaterialId(goodsMaterialId)
                .setGoodsMaterialName(goodsMaterial.getGoodsName())
                .setGoodsSpecsName(goodsMaterial.getSpecsName())
                .setCompanyWechatId(goodsMaterial.getCompanyWechatId())
                .setCompanyWechatName(goodsMaterial.getCompanyWechatName())
                .setBrandId(goodsMaterial.getBrandId())
                .setBrandName(goodsMaterial.getBrandName())
                .setMainId(goodsMaterial.getMainId())
                .setMainName(goodsMaterial.getMainName())
                .setSmallId(goodsMaterial.getSmallId())
                .setSmallName(goodsMaterial.getSmallName())
                .setUnit(goodsMaterial.getUnit())
                .setPrice(price)
                .setSdate(new Date())
                .setDirectFlag(directFlagEnum.getKey())
                //.setQty(goodsMaterial.getStockQty())
                .setChangeQty(changeQty)
                .setCloseQty(goodsMaterialStorage != null?goodsMaterialStorage.getStockQty():changeQty)
                .setRefBillNo(refBillNo)
                .setStorageId(storageId)
                .setStorageName(goodsMaterialStorage.getStorageName())
                .setRefBillType(refBillType.getKey());
        //反向算出原库存数量
        if(directFlagEnum == DirectFlagEnum.ADD){
            goodsMaterialStockAcc.setQty(goodsMaterialStockAcc.getCloseQty() - changeQty);
        }else{
            goodsMaterialStockAcc.setQty(goodsMaterialStockAcc.getCloseQty() + changeQty);
        }
        goodsMaterialStockAcc.insert();
    }
}
