package com.sifang.modules.insurance.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.insurance.entity.CrmCustomerNoSystemEntryInsurance;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;

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

@Repository
public class CrmCustomerNoSystemEntryInsuranceDao extends BaseDao<CrmCustomerNoSystemEntryInsurance>  {

    public long queryCount(Map<String, String> queryParams) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select count(*) ")
                .line("from crm_customer_entry_insurance a")
                .line("where 1=1");
        queryCondition(queryParams, builder);
        return Db.queryLong(builder.build(), builder.params());
    }

    /**
     * 保险记录列表
     *
     * @param queryParams
     * @param page
     * @return
     */
    public List<Record> list(Map<String, String> queryParams, LayuiPage page) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select a.id,a.status,a.item_no,a.number,a.insurance_money,a.price,a.tenant_name,a.project_name,a.trader_name,a.address,a.order_status,a.partner_name,a.start_time,a.end_time,")
                .line("a.insurance_company_name,a.policyholder,a.insurant,a.create_time,a.remarks,a.request_no,a.source,a.down_url,a.order_time,a.partner_name, a.remove_sign,a.renewal_status,a.input_piece_id ")
                .line("from crm_customer_no_system_entry_insurance a")
                .line("where 1=1");
        queryCondition(queryParams, builder);
        builder.line("order by a.create_time desc");
        builder.limit(page.getPage(), page.getLimit());
        return Db.find(builder.build(), builder.params());
    }

    public void queryCondition(Map<String, String> queryParams, SqlBuilder builder) {
        if (queryParams == null || queryParams.size() == 0) {
            return;
        }
        if (StringUtil.isNotBlank(queryParams.get("insuranceCompanyName"))) {
            builder.line("and a.insurance_company_name=?", queryParams.get("insuranceCompanyName"));
        }
        if (StringUtil.isNotBlank(queryParams.get("traderName"))) {
            builder.line("and a.trader_name=?", queryParams.get("traderName"));
        }
        if (StringUtil.isNotBlank(queryParams.get("powerStationCode"))) {
            builder.line("and a.item_no=?", queryParams.get("powerStationCode"));
        }
        if (StringUtil.isNotBlank(queryParams.get("tenantName"))) {
            builder.like("and a.tenant_name like ?", queryParams.get("tenantName"));
        }
        if (StringUtil.isNotBlank(queryParams.get("address"))) {
            builder.like("and a.address like ?", queryParams.get("address"));
        }
        if (StringUtil.isNotBlank(queryParams.get("startTimeMin"))) {
            builder.like("and a.start_time >= ?", queryParams.get("startTimeMin"));
        }
        if (StringUtil.isNotBlank(queryParams.get("startTimeMax"))) {
            builder.like("and a.start_time <= ?", queryParams.get("startTimeMax"));
        }
        if (StringUtil.isNotBlank(queryParams.get("endTimeMin"))) {
            builder.like("and a.end_time >= ?", queryParams.get("endTimeMin"));
        }
        if (StringUtil.isNotBlank(queryParams.get("endTimeMax"))) {
            builder.like("and a.end_time <= ?", queryParams.get("endTimeMax"));
        }
        // 推送时间
        if (StringUtil.isNotBlank(queryParams.get("orderTimeMin"))) {
            builder.line("and a.order_time >= ?", queryParams.get("orderTimeMin"));
        }
        if (StringUtil.isNotBlank(queryParams.get("orderTimeMax"))) {
            String orderTimeMax = queryParams.get("orderTimeMax");
            Date date = DateUtils.parseDate(orderTimeMax);
            String tomorrow = DateUtils.formatDate(DateUtils.addDays(date, 1), "yyyy-MM-dd");
            builder.line("and a.order_time < ?", tomorrow);
        }
        if (StringUtil.isNotBlank(queryParams.get("orderStatus"))) {
            builder.line("and a.order_status = ?", queryParams.get("orderStatus"));
        }
        if (StringUtil.isNotBlank(queryParams.get("partnerName"))) {
            builder.line("and a.partner_name = ?", queryParams.get("partnerName").toString());
        }
        if (StringUtil.isNotBlank(queryParams.get("policyType"))) {
            builder.line("and a.policy_type = ?", queryParams.get("policyType"));
        }
        if (StringUtil.isNotBlank(queryParams.get("removeSign"))) {
            builder.line("and a.remove_sign = ?", queryParams.get("removeSign"));
        }
    }

    public Record selectDetailById(String id) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select a.id,a.status,a.item_no,a.number,a.insurance_money,a.price,a.tenant_name,a.project_name,a.trader_name,a.address,a.process_detail,a.project_company_name,a.insurance_name,")
                .line("a.insurance_company_name,a.policyholder,a.insurant,a.create_time,a.remarks,a.request_no,a.source,a.down_url,a.order_time,a.partner_name, a.remove_sign,a.renewal_status,a.input_piece_id ")
                .line(" ,a.start_time ,a.end_time ,a.province,a.city ,a.area ")
                .line("from crm_customer_no_system_entry_insurance a")
                .line("where a.id=?", id);
        return Db.findFirst(builder.build(), builder.params());
    }

}
