package com.hzya.frame.sys.tableCache.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sys.defultField.dao.IDefultFieldMapper;
import com.hzya.frame.sys.defultField.entity.DefultField;
import com.hzya.frame.sys.userViewFiled.dao.IUserViewFiledMapper;
import com.hzya.frame.sys.userViewFiled.entity.UserViewFiled;
import com.hzya.frame.sys.viewFiled.dao.IViewFiledMapper;
import com.hzya.frame.sys.viewFiled.entity.ViewFiled;
import com.hzya.frame.web.entity.BaseResult;
import org.springframework.stereotype.Service;
import com.hzya.frame.serviceUtil.ServiceUtil;
import com.hzya.frame.sys.tableCache.entity.TableCache;
import com.hzya.frame.sys.tableCache.service.ITableCacheService;
import com.hzya.frame.sys.tableCache.dao.ITableCacheMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hzya.frame.web.entity.JsonResultEntity;

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


/**
 * @description: 表缓存表  service
 * @tableName: sys_table_cache
 * @entityName: TableCache
 * @author: gjh
 * @history: 1.0
 */
@Service(value = "tablecacheService")
public class TableCacheServiceImpl extends ServiceUtil implements ITableCacheService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    protected ITableCacheMapper tablecacheMapper;
    @Resource
    protected IDefultFieldMapper defultfieldMapper;
    @Resource
    protected IViewFiledMapper viewfiledMapper;
    @Resource
    protected IUserViewFiledMapper userviewfiledMapper;
    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 根据模块查询个人表所有字段
     * @Date 4:57 下午 2023/6/14
     **/
    @Override
    public JsonResultEntity queryUserFiled(JSONObject object) {
        ViewFiled entity = getData("jsonStr", object, ViewFiled.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("模块为空");
        }
        if (entity.getServerName() == null || "".equals(entity.getServerName())) {
            return BaseResult.getFailureMessageEntity("服务名为空");
        }

        UserViewFiled userViewFiled = new UserViewFiled();
        userViewFiled.setSts("Y");
        userViewFiled.setUserId(String.valueOf(StpUtil.getLoginId()));
        List<UserViewFiled>  userViewFileds = userviewfiledMapper.entity_list_base(userViewFiled);
        entity.setUserViewFileds(userViewFileds);
        entity.setSts("Y");
        entity.setShowType("1");
        List<ViewFiled> viewFiledList = viewfiledMapper.entity_list_base(entity);
        return BaseResult.getSuccessMessageEntity("根据模块查询个人表所有字段", viewFiledList);
    }


    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 根据模块查询列表字段不包含系统字段
     * @Date 4:57 下午 2023/6/14
     **/
    @Override
    public JsonResultEntity queryFiled(JSONObject object) {
        ViewFiled entity = getData("jsonStr", object, ViewFiled.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("模块为空");
        }
        if (entity.getServerName() == null || "".equals(entity.getServerName())) {
            return BaseResult.getFailureMessageEntity("服务名为空");
        }
        entity.setSts("Y");
        entity.setShowType("1");
        List<ViewFiled> viewFiledList = viewfiledMapper.entity_list_base(entity);
        return BaseResult.getSuccessMessageEntity("根据模块查询所有表字段", viewFiledList);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询表并且添加到数据表
     * @Date 4:57 下午 2023/6/14
     **/
    @Override
    public JsonResultEntity queryTableCache(JSONObject object) {
        TableCache entity = getData("jsonStr", object, TableCache.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("对象为空");
        }
        if (entity.getTableName() == null || "".equals(entity.getTableName())) {
            return BaseResult.getFailureMessageEntity("表名为空");
        }
        if (entity.getTableService() == null || "".equals(entity.getTableService())) {
            return BaseResult.getFailureMessageEntity("服务名称为空");
        }
        List<ViewFiled> viewFiledList = viewfiledMapper.query_table_cache(entity);
        if (viewFiledList != null && viewFiledList.size() > 0) {
            DefultField defultField = new DefultField();
            defultField.setSts("Y");
            List<DefultField> defultFields = defultfieldMapper.entity_list_base(defultField);
            for (int i = 0; i < viewFiledList.size(); i++) {
                viewFiledList.get(i).setViewFiled(convertToCamelCase(viewFiledList.get(i).getViewFiled()));//驼峰转换
                if (checkSysFiled(viewFiledList.get(i).getDatebaseName(), defultFields)) {
                    viewFiledList.get(i).setShowType("2");//'展示类型 1、显示 2、不显示'
                    viewFiledList.get(i).setQueryType("2");//'查询类型 1、查询 2、不查询'
                    viewFiledList.get(i).setAddShowType("2");//'新增展示类型 1、显示 2、不显示'
                    viewFiledList.get(i).setAddRequiredType("2");//'新增是否必填类型 1、必填 2、不必填'
                    viewFiledList.get(i).setUpdateShowType("2");//'修改展示类型 1、显示 2、不显示'
                    viewFiledList.get(i).setUpdateRequiredType("2");//'修改是否必填类型 1、必填 2、不必填'
                } else {
                    viewFiledList.get(i).setShowType("1");//'展示类型 1、显示 2、不显示'
                    viewFiledList.get(i).setQueryType("1");//'查询类型 1、查询 2、不查询'
                    viewFiledList.get(i).setAddShowType("1");//'新增展示类型 1、显示 2、不显示'
                    viewFiledList.get(i).setAddRequiredType("1");//'新增是否必填类型 1、必填 2、不必填'
                    viewFiledList.get(i).setUpdateShowType("1");//'修改展示类型 1、显示 2、不显示'
                    viewFiledList.get(i).setUpdateRequiredType("1");//'修改是否必填类型 1、必填 2、不必填'
                }
                viewFiledList.get(i).setSorts(i + 1L);
                viewFiledList.get(i).setCreate();
                viewfiledMapper.entity_insert(viewFiledList.get(i));
            }
        }
        return BaseResult.getSuccessMessageEntity("根据模块查询所有表字段", viewFiledList);
    }

    /**
     * @param showType
     * @param defultFields
     * @return boolean
     * @Author lvleigang
     * @Description 判断是不是系统字段
     * @Date 11:02 上午 2023/6/15
     **/
    private boolean checkSysFiled(String showType, List<DefultField> defultFields) {
        if (defultFields != null && defultFields.size() > 0 && showType != null) {
            for (int i = 0; i < defultFields.size(); i++) {
                if (defultFields.get(i).getFieldName().equals(showType)) {
                    return true;
                }
            }
        } else {
            return false;
        }
        return false;
    }

    /**
     * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空，则返回空字符串
     *
     * @param name 转换前的下划线大写方式命名的字符串
     * @return 转换后的驼峰式命名的字符串
     */
    private String convertToCamelCase(String name) {
        StringBuilder result = new StringBuilder();
        // 快速检查
        if (name == null || name.isEmpty()) {
            // 没必要转换
            return "";
        } else if (!name.contains("_")) {
            // 不含下划线，仅将首字母大写
            return name.substring(0, 1).toLowerCase() + name.substring(1);
        }
        // 用下划线将原始字符串分割
        String[] camels = name.split("_");
        for (int i = 0; i < camels.length; i++) {
            // 跳过原始字符串中开头、结尾的下换线或双重下划线
            if (camels[i].isEmpty()) {
                continue;
            }
            if (i == 0) {
                result.append(camels[i].toLowerCase());
            } else {
                // 首字母大写
                result.append(camels[i].substring(0, 1).toUpperCase());
                result.append(camels[i].substring(1).toLowerCase());
            }
        }
        return result.toString();

    }

}
