package com.xingxi.web.controller.business.ProdInventory.service.impl;

import com.xingxi.business.ProdInventory.domain.ProdInventory;
import com.xingxi.business.ProdInventory.domain.ProdInventoryBill;
import com.xingxi.business.ProdInventory.domain.ProdInventoryOrder;
import com.xingxi.business.ProdInventory.domain.ProdInventoryOrderDetail;
import com.xingxi.business.ProdInventory.mapper.ProdInventoryBillMapper;
import com.xingxi.business.ProdInventory.mapper.ProdInventoryMapper;
import com.xingxi.business.ProdInventory.mapper.ProdInventoryOrderDetailMapper;
import com.xingxi.business.ProdInventory.mapper.ProdInventoryOrderMapper;
import com.xingxi.common.enums.EDelFlag;
import com.xingxi.common.enums.EInventoryBillType;
import com.xingxi.common.enums.EInventoryBillReason;
import com.xingxi.common.utils.DateUtils;
import com.xingxi.common.utils.ShiroUtils;
import com.xingxi.common.utils.bean.BeanUtils;
import com.xingxi.web.controller.business.ProdInventory.domain.ProdInventoryVo;
import com.xingxi.web.controller.business.ProdInventory.mapper.ProdInventoryVoMapper;
import com.xingxi.web.controller.business.ProdInventory.service.IProdInventoryVoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 商品库存Service业务层处理
 *
 * @author xingxi
 * @date 2025-03-06
 */
@Service
public class ProdInventoryVoServiceImpl implements IProdInventoryVoService {
    @Resource
    private ProdInventoryMapper prodInventoryMapper;
    @Resource
    private ProdInventoryVoMapper prodInventoryVoMapper;
    @Resource
    private ProdInventoryBillMapper prodInventoryBillMapper;
    @Resource
    private ProdInventoryOrderMapper prodInventoryOrderMapper;
    @Resource
    private ProdInventoryOrderDetailMapper prodInventoryOrderDetailMapper;

    /**
     * 查询商品库存
     *
     * @param prodInventoryId 商品库存主键
     * @return 商品库存
     */
    @Override
    public ProdInventoryVo selectProdInventoryVoByProdInventoryId(Long prodInventoryId) {
        return prodInventoryVoMapper.selectProdInventoryVoByProdInventoryId(prodInventoryId);
    }

    /**
     * 查询商品库存列表
     *
     * @param prodInventoryVo 商品库存
     * @return 商品库存
     */
    @Override
    public List<ProdInventoryVo> selectProdInventoryVoList(ProdInventoryVo prodInventoryVo) {
        return prodInventoryVoMapper.selectProdInventoryVoList(prodInventoryVo);
    }

    /**
     * 新增商品库存
     *
     * @param prodInventoryVo 商品库存Vo
     * @return 结果
     */
    @Override
    public int insertProdInventoryVo(ProdInventoryVo prodInventoryVo) {
        int effectRows;
        Integer addInvQty = prodInventoryVo.getAddInvQty();
        Date nowDate = DateUtils.getNowDate();
        // 更新商品库存表
        if (prodInventoryVo.getProdInventoryId() != null) {
            prodInventoryVo = prodInventoryVoMapper.selectProdInventoryVoByProdInventoryId(prodInventoryVo.getProdInventoryId());
            prodInventoryVo.setIoFlag(EInventoryBillType.STOCKIN.getVal());
            prodInventoryVo.setAddInvQty(addInvQty);
            prodInventoryVo.setUpdateUser(ShiroUtils.getLoginName());
            prodInventoryVo.setUpdateTime(nowDate);
            effectRows = prodInventoryVoMapper.updateProdInventoryVo(prodInventoryVo);
        } else {
            ProdInventory prodInventory = new ProdInventory();
            BeanUtils.copyBeanProp(prodInventory, prodInventoryVo);
            prodInventory.setQuantity(addInvQty);
            prodInventory.setAvailQty(addInvQty);
            prodInventory.setDelFlag(EDelFlag.NO.getVal());
            prodInventory.setCreateUser(ShiroUtils.getLoginName());
            prodInventory.setCreateTime(nowDate);
            effectRows = prodInventoryMapper.insertProdInventory(prodInventory);
            prodInventoryVo = prodInventoryVoMapper.selectProdInventoryVoByProdInventoryId(prodInventoryVo.getProdInventoryId());
        }
        // 更新商品出入库单
        ProdInventoryOrder prodInventoryOrder = new ProdInventoryOrder();
        prodInventoryOrder.setIoFlag(EInventoryBillType.STOCKIN.getVal());
        prodInventoryOrder.setIoType(EInventoryBillReason.IN_PURCHASE.getVal());
        prodInventoryOrder.setInId(prodInventoryVo.getMercId());
        prodInventoryOrder.setDelFlag(EDelFlag.NO.getVal());
        prodInventoryOrder.setCreateUser(ShiroUtils.getLoginName());
        prodInventoryOrder.setCreateTime(nowDate);
        effectRows += prodInventoryOrderMapper.insertProdInventoryOrder(prodInventoryOrder);
        // 更新商品出入库明细单
        ProdInventoryOrderDetail prodInventoryOrderDetail = new ProdInventoryOrderDetail();
        prodInventoryOrderDetail.setInventoryOrderId(prodInventoryOrder.getInventoryOrderId());
        prodInventoryOrderDetail.setProdId(prodInventoryVo.getProdId());
        prodInventoryOrderDetail.setProdName(prodInventoryVo.getProdName());
        prodInventoryOrderDetail.setProdAttrId(prodInventoryVo.getProdAttrId());
        prodInventoryOrderDetail.setProdAttrName(prodInventoryVo.getAttrName());
        prodInventoryOrderDetail.setQuantity(addInvQty);
        prodInventoryOrderDetail.setDelFlag(EDelFlag.NO.getVal());
        prodInventoryOrderDetail.setCreateUser(ShiroUtils.getLoginName());
        prodInventoryOrderDetail.setCreateTime(nowDate);
        effectRows += prodInventoryOrderDetailMapper.insertProdInventoryOrderDetail(prodInventoryOrderDetail);
        // 更新商品出入库流水
        ProdInventoryBill prodInventoryBill = new ProdInventoryBill();
        prodInventoryBill.setBillTime(nowDate);
        prodInventoryBill.setBillType(EInventoryBillType.STOCKIN.getVal());
        prodInventoryBill.setInventoryId(prodInventoryVo.getProdInventoryId());
        prodInventoryBill.setQuantity(addInvQty);
        prodInventoryBill.setDelFlag(EDelFlag.NO.getVal());
        prodInventoryBill.setCreateUser(ShiroUtils.getLoginName());
        prodInventoryBill.setCreateTime(nowDate);
        effectRows += prodInventoryBillMapper.insertProdInventoryBill(prodInventoryBill);

        return effectRows;
    }
}
