package com.nmgs.service.impl;

import com.nmgs.mapper.Mapper;
import com.nmgs.service.IntegrationListService;
import com.nmgs.util.LogUtil;
import com.nmgs.util.PropertiesUtil;
import com.nmgs.util.PubTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class IntegrationListServiceImpl implements IntegrationListService {

    @Autowired
    public Mapper mapperI;
    private static String  DBType= PropertiesUtil.getValue("DBType");
    private static String sql="select " +
            " ROWNUM AS num,ilt.ID,ilt.USERID ,COALESCE(ilt.ACARNO,'') AS ACARNO," +
            " ROUND((ilt.LASTINTEGRATION/100),1) AS LASTINTEGRATION ," +
            " (CASE WHEN substr(ilt.PAYTYPE,1,1)='F' THEN '-' || ROUND((ilt.PAYINTEGRATION/100),1) ELSE '+'||ROUND((ilt.PAYINTEGRATION/100),1) END) AS PAYINTEGRATION ," +
            " ROUND((ilt.RESTINTEGRATION/100),1) AS RESTINTEGRATION," +
            " TO_CHAR(ilt.PAYTIME, 'YYYY-MM-DD HH24:MI:SS') AS PAYTIME," +
            " ilt.BAK1,ilt.BAK2,ilt.BAK3," +
            " pt.TEXT as PAYTYPENAME" +
            " from integration_list_table ilt " +
            " left join peytype_text  pt on(ilt.PAYTYPE = pt.value)" +
            " left join WEIXN_USER_TABLE  ut on(ilt.USERID = ut.ID)" +
            " where 1=1 ";
    static{
        if("4".equals(DBType)){
            sql="select " +
                    " ilt.ID,ilt.USERID ,COALESCE(ilt.ACARNO,'') AS ACARNO," +
                    " ROUND((ilt.LASTINTEGRATION/100),1) AS LASTINTEGRATION ," +
                    " (CASE WHEN substr(ilt.PAYTYPE,1,1)='F' THEN '-' || ROUND((ilt.PAYINTEGRATION/100),1) ELSE '+'||ROUND((ilt.PAYINTEGRATION/100),1) END) AS PAYINTEGRATION ," +
                    " ROUND((ilt.RESTINTEGRATION/100),1) AS RESTINTEGRATION," +
                    " date_format(ilt.PAYTIME, '%Y-%m-%d %H:%i:%s') AS PAYTIME," +
                    " ilt.BAK1,ilt.BAK2,ilt.BAK3," +
                    " pt.TEXT as PAYTYPENAME" +
                    " from integration_list_table ilt " +
                    " left join peytype_text  pt on(ilt.PAYTYPE = pt.value)" +
                    " left join WEIXN_USER_TABLE  ut on(ilt.USERID = ut.ID)" +
                    " where 1=1 ";
        }
    }
    @Override
    public List getIntegrationList(String userId, String aCarNo,String payType,int start,int end) {
        List list=null;
        String sqlTemp=sql;
        try{
            if("2".equals(DBType)){
                sqlTemp += " and ut.OPENID = '"+userId+"'";
                if(!PubTools.isNull(aCarNo)){
                    sqlTemp += " and ilt.ACARNO = '"+aCarNo+"'";
                }
                if("ADD".equals(payType)){
                    sqlTemp +=" and (substr(ilt.PAYTYPE,1,1) ='A' OR substr(ilt.PAYTYPE,1,1) ='T')  ";
                }else if("DEC".equals(payType)){
                    sqlTemp +=" and substr(ilt.PAYTYPE,1,1) ='F'  ";
                }
                if(start>0){
                    sqlTemp +=" AND ROWNUM >="+start;
                }
                sqlTemp +=" order by ilt.PAYTIME DESC";
                if(start==0 && end==0){
                    list= this.mapperI.selectforlist(sqlTemp);
                }else{
                    String startSql="SELECT ID,USERID,ACARNO,LASTINTEGRATION,PAYINTEGRATION,RESTINTEGRATION,PAYTIME," +
                            "PAYTYPENAME,BAK1,BAK2,BAK3 FROM("+sqlTemp+")tt where tt.num <= "+end;
                    list= this.mapperI.selectforlist(startSql);
                }
            }else if("4".equals(DBType)){//mysql数据库
                sqlTemp += " and ut.OPENID = '"+userId+"'";
                if(!PubTools.isNull(aCarNo)){
                    sqlTemp += " and ilt.ACARNO = '"+aCarNo+"'";
                }
                if("ADD".equals(payType)){
                    sqlTemp +=" and substr(ilt.PAYTYPE,1,1) ='A'  ";
                }else if("DEC".equals(payType)){
                    sqlTemp +=" and substr(ilt.PAYTYPE,1,1) ='F'  ";
                }
                sqlTemp +=" order by ilt.PAYTIME DESC";
                if(start>0){
                    sqlTemp +=" Limit "+(start-1)+","+(end-start+1);
                }
                if(start==0 && end==0){
                    list= this.mapperI.selectforlist(sqlTemp);
                }else{
                    String startSql="SELECT ID,USERID,ACARNO,LASTINTEGRATION,PAYINTEGRATION,RESTINTEGRATION,PAYTIME," +
                            "PAYTYPENAME,BAK1,BAK2,BAK3 FROM("+sqlTemp+")tt";
                    list= this.mapperI.selectforlist(startSql);
                }
            }else{}

        }catch (Exception e){
            LogUtil.WriteLog_Error("根据用户id查询积分明细===>"+e.getMessage(),"IntegrationListServiceImpl");
            return list=null;
        }
        return list;
    }
}
