package cn.muchinfo.rma.business.common

import cn.muchinfo.rma.global.data.ChangeLogData
import cn.muchinfo.rma.global.data.CommonTableData
import cn.muchinfo.rma.global.data.ContractData
import cn.muchinfo.rma.global.data.NoticeData
import cn.muchinfo.rma.netManage.base.ResponseCallback
import cn.muchinfo.rma.netManage.utils.MyOkHttpUtils
import cn.muchinfo.rma.view.base.app.BaseResult
import cn.muchinfo.rma.view.base.app.Constant
import com.blankj.utilcode.util.SPUtils
import okhttp3.Call
import java.lang.Exception

/**
 * 公共管理类
 */
class CommonManager {

    /**
     * 通用表头请求
     * @param params Map<String, String> 参数tableKey 表key， tableType 列表类型 - 1：管理端 2：H5终端 3：移动终端
     * @param responseBack Function3<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] List<CommonTableData>?, [@kotlin.ParameterName] Error?, Unit>
     */
    fun queryTableDefine(
        params: Map<String, String>,
        responseBack: (isSuccess: Boolean, respData: List<CommonTableData>?, error: Error?) -> Unit
    ){
        MyOkHttpUtils().query(URL = SPUtils.getInstance().getString(Constant.goCommonSearchUrl) + "/Common/QueryTableDefine",
            params = params,
            type = "1",
            callback = object : ResponseCallback<BaseResult<List<CommonTableData>>>() {
                override fun onResponse(response: BaseResult<List<CommonTableData>>?, id: Int) {
                    responseBack(true, response?.data, null)
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {
                    responseBack(false, null, Error(e?.message))
                }
            }
        )
    }

    /**
     * 查询合同（采购和销售）
     * @param params Map<String, String> userId ，contracttype（合同类型 1-采购, -1-销售），querytype（查询类型 1-全部 2-待点价 3-履约结算）
     * @param responseBack Function3<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] List<ContractData>?, [@kotlin.ParameterName] Error?, Unit>
     */
    fun queryContract(
        params: Map<String, String>,
        responseBack: (isSuccess: Boolean, respData: List<ContractData>?, error: Error?) -> Unit
    ) {
        MyOkHttpUtils().query(
            URL = SPUtils.getInstance().getString(Constant.goCommonSearchUrl) + "/Ermcp/QueryContract",
            params = params,
            type = "1",
            callback = object : ResponseCallback<BaseResult<List<ContractData>>>(){
                override fun onResponse(response: BaseResult<List<ContractData>>?, id: Int) {
                    responseBack(true,response?.data,null)
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {
                    responseBack(false, null, Error(e?.message))
                }

            }
        )
    }

    /**
     * 通知公告系统消息查询
     * @param params Map<String, String>
     * @param responseBack Function3<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] List<NoticeData>?, [@kotlin.ParameterName] Error?, Unit>
     */
    fun queryNotice(
        params: Map<String, String>,
        responseBack: (isSuccess: Boolean, respData: List<NoticeData>?, error: Error?) -> Unit
    ){
        MyOkHttpUtils().query(
            URL = SPUtils.getInstance().getString(Constant.goCommonSearchUrl) + "/Common/QueryNotice",
            params = params,
            type = "1",
            callback = object : ResponseCallback<BaseResult<List<NoticeData>>>(){
                override fun onResponse(response: BaseResult<List<NoticeData>>?, id: Int) {
                    responseBack(true,response?.data,null)
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {
                    responseBack(false, null, Error(e?.message))
                }

            }
        )
    }

    /**
     * 查询变更记录
     * @param params Map<String, String> RelatedId 合同ID
     * @param responseBack Function3<[@kotlin.ParameterName] Boolean, [@kotlin.ParameterName] List<NoticeData>?, [@kotlin.ParameterName] Error?, Unit>
     */
    fun queryChangeLog(
        params: Map<String, String>,
        responseBack: (isSuccess: Boolean, respData: List<ChangeLogData>?, error: Error?) -> Unit
    ){

        MyOkHttpUtils().query(
            URL = SPUtils.getInstance().getString(Constant.goCommonSearchUrl) + "/Ermcp/QueryChangeLog",
            params = params,
            type = "1",
            callback = object : ResponseCallback<BaseResult<List<ChangeLogData>>>(){
                override fun onResponse(response: BaseResult<List<ChangeLogData>>?, id: Int) {
                    responseBack(true,response?.data,null)
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {
                    responseBack(false, null, Error(e?.message))
                }

            }
        )
    }

}