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.constant.UserConstants;
import com.sifang.modules.customer.entity.CrmCustomerEntryRentManagement;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Repository
public class CrmCustomerEntryRentManagementDao extends BaseDao<CrmCustomerEntryRentManagement> {
    /**
     * 列表数据
     */
    public List<Record> findPageData(LayuiPage page, Map<String, String> map, Operator operator) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("SELECT  a.* ,DATE_FORMAT(a.due_time,'%Y-%m-%d')AS dueTime ,DATE_FORMAT(a.paid_time,'%Y-%m-%d')AS paidTime  ");
        sql.line("FROM crm_customer_entry_rent_management a");
        sql.line("WHERE 1 = 1");
        queryCondition(sql, map, operator);
        sql.line("order by a.power_station_code,a.due_time ");
        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_rent_management a");
        sql.line("WHERE 1 = 1");
        queryCondition(sql, map, operator);
        return Db.queryLong(sql.build(), sql.params());
    }

    private void queryCondition(SqlBuilder sql, Map<String, String> map, Operator operator) {
        if(StringUtil.equalsAny(operator.getUserType(), UserConstants.TYPE_TRADER, User.USER_TYPE_ADVISOR)){
            Record record= CrmUtils.getUserTrader(operator.getUserType(),operator.getUserId());
            String traderName = record.getStr("name");
            sql.line(" and a.office_name =?", traderName );
        }
        if (StringUtil.checkParamsValid(map.get("traderIds"))) {
            sql.inst("and b.distributor_id IN ", Arrays.asList(map.get("traderIds").split(",")));
        }
        if (StringUtil.checkParamsValid(map.get("contractNumber"))) {
//            sql.like("and a.contract_number like ?", map.get("contractNumber"));
            sql.like("and a.partners_contract_number like ?", map.get("contractNumber"));
        }
        if (StringUtil.checkParamsValid(map.get("tenantName"))) {
            sql.like("and a.tenant_name  like ?", map.get("tenantName"));
        }
        if (StringUtil.checkParamsValid(map.get("stationName"))) {
            sql.like("and a.station_name  like ?", map.get("stationName"));
        }
        if (StringUtil.checkParamsValid(map.get("officeName"))) {
            sql.like("and a.office_name  like ?", map.get("officeName"));
        }
        if (StringUtil.checkParamsValid(map.get("powerStationCode"))) {
            sql.line("and a.power_station_code like concat('%', ?, '%')", map.get("powerStationCode").trim());
        }
        if (StringUtil.checkParamsValid(map.get("dueTime"))) {
            String dueTime = map.get("dueTime");
            String begin = dueTime + "-01 00:00:00";
            String end = dueTime +"-31 23:59:59";
            sql.line("and a.due_time  >=  ? and  a.due_time <= ?",begin,end);
        }
        if (StringUtil.checkParamsValid(map.get("paidTime"))) {
            String paidTime = map.get("paidTime");
            String begin = paidTime + " 00:00:00";
            String end = paidTime +" 23:59:59";
            sql.line("and a.paid_time  >=  ? and  a.paid_time <= ?",begin,end);
        }



    }
}
