package com.cool.store.dao.tp;

import com.cool.store.entity.tp.TpScoreJournalDO;
import com.cool.store.mapper.tp.TpScoreJournalMapper;
import com.cool.store.request.tp.TpScoreJournalQueryRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 十二分制-积分流水DAO
 * </p>
 *
 * @author wangff
 * @since 2025/11/6
 */
@Repository
@RequiredArgsConstructor
public class TpScoreJournalDAO {
    private final TpScoreJournalMapper tpScoreJournalMapper;

    /**
     * 新增
     */
    public void insertSelective(TpScoreJournalDO record) {
        tpScoreJournalMapper.insertSelective(record);
    }

    /**
     * 列表查询
     */
    public List<TpScoreJournalDO> getList(TpScoreJournalQueryRequest request) {
        Example example = buildExample(request);
        example.setOrderByClause("create_time DESC");
        return tpScoreJournalMapper.selectByExample(example);
    }

    /**
     * 统计数量
     */
    public Long count(TpScoreJournalQueryRequest request) {
        Example example = buildExample(request);
        return (long) tpScoreJournalMapper.selectCountByExample(example);
    }

    private Example buildExample(TpScoreJournalQueryRequest request) {
        Example example = new Example(TpScoreJournalDO.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(request.getStoreNameOrNum())) {
            criteria.andCondition("store_num LIKE '%" + request.getStoreNameOrNum() + "%' OR store_name LIKE '%" + request.getStoreNameOrNum() + "%'");
        }
        if (Objects.nonNull(request.getJoinBrand())) {
            criteria.andEqualTo("joinBrand", request.getJoinBrand());
        }
        if (Objects.nonNull(request.getJoinModel())) {
            criteria.andEqualTo("joinModel", request.getJoinModel());
        }
        if (Objects.nonNull(request.getStoreType())) {
            criteria.andEqualTo("storeType", request.getStoreType());
        }
        if (Objects.nonNull(request.getStartDate())) {
            criteria.andGreaterThanOrEqualTo("occurDate", request.getStartDate());
        }
        if (Objects.nonNull(request.getEndDate())) {
            criteria.andLessThanOrEqualTo("occurDate", request.getEndDate());
        }
        if (Objects.nonNull(request.getApplyType())) {
            criteria.andEqualTo("applyType", request.getApplyType());
        }
        if (StringUtils.isNotBlank(request.getApplyNo())) {
            criteria.andLike("applyNo", "%" + request.getApplyNo() + "%");
        }
        return example;
    }
}
