package com.njzscloud.dispose.goods.service;

import cn.hutool.core.lang.Assert;
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.common.sn.support.SnUtil;
import com.njzscloud.dispose.goods.contant.BizType;
import com.njzscloud.dispose.goods.mapper.GoodsCategoryMapper;
import com.njzscloud.dispose.goods.pojo.entity.GoodsCategoryEntity;
import com.njzscloud.dispose.goods.pojo.param.SearchGoodsCategoryParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 产品分类
 *
 * @author ljw
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class GoodsCategoryService extends ServiceImpl<GoodsCategoryMapper, GoodsCategoryEntity> {

    /**
     * 新增
     */
    public void add(GoodsCategoryEntity goodsCategoryEntity) {
        BizType bizType = goodsCategoryEntity.getBizType();
        Assert.notNull(bizType, () -> Exceptions.clierr("业务类型不能为空"));
        if (bizType == BizType.HuiShouPin) {
            String sn = goodsCategoryEntity.getSn();
            Assert.notBlank(sn, () -> Exceptions.clierr("国标码不能为空"));
        } else {
            goodsCategoryEntity.setSn(SnUtil.next("GC-SN"));
        }
        this.save(goodsCategoryEntity);
    }

    /**
     * 修改
     */
    public void modify(GoodsCategoryEntity goodsCategoryEntity) {
        this.updateById(goodsCategoryEntity);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     */
    public GoodsCategoryEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<GoodsCategoryEntity> paging(PageParam pageParam, SearchGoodsCategoryParam searchGoodsCategoryParam) {
        String categoryName = searchGoodsCategoryParam.getCategoryName();
        BizType bizType = searchGoodsCategoryParam.getBizType();
        LocalDateTime createTimeStart = searchGoodsCategoryParam.getCreateTimeStart();
        LocalDateTime createTimeEnd = searchGoodsCategoryParam.getCreateTimeEnd();
        String keywords = searchGoodsCategoryParam.getKeywords();

        Page<GoodsCategoryEntity> page = this.page(pageParam.toPage(), Wrappers.lambdaQuery(GoodsCategoryEntity.class)
            .like(StrUtil.isNotBlank(categoryName), GoodsCategoryEntity::getCategoryName, categoryName)
            .eq(bizType != null, GoodsCategoryEntity::getBizType, bizType)
            .ge(createTimeStart != null, GoodsCategoryEntity::getCreateTime, createTimeStart)
            .le(createTimeEnd != null, GoodsCategoryEntity::getCreateTime, createTimeEnd)
            .and(StrUtil.isNotBlank(keywords), it -> it.like(GoodsCategoryEntity::getCategoryName, keywords)
                .or().like(GoodsCategoryEntity::getSn, keywords)
            )
        );
        return PageResult.of(page);
    }
}


