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.WarehouseCheckPlanMapper;
import com.njzscloud.dispose.wh.pojo.entity.WarehouseCheckPlanEntity;
import com.njzscloud.dispose.wh.pojo.param.AddWarehouseCheckPlanParam;
import com.njzscloud.dispose.wh.pojo.param.SearchWarehouseCheckPlanParam;
import com.njzscloud.dispose.wh.pojo.result.SearchWarehouseCheckPlanResult;
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 WarehouseCheckPlanService extends ServiceImpl<WarehouseCheckPlanMapper, WarehouseCheckPlanEntity> {

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

    /**
     * 修改
     */
    public void modify(WarehouseCheckPlanEntity warehouseCheckPlanEntity) {
        this.updateById(warehouseCheckPlanEntity);
    }

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

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

    /**
     * 分页查询
     */
    public PageResult<SearchWarehouseCheckPlanResult> paging(PageParam pageParam, SearchWarehouseCheckPlanParam searchWarehouseCheckPlanParam) {
        String keywords = searchWarehouseCheckPlanParam.getKeywords();
        String planStatus = searchWarehouseCheckPlanParam.getPlanStatus();
        Long warehouseId = searchWarehouseCheckPlanParam.getWarehouseId();
        return PageResult.of(baseMapper.paging(pageParam.toPage(), Wrappers.query()
            .eq("deleted", 0)
            .and(StrUtil.isNotBlank(keywords), it -> it
                .like("sn", keywords)
                .or().like("memo", keywords)
            )
            .eq(planStatus != null, "plan_status", planStatus)
            .eq(warehouseId != null, "warehouse_id", warehouseId)
        ));
    }

}
