package com.ltgk.catches.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltgk.catches.domain.dto.GetFishLimitAllocationListReq;
import com.ltgk.catches.domain.dto.SaveFishLimitAllocationReq;
import com.ltgk.catches.domain.entity.TcFishLimitAllocation;
import com.ltgk.catches.domain.vo.FishLimitAllocationVo;
import com.ltgk.catches.mapper.TcFishLimitAllocationMapper;
import com.ltgk.catches.service.TcFishLimitAllocationService;
import com.ltgk.common.core.domain.AjaxResult;
import com.ltgk.common.utils.DateUtils;
import com.ltgk.common.utils.SecurityUtils;
import com.ltgk.common.utils.StringUtils;
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 TcFishLimitAllocationServiceImpl extends ServiceImpl<TcFishLimitAllocationMapper, TcFishLimitAllocation> implements TcFishLimitAllocationService {
    @Autowired
    private TcFishLimitAllocationMapper tcFishLimitAllocationMapper;

    @Override
    public AjaxResult save(List<SaveFishLimitAllocationReq> reqList) {

        List<TcFishLimitAllocation> tcFishLimitAllocationList = BeanUtil.copyToList(reqList, TcFishLimitAllocation.class);
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        tcFishLimitAllocationList.forEach(item -> {
            if (StringUtils.isBlank(item.getId())) {
                item.setCreateBy(userName);
                item.setCreateAt(DateUtils.getNowDate());
            }
            item.setUpdateBy(userName);
            item.setUpdateAt(DateUtils.getNowDate());
        });
        return AjaxResult.success(this.saveOrUpdateBatch(tcFishLimitAllocationList));

    }

    @Override
    public List<FishLimitAllocationVo> list(GetFishLimitAllocationListReq getFishLimitAllocationListReq) {
        startPage(getFishLimitAllocationListReq.getPageNum(), getFishLimitAllocationListReq.getPageSize());
        return tcFishLimitAllocationMapper.getFishLimitAllocationVo(getFishLimitAllocationListReq);
    }
}
