package com.kxs.adminap.controller;

import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.kxs.adminap.enity.AppResultJson;
import com.kxs.adminap.enity.RelationData;
import com.kxs.adminap.model.Users;
import com.kxs.adminap.service.main.UsersService;
import com.kxs.adminap.util.DesUtil;
import com.kxs.adminap.util.RedisUtils;
import com.kxs.adminap.util.UsersUtil;
import com.kxs.adminap.util.Util;

import jakarta.annotation.Resource;

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

@RestController
public class UsersController
{
    @Resource
	RedisUtils redisUtils;

    //根据条件查询创客列表

    /// <summary>
    /// 创客列表
    /// </summary>
    /// <returns></returns>
    @PostMapping("/main/users/all")
    public @ResponseBody AppResultJson List(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject data = JSONObject.parseObject(value);
        Integer page = Integer.parseInt(data.getString("PageNum").toString());
        Integer limit = Integer.parseInt(data.getString("PageSize").toString());
        String condition = " and Status>-1"; //查询条件
        // String CreateDate = data.getString("CreateDate").toString(); //昵称
        String RealName = data.getString("RealName").toString(); //真实姓名
        String CertId = data.getString("CertId").toString(); //身份证号
        String Mobile = data.getString("Mobile").toString(); //手机号
        String MakerCode = data.getString("MakerCode").toString(); //创客编号
        Integer CheckParent = Integer.parseInt(Util.CheckInt(data.getString("CheckParent").toString())); //是否查询上级创客(默认为0或空 否 当前选中数据的UserId)
        Integer CheckChild = Integer.parseInt(Util.CheckInt(data.getString("CheckChild").toString())); //是否查询直属创客(默认为0或空 否 当前选中数据的UserId)
        // String SettleAmount = data.getString("SettleAmount").toString(); //最低结算金额
        // String CashFreezeAmt = data.getString("CashFreezeAmt").toString(); //提现冻结金额
        // String DeviceType = data.getString("DeviceType").toString(); //设备类型

        // if (!Util.IsNullOrEmpty(CreateDate))
        // {
        //     string[] dates = CreateDate.Split(new string[] { " - " }, StringSplitOptions.None);
        //     condition += " and CreateDate>='" + dates[0] + "' and CreateDate<='" + dates[1] + "'";
        // }
        if (CheckParent > 0)
        {
            var self = UsersService.Query(CheckParent, "*");
            if (self != null)
            {
                String ParentNav = Util.CheckNull(self.ParentNav);
                if (!ParentNav.isEmpty())
                {
                    condition += " and Id in (" + ParentNav.substring(1, ParentNav.length() - 2).replaceAll(",,", ",") + ")";
                }
                else
                {
                    condition += " and Id=0";
                }
            }
        }
        else if (CheckChild > 0)
        {
            condition += " and ParentUserId=" + CheckChild;
        }
        else
        {
            if (!Util.IsNullOrEmpty(RealName))
            {
                condition += " and RealName like '%" + RealName + "%'";
            }
            if (!Util.IsNullOrEmpty(CertId))
            {
                condition += " and CertId like '%" + CertId + "%'";
            }
            if (!Util.IsNullOrEmpty(Mobile))
            {
                condition += " and Mobile='" + Mobile + "'";
            }
            if (!Util.IsNullOrEmpty(MakerCode))
            {
                condition += " and MakerCode like '%" + MakerCode + "%'";
            }
        }

        //关联表数据
        List<RelationData> relationData = new ArrayList<>();
        RelationData RelationDataItem = new RelationData();
        RelationDataItem.Field = "ParentUserId";
        RelationDataItem.Table = "Users";
        List<String> ReturnField = new ArrayList<>();
        ReturnField.add("MakerCode");
        ReturnField.add("RealName");
        RelationDataItem.ReturnField = ReturnField;
        relationData.add(RelationDataItem);

        Integer count = 0;
        List<Dictionary<String, Object>> list = UsersService.List(relationData, condition, count, page, limit, "Sort desc,Id desc");
        for (Dictionary<String, Object> dic : list)
        {
            if (!Util.IsNullOrEmpty(dic.get("ParentNav").toString()))
            {
                String ParentNav = dic.get("ParentNav").toString();
                String[] ParentNavList = ParentNav.substring(1, ParentNav.length() - 2).replaceAll(",,", ",").split(",");
                var topUserInfo = new Users();
                if (ParentNavList.length > 1)
                {
                    topUserInfo = UsersUtil.GetTopUserInfo(dic.get("ParentNav").toString(), 1);
                }
                else
                {
                    topUserInfo = UsersUtil.GetTopUserInfo(dic.get("ParentNav").toString(), 0);
                }
                dic.put("TopMakerCode", topUserInfo.MakerCode);
                dic.put("TopRealName", topUserInfo.RealName);
            }
            else
            {
                dic.put("TopMakerCode", "");
                dic.put("TopRealName", "");
            }
        }
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = list;
        result.Other = count;
        return result;
    }


    //根据条件查询上级创客列表

