package com.ruoyi.asset.service.impl;

import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.TbAssetInformationMapper;
import com.ruoyi.asset.domain.TbAssetInformation;
import com.ruoyi.asset.service.ITbAssetInformationService;

import javax.annotation.Resource;
import javax.validation.Validator;

/**
 * 资产信息Service业务层处理
 * 
 * @author 原动力
 * @date 2023-05-15
 */
@Service
public class TbAssetInformationServiceImpl implements ITbAssetInformationService 
{
    @Autowired
    private TbAssetInformationMapper tbAssetInformationMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    protected Validator validator;

    /**
     * 查询资产信息
     * 
     * @param id 资产信息主键
     * @return 资产信息
     */
    @Override
    public TbAssetInformation selectTbAssetInformationById(Long id)
    {
        return tbAssetInformationMapper.selectTbAssetInformationById(id);
    }

    @Override
    public TbAssetInformation selectTbAssetAdjustmentByAssetNumber(String assetNumber) {
        return tbAssetInformationMapper.selectTbAssetInformationByAssetNumber(assetNumber);
    }

    /**
     * 查询资产信息列表
     * 
     * @param tbAssetInformation 资产信息
     * @return 资产信息
     */
    @Override
    public List<TbAssetInformation> selectTbAssetInformationList(TbAssetInformation tbAssetInformation)
    {
        return tbAssetInformationMapper.selectTbAssetInformationList(tbAssetInformation);
    }

    /**
     * 新增资产信息
     * 
     * @param tbAssetInformation 资产信息
     * @return 结果
     */
    @Override
    public int insertTbAssetInformation(TbAssetInformation tbAssetInformation)
    {
        tbAssetInformation.setCreateTime(DateUtils.getNowDate());
        tbAssetInformation.setCreateBy(SecurityUtils.getUsername());
        return tbAssetInformationMapper.insertTbAssetInformation(tbAssetInformation);
    }

    /**
     * 修改资产信息
     * 
     * @param tbAssetInformation 资产信息
     * @return 结果
     */
    @Override
    public int updateTbAssetInformation(TbAssetInformation tbAssetInformation)
    {
        tbAssetInformation.setUpdateTime(DateUtils.getNowDate());
        return tbAssetInformationMapper.updateTbAssetInformation(tbAssetInformation);
    }

    /**
     * 批量删除资产信息
     * 
     * @param ids 需要删除的资产信息主键
     * @return 结果
     */
    @Override
    public int deleteTbAssetInformationByIds(Long[] ids)
    {
        return tbAssetInformationMapper.deleteTbAssetInformationByIds(ids);
    }

    /**
     * 删除资产信息信息
     * 
     * @param id 资产信息主键
     * @return 结果
     */
    @Override
    public int deleteTbAssetInformationById(Long id)
    {
        return tbAssetInformationMapper.deleteTbAssetInformationById(id);
    }

    @Override
    public boolean setDiy(List<String> labels) {
        String key = "func:assetInfo:" + SecurityUtils.getUserId();
        String jsonStr = JSONUtil.toJsonStr(labels);
        stringRedisTemplate.opsForValue().set(key, jsonStr);
        return true;
    }

    @Override
    public List<String> getDiy(){
        String json = stringRedisTemplate.opsForValue().get("func:assetInfo:" + SecurityUtils.getUserId());
        List<String> labels = JSONUtil.toList(json, String.class);
        return labels;
    }

    @Override
    public boolean updateProperty(TbAssetInformation information) {
        return tbAssetInformationMapper.updateProperty(information);
    }

    @Override
    public String importCon(List<TbAssetInformation> stuList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(stuList) || stuList.size() == 0)
        {
            throw new ServiceException("导入资产信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TbAssetInformation con : stuList)
        {
            try
            {
                // 验证是否存在这个用户
                TbAssetInformation u = tbAssetInformationMapper.selectTbAssetInformationById(con.getId());
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, con);
                    con.setCreateBy(operName);
                    this.insertTbAssetInformation(con);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编码 " + con.getNumber() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, con);
                    con.setUpdateBy(operName);
                    this.updateTbAssetInformation(con);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编码 " + con.getNumber() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、编码 " + con.getNumber() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、编码 " + con.getNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
