using ce.autofac.extension;
using Infrastructure;
using Infrastructure.Extensions;
using Microsoft.Extensions.Configuration;
using OpenAuth.App.BaseApp.WFTask;
using OpenAuth.App.BasicQueryService;
using OpenAuth.App.Interface;
using OpenAuth.App.WorkFlow;
using OpenAuth.Repository.Domain;
using SqlSugar;
using WFTask = OpenAuth.Repository.Domain.WFTask;


namespace OpenAuth.App.ServiceApp.DroneCaseInfo;

[BLLName("toNonGrain1")]
public class ToNonGrainWorkMethod1 : IWorkFlowMethod
{
    private readonly WFProcessApp _processApp;
    private readonly DroneCaseInfoSingleApp _droneCaseInfoSingleApp;
    private readonly DroneCaseInfoFLHApp _droneCaseInfoFLHApp;
    private readonly IConfiguration _configuration;
    private readonly WFSchemeInfoApp _wfSchemeInfoApp;
    private readonly WFTaskApp _wfTaskApp;
    private readonly IAuth _auth;
    private readonly ISqlSugarClient _client;
    CommonDataManager _commonDataManager;
    public ToNonGrainWorkMethod1(WFProcessApp processApp, DroneCaseInfoSingleApp droneCaseInfoSingleApp,
        DroneCaseInfoFLHApp droneCaseInfoFLHApp, IConfiguration configuration, WFSchemeInfoApp wfSchemeInfoApp,
        IAuth auth, WFTaskApp wfTaskApp, ISqlSugarClient sqlSugarClient, CommonDataManager commonDataManager)
    {
        _processApp = processApp;
        _droneCaseInfoSingleApp = droneCaseInfoSingleApp;
        _droneCaseInfoFLHApp = droneCaseInfoFLHApp;
        _configuration = configuration;
        _wfSchemeInfoApp = wfSchemeInfoApp;
        _auth = auth;
        _wfTaskApp = wfTaskApp;
        _client = sqlSugarClient;
        _commonDataManager = commonDataManager;
    }

