package com.yuchen.saas.park.service.service.repair.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.repair.SaasMatterRepairTypeDto;
import com.yuchen.saas.park.api.dto.repair.SaasMatterRepairTypeQueryDto;
import com.yuchen.saas.park.api.entity.repair.SaasMatterRepairType;
import com.yuchen.saas.park.api.vo.repair.SaasMatterRepairTypeVo;
import com.yuchen.saas.park.service.mapper.repair.SaasMatterRepairTypeMapper;
import com.yuchen.saas.park.service.service.repair.ISaasMatterRepairTypeService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.utils.BeanUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 报事报修类型表 服务实现类
 * </p>
 *
 * @author caiwq
 * @since 2024-04-22
 */
@Service
public class SaasMatterRepairTypeServiceImpl extends BaseServiceImpl<SaasMatterRepairTypeMapper, SaasMatterRepairType> implements ISaasMatterRepairTypeService {

    @Override
    public Boolean add(SaasMatterRepairTypeDto matterRepairTypeDto) {
        SaasMatterRepairType matterRepairType = BeanUtil.copyProperties(matterRepairTypeDto, SaasMatterRepairType.class);
        matterRepairType.setCreateOrgUser(AuthUtil.getUser().getOrgUserId());
        return save(matterRepairType);
    }

    @Override
    public Boolean edit(SaasMatterRepairTypeDto matterRepairTypeDto) {
        SaasMatterRepairType matterRepairType = BeanUtil.copyProperties(matterRepairTypeDto, SaasMatterRepairType.class);
        return updateById(matterRepairType);
    }

    @Override
    public Boolean remove(Long id) {
        return removeById(id);
    }

    @Override
    public NestPage<SaasMatterRepairTypeVo> list(SaasMatterRepairTypeQueryDto queryDto) {
        LambdaQueryWrapper<SaasMatterRepairType> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Func.isNotEmpty(queryDto.getProjectId()), SaasMatterRepairType::getProjectId, queryDto.getProjectId());
        queryWrapper.eq(Func.isNotEmpty(queryDto.getType()), SaasMatterRepairType::getType, queryDto.getType());
        queryWrapper.like(Func.isNotEmpty(queryDto.getName()), SaasMatterRepairType::getName, queryDto.getName());
        queryWrapper.like(Func.isNotEmpty(queryDto.getCode()), SaasMatterRepairType::getCode, queryDto.getCode());
        queryWrapper.orderByDesc(SaasMatterRepairType::getCreateTime);
        IPage<SaasMatterRepairType> page = this.page(Condition.getPage(queryDto), queryWrapper);
        IPage<SaasMatterRepairTypeVo> result = new Page<>(queryDto.getCurrent(), queryDto.getSize());
        List<SaasMatterRepairType> recordList = page.getRecords();
        List<SaasMatterRepairTypeVo> list = BeanUtil.copyProperties(recordList, SaasMatterRepairTypeVo.class);
        result.setRecords(list);
        result.setTotal(page.getTotal());
        return NestPage.of(result);
    }

    @Override
    public List<SaasMatterRepairTypeVo> listAll(SaasMatterRepairTypeQueryDto queryDto) {
        LambdaQueryWrapper<SaasMatterRepairType> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Func.isNotEmpty(queryDto.getProjectId()), SaasMatterRepairType::getProjectId, queryDto.getProjectId());
        queryWrapper.eq(Func.isNotEmpty(queryDto.getType()), SaasMatterRepairType::getType, queryDto.getType());
        queryWrapper.like(Func.isNotEmpty(queryDto.getName()), SaasMatterRepairType::getName, queryDto.getName());
        queryWrapper.like(Func.isNotEmpty(queryDto.getCode()), SaasMatterRepairType::getCode, queryDto.getCode());
        queryWrapper.orderByDesc(SaasMatterRepairType::getCreateTime);
        List<SaasMatterRepairType> list = list(queryWrapper);
        return BeanUtil.copyProperties(list, SaasMatterRepairTypeVo.class);
    }

}
