package com.sifang.modules.changes.dao;

import com.jfinal.plugin.activerecord.Db;
import com.sifang.modules.changes.entity.CrmCustomerEntryChangeRecord;
import com.sifang.modules.site.siteInfo.service.SiteEmployeeService;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.dao.UserDao;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CustomerEntryChangeRecordDao extends BaseDao<CrmCustomerEntryChangeRecord> {

    @Autowired
    UserDao userDao;

    @Autowired
    SiteEmployeeService siteEmployeeService;
    @Autowired
    private TraderService traderService;

    public List<CrmCustomerEntryChangeRecord> fingStatusAll() {
        return find("from CrmCustomerEntryChangeRecord where changeStatus='20'");
    }
    public CrmCustomerEntryChangeRecord findByChangeNo(String changeNo) {
        return  getByHql("from CrmCustomerEntryChangeRecord where changeNumber=:p1", new Parameter(changeNo));
    }

    public Long getIfHaveTenantChange(String entryId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select count(1) from crm_customer_entry_change_record r ")
                .line("left join  crm_customer_entry_tenant_change_record t on t.id = r.change_id  ")
                .line(" where t.entry_id = ? and r.change_status in('15','20') ",entryId);
        return Db.queryLong(builder.build(), builder.params());
    }
    public Long getIfHaveSurveyChange(String entryId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select count(1) from crm_customer_entry_change_record r ")
                .line("left join  crm_customer_entry_survey_change_record t on t.id = r.change_id  ")
                .line(" where t.entry_id = ? and r.change_status in('15','20') ",entryId);
        return Db.queryLong(builder.build(), builder.params());
    }
    public Long getIfHaveDesignChange(String entryId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select count(1) from crm_customer_entry_change_record r ")
                .line("left join  crm_customer_entry_change_design t on t.id = r.change_id  ")
                .line(" where t.entry_id = ? and r.change_status in('15','20') ",entryId);
        return Db.queryLong(builder.build(), builder.params());
    }
    public Long getIfHaveCompleteChange(String entryId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select count(1) from crm_customer_entry_change_record r ")
                .line("left join  crm_customer_entry_completion_change_record t on t.id = r.change_id  ")
                .line(" where t.entry_id = ? and r.change_status in('15','20') ",entryId);
        return Db.queryLong(builder.build(), builder.params());
    }
    public Long getIfHaveMaterialChange(String entryId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select count(1) from crm_customer_entry_change_record r ")
                .line("left join  crm_material_information_change_apply t on t.id = r.change_id  ")
                .line(" where t.entry_id = ? and r.change_status in('15','20') ",entryId);
        return Db.queryLong(builder.build(), builder.params());
    }
}
