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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.common.datamodel.ModelConvertUtil;
import com.wn.common.validator.Validator;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.app.common.dto.EnteringZhejiangFilingDTO;
import org.springblade.modules.app.common.query.EnteringZhejiangFilingListQuery;
import org.springblade.modules.app.common.query.EnteringZhejiangFilingQuery;
import org.springblade.modules.app.constant.DbField;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.entity.EnteringZhejiangFilingDO;
import org.springblade.modules.app.mapper.EnteringZhejiangFilingMapper;
import org.springblade.modules.app.service.IEnteringZhejiangFilingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 入浙备案登记信息 服务实现类
 * </p>
 *
 * @author huyinuo
 * @since 2022-01-11
 */
@Service
public class EnteringZhejiangFilingServiceImpl extends ServiceImpl<EnteringZhejiangFilingMapper, EnteringZhejiangFilingDO> implements IEnteringZhejiangFilingService {

    @Override
    public Long addEnteringZhejiangFiling(EnteringZhejiangFilingDTO enteringZhejiangFilingDTO) {
        Validator.notNull(enteringZhejiangFilingDTO, ErrorCode.INPUT_PARAMETERS_NULL);
        EnteringZhejiangFilingDO enteringZhejiangFilingDO = ModelConvertUtil.copyTo(enteringZhejiangFilingDTO, EnteringZhejiangFilingDO.class);
        Validator.isTrue(save(enteringZhejiangFilingDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return enteringZhejiangFilingDO.getId();
    }

    @Override
    public EnteringZhejiangFilingDTO getEnteringZhejiangFilingByUserId(Long userId) {
        Validator.notNull(userId, ErrorCode.INPUT_PARAMETERS_NULL);
        QueryWrapper<EnteringZhejiangFilingDO> wrapper = new QueryWrapper<>();
        wrapper.eq(DbField.USER_ID, userId);
        EnteringZhejiangFilingDO enteringZhejiangFilingDO = getOne(wrapper);
        if(StringUtil.isEmpty(enteringZhejiangFilingDO)){
            return null;
        }
        return ModelConvertUtil.copyTo(enteringZhejiangFilingDO, EnteringZhejiangFilingDTO.class);
    }

    @Override
    public Page<EnteringZhejiangFilingDTO> pageEnteringZhejiangFiling(EnteringZhejiangFilingQuery enteringZhejiangFilingQuery) {
        Validator.notNull(enteringZhejiangFilingQuery, ErrorCode.INPUT_PARAMETERS_NULL);
        QueryWrapper<EnteringZhejiangFilingDO> wrapper = new QueryWrapper<>();
        Page<EnteringZhejiangFilingDO> page = enteringZhejiangFilingQuery.covertToPage();
        wrapper.eq(ObjectUtils.isNotEmpty(enteringZhejiangFilingQuery.getUserId()), DbField.USER_ID, enteringZhejiangFilingQuery.getUserId());
        wrapper.like(StringUtils.isNotBlank(enteringZhejiangFilingQuery.getUserName()), DbField.USER_NAME, enteringZhejiangFilingQuery.getUserName());
        wrapper.eq(ObjectUtils.isNotEmpty(enteringZhejiangFilingQuery.getStatus()), DbField.STATUS, enteringZhejiangFilingQuery.getStatus());
        wrapper.ge(ObjectUtils.isNotEmpty(enteringZhejiangFilingQuery.getStartTime()), DbField.CREATED_TIME, enteringZhejiangFilingQuery.getStartTime());
        wrapper.le(ObjectUtils.isNotEmpty(enteringZhejiangFilingQuery.getEndTime()), DbField.CREATED_TIME, enteringZhejiangFilingQuery.getEndTime());
        wrapper.orderByDesc(DbField.CREATED_TIME);
        Page<EnteringZhejiangFilingDO> enteringZhejiangFilingDOPage = page(page, wrapper);
        return ModelConvertUtil.copyPageTo(enteringZhejiangFilingDOPage, EnteringZhejiangFilingDTO.class);
    }

    @Override
    public EnteringZhejiangFilingDTO getEnteringZhejiangFilingById(Long id) {
        Validator.notNull(id, ErrorCode.INPUT_PARAMETERS_NULL);
        EnteringZhejiangFilingDO enteringZhejiangFilingDO = getById(id);
        return ModelConvertUtil.copyTo(enteringZhejiangFilingDO, EnteringZhejiangFilingDTO.class);
    }

    @Override
    public Boolean updateEnteringZhejiangFiling(EnteringZhejiangFilingDTO enteringZhejiangFilingDTO) {
        Validator.notNull(enteringZhejiangFilingDTO, ErrorCode.INPUT_PARAMETERS_NULL);
        EnteringZhejiangFilingDO enteringZhejiangFilingDO = ModelConvertUtil.copyTo(enteringZhejiangFilingDTO, EnteringZhejiangFilingDO.class);
        Validator.isTrue(updateById(enteringZhejiangFilingDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return true;
    }

    @Override
    public List<EnteringZhejiangFilingDTO> listEnteringZhejiangFilingList(EnteringZhejiangFilingListQuery enteringZhejiangFilingListQuery) {
        Validator.notNull(enteringZhejiangFilingListQuery, ErrorCode.INPUT_PARAMETERS_NULL);
        QueryWrapper<EnteringZhejiangFilingDO> wrapper = new QueryWrapper<>();
        wrapper.in(CollectionUtils.isNotEmpty(enteringZhejiangFilingListQuery.getIds()), DbField.ID, enteringZhejiangFilingListQuery.getIds());
        List<EnteringZhejiangFilingDO> enteringZhejiangFilingDOList = list(wrapper);
        return ModelConvertUtil.copyListTo(enteringZhejiangFilingDOList, EnteringZhejiangFilingDTO.class);
    }
}
