package org.linlinjava.jz.db.service;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;

import org.apache.commons.lang3.StringUtils;
import org.linlinjava.jz.db.dao.JzSkillGroupMapper;
import org.linlinjava.jz.db.dao.SysUserMapper;
import org.linlinjava.jz.db.domain.JzSkillGroup;
import org.linlinjava.jz.db.domain.JzSkillGroupExample;
import org.linlinjava.jz.db.domain.JzSkillGroupUser;
import org.linlinjava.jz.db.domain.SysUser;
import org.linlinjava.jz.db.domain.SysUserExample;
import org.linlinjava.jz.db.dto.SkillGroupUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 系统操作人员技能组划分，主要用来工单升级
 * @author leo
 * 2020-12-14
 */
@Service
public class JzSkillGroupService {

    @Resource
    private JzSkillGroupMapper skillGroupMapper;
    
	@Autowired
	private JzSkillGroupUserService groupUserService;

	@Autowired
	private SysUserMapper sysUserMapper;



    public List<JzSkillGroup> querySelective(String title, Integer page, Integer limit, String sort, String order) {
        JzSkillGroupExample example = new JzSkillGroupExample();
        JzSkillGroupExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(title)) {
            criteria.andNameLike("%" + title + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return skillGroupMapper.selectByExample(example);
    }

    public int add(JzSkillGroup skillGroup) {
        skillGroup.setCreateTime(LocalDateTime.now());
        skillGroup.setUpdateTime(LocalDateTime.now());
        return skillGroupMapper.insertSelective(skillGroup);
    }

    public int deleteById(JzSkillGroup skillGroup) {
        skillGroup.setUpdateTime(LocalDateTime.now());
        skillGroup.setDeleted(true);
        return   skillGroupMapper.updateByPrimaryKeySelective(skillGroup);
    }

    public int updateById(JzSkillGroup skillGroup) {
        skillGroup.setUpdateTime(LocalDateTime.now());
        return skillGroupMapper.updateByPrimaryKeySelective(skillGroup);
    }

    public JzSkillGroup findById(Integer id) {
        return skillGroupMapper.selectByPrimaryKey(id);
    }

    public List<JzSkillGroup> queryAll(){
        JzSkillGroupExample example = new JzSkillGroupExample();
        example.or().andDeletedEqualTo(false);
        return skillGroupMapper.selectByExample(example);
    }

    public List<JzSkillGroup> queryAll(List<Integer> exists) {
        JzSkillGroupExample example = new JzSkillGroupExample();
        if (exists != null && exists.size() > 0)
            example.or().andDeletedEqualTo(false).andIdNotIn(exists);
        else
            example.or().andDeletedEqualTo(false);
        return skillGroupMapper.selectByExample(example);
    }


    /**
     * 获取所有可以处理升级工单的技能组
     * @return
     */
    public List<JzSkillGroup> queryCanUpgrade(){
        JzSkillGroupExample example = new JzSkillGroupExample();
        example.or().andDeletedEqualTo(false).andIsUpgradeEqualTo(true);
        return skillGroupMapper.selectByExample(example);
    }

    /**
     * 获取所有可以重新打开工单的技能组
     * @return
     */
    public List<JzSkillGroup> queryCanReopen(){
        JzSkillGroupExample example = new JzSkillGroupExample();
        example.or().andDeletedEqualTo(false).andIsReopenEqualTo(true);
        return skillGroupMapper.selectByExample(example);
    }
    

	public Integer getSkillGroupIdByUserId(Integer userId) {
		return groupUserService.findSkillGroupIdbyUserId(userId);
	}

	/**
	 * 返回技能组下的用户信息
	 * 
	 * @return
	 */
	public List<SkillGroupUserVo> getSkillSysUser() {
		List<JzSkillGroup> skillAll = queryAll();
//        List<JzDepartment> departments=departmentService.queryAll();

		List<SkillGroupUserVo> sKilluserVo = Lists.newArrayList();
		for (JzSkillGroup sk : skillAll) {
			SkillGroupUserVo vo = new SkillGroupUserVo();
			vo.setId(sk.getId().toString());
			vo.setLabel(sk.getName());
			 

			//技能下的用户
			List<JzSkillGroupUser> findByGroupId = groupUserService.findByGroupId(sk.getId());
			List<Long> listIds = new ArrayList<Long>();
			for (JzSkillGroupUser c : findByGroupId) {
				listIds.add(c.getUserId().longValue());
			}
			
			//用户信息
			SysUserExample example = new SysUserExample();
			example.or().andIdIn(listIds);
			List<SysUser> userList = sysUserMapper.selectByExampleSelective(example, SysUser.Column.id,
					SysUser.Column.userName);

			vo.setUserList(userList);
			sKilluserVo.add(vo);
		}

		return sKilluserVo;
	}

}