    /// <summary>
    /// 根据条件查询上级创客列表
    /// </summary>
    /// <returns></returns>
    @PostMapping("/main/users/parentlist")
    public @ResponseBody AppResultJson ParentList(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject data = JSONObject.parseObject(value);
        Integer page = Integer.parseInt(data.getString("PageNum").toString());
        Integer limit = Integer.parseInt(data.getString("PageSize").toString());
        String RealName = data.getString("RealName").toString(); //真实姓名
        String CertId = data.getString("CertId").toString(); //身份证号
        String Mobile = data.getString("Mobile").toString(); //手机号
        String MakerCode = data.getString("MakerCode").toString(); //创客编号
        Integer ParentUserId = Integer.parseInt(data.getString("ParentUserId").toString()); //上级创客Id
        String condition = " and Status>-1 and ParentUserId=" + ParentUserId; //查询条件
        if (!Util.IsNullOrEmpty(RealName))
        {
            condition += " and RealName like '%" + RealName + "%'";
        }
        if (!Util.IsNullOrEmpty(CertId))
        {
            condition += " and CertId like '%" + CertId + "%'";
        }
        if (!Util.IsNullOrEmpty(Mobile))
        {
            condition += " and Mobile='" + Mobile + "'";
        }
        if (!Util.IsNullOrEmpty(MakerCode))
        {
            condition += " and MakerCode like '%" + MakerCode + "%'";
        }
        //关联表数据
        List<RelationData> relationData = new ArrayList<>();
        RelationData RelationDataItem = new RelationData();
        RelationDataItem.Field = "ParentUserId";
        RelationDataItem.Table = "Users";
        List<String> ReturnField = new ArrayList<>();
        ReturnField.add("MakerCode");
        ReturnField.add("RealName");
        RelationDataItem.ReturnField = ReturnField;
        relationData.add(RelationDataItem);

        Integer count = 0;
        List<Dictionary<String, Object>> list = UsersService.List(relationData, condition, count, page, limit, "Sort desc,Id desc");
        for (Dictionary<String, Object> dic : list)
        {
            // dic["LevelName"] = UserLevelSetService.Query(Integer.parseInt(dic["UserLevel"].toString())).Name;

            // //实名状态
            // dic["AuthFlagName"] = dic["AuthFlag"].toString() == "1" ? "已实名" : "未实名";

            // //风控状态
            // dic["RiskFlagName"] = dic["RiskFlag"].toString() == "1" ? "已风控" : "未风控";

            // //商户创客
            // dic["MerchantTypeName"] = Func.GetMerchantTypeInfo(Integer.parseInt(dic["MerchantType"].toString()));


        }
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = list;
        result.Other = count;
        return result;
    }


    //根据条件查询直属创客列表

    /// <summary>
    /// 根据条件查询直属创客列表
    /// </summary>
    /// <returns></returns>
    @RequestMapping("/main/users/childlist")
    public @ResponseBody AppResultJson ChildList(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject data = JSONObject.parseObject(value);
        Integer page = Integer.parseInt(data.getString("PageNum").toString());
        Integer limit = Integer.parseInt(data.getString("PageSize").toString());
        String RealName = data.getString("RealName").toString(); //真实姓名
        String CertId = data.getString("CertId").toString(); //身份证号
        String Mobile = data.getString("Mobile").toString(); //手机号
        String MakerCode = data.getString("MakerCode").toString(); //创客编号
        Integer UserId = Integer.parseInt(data.getString("ParentUserId").toString()); //创客Id
        String condition = " and Status>-1 and ParentUserId=" + UserId; //查询条件
        if (!Util.IsNullOrEmpty(RealName))
        {
            condition += " and RealName like '%" + RealName + "%'";
        }
        if (!Util.IsNullOrEmpty(CertId))
        {
            condition += " and CertId like '%" + CertId + "%'";
        }
        if (!Util.IsNullOrEmpty(Mobile))
        {
            condition += " and Mobile='" + Mobile + "'";
        }
        if (!Util.IsNullOrEmpty(MakerCode))
        {
            condition += " and MakerCode like '%" + MakerCode + "%'";
        }
        //关联表数据
        List<RelationData> relationData = new ArrayList<>();
        RelationData RelationDataItem = new RelationData();
        RelationDataItem.Field = "ParentUserId";
        RelationDataItem.Table = "Users";
        List<String> ReturnField = new ArrayList<>();
        ReturnField.add("MakerCode");
        ReturnField.add("RealName");
        RelationDataItem.ReturnField = ReturnField;
        relationData.add(RelationDataItem);

        Integer count = 0;
        List<Dictionary<String, Object>> list = UsersService.List(relationData, condition, count, page, limit, "Sort desc,Id desc");
        for (Dictionary<String, Object> dic : list)
        {
            // //创客等级
            // dic["LevelName"] = UserLevelSetService.Query(Integer.parseInt(dic["UserLevel"].toString())).Name;

            // //实名状态
            // dic["AuthFlagName"] = dic["AuthFlag"].toString() == "1" ? "已实名" : "未实名";

            // //风控状态
            // dic["RiskFlagName"] = dic["RiskFlag"].toString() == "1" ? "已风控" : "未风控";

            // //商户创客
            // dic["MerchantTypeName"] = Func.GetMerchantTypeInfo(Integer.parseInt(dic["MerchantType"].toString()));


        }
        // 返回字段：
        // MakerCode //创客编号
        // RealName //真实姓名
        // LevelName //创客等级
        // Mobile //手机号
        // CertId //身份证号
        // AgentAreas //展业区域
        // MerchantTypeName //商户创客类型
        // MerchantDate //成为商户型创客时间
        // AuthFlagName //实名状态
        // CreateDate //注册时间
        // AuthDate //实名时间
        // ParentUserIdMakerCode //直属创客编号
        // ParentUserIdRealName //直属创客名称
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = list;
        result.Other = count;
        return result;
    }


    //根据条件查询直属创客交易额列表

