package com.ozs.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ozs.base.domain.BaseProfessional;
import com.ozs.base.domain.vo.BaseProfessionalVo;
import com.ozs.base.mapper.BaseProfessionalMapper;
import com.ozs.base.service.BaseProfessionalService;
import com.ozs.common.core.domain.entity.SysDictType;
import com.ozs.common.core.domain.entity.SysUser;
import com.ozs.common.exception.ServiceException;
import com.ozs.common.utils.DictUtils;
import com.ozs.common.utils.bean.BeanUtils;
import com.ozs.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;

@Service
public class BaseProfessionalServiceImpl extends ServiceImpl<BaseProfessionalMapper, BaseProfessional> implements BaseProfessionalService {

    @Autowired
    private BaseProfessionalMapper baseProfessionalMapper;
    @Autowired
    protected Validator validator;

    @Override
    public void deleteIds(List<Integer> deleteIds) {
        for (Integer deleteId : deleteIds) {
            BaseProfessional baseProfessional = baseProfessionalMapper.selectById(deleteId);
            List<BaseProfessional> baseProfessionalList = baseProfessionalMapper.selectByParentCode(baseProfessional.getProfessionalCode());
//            int ii = baseProfessionalMapper.countByParentCode(baseProfessional.getProfessionalCode());
            if (baseProfessionalList.size() > 0) {
                throw new ServiceException(String.format("%1$s已有子级,不能删除", baseProfessional.getProfessionalName()));
            }
            baseProfessionalMapper.deleteById(deleteId);
        }
    }

    @Override
    public String importBaseProfessional(List<BaseProfessional> baseProfessionalList, boolean updateSupport) {
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BaseProfessional baseProfessional : baseProfessionalList) {
            try {
                // 判断编码是否存在
                List<BaseProfessional> professionalCode = baseProfessionalMapper.selectByProfessionalCode(baseProfessional);
                List<BaseProfessional> professionalName = baseProfessionalMapper.selectByProfessionalName(baseProfessional);
                //List<BaseProfessional> parentCode = baseProfessionalMapper.selectByParentCodes(baseProfessional);
                if (professionalCode.size() > 0 || professionalName.size() > 0) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、编码或专业名称" + baseProfessional.getProfessionalCode() + "、" + baseProfessional.getProfessionalName() + " 已存在");
                } else {
                    // bean对象属性认证
                    BeanValidators.validateWithException(validator, baseProfessional);
                    baseProfessionalMapper.insert(baseProfessional);
                    successNum++;
                    // successMsg.append("<br/>" + successNum + "、专业名称 " + baseProfessional.getProfessionalName() + " 导入成功");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、专业名称 " + baseProfessional.getProfessionalName() + " 导入失败：";
                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();
    }

    @Override
    public List<BaseProfessionalVo> selectBaseProfessionalVo(BaseProfessionalVo baseProfessionalVo) {
        List<BaseProfessional> allList = baseProfessionalMapper.selectBaseProfessional(baseProfessionalVo);
        // 将实体类转换为Vo类
        List<BaseProfessionalVo> allListVo = BeanUtils.entityListToVOList(allList, BaseProfessionalVo.class);
        List<BaseProfessionalVo> treeList = buildTree(allListVo);
        return treeList;
    }

    @Override
    public List<BaseProfessional> selectBaseProfessional(BaseProfessionalVo baseProfessionalVo) {
        List<BaseProfessional> allList = baseProfessionalMapper.selectBaseProfessional(baseProfessionalVo);
        return allList;
    }

    /**
     * 构建树形结构
     *
     * @return
     */
    public static List<BaseProfessionalVo> buildTree(List<BaseProfessionalVo> allList) {
        //创建集合封装最终数据
        List<BaseProfessionalVo> trees = new ArrayList<>();
        //遍历所有菜单集合
        for (BaseProfessionalVo baseProfessional : allList) {
            //找到递归入口，parentId=0
            if (baseProfessional.getParentCode().equals("0")) {
                trees.add(findChildren(baseProfessional, allList));
            }
        }
        return trees;
    }

    /**
     * 从根节点进行递归查询，查询子节点
     * 判断 编码 =  父级编码是否相同，如果相同是子节点，进行数据封装
     */
    private static BaseProfessionalVo findChildren(BaseProfessionalVo baseProfessional, List<BaseProfessionalVo> allList) {
        //数据初始化
        baseProfessional.setBaseProfessionalVoList(new ArrayList<BaseProfessionalVo>());
        //遍历递归查找
        for (BaseProfessionalVo it : allList) {
            if (baseProfessional.getProfessionalCode().equals(it.getParentCode())) {
                if (baseProfessional.getBaseProfessionalVoList() == null) {
                    baseProfessional.setBaseProfessionalVoList(new ArrayList<>());
                }
                baseProfessional.getBaseProfessionalVoList().add(findChildren(it, allList));
            }
        }
        return baseProfessional;
    }

}
