package com.cf.imes.module.system.controller.admin.datasource.dataQuery;


import com.cf.imes.module.system.dal.mysql.machine.MachineMapper;
import com.cf.imes.module.system.dal.mysql.organ.OrganMapper;
import com.cf.imes.module.system.dal.mysql.user.AdminUserMapper;
import org.springframework.context.annotation.Configuration;

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

import static com.cf.imes.framework.common.util.json.JsonUtils.toJsonString;
import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getUserOrganId;



/**
 * @author Administrator
 */
@Configuration
public class DataQuery {

    String SEPARATE = "select";

    @Resource
    private OrganMapper organMapper;


    @Resource
    private AdminUserMapper userMapper;

    @Resource
    private MachineMapper machineMapper;



    public String dataGetOrgan(String filed) {

//        String sql = "select   from system_organization where id = #{id}  and deleted = false";
//
//        sql = sql.replace("#{id}", String.valueOf(getUserOrganId()));


        return toJsonString(organMapper.selectOrganDataSource(conversion(filed),getUserOrganId()));

    }





    public String dataGetUser(String filed, List<String> nameList) {


//        String sql = "select  from system_users where nickname in (#{nameList}) and deleted = false  and organ_id = #{organId}";
//
//        sql = sql.replace("#{organId}", getUserOrganId() + "").replace("#{orderIds}", nameList.toString().replaceAll("\\[|\\]", ""));


        return toJsonString(userMapper.selectUserDataSource(conversion(filed),getUserOrganId(),nameList));



    }


    public String dataGetMachine(String filed,Long machineId) {


        return toJsonString(machineMapper.selectMachineDataSource(conversion(filed),machineId,getUserOrganId()));

    }


    // 字符串处理
    private String conversion(String filed) {

        // 去除字段中的 【 】
        return filed.replaceAll("\\[|\\]", "");

        // 使用正则表达式在单词后插入字符串
//        return sql.replaceAll(SEPARATE + "(\\b)", SEPARATE + " " + toInsert);

    }


}
