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 IntegralFlowRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Integral_Flow>, IIntegralFlowRepository
    {


        public int InsertPubIntegralFlow(Pub_Integral_Flow pubIntegralFlow)
        {
            StringBuilder str = new StringBuilder();
            str.Append("insert into Pub_Integral_Flow ");
            str.Append(@"(PHONE_NO,
                            CHANGED_INTEGRAL,
                            CHANGED_TYPE,
                            CHANGED_TYPE_STR,
                            STATUS,
                            CREATE_USER,
                            CREATE_TIME,
                            UPDATE_TIME,
                            UPDATE_USER,
                            REMARK) ");
            str.Append(" values ");
            str.Append(@"(@PHONE_NO,
                          @CHANGED_INTEGRAL,
                          @CHANGED_TYPE,
                          @CHANGED_TYPE_STR,
                          @STATUS,
                          @CREATE_USER,
                          @CREATE_TIME,
                          @UPDATE_TIME,
                          @UPDATE_USER,
                          @REMARK); ");
            str.Append("select @@identity; ");
            var parms = new Dictionary<string, object>();
            parms.Add("@PHONE_NO", pubIntegralFlow.Phone_NO);
            parms.Add("@CHANGED_INTEGRAL", pubIntegralFlow.Changed_Integral);
            parms.Add("@CHANGED_TYPE", pubIntegralFlow.Changed_Type);
            parms.Add("@CHANGED_TYPE_STR", pubIntegralFlow.Changed_Type_Str);
            parms.Add("@STATUS", pubIntegralFlow.Status);
            parms.Add("@CREATE_USER", pubIntegralFlow.Create_User);
            parms.Add("@CREATE_TIME", pubIntegralFlow.Create_Time);
            parms.Add("@UPDATE_TIME", pubIntegralFlow.Update_Time);
            parms.Add("@UPDATE_USER", pubIntegralFlow.Update_User);
            parms.Add("@REMARK", pubIntegralFlow.Remark);
            var result = Execute(str.ToString(), parms);
            return result;
        }

        public int SelectTotalIntegralInMonth(string phone)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@phone", phone);
            var selectSql = @"select CASE
        WHEN sum(CHANGED_INTEGRAL) is null THEN
            0
        ELSE
            sum(CHANGED_INTEGRAL)
        END as totalIntegral
        from pub_integral_flow where PHONE_NO = @phone and CHANGED_TYPE = 1
        and DATE_FORMAT(NOW(), '%Y-%m') = DATE_FORMAT(CREATE_TIME, '%Y-%m') and status != 0";
            var result = Query<int>(selectSql, parms);
            return result.FirstOrDefault();
        }
    }


}
