package com.ltgk.port.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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 com.ltgk.common.utils.bean.BeanUtils;
import com.ltgk.port.domain.dto.SaveWarnAreaTypeReq;
import com.ltgk.port.domain.dto.WarnAreaTypeListReq;
import com.ltgk.port.domain.entity.WarnAreaType;
import com.ltgk.port.mapper.WarnAreaTypeMapper;
import com.ltgk.port.service.WarnAreaTypeService;
import org.springframework.stereotype.Service;

import java.util.List;

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

@Service
public class WarnAreaTypeServiceImpl extends ServiceImpl<WarnAreaTypeMapper, WarnAreaType> implements WarnAreaTypeService {

    @Override
    public AjaxResult save(SaveWarnAreaTypeReq saveWarnAreaTypeReq) {
        WarnAreaType warnAreaType = new WarnAreaType();
        BeanUtils.copyProperties(saveWarnAreaTypeReq, warnAreaType);
        if (StringUtils.isNotEmpty(saveWarnAreaTypeReq.getId())) {
            warnAreaType.setUpdateBy(SecurityUtils.getUserId().toString());
            warnAreaType.setUpdateAt(DateUtils.getNowDate());
        } else {
            warnAreaType.setCreateBy(SecurityUtils.getUserId().toString());
            warnAreaType.setCreateAt(DateUtils.getNowDate());
        }
        if (this.saveOrUpdate(warnAreaType)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    public List<WarnAreaType> list(WarnAreaTypeListReq warnAreaTypeListReq) {
        startPage(warnAreaTypeListReq.getPageNum(), warnAreaTypeListReq.getPageSize());
        LambdaQueryWrapper<WarnAreaType> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(warnAreaTypeListReq.getWarnAreaName()), WarnAreaType::getWarnAreaName, warnAreaTypeListReq.getWarnAreaName());
        lqw.like(StringUtils.isNotEmpty(warnAreaTypeListReq.getWarnAreaType()), WarnAreaType::getWarnAreaType, warnAreaTypeListReq.getWarnAreaType());
        lqw.eq(WarnAreaType::getDelFlag, 0);
        return this.list(lqw);
    }

    @Override
    public List<WarnAreaType> selectAll() {
        LambdaQueryWrapper<WarnAreaType> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WarnAreaType::getDelFlag, 0);
        return this.list(lqw);
    }
}
