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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.PictureInfoDTO;
import org.springblade.modules.app.common.query.PictureQuery;
import org.springblade.modules.app.constant.DbField;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.entity.PictureInfoDO;
import org.springblade.modules.app.mapper.PictureInfoMapper;
import org.springblade.modules.app.service.IPictureInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 附件表 服务实现类
 * </p>
 *
 * @author fuxinsheng
 * @since 2021-11-05
 */
@Service
public class PictureInfoServiceImpl extends ServiceImpl<PictureInfoMapper, PictureInfoDO> implements IPictureInfoService {

    @Override
    public Boolean addPictureInfo(PictureInfoDTO updatePictureInfoDTO) {
        PictureInfoDO pictureInfoDO = updatePictureInfoDTO.copyTo(PictureInfoDO.class);
        Validator.isTrue(save(pictureInfoDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return true;
    }

    @Override
    public PictureInfoDTO getPictureInfo(PictureQuery pictureQuery) {
        Validator.notNull(pictureQuery, ErrorCode.INPUT_PARAMETERS_NULL);
        QueryWrapper<PictureInfoDO> wrapper = new QueryWrapper<>();
        wrapper.eq(DbField.LINK_ID, pictureQuery.getLinkId());
        wrapper.eq(pictureQuery.getType() != null, DbField.TYPE, pictureQuery.getType());
        wrapper.last("limit 1");
        PictureInfoDO pictureInfoDO = getOne(wrapper);
        return pictureInfoDO == null ? null : pictureInfoDO.copyTo(PictureInfoDTO.class);
    }

    @Override
    public Boolean updatePictureInfo(PictureInfoDTO updatePictureInfoDTO) {
        PictureInfoDO pictureInfoDO = updatePictureInfoDTO.copyTo(PictureInfoDO.class);
        Validator.isTrue(updateById(pictureInfoDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return true;
    }

    @Override
    public List<PictureInfoDTO> listPictureInfo(PictureQuery pictureQuery) {
        Validator.notNull(pictureQuery, ErrorCode.INPUT_PARAMETERS_NULL);
        QueryWrapper<PictureInfoDO> wrapper = new QueryWrapper<>();
        wrapper.eq(pictureQuery.getLinkId() != null, DbField.LINK_ID, pictureQuery.getLinkId());
        wrapper.eq(pictureQuery.getType() != null, DbField.TYPE, pictureQuery.getType());
        wrapper.orderBy(true, true, DbField.SORT);
        List<PictureInfoDO> list = list(wrapper);
        return CollectionUtils.isNotEmpty(list) ? ModelConvertUtil.copyListTo(list, PictureInfoDTO.class) : null;
    }

    @Override
    public Boolean deleteByLinkId(PictureQuery query) {
        Validator.notNull(query, ErrorCode.INPUT_PARAMETERS_NULL);
        QueryWrapper<PictureInfoDO> wrapper = new QueryWrapper<>();
        wrapper.eq(DbField.LINK_ID, query.getLinkId());
        return remove(wrapper);
    }
}
