package com.cool.store.dao;

import com.cool.store.entity.SplitOrderDO;
import com.cool.store.mapper.SplitOrderMapper;
import com.cool.store.request.SplitOrderPageRequest;
import com.cool.store.response.SplitOrderResponse;
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 SplitOrderDAO {

    @Resource
    private SplitOrderMapper zxjpSplitOrderMapper;

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

    /**
     * 根据分账单号查询
     */
    public SplitOrderDO getBySplitNo(String splitNo) {
        if (StringUtils.isBlank(splitNo)) {
            return null;
        }
        return zxjpSplitOrderMapper.getBySplitNo(splitNo);
    }

    /**
     * 根据状态查询
     */
    public List<SplitOrderDO> listByStatus(String status) {
        return zxjpSplitOrderMapper.listByStatus(status);
    }

    /**
     * 根据关联单据号查询
     */
    public List<SplitOrderDO> listByRelatedDocNo(String relatedDocNo,List<Integer> statusList) {
        if (StringUtils.isBlank(relatedDocNo)||CollectionUtils.isEmpty(statusList)) {
            return new ArrayList<>();
        }
        return zxjpSplitOrderMapper.listByRelatedDocNo(relatedDocNo,statusList);
    }

    /**
     * 根据门店ID查询
     */
    public List<SplitOrderDO> listByStoreId(String storeId) {
        if (StringUtils.isBlank(storeId)) {
            return  new ArrayList<>();
        }
        return zxjpSplitOrderMapper.listByStoreId(storeId);
    }

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

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

    /**
     * 批量更新状态
     */
    public int batchUpdateStatus(List<Long> ids, String status) {
        if (CollectionUtils.isEmpty(ids) || StringUtils.isBlank(status)) {
            return 0;
        }
        return zxjpSplitOrderMapper.batchUpdateStatus(ids, status);
    }

    /**
     * 分页页查询
     */
    public List<SplitOrderDO> pageQuery(SplitOrderPageRequest request) {
        return zxjpSplitOrderMapper.pageQuery(request);
    }

    /**
     * 分页查询（关联门店信息）
     */
    public List<SplitOrderResponse> pageQueryWithStore(SplitOrderPageRequest request) {
        return zxjpSplitOrderMapper.pageQueryWithStore(request);
    }

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