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.constant.InvoiceType;
import com.njzscloud.supervisory.biz.mapper.BizInvoiceMapper;
import com.njzscloud.supervisory.biz.pojo.entity.BizInvoiceEntity;
import com.njzscloud.supervisory.biz.pojo.param.SearchBizInvoiceParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 发票抬头管理
 * @author ljw
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BizInvoiceService extends ServiceImpl<BizInvoiceMapper, BizInvoiceEntity> {

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

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

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

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

    /**
     * 分页查询
     *
     * @param pageParam 分页参数
     * @param searchParam 查询参数
     * @return PageResult<BizInvoiceEntity> 分页结果
     */
    public PageResult<BizInvoiceEntity> paging(PageParam pageParam, SearchBizInvoiceParam searchParam) {
        String contact = searchParam.getContact();
        String companyName = searchParam.getCompanyName();
        String phone = searchParam.getPhone();
        InvoiceType type = searchParam.getType();

        Page<BizInvoiceEntity> page = this.page(pageParam.toPage(), Wrappers.<BizInvoiceEntity>lambdaQuery()
                .like(StrUtil.isNotBlank(contact), BizInvoiceEntity::getContact, contact)
                .like(StrUtil.isNotBlank(companyName), BizInvoiceEntity::getCompanyName, companyName)
                .like(StrUtil.isNotBlank(phone), BizInvoiceEntity::getPhone, phone)
                .eq(type != null, BizInvoiceEntity::getType, type)
                .orderByDesc(BizInvoiceEntity::getCreateTime));
        return PageResult.of(page);
    }

}
