package com.sptg.permissions.biz.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.constant.CommonConstant;
import com.sptg.permissions.api.dto.AccountDto;
import com.sptg.permissions.api.entity.DeviceAdminUser;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.biz.mapper.DeviceAdminUserMapper;
import com.sptg.permissions.biz.service.DeviceAdminUserService;
import com.sptg.permissions.biz.service.OrgServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * title: DeviceAdminUserServiceImpl
 * description:
 *
 *
 *
 */
@Service
@RequiredArgsConstructor
public class DeviceAdminUserServiceImpl extends ServiceImpl<DeviceAdminUserMapper, DeviceAdminUser> implements DeviceAdminUserService {

    private final OrgServiceInterface orgService;

    @Override
    public Page<DeviceAdminUser> pagingQuery(PageBean<DeviceAdminUser, AccountDto> pageBean) {
        AccountDto dto = pageBean.getDto();
        //  构建查询对象
        Wrapper<DeviceAdminUser> wrapper = new EntityWrapper<>();
        //  判断是否选中机构
        if (null != dto.getSelectedId()){
            Organization selectedOrg = this.orgService.selectById(dto.getSelectedId());
            if (null == selectedOrg){
                return pageBean;
            }
            wrapper.like("org_position",selectedOrg.getPosition(), SqlLike.RIGHT);
        }
        //  判断是否以登录名为条件
        if (!StringUtils.isEmpty(dto.getLoginName())){
            wrapper.like("login_name",dto.getLoginName(), SqlLike.RIGHT);
        }
        //  判断是否以用户姓名为条件
        if (!StringUtils.isEmpty(dto.getUserName())){
            wrapper.like("user_name",dto.getUserName(), SqlLike.RIGHT);
        }
        //  判断是否以登陆状态作为条件
        if (null != dto.getIsEnable()){
            wrapper.eq("is_enable",dto.getIsEnable());
        }
        //  判断是否以联系方式为条件
        if (!StringUtils.isEmpty(dto.getUserMobile())){
            wrapper.like("user_mobile",dto.getUserMobile(), SqlLike.RIGHT);
        }
        //  数据过滤，只查询用户所属机构和子机构下的用户信息
        wrapper.like("org_position",dto.getOrgPosition(), SqlLike.RIGHT);
        //  排序字段 时间降序
        wrapper.orderBy("create_time",false);
        Page<DeviceAdminUser> page = this.selectPage(pageBean, wrapper);
        //  隐藏用户密码
//        page.getRecords().forEach(account -> account.setLoginPwd(CommonConstant.HIDE));
        return page;
    }
}
