package com.mcwl.communityCenter.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.page.PageDomain;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.common.utils.StringUtils;
import com.mcwl.communityCenter.domain.Community;
import com.mcwl.communityCenter.domain.dto.CommunityRes;
import com.mcwl.communityCenter.domain.vo.CommunityVo;
import com.mcwl.communityCenter.mapper.CommunityMapper;
import com.mcwl.communityCenter.service.CommunityService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community> implements CommunityService {


    @Override
    public TableDataInfo listByPage(PageDomain pageDomain) {

        Page<Community> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());


        boolean isAsc = Objects.equals(pageDomain.getIsAsc(), "asc");

        if (StringUtils.isBlank(pageDomain.getOrderByColumn())) {
            pageDomain.setOrderByColumn("create_time");
        }
        OrderItem orderItem = new OrderItem(pageDomain.getOrderByColumn(), isAsc);
        page.addOrder(orderItem);

        baseMapper.selectPage(page, null);

        List<Community> communityList = page.getRecords();
        List<CommunityVo> communityVoList = new ArrayList<>();
        for (Community community : communityList) {
            CommunityVo communityVo = new CommunityVo();
            BeanUtil.copyProperties(community, communityVo);
            communityVoList.add(communityVo);
        }

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(communityVoList);
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");

        return tableDataInfo;
    }

    @Override
    public void addCommunity(CommunityRes communityRes) {
        Community community = new Community();
        BeanUtil.copyProperties(communityRes, community);
        baseMapper.insert(community);

    }


}
