using PoJun.Dapper;
using ASHermed.SinoInterface.Api.IRepository.Test;
using ASHermed.SinoInterface.Entity.Test;
using ASHermed.SinoInterface.Repository.MySql;
using ASHermed.SinoInterface.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.IRepository;

namespace ASHermed.SinoInterface.Api.MySqlRepository
{

    public class PubUserAddressRepository : BaseRepositoryToASHermed_SinoInterface<Pub_User_Address>, IPubUserAddressRepository
    {
        public int InsertPubUserAddress(Pub_User_Address userAddress)
        {
            var parms = new Dictionary<string, object>();
            var selectSql = @"insert into pub_user_address (UserId,
	                                                        ReceivePhone,
	                                                        ReceiveName,
	                                                        Address,
	                                                        Province,
	                                                        ProvinceId,
	                                                        City,
	                                                        CityId,
	                                                        Area,
	                                                        AreaId,
	                                                        Localization,
	                                                        CreateTime,
	                                                        UpdateTime,
	                                                        Remark,
	                                                        IsDefaultAddress,
                                                            IsDelete
                                                ) values (@UserId,
	                                                        @ReceivePhone,
	                                                        @ReceiveName,
	                                                        @Address,
	                                                        @Province,
	                                                        @ProvinceId,
	                                                        @City,
	                                                        @CityId,
	                                                        @Area,
	                                                        @AreaId,
	                                                        @Localization,
	                                                        @CreateTime,
	                                                        @UpdateTime,
	                                                        @Remark,
	                                                        @IsDefaultAddress,@IsDelete);select @@identity; ";
            parms.Add("@UserId", userAddress.UserId);
            parms.Add("@ReceivePhone", userAddress.ReceivePhone);
            parms.Add("@ReceiveName", userAddress.ReceiveName);
            parms.Add("@Address", userAddress.Address);
            parms.Add("@Province", userAddress.Province);
            parms.Add("@ProvinceId", userAddress.ProvinceId);
            parms.Add("@City", userAddress.City);
            parms.Add("@CityId", userAddress.CityId);
            parms.Add("@Area", userAddress.Area);
            parms.Add("@AreaId", userAddress.AreaId);
            parms.Add("@Localization", userAddress.Localization);
            parms.Add("@CreateTime", userAddress.CreateTime);
            parms.Add("@UpdateTime", userAddress.UpdateTime);
            parms.Add("@Remark", userAddress.Remark);
            parms.Add("@IsDefaultAddress", userAddress.IsDefaultAddress);
            parms.Add("@IsDelete", userAddress.IsDelete);
            var result = Execute(selectSql, parms);
            return result;
        }

        public int InsertPubDeliverAddress(Pub_Deliver_Address deliverAddress)
        {
            var parms = new Dictionary<string, object>();
            var selectSql = @"insert into pub_deliver_address (OrderNo,
	                                                        ReceivePhone,
	                                                        ReceiveName,
	                                                        Address
                                                ) values (@OrderNo,
	                                                        @ReceivePhone,
	                                                        @ReceiveName,
	                                                        @Address);";
            parms.Add("@OrderNo", deliverAddress.OrderNo);
            parms.Add("@ReceivePhone", deliverAddress.ReceivePhone);
            parms.Add("@ReceiveName", deliverAddress.ReceiveName);
            parms.Add("@Address", deliverAddress.Address);
         
