package com.yuchen.saas.park.service.service.work.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.work.SaasWorkOrderTimeLimitDto;
import com.yuchen.saas.park.api.dto.work.SaasWorkOrderTimeLimitQueryDto;
import com.yuchen.saas.park.api.entity.work.SaasWorkOrderTimeLimit;
import com.yuchen.saas.park.api.vo.work.SaasWorkOrderTimeLimitVo;
import com.yuchen.saas.park.service.mapper.work.SaasWorkOrderTimeLimitMapper;
import com.yuchen.saas.park.service.service.work.ISaasWorkOrderTimeLimitService;
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.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-01
 */
@Service
public class SaasWorkOrderTimeLimitServiceImpl extends BaseServiceImpl<SaasWorkOrderTimeLimitMapper, SaasWorkOrderTimeLimit> implements ISaasWorkOrderTimeLimitService {

    @Override
    public Boolean add(SaasWorkOrderTimeLimitDto workOrderTimeLimitDto) {
        SaasWorkOrderTimeLimit workOrderTimeLimit = BeanUtil.copyProperties(workOrderTimeLimitDto, SaasWorkOrderTimeLimit.class);
        return save(workOrderTimeLimit);
    }

    @Override
    public Boolean edit(SaasWorkOrderTimeLimitDto workOrderTimeLimitDto) {
        SaasWorkOrderTimeLimit workOrderTimeLimit = BeanUtil.copyProperties(workOrderTimeLimitDto, SaasWorkOrderTimeLimit.class);
        return updateById(workOrderTimeLimit);
    }

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

    @Override
    public NestPage<SaasWorkOrderTimeLimitVo> list(SaasWorkOrderTimeLimitQueryDto queryDto) {
        LambdaQueryWrapper<SaasWorkOrderTimeLimit> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(Func.isNotEmpty(queryDto.getTimeLimitName()), SaasWorkOrderTimeLimit::getTimeLimitName, queryDto.getTimeLimitName());
        queryWrapper.orderByDesc(SaasWorkOrderTimeLimit::getCreateTime);
        IPage<SaasWorkOrderTimeLimit> page = this.page(Condition.getPage(queryDto), queryWrapper);
        IPage<SaasWorkOrderTimeLimitVo> result = new Page<>(queryDto.getCurrent(), queryDto.getSize());
        List<SaasWorkOrderTimeLimit> recordList = page.getRecords();
        List<SaasWorkOrderTimeLimitVo> list = BeanUtil.copyProperties(recordList, SaasWorkOrderTimeLimitVo.class);
        result.setRecords(list);
        result.setTotal(page.getTotal());
        return NestPage.of(result);
    }

    @Override
    public List<SaasWorkOrderTimeLimitVo> listAll(SaasWorkOrderTimeLimitQueryDto queryDto) {
        LambdaQueryWrapper<SaasWorkOrderTimeLimit> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(Func.isNotEmpty(queryDto.getTimeLimitName()), SaasWorkOrderTimeLimit::getTimeLimitName, queryDto.getTimeLimitName());
        queryWrapper.orderByDesc(SaasWorkOrderTimeLimit::getCreateTime);
        List<SaasWorkOrderTimeLimit> list = list(queryWrapper);
        return BeanUtil.copyProperties(list, SaasWorkOrderTimeLimitVo.class);
    }

}
