package com.njzscloud.dispose.wh.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.dispose.wh.mapper.WarehouseCheckDetailMapper;
import com.njzscloud.dispose.wh.pojo.entity.WarehouseCheckDetailEntity;
import com.njzscloud.dispose.wh.pojo.param.AddWarehouseCheckDetailParam;
import com.njzscloud.dispose.wh.pojo.param.SearchWarehouseCheckDetailParam;
import com.njzscloud.dispose.wh.pojo.result.SearchWarehouseCheckDetailResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 库存盘点明细
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WarehouseCheckDetailService extends ServiceImpl<WarehouseCheckDetailMapper, WarehouseCheckDetailEntity> {

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(AddWarehouseCheckDetailParam addWarehouseCheckDetailParam) {
        WarehouseCheckDetailEntity entity = BeanUtil
            .copyProperties(addWarehouseCheckDetailParam, WarehouseCheckDetailEntity.class);
        this.save(entity);
    }

    /**
     * 修改
     */
    public void modify(WarehouseCheckDetailEntity warehouseCheckDetailEntity) {
        this.updateById(warehouseCheckDetailEntity);
    }

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

    /**
     * 详情
     */
    public SearchWarehouseCheckDetailResult detail(Long id) {
        return baseMapper.detail(id);
    }

    /**
     * 分页查询
     */
    public PageResult<SearchWarehouseCheckDetailResult> paging(PageParam pageParam, SearchWarehouseCheckDetailParam searchWarehouseCheckDetailParam) {
        String keywords = searchWarehouseCheckDetailParam.getKeywords();
        Long checkPlanId = searchWarehouseCheckDetailParam.getCheckPlanId();
        return PageResult.of(baseMapper.paging(pageParam.toPage(), Wrappers.query()
            .eq("deleted", 0)
            .and(StrUtil.isNotBlank(keywords), it -> it
                .like("goods_name", keywords)
                .or().like("good_sn", keywords)
                .or().like("goods_category_name", keywords)
            )
            .eq(checkPlanId != null, "check_plan_id", checkPlanId)
        ));
    }

}