    /// <summary>
    /// 根据条件查询直属创客交易额列表
    /// </summary>
    /// <returns></returns>
    @RequestMapping("/main/users/childamountrecordlist")
    public @ResponseBody AppResultJson ChildAmountRecordList(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject data = JSONObject.parseObject(value);
        Integer page = Integer.parseInt(data.getString("PageNum").toString());
        Integer limit = Integer.parseInt(data.getString("PageSize").toString());
        Integer UserId = Integer.parseInt(Util.CheckInt(data.getString("UserId").toString())); // 创客Id
        Integer BrandId = Integer.parseInt(Util.CheckInt(data.getString("BrandId").toString())); //品牌
        String MakerCode = data.getString("MakerCode").toString(); //创客编号
        String CreateDateData = data.getString("CreateDateData").toString(); //交易时间

        String start = "";
        String end = "";

        String condition = " and Status>-1 and AuthFlag >0 and ParentUserId=" + UserId;
        String con = " and Status>-1";
        String cons = " and Status>=-1";
        //创客编号
        if (!Util.IsNullOrEmpty(MakerCode))
        {
            condition += " and MakerCode like '%" + MakerCode + "%'";
        }
        //时间不为空
        if (!Util.IsNullOrEmpty(CreateDateData))
        {
            String[] datelist = CreateDateData.split(" - ");
            start = datelist[0].replaceAll("-", "");
            end = datelist[1].replaceAll("-", "");
            con += " and TradeDate>='" + start + "' and TradeDate<='" + end + "'";
            cons += " and SeoKeyword >=" + start + " and SeoKeyword <=" + end;

        }
        //时间为空
        else
        {
            start = Util.DateFormat(new Date(), "yyyyMM") + "01";
            end = Util.DateFormat(Util.AddMonth(new Date(), 1), "yyyyMM") + "01";
            con += " and TradeDate>='" + start + "' and TradeDate<'" + end + "'";
            cons += " and SeoKeyword >=" + start + " and SeoKeyword <" + end;
        }
        if (!Util.IsNullOrEmpty(BrandId.toString()) && BrandId > 0)
        {
            con += " and BrandId='" + BrandId + "'";
            cons += " and BrandId='" + BrandId + "'";
        }
        //关联表数据
        List<RelationData> relationData = new ArrayList<>();

        Integer count = 0;
        List<Dictionary<String, Object>> list = UsersService.List(relationData, condition, count, page, limit, "Sort desc, Id desc");
        for (Dictionary<String, Object> dic : list)
        {
            Integer UId = Integer.parseInt(dic.get("Id").toString());
            // var Info = TradeDaySummaryService.GetMonthTradeForBrand(UId, start, end, BrandId);

            // dic["TotalAmtfc"] = Convert.ToDecimal(Info["TotalAmtfc"].toString()) + Convert.ToDecimal(Info["YAmtfc"].toString());
            // dic["DAmtfc"] = Convert.ToDecimal(Info["DAmtfc"].toString());
            // dic["JAmtfc"] = Convert.ToDecimal(Info["JAmtfc"].toString());
            // dic["JfAmtfc"] = Convert.ToDecimal(Info["JfAmtfc"].toString());
            // dic["JCountfc"] = Convert.ToInt32(Info["JCountfc"].toString());
            // dic["YAmtfc"] = Convert.ToDecimal(Info["YAmtfc"].toString());

            // dic["TotalAmtwd"] = Convert.ToDecimal(Info["TotalAmtwd"].toString()) + Convert.ToDecimal(Info["YAmtwd"].toString());
            // dic["DAmtwd"] = Convert.ToDecimal(Info["DAmtwd"].toString());
            // dic["JAmtwd"] = Convert.ToDecimal(Info["JAmtwd"].toString());
            // dic["JfAmtwd"] = Convert.ToDecimal(Info["JfAmtwd"].toString());
            // dic["JCountwd"] = Convert.ToInt32(Info["JCountwd"].toString());
            // dic["YAmtwd"] = Convert.ToDecimal(Info["YAmtwd"].toString());

            // dic["TotalAmtyl"] = Convert.ToDecimal(Info["TotalAmtyl"].toString()) + Convert.ToDecimal(Info["YAmtyl"].toString());
            // dic["DAmtyl"] = Convert.ToDecimal(Info["DAmtyl"].toString());
            // dic["JAmtyl"] = Convert.ToDecimal(Info["JAmtyl"].toString());
            // dic["JfAmtyl"] = Convert.ToDecimal(Info["JfAmtyl"].toString());
            // dic["JCountyl"] = Convert.ToInt32(Info["JCountyl"].toString());
            // dic["YAmtyl"] = Convert.ToDecimal(Info["YAmtyl"].toString());

            // dic["TBCount"] = Info["TBCount"].toString();
            // dic["TACount"] = Info["TACount"].toString();


        }
        Dictionary<String, Object> other = new Hashtable<>();
        Dictionary<String, Object> personal = new Hashtable<>();
        Dictionary<String, Object> team = new Hashtable<>();
        // var sumTitleInfo = TradeDaySummaryService.GetSumTitleMonthTrade(UserId, con, cons);
        // //个人
        // //扶持期
        // personal.Add("TotalAmtfc", Convert.ToDecimal(sumTitleInfo["TotalAmtfc"].toString()));
        // personal.Add("DAmtfc", Convert.ToDecimal(sumTitleInfo["DAmtfc"].toString()));
        // personal.Add("JAmtfc", Convert.ToDecimal(sumTitleInfo["JAmtfc"].toString()));
        // personal.Add("JfAmtfc", Convert.ToDecimal(sumTitleInfo["JfAmtfc"].toString()));
        // personal.Add("JCountfc", Convert.ToDecimal(sumTitleInfo["JCountfc"].toString()));
        // personal.Add("YAmtfc", Convert.ToDecimal(sumTitleInfo["YAmtfc"].toString()));

        // //稳定期
        // personal.Add("TotalAmtwd", Convert.ToDecimal(sumTitleInfo["TotalAmtwd"].toString()));
        // personal.Add("DAmtwd", Convert.ToDecimal(sumTitleInfo["DAmtwd"].toString()));
        // personal.Add("JAmtwd", Convert.ToDecimal(sumTitleInfo["JAmtwd"].toString()));
        // personal.Add("JfAmtwd", Convert.ToDecimal(sumTitleInfo["JfAmtwd"].toString()));
        // personal.Add("JCountwd", Convert.ToDecimal(sumTitleInfo["JCountwd"].toString()));
        // personal.Add("YAmtwd", Convert.ToDecimal(sumTitleInfo["YAmtwd"].toString()));

        // //盈利期
        // personal.Add("TotalAmtyl", Convert.ToDecimal(sumTitleInfo["TotalAmtyl"].toString()));
        // personal.Add("DAmtyl", Convert.ToDecimal(sumTitleInfo["DAmtyl"].toString()));
        // personal.Add("JAmtyl", Convert.ToDecimal(sumTitleInfo["JAmtyl"].toString()));
        // personal.Add("JfAmtyl", Convert.ToDecimal(sumTitleInfo["JfAmtyl"].toString()));
        // personal.Add("JCountyl", Convert.ToDecimal(sumTitleInfo["JCountyl"].toString()));
        // personal.Add("YAmtyl", Convert.ToDecimal(sumTitleInfo["YAmtyl"].toString()));

        // //团队
        // //扶持期
        // team.Add("TotalAmtfc1", Convert.ToDecimal(sumTitleInfo["TotalAmtfc1"].toString()));
        // team.Add("DAmtfc1", Convert.ToDecimal(sumTitleInfo["DAmtfc1"].toString()));
        // team.Add("JAmtfc1", Convert.ToDecimal(sumTitleInfo["JAmtfc1"].toString()));
        // team.Add("JfAmtfc1", Convert.ToDecimal(sumTitleInfo["JfAmtfc1"].toString()));
        // team.Add("JCountfc1", Convert.ToDecimal(sumTitleInfo["JCountfc1"].toString()));
        // team.Add("YAmtfc1", Convert.ToDecimal(sumTitleInfo["YAmtfc1"].toString()));

        // //稳定期
        // team.Add("TotalAmtwd1", Convert.ToDecimal(sumTitleInfo["TotalAmtwd1"].toString()));
        // team.Add("DAmtwd1", Convert.ToDecimal(sumTitleInfo["DAmtwd1"].toString()));
        // team.Add("JAmtwd1", Convert.ToDecimal(sumTitleInfo["JAmtwd1"].toString()));
        // team.Add("JfAmtwd1", Convert.ToDecimal(sumTitleInfo["JfAmtwd1"].toString()));
        // team.Add("JCountwd1", Convert.ToDecimal(sumTitleInfo["JCountwd1"].toString()));
        // team.Add("YAmtwd1", Convert.ToDecimal(sumTitleInfo["YAmtwd1"].toString()));

        // //盈利期
        // team.Add("TotalAmtyl1", Convert.ToDecimal(sumTitleInfo["TotalAmtyl1"].toString()));
        // team.Add("DAmtyl1", Convert.ToDecimal(sumTitleInfo["DAmtyl1"].toString()));
        // team.Add("JAmtyl1", Convert.ToDecimal(sumTitleInfo["JAmtyl1"].toString()));
        // team.Add("JfAmtyl1", Convert.ToDecimal(sumTitleInfo["JfAmtyl1"].toString()));
        // team.Add("JCountyl1", Convert.ToDecimal(sumTitleInfo["JCountyl1"].toString()));
        // team.Add("YAmtyl1", Convert.ToDecimal(sumTitleInfo["YAmtyl1"].toString()));

        // personal.Add("PBCount", Convert.ToDecimal(sumTitleInfo["PBCount"].toString()));
        // personal.Add("PACount", Convert.ToDecimal(sumTitleInfo["PACount"].toString()));
        // team.Add("TBCount", Convert.ToDecimal(sumTitleInfo["TBCount"].toString()));
        // team.Add("TACount", Convert.ToDecimal(sumTitleInfo["TACount"].toString()));

        // other.Add("personal", personal);
        // other.Add("team", team);
        // other.Add("count", count);
        // 返回字段：
        // MakerCode // 创客编号
        // RealName // 创客姓名
        // TotalAmtfc // 刷卡交易总额(扶)
        // DAmtfc // 贷记卡交易额(扶)
        // JAmtfc // 借记卡非封顶交易额(扶)
        // JfAmtfc // 借记卡封顶交易额(扶)
        // JCountfc // 借记卡交易笔数(扶)
        // YAmtfc // 云闪付小额交易额(扶)
        // TotalAmtwd // 刷卡交易总额(稳)
        // DAmtwd // 贷记卡交易额(稳)
        // JAmtwd // 借记卡非封顶交易额(稳)
        // JfAmtwd // 借记卡封顶交易额(稳)
        // JCountwd // 借记卡交易笔数(稳)
        // YAmtwd // 云闪付小额交易额(稳)
        // TotalAmtyl // 刷卡交易总额(盈)
        // DAmtyl // 贷记卡交易额(盈)
        // JAmtyl // 借记卡非封顶交易额(盈)
        // JfAmtyl // 借记卡封顶交易额(盈)
        // JCountyl // 借记卡交易笔数(盈)
        // YAmtyl // 云闪付小额交易额(盈)
        // TBCount // 团队机具绑定总数
        // TACount // 团队机具激活总数

        // 个人交易额：
        // 刷卡交易总额(扶) TotalAmtfc 贷记卡交易额(扶) DAmtfc 借记卡非封顶交易额(扶) JAmtfc 借记卡封顶交易额(扶) JfAmtfc 借记卡交易笔数(扶) JCountfc 云闪付小额交易额(扶) YAmtfc
        // 刷卡交易总额(稳) TotalAmtwd 贷记卡交易额(稳) DAmtwd 借记卡非封顶交易额(稳) JAmtwd 借记卡封顶交易额(稳) JfAmtwd 借记卡交易笔数(稳) JCountwd 云闪付小额交易额(稳) YAmtwd
        // 刷卡交易总额(盈) TotalAmtyl 贷记卡交易额(盈) DAmtyl 借记卡非封顶交易额(盈) JAmtyl 借记卡封顶交易额(盈) JfAmtyl 借记卡交易笔数(盈) JCountyl 云闪付小额交易额(盈) YAmtyl
        // 个人机具绑定总数 PBCount 个人机具激活总数 PACount
        // 团队交易额：
        // 刷卡交易总额(扶) TotalAmtfc1 贷记卡交易额(扶) DAmtfc1 借记卡非封顶交易额(扶) JAmtfc1 借记卡封顶交易额(扶) JfAmtfc1 借记卡交易笔数(扶) JCountfc1 云闪付小额交易额(扶) YAmtfc1
        // 刷卡交易总额(稳) TotalAmtwd1 贷记卡交易额(稳) DAmtwd1 借记卡非封顶交易额(稳) JAmtwd1 借记卡封顶交易额(稳) JfAmtwd1 借记卡交易笔数(稳) JCountwd1 云闪付小额交易额(稳) YAmtwd1
        // 刷卡交易总额(盈) TotalAmtyl1 贷记卡交易额(盈) DAmtyl1 借记卡非封顶交易额(盈) JAmtyl1 借记卡封顶交易额(盈) JfAmtyl1 借记卡交易笔数(盈) JCountyl1 云闪付小额交易额(盈) YAmtyl1
        // 团队机具绑定总数 TBCount 团队机具激活总数 TACoun
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = list;
        result.Other = other;
        return result;
    }


