package com.zbf.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbf.common.exception.ServiceException;
import com.zbf.common.utils.DateUtils;
import com.zbf.common.utils.StringUtils;
import com.zbf.common.utils.bean.BeanValidators;
import com.zbf.system.domain.TBaseProvider;
import com.zbf.system.mapper.TBaseProviderMapper;
import com.zbf.system.service.ITBaseProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 供应商管理Service业务层处理
 *
 * @author sunits
 * @date 2024-06-16
 */
@Service
public class TBaseProviderServiceImpl extends ServiceImpl<TBaseProviderMapper, TBaseProvider> implements ITBaseProviderService //修改
{
    @Autowired
    private TBaseProviderMapper tBaseProviderMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询供应商管理
     *
     * @param id 供应商管理主键
     * @return 供应商管理
     */
    @Override
    public TBaseProvider selectTBaseProviderById(Long id) {
        return tBaseProviderMapper.selectTBaseProviderById(id);
    }

    /**
     * 查询供应商管理列表
     *
     * @param tBaseProvider 供应商管理
     * @return 供应商管理
     */
    @Override
    public List<TBaseProvider> selectTBaseProviderList(TBaseProvider tBaseProvider) {
        return tBaseProviderMapper.selectTBaseProviderList(tBaseProvider);
    }

    /**
     * 新增供应商管理
     *
     * @param tBaseProvider 供应商管理
     * @return 结果
     */
    @Override
    public int insertTBaseProvider(TBaseProvider tBaseProvider) {
        tBaseProvider.setCreateTime(DateUtils.getNowDate());
        //查询是否已经包含了该供应商
        TBaseProvider tBaseProviderDictionary1 = new TBaseProvider();
        tBaseProviderDictionary1.setProviderId(tBaseProvider.getProviderId());
        List<TBaseProvider> tBaseProviderDictionaries = tBaseProviderMapper.selectTBaseProviderList(tBaseProviderDictionary1);
        if (tBaseProviderDictionaries.size() > 0) {
            throw new RuntimeException("该供应商已存在，请勿重复添加");
        }
        return tBaseProviderMapper.insertTBaseProvider(tBaseProvider);
    }

    /**
     * 修改供应商管理
     *
     * @param tBaseProvider 供应商管理
     * @return 结果
     */
    @Override
    public int updateTBaseProvider(TBaseProvider tBaseProvider) {
        tBaseProvider.setUpdateTime(DateUtils.getNowDate());
        return tBaseProviderMapper.updateTBaseProvider(tBaseProvider);
    }

    /**
     * 批量删除供应商管理
     *
     * @param ids 需要删除的供应商管理主键
     * @return 结果
     */
    @Override
    public int deleteTBaseProviderByIds(Long[] ids) {
        return tBaseProviderMapper.deleteTBaseProviderByIds(ids);
    }

    /**
     * 删除供应商管理信息
     *
     * @param id 供应商管理主键
     * @return 结果
     */
    @Override
    public int deleteTBaseProviderById(Long id) {
        return tBaseProviderMapper.deleteTBaseProviderById(id);
    }


    /**
     * 批量禁用
     *
     * @param id 供应商管理主键
     * @return 结果
     */
    @Override
    public int disabledTBaseProviderByIds(Long[] ids) {
        return tBaseProviderMapper.disabledTBaseProviderByIds(ids);
    }

    /**
     * 批量启用
     *
     * @param id 供应商管理主键
     * @return 结果
     */
    @Override
    public int enableTBaseProviderByIds(Long[] ids) {
        return tBaseProviderMapper.enableTBaseProviderByIds(ids);
    }

    /**
     * 修改状态
     *
     * @param tBaseProvider 工序管理主键
     * @return 结果
     */
    @Override
    public int updateStatus(TBaseProvider tBaseProvider) {
        return tBaseProviderMapper.updateTBaseProvider(tBaseProvider);
    }

    @Override
    public String importData(List<TBaseProvider> list, boolean updateSupport, String userName) {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入托盘数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TBaseProvider tBaseProvider : list) {
            try {
                // 验证是否存在这个托盘
                TBaseProvider old = tBaseProviderMapper.selectOne(new QueryWrapper<TBaseProvider>().eq("provider_id", tBaseProvider.getProviderId()));
                if (StringUtils.isNull(old)) {
                    BeanValidators.validateWithException(validator, tBaseProvider);
                    tBaseProvider.setCreateBy(userName);
                    tBaseProviderMapper.insertTBaseProvider(tBaseProvider);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、供应商 " + tBaseProvider.getProviderId() + " 导入成功");
                } else if (updateSupport) {
                    BeanValidators.validateWithException(validator, tBaseProvider);
                    tBaseProvider.setUpdateBy(userName);
                    tBaseProviderMapper.updateTBaseProvider(tBaseProvider);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、供应商 " + tBaseProvider.getProviderId() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、供应商 " + tBaseProvider.getProviderId() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、供应商 " + tBaseProvider.getProviderId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
