package com.njcn.serviceimpl.configuration;

import com.njcn.mapper.configuration.DicDataTypeMapper;
import com.njcn.mapper.configuration.DicTypeTreeMapper;
import com.njcn.pojo.configuration.DicType;
import com.njcn.pojo.configuration.DicTypeTree;
import com.njcn.sso.pojo.user.User;
import com.njcn.service.configuration.DicDataTypeService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.PubUtils;
import com.njcn.utils.UserUtil;
import com.njcn.utils.redis.RedisCacheUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

@Service
public class DicDataTypeServiceImpl implements DicDataTypeService {
    @Resource
    DicDataTypeMapper ddt;
    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Autowired
    UserUtil uu;

    @Autowired
    DicTypeTreeMapper dicTypeTreeMapper;


    @Override
    public List<DicType> All(String typeId) {
        // TODO Auto-generated method stub
        return ddt.All(typeId);
    }

    @Override
    public int selNumMax() {
        // TODO Auto-generated method stub
        return ddt.selNumMax();
    }

    @Override
    public int deleteType(DicType dt) {
        // TODO Auto-generated method stub
        redisCacheUtil.clearDicData();
        return ddt.deleteType(dt);
    }

    @Override
    public int updateType(DicType dt) {
        // TODO Auto-generated method stub
        redisCacheUtil.clearDicData();
        return ddt.updateType(dt);
    }

    @Override
    public List<DicType> selTypeId(String dicTypeIndex) {
        // TODO Auto-generated method stub
        Example example = new Example(DicType.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("dicTypeIndex", dicTypeIndex);
        List<DicType> dicType = ddt.selectByExample(example);
        return dicType;
    }

    @Override
    public String saveType(String dicTypeName, String dicTypeDescription) {
        // TODO Auto-generated method stub
        //正则验证
        if(!StringUtils.isBlank(dicTypeName)) {
            if(PubUtils.patternTyPe(dicTypeName)) {
                DicType temp = new DicType();
                temp.setDicTypeName(dicTypeName);
                temp.setState(1);
                temp = ddt.selectOne(temp);
                if (null == temp) {
                    DicType dicType = new DicType();
                    dicType.setDicTypeIndex(UUID.randomUUID().toString());
                    dicType.setDicTypeName(dicTypeName);
                    dicType.setDicTypeNumber(ddt.selNumMax() + 1);
                    dicType.setDicTypeDiscribe(dicTypeDescription);
                    User user = TokenManager.getToken();
                    dicType.setUserIndex(user.getUserIndex());
                    dicType.setUpdateTime(new Date());
                    dicType.setState(1);//1为正常
                    try {
                        ddt.insert(dicType);
                        redisCacheUtil.clearDicData();
                        return "新增字典类型成功！";
                    } catch (Exception e) {
                        return "新增字典类型失败，请稍后再试。";
                    }
                } else {
                    return "字典类型名称已存在";
                }
            }else {
                return "字典类型由中文组成";
            }
        }else {
            return "字典类型不可为空";
        }
    }

    @Override
    public List<DicType> getLog(List<String> list) {
        // TODO Auto-generated method stub
        return ddt.getLog(list);
    }

    @Override
    public List<DicType> selectName(String dicTypeIndex) {
        // TODO Auto-generated method stub
        return ddt.selectName(dicTypeIndex);
    }

    @Override
    public DicType selectTypeName(String typeId) {
        // TODO Auto-generated method stub
        return ddt.selectTypeName(typeId);
    }

    @Override
    public List<DicType> selectAll() {
        // TODO Auto-generated method stub
        return ddt.selectIndexAll();
    }

    @Override
    public List<DicTypeTree> getTreeType() {
        List<DicTypeTree> list = new ArrayList<>();

        Map<Integer, Integer> value = new HashMap<Integer, Integer>();
        int p = 1000;
        List<DicTypeTree> one = dicTypeTreeMapper.getfirstData();

        if (one.isEmpty()) {
            DicTypeTree dt = new DicTypeTree();
            dt.setId(100);
            dt.setNodeType(0);
            dt.setName("通用字典");
            dt.setRdIndex("0");
            dt.setpId(0);
            dt.setPpId(0);
            list.add(dt);
            return list;
        }

        DicTypeTree dt = new DicTypeTree();
        dt.setId(100);
        dt.setNodeType(0);
        dt.setName("通用字典");
        dt.setRdIndex("0");
        dt.setpId(100);
        dt.setPpId(100);
        list.add(dt);

        List<Integer> first = new ArrayList<>();

        for (int i = 0; i < one.size(); i++) {
            DicTypeTree dto = new DicTypeTree();
            dto.setId(++p);
            dto.setName(one.get(i).getName());
            dto.setNodeType(1);
            dto.setpId(100);
            dto.setPpId(100);
            dto.setRdIndex(one.get(i).getRdIndex());
            list.add(dto);
        }
        return list;
    }
}