    //根据条件查询已注销创客列表

    /// <summary>
    /// 根据条件查询已注销创客列表
    /// </summary>
    /// <returns></returns>
    @RequestMapping("/main/users/canclelist")
    public @ResponseBody AppResultJson CancleList(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject data = JSONObject.parseObject(value);
        Integer page = Integer.parseInt(data.getString("PageNum").toString());
        Integer limit = Integer.parseInt(data.getString("PageSize").toString());
        String condition = " and Status=-1"; //查询条件
        String MakerCode = data.getString("MakerCode").toString(); //创客编号
        String RealName = data.getString("RealName").toString(); //真实姓名
        String Mobile = data.getString("Mobile").toString(); //手机号
        String CertId = data.getString("CertId").toString(); //身份证号
        String CreateDate = data.getString("CreateDate").toString(); //注册时间
        String AuthFlag = data.getString("AuthFlag").toString(); //实名状态(0 未实名 1 已实名)
        if (!Util.IsNullOrEmpty(RealName))
        {
            condition += " and RealName like '%" + RealName + "%'";
        }
        if (!Util.IsNullOrEmpty(CertId))
        {
            condition += " and CertId like '%" + CertId + "%'";
        }
        if (!Util.IsNullOrEmpty(Mobile))
        {
            condition += " and Mobile like '%" + Mobile + "%'";
        }
        if (!Util.IsNullOrEmpty(MakerCode))
        {
            condition += " and MakerCode like '%" + MakerCode + "%'";
        }
        if (!Util.IsNullOrEmpty(CreateDate))
        {
            String[] datelist = CreateDate.split(" - ");
            String start = datelist[0];
            String end = datelist[1];
            condition += " and CreateDate>='" + start + " 00:00:00' and CreateDate<='" + end + " 23:59:59'";
        }
        if (!Util.IsNullOrEmpty(AuthFlag))
        {
            condition += " and AuthFlag=" + AuthFlag;
        }

        //关联表数据
        List<RelationData> relationData = new ArrayList<>();
        RelationData RelationDataItem = new RelationData();
        RelationDataItem.Field = "ParentUserId";
        RelationDataItem.Table = "Users";
        List<String> ReturnField = new ArrayList<>();
        ReturnField.add("MakerCode");
        ReturnField.add("RealName");
        RelationDataItem.ReturnField = ReturnField;
        relationData.add(RelationDataItem);

        Integer count = 0;
        List<Dictionary<String, Object>> list = UsersService.List(relationData, condition, count, page, limit, "Sort desc,Id desc");
        for (Dictionary<String, Object> dic : list)
        {
            // var userLevel = " and Id=" + Integer.parseInt(dic["UserLevel"].toString());
            //创客等级
            // dic["LevelName"] = UserLevelSetService.Query(Integer.parseInt(dic["UserLevel"].toString())).Name;

            //实名状态
            // dic["AuthFlagName"] = dic["AuthFlag"].toString() == "1" ? "已实名" : "未实名";

            //风控状态
            // dic["RiskFlagName"] = dic["RiskFlag"].toString() == "1" ? "已风控" : "未风控";

            //商户创客
            // dic["MerchantTypeName"] = Func.GetMerchantTypeInfo(Integer.parseInt(dic["MerchantType"].toString()));

            if (!Util.IsNullOrEmpty(dic.get("ParentNav").toString()))
            {
                String ParentNav = dic.get("ParentNav").toString();
                String[] ParentNavList = ParentNav.substring(1, ParentNav.length() - 2).replaceAll(",,", ",").split(",");
                var topUserInfo = new Users();
                if (ParentNavList.length > 1)
                {
                    topUserInfo = UsersService.Query(Integer.parseInt(ParentNavList[1]), "*");
                }
                else
                {
                    topUserInfo = UsersService.Query(Integer.parseInt(ParentNavList[0]), "*");
                }
                dic.put("TopMakerCode", topUserInfo.MakerCode);
                dic.put("TopRealName", topUserInfo.RealName);
            }
            else
            {
                dic.put("TopMakerCode", "");
                dic.put("TopRealName", "");
            }
        }
        // 返回字段：
        // MakerCode //创客编号
        // RealName //真实姓名
        // UserLevel //创客等级(1~9对应K1~K9)
        // Mobile //手机号
        // CertId //身份证号
        // AgentAreas //展业区域
        // MerchantType //商户创客类型(0 非商户型合伙人 1商户型合伙人)
        // MerchantDate //成为商户型创客时间
        // AuthFlag //实名状态(1 已实名 0 未实名)
        // CreateDate //注册时间
        // AuthDate //实名时间
        // ParentUserIdMakerCode //直属创客编号
        // ParentUserIdRealName //直属创客名称
        // TopMakerCode //顶级创客编号
        // TopRealName //顶级创客名称
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = list;
        result.Other = count;
        return result;
    }


