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.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.utils.DateUtils;
import com.xingxi.common.utils.ShiroUtils;
import com.xingxi.common.utils.bean.BeanUtils;
import com.xingxi.web.controller.business.ProdInventory.domain.ProdInventoryOrderVo;
import com.xingxi.web.controller.business.ProdInventory.domain.ProdInventoryVo;
import com.xingxi.web.controller.business.ProdInventory.mapper.ProdInventoryOrderVoMapper;
import com.xingxi.web.controller.business.ProdInventory.mapper.ProdInventoryVoMapper;
import com.xingxi.web.controller.business.ProdInventory.service.IProdInventoryOrderVoService;
import org.springframework.stereotype.Service;

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

/**
 * 商品出入库单Vo Service业务层处理
 *
 * @author xingxi
 * @date 2025-03-06
 */
@Service
public class ProdInventoryOrderVoServiceImpl implements IProdInventoryOrderVoService {
    @Resource
    private ProdInventoryMapper prodInventoryMapper;
    @Resource
    private ProdInventoryVoMapper prodInventoryVoMapper;
    @Resource
    private ProdInventoryBillMapper prodInventoryBillMapper;
    @Resource
    private ProdInventoryOrderMapper prodInventoryOrderMapper;
    @Resource
    private ProdInventoryOrderVoMapper prodInventoryOrderVoMapper;
    @Resource
    private ProdInventoryOrderDetailMapper prodInventoryOrderDetailMapper;


    /**
     * 商品出入库单Vo
     *
     * @param inventoryOrderId 商品出入库单ID
     * @return 商品出入库单Vo
     */
    @Override
    public ProdInventoryOrderVo selectProdInventoryOrderVoById(Long inventoryOrderId) {
        return prodInventoryOrderVoMapper.selectProdInventoryOrderVoById(inventoryOrderId);
    }

    /**
     * 查询商品出入库单Vo列表
     *
     * @param prodInventoryOrderVo 商品出入库单Vo
     * @return 商品出入库单Vo集合
     */
    @Override
    public List<ProdInventoryOrderVo> selectProdInventoryOrderVoList(ProdInventoryOrderVo prodInventoryOrderVo) {
        return prodInventoryOrderVoMapper.selectProdInventoryOrderVoList(prodInventoryOrderVo);
    }

    /**
     * 更新商品出入库单Vo
     *
     * @param prodInventoryOrderVo 商品出入库单Vo
     * @return 更新结果
     */
    @Override
    public int updateProdInventoryOrderVo(ProdInventoryOrderVo prodInventoryOrderVo) {
        int effectRows = 0;
        Date nowDate = DateUtils.getNowDate();
        Long mercId;

        ProdInventoryOrderVo checkResult = prodInventoryOrderVoMapper.selectProdInventoryOrderVoById(prodInventoryOrderVo.getInventoryOrderId());
        if (EInventoryBillType.STOCKIN.getVal().equals(prodInventoryOrderVo.getIoFlag())) {
            mercId = checkResult.getInId();
        } else {
            mercId = checkResult.getOutId();
        }

        for (ProdInventoryOrderDetail prodInventoryOrderDetail : prodInventoryOrderVo.getInventoryOrderDetails()) {
            ProdInventory cond = new ProdInventory();
            cond.setProdId(prodInventoryOrderDetail.getProdId());
            cond.setProdAttrId(prodInventoryOrderDetail.getProdAttrId());
            cond.setProdAttrId(mercId);
            List<ProdInventory> listProdInventory = prodInventoryMapper.selectProdInventoryList(cond);
            Integer addInvQty = prodInventoryOrderDetail.getQuantity();
            ProdInventoryVo prodInventoryVo = new ProdInventoryVo();

            // 更新商品库存表
            if (listProdInventory.size() > 0) {
                BeanUtils.copyBeanProp(prodInventoryVo, listProdInventory.get(0));
                prodInventoryVo.setIoFlag(EInventoryBillType.STOCKIN.getVal());
                prodInventoryVo.setAddInvQty(addInvQty);
                prodInventoryVo.setUpdateUser(ShiroUtils.getLoginName());
                prodInventoryVo.setUpdateTime(nowDate);
                effectRows = prodInventoryVoMapper.updateProdInventoryVo(prodInventoryVo);
            } else {
                ProdInventory prodInventory = new ProdInventory();
                prodInventory.setProdId(prodInventoryOrderDetail.getProdId());
                prodInventory.setProdAttrId(prodInventoryOrderDetail.getProdAttrId());
                prodInventory.setMercId(mercId);
                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());
            }

            // 更新商品出入库明细单
            prodInventoryOrderDetail.setInventoryOrderId(prodInventoryOrderVo.getInventoryOrderId());
            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(prodInventoryOrderVo.getIoFlag());
            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;
    }
}
