package com.sifang.modules.sunshine.partnerManagement.dao;


import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.partnerManagement.entity.CrmPartnerStaff;
import com.sifang.modules.sys.common.persistence.BaseDao;
import org.springframework.stereotype.Repository;

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

@Repository
public class PartnerStaffDao extends BaseDao<CrmPartnerStaff> {
    /**
     * 获取服务商员工设置列表数据
     *
     * @return
     */
    public List<Record> getTaderEmployeData(Map<String, String> params, String traderId) {
        return getTaderEmployeData(params, "", traderId);
    }

    public List<Record> getTaderEmployeData(Map<String, String> map, String id, String partnerId) {
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT a.*,b.login_name,b.password FROM crm_partner_staff a ");
        sf.append("LEFT JOIN sys_user b ON b.id = a.`user_id` AND b.`status`= '0' AND b.`user_type` = 'a' ");
        sf.append(" where a.partner_id =? ");
        if (StrKit.notBlank(id)) {
            sf.append(" and a.id = '" + id + "' ");
        }
        if (map != null) {
            if (map.containsKey("name") && StrKit.notBlank(map.get("name"))) {
                sf.append(" and a.name like '%" + map.get("name").trim() + "%' ");
            }
            if (map.containsKey("mobile") && StrKit.notBlank(map.get("mobile"))) {
                sf.append(" and a.mobile like '%" + map.get("mobile").trim() + "%' ");
            }
        }
        sf.append(" and a.status = '1' ");
        return Db.find(sf.toString(), partnerId);

    }



    public List<Record> getEmpByName(String name,String partnerId,String id) {
        StringBuilder sb = new StringBuilder("");
        sb.append("select id,name from crm_partner_staff where status='1' and name =? and partner_id=?");
        if(StringUtil.isNotBlank(id)){
            sb.append(" and id<> '"+id+"'");
        }
        List<Record> list = Db.find(sb.toString(), name,partnerId);
        return list;
    }

    /**
     * 新增员工之前进行校验登录账号是否已经存在
     */
    public boolean getExistsLoginName(String loginName) {
        Record rd =  Db.findFirst(" select * from sys_user a where a.login_name = ?  and a.status = '0' ", loginName);

        return rd!=null;
    }

    /**
     * 修改员工保存之前进行校验登录账号是否已经存在
     */
    public boolean getExistsLoginNameNew(String userId, String loginName) {
        Record rd =  Db.findFirst(" select * from sys_user a where a.login_name = ? and a.status = '0' and a.id !=? ",
                loginName, userId);

        return rd!= null;
    }


    /**
     * 查询修改员工信息页面数据
     *
     * @return
     */
    public Record getSiteEmployeeById(String id, String partnerId) {
        List<Record> rds = getTaderEmployeData(null, id, partnerId);
        return rds == null ? null : rds.get(0);
    }

    /**
     * 删除员工信息
     *
     * @return
     */
    public boolean delEmploye(String id) {
        try {
            StringBuilder sb = new StringBuilder("");
            sb.append(" update crm_partner_staff a , sys_user b ");
            sb.append(" set a.status = '2', b.status = '1' ");
            sb.append(" where b.id = a.user_id and a.id = ? ");
            Db.update(sb.toString(), id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public String getPartnerIdByUserId(String userId) {
        StringBuffer sf = new StringBuffer();
        sf.append("select s.id from crm_partner s where s.status ='0' and s.user_id = ? ");
        return Db.queryStr(sf.toString(), userId);
    }

}
