package com.njzscloud.supervisory.biz.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.biz.mapper.BizPatrolReportMapper;
import com.njzscloud.supervisory.biz.pojo.entity.BizPatrolReportEntity;
import com.njzscloud.supervisory.biz.pojo.param.SearchBizPatrolReportParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 巡查上报
 *
 * @author ljw
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BizPatrolReportService extends ServiceImpl<BizPatrolReportMapper, BizPatrolReportEntity> {

    /**
     * 新增
     *
     * @param bizPatrolReportEntity 数据
     */
    public void add(BizPatrolReportEntity bizPatrolReportEntity) {
        this.save(bizPatrolReportEntity);
    }

    /**
     * 修改
     *
     * @param bizPatrolReportEntity 数据
     */
    public void modify(BizPatrolReportEntity bizPatrolReportEntity) {
        Long id = bizPatrolReportEntity.getId();
        BizPatrolReportEntity oldData = getById(id);
        if (oldData == null) {
            throw Exceptions.exception("未找到要修改的数据");
        }
        this.updateById(bizPatrolReportEntity);
    }

    /**
     * 删除
     *
     * @param id Id
     */
    public void remove(Long id) {
        BizPatrolReportEntity oldData = getById(id);
        if (oldData == null) {
            throw Exceptions.exception("未找到要删除的数据");
        }
        this.removeById(id);
    }

    /**
     * 详情
     *
     * @param id Id
     * @return BizPatrolReportEntity 结果
     */
    public BizPatrolReportEntity detail(Long id) {
        BizPatrolReportEntity bizPatrolReportEntity = this.getById(id);
        if (bizPatrolReportEntity == null) {
            throw Exceptions.exception("数据不存在");
        }
        return bizPatrolReportEntity;
    }

    /**
     * 分页查询
     *
     * @param pageParam   分页参数
     * @param searchParam 查询参数
     * @return PageResult<BizPatrolReportEntity> 分页结果
     */
    public PageResult<BizPatrolReportEntity> paging(PageParam pageParam, SearchBizPatrolReportParam searchParam) {
        String contact = searchParam.getContact();
        String phone = searchParam.getPhone();

        Page<BizPatrolReportEntity> page = this.page(pageParam.toPage(), Wrappers.<BizPatrolReportEntity>lambdaQuery()
                .like(StrUtil.isNotBlank(contact), BizPatrolReportEntity::getContact, contact)
                .like(StrUtil.isNotBlank(phone), BizPatrolReportEntity::getPhone, phone)
                .orderByDesc(BizPatrolReportEntity::getCreateTime));
        return PageResult.of(page);
    }

}
