package com.njzscloud.supervisory.discount.service.impl;

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.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.biz.pojo.entity.BizCompanyEntity;
import com.njzscloud.supervisory.biz.service.BizCompanyService;
import com.njzscloud.supervisory.discount.mapper.DiscountManageMapper;
import com.njzscloud.supervisory.discount.pojo.DiscountManageEntity;
import com.njzscloud.supervisory.discount.service.DiscountManageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 优惠管理
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DiscountManageServiceImpl extends ServiceImpl<DiscountManageMapper, DiscountManageEntity> implements DiscountManageService {

    private final BizCompanyService bizCompanyService;

    /**
     * 新增
     */
    @Override
    public void add(DiscountManageEntity discountManageEntity) {
        this.save(discountManageEntity);
    }

    /**
     * 修改
     */
    @Override
    public void modify(DiscountManageEntity discountManageEntity) {
        this.updateById(discountManageEntity);
    }

    /**
     * 删除
     */
    @Override
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    @Override
    public DiscountManageEntity detail(Long id) {
        DiscountManageEntity entity = this.getById(id);
        BizCompanyEntity companyEntity = bizCompanyService.getById(entity.getCompanyId());
        if (null != companyEntity) {
            entity.setCompanyName(companyEntity.getCompanyName());
        }
        return entity;
    }

    /**
     * 分页查询
     */
    @Override
    public PageResult<DiscountManageEntity> paging(PageParam pageParam, DiscountManageEntity discountManageEntity) {
        Page<DiscountManageEntity> page = this.page(pageParam.toPage(), Wrappers.<DiscountManageEntity>lambdaQuery()
                .eq(discountManageEntity.getCompanyId() != null, DiscountManageEntity::getCompanyId, discountManageEntity.getCompanyId())
                .eq(StrUtil.isNotBlank(discountManageEntity.getFeeType()), DiscountManageEntity::getFeeType, discountManageEntity.getFeeType())
                .eq(discountManageEntity.getExpenseId() != null, DiscountManageEntity::getExpenseId, discountManageEntity.getExpenseId())
                .eq(StrUtil.isNotBlank(discountManageEntity.getType()), DiscountManageEntity::getType, discountManageEntity.getType())
                .eq(discountManageEntity.getDeleted() != null, DiscountManageEntity::getDeleted, Boolean.FALSE)
                .orderByDesc(DiscountManageEntity::getCreateTime));
        for (DiscountManageEntity entity: page.getRecords()) {
            BizCompanyEntity companyEntity = bizCompanyService.getById(entity.getCompanyId());
            if (null != companyEntity) {
                entity.setCompanyName(companyEntity.getCompanyName());
            }
        }
        return PageResult.of(page);
    }

}

