package com.zhgd.xmgl.modules.basicdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhgd.jeecg.common.system.query.QueryGenerator;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.dto.SystemUserDto;
import com.zhgd.xmgl.modules.basicdata.entity.SystemUser;
import com.zhgd.xmgl.modules.basicdata.entity.SystemUserRole;
import com.zhgd.xmgl.modules.basicdata.mapper.SystemUserMapper;
import com.zhgd.xmgl.modules.basicdata.mapper.SystemUserRoleMapper;
import com.zhgd.xmgl.modules.basicdata.service.ISystemUserService;
import com.zhgd.xmgl.modules.basicdata.vo.SystemUserVo;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @Description: 系统用户表
 * @author： pds
 * @date：   2023-02-08
 * @version： V1.0
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser> implements ISystemUserService {

    @Autowired
    private SystemUserRoleMapper systemUserRoleMapper;

    @Override
    public Page<SystemUserDto> getPageList(Map<String, Object> map) {
        Page<SystemUser> page = PageUtil.getPage(map);
        SecurityUser user = SecurityUtil.getUser();
        QueryWrapper<SystemUser> wrapper = QueryGenerator.initPageQueryWrapper(SystemUser.class, map);
        wrapper.eq("u.account_type", user.getAccountType());
        wrapper.eq("u.sn", user.getSn());
        if (StringUtils.isNotBlank(MapUtils.getString(map, "beginTime"))) {
            wrapper.ge("u.create_time", MapUtils.getString(map, "beginTime"));
        }
        if (StringUtils.isNotBlank(MapUtils.getString(map, "endTime"))) {
            wrapper.le("u.create_time", MapUtils.getString(map, "endTime"));
        }
        return baseMapper.getPageList(page, wrapper);
    }

    @Override
    public int saveInfo(SystemUserVo systemUserVo) {
        SystemUser systemUser = new SystemUser();
        BeanUtils.copyProperties(systemUserVo, systemUser);
        systemUser.setCreateTime(new Date());
        this.save(systemUser);
        //新增用户角色
        SystemUserRole systemUserRole = new SystemUserRole();
        systemUserRole.setRoleId(systemUserVo.getRoleId());
        systemUserRole.setUserId(systemUser.getUserId());
        return systemUserRoleMapper.insert(systemUserRole);
    }

    @Override
    public int updateInfo(SystemUserVo systemUserVo) {
        SystemUser systemUser = new SystemUser();
        BeanUtils.copyProperties(systemUserVo, systemUser);
        systemUser.setCreateTime(new Date());
        this.updateById(systemUser);
        //新增用户角色
        systemUserRoleMapper.delete(Wrappers.<SystemUserRole>lambdaQuery()
                .eq(SystemUserRole::getUserId, systemUser.getUserId()));
        SystemUserRole systemUserRole = new SystemUserRole();
        systemUserRole.setRoleId(systemUserVo.getRoleId());
        systemUserRole.setUserId(systemUser.getUserId());
        return systemUserRoleMapper.insert(systemUserRole);
    }
}
