package com.template.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.mapper.SmartDutiesMapper;
import com.template.model.pojo.SmartDuties;
import com.template.model.result.PageUtils;
import com.template.services.SmartDutiesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 应用管理 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2024-01-16
 */
@Service
public class SmartDutiesServiceImpl extends ServiceImpl<SmartDutiesMapper, SmartDuties> implements SmartDutiesService {

    @Autowired
    private SmartDutiesMapper smartDutiesMapper;

    @Override
    public int insertSmartDuties(SmartDuties rns) {
        int result = smartDutiesMapper.insert(rns);
        return result;
    }

    @Override
    public int updateSmartDuties(SmartDuties rns) {
        int result = smartDutiesMapper.updateById(rns);
        return result;
    }

    @Override
    public PageUtils<SmartDuties> queryPageSmartDutiess(int currentPage, int pageCount, String name) {
        Page<SmartDuties> page = new Page<>(currentPage, pageCount);
        QueryWrapper<SmartDuties> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name), "name", name);
        IPage<SmartDuties> result = smartDutiesMapper.selectPage(page, queryWrapper);
        return new PageUtils<>(result);
    }

    @Override
    public int deleteSmartDutiesById(int id) {
        int result = smartDutiesMapper.deleteById(id);
        return result;
    }

    @Override
    public SmartDuties getSmartById(int id) {
        SmartDuties result = smartDutiesMapper.selectById(id);
        return result;
    }

    @Override
    public SmartDuties getSmartDuties() {
        SmartDuties newOne = this.getOne(new LambdaQueryWrapper<SmartDuties>()
                .orderBy(true, false, SmartDuties::getCreateTime)
                .last("limit 1")
        );

        return newOne == null ? new SmartDuties() : newOne;
    }

    @Override
    public SmartDuties querySmartDutiesByName(String name, Integer categoryId) {
        QueryWrapper<SmartDuties> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(name), "name", name)
                .eq(categoryId != null && categoryId > 0, "category_id", categoryId);
        SmartDuties result = smartDutiesMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public List<SmartDuties> getSmartDutiesByCategoryId(Integer categoryId) {
        QueryWrapper<SmartDuties> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(categoryId != null && categoryId > 0, "category_id", categoryId);

        List<SmartDuties> result = smartDutiesMapper.selectList(queryWrapper);

        return result;
    }
}
