package com.kxs.adminap.service.main;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.kxs.adminap.config.Base;
import com.kxs.adminap.enity.AppResultJson;
import com.kxs.adminap.enity.RelationData;
import com.kxs.adminap.model.Users;
import com.kxs.adminap.util.DbService;

public class UsersService
{

    /// <summary>
    /// 查询列表
    /// </summary>
    /// <param name="relationData">关联表</param>
    /// <param name="condition">查询条件(sql语句)</param>
    /// <param name="count">总数（输出）</param>
    /// <param name="page">页码</param>
    /// <param name="limit">每页条数</param>
    /// <returns></returns>
    public static List<Dictionary<String, Object>> List(List<RelationData> relationData, String condition, Integer count, Integer page, Integer limit, String orderBy)
    {
        List<String> fields = new ArrayList<>(); //要显示的列
        fields.add("Id");
        fields.add("CreateDate"); //注册时间
        fields.add("Status"); //状态
        fields.add("MakerCode"); //创客编号
        fields.add("RealName"); //真实姓名
        fields.add("Mobile"); //手机号
        fields.add("CertId"); //身份证号
        fields.add("UserLevel"); //创客等级
        fields.add("AgentAreas"); //代理区域
        fields.add("MerchantType"); //商户创客类型
        fields.add("MerchantDate"); //成为商户型创客时间
        fields.add("AuthFlag"); //实名标识
        fields.add("AuthDate"); //实名时间
        fields.add("RiskFlag"); //风控标识
        fields.add("ParentUserId"); //直属创客Id
        fields.add("ParentNav"); //父级集合
        // fields.add("SettleAmount"); //最低结算金额
        // fields.add("CashFreezeAmt"); //提现冻结金额
        // fields.add("CertValidStartDate"); //身份证有效期开始时间
        // fields.add("CertValidEndDate"); //身份证有效期结束时间
        // fields.add("SettleBankCardName"); //结算银行卡户名
        // fields.add("AgentLevel"); //代理等级
        // fields.add("CashStatus"); //代付账户状态
        // fields.add("LeaderLevel"); //盟主等级
        // fields.add("BusinessFlag"); //企业标记
        // fields.add("DeviceType"); //设备类型
        // fields.add("HelpProfitAgree"); //助利宝协议标记
        // fields.add("HelpProfitAgreeDate"); //助利宝协议标记时间

        Dictionary<String, Object> obj = new DbService(Base.main_tables, Base.main_url, Base.main_username, Base.main_password).IndexData("Users", relationData, orderBy, page, limit, condition, fields);
        Object data = obj.get("data");
        List<Dictionary<String, Object>> diclist = (List<Dictionary<String, Object>>) data;
        count = (Integer)obj.get("count");
        return diclist;
    }
    public static List<Dictionary<String, Object>> List(List<RelationData> relationData, String condition, Integer page, Integer limit, String orderBy)
    {
        List<String> fields = new ArrayList<>(); //要显示的列
        fields.add("Id");
        fields.add("CreateDate"); //添加时间
        fields.add("Status"); //状态
        fields.add("RealName"); //真实姓名
        fields.add("CertId"); //身份证号
        fields.add("UserLevel"); //创客等级
        fields.add("MerchantType"); //商户创客类型
        fields.add("MerchantDate"); //成为商户型创客时间
        fields.add("AuthFlag"); //实名标识
        fields.add("RiskFlag"); //风控标识
        fields.add("MakerCode"); //创客编号
        fields.add("SettleAmount"); //最低结算金额
        fields.add("CashFreezeAmt"); //提现冻结金额
        fields.add("CertValidStartDate"); //身份证有效期开始时间
        fields.add("CertValidEndDate"); //身份证有效期结束时间
        fields.add("SettleBankCardName"); //结算银行卡户名
        fields.add("AgentLevel"); //代理等级
        fields.add("AgentAreas"); //代理区域
        fields.add("CashStatus"); //代付账户状态
        fields.add("LeaderLevel"); //盟主等级
        fields.add("BusinessFlag"); //企业标记
        fields.add("DeviceType"); //设备类型
        fields.add("HelpProfitAgree"); //助利宝协议标记
        fields.add("HelpProfitAgreeDate"); //助利宝协议标记时间

        Dictionary<String, Object> obj = new DbService(Base.main_tables, Base.main_url, Base.main_username, Base.main_password).IndexData("Users", relationData, orderBy, page, limit, condition, fields);
        Object data = obj.get("data");
        List<Dictionary<String, Object>> diclist = (List<Dictionary<String, Object>>) data;
        return diclist;
    }

