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.WzMaterialsManagementMapper;
import com.zhyc.module.stock.domain.WzMaterialsManagement;
import com.zhyc.module.stock.service.IWzMaterialsManagementService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物资管理Service业务层处理
 *
 * @author HashMap
 * @date 2025-08-05
 */
@Service
public class WzMaterialsManagementServiceImpl implements IWzMaterialsManagementService {
    private final WzMaterialsManagementMapper wzMaterialsManagementMapper;

    public WzMaterialsManagementServiceImpl(WzMaterialsManagementMapper wzMaterialsManagementMapper) {
        this.wzMaterialsManagementMapper = wzMaterialsManagementMapper;
    }

    /**
     * 查询物资管理
     *
     * @param materialManagementCode 物资管理主键
     * @return 物资管理
     */
    @Override
    public WzMaterialsManagement selectWzMaterialsManagementByMaterialManagementCode(Long materialManagementCode) {
        return wzMaterialsManagementMapper.selectWzMaterialsManagementByMaterialManagementCode(materialManagementCode);
    }

    /**
     * 查询物资管理列表
     *
     * @param wzMaterialsManagement 物资管理
     * @return 物资管理
     */
    @Override
    public List<WzMaterialsManagement> selectWzMaterialsManagementList(WzMaterialsManagement wzMaterialsManagement) {
        return wzMaterialsManagementMapper.selectWzMaterialsManagementList(wzMaterialsManagement);
    }

    /**
     * 新增物资管理
     *
     * @param wzMaterialsManagement 物资管理
     * @return 结果
     */
    @Override
    public int insertWzMaterialsManagement(WzMaterialsManagement wzMaterialsManagement) {
        return wzMaterialsManagementMapper.insertWzMaterialsManagement(wzMaterialsManagement);
    }

    /**
     * 修改物资管理
     *
     * @param wzMaterialsManagement 物资管理
     * @return 结果
     */
    @Override
    public int updateWzMaterialsManagement(WzMaterialsManagement wzMaterialsManagement) {
        return wzMaterialsManagementMapper.updateWzMaterialsManagement(wzMaterialsManagement);
    }

    /**
     * 批量删除物资管理
     *
     * @param materialManagementCodes 需要删除的物资管理主键
     * @return 结果
     */
    @Override
    public int deleteWzMaterialsManagementByMaterialManagementCodes(Long[] materialManagementCodes) {
        return wzMaterialsManagementMapper.deleteWzMaterialsManagementByMaterialManagementCodes(materialManagementCodes);
    }

    /**
     * 删除物资管理信息
     *
     * @param materialManagementCode 物资管理主键
     * @return 结果
     */
    @Override
    public int deleteWzMaterialsManagementByMaterialManagementCode(Long materialManagementCode) {
        return wzMaterialsManagementMapper.deleteWzMaterialsManagementByMaterialManagementCode(materialManagementCode);
    }

    /**
     * 导入用户数据
     *
     * @param MaterialsManagementList 物资数据列表
     * @param isUpdateSupport         是否更新支持，如果已存在，则进行更新数据
     * @param operName                操作用户
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importUser(List<WzMaterialsManagement> MaterialsManagementList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(MaterialsManagementList) || MaterialsManagementList.isEmpty()) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int sameNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        try {
            for (WzMaterialsManagement wzMaterialsManagement : MaterialsManagementList) {
                if (wzMaterialsManagement.getMaterialId() != null) {
                    WzMaterialsManagement isExist = wzMaterialsManagementMapper.selectWzMaterialsManagementByMaterialID(wzMaterialsManagement.getMaterialId());
                    // 存在则更新
                    if (isExist != null) {
                        sameNum++;
                        wzMaterialsManagementMapper.updateWzMaterialsManagement(wzMaterialsManagement);
                        continue;
                    }
                }
                int result = wzMaterialsManagementMapper.insertWzMaterialsManagement(wzMaterialsManagement);
                if (result > 0) successNum++;
            }
        } catch (Exception e) {
            return failureMsg.append("导入出错：").append(e.getMessage()).toString();
        }
        successMsg.append("导入完成 ： 导入 ").append(successNum).append(" 条\n更新 ").append(sameNum).append(" 条（已存在记录）");
        return successMsg.toString();
    }

}
