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

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.WzStockInMapper;
import com.zhyc.module.stock.domain.WzStockIn;
import com.zhyc.module.stock.service.IWzStockInService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库记录Service业务层处理
 *
 * @author HashMap
 * @date 2025-08-05
 */
@Service
public class WzStockInServiceImpl implements IWzStockInService {
    private final WzStockInMapper wzStockInMapper;

    public WzStockInServiceImpl(WzStockInMapper wzStockInMapper) {
        this.wzStockInMapper = wzStockInMapper;
    }

    /**
     * 查询入库记录
     *
     * @param stockInCode 入库记录主键
     * @return 入库记录
     */
    @Override
    public WzStockIn selectWzStockInByStockInCode(Long stockInCode) {
        return wzStockInMapper.selectWzStockInByStockInCode(stockInCode);
    }

    /**
     * 查询入库记录列表
     *
     * @param wzStockIn 入库记录
     * @return 入库记录
     */
    @Override
    public List<WzStockIn> selectWzStockInList(WzStockIn wzStockIn) {
        return wzStockInMapper.selectWzStockInList(wzStockIn);
    }

    /**
     * 新增入库记录
     *
     * @param wzStockIn 入库记录
     * @return 结果
     */
    @Override
    public int insertWzStockIn(WzStockIn wzStockIn) {
        return wzStockInMapper.insertWzStockIn(wzStockIn);
    }

    /**
     * 修改入库记录
     *
     * @param wzStockIn 入库记录
     * @return 结果
     */
    @Override
    public int updateWzStockIn(WzStockIn wzStockIn) {
        return wzStockInMapper.updateWzStockIn(wzStockIn);
    }

    /**
     * 批量删除入库记录
     *
     * @param stockInCodes 需要删除的入库记录主键
     * @return 结果
     */
    @Override
    public int deleteWzStockInByStockInCodes(Long[] stockInCodes) {
        return wzStockInMapper.deleteWzStockInByStockInCodes(stockInCodes);
    }

    /**
     * 删除入库记录信息
     *
     * @param stockInCode 入库记录主键
     * @return 结果
     */
    @Override
    public int deleteWzStockInByStockInCode(Long stockInCode) {
        return wzStockInMapper.deleteWzStockInByStockInCode(stockInCode);
    }

    /**
     * 导入用户数据
     *
     * @param StockInList     用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importUser(List<WzStockIn> StockInList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(StockInList) || StockInList.isEmpty()) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        int sameNum = 0;
        boolean emptyTable = true;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        try {
            WzStockIn earliestStockIn = wzStockInMapper.getEarliestStockIn();
            if (null != earliestStockIn) emptyTable = false;
            System.out.println(earliestStockIn);
            for (WzStockIn wzStockIn : StockInList) {
                if (!emptyTable && earliestStockIn.getDocDate().getTime() >= wzStockIn.getDocDate().getTime()) {
                    sameNum++;
                    continue;
                }
                int result = wzStockInMapper.insertWzStockIn(wzStockIn);
                if (result == 0) {
                    failureNum++;
                    failureMsg.append("失败的项：").append(wzStockIn.getDocId()).append("\n");
                }
                else successNum++;
            }

        } catch (Exception e) {
            failureNum++;
        }
        if (failureNum > 0) {
            return failureMsg.toString();
        }
        successMsg.append("导入完成 ： 导入 ").append(successNum).append(" 条\n跳过 ").append(sameNum).append(" 条（重复记录）");
        return successMsg.toString();
    }
}
