package com.zhyc.module.stock.service.impl;

import java.util.Date;
import java.util.List;

import com.zhyc.common.exception.ServiceException;
import com.zhyc.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.zhyc.module.stock.mapper.WzStockOutMapper;
import com.zhyc.module.stock.domain.WzStockOut;
import com.zhyc.module.stock.service.IWzStockOutService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库记录Service业务层处理
 *
 * @author HashMap
 * @date 2025-08-05
 */
@Service
public class WzStockOutServiceImpl implements IWzStockOutService {
    private final WzStockOutMapper wzStockOutMapper;

    public WzStockOutServiceImpl(WzStockOutMapper wzStockOutMapper) {
        this.wzStockOutMapper = wzStockOutMapper;
    }

    /**
     * 查询出库记录
     *
     * @param stockOutCode 出库记录主键
     * @return 出库记录
     */
    @Override
    public WzStockOut selectWzStockOutByStockOutCode(Long stockOutCode) {
        return wzStockOutMapper.selectWzStockOutByStockOutCode(stockOutCode);
    }

    /**
     * 查询出库记录列表
     *
     * @param wzStockOut 出库记录
     * @return 出库记录
     */
    @Override
    public List<WzStockOut> selectWzStockOutList(WzStockOut wzStockOut) {
        return wzStockOutMapper.selectWzStockOutList(wzStockOut);
    }

    /**
     * 新增出库记录
     *
     * @param wzStockOut 出库记录
     * @return 结果
     */
    @Override
    public int insertWzStockOut(WzStockOut wzStockOut) {
        return wzStockOutMapper.insertWzStockOut(wzStockOut);
    }

    /**
     * 修改出库记录
     *
     * @param wzStockOut 出库记录
     * @return 结果
     */
    @Override
    public int updateWzStockOut(WzStockOut wzStockOut) {
        return wzStockOutMapper.updateWzStockOut(wzStockOut);
    }

    /**
     * 批量删除出库记录
     *
     * @param stockOutCodes 需要删除的出库记录主键
     * @return 结果
     */
    @Override
    public int deleteWzStockOutByStockOutCodes(Long[] stockOutCodes) {
        return wzStockOutMapper.deleteWzStockOutByStockOutCodes(stockOutCodes);
    }

    /**
     * 删除出库记录信息
     *
     * @param stockOutCode 出库记录主键
     * @return 结果
     */
    @Override
    public int deleteWzStockOutByStockOutCode(Long stockOutCode) {
        return wzStockOutMapper.deleteWzStockOutByStockOutCode(stockOutCode);
    }

    /**
     * 导入用户数据
     *
     * @param WzStockOutList  入库数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importUser(List<WzStockOut> WzStockOutList, Boolean isUpdateSupport, String operName) throws Exception {
        if (StringUtils.isNull(WzStockOutList) || WzStockOutList.isEmpty()) {
            throw new ServiceException("导入入库数据不能为空！");
        }
        int successNum = 0;
        int sameNum = 0;
        StringBuilder successMsg = new StringBuilder();
        WzStockOut earliestStockOut = wzStockOutMapper.getEarliestStockOut();
        // 确保最近记录有值
        if (null == earliestStockOut || earliestStockOut.getCreateDate() == null) {
            if (earliestStockOut != null) {
                earliestStockOut.setCreateDate(new Date(0));
            } else {
                earliestStockOut = new WzStockOut();
                earliestStockOut.setCreateDate(new Date(0));
            }
        }
        for (WzStockOut wzStockOut : WzStockOutList) {
            if (null != wzStockOut && null != wzStockOut.getCreateDate()) {
                if (earliestStockOut.getCreateDate().getTime() >= wzStockOut.getCreateDate().getTime()) {
                    sameNum++;
                    continue;
                }
            } else {
                continue;
            }
            int result = wzStockOutMapper.insertWzStockOut(wzStockOut);
            if (result > 0) successNum++;
        }
        successMsg.append("导入完成 ： 导入 ").append(successNum).append(" 条\n跳过 ").append(sameNum).append(" 条（重复记录）");
        return successMsg.toString();
    }
}
