package com.sifang.modules.customer.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.entity.CrmCustomerEntryProject;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.SqlBuilder;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CrmCustomerEntryProjectDao extends BaseDao<CrmCustomerEntryProject> {

    public List<Record> getProjectsByEntryId(String entryId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select p.id,p.code,p.name,p.pay_cycle,t.roof_type,p.assembly_specs from crm_customer_entry_project t");
        builder.line("LEFT JOIN `crm_project_setting` p ON p.id=t.project_id ");
        builder.line("where t.entry_id = ? ",entryId);
        builder.line(" order by t.project_id asc ");
       return Db.find(builder.build(),builder.params() );
    }

    public void deleteByEntryId(String entryId){
            SQLQuery sqlQuery = getSession().createSQLQuery("DELETE FROM crm_customer_entry_project WHERE entry_id=:mainId");
            sqlQuery.setParameter("mainId", entryId);
            sqlQuery.executeUpdate();
    }

    public void deleteEntryProjectById(String entryId){
        SQLQuery sqlQuery = getSession().createSQLQuery("DELETE FROM crm_customer_entry_project WHERE entry_id=:entryId");
        sqlQuery.setParameter("entryId", entryId);
        sqlQuery.executeUpdate();
    }

    public String getRoofTypeByProjectId(String entryId ,String projectId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select t.roof_type from crm_customer_entry_project t");
        builder.line("where t.entry_id = ? and t.project_id = ? ",entryId,projectId);
        return Db.queryStr(builder.build(),builder.params() );
    }


    //获取电站的projectId;
   public List<String> getEntryProjectIds(String entryId){
        return Db.query(" SELECT project_id FROM crm_customer_entry_project WHERE entry_id=? ",entryId);
   }

}