    //创客详情

    /// <summary>
    /// 创客详情
    /// </summary>
    /// <returns></returns>
    @GetMapping("/main/users")
    public @ResponseBody AppResultJson Query(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject data = JSONObject.parseObject(value);
        Integer Id = Integer.parseInt(Util.CheckInt(data.get("Id").toString()));
        Users item = UsersService.Query(Id, "*");
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = item;
        return result;
    }


    //增加创客

    /// <summary>
    /// 增加或修改创客信息
    /// </summary>
    /// <returns></returns>
    @PostMapping("/main/users")
    public @ResponseBody AppResultJson Add(String value)
    {
        value = DesUtil.decrypt(value);
        Users data = JSONObject.parseObject(JSONObject.toJSONString(value), Users.class);
        Map<String, Object> fields = new HashMap<>();
        fields.put("NickName", data.NickName); //昵称
        fields.put("RealName", data.RealName); //真实姓名
        fields.put("Sex", data.Sex); //性别
        fields.put("CertId", data.CertId); //身份证号
        fields.put("Areas", data.Areas); //所在地区
        fields.put("Address", data.Address); //详细地址
        fields.put("HeadPhoto", data.HeadPhoto); //头像
        fields.put("UserLevel", data.UserLevel); //创客等级
        fields.put("CardNo", data.CardNo); //卡号
        fields.put("Mobile", data.Mobile); //手机号
        fields.put("GroupId", data.GroupId); //分组
        fields.put("Tags", data.Tags); //标签
        if (!Util.IsNullOrEmpty(data.LoginPwd)) //密码不为空就修改密码，否则不操作
        {
            fields.put("LoginPwd", Util.MD532(data.LoginPwd)); //登录密码
        }
        if (!Util.IsNullOrEmpty(data.PayPwd)) //密码不为空就修改密码，否则不操作
        {
            fields.put("PayPwd", Util.MD532(data.PayPwd)); //支付密码
        }
        fields.put("IsNewUser", data.IsNewUser); //是否新创客
        fields.put("IdCardEndNo", data.IdCardEndNo); //身份证后四位
        fields.put("SettleBankCardNo", data.SettleBankCardNo); //结算银行卡号
        fields.put("SettleBankName", data.SettleBankName); //结算银行名称
        fields.put("ReferenceCode", data.ReferenceCode); //推荐码
        fields.put("CardRecCode", data.CardRecCode); //办卡分享图片地址
        fields.put("ReferenceQrCode", data.ReferenceQrCode); //推广二维码地址
        fields.put("SignImgUrl", data.SignImgUrl); //特定协议签名图片地址
        fields.put("UserType", data.UserType); //创客类型
        fields.put("UserProperty", data.UserProperty); //创客属性
        fields.put("MerchantType", data.MerchantType); //商户创客类型
        fields.put("MerchantDate", data.MerchantDate); //成为商户型创客时间
        fields.put("LoginStatus", data.LoginStatus); //登录状态
        fields.put("AuditStatus", data.AuditStatus); //审核状态
        fields.put("LockStatus", data.LockStatus); //锁定状态
        fields.put("AuthFlag", data.AuthFlag); //实名标识
        fields.put("AuthDate", data.AuthDate); //实名时间
        fields.put("RiskFlag", data.RiskFlag); //风控标识
        fields.put("RiskRemark", data.RiskRemark); //风控备注
        fields.put("RreezeRemark", data.RreezeRemark); //账户冻结备注信息
        fields.put("SettleRemark", data.SettleRemark); //结算金额调整备注
        fields.put("ExpiredDate", data.ExpiredDate); //过期时间
        fields.put("DeviceId", data.DeviceId); //设备ID
        fields.put("DeviceToken", data.DeviceToken); //设备令牌
        fields.put("CertFrontImage", data.CertFrontImage); //身份证正面照
        fields.put("CertReverseImage", data.CertReverseImage); //身份证反面照
        fields.put("HandCertImage", data.HandCertImage); //手持身份证
        fields.put("BankCardPositiveImage", data.BankCardPositiveImage); //银行卡正面照
        fields.put("Remark", data.Remark); //备注信息
        if (!Util.IsNullOrEmpty(data.HandPwd)) //密码不为空就修改密码，否则不操作
        {
            fields.put("HandPwd", Util.MD532(data.HandPwd)); //手势密码
        }
        fields.put("MakerCode", data.MakerCode); //创客编号
        fields.put("CashFreezeAmt", data.CashFreezeAmt); //提现冻结金额
        fields.put("ActiveRewardAmount", data.ActiveRewardAmount); //激活奖励金额
        fields.put("ProfitRewardRate", data.ProfitRewardRate); //分润奖励
        fields.put("CertValidStartDate", data.CertValidStartDate); //身份证有效期开始时间
        fields.put("CertValidEndDate", data.CertValidEndDate); //身份证有效期结束时间
        fields.put("SettleBankCardName", data.SettleBankCardName); //结算银行卡户名
        fields.put("AgentLevel", data.AgentLevel); //代理等级
        fields.put("AgentAreas", data.AgentAreas); //代理区域
        fields.put("CashStatus", data.CashStatus); //代付账户状态
        fields.put("CashNote", data.CashNote); //代付账户备注
        fields.put("LeaderLevel", data.LeaderLevel); //盟主等级
        fields.put("BusinessFlag", data.BusinessFlag); //企业标记
        fields.put("DeviceType", data.DeviceType); //设备类型
        fields.put("SignPic", data.SignPic); //签名图片

        AppResultJson resultJson = UsersService.Add(fields);
        return resultJson;
    }


