package com.sifang.modules.sunshine.SystemSetting.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.financialManagement.entity.CrmPlatformCollections;
import com.sifang.modules.sys.common.persistence.BaseDao;

import com.sifang.modules.sys.util.LayuiPage;
import org.springframework.stereotype.Repository;

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

@Repository
public class FittingAmountDao extends BaseDao<CrmPlatformCollections> {

    public List<Record> getRechargeList(LayuiPage page, Map<String, String> map){
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT * from crm_platform_collections  a where a.status='0' and a.payment_type='3' ");
        sf.append(sqlConditions(map));
        sf.append(" order by a.pay_time desc  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString());
    }

    public Long getRechargeCount(Map<String, String> map){
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT count(*) FROM crm_platform_collections a where a.status='0' and a.payment_type='3'");
        sf.append(sqlConditions(map));
        return Db.queryLong(sf.toString());
    }

    public String sqlConditions(Map<String, String> map) {
        StringBuilder sb = new StringBuilder("");
        if (map != null) {
            if(StringUtil.checkParamsValid(map.get("payTimeMin"))){
                String payTimeMin = map.get("payTimeMin");
                sb.append(" and a.pay_time >= '"+payTimeMin+"'");
            }
            if(StringUtil.checkParamsValid(map.get("payTimeMax"))){
                String payTimeMax = map.get("payTimeMax");
                sb.append(" and a.pay_time <= '"+payTimeMax+"'");
            }

            if(StringUtil.checkParamsValid(map.get("siteId"))){
                sb.append("and a.site_id = '" + map.get("siteId")+ "'");
            }
            if(StringUtil.checkParamsValid(map.get("remarks"))){
                sb.append("and a.marks like '%" + map.get("remarks").trim()+ "%'");
            }
            if(StringUtil.checkParamsValid(map.get("fittingCode"))){
                sb.append("and f.code like '%" + map.get("fittingCode").trim()+ "%'");
            }
            if(StringUtil.checkParamsValid(map.get("fittingName"))){
                sb.append("and f.name like '%" + map.get("fittingName").trim()+ "%'");
            }
            if(StringUtil.checkParamsValid(map.get("createTimeMin"))){
                String createTimeMin = map.get("createTimeMin");
                sb.append(" and a.create_time >= '"+createTimeMin+"'");
            }
            if(StringUtil.checkParamsValid(map.get("createTimeMax"))){
                String createTimeMax = map.get("createTimeMax");
                sb.append(" and a.create_time <= '"+createTimeMax+"'");
            }
        }
        return sb.toString();
    }

    public List<Record> getCostList(LayuiPage page, Map<String, String> map){
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT a.*,s.name as siteName,f.name as fittingName,f.code as fittingCode,k.fitting_audit_num as nums FROM crm_site_asset_frozen_record a  left join crm_site s on s.id = a.site_id  left join crm_platform_fitting f on a.fitting_id = f.id ");
        sf.append(" left join crm_platform_fitting_apply k on a.apply_fitting_id = k.id where a.status='0' and f.status='1' ");
        sf.append(sqlConditions(map));
        sf.append(" order by a.create_time desc  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString());
    }

    public Long getCostCount(Map<String, String> map){
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT count(*) FROM crm_site_asset_frozen_record  a  left join crm_platform_fitting f on a.fitting_id = f.id  WHERE  a.status='0' and f.status='1'");
        sf.append(sqlConditions(map));
        return Db.queryLong(sf.toString());
    }



}
