package com.njcn.serviceimpl.configuration;

import com.njcn.enums.DicDataTypeEnum;
import com.njcn.mapper.configuration.DicDataMapper;
import com.njcn.mapper.configuration.DicDataTypeMapper;
import com.njcn.mapper.user.DeptsMapper;
import com.njcn.mapper.user.UserMapper;
import com.njcn.pojo.configuration.DicData;
import com.njcn.pojo.configuration.DicType;
import com.njcn.pojo.user.Depts;
import com.njcn.sso.pojo.user.User;
import com.njcn.service.configuration.DicDataService;
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

@Service
public class DicDataServiceImpl implements DicDataService {
    @Resource
    DicDataMapper ddm;
    @Autowired
    private RedisCacheUtil redisCacheUtil;
    @Autowired
    UserUtil uu;
    @Autowired
    DicDataTypeMapper dicDataTypeMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    DeptsMapper deptsMapper;

    @Override
    public List<List> selAll(String typeId) {
        // TODO Auto-generated method stub
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<List> dicDataList = new ArrayList<List>();
        List<DicData> datas = ddm.selAll(typeId);
        if (datas.isEmpty()) {
            return dicDataList;
        } else {
            for (DicData data : datas) {
                List<String> temp = new ArrayList<String>();
                temp.add(data.getDicName());
                DicType dicType = new DicType();
                dicType.setDicTypeIndex(data.getDicType());
                dicType = dicDataTypeMapper.selectOne(dicType);
                String typeName = dicType.getDicTypeName();
                temp.add(typeName);
                temp.add(sdf.format(data.getUpdateTime()));
                User user = new User();
                user.setUserIndex(data.getUserIndex());
                user = userMapper.selectOne(user);
                if (user == null) {
                    temp.add("暂无用户");
                } else {
                    temp.add(user.getLoginName());
                }
                if (typeName.equals(DicDataTypeEnum.emergencyEnum.getName()) || typeName.equals(DicDataTypeEnum.serviceEnum.getName())
                        || typeName.equals(DicDataTypeEnum.systemEnum.getName())) {
                    int leavl = data.getDicLeave();
                    if (leavl == 0) {
                        temp.add("普通");
                    } else if (leavl == 1) {
                        temp.add("中等");
                    } else {
                        temp.add("严重");
                    }
                } else {
                    temp.add("/");
                }
                temp.add(
                        "<button type='button' class='btn  btn-primary' style='margin-right:10px;' href='javascript:void(0);' onclick='updateDataByIndex(\"" + data.getDicIndex() + "\")' ><i class='fa fa-edit'></i>修改</button>"
                                + "<button type='button' class='btn  btn-danger' style='margin-right:10px;' href='javascript:void(0);' onclick='deleteDataByIndex(\"" + data.getDicIndex() + "\")' ><i class='fa fa-trash'></i>删除</button>"
                );
                dicDataList.add(temp);

            }
        }
        return dicDataList;
    }

    @Override
    public List<List> seleAll(String name) {
        // TODO Auto-generated method stub
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (name == null) {
            name = "";
        }
        name = "%" + name + "%";
        List<List> dicDataList = new ArrayList<List>();
        List<DicData> datas = ddm.seleAll(name);
        if (datas.isEmpty()) {
            return dicDataList;
        } else {
            for (DicData data : datas) {
                List<String> temp = new ArrayList<String>();
                temp.add(data.getDicName());
                DicType dicType = new DicType();
                dicType.setDicTypeIndex(data.getDicType());
                dicType = dicDataTypeMapper.selectOne(dicType);
                String typeName = dicType.getDicTypeName();
                temp.add(typeName);
                temp.add(sdf.format(data.getUpdateTime()));
                User user = new User();
                user.setUserIndex(data.getUserIndex());
                user = userMapper.selectOne(user);
                if (user == null) {
                    temp.add("暂无用户");
                } else {
                    temp.add(user.getLoginName());
                }
                if (typeName.equals(DicDataTypeEnum.emergencyEnum.getName()) || typeName.equals(DicDataTypeEnum.serviceEnum.getName())
                        || typeName.equals(DicDataTypeEnum.systemEnum.getName())) {
                    int leavl = data.getDicLeave();
                    if (leavl == 0) {
                        temp.add("普通");
                    } else if (leavl == 1) {
                        temp.add("中等");
                    } else {
                        temp.add("严重");
                    }
                } else {
                    temp.add("/");
                }
                temp.add(
                        "<button type='button' class='btn  btn-primary' style='margin-right:10px;' href='javascript:void(0);' onclick='updateDataByIndex(\"" + data.getDicIndex() + "\")' ><i class='fa fa-edit'></i>修改</button>"
                                + "<button type='button' class='btn  btn-danger' style='margin-right:10px;' href='javascript:void(0);' onclick='deleteDataByIndex(\"" + data.getDicIndex() + "\")' ><i class='fa fa-trash'></i>删除</button>"
                );
                dicDataList.add(temp);
            }
        }
        return dicDataList;
    }

