package com.chinacreator.process.dao;

import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.PreparedDBUtil;
import com.frameworkset.common.poolman.SQLExecutor;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * @author 黑猫警长
 * @create 2021/3/30 9:35
 *
 */
@Component
public class OrderPushDao {
    /**
     * 获取TD_ORDER_RELATIONS_CHILDREN表RESULTCODE为待处理且CALCULATEDATE为NULL或CALCULATEDATE小于当前时间
     * @param partition 分区标识
     * @param rows 每次取多少条数据
     * @return
     * @throws SQLException
     */
    public List<HashMap> getOrderData(String partition, int rows)throws SQLException {
        String sql = " SELECT ID, USERID, SPID,RESULTCODE,TO_CHAR(CALCULATEDATE,'YYYYMMDDHH24MISS') CALCULATEDATE FROM TD_ORDER_RELATIONS_CHILDREN PARTITION("+partition+") WHERE RESULTCODE = '1' AND (CALCULATEDATE IS NULL OR SYSDATE >= CALCULATEDATE ) ";
        PreparedDBUtil pdb = new PreparedDBUtil();
        return pdb.executeSelectForList(DataSource.NET3G, sql,0,rows, HashMap.class);
    }


    /**
     * 更新resultcode ， resultinfo状态
     * @param id
     * @param resultcode
     * @param resultinfo
     * @return
     * @throws SQLException
     */
    public boolean updatePush(String id, String resultcode, String resultinfo)throws SQLException {
        String sql = "UPDATE TD_ORDER_RELATIONS_CHILDREN SET RESULTCODE = ? , REUSLTINFO = ? , UPDTIME = SYSDATE WHERE ID = ? ";
        Object rows = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, resultcode,resultinfo,id);
        if(Integer.parseInt(rows.toString()) > 0){
            return true;
        }else{
            return false;
        }

    }


    /**
     * 更新resultcode ， resultinfo状态
     * @param id
     * @param resultcode
     * @param resultinfo
     * @return
     * @throws SQLException
     */
    public boolean updatePushAll(String id, String resultcode, String resultinfo,String realflag ,String status)throws SQLException {
        String sql = "UPDATE TD_ORDER_RELATIONS_CHILDREN SET RESULTCODE = ? , REUSLTINFO = ? , REALFLAG = ? , UPDTIME = SYSDATE ,CALCULATEDATE = TO_DATE(TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE,'MM'),+1),'YYYYMMDD')|| '003001', 'YYYYMMDDHH24MISS'),STATUS = ? WHERE ID = ? ";
        Object rows = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, resultcode,resultinfo,realflag,status,id);
        if(Integer.parseInt(rows.toString()) > 0){
            return true;
        }else{
            return false;
        }

    }


    public String getVaccproductId(String spid)throws SQLException {
        String sql = "SELECT VACPRODUCTID FROM TB_SP_INFO WHERE SPID = "+spid;
        PreparedDBUtil pdb = new PreparedDBUtil();
        return (String) pdb.executeSelectForObject(DataSource.NET3G, sql, String.class);
    }

    /**
     * 根据手机号码，spid获取订购关系
     * @param userid
     * @param spid
     * @return
     * @throws SQLException
     */
    public List<HashMap> findOrderRelaBySpid(String userid, String spid)throws SQLException {
        String sql = " SELECT CPID,SPID,USERID, TO_CHAR(ORDERTIME, 'yyyymmddhh24miss') ORDERTIME, " +
                " TO_CHAR(endtime, 'yyyymmddhh24miss') endtime, TO_CHAR(CANCELTIME, 'yyyymmddhh24miss') CANCELTIME, " +
                " (case when endtime is null then '0' when endtime >= sysdate then '1' else '2' end) STATUS " +
                " FROM TD_ORDER_RELATIONS WHERE  USERID = ? AND SPID = ? ";
        return SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql, userid, spid);

    }

}


