using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.ContractModel.Framework.Login;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Api.IService;
using ASHermed.SinoInterface.ContractModel;
using ASHermed.SinoInterface.IFramework.SingleSignOn;
using PoJun.Util.Maps;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ASHermed.SinoInterface.Api.Service
{
    /// <summary>
	/// 积分流水
	/// </summary>
	public class PubMapUserTypeService : BaseService, IPubMapUserTypeService
    {
        #region 初始化
        IPubMapUserTypeRepository pubMapUserTypeRepository;
        public PubMapUserTypeService(IPubMapUserTypeRepository _pubMapUserTypeRepository)
        {
            pubMapUserTypeRepository = _pubMapUserTypeRepository;
        }
        #endregion

        public int SetUserType(string phone, int type)
        {
            Pub_Map_User_Type userType = pubMapUserTypeRepository.initLinq().Where(p => p.Phone_No == phone).Single();
            if (userType == null)
            {
                userType = new Pub_Map_User_Type();
                userType.Phone_No = phone;
                userType.User_Type = type;
                userType.Available_Coupon = 1;
                pubMapUserTypeRepository.InsertPubMapUserType(userType);
            }
            else
            {
                if (type == 8)
                {//宁波商会渠道用户
                    // 宁波商会要覆盖ruby融创
                    userType.User_Type = (userType.User_Type | 4) ^ 4 | type;
                }
                else if (type == 4 && (8 & userType.User_Type) == 0)
                {
                    // 融创需要当前用户不是宁波商会时才会加入标签
                    userType.User_Type = userType.User_Type | type;
                }
                else if (userType.User_Type == 1)
                {
                    // 当前用户是普通用户，直接覆盖，以免出现用户既是普通用户又是其他类型
                    userType.User_Type = type;
                }
                else if (type != 1)
                {
                    // 当前设置的标签不为普通，添加标签
                    userType.User_Type = type | userType.User_Type;
                }
                pubMapUserTypeRepository.UpdatePubMapUserType(userType);
            }
            return userType.Id;
        }


    }
}