    //修改创客

    /// <summary>
    /// 增加或修改创客信息
    /// </summary>
    /// <returns></returns>
    @PutMapping("/main/users")
    public @ResponseBody AppResultJson Edit(String value)
    {
        value = DesUtil.decrypt(value);
        Users data = JSONObject.parseObject(JSONObject.toJSONString(value), Users.class);
        Map<String, Object> fields = new HashMap<>();
        fields.put("NickName", data.NickName); //昵称
        fields.put("RealName", data.RealName); //真实姓名
        fields.put("Sex", data.Sex); //性别
        fields.put("CertId", data.CertId); //身份证号
        fields.put("Areas", data.Areas); //所在地区
        fields.put("Address", data.Address); //详细地址
        fields.put("HeadPhoto", data.HeadPhoto); //头像
        fields.put("UserLevel", data.UserLevel); //创客等级
        fields.put("CardNo", data.CardNo); //卡号
        fields.put("Mobile", data.Mobile); //手机号
        fields.put("GroupId", data.GroupId); //分组
        fields.put("Tags", data.Tags); //标签
        if (!Util.IsNullOrEmpty(data.LoginPwd)) //密码不为空就修改密码，否则不操作
        {
            fields.put("LoginPwd", Util.MD532(data.LoginPwd)); //登录密码
        }
        if (!Util.IsNullOrEmpty(data.PayPwd)) //密码不为空就修改密码，否则不操作
        {
            fields.put("PayPwd", Util.MD532(data.PayPwd)); //支付密码
        }
        fields.put("IsNewUser", data.IsNewUser); //是否新创客
        fields.put("IdCardEndNo", data.IdCardEndNo); //身份证后四位
        fields.put("SettleBankCardNo", data.SettleBankCardNo); //结算银行卡号
        fields.put("SettleBankName", data.SettleBankName); //结算银行名称
        fields.put("ReferenceCode", data.ReferenceCode); //推荐码
        fields.put("CardRecCode", data.CardRecCode); //办卡分享图片地址
        fields.put("ReferenceQrCode", data.ReferenceQrCode); //推广二维码地址
        fields.put("SignImgUrl", data.SignImgUrl); //特定协议签名图片地址
        fields.put("UserType", data.UserType); //创客类型
        fields.put("UserProperty", data.UserProperty); //创客属性
        fields.put("MerchantType", data.MerchantType); //商户创客类型
        fields.put("MerchantDate", data.MerchantDate); //成为商户型创客时间
        fields.put("LoginStatus", data.LoginStatus); //登录状态
        fields.put("AuditStatus", data.AuditStatus); //审核状态
        fields.put("LockStatus", data.LockStatus); //锁定状态
        fields.put("AuthFlag", data.AuthFlag); //实名标识
        fields.put("AuthDate", data.AuthDate); //实名时间
        fields.put("RiskFlag", data.RiskFlag); //风控标识
        fields.put("RiskRemark", data.RiskRemark); //风控备注
        fields.put("RreezeRemark", data.RreezeRemark); //账户冻结备注信息
        fields.put("SettleRemark", data.SettleRemark); //结算金额调整备注
        fields.put("ExpiredDate", data.ExpiredDate); //过期时间
        fields.put("DeviceId", data.DeviceId); //设备ID
        fields.put("DeviceToken", data.DeviceToken); //设备令牌
        fields.put("CertFrontImage", data.CertFrontImage); //身份证正面照
        fields.put("CertReverseImage", data.CertReverseImage); //身份证反面照
        fields.put("HandCertImage", data.HandCertImage); //手持身份证
        fields.put("BankCardPositiveImage", data.BankCardPositiveImage); //银行卡正面照
        fields.put("Remark", data.Remark); //备注信息
        if (!Util.IsNullOrEmpty(data.HandPwd)) //密码不为空就修改密码，否则不操作
        {
            fields.put("HandPwd", Util.MD532(data.HandPwd)); //手势密码
        }
        fields.put("MakerCode", data.MakerCode); //创客编号
        fields.put("CashFreezeAmt", data.CashFreezeAmt); //提现冻结金额
        fields.put("ActiveRewardAmount", data.ActiveRewardAmount); //激活奖励金额
        fields.put("ProfitRewardRate", data.ProfitRewardRate); //分润奖励
        fields.put("CertValidStartDate", data.CertValidStartDate); //身份证有效期开始时间
        fields.put("CertValidEndDate", data.CertValidEndDate); //身份证有效期结束时间
        fields.put("SettleBankCardName", data.SettleBankCardName); //结算银行卡户名
        fields.put("AgentLevel", data.AgentLevel); //代理等级
        fields.put("AgentAreas", data.AgentAreas); //代理区域
        fields.put("CashStatus", data.CashStatus); //代付账户状态
        fields.put("CashNote", data.CashNote); //代付账户备注
        fields.put("LeaderLevel", data.LeaderLevel); //盟主等级
        fields.put("BusinessFlag", data.BusinessFlag); //企业标记
        fields.put("DeviceType", data.DeviceType); //设备类型
        fields.put("SignPic", data.SignPic); //签名图片

        AppResultJson resultJson = UsersService.Edit(fields, data.Id);
        return resultJson;
    }


