package com.sifang.modules.site.siteInfo.service;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.config.SfCacheService;
import com.sifang.modules.site.siteInfo.dao.SiteEmployeeDao;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class SiteEmployeeService {
    @Autowired
    private SiteEmployeeDao siteEmployeeDao;

    @Autowired
    private SfCacheService cacheService;

    /**
     * 获取服务商员工设置列表数据
     *
     * @return
     */
    public List<Record> getSiteEmployeeData(Map<String, String> map, String siteId) {
        List<Record> memberRds = siteEmployeeDao.getSiteEmployeeData(map, siteId);
        return memberRds;
    }

    /**
     * 新增员工保存之前进行校验登录账号是否已经存在
     *
     * @param map
     * @return
     */
    public String beforeSave(Map<String, String> map) {
        String id = "";
        String mess = "0";//正常
        String loginName = map.get("loginName");
        String mobile = map.get("mobile");
        Record rd = siteEmployeeDao.getExistsLoginName(id, loginName, mobile);
        cacheService.del("siteEmploye");
        cacheService.del("findEmployeeByUserId");
        if (rd != null) {
            mess = "1";
        }
        return mess;
    }


    /**
     * 查询修改员工信息页面数据
     *
     * @return
     */
    public Record getSiteEmployeeById(String id, String siteId) {
        Record rd = new Record();
        if (StrKit.isBlank(id)) {
            return rd;
        } else {
            rd = siteEmployeeDao.getSiteEmployeeById(id, siteId);
            return rd;
        }
    }

    /**
     * 修改员工保存之前进行校验登录账号是否已经存在
     *
     * @param map
     * @return
     */
    public String updateSaveBefore(Map<String, String> map) {
        String id = map.get("id");
        String mess = "0";//正常
        String loginName = map.get("loginName");
        Record rd = siteEmployeeDao.getExistsLoginNameNew(id, loginName);
        cacheService.del("siteEmploye");
        cacheService.del("findEmployeeByUserId");
        if (rd != null) {
            mess = "1";
        }
        return mess;
    }

    /**
     * 修改保存
     *
     * @param map
     * @return
     */
    public String updateSave(Map<String, String> map) {
        String id = map.get("id");
        String msg = "ok";
        if (StringUtils.isNotBlank(id)) {
            msg = siteEmployeeDao.updateEmployee(map);
            cacheService.del("siteEmploye");
            cacheService.del("findEmployeeByUserId");
        }
        return msg;
    }

    /**
     * 删除员工信息
     *
     * @return
     */
    public String delEmployee(Map<String, String> params) {
        String id = params.get("id");
        boolean bo = siteEmployeeDao.delEmployee(id);
        cacheService.del("siteEmploye");
        cacheService.del("findEmployeeByUserId");
        return bo ? "ok" : "err";
    }

    /**
     * 获取当前服务商id
     *
     * @param userId
     * @return
     */
    public String getSiteId(String userId) {
        return siteEmployeeDao.getSiteId(userId);
    }

    public List<Record> findProjectEmpList(String type){


        String emp=  cacheService.hget("siteEmploye",type);
        List<Record> listmap = Lists.newArrayList();
        if (StringUtil.isNotBlank(emp)) {
            listmap = new Gson().fromJson(emp, new TypeToken< List<Record>>() {
            }.getType());
        }else
        {
            List<Record> list =siteEmployeeDao.findProjectEmpList(type);
            listmap = list;
            cacheService.hset("siteEmploye",type,new Gson().toJson(list));
        }
        return listmap;
    }

    public List<Record> findEmployeeByUserId(String userId) {

        String emp=  cacheService.hget("findEmployeeByUserId",userId);
        List<Record> listmap;
        if (StringUtil.isNotBlank(emp)) {
            listmap = new Gson().fromJson(emp, new TypeToken< List<Record>>() {
            }.getType());
        }else
        {
            List<Record> list = siteEmployeeDao.findEmployeeByUserIds(userId);
            listmap = list;
            cacheService.hset("findEmployeeByUserId",userId,new Gson().toJson(list));
        }
        return  listmap;
    }

}
