package org.linlinjava.jz.admin.service;


import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.google.common.collect.Lists;
import org.linlinjava.jz.admin.util.SecurityHelper;
import org.linlinjava.jz.admin.vo.SkillGroupVo;
import org.linlinjava.jz.core.util.ResponseUtil;
import org.linlinjava.jz.core.util.StringUtils;
import org.linlinjava.jz.db.domain.*;
import org.linlinjava.jz.db.service.JzDepartmentService;
import org.linlinjava.jz.db.service.JzSkillGroupService;
import org.linlinjava.jz.db.service.JzSkillGroupUserService;
import org.linlinjava.jz.db.service.JzSysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 客服技能组
 */
@Service
public class AdminSkillGroupService {
    private final Log logger = LogFactory.getLog(AdminSkillGroupService.class);

    @Autowired
    private JzSkillGroupService groupService;

    @Autowired
    private JzSkillGroupUserService groupUserService;

    @Autowired
    private JzSysUserService sysUserService;

    @Autowired
    private JzDepartmentService departmentService;


    public Object add(JzSkillGroup skillGroup) {
        skillGroup.setCreateUser(SecurityHelper.getAdminId());
        return ResponseUtil.ok(groupService.add(skillGroup));
    }

    public Object delete(Integer id) {
        JzSkillGroup group = groupService.findById(id);
        group.setUpdateUser(SecurityHelper.getAdminId());
        group.setDeleted(true);
        return ResponseUtil.ok(groupService.updateById(group));
    }

    public List<JzSkillGroup> querySelective(Integer page,Integer limit,String sort,String order){
      return   groupService.querySelective(null,page,limit,sort,order);
    }

    public Object update(JzSkillGroup skillGroup) {
        skillGroup.setUpdateUser(SecurityHelper.getAdminId());
        return ResponseUtil.ok(groupService.updateById(skillGroup));
    }


    /**
     * 坐席绑定到技能组
     *
     * @param admins
     * @param id
     * @return
     */
    @Transactional
    public Object binding(List<String> admins, Integer id) {
        //冻结已有的绑定关系
        List<JzSkillGroupUser> users = groupUserService.findByGroupId(id);
        for (JzSkillGroupUser user : users) {
            user.setDeleted(true);
            user.setUpdateUser(SecurityHelper.getAdminId());
            groupUserService.updateById(user);
        }
        logger.info(admins.toString());
        //重新绑定关系
        for (String admin : admins) {
            JzSkillGroupUser user = new JzSkillGroupUser();
            logger.info(admin);
            user.setDeleted(false);
            user.setSkillGroupId(id);
            user.setUserId(Integer.valueOf(admin));
            user.setUpdateUser(SecurityHelper.getAdminId());
            user.setCreateUser(SecurityHelper.getAdminId());
            groupUserService.add(user);
        }
        return ResponseUtil.ok();
    }


    /**
     * 根据技能组id，获取所有成员信息
     * @param groupid
     * @return
     */
    public Set<String> getSkillGroupUser(Integer groupid) {
        logger.info(groupid.toString());
        Set<String> admins = new HashSet<String>();
        if(groupid == null){
            return admins;
        }
        List<JzSkillGroupUser> groupUsers =groupUserService.findByGroupId(groupid);

        for(JzSkillGroupUser admin : groupUsers){
            admins.add(admin.getUserId().toString());
        }
        return admins;
    }

    /**
     * 返回系统所有用户信息
     * @return
     */
    public List<SkillGroupVo> getSysUser() {
        List<JzDepartment> departments=departmentService.queryAll();

        List<SkillGroupVo> skillGroupVos = Lists.newArrayList();
        for (JzDepartment department : departments) {
            SkillGroupVo vo = new SkillGroupVo();
            vo.setId(department.getId().toString());
            vo.setLabel(department.getName());
            //部门成员信息

            List<SysUser> users = sysUserService.queryByDepartmentId(department.getId());
            List<SkillGroupVo> subVos=Lists.newArrayList();
            for (SysUser user:users){
                SkillGroupVo subVo=new SkillGroupVo();
                subVo.setLabel(user.getUserName());
                subVo.setId(user.getId().toString());
                subVos.add(subVo);
            }
            vo.setChildren(subVos);
            skillGroupVos.add(vo);
        }

        return skillGroupVos;
    }



}
