using JLHHJSvr.DBA.DBModle;
using JLHHJSvr.LJException;
using LJLib.DAL.SQL;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JLHHJSvr.BLL
{
    internal class FuncPowerHelper
    {
        public static Dictionary<int, string> _stupidFunc = new Dictionary<int, string>
        {
            {703,"销售报价单隐藏成本" },
            {2179,"销售发货单不显示客户欠款情况" },
            {2402,"销售发货单单价不允许编辑" },
            {2403,"销售订单单价不允许编辑" },
            {2635,"销售订单必须选择订单分类" },
            {3066,"销售发货单仓审后不能打印" },
            {3158,"销售订单折扣不允许编辑" },
            {3159,"销售发货单折扣不允许编辑" },
            {3200,"调出明细只能选公共库存" },
            {3201,"调货时只能调空闲库存" },
            {3672,"销售订单0单价不允许保存" },
            {3703,"只允许修改自己及下级用户建立的单据" },
            {3704,"只允许修改自己及下级用户建立的单据" },
            {3712,"不允许审核订单明细没有默认供应商的采购订单" },
            {3713,"不允许审核订单明细没有申购单的采购订单" },
            {3714,"不允许审核订单明细没有采购合同的采购订单" },
            {3715,"不能仓库审核有实发数与计划发货数不相等的发货单" },
            {3847,"明细只能选择工序" },
            {3890,"客户补件单确认后才能打印" },
            {3905,"明细不能选择更新原订单" },
            {3910,"已打印的销售发货单不能撤审" },
            {3911,"销售发货单不允许重复打印" },
            {3990,"销售订单审核检查审核日期+生产周期<=订单明细交期" },
            {4141,"建立日期和计件月份不允许修改" },
            {4169,"不允许查看无效物料" },
            {4200,"销售发货单仓审日期不能早于销售日期" },
            {4354,"板式销售意向单单价不允许编辑" },
            {4355,"板式销售意向单折扣不允许编辑" },
            {4365,"不允许审核订单明细有0单价的采购订单" },
            {4366,"不允许审核订单明细有0单价的采购订单" },
            {4400,"销售发货单0单价不允许保存" },
            {4427,"销售报价不能低于标准销售价" },
            {4428,"销售订单严格按照客户指定的货运部选择" },
            {4429,"销售发货单严格按照客户指定的货运部选择" },
            {4535,"不允许编辑单价" },
            {4555,"单价少于定价不能审核" },
            {4615,"编辑状态下不允许删除明细" },
            {4616,"编辑状态下不允许删除明细" },
            {4690,"不允许审核订单明细有0单价的采购订单" },
            {4843,"必须选销售退货单明细" },
            {4883,"不允许重复打印派工单的权限" },
            {5078,"不允许编辑工价" },
            {5168,"不允许修改单价" },
            {5201,"销售限价为0不能审核" },
            {5207,"销售退货单不允许重复打印" },
            {5274,"只能查看自己建立的单据" },
            {5289,"明细没有合同不允许审核" },
            {5290,"明细没有单价不允许审核" },
            {5291,"明细没有默认供应商不允许审核" },
            {5599,"隐藏规格参数页" },
            {5633,"只允许查看自己建立的单据" },
            {5634,"只允许查看自己建立的单据" },
            {5734,"不允许编辑单价" },
            {5738,"不允许修改明细员工" },
            {5739,"不允许修改客户折扣" },
            {5740,"不允许修改客户折扣" },
            {5795,"限制派工员工受限于工种技术等级" },
            {5888,"只允许修改自己及下级用户建立的单据" },
            {6007,"税率为0不允许保存" },
            {6008,"税率为0不允许保存" },
            {6027,"只允许操作有权限工段的排程" },
            {6256,"隐藏绩效分页" },
            {6371,"借用原因只能选不能录入" },
            {6390,"报销内容不能为空白或金额为0" },
            {6444,"定价为0不能审核" },
            {6502,"只能显示自己建立的单据" },
            {6503,"只能修改自己建立的单据" },
            {6527,"财审前修改费用明细" },
            {6548,"不允许保存调入日期和调出日期不一致的单据" },
            {6573,"隐藏费用明细页" },
            {6580,"出口销售订单单价不允许编辑" },
            {6725,"限制录入客户信息" },
            {6727,"销售发货单0税率不允许保存" },
            {6777,"只能修改自己建立的单据" },
            {6807,"不允许修改客户币种" },
            {6808,"不允许修改供应商币种" },
            {6809,"不允许修改客户币种" },
            {6835,"必须选择部门" },
            {6836,"必须选择部门" },
            {6845,"不允许保存订单明细0单价的采购订单" },
        };
        /// <summary>
        /// 包含对反转权限的转义，有权限/无限制返回true，无权限/有限制返回false
        /// </summary>
        public static bool CheckFuncPower(SqlCommand cmd, int empid, int funcid)
        {
            if (empid == 0)
            {
                return true;
            }
            var user = new u_user_jlhprice() { empid = empid };
            if (DbSqlHelper.SelectOne(cmd, user, "Empid,rightstring") != 1)
            {
                throw new LJCommonException(string.Format("查询用户信息失败,empid:{0}", empid));
            }
            var ifStupidFunc = false;
            if (_stupidFunc.ContainsKey(funcid))
            {
                ifStupidFunc = true;
            }
            else
            {
                cmd.CommandText = "SELECT parentid FROM sys_func_pwr WHERE funcid = " + funcid;
                cmd.Parameters.Clear();
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        if (Convert.ToInt32(reader["parentid"]) == 521)
                        {
                            ifStupidFunc = true;
                        }
                    }
                }
            }            
            var funcVal = GetFuncPowerVal(funcid, user.rightstring);
            return funcVal != ifStupidFunc;
        }
        private static bool GetFuncPowerVal(int funcid, string sys_pwrstr)
        {
            bool rslt;
            rslt = funcid > 0 && sys_pwrstr.Length >= funcid &&
                       sys_pwrstr.Substring(funcid - 1, 1) == "1";
            return rslt;
        }
    }
}
