/**
 */
package com.sifang.modules.flowpath.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.entity.Role;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;


import com.sifang.modules.flowpath.entity.SupplySupplierApply;

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

/**
 * 代理商审批信息DAO接口
 * @author lzp
 * @version 
 */
@Repository
public class SupplySupplierApplyDao extends BaseDao<SupplySupplierApply> {


    public SupplySupplierApply findBySupplierId(String supplierId){
        return getByHql("from SupplySupplierApply where supplierId = :p1 ", new Parameter(supplierId));
    }

    public List<Record> findApplyList(LayuiPage<Record> page, Map<String, String> map,User user){
        SqlBuilder sql =  new SqlBuilder();
        sql.line("SELECT t.*,CONCAT_WS('',t.province,t.city,t.area,t.address) AS traderAddressfrom  ");
        sql.line(" ,(case when  k.`ASSIGNEE_`='"+user.getId()+"' then 1 else 0 end) as audit ");
        sql.line(" ,(case when FIND_IN_SET(?,t.operate_user_ids) then 1 else 0 end ) as lookAuth ",user.getId());
        sql.line(" FROM`crm_supply_supplier_apply` t ");
        sql.line(" LEFT JOIN `act_ru_task` k ON t.`process_instance_id`=k.PROC_INST_ID_ AND k.FORM_KEY_='02' and k.ASSIGNEE_ =?",user.getId() );
        sql.line(" WHERE t.status<>'4'");
        appendCondition(sql,map);
        sql.line(" order by t.create_time desc  limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sql.build(),sql.params());

    }

    public Long findApplyCount(Map<String, String> map,User user){
        SqlBuilder sql =  new SqlBuilder();
        sql.line("SELECT count(*) FROM`crm_supply_supplier_apply` t ");
        sql.line(" LEFT JOIN `act_ru_task` k ON t.`process_instance_id`=k.PROC_INST_ID_ AND k.FORM_KEY_='02' ");
        sql.line("WHERE t.status<>'4'");
        appendCondition(sql,map);
        return Db.queryLong(sql.build(),sql.params());

    }

    private void appendCondition(SqlBuilder builder, Map<String, String> map) {
        if (StringUtil.checkParamsValid(map.get("name"))) {
            builder.line("and t.name like concat('%', ?, '%')", map.get("name").trim());
        }
        if (StringUtil.checkParamsValid(map.get("actual_contacts"))) {
            builder.line("and t.actual_contacts like concat('%', ?, '%')", map.get("actual_contacts").trim());
        }
        if (StringUtil.checkParamsValid(map.get("telephone"))) {
            builder.line("and t.telephone like concat('%', ?, '%')", map.get("telephone").trim());
        }
        if (StringUtil.checkParamsValid(map.get("contacts"))) {
            builder.line("and t.contacts like concat('%', ?, '%')", map.get("contacts").trim());
        }
        if (StringUtil.checkParamsValid(map.get("mobile"))) {
            builder.line("and t.mobile like concat('%', ?, '%')", map.get("mobile").trim());
        }
        if (StringUtil.checkParamsValid(map.get("remarks"))) {
            builder.line("and t.remarks like concat('%', ?, '%')", map.get("remarks").trim());
        }
        if (StringUtil.checkParamsValid(map.get("create_timeMin"))) {
            builder.line(" and t.create_time >= '" + map.get("create_timeMin") + " 00:00:00' ");
        }
        if (StringUtil.checkParamsValid(map.get("create_timeMax"))) {
            builder.line(" and t.create_time <= '" + map.get("create_timeMax") + " 23:59:59' ");
        }
        if(StringUtil.isNotBlank(map.get("provinceE"))){
            builder.inst(" and t.province in ",map.get("provinceE").split(","));
        }
        if(StringUtil.isNotBlank(map.get("cityE"))){
            builder.inst(" and t.city in  ",map.get("cityE").split(","));
        }
    }
	
}
