package com.hhh.cloud.framework.example.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhh.cloud.framework.example.client.entity.SysClient;
import com.hhh.cloud.framework.example.client.mapper.SysClientMapper;
import com.hhh.cloud.framework.example.client.service.ISysClientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhh.cloud.framework.util.CommonUtil;
import com.hhh.cloud.framework.util.enumeration.CodeEnum;
import com.hhh.cloud.framework.util.result.TableResult;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 3hhjs
 * @since 2020-12-28
 */
@Service
public class SysClientServiceImpl extends ServiceImpl<SysClientMapper, SysClient> implements ISysClientService {

    @Override
    public TableResult listClient(SysClient search, int page, int limit) {

        //QueryWrapper查询法
        /*QueryWrapper<SysClient> queryWrapper = new QueryWrapper <>();
        if(CommonUtil.isNotEmpty(search.getClientName())) {
            queryWrapper.eq("client_name", search.getClientName());
        }
        queryWrapper.in("status", 1, 2);

        queryWrapper.orderByDesc("created_date");
        IPage<SysClient> searchPage = new Page<>(page, limit);
        searchPage = baseMapper.selectPage(searchPage, queryWrapper);*/

        //LambdaQueryWrapper查询法
        LambdaQueryWrapper<SysClient> queryWrapper = new LambdaQueryWrapper<>();
        if(CommonUtil.isNotEmpty(search.getClientName())) {
            queryWrapper.eq(SysClient::getClientName, search.getClientName());
        }
        if(null != search.getStatusArr() && search.getStatusArr().length > 0) {
            queryWrapper.in(SysClient::getStatus, search.getStatusArr());
        }

        queryWrapper.orderByDesc(SysClient::getCreatedDate);
        IPage<SysClient> searchPage = new Page<>(page, limit);
        searchPage = baseMapper.selectPage(searchPage, queryWrapper);
        return new TableResult(CodeEnum.SUCCESS.get(), searchPage.getRecords(), searchPage.getTotal(), "");
    }

    @Override
    public TableResult listClient2(SysClient search, int page, int limit) {
        Page<SysClient> list = baseMapper.list(new Page<>(page, limit), search);
        return new TableResult(CodeEnum.SUCCESS.get(), list.getRecords(), list.getTotal(), "查询成功");
    }
}
