package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TServiceManageMapper;
import com.ruoyi.system.domain.TServiceManage;
import com.ruoyi.system.service.ITServiceManageService;

/**
 * 服务商管理Service业务层处理
 * 
 * @author zhengjie
 * @date 2023-08-22
 */
@Service
public class TServiceManageServiceImpl implements ITServiceManageService 
{
    @Autowired
    private TServiceManageMapper tServiceManageMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询服务商管理
     * 
     * @param id 服务商管理主键
     * @return 服务商管理
     */
    @Override
    public TServiceManage selectTServiceManageById(Integer id)
    {
        return tServiceManageMapper.selectTServiceManageById(id);
    }

    /**
     * 查询服务商管理列表
     * 
     * @param tServiceManage 服务商管理
     * @return 服务商管理
     */
    @Override
    public List<TServiceManage> selectTServiceManageList(TServiceManage tServiceManage)
    {
        return tServiceManageMapper.selectTServiceManageList(tServiceManage);
    }

    /**
     * 新增服务商管理
     * 
     * @param tServiceManage 服务商管理
     * @return 结果
     */
    @Override
    public int insertTServiceManage(TServiceManage tServiceManage)
    {
        SysUser user = new SysUser();
        if (StringUtils.isNotNull(tServiceManage.getPassword())){
            user.setPassword(tServiceManage.getPassword());
        }else {
            user.setPassword("123456");
        }
        if (StringUtils.isNotNull(tServiceManage.getUsername()) && StringUtils.isNotNull(user.getPassword())){
            user.setUserName(tServiceManage.getUsername());
            user.setNickName(tServiceManage.getUsername());
            sysUserMapper.insertUser(user);
        }
        tServiceManage.setCreateTime(DateUtils.getNowDate());
        return tServiceManageMapper.insertTServiceManage(tServiceManage);
    }

    /**
     * 修改服务商管理
     * 
     * @param tServiceManage 服务商管理
     * @return 结果
     */
    @Override
    public int updateTServiceManage(TServiceManage tServiceManage)
    {
        return tServiceManageMapper.updateTServiceManage(tServiceManage);
    }

    /**
     * 批量删除服务商管理
     * 
     * @param ids 需要删除的服务商管理主键
     * @return 结果
     */
    @Override
    public int deleteTServiceManageByIds(Integer[] ids)
    {
        return tServiceManageMapper.deleteTServiceManageByIds(ids);
    }

    /**
     * 删除服务商管理信息
     * 
     * @param id 服务商管理主键
     * @return 结果
     */
    @Override
    public int deleteTServiceManageById(Integer id)
    {
        return tServiceManageMapper.deleteTServiceManageById(id);
    }

    @Override
    public String importTServiceManage(List<TServiceManage> tServiceManageList) {

        if (StringUtils.isNull(tServiceManageList) || tServiceManageList.size() == 0)
        {
            throw new ServiceException("导入服务商管理信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TServiceManage tServiceManage : tServiceManageList)
        {
            try
            {
                if (checkManageData(tServiceManage))
                {
                    this.insertTServiceManage(tServiceManage);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "服务商管理信息" + " 导入成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "服务商管理信息" + " 信息不完整");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "服务商管理信息" + " 导入失败：";
                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();

    }

    public boolean checkManageData(TServiceManage tServiceManage){
        return StringUtils.isNotNull(tServiceManage.getStoreName()) && StringUtils.isNotNull(tServiceManage.getUsername()) &&
                StringUtils.isNotNull(tServiceManage.getPassword()) && StringUtils.isNotNull(tServiceManage.getLeader()) &&
                StringUtils.isNotNull(tServiceManage.getLeaderTel());
    }
}
