/**
 */
package com.sifang.modules.customer.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.UserConstants;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.entity.User;
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 com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.customer.entity.CustomerEntryRent;

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

/**
 * 租金信息DAO接口
 * @author lzp
 * @version 
 */
@Repository
public class CustomerEntryRentDao extends BaseDao<CustomerEntryRent> {

    public List<Record> findEntryRentList(LayuiPage<Record> page, Map<String, String> map, Operator operator){
        SqlBuilder sql = new SqlBuilder();

        sql.line("SELECT r.id,e.project_name,e.project_code,e.id_code,e.trader_name,e.trader_tel,r.power_station_code,e.tenant_name,e.mobile,r.entry_id,");
        sql.line("r.the_period_rent,r.pay_status,r.rent_staus,r.meet_time,r.current_payable,r.pay_time FROM `crm_customer_entry_rent` r");
        sql.line("LEFT JOIN `crm_customer_entry` e ON e.id= r.entry_id where 1=1 ");
        appendConditions(sql, map, operator);
        sql.limit(page.getPage(), page.getLimit());
        return Db.find(sql.build(),sql.params());
    }

    public long findEntryRentCount(Map<String, String> map, Operator operator){
        SqlBuilder sql = new SqlBuilder();

        sql.line("SELECT count(*) FROM `crm_customer_entry_rent` r");
        sql.line("LEFT JOIN `crm_customer_entry` e ON e.id= r.entry_id where 1=1");
        appendConditions(sql, map, operator);
        return Db.queryLong(sql.build(),sql.params());
    }

    public List<Record> findEntryRentHisList(LayuiPage<Record> page,String entry_id){
        SqlBuilder sql = new SqlBuilder();

        sql.line("SELECT r.id,e.project_name,e.project_code,e.id_code,e.trader_name,e.trader_tel,e.project_company_name,r.power_station_code,e.tenant_name,e.mobile,r.entry_id,");
        sql.line("r.the_period_rent,r.pay_status,r.pay_cycle,r.rent_staus,r.meet_time,r.current_payable,r.pay_time FROM `crm_customer_entry_rent` r");
        sql.line("LEFT JOIN `crm_customer_entry` e ON e.id= r.entry_id where r.pay_status='1' and r.entry_id=?",entry_id);

        sql.limit(page.getPage(), page.getLimit());
        return Db.find(sql.build(),sql.params());
    }

    public long findEntryRentHisCount(String entry_id){
        SqlBuilder sql = new SqlBuilder();

        sql.line("SELECT count(*) FROM `crm_customer_entry_rent` r");
        sql.line("LEFT JOIN `crm_customer_entry` e ON e.id= r.entry_id where r.pay_status='1' and r.entry_id=?",entry_id);

        return Db.queryLong(sql.build(),sql.params());
    }

    private void appendConditions(SqlBuilder builder, Map<String, String> map, Operator operator) {
//        String status = map.get("status");

        if (UserConstants.TYPE_TRADER.equals(operator.getUserType())) {
            //代理商只能查看自己的
            String traderId = CrmUtils.getTraderIdByUserId(operator.getUserId());
            // builder.line(" and e.create_by =?" ,operator.getUserId());
            builder.line(" and e.distributor_id =?" , traderId );
        }
        if(User.USER_TYPE_ADVISOR.equals(operator.getUserType())){
            builder.line(" and ((e.dispatch_status = '0' and e.create_by =?)" ,operator.getUserId());
            builder.line(" or (e.dispatch_status = '1' and find_in_set(?, e.engineer_id)))", map.get("engineerId"));
        }
        if (StringUtil.checkParamsValid(map.get("projectIds"))) {
            builder.inst("and e.project_id IN ", Arrays.asList(map.get("projectIds").split(",")));
        }


        if (StringUtil.checkParamsValid(map.get("tenantName"))) {
            builder.line("and e.tenant_name =?", map.get("tenantName"));
        }


        if (StringUtil.checkParamsValid(map.get("projectCode"))) {
            builder.line("and e.project_id=?", map.get("projectCode"));
        }

        if (StringUtil.checkParamsValid(map.get("projectName"))) {
            builder.like("and e.project_name like ?", map.get("projectName"));
        }

        if (StringUtil.checkParamsValid(map.get("mobile"))) {
            builder.like("and e.mobile like ?", map.get("mobile"));
        }

        if (StringUtil.checkParamsValid(map.get("traderIds"))) {
            builder.inst("and e.distributor_id IN ", Arrays.asList(map.get("traderIds").split(",")));
        }
        if (StringUtil.checkParamsValid(map.get("projectCompanyIds"))) {
            builder.inst("and e.project_company_id IN ", Arrays.asList(map.get("projectCompanyIds").split(",")));
        }

        if (StringUtil.checkParamsValid(map.get("powerStationCode"))) {
            builder.line("and e.power_station_code like concat('%', ?, '%')", map.get("powerStationCode").trim());
        }

        if (StringUtil.checkParamsValid(map.get("createTimeMin"))) {
            builder.line("and e.pay_time >= concat(?, ' 00:00:00')", map.get("createTimeMin"));
        }
        if (StringUtil.checkParamsValid(map.get("createTimeMax"))) {
            builder.line("and e.pay_time <= concat(?, ' 23:59:59')", map.get("createTimeMax"));
        }


        if (StringUtil.checkParamsValid(map.get("traderName"))) {
            builder.line("and e.trader_name like concat('%', ?, '%')", map.get("traderName"));
        }
        if (StringUtil.checkParamsValid(map.get("traderMobile"))) {
            builder.line("and e.trader_tel like concat('%', ?, '%')", map.get("traderMobile"));
        }


    }

}
