package com.ltgk.catches.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltgk.catches.domain.dto.GetFishLimitSetListReq;
import com.ltgk.catches.domain.dto.SaveFishLimitSetReq;
import com.ltgk.catches.domain.entity.TcFishLimitSet;
import com.ltgk.catches.mapper.TcFishLimitSetMapper;
import com.ltgk.catches.service.TcFishLimitSetService;
import com.ltgk.common.core.domain.AjaxResult;
import com.ltgk.common.utils.DateUtils;
import com.ltgk.common.utils.SecurityUtils;
import com.ltgk.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

@Service
public class TcFishLimitSetServiceImpl extends ServiceImpl<TcFishLimitSetMapper, TcFishLimitSet> implements TcFishLimitSetService {
    @Autowired
    private TcFishLimitSetMapper tcFishLimitSetMapper;

    @Override
    public AjaxResult save(SaveFishLimitSetReq saveFishLimitSetReq) {
        TcFishLimitSet tcFishLimitSet = new TcFishLimitSet();
        BeanUtils.copyProperties(saveFishLimitSetReq, tcFishLimitSet);
        if (ObjectUtils.isNotEmpty(saveFishLimitSetReq.getId())) {
            tcFishLimitSet.setUpdateBy(SecurityUtils.getUserId().toString());
            tcFishLimitSet.setUpdateAt(DateUtils.getNowDate());
        } else {
            tcFishLimitSet.setCreateBy(SecurityUtils.getUserId().toString());
            tcFishLimitSet.setCreateAt(DateUtils.getNowDate());
        }
        return AjaxResult.success(this.saveOrUpdate(tcFishLimitSet));
    }

    @Override
    public List<TcFishLimitSet> list(GetFishLimitSetListReq req) {
        TcFishLimitSet entity = new TcFishLimitSet();
        BeanUtils.copyProperties(req, entity);
        LambdaQueryWrapper<TcFishLimitSet> wrapper = new LambdaQueryWrapper<>(entity);
        startPage(req.getPageNum(), req.getPageSize());
        return this.list(wrapper);
    }
}
