package org.springblade.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wn.common.datamodel.ModelConvertUtil;
import com.wn.common.validator.Validator;
import org.springblade.modules.app.common.dto.ProblemBackDTO;
import org.springblade.modules.app.common.query.ProblemBackQuery;
import org.springblade.modules.app.constant.DbField;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.entity.ProblemBackDO;
import org.springblade.modules.app.mapper.ProblemBackMapper;
import org.springblade.modules.app.service.IProblemBackService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 问题反馈表 服务实现类
 * </p>
 *
 * @author zhangK
 * @since 2022-01-13
 */
@Service
public class ProblemBackServiceImpl extends ServiceImpl<ProblemBackMapper, ProblemBackDO> implements IProblemBackService {

    @Override
    public Long insert(ProblemBackDTO dto) {
        ProblemBackDO problemBackDO = dto.copyTo(ProblemBackDO.class);
        Validator.isTrue(save(problemBackDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return problemBackDO.getId();
    }

    @Override
    public ProblemBackDTO infoById(Long id) {
        Validator.notNull(id, ErrorCode.INPUT_PARAMETERS_NULL);
        ProblemBackDO problemBackDO = getById(id);
        return problemBackDO == null ? null : problemBackDO.copyTo(ProblemBackDTO.class);
    }

    @Override
    public Page<ProblemBackDTO> page(ProblemBackQuery query, List<Long> ids) {
        Page<ProblemBackDO> page = query.covertToPage();
        QueryWrapper<ProblemBackDO> wrapper = new QueryWrapper<>();
        wrapper.in(CollectionUtils.isNotEmpty(ids), DbField.USER_ID, ids);
        wrapper.eq(query.getState()!=null && query.getState().intValue()>=0,DbField.STATE,query.getState());
        wrapper.le(StringUtils.isNotBlank(query.getEndTime()), DbField.CREATED_TIME, query.getEndTime());
        wrapper.ge(StringUtils.isNotBlank(query.getStartTime()), DbField.CREATED_TIME, query.getStartTime());
        wrapper.orderByDesc(DbField.CREATED_TIME);
        page = page(page, wrapper);
        return ModelConvertUtil.copyPageTo(page, ProblemBackDTO.class);
    }

    @Override
    public Boolean update(ProblemBackDTO dto) {
        Validator.isTrue(updateById(dto.copyTo(ProblemBackDO.class)), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return true;
    }
}