    public async Task Execute(WfMethodParameter parameter)
    {
        var user = _auth.GetCurrentUser();

        Console.WriteLine("==============================================");
        var caseId = parameter.ProcessId;
        // 取得案件信息
        var caseList = _droneCaseInfoSingleApp.ListAllNonGrainCase();
        foreach (var originCaseInfo in caseList)
        {
            var caseno = originCaseInfo.case_no;
            // 验证非粮化是否存在该专题
            if (_droneCaseInfoFLHApp.ExistCase(caseno))
            {
                continue;
            }
            // 违法用地专题判断违法+非粮化+已归档+耕地不为0的图斑
            // 非粮化案件信息
            var nonGrainCaseInfo = originCaseInfo.MapTo<DroneCaseInfoFLHSpecial>();

            if (!string.IsNullOrEmpty(originCaseInfo.jieshou_time))
            {
                nonGrainCaseInfo.jieshou_time = DateTime.Parse(originCaseInfo.jieshou_time);
            }

            //nonGrainCaseInfo.jieshou_time = null;
            nonGrainCaseInfo.hexiaotime = null;
            var newProcessId = Guid.NewGuid().ToString();
            nonGrainCaseInfo.Id = newProcessId;
            nonGrainCaseInfo.handle_status_id = 2;
            nonGrainCaseInfo.handle_status_name = "待整改";
            // 去掉字段
            nonGrainCaseInfo.verifytime = null;
            nonGrainCaseInfo.verifyuser = null;
            nonGrainCaseInfo.verifyuserid = null;
            nonGrainCaseInfo.verifytime = null;
            nonGrainCaseInfo.verifystatus = null;
            nonGrainCaseInfo.hexiaotime = null;
            nonGrainCaseInfo.is_drawback = null;
            //nonGrainCaseInfo.is_shijibohui = null;
            //nonGrainCaseInfo.xianjiyijian = null;
            nonGrainCaseInfo.hexiaotime = null;
            nonGrainCaseInfo.hexiaoren = null;
            //  复制一份图斑
            var shp = _droneCaseInfoSingleApp.GetShpData(nonGrainCaseInfo.geomid);
            var geoms = "";
            foreach (var droneShpData in shp)
            {
                var gidStr = _commonDataManager.GetMaxKeyVal("gid", "drone_shp_data", 1);
                geoms += gidStr + ",";
                droneShpData.relid = nonGrainCaseInfo.Id;
                // 多图斑处置
                droneShpData.gid = gidStr.ToInt();
                droneShpData.relid = nonGrainCaseInfo.Id;
                _droneCaseInfoSingleApp.SaveShpData(droneShpData);
            }
            geoms = geoms.TrimEnd(',');
            nonGrainCaseInfo.geomid = geoms;

            var finish = _droneCaseInfoFLHApp.AddCaseInfo(nonGrainCaseInfo);
            if (finish)
            {
                // 非粮化code
                try
                {
                    var schemeCode = _configuration.GetSection("AppSetting:DroneCaseInfoNonFoodSchemeCode").Value;
                    var schemeInfo = _wfSchemeInfoApp.SelectBySchemeCode(schemeCode);
                    // 发起新流程
                    var instanceInfo = new WFInstanceInfo
                    {
                        pkeyValue = newProcessId,
                        pkey = "Id",
                        schemeId = schemeInfo.SchemeId
                    };
                    instanceInfo.pkeyValue = newProcessId;
                    _processApp.SaveDraft(nonGrainCaseInfo.Id, schemeCode, user.User.Id + "",
                        Json.ToJson(instanceInfo), "", "", "", 0,
                        nonGrainCaseInfo.case_description).Wait();
                    _processApp.Create(nonGrainCaseInfo.Id, "", user.User.Id + "", null, Json.ToJson(instanceInfo),
                        nonGrainCaseInfo.case_description).Wait();
                    // todo 补充任务信息
                    /// 
                    var node1 = _wfTaskApp.GetNodeByProcessId(nonGrainCaseInfo.Id);
                    var node2 = new WFTask(); // 脚本节点
                    var node3 = new WFTask();
                    var node4 = new WFTask();
                    var node5 = new WFTask(); // 脚本节点

                    //node1.Id = Guid.NewGuid().ToString();
                    node2.Id = Guid.NewGuid().ToString();
                    node3.Id = Guid.NewGuid().ToString();
                    node4.Id = Guid.NewGuid().ToString();
                    node5.Id = Guid.NewGuid().ToString();

                    //node1.Token = Guid.NewGuid().ToString();
                    node2.Token = Guid.NewGuid().ToString();
                    node3.Token = Guid.NewGuid().ToString();
                    node4.Token = Guid.NewGuid().ToString();
                    node5.Token = Guid.NewGuid().ToString();

                    //node1.Type = 7;
                    node2.Type = 10;
                    node3.Type = 7;
                    node4.Type = 7;
                    node5.Type = 10;

                    //node1.ProcessTitle = nonGrainCaseInfo.case_description;
                    node2.ProcessTitle = null;
                    node3.ProcessTitle = nonGrainCaseInfo.case_description;
                    node4.ProcessTitle = nonGrainCaseInfo.case_description;
                    node5.ProcessTitle = null;

                    //node1.ProcessCode = schemeCode;
                    node2.ProcessCode = null;
                    node3.ProcessCode = schemeCode;
                    node4.ProcessCode = schemeCode;
                    node5.ProcessCode = null;

                    //node1.ProcessId = nonGrainCaseInfo.Id;;
                    node2.ProcessId = nonGrainCaseInfo.Id; ;
                    node3.ProcessId = nonGrainCaseInfo.Id; ;
                    node4.ProcessId = nonGrainCaseInfo.Id; ;
                    node5.ProcessId = nonGrainCaseInfo.Id; ;

                    //node1.ProcessUserId = user.User.Id + "";
                    node2.ProcessUserId = null;
                    node3.ProcessUserId = user.User.Id + "";
                    node4.ProcessUserId = user.User.Id + "";
                    node5.ProcessUserId = null;

                    //node1.ProcessUserName = user.User.Name;
                    node2.ProcessUserName = null;
                    node3.ProcessUserName = user.User.Name;
                    node4.ProcessUserName = user.User.Name;
                    node5.ProcessUserName = null;

                    //node1.UnitId = "Activity_02tspis";
                    node2.UnitId = "Activity_13vfcwg";
                    node3.UnitId = "Activity_1aiy5ea";
                    node4.UnitId = "Activity_1t58a1v";
                    node5.UnitId = "Activity_09zxeay";

                    //node1.UnitName = "待接收";
                    node2.UnitName = "111";
                    node3.UnitName = "待填报";
                    node4.UnitName = "待整改";
                    node5.UnitName = "任务节点";

                    //node1.PrevUnitId = "Event_0li55cb";
                    node2.PrevUnitId = "Activity_02tspis";
                    node3.PrevUnitId = "Activity_02tspis";
                    node4.PrevUnitId = "Activity_1aiy5ea";
                    node5.PrevUnitId = "Activity_1aiy5ea";

                    //node1.PrevUnitName = "开始节点";
                    node2.PrevUnitName = "待接收";
                    node3.PrevUnitName = "111";
                    node4.PrevUnitName = "任务节点";
                    node5.PrevUnitName = null;

                    //node1.PrevToken = "create";
                    node2.PrevToken = node1.Token; // 脚本节点
                    node3.PrevToken = node1.Token;
                    node4.PrevToken = node3.Token;
                    node5.PrevToken = node3.Token; // 脚本节点

                    //node1.PrevTaskId = "create";
                    node2.PrevTaskId = null; // 脚本节点
                    node3.PrevTaskId = node1.Id;
                    node4.PrevTaskId = node3.Id;
                    node5.PrevTaskId = null; // 脚本节点

                    //node1.UserId = user.User.Id + "";
                    node2.UserId = null; // 脚本节点
                    node3.UserId = user.User.Id + "";
                    node4.UserId = user.User.Id + "";
                    node5.UserId = null; // 脚本节点

                    //node1.UserName = user.User.Name + "";
                    node2.UserName = null; // 脚本节点
                    node3.UserName = user.User.Name + "";
                    node4.UserName = user.User.Name + "";
                    node5.UserName = null; // 脚本节点

                    node1.State = 3;
                    node2.State = null;
                    node3.State = 3;
                    node4.State = 1;
                    node5.State = null;

                    node1.IsAgree = 1;
                    node2.IsAgree = null;
                    node3.IsAgree = 1;
                    node4.IsAgree = null; // 还未审核
                    node5.IsAgree = null;

                    node1.IsLast = 1;
                    node2.IsLast = 1;
                    node3.IsLast = 1;
                    node4.IsLast = 1;
                    node5.IsLast = 1;

                    node1.Sort = 0;
                    node2.Sort = 0;
                    node3.Sort = 0;
                    node4.Sort = 0;
                    node5.Sort = 0;

                    //node1.CreateUserId = user.User.Id + "";
                    node2.CreateUserId = user.User.Id + "";
                    node3.CreateUserId = user.User.Id + "";
                    node4.CreateUserId = user.User.Id + "";
                    node5.CreateUserId = user.User.Id + "";

                    //node1.CreateUserName = user.User.Name;
                    node2.CreateUserName = user.User.Name;
                    node3.CreateUserName = user.User.Name;
                    node4.CreateUserName = user.User.Name;
                    node5.CreateUserName = user.User.Name;

                    node1.IsBatchAudit = 0;
                    node2.IsBatchAudit = null;
                    node3.IsBatchAudit = 0;
                    node4.IsBatchAudit = 0;
                    node5.IsBatchAudit = null;

                    node1.IsReject = 0;
                    node2.IsReject = null;
                    node3.IsReject = 0;
                    node4.IsReject = 0;
                    node5.IsReject = null;


                    node1.SuperviseId = 0;
                    node2.SuperviseId = 0;
                    node3.SuperviseId = 0;
                    node4.SuperviseId = 0;
                    node5.SuperviseId = 0;

                    node1.IsRetract = 0;
                    node2.IsRetract = null;
                    node3.IsRetract = 0;
                    node4.IsRetract = 0;
                    node5.IsRetract = null;

                    //ode1.CreateDate = DateTime.Now;
                    node2.CreateDate = DateTime.Now;
                    node3.CreateDate = DateTime.Now;
                    node4.CreateDate = DateTime.Now;
                    node5.CreateDate = DateTime.Now;

                    _wfTaskApp.Update(node1);
                    _wfTaskApp.OriginalAdd(node2);
                    _wfTaskApp.OriginalAdd(node3);
                    _wfTaskApp.OriginalAdd(node4);
                    _wfTaskApp.OriginalAdd(node5);


                }
                catch (Exception ex)
                {
                    // 删除案件记录
                    _droneCaseInfoFLHApp.DeleteCase(nonGrainCaseInfo.Id);
                    // 打印错误
                    Console.WriteLine("当前时间：" + DateTime.Now.ToString());
                    Console.WriteLine("异常信息：" + ex.Message);
                    Console.WriteLine("异常对象：" + ex.Source);
                    Console.WriteLine("调用堆栈：\n" + ex.StackTrace);
                    throw;
                }
            }
        }
        Console.WriteLine("==============================================");
    }
}