package com.bonus.sgzb.machine.service.impl;

import com.bonus.sgzb.common.core.exception.ServiceException;
import com.bonus.sgzb.common.core.utils.StringUtils;
import com.bonus.sgzb.common.core.utils.bean.BeanValidators;
import com.bonus.sgzb.machine.damain.MaPropInfo;
import com.bonus.sgzb.machine.mapper.MaLabelBindMapper;
import com.bonus.sgzb.machine.service.IMaLabelBindService;
import com.bonus.sgzb.machine.vo.MaLabelBindVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 机具设备标签ma_label_bind(MaLabelBind)表服务实现类
 *
 * @author makejava
 * @since 2023-11-28 16:37:53
 */
@Service("maLabelBindService")
@Slf4j
public class MaLabelBindServiceImpl implements IMaLabelBindService {

    @Autowired
    private MaLabelBindMapper maLabelBindMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询标签绑定管理列表
     * @param typeName
     * @return
     */
    @Override
    public List<MaLabelBindVO> selectMaLabelList(String typeName) {
        return maLabelBindMapper.selectMaLabelList(typeName);
    }

    /**
     * 批量绑定
     * @param labelIds
     * @return
     */
    @Override
    public int batchBindByIds(Long[] labelIds) {
        return maLabelBindMapper.updateBind(labelIds);
    }

    /**
     * 标签解绑
     * @param labelId
     * @return
     */
    @Override
    public int batchNoBindByIds(Long labelId) {
        return maLabelBindMapper.updateNoBind(labelId);
    }

    /**
     * 导入标签管理数据
     * @param maLabelBindList 导入数据
     * @param updateSupport 存在是否更新
     * @param operName 操作人
     * @return
     */
    @Override
    public String importMaLabel(List<MaLabelBindVO> maLabelBindList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(maLabelBindList) || maLabelBindList.size() == 0)
        {
            throw new ServiceException("导入的数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MaLabelBindVO maLabelBindVO : maLabelBindList)
        {
            try
            {
                // 验证是否存在这个类型名称
                MaLabelBindVO m = maLabelBindMapper.checkLabelNameUnique(maLabelBindVO.getTypeName());
                if (StringUtils.isNull(m))
                {
                    BeanValidators.validateWithException(validator, maLabelBindVO);
                    maLabelBindMapper.insertLabel(maLabelBindVO);
                    maLabelBindMapper.insertType(maLabelBindVO);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、类型名称 " + maLabelBindVO.getTypeName() + " 导入成功");
                }
                else if (updateSupport)
                {
                    BeanValidators.validateWithException(validator, maLabelBindVO);
                    maLabelBindVO.setLabelId(m.getLabelId());
                    maLabelBindMapper.updateMaLabel(maLabelBindVO);
                    maLabelBindMapper.updateMaType(maLabelBindVO);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、类型名称 " + maLabelBindVO.getTypeName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、类型名称 " + maLabelBindVO.getTypeName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、类型名称 " + maLabelBindVO.getTypeName() + " 导入失败：";
                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();
    }

    /**
     * 修改标签管理数据
     * @param maLabelBindVO
     * @return
     */
    @Override
    public void updatetMaLabel(MaLabelBindVO maLabelBindVO) {
        //修改标签列表数据
        maLabelBindMapper.updateMaLabel(maLabelBindVO);
        //修改类型数据
        maLabelBindMapper.updateMaType(maLabelBindVO);
    }
}

