package com.chinacreator.process.dao;

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

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * WO+会员产品订购次月初短信
 */
@Component
public class MonthlyOrderSmsDao {

    //非复合产品sql
    public static String sql = "select t.*,b.spname from \n" +
            "(select a.* from \n" +
            "(select userid,spid,cpid from TD_BUSSINESS_HANDLE \n" +
            "where fhcpid is null \n" +
            "and fhspid is null\n" +
            "and viptime between to_date(?,'yyyy-mm-dd hh24:mi:ss')\n" +
            "and to_date(?,'yyyy-mm-dd hh24:mi:ss')\n" +
            "and spid in (select spid from tb_vip_sp_info where isfchpro = '0')\n" +
            "and vipstatus = '0')a,\n" +
            "(select userid,spid,cpid\n" +
            "from td_order_relations \n" +
            "where status = '0'\n" +
            "and canceltime IS NULL\n" +
            "and spid in (select spid from tb_vip_sp_info where isfchpro = '0'))b\n" +
            "where a.userid = b.userid and a.cpid = b.cpid and a.spid = b.spid)t,\n" +
            "tb_sp_info b where t.spid = b.spid\n";

    //复合产品sql
    public static String fhSql = "select t.userid, t.fhspid spid, t.fhcpid cpid,b.spname from\n" +
            "(select a.userid,a.fhspid,a.fhcpid from \n" +
            "(select b.userid,b.fhspid,b.fhcpid from TD_BUSSINESS_HANDLE b\n" +
            "where b.fhcpid is not null \n" +
            "and b.fhspid is not null\n" +
            "and b.viptime between to_date(?,'yyyy-mm-dd hh24:mi:ss')\n" +
            "and to_date(?,'yyyy-mm-dd hh24:mi:ss')\n" +
            "and b.vipstatus = '0'\n" +
            "group by userid,fhspid,fhcpid )a,\n" +
            "(select t.userid,t.spid,t.cpid\n" +
            "from td_order_relations t\n" +
            "where t.status = '0'\n" +
            "and t.canceltime IS NULL\n" +
            "and t.spid in (select spid from tb_vip_sp_info where isfchpro = '1')\n" +
            "group by t.userid,t.spid,t.cpid) b\n" +
            "where a.fhcpid = b.cpid and a.fhspid = b.spid and a.userid = b.userid) t,\n" +
            "tb_sp_info b where t.fhspid = b.spid";

    /**
     * 查询WO+会员产品订购次月初短信集合
     *
     * @return 返回userId, spid, cpid
     * @throws SQLException SQL异常
     */
    public List<MonthlyOrderSmsBean> queryUsersList(String type) throws SQLException {
        System.out.println("======getUserListStart======");
        Map dateMap = getDate();
        String daoSql = sql;
        if ("0".equals(type)) {
            daoSql = fhSql;
        }
        String startDay = dateMap.get("startDay").toString();
        String endDay = dateMap.get("endDay").toString();
        List<MonthlyOrderSmsBean> beans = SQLExecutor.queryListWithDBName(MonthlyOrderSmsBean.class, DataSource.NET3G,
                daoSql, startDay + " 00:00:00", endDay + " 23:59:59");
        System.out.println("======getUserListEnd======");
        return beans;
    }


    public boolean saveInfo(MonthlyOrderSmsBean bean) {

        String sql = "insert into tb_send_monthly_sms_log(userid,spid,cpid,sendmonth,INSERTTIME,CONTEXT) \n" +
                "values(#[userid],#[spid],#[cpid],#[sendmonth],sysdate,#[context])";
        try {
            SQLExecutor.insertBean(DataSource.NET3G, sql, bean);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 保存发送情况
     * @param bean
     * @return
     */
    public boolean saveSendCaseLog(MonthlyOrderSmsBean bean) {
        String sql = "insert into tb_send_monthly_case_log(userid,spid,cpid,resultcode,errorinfo,sendmonth,inserttime) \n" +
                "values(#[userid],#[spid],#[cpid],#[resultcode],#[errorinfo],#[sendmonth],sysdate)";
        try {
            SQLExecutor.insertBean(DataSource.NET3G, sql, bean); 
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public Map getDate() {
        Map<String, String> resultMap = new HashMap<String, String>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.setTime(new Date());
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String startDay = format.format(cal_1.getTime());
        resultMap.put("startDay", startDay);
        cal_1.set(Calendar.DAY_OF_MONTH, 2);
        String endDay = format.format(cal_1.getTime());
        resultMap.put("endDay", endDay);
        return resultMap;
    }

}
