package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.YtjMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GtgsywOldMapper;
import com.ruoyi.system.domain.GtgsywOld;
import com.ruoyi.system.service.IGtgsywOldService;
import com.ruoyi.common.core.text.Convert;

/**
 * 个体字号Service业务层处理
 *
 * @author xu
 * @date 2019-12-04
 */
@Service
public class GtgsywOldServiceImpl implements IGtgsywOldService
{
    @Autowired
    private GtgsywOldMapper gtgsywOldMapper;
    @Autowired
    private YtjMapper ytjMapper;


    private static final Logger log = LoggerFactory.getLogger(GtgsywOldServiceImpl.class);
    /**
     * 查询个体字号
     *
     * @param id 个体字号ID
     * @return 个体字号
     */
    @Override
    public GtgsywOld selectGtgsywOldById(Long id)
    {
        return gtgsywOldMapper.selectGtgsywOldById(id);
    }

    /**
     * 查询个体字号列表
     *
     * @param gtgsywOld 个体字号
     * @return 个体字号
     */
    @Override
    public List<GtgsywOld> selectGtgsywOldList(GtgsywOld gtgsywOld)
    {
        return gtgsywOldMapper.selectGtgsywOldList(gtgsywOld);
    }

    /**
     * 新增个体字号
     *
     * @param gtgsywOld 个体字号
     * @return 结果
     */
    @Override
    public int insertGtgsywOld(GtgsywOld gtgsywOld)
    {
        return gtgsywOldMapper.insertGtgsywOld(gtgsywOld);
    }

    /**
     * 修改个体字号
     *
     * @param gtgsywOld 个体字号
     * @return 结果
     */
    @Override
    public int updateGtgsywOld(GtgsywOld gtgsywOld)
    {
        return gtgsywOldMapper.updateGtgsywOld(gtgsywOld);
    }

    /**
     * 删除个体字号对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGtgsywOldByIds(String ids)
    {
        return gtgsywOldMapper.deleteGtgsywOldByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除个体字号信息
     *
     * @param id 个体字号ID
     * @return 结果
     */
    @Override
    public int deleteGtgsywOldById(Long id)
    {
        return gtgsywOldMapper.deleteGtgsywOldById(id);
    }

    @Override
    public String importOld(List<GtgsywOld>olds, Boolean updateSupport){


        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for(GtgsywOld old:olds){
            try
            {
                String hantye=ytjMapper.selectByRemark(old.getHangyeid());
                old.setHangye(hantye);
                this.insertGtgsywOld(old);

            successNum++;
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + old.getZwzh() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
       /* int i=0;
        for(GtgsywOld old:olds){
                this.insertGtgsywOld(old);
            i++;
        }
        return  i;*/
    }

}
