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

import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.ComUtils;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import org.springframework.stereotype.Repository;

import com.sifang.modules.sunshine.SystemSetting.entity.CrmOrderCustomerSettlementTemplate;

import java.util.List;


@Repository
public class OrderCustomerSettlementTemplateDao extends BaseDao<CrmOrderCustomerSettlementTemplate> {




    /**
     * 获取平台与用户结算方案
    */
    public List<Record> getCustomerSettlementTemplate(String category, String setviceType, String warrType,String service_step){

        StringBuilder sf = new StringBuilder();
        StringBuilder facsf = new StringBuilder();
        sf.append("SELECT a.service_measures,a.number FROM crm_order_customer_settlement_template a WHERE a.category=? AND a.status='0' ");
        if(StringUtil.isNotBlank(setviceType)){
            sf.append(" AND a.service_type='"+setviceType+"'   ");
        }
        if(StringUtil.isNotBlank(warrType)){
            sf.append("  AND a.warranty_type='"+warrType+"'   ");
        }
        if(StringUtil.isNotBlank(service_step)){
            sf.append("  AND a.service_step='"+service_step+"'   ");
        }
        sf.append("and is_factory ='1' group by a.number ORDER BY a.charge_amount DESC    ");
        facsf.append("group by a.number ORDER BY a.charge_amount DESC    ");
        List<Record> rds = Db.find(facsf.toString(),category);
        if(rds.size()>0){
            return rds;
        }
        return Db.find(sf.toString(),category);
    }


    public long getTemplateByNumber(String warranty_type,String service_step){
        String sql =" SELECT count(id) FROM crm_order_customer_settlement_template a WHERE a.warranty_type=? and status='0' and a.service_step=? ";
        return Db.queryLong(sql,warranty_type,service_step);
    }

    public long getTemplateByNumberNotById(String warranty_type,String service_step,String id){
        String sql =" SELECT count(id) FROM crm_order_customer_settlement_template a WHERE a.warranty_type=? and status='0' and a.service_step=? and a.id != ? ";
        return Db.queryLong(sql,warranty_type,service_step,id);
    }

}
