package com.dgtly.order.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dgtly.common.exception.BusinessException;
import com.dgtly.common.utils.DateUtils;
import com.dgtly.common.utils.StringUtils;
import com.dgtly.order.domain.BpmWxNoticeOrg;
import com.dgtly.order.mapper.BpmWxNoticeOrgMapper;
import com.dgtly.order.service.IBpmWxNoticeOrgService;
import com.dgtly.system.domain.SysUser;
import com.dgtly.system.mapper.SysUserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dgtly.common.core.text.Convert;

/**
 * 企微人员发送Service业务层处理
 * 
 * @author dgtly
 * @date 2021-08-26
 */
@Service
public class BpmWxNoticeOrgServiceImpl implements IBpmWxNoticeOrgService
{
    private static final Logger log = LoggerFactory.getLogger(BpmWxNoticeOrgServiceImpl.class);
    @Autowired
    private BpmWxNoticeOrgMapper bpmWxNoticeOrgMapper;
    @Autowired
    private SysUserMapper userMapper;


    /**
     * 查询企微人员发送
     * 
     * @param id 企微人员发送ID
     * @return 企微人员发送
     */
    @Override
    public BpmWxNoticeOrg selectBpmWxNoticeOrgById(Long id)
    {
        return bpmWxNoticeOrgMapper.selectBpmWxNoticeOrgById(id);
    }

    /**
     * 查询企微人员发送列表
     * 
     * @param bpmWxNoticeOrg 企微人员发送
     * @return 企微人员发送
     */
    @Override
    public List<BpmWxNoticeOrg> selectBpmWxNoticeOrgList(BpmWxNoticeOrg bpmWxNoticeOrg)
    {
        return bpmWxNoticeOrgMapper.selectBpmWxNoticeOrgList(bpmWxNoticeOrg);
    }

    /**
     * 新增企微人员发送
     * 
     * @param bpmWxNoticeOrg 企微人员发送
     * @return 结果
     */
    @Override
    public int insertBpmWxNoticeOrg(BpmWxNoticeOrg bpmWxNoticeOrg)
    {
        bpmWxNoticeOrg.setCreateTime(DateUtils.getNowDate());
        return bpmWxNoticeOrgMapper.insertBpmWxNoticeOrg(bpmWxNoticeOrg);
    }

    /**
     * 修改企微人员发送
     * 
     * @param bpmWxNoticeOrg 企微人员发送
     * @return 结果
     */
    @Override
    public int updateBpmWxNoticeOrg(BpmWxNoticeOrg bpmWxNoticeOrg)
    {
        bpmWxNoticeOrg.setUpdateTime(DateUtils.getNowDate());
        return bpmWxNoticeOrgMapper.updateBpmWxNoticeOrg(bpmWxNoticeOrg);
    }

    /**
     * 删除企微人员发送对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBpmWxNoticeOrgByIds(String ids)
    {
        return bpmWxNoticeOrgMapper.deleteBpmWxNoticeOrgByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除企微人员发送信息
     * 
     * @param id 企微人员发送ID
     * @return 结果
     */
    @Override
    public int deleteBpmWxNoticeOrgById(Long id)
    {
        return bpmWxNoticeOrgMapper.deleteBpmWxNoticeOrgById(id);
    }

    /*导入数据*/
    @Override
    public String importBpmWxNoticeOrg(List<BpmWxNoticeOrg> wxNoticeOrgList, String operName) {
        if (StringUtils.isNull(wxNoticeOrgList) || wxNoticeOrgList.size() == 0)
        {
            throw new BusinessException("导入用户数据不能为空！");
        }

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

        List<BpmWxNoticeOrg> addList = new ArrayList<>();

        for (BpmWxNoticeOrg bn: wxNoticeOrgList) {
            try {
                /*验证用户是否存*/
//                BpmWxNoticeOrg wxNoticeOrg = bpmWxNoticeOrgMapper.selectBpmWxNoticeOrgByAccount(bn.getAccount());
//                if(StringUtils.isNull(wxNoticeOrg)){
                    /*查询orgcode*/
                    String orgcode =  bpmWxNoticeOrgMapper.selectMetaDiySalesorgtreeOrgCode(bn.getOrgName());
                    /*查询用户名*/
                    SysUser user = userMapper.selectUserByLoginName(bn.getAccount());
                    if (orgcode != null && user != null){
                        bn.setOrgCode(orgcode);
                        bn.setUserName(user.getUserName());
                        bn.setCreateBy(operName);
//                        insertBpmWxNoticeOrg(bn);
                        addList.add(bn);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、账号 " + bn.getAccount() + " 导入成功");
                    }else{
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、序号 " + bn.getAccount() + " 组织架构或者账号不存在");
                    }
//                }else{
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、序号 " + bn.getAccount() + " 已存在");
//                }
            } catch (Exception e) {
                e.printStackTrace();
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + bn.getAccount() + " 导入失败：";
                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 + " 条，数据如下：");

            //删除
            bpmWxNoticeOrgMapper.deleteBpmWxNoticeOrgAll();
            //新增
            addList.forEach(item->{
                this.insertBpmWxNoticeOrg(item);
            });

        }
        return successMsg.toString();
    }
}
