package org.linlinjava.jz.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.jz.db.dao.SysUserMapper;
import org.linlinjava.jz.db.domain.JzBuiltList;
import org.linlinjava.jz.db.domain.SysUser;
import org.linlinjava.jz.db.domain.SysUserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Service
public class JzSysUserService {
	private final SysUser.Column[] result = new SysUser.Column[] { SysUser.Column.id, SysUser.Column.userName,
			SysUser.Column.userAccount, SysUser.Column.avatar, SysUser.Column.roleIds, SysUser.Column.extNo };
	@Resource
	private SysUserMapper adminMapper;

	@Autowired
	private JzSkillGroupUserService skillGroupUserService;

	@Autowired
	private JzBuiltListService jzBuiltListService;

	public List<SysUser> findAdmin(String username) {
		SysUserExample example = new SysUserExample();
		example.or().andUserAccountEqualTo(username).andStateEqualTo(0L);
		return adminMapper.selectByExample(example);
	}
	
	public List<SysUser> userserLong(String username,String userPassword,Integer type) {
		SysUserExample example = new SysUserExample();
		example.or().andUserAccountEqualTo(username)
		.andUserPasswordEqualTo(userPassword)
		.andTypeEqualTo(type)
		.andStateEqualTo(0L);
		return adminMapper.selectByExample(example);
	}

	public SysUser findAdmin(Integer id) {
		return adminMapper.selectByPrimaryKey(id.longValue());
	}

	public List<SysUser> querySelectQuit(String username, String userAccount, Integer page, Integer limit, String sort,
			String order) {
		SysUserExample example = new SysUserExample();
		SysUserExample.Criteria criteria = example.createCriteria();

		if (!StringUtils.isEmpty(username)) {
			criteria.andUserNameLike("%" + username + "%");
		}

		if (!StringUtils.isEmpty(userAccount)) {
			criteria.andUserAccountLike("%" + userAccount + "%");
		}

		example.setOrderByClause(" id " + " " + order);
		criteria.andTypeEqualTo(0);// 内部账号
//        criteria.andStateEqualTo(1L);//已离职

		PageHelper.startPage(page, limit);
		List<SysUser> userList = adminMapper.selectByExampleSelective(example);

		for (SysUser su : userList) {
			 List<Map<Object,Object>> builtList = jzBuiltListService.getGroupCampaignCount(su.getId().intValue());

			if (null != builtList && builtList.size() > 0) {
				su.setTypeTaskTotal(builtList);
			}
		}

		return userList;
	}

	public List<SysUser> querySelective(String username, String userAccount, Integer page, Integer limit, String sort,
			String order) {
		SysUserExample example = new SysUserExample();
		SysUserExample.Criteria criteria = example.createCriteria();

		if (!StringUtils.isEmpty(username)) {
			criteria.andUserNameLike("%" + username + "%");
		}

		if (!StringUtils.isEmpty(userAccount)) {
			criteria.andUserAccountLike("%" + userAccount + "%");
		}
		criteria.andStateEqualTo(0L);

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

//		criteria.andTypeEqualTo(0);

		PageHelper.startPage(page, limit);
		return adminMapper.selectByExampleSelective(example);
	}

	public int findSkillGroupIdById(int id) {
		return skillGroupUserService.findSkillGroupIdbyUserId(id);
	}

	public int updateById(SysUser admin) {
		admin.setUpdateTime(LocalDateTime.now());
		return adminMapper.updateByPrimaryKeySelective(admin);
	}

	public void deleteById(Integer id) {

		SysUser admin = adminMapper.selectByPrimaryKey(id.longValue());
		admin.setState(1L);
		admin.setUpdateTime(LocalDateTime.now());
		adminMapper.updateByPrimaryKeySelective(admin);
	}

	public void add(SysUser admin) {
		admin.setCreateTime(LocalDateTime.now());
		admin.setUpdateTime(LocalDateTime.now());
		adminMapper.insertSelective(admin);
	}

	public List<SysUser> queryByDepartmentId(Integer id) {
		SysUserExample example = new SysUserExample();
		example.or().andStateEqualTo(0L).andDepartmentIdEqualTo(id);

		return adminMapper.selectByExampleSelective(example, result);
	}

	public SysUser findById(Integer id) {
		return adminMapper.selectByPrimaryKeySelective(id.longValue(), result);
	}

	public List<SysUser> all() {
		SysUserExample example = new SysUserExample();
		example.or().andStateEqualTo(0L);
		return adminMapper.selectByExampleSelective(example, result);
	}

	public List<SysUser> all(List<Long> notIn) {
		SysUserExample example = new SysUserExample();
		example.or().andStateEqualTo(0L).andIdNotIn(notIn);
		return adminMapper.selectByExampleSelective(example, result);
	}
}
