public bool AddPolicyCustomerRelation(CorpPolicyConfigCustomerModel model)
        {
            //1.先清空所有关系
            List <CorpPolicyConfigCustomerEntity> corpPolicyConfigCustomerEntities =
                _corpPolicyConfigCustomerDal.Query <CorpPolicyConfigCustomerEntity>(n => n.PolicyId == model.PolicyId)
                .ToList();

            if (corpPolicyConfigCustomerEntities.Count > 0)
            {
                foreach (var corpPolicyConfigCustomerEntity in corpPolicyConfigCustomerEntities)
                {
                    _corpPolicyConfigCustomerDal.Delete <CorpPolicyConfigCustomerEntity>(corpPolicyConfigCustomerEntity.Id);
                }
            }

            //2.在插入关联信息
            if (model.CidList != null && model.CidList.Count > 0)
            {
                foreach (int cid in model.CidList)
                {
                    _corpPolicyConfigCustomerDal.Insert <CorpPolicyConfigCustomerEntity>(new CorpPolicyConfigCustomerEntity()
                    {
                        Cid      = cid,
                        PolicyId = model.PolicyId
                    });
                }
            }


            return(true);
        }
Example #2
0
        public bool AddPolicyDepartmentRelation(CorpPolicyConfigDepartmentModel model)
        {
            List <CorpPolicyConfigDepartmentEntity> configDepartmentEntities =
                _corpPolicyConfigDepartmentDal.Query <CorpPolicyConfigDepartmentEntity>(
                    n => n.PolicyId == model.PolicyId, true)
                .ToList();                                                                 //根据政策id获取与部门的关系信息

            bool isClearAll = (model.DepartmentIdList.Find(n => n.Value == true) == null); //是否清空所有关系

            if (!isClearAll)
            {
                foreach (var departKeyValue in model.DepartmentIdList)
                {
                    var configDepartment = configDepartmentEntities.Find(n => n.DepartmentId == departKeyValue.Key);

                    if (configDepartment == null && departKeyValue.Value)//不存在关系,并且勾选了部门
                    {
                        //新增
                        _corpPolicyConfigDepartmentDal.Insert <CorpPolicyConfigDepartmentEntity>(new CorpPolicyConfigDepartmentEntity()
                        {
                            DepartmentId = departKeyValue.Key,
                            PolicyId     = model.PolicyId
                        });
                    }

                    if (configDepartment != null && !departKeyValue.Value)//存在关系,并且不选部门
                    {
                        //删除部门关系
                        _corpPolicyConfigDepartmentDal.Delete <CorpPolicyConfigDepartmentEntity>(configDepartment.Id);
                        //删除员工关系
                        List <CorpPolicyConfigCustomerEntity> corpPolicyConfigCustomerEntities =
                            _corpPolicyConfigCustomerDal.Query <CorpPolicyConfigCustomerEntity>(
                                n => n.PolicyId == model.PolicyId, true).ToList();
                        if (corpPolicyConfigCustomerEntities.Count > 0)
                        {
                            foreach (var entity in corpPolicyConfigCustomerEntities)
                            {
                                _corpPolicyConfigCustomerDal.Delete <CorpPolicyConfigCustomerEntity>(entity.Id);
                            }
                        }
                    }
                }
            }
            else
            {
                //清空所有关系
                if (configDepartmentEntities.Count > 0)
                {
                    foreach (var entity in configDepartmentEntities)
                    {
                        _corpPolicyConfigDepartmentDal.Delete <CorpPolicyConfigDepartmentEntity>(entity.Id);
                    }
                }

                //在清空与员工的关系
                List <CorpPolicyConfigCustomerEntity> corpPolicyConfigCustomerEntities =
                    _corpPolicyConfigCustomerDal.Query <CorpPolicyConfigCustomerEntity>(
                        n => n.PolicyId == model.PolicyId, true).ToList();
                if (corpPolicyConfigCustomerEntities.Count > 0)
                {
                    foreach (var entity in corpPolicyConfigCustomerEntities)
                    {
                        _corpPolicyConfigCustomerDal.Delete <CorpPolicyConfigCustomerEntity>(entity.Id);
                    }
                }
            }


            return(true);
        }