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

import com.bonus.sgzb.common.core.constant.UserConstants;
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.common.security.utils.SecurityUtils;
import com.bonus.sgzb.machine.damain.MaHouse;
import com.bonus.sgzb.machine.damain.MaPropInfo;
import com.bonus.sgzb.machine.mapper.MaPropInfoMapper;
import com.bonus.sgzb.machine.service.IMaPropInfoService;
import com.bonus.sgzb.system.api.domain.SysUser;
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_prop_info(MaPropInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-11-28 10:18:12
 */
@Service("maPropInfoService")
@Slf4j
public class MaPropInfoServiceImpl implements IMaPropInfoService {

    @Autowired
    private MaPropInfoMapper maPropInfoMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询资产属性列表
     * @param propName
     * @return
     */
    @Override
    public List<MaPropInfo> selectMaPropInfoList(String propName) {
        return maPropInfoMapper.selectMaPropSet(propName);
    }

    /**
     * 校验资产项目名称唯一性
     * @param maPropInfo
     * @return
     */
    @Override
    public boolean checkPropNameUnique(MaPropInfo maPropInfo) {
        Long propId = StringUtils.isNull(maPropInfo.getPropId()) ? -1L : maPropInfo.getPropId();
        MaPropInfo info = maPropInfoMapper.checkPropNameUnique(maPropInfo.getPropName());
        if (StringUtils.isNotNull(info) && info.getPropId().longValue() != propId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增资产项目
     * @param maPropInfo
     * @return
     */
    @Override
    public int insertMaPropInfo(MaPropInfo maPropInfo) {
        return maPropInfoMapper.insertProp(maPropInfo);
    }

    /**
     * 删除资产管理
     * @param propId
     * @return
     */
    @Override
    public int deleteMaPropById(Long propId) {
        return maPropInfoMapper.deleteMaProp(propId);
    }

    /**
     * 修改资产管理
     * @param maPropInfo
     * @return
     */
    @Override
    public int updatetMaProp(MaPropInfo maPropInfo) {
        return maPropInfoMapper.updateMaProp(maPropInfo);
    }

    /**
     * 导入资产管理数据
     *
     * @param maPropInfoList 资产管理数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importMaProp(List<MaPropInfo> maPropInfoList, boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(maPropInfoList) || maPropInfoList.size() == 0)
        {
            throw new ServiceException("导入的数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MaPropInfo maPropInfo : maPropInfoList)
        {
            try
            {
                // 验证是否存在这个资产名称
                MaPropInfo m = maPropInfoMapper.checkPropNameUnique(maPropInfo.getPropName());
                if (StringUtils.isNull(m))
                {
                    BeanValidators.validateWithException(validator, maPropInfo);
                    maPropInfo.setCreateBy(operName);
                    maPropInfoMapper.insertProp(maPropInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、资产名称 " + maPropInfo.getPropName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, maPropInfo);
                    maPropInfo.setPropId(m.getPropId());
                    maPropInfo.setUpdateBy(operName);
                    maPropInfoMapper.updateMaProp(maPropInfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、资产名称 " + maPropInfo.getPropName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、资产名称 " + maPropInfo.getPropName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、资产名称 " + maPropInfo.getPropName() + " 导入失败：";
                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();
    }
}

