package com.sifang.modules.supply.setting.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.supply.setting.entity.CrmSupplySupplier;
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 java.util.List;
import java.util.Map;

@Repository
public class CrmSupplySupplierDao extends BaseDao<CrmSupplySupplier> {


    public List<Record> getList(LayuiPage<Record> page, Map<String, String> map){
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT any_value(s.id) AS id, any_value(s.user_id) AS user_id, any_value(s.name) AS name, ")
                .line(" any_value(s.contacts) AS contacts, any_value(s.mobile) mobile, any_value(s.actual_contacts) AS actual_contacts, ")
                .line("  any_value(s.telephone) AS telephone ,any_value( s.actual_id_code) AS actual_id_code, any_value(s.bank_name) AS bank_name,  ")
                .line(" any_value(s.bank_code) AS bank_code, ")
                .line("  any_value(s.tax_number) AS tax_number, any_value(s.license_img) AS license_img, any_value(s.remarks) AS  remarks , ")
                .line(" CONCAT(any_value(s.province),any_value(s.city),any_value(s.area),any_value(s.address)) AS address, ")
                .line(" any_value(u.login_name) AS login_name,any_value(s.create_time) AS create_time,any_value(a.status) AS applyStatus  ")
                .line(" FROM `crm_supply_supplier` s ")
                .line(" LEFT JOIN crm_supply_supplier_apply a ON a.supplier_id=s.id   ")
                .line(" LEFT JOIN `sys_user` u ON s.user_id=u.id  ")
                .line("  WHERE s.status='0'  ");
        appendCondition(builder, map);
        builder.line("  GROUP BY s.id order by s.create_time desc");
        if (page != null) {
            builder.limit(page.getPage(), page.getLimit());
        }
        return Db.find(builder.build(), builder.params());
    }

    public Long listCount(Map<String, String> map){
        SqlBuilder builder = new SqlBuilder();
         builder.line(" SELECT COUNT(s.id) FROM `crm_supply_supplier`s ")
                 .line(" where s.status='0' ");
        appendCondition(builder, map);
        return Db.queryLong(builder.build(),builder.params());
    }

    public void appendCondition(SqlBuilder builder, Map<String, String> map){

        if (StringUtil.checkParamsValid(map.get("name"))) {
            builder.line("and s.name like concat('%', ?, '%')", map.get("name").trim());
        }
        if (StringUtil.checkParamsValid(map.get("actualContacts"))) {
            builder.line("and s.actual_contacts like concat('%', ?, '%')", map.get("actualContacts").trim());
        }
        if (StringUtil.checkParamsValid(map.get("telephone"))) {
            builder.line("and s.telephone like concat('%', ?, '%')", map.get("telephone").trim());
        }
        if (StringUtil.checkParamsValid(map.get("contacts"))) {
            builder.line("and s.contacts like concat('%', ?, '%')", map.get("contacts").trim());
        }
        if (StringUtil.checkParamsValid(map.get("mobile"))) {
            builder.line("and s.mobile like concat('%', ?, '%')", map.get("mobile").trim());
        }
        if (StringUtil.checkParamsValid(map.get("remarks"))) {
            builder.line("and s.remarks like concat('%', ?, '%')", map.get("remarks").trim());
        }
        if (StringUtil.checkParamsValid(map.get("createTimeSta"))) {
            builder.line("and s.create_time >= concat(?, ' 00：00：00')", map.get("createTimeSta").trim());
        }
        if (StringUtil.checkParamsValid(map.get("createTimeEnd"))) {
            builder.line("and s.create_time <= concat(?, ' 23：59：59')", map.get("createTimeEnd").trim());
        }
    }

    public static String getSupplyName(String supplyId){
        return Db.queryStr(" SELECT NAME FROM `crm_supply_supplier` WHERE id=? ",supplyId);
    }

    // 根据监造员 查找供应商
    public CrmSupplySupplier findBySupervisorId(String supervisionId) {
        return getByHql(" from CrmSupplySupplier where status = '1' and bindingFlag = '1' and supervisionId=:p1 ", new Parameter(supervisionId));
    }

}
