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

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.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.estate.api.dto.UserFeedbackDto;
import com.sptg.estate.api.entity.UserFeedback;
import com.sptg.estate.biz.mapper.UserFeedbackMapper;
import com.sptg.estate.biz.service.UserFeedbackServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@RequiredArgsConstructor
public class UserFeedbackServiceImpl extends ServiceImpl<UserFeedbackMapper,UserFeedback> implements UserFeedbackServiceInterface {
    @Override
    public ResultBean<Page<UserFeedback>> pagingQuery(UserFeedbackDto userFeedbackDto) {
        Wrapper<UserFeedback> wrapper = new EntityWrapper<>();
        //根据类型
        wrapper.eq(userFeedbackDto.getType() != null,"i_type",userFeedbackDto.getType());
        //根据时间
        wrapper.ge(userFeedbackDto.getBeginDate() != null,"t_create_date",userFeedbackDto.getBeginDate());
        wrapper.le(userFeedbackDto.getEndDate() != null,"t_create_date",userFeedbackDto.getEndDate());
        //  排序字段
        if (userFeedbackDto.getSortField() != null && userFeedbackDto.getSortOrder() != null){
            wrapper.orderBy(userFeedbackDto.getSortField(),userFeedbackDto.getSortOrder().equals("asc"));
        }else {
            //  排序字段 时间降序
            wrapper.orderBy("t_create_date",false);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectPage(userFeedbackDto.getPage(),wrapper));
    }

    @Override
    public ResultBean<Boolean> add(UserFeedbackDto userFeedbackDto) {
        userFeedbackDto.setCreateDate(new Date());
        return new ResultBean<>(ResultEnum.SUCCESS,this.insert(userFeedbackDto));
    }

    @Override
    public ResultBean<UserFeedback> getOne(UserFeedbackDto userFeedbackDto) {
        Wrapper<UserFeedback> wrapper = new EntityWrapper<>();
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectOne(wrapper));
    }

    @Override
    public ResultBean<Boolean> handle(UserFeedbackDto userFeedbackDto) {
        boolean update = this.updateById(userFeedbackDto);
        if(update){
            return new ResultBean<>(ResultEnum.SUCCESS, update);
        }else{
            return new ResultBean<>(ResultEnum.FAILURE, update);
        }
    }
}