    //逻辑删除创客信息

    /// <summary>
    /// 删除创客信息
    /// </summary>
    /// <returns></returns>
    @DeleteMapping("/main/users/remove")
    public @ResponseBody AppResultJson Remove(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject jsonObj = JSONObject.parseObject(value);
        String Id = jsonObj.get("Id").toString();
        String[] idlist = Id.split(",");
        for (String subid : idlist)
        {
            Integer id = Integer.parseInt(subid);
            UsersService.Remove(id);
        }
        Dictionary<String, Object> obj = new Hashtable<>(); //返回字段
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = obj;
        return result;
    }


    //彻底删除创客信息

    /// <summary>
    /// 删除创客信息
    /// </summary>
    /// <returns></returns>
    @DeleteMapping("/main/users")
    public @ResponseBody AppResultJson Delete(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject jsonObj = JSONObject.parseObject(value);
        String Id = jsonObj.get("Id").toString();
        String[] idlist = Id.split(",");
        for (String subid : idlist)
        {
            Integer id = Integer.parseInt(subid);
            UsersService.Delete(id);
        }
        Dictionary<String, Object> obj = new Hashtable<>(); //返回字段
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = obj;
        return result;
    }


    //开启

    /// <summary>
    /// 开启
    /// </summary>
    /// <returns></returns>
    @PutMapping("/main/users/open")
    public @ResponseBody AppResultJson Open(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject jsonObj = JSONObject.parseObject(value);
        String Id = jsonObj.get("Id").toString();
        String[] idlist = Id.split(",");
        for (String subid : idlist)
        {
            Integer id = Integer.parseInt(subid);
            Map<String, Object> Fields = new HashMap<>();
            Fields.put("Status", 1);
            UsersService.Edit(Fields, id);
        }
        Dictionary<String, Object> obj = new Hashtable<>(); //返回字段
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = obj;
        return result;
    }