    /// <summary>
    /// 查询一条记录
    /// </summary>
    /// <param name="Id">主键Id</param>
    /// <returns></returns>
    public static Users Query(Integer Id, String fields)
    {
        if(fields.isEmpty()) fields = "*";
        var item = new DbService(Base.main_tables, Base.main_url, Base.main_username, Base.main_password).Query(fields, "Users", Id);
        if (item.size() > 0)
        {
            return JSONObject.parseObject(JSONObject.toJSONString(item), Users.class);
        }
        return new Users();
    }

    public static Users Query(String condition, String fields)
    {
        if(fields.isEmpty()) fields = "*";
        var item = new DbService(Base.main_tables, Base.main_url, Base.main_username, Base.main_password).Query(fields, "Users", condition);
        if (item.size() > 0)
        {
            return JSONObject.parseObject(JSONObject.toJSONString(item), Users.class);
        }
        return new Users();
    }

    /// <summary>
    /// 通过创客编号查询一条记录
    /// </summary>
    /// <param name="MakerCode">创客编号</param>
    /// <returns></returns>
    public static Users QueryByMakerCode(String MakerCode)
    {
        return Query("MakerCode='" + MakerCode + "'", "*");
    }

    /// <summary>
    /// 查询记录数
    /// </summary>
    /// <param name="Id">主键Id</param>
    /// <returns></returns>
    public static Integer Count(String condition)
    {
        Integer result = 0;
        var item = new DbService(Base.main_tables, Base.main_url, Base.main_username, Base.main_password).Query("count(Id) count", "Users", condition);
        if (item.size() > 0)
        {
            result = (Integer)item.get("count");
        }
        return result;
    }

    /// <summary>
    /// 查询是否存在
    /// </summary>
    /// <param name="Id">主键Id</param>
    /// <returns></returns>
    public static Boolean Exist(int Id)
    {
        var item = new DbService(Base.main_tables, Base.main_url, Base.main_username, Base.main_password).Query("1", "Users", "Id=" + Id);
        if (item.size() > 0)
        {
            return true;
        }
        return false;
    }

    /// <summary>
    /// 添加数据
    /// </summary>
    /// <param name="Fields">要设置的字段</param>
    /// <returns></returns>
    public static AppResultJson Add(Map<String, Object> fields)
    {
        int Id = new DbService(Base.main_tables, Base.main_url, Base.main_username, Base.main_password).Add("Users", fields, 0, true);
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = Id;
        return result;
    }

    /// <summary>
    /// 修改数据
    /// </summary>
    /// <param name="Fields">要设置的字段</param>
    /// <param name="Id">主键Id</param>
    public static AppResultJson Edit(Map<String, Object> fields, Integer Id)
    {
        new DbService(Base.main_tables, Base.main_url, Base.main_username, Base.main_password).Edit("Users", fields, Id);
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = Id;
        return result;
    }

    /// <summary>
    /// 逻辑删除
    /// </summary>
    /// <param name="Id">主键Id</param>
    public static void Remove(int Id)
    {
        Map<String, Object> fields = new HashMap<>();
        fields.put("Status", -1);
        new DbService(Base.main_tables, Base.main_url, Base.main_username, Base.main_password).Edit("Users", fields, Id);
    }

    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="Id">主键Id</param>
    public static void Delete(int Id)
    {
        new DbService(Base.main_tables, Base.main_url, Base.main_username, Base.main_password).Delete("Users", Id);
    }

    /// <summary>
    /// 排序
    /// </summary>
    /// <param name="Id">主键Id</param>
    /// <param name="Sort">排序序号</param>
    public static void Sort(int Id, int Sort)
    {
        new DbService(Base.main_tables, Base.main_url, Base.main_username, Base.main_password).Sort("Users", Sort, Id);
    }

    /// <summary>
    /// 导入数据
    /// </summary>
    /// <param name="ExcelData">json数据</param>
    public static void Import(String ExcelData)
    {
    }
}