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.GetFishEarlyReq;
import com.ltgk.catches.domain.dto.HandleEarlyReq;
import com.ltgk.catches.domain.entity.TcFishLimitEarly;
import com.ltgk.catches.domain.vo.FishLimitEarlyVo;
import com.ltgk.catches.mapper.TcFishLimitEarlyMapper;
import com.ltgk.catches.service.TcFishLimitEarlyService;
import com.ltgk.common.constant.ReturnMsgConstants;
import com.ltgk.common.core.domain.AjaxResult;
import com.ltgk.common.utils.DateUtils;
import com.ltgk.common.utils.SecurityUtils;
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 TcFishLimitEarlyServiceImpl extends ServiceImpl<TcFishLimitEarlyMapper, TcFishLimitEarly> implements TcFishLimitEarlyService {
    @Autowired
    private TcFishLimitEarlyMapper tcFishLimitEarlyMapper;

    @Override
    public List<FishLimitEarlyVo> list(GetFishEarlyReq getFishEarlyReq) {
        startPage(getFishEarlyReq.getPageNum(), getFishEarlyReq.getPageSize());
        return tcFishLimitEarlyMapper.list(getFishEarlyReq);
    }

    @Override
    public AjaxResult handleEarly(HandleEarlyReq handleEarlyReq) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        LambdaQueryWrapper<TcFishLimitEarly> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TcFishLimitEarly::getId, handleEarlyReq.getId());
        TcFishLimitEarly tcFishLimitEarly = this.getOne(lqw);
        if (ObjectUtils.isEmpty(tcFishLimitEarly)) return AjaxResult.error(ReturnMsgConstants.LIMIT_WARN_NOT_EXIT);
        tcFishLimitEarly.setHandlePerson(handleEarlyReq.getHandlePerson())
                .setHandlePhone(handleEarlyReq.getHandlePhone())
                .setHandleDate(DateUtils.parseDate(handleEarlyReq.getHandleDate()))
                .setHandleDesc(handleEarlyReq.getHandleDesc())
                .setHandleResult(handleEarlyReq.getHandleResult())
                .setRationale(handleEarlyReq.getRationale())
                .setBeHandle(1)
                .setUpdateBy(userName)
                .setUpdateAt(DateUtils.getNowDate())
        ;
        return this.updateById(tcFishLimitEarly) ? AjaxResult.success() : AjaxResult.error();
    }
}
