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.CrmSiteEmploye;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import org.springframework.stereotype.Repository;

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

@Repository
public class EmployeInformationDao extends BaseDao<CrmSiteEmploye> {

    public List<Record> getSiteEmploye(LayuiPage page, Map<String, String> map){
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT a.*,s.name as siteName,u.login_name FROM crm_site_employee a left join crm_site s on a.site_id = s.id ");
        sf.append(" left join sys_user u on a.user_id = u.id WHERE  a.status='0' and a.role='2' ");
        sf.append(sqlConditions(map));
        sf.append(" order by s.create_time desc  ");
        sf.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sf.toString());
    }

    public Long getCount(Map<String, String> map){
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT count(*) FROM crm_site_employee a left join crm_site s on a.site_id = s.id WHERE  a.status='0' and a.role='2'");
        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("name"))) {
                sb.append(" and a.name like '%" + map.get("name").trim() + "%' ");
            }
            if(StringUtil.checkParamsValid(map.get("siteId"))){
                sb.append("and a.site_id = '" + map.get("siteId")+ "'");
            }
            if(StringUtil.checkParamsValid(map.get("mobile"))){
                sb.append("and a.mobile like '%" + map.get("mobile").trim()+ "%'");
            }
        }
        return sb.toString();
    }

    //未审核申请列表
    public List<Record> getNoAuditList(LayuiPage page, Map<String, String> map){
        String siteId = CrmUtils.getSiteId(UserUtils.getUser());
        StringBuffer sf = new StringBuffer();
        sf.append("select a.*,o.number,o.appliance_category,o.service_type,o.warranty_type,o.service_measures,o.power_station_name,o.employe_name as empName from crm_site_fitting_apply a left join crm_order o on a.order_id = o.id ");
        sf.append(" where a.status='0' and a.site_id ='" + siteId + "' ");
        sf.append(search(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 getNoAuditCount(Map<String, String> map){
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT count(*) FROM crm_site_fitting_apply a left join crm_order o on a.order_id = o.id where a.status='0'");
        sf.append(search(map));
        return Db.queryLong(sf.toString());
    }

    //已审核申请列表
    public List<Record> getAuditedList(LayuiPage page, Map<String, String> map){
        String siteId = CrmUtils.getSiteId(UserUtils.getUser());
        StringBuffer sf = new StringBuffer();
        sf.append("select a.*,o.number,o.appliance_category,o.service_type,o.warranty_type,o.service_measures,o.power_station_name,o.employe_name as empName from crm_site_fitting_apply a left join crm_order o on a.order_id = o.id ");
        sf.append(" where a.status in('1','3')  and a.site_id ='" + siteId + "' ");
        sf.append(search(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 getAuditedCount(Map<String, String> map){
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT count(*) FROM crm_site_fitting_apply a left join crm_order o on a.order_id = o.id where a.status in('1','3')");
        sf.append(search(map));
        return Db.queryLong(sf.toString());
    }

    public String search(Map<String, String> map) {
        StringBuilder sb = new StringBuilder("");
        if (map != null) {
            if(StringUtil.checkParamsValid(map.get("empId"))){
                sb.append(" and a.employe_id = '" + map.get("empId")+ "'");
            }
            if (StringUtil.checkParamsValid(map.get("fittingCode"))) {
                sb.append(" and a.fitting_code like '%" + map.get("fittingCode").trim() + "%' ");
            }
            if (StringUtil.checkParamsValid(map.get("fittingName"))) {
                sb.append(" and a.fitting_name like '%" + map.get("fittingName").trim() + "%' ");
            }
            if (StringUtil.checkParamsValid(map.get("fittingVersion"))) {
                sb.append(" and a.fitting_version like '%" + map.get("fittingVersion").trim() + "%' ");
            }
            if (StringUtil.checkParamsValid(map.get("station"))) {
                sb.append(" and o.power_station_name like '%" + map.get("station").trim() + "%' ");
            }
            if (StringUtil.checkParamsValid(map.get("orderNum"))) {
                sb.append(" and o.number like '%" + map.get("orderNum").trim() + "%' ");
            }
            if(StringUtil.checkParamsValid(map.get("equipment"))){
                sb.append(" and o.appliance_category = '" + map.get("equipment")+ "'");
            }
            if (StringUtil.checkParamsValid(map.get("applyMarks"))) {
                sb.append(" and a.employe_feedback like '%" + map.get("applyMarks").trim() + "%' ");
            }
            if(StringUtil.checkParamsValid( map.get("applyTimeSta"))){
                sb.append(" and a.create_time >= '" + map.get("applyTimeSta") + " 00:00:00' ");
            }
            if(StringUtil.checkParamsValid( map.get("applyTimeEnd"))){
                sb.append(" and a.create_time <= '" + map.get("applyTimeEnd") + " 23:59:59' ");
            }
            if(StringUtil.checkParamsValid( map.get("auditTimeSta"))){
                sb.append(" and a.audit_time >= '" + map.get("auditTimeSta") + " 00:00:00' ");
            }
            if(StringUtil.checkParamsValid( map.get("auditTimeEnd"))){
                sb.append(" and a.audit_time <= '" + map.get("auditTimeEnd") + " 23:59:59' ");
            }
            if (StringUtil.checkParamsValid(map.get("auditMarks"))) {
                sb.append(" and a.audit_marks like '%" + map.get("auditMarks").trim() + "%' ");
            }
            if (StringUtil.checkParamsValid(map.get("status"))) {
                sb.append(" and a.status='" + map.get("status").trim() + "' ");
            }
        }
        return sb.toString();
    }

    public List<Record> getEmp(String siteId) {
        StringBuilder sb = new StringBuilder("");
        sb.append("select id,name from crm_site_employee where status='0' and site_id=? ");
        List<Record> list = Db.find(sb.toString(), siteId);
        return list;
    }

    public Record getEmpApplyDetail(String id){
        StringBuffer sf = new StringBuffer();
        sf.append("select a.id as applyId,a.employe_name as employeName,a.create_time as ctime,a.*,o.*,f.warning,f.refund_old_flag from crm_site_fitting_apply a left join crm_order o on a.order_id = o.id left join crm_site_fitting f on a.fitting_id = f.id");
        sf.append(" where a.id = ? ");
        return Db.findFirst(sf.toString(),id);
    }


    public List<Record> getEmpByName(String name,String siteId) {
        StringBuilder sb = new StringBuilder("");
        sb.append("select id,name from crm_site_employee where status='0' and name =? and site_id=?");
        List<Record> list = Db.find(sb.toString(), name,siteId);
        return list;
    }
}
