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.entity.CrmPlan;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author hongzq
 */
@Repository
public class PlanDao extends BaseDao<CrmPlan> {

    public Record getPlanById(String id){
        return Db.findFirst("select * from crm_plan where id=?", id);
    }

    public List<Record> findAllPlan(){
        return Db.find("select * from crm_plan where status='0' and type='1' order by create_time desc");
    }

    public List<Record> findAllPlanByTypeId(String typeId){
        return Db.find("select * from crm_plan where status='0' and type='1' AND plan_type_id = ? order by create_time desc", typeId);
    }

    public List<Record> findPlanByAssembly(String name, String planType){
        SqlBuilder sql= new SqlBuilder();
        sql.line(" SELECT any_value(p.id)  AS id,any_value(p.type)AS type,any_value(p.code) AS code,any_value(p.name) AS name,any_value(p.attach) AS attach  ");
        sql.line(" ,any_value(p.attach_name) AS attach_name,any_value(p.plan_type_id) AS plan_type_id,any_value(p.plan_type) AS plan_type ");
        sql.line(" ,any_value(p.assembly) AS assembly,any_value(p.assembly_count) AS assembly_count,any_value(p.attach2) AS attach2 ");
        sql.line(" ,any_value(p.attach_name2) AS attach_name2 ");
        sql.line(" FROM crm_plan p WHERE p.status='0' ");

        if(StringUtil.isNotBlank(name)){
            sql.line("AND p.name=? ",name);
        }
        if(StringUtil.isNotBlank(planType)){
            sql.line("AND p.plan_type_id=?",planType.trim());
        }else{
            sql.line(" GROUP BY p.plan_type ");
        }

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

}