            var result = Execute(selectSql, parms);
            return result;
        }

        public int UpdatePubUserAddress(Pub_User_Address userAddress)
        {
            var parms = new Dictionary<string, object>();
            StringBuilder str = new StringBuilder();
            str.Append(@"update pub_user_address set
                            UserId =@UserId, 
                            ReceivePhone=@ReceivePhone,
                            ReceiveName=@ReceiveName,
                            Address=@Address,
                            Province=@Province,
                            ProvinceId=@ProvinceId,
                            City=@City,
                            CityId=@CityId,
                            Area=@Area,
                            AreaId=@AreaId,
                            Localization=@Localization,
                            UpdateTime=@UpdateTime,
                            Remark=@Remark,
                            IsDefaultAddress=@IsDefaultAddress,
                            IsDelete=@IsDelete
                            where Id=@Id
                            ");
            parms.Add("@UserId", userAddress.UserId);
            parms.Add("@ReceivePhone", userAddress.ReceivePhone);
            parms.Add("@ReceiveName", userAddress.ReceiveName);
            parms.Add("@Address", userAddress.Address);
            parms.Add("@Province", userAddress.Province);
            parms.Add("@ProvinceId", userAddress.ProvinceId);
            parms.Add("@City", userAddress.City);
            parms.Add("@CityId", userAddress.CityId);
            parms.Add("@Area", userAddress.Area);
            parms.Add("@AreaId", userAddress.AreaId);
            parms.Add("@Localization", userAddress.Localization);
            parms.Add("@CreateTime", userAddress.CreateTime);
            parms.Add("@UpdateTime", userAddress.UpdateTime);
            parms.Add("@Remark", userAddress.Remark);
            parms.Add("@IsDefaultAddress", userAddress.IsDefaultAddress);
            parms.Add("@IsDelete", userAddress.IsDelete);
            parms.Add("@Id", userAddress.Id);
            var result = Execute(str.ToString(), parms);
            return result;
        }

        public int DeletePubUserAddress(int addId)
        {
            var parms = new Dictionary<string, object>();
            StringBuilder str = new StringBuilder();
            str.Append(@"update pub_user_address set
                            IsDelete=@IsDelete
                            where Id=@Id
                            ");

            parms.Add("@IsDelete", true);
            parms.Add("@Id", addId);
            var result = Execute(str.ToString(), parms);
            return result;
        }

        public Pub_Deliver_Address GetDeliverAddress(string orderNo)
        {
            var parms = new Dictionary<string, object>();
            StringBuilder str = new StringBuilder();
            str.Append(@"select * from Pub_Deliver_Address 
                            where orderNo = @orderNo");
            
            parms.Add("@orderNo", orderNo);
            var result = Query<Pub_Deliver_Address>(str.ToString(), parms);
            return result.FirstOrDefault();
        }

        public List<Pub_User_Address> GetAddress(int userId, int id)
        {
            var parms = new Dictionary<string, object>();
            StringBuilder str = new StringBuilder();
            str.Append(@"select Id,ReceivePhone,ReceiveName,Address,Province,ProvinceId,City,CityId,Area,AreaId,Localization,Remark,IsDefaultAddress from pub_user_address 
                            where IsDelete=0 and userId = @userId ");
            if (id != 0)
            {
                str.Append(" and id=@Id");
                parms.Add("@Id", id);
            }
            str.Append(" ORDER BY UpdateTime desc,CreateTime desc");
            parms.Add("@userId", userId);
            var result = Query<Pub_User_Address>(str.ToString(), parms);
            return result.ToList();
        }
        public Pub_User_Address GetAddressForId(int id)
        {
            var parms = new Dictionary<string, object>();
            StringBuilder str = new StringBuilder();
            str.Append(@"select Id,ReceivePhone,ReceiveName,Address,Province,ProvinceId,City,CityId,Area,AreaId,Localization,Remark,IsDefaultAddress from pub_user_address 
                            where IsDelete=0 
                            ");
            str.Append(" and id=@Id");
            parms.Add("@Id", id);
            var result = Query<Pub_User_Address>(str.ToString(), parms).FirstOrDefault();
            return result;
        }
        public int ShutOtherDefault(int UserId)
        {
            var parms = new Dictionary<string, object>();
            StringBuilder str = new StringBuilder();
            str.Append(@"update pub_user_address set
                            IsDefaultAddress=@IsDefaultAddress
                            where UserId=@UserId
                            ");

            parms.Add("@IsDefaultAddress", 0);
            parms.Add("@UserId", UserId);
            var result = Execute(str.ToString(), parms);
            return result;
        }



    }


}
