package com.github.wxiaoqi.security.admin.biz;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.wxiaoqi.security.admin.dto.AppUserManageDTO;
import com.github.wxiaoqi.security.admin.entity.AppUserDetail;
import com.github.wxiaoqi.security.admin.entity.AppUserLogin;
import com.github.wxiaoqi.security.admin.mapper.AppUserDetailMapper;
import com.github.wxiaoqi.security.admin.mapper.AppUserLoginMapper;
import com.github.wxiaoqi.security.admin.vo.AppUserManageVo;
import com.github.wxiaoqi.security.admin.vo.AppUserVo;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.exception.BaseException;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.process.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * app用户管理业务逻辑层
 *
 * @author Administrator
 */
@Service
@Slf4j
public class AppUserManageBiz extends BaseBiz<AppUserDetailMapper, AppUserDetail> {

	@Autowired
	private AppUserLoginMapper appUserLoginMapper;

	@Autowired
	private AppUserDetailBiz appUserDetailBiz;

	/**
	 * 根据条件查询
	 *
	 * @param appUserManageDTO 查询条件
	 * @return
	 */
	public PageInfo<AppUserManageVo> findAllByQuery(AppUserManageDTO appUserManageDTO) {
		if (Objects.nonNull(appUserManageDTO.getRegistrationTimeEnd())) {
			long registerEndTime = appUserManageDTO.getRegistrationTimeEnd() + (24 * 60 * 60);
			appUserManageDTO.setRegistrationTimeEnd(registerEndTime);
		}
		PageHelper.startPage(appUserManageDTO.getPage(), appUserManageDTO.getLimit());
		List<AppUserManageVo> appUserManageVos = mapper.selectAppUser(appUserManageDTO);
		PageInfo<AppUserManageVo> pageInfo = PageInfo.of(appUserManageVos);
		return pageInfo;
	}

	/**
	 * 禁用账户
	 *
	 * @param id
	 */

	@Transactional(rollbackFor = Exception.class)
	public void deleteAppUser(Integer id, Integer isDel) {
		AppUserLogin appUserLogin = new AppUserLogin();
		//修改为禁用并设置修改时间
		appUserLogin.setIsdel(isDel);
		appUserLogin.setUpdatetime(System.currentTimeMillis());
		Example example = new Example(AppUserLogin.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("id", id);
		appUserLoginMapper.updateByExampleSelective(appUserLogin, example);

		//设置
		AppUserDetail appUserDetail = new AppUserDetail();
		appUserDetail.setIsdel(isDel);
		appUserDetail.setUpdatetime(System.currentTimeMillis());
		Example detailExample = new Example(AppUserLogin.class);
		Example.Criteria detailCriteria = detailExample.createCriteria();
		criteria.andEqualTo("id", id);
		mapper.updateByExampleSelective(appUserDetail, detailCriteria);
	}

	/**
	 * 查询一条
	 *
	 * @param id
	 * @return
	 */
	public AppUserVo findOneById(Integer id) {
		return mapper.getUserInfo(id);
	}


	/**
	 * 启用禁用
	 *
	 * @param appUserLogin
	 * @return
	 */
	public ObjectRestResponse updateStatus(AppUserLogin appUserLogin) {
		if (appUserLogin == null || appUserLogin.getId() == null || appUserLogin.getStatus() == null) {
			return ObjectRestResponse.paramIsEmpty();
		}
		AppUserLogin old = appUserLoginMapper.selectByPrimaryKey(appUserLogin.getId());
		if (old == null || old.getIsdel() == 1) {
			return ObjectRestResponse.createFailedResult(ResultCode.NOTEXIST_CODE, ResultCode.getMsg(ResultCode.NOTEXIST_CODE));
		}
		old.setStatus(appUserLogin.getStatus());
		appUserLoginMapper.updateByPrimaryKeySelective(old);
		return ObjectRestResponse.succ();
	}

	/**
	 * 保存
	 *
	 * @param appUserVo
	 */
	@Transactional(rollbackFor = Exception.class)
	public void save(AppUserVo appUserVo) {
		AppUserLogin appUserLogin = new AppUserLogin();
		AppUserDetail appUserDetail = new AppUserDetail();

		try {
			BeanUtilsBean.getInstance().copyProperties(appUserLogin, appUserVo);
			BeanUtilsBean.getInstance().copyProperties(appUserDetail, appUserVo);

			if (appUserVo.getUserid() == null || appUserVo.getUserid() == 0) {
				//设置登录用户信息
				appUserLogin.setIsdel(0);
				appUserLogin.setCreatetime(System.currentTimeMillis());
				appUserLogin.setCertificationStatus(0);
				appUserLogin.setStatus(0);
				//设置用户详情
				appUserDetail.setIsdel(0);
				if (appUserDetail.getIsMember() == null) {
					appUserDetail.setIsMember(0);
				}

				appUserLoginMapper.insertSelective(appUserLogin);
				mapper.insertSelective(appUserDetail);

			} else {
				appUserLogin.setUpdatetime(System.currentTimeMillis());
				appUserDetail.setUpdatetime(System.currentTimeMillis());
				appUserLoginMapper.updateByPrimaryKeySelective(appUserLogin);
				mapper.updateByPrimaryKeySelective(appUserDetail);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
	}

	public AppUserManageVo findUserInfoById(Integer userId) {
		AppUserManageVo appUserManageVo = new AppUserManageVo();
		List<AppUserManageVo> appUserManageVos = mapper.selectAppUserManage(userId);
		if (CollectionUtils.isEmpty(appUserManageVos)) {
			return appUserManageVo;
		}
		return appUserManageVos.get(0);
	}
}