    //关闭

    /// <summary>
    /// 关闭
    /// </summary>
    /// <returns></returns>
    @PutMapping("/main/users/close")
    public @ResponseBody AppResultJson Close(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject jsonObj = JSONObject.parseObject(value);
        String Id = jsonObj.get("Id").toString();
        String[] idlist = Id.split(",");
        for (String subid : idlist)
        {
            Integer id = Integer.parseInt(subid);
            Map<String, Object> Fields = new HashMap<>();
            Fields.put("Status", 0);
            UsersService.Edit(Fields, id);
        }
        Dictionary<String, Object> obj = new Hashtable<>(); //返回字段
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = obj;
        return result;
    }


    //排序
    /// <summary>
    /// 排序
    /// </summary>
    /// <param name="Id"></param>
    @PutMapping("/main/users/sort")
    public @ResponseBody AppResultJson Sort(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject jsonObj = JSONObject.parseObject(value);
        Integer Id = Integer.parseInt(jsonObj.get("Id").toString());
        Integer Sort = Integer.parseInt(jsonObj.get("Sort").toString());
        UsersService.Sort(Id, Sort);
        Dictionary<String, Object> obj = new Hashtable<>(); //返回字段
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = obj;
        return result;
    }

    //导入数据
    /// <summary>
    /// 导入数据
    /// </summary>
    /// <param name="ExcelData"></param>
    @PostMapping("/main/users/import")
    public @ResponseBody AppResultJson Import(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject jsonObj = JSONObject.parseObject(value);
        String ExcelData = jsonObj.get("ExcelData").toString();
        UsersService.Import(ExcelData);
        Dictionary<String, Object> obj = new Hashtable<>(); //返回字段
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Data = obj;
        return result;
    }

    //导出Excel

    /// <summary>
    /// 导出Excel
    /// </summary>
    /// <returns></returns>
    @PostMapping("/main/users/export")
    public @ResponseBody AppResultJson ExportExcel(String value)
    {
        value = DesUtil.decrypt(value);
        Users data = JSONObject.parseObject(JSONObject.toJSONString(value), Users.class);
        String SqlString = "select * from Users order by Id desc limit 20";
        Dictionary<String, Object> dic = new Hashtable<>();
        dic.put("Operater", 0); //后台操作员账户Id
        dic.put("SqlString", SqlString); //sql语句，表头用指定中文
        dic.put("FileName", "仓库-" + Util.DateFormat(new Date(), "yyyy-MM-dd HH:mm:ss")); //excel文件名
        dic.put("MaxCount", "0"); //最大返回数，0为不限制
        redisUtils.addList("ExportQueue", JSONObject.toJSONString(dic));
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        return result;
    }


    //同步创客机具数

    /// <summary>
    /// 同步创客机具数
    /// </summary>
    /// <returns></returns>
    @PostMapping("/main/users/sycnposcount")
    public @ResponseBody AppResultJson SycnData(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject data = JSONObject.parseObject(value);
        String UserId = data.getString("UserId").toString();
        // var BrandIds = KqProductsService.QueryBrandId();
        // foreach (var BrandId in BrandIds)
        // {
            // BaseClass.SycnUserPosCount(Integer.parseInt(UserId), Integer.parseInt(BrandId.toString()));
            // RedisDbconn.Instance.AddList("SycnUserMachineCountQueue", "{\"UserId\":\"" + UserId + "\",\"BrandId\":\"" + BrandId + "\"}");
        // }
        AppResultJson result = new AppResultJson();
        result.Status = "1";
        result.Info = "同步成功！";
        return result;
    }


    //重置密码

    /// <summary>
    /// 重置密码
    /// </summary>
    /// <returns></returns>
    @PostMapping("/main/users/resetpwd")
    public @ResponseBody AppResultJson ResetPwd(String value)
    {
        value = DesUtil.decrypt(value);
        JSONObject data = JSONObject.parseObject(value);
        Integer UserId = Integer.parseInt(data.getString("UserId").toString());
        var info = "";
        // info = UsersUtil.ResetLoginPwd(UserId);
        if (info == "success")
        {
            AppResultJson result = new AppResultJson();
            result.Status = "1";
            result.Info = "重置成功";
            return result;
        }
        else
        {
            AppResultJson result = new AppResultJson();
            result.Status = "0";
            result.Info = "重置失败";
            result.Data = info;
            return result;
        }
    }

}