package com.sifang.modules.customer.dao;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.entity.CrmCustomerEntryWeeklyRecord;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Repository
public class CrmCustomerEntryWeeklyRecordDao extends BaseDao<CrmCustomerEntryWeeklyRecord> {

    /**
     * 获取最小日期
     */
    public Date getMinDate(){
        SqlBuilder sql = new SqlBuilder();
        sql.line(" select creat_time from  crm_customer_entry_weekly_record order by  creat_time asc limit 1 ");
        return Db.queryDate(sql.build());
    }
    /**
     * 列表数据
     */
    public List<Record> findPageData(LayuiPage page, Map<String, String> map, Operator operator) {
        SqlBuilder sql = new SqlBuilder();
        sql.line(" SELECT  a.* ,b.province,b.leader_name ");
        sql.line(" FROM crm_customer_entry_weekly_record a ");
        sql.line(" LEFT JOIN crm_customer_entry_province_leader_info b ON b.id = a.province_rel_id ");
        sql.line(" WHERE 1 = 1");
        queryCondition(sql, map, operator);
        sql.line(" order by b.`province` desc");
        if (StrKit.notNull(page)) {
            sql.limit(page.getPage(), page.getLimit());
        }
        return Db.find(sql.build(), sql.params());
    }

    /**
     * 列表数据统计
     */
    public long findPageCount(Map<String, String> map, Operator operator) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("SELECT COUNT(1)");
        sql.line("FROM crm_customer_entry_weekly_record a ");
        sql.line("LEFT JOIN crm_customer_entry_province_leader_info b ON b.id = a.province_rel_id ");
        sql.line("WHERE 1 = 1");
        queryCondition(sql, map, operator);
        return Db.queryLong(sql.build(), sql.params());
    }
    /**
     * 获取指定日期的数据
     */
    public List<Record> getWeeklyRecordsByDate(String startDate,String endDate){
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT  a.* ,b.province,b.leader_name  ")
                .line(" FROM crm_customer_entry_weekly_record a ")
                .line(" LEFT JOIN crm_customer_entry_province_leader_info b ON b.id = a.province_rel_id ")
                .line("WHERE a.creat_time_sta = ?  and a.creat_time_end = ? ",startDate,endDate);
        return Db.find(builder.build(),builder.params());
    }

    private void queryCondition(SqlBuilder sql, Map<String, String> map, Operator operator) {
        // 统计时间
        if (StringUtil.checkParamsValid(map.get("creatTime"))) {
            String[] times = map.get("creatTime").split("~");
            String startTime = times[0];
            String endTime = times[1];
            sql.line("AND a.creat_time_sta = ?  AND a.creat_time_end =  ? ", startTime,endTime);
        }

    }

}