    @Override
    public List<DicData> getMangType() {
        return ddm.getMangType(DicDataTypeEnum.resourceEnum.getName());
    }

    @Override
    public List<DicData> getType() {
        return ddm.getMangType(DicDataTypeEnum.prepositionEnum.getName());
    }

    @Override
    public List<DicData> getSystemType() {
        return ddm.getMangType(DicDataTypeEnum.systemTypeEnum.getName());
    }

    @Override
    public String getDicName(String dicIndex) {
        return ddm.getDicName(dicIndex);
    }

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

    @Override
    public int updateData(DicData dic) {
        // TODO Auto-generated method stub
        redisCacheUtil.clearDicData();
        redisCacheUtil.clearConData();
        return ddm.updateData(dic);
    }

    @Override
    public DicData selectId(String dicIndex) {
        // TODO Auto-generated method stub

        return ddm.selectId(dicIndex);
    }

    @Override
    public int selCount(String dicTypeIndex) {
        // TODO Auto-generated method stub
        return ddm.selCount(dicTypeIndex);
    }

    @Override
    public String saveData(String dicName, String dicType, int leave, int triphase) {
        // TODO Auto-generated method stub
        //字典名不能重复
        //正则表达
        if (!StringUtils.isBlank(dicName)) {
            if (PubUtils.patternDic(dicName)) {
                Example example = new Example(DicData.class);
                example.createCriteria().andEqualTo("dicType", dicType)
                .andEqualTo("dicName", dicName)
                .andEqualTo("state", 1);
                List<DicData> data = ddm.selectByExample(example);
                if (data.isEmpty()) {
                    DicData dicData = new DicData();
                    dicData.setDicIndex(UUID.randomUUID().toString());
                    dicData.setDicName(dicName);
                    dicData.setDicType(dicType);
                    dicData.setDicNumber(ddm.selNumMax() + 1);
                    dicData.setUpdateTime(new Date());
                    dicData.setDicLeave(leave);
                    dicData.setState(1);//1为正常
                    dicData.setTriphase(triphase);
                    User user = TokenManager.getToken();
                    dicData.setUserIndex(user.getUserIndex());
                    try {
                        int flag = ddm.insert(dicData);
                        redisCacheUtil.clearDicData();
                        redisCacheUtil.clearConData();
                        return "新增字典成功！";
                    } catch (Exception e) {
                        return "新增字典失败，请稍后再试。";
                    }
                } else {
                    return "字典名称已存在";
                }
            } else {
                return "字典名称包括中文、数字、字母、括号以及点号";
            }
        } else {
            return "字典名称不可为空";
        }

    }

    @Override
    public String deleteDataByIndex(String index) {
        // TODO Auto-generated method stub
        //先判断字典是否存在
        DicData temp = new DicData();
        temp = ddm.selectByPrimaryKey(index);
        if (null == temp) {
            return "该字典不存在，已删除";
        } else {
            try {
                //判断该行政区域是否被使用
                Depts depts = new Depts();
                depts.setArea(index);
                depts.setState(1);
                depts = deptsMapper.selectOne(depts);
                if (depts == null) {
                    ddm.deleteData(temp);
                    redisCacheUtil.clearDicData();
                    redisCacheUtil.clearConData();
                    return "删除字典成功！";
                } else {
                    return "删除字典失败！，该行政区域已被部门绑定";
                }
            } catch (Exception e) {
                return "删除字典失败，请稍后再试";
            }
        }
    }

    @Override
    public DicData getSelName(String name) {
        // TODO Auto-generated method stub
        return ddm.getSelName(name);
    }

    @Override
    public List<DicData> selAllId(String typeId) {
        // TODO Auto-generated method stub
        return ddm.selAllId(typeId);
    }

    @Override
    public List<DicData> selAllType(List<String> typeId) {
        // TODO Auto-generated method stub
        return ddm.selAllType(typeId);
    }

    @Override
    public List<DicData> setArea() {
        // TODO Auto-generated method stub
        return ddm.setArea(TokenManager.getSysType(), DicDataTypeEnum.areaEnum.getName());
    }

    @Override
    public List<DicData> selectName(String dicIndex, String dicType) {
        // TODO Auto-generated method stub
        return ddm.selectName(dicIndex, dicType);
    }

    @Override
    public List<DicData> selectWring() {
        // TODO Auto-generated method stub
        return ddm.selectWring(DicDataTypeEnum.transFormerEnum.getName());
    }

    @Override
    public List<DicData> selectDataName(String dicIndex) {
        // TODO Auto-generated method stub
        return ddm.selectDataName(dicIndex);
    }

    @Override
    public List<DicData> selectWiring(String typeId) {
        // TODO Auto-generated method stub
        return ddm.selectWiring(typeId, DicDataTypeEnum.transFormerEnum.getName());
    }

    @Override
    public List<DicData> selectdicName(String dicName) {
        // TODO Auto-generated method stub
        return ddm.selectdicName(dicName);
    }
}
