package com.cool.store.dao;

import com.cool.store.entity.AdjustmentOrderDO;
import com.cool.store.mapper.AdjustmentOrderMapper;
import com.cool.store.request.AdjustmentOrderPageRequest;
import com.cool.store.response.AdjustmentOrderResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 费用调整单 DAO
 */
@Repository
public class AdjustmentOrderDAO {

    @Resource
    private AdjustmentOrderMapper AdjustmentOrderMapper;

    /**
     * 根据ID查询
     */
    public AdjustmentOrderDO getById(Long id) {
        if (id == null) {
            return null;
        }
        return AdjustmentOrderMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据调整单号查询
     */
    public AdjustmentOrderDO getByAdjustmentNo(String adjustmentNo) {
        if (StringUtils.isBlank(adjustmentNo)) {
            return null;
        }
        return AdjustmentOrderMapper.getByAdjustmentNo(adjustmentNo);
    }

    /**
     * 根据状态查询
     */
    public List<AdjustmentOrderDO> listByStatus(Integer status) {
        return AdjustmentOrderMapper.listByStatus(status);
    }

    /**
     * 根据关联分账单号查询
     */
    public List<AdjustmentOrderDO> listByRelatedSplitNo(String relatedSplitNo) {
        if (StringUtils.isBlank(relatedSplitNo)) {
            return  new ArrayList<>();
        }
        return AdjustmentOrderMapper.listByRelatedSplitNo(relatedSplitNo);
    }

    /**
     * 根据门店ID查询

     */
    public List<AdjustmentOrderDO> listByStoreId(String storeId) {
        if (StringUtils.isBlank(storeId)) {
            return  new ArrayList<>();
        }
        return AdjustmentOrderMapper.listByStoreId(storeId);
    }

    /**
     * 新增
     */
    public int insert(AdjustmentOrderDO record) {
        if (record == null) {
            return 0;
        }
        return AdjustmentOrderMapper.insertSelective(record);
    }

    /**
     * 更新
     */
    public int update(AdjustmentOrderDO record) {
        if (record == null || record.getId() == null) {
            return 0;
        }
        return AdjustmentOrderMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 批量更新状态
     */
    public int batchUpdateStatus(List<Long> ids, Integer status) {
        if (CollectionUtils.isEmpty(ids) || status == null) {
            return 0;
        }
        return AdjustmentOrderMapper.batchUpdateStatus(ids, status);
    }

    /**
     * 根据ID更新关联分账单号
     */
    public int updateRelatedSplitNo(Long id, String relatedSplitNo) {
        if (id == null || StringUtils.isBlank(relatedSplitNo)) {
            return 0;
        }
        return AdjustmentOrderMapper.updateRelatedSplitNo(id, relatedSplitNo);
    }

    /**
     * 分页查询
     */
    public List<AdjustmentOrderResponse> pageQuery(AdjustmentOrderPageRequest request) {
        return AdjustmentOrderMapper.pageQuery(request);
    }

    /**
     * 删除
     */
    public int delete(Long id) {
        if (id == null) {
            return 0;
        }
        return AdjustmentOrderMapper.deleteByPrimaryKey(id);
    }
}
