package com.sifang.modules.customer.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.entity.CrmCustomerYxAuthorization;
import com.sifang.modules.customer.entity.CrmCustomerYxCondition;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.utils.IdGen;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

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

@Repository
public class CrmCustomerYxAuthorizationDao extends BaseDao<CrmCustomerYxAuthorization> {


    public CrmCustomerYxAuthorization addYxAuthorization(String name,String mobile,String idCode,String authorization,String signFlowId,String partnerId,String filingType){
        CrmCustomerYxAuthorization one = new CrmCustomerYxAuthorization();
        one.setName(name);
        one.setMobile(mobile);
        one.setIdCode(idCode);
        one.setAuthorization(authorization);
        one.setSignFlowId(signFlowId);
        one.setPartnerId(partnerId);
        one.setCreateTime(new Date());
        one.setFilingType(filingType);
        one.setOldFlag("0");
        save(one);
        return one;
    }

    public CrmCustomerYxAuthorization findCustomerYxAuthorization(String idCode, String partnerId) {
        String sql = "from CrmCustomerYxAuthorization WHERE idCode =:p1 and partnerId=:p2";
        Query query = getSession().createQuery(sql);
        query.setParameter("p1", idCode);
        query.setParameter("p2", partnerId);
        List list = query.list();
        if (list.size() > 0) {
            return (CrmCustomerYxAuthorization) list.get(0);
        } else {
            return null;
        }
    }
    public CrmCustomerYxAuthorization findCustomerYxAuthorization(String idCode, String partnerId ,String filingType) {
        String sql = "from CrmCustomerYxAuthorization WHERE idCode =:p1 and partnerId=:p2 and  filingType =:p3 ";
        Query query = getSession().createQuery(sql);
        query.setParameter("p1", idCode);
        query.setParameter("p2", partnerId);
        query.setParameter("p3", filingType);
        List list = query.list();
        if (list.size() > 0) {
            return (CrmCustomerYxAuthorization) list.get(0);
        } else {
            return null;
        }
    }

    public List<Record> ajaxForInfoAuthorizationData(LayuiPage<Record> page, Map<String, String> map, Operator operator) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" select * from  crm_customer_yx_authorization a ")
                .line(" where  a.authorization in ('2','3') ");
        infoAuthorizationConditions(builder, map, operator);
        builder.line(" order by a.create_time desc");
        builder.limit(page.getPage(), page.getLimit());
        return Db.find(builder.build(),builder.params());
    }
    public Long countInfoAuthorizationData(Map<String, String> map, Operator operator) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select count(1) from crm_customer_yx_authorization a ")
                .line(" where  a.authorization in ('2','3') ");
        infoAuthorizationConditions(builder, map, operator);
        return Db.queryLong(builder.build(), builder.params());
    }
    public void infoAuthorizationConditions(SqlBuilder builder, Map<String, String> map, Operator operator) {
        if (StringUtil.isNotBlank(map.get("partner"))) {
            builder.line(" and a.partner_id =?", map.get("partner"));
        }
        if (StringUtil.isNotBlank(map.get("authorization"))) {
            builder.line(" and a.authorization =?", map.get("authorization"));
        }
        if (StringUtil.isNotBlank(map.get("tenantName"))) {
            builder.like(" and a.name like ? ", map.get("tenantName"));
        }
        if (StringUtil.isNotBlank(map.get("mobile"))) {
            builder.like(" and a.mobile like ? ", map.get("mobile"));
        }
        if (StringUtil.checkParamsValid(map.get("createTimeMin"))) {
            builder.line("and a.create_time >= ? ", map.get("createTimeMin") + " 00:00:00");
        }
        if (StringUtil.checkParamsValid(map.get("createTimeMax"))) {
            builder.line("and a.create_time <= ? ", map.get("createTimeMax") + " 23:59:59");
        }

    }

}
