package com.cool.store.dao.visit;

import com.alibaba.excel.util.CollectionUtils;
import com.cool.store.entity.visit.VisitRecordDO;
import com.cool.store.enums.visit.VisitStatusEnum;
import com.cool.store.mapper.visit.VisitRecordMapper;
import com.cool.store.request.visit.VisitRecordQueryRequest;
import com.cool.store.vo.visit.VisitPartnerListVO;
import com.cool.store.vo.visit.VisitRecordListVO;
import com.cool.store.vo.visit.VisitRecordStatusStatisticsVO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 拜访记录DAO
 * </p>
 *
 * @author wangff
 * @since 2025/12/8
 */
@Repository
@AllArgsConstructor
public class VisitRecordDAO {
    private final VisitRecordMapper visitRecordMapper;

    public Boolean insertSelective(VisitRecordDO visitRecordDO) {
        return visitRecordMapper.insertSelective(visitRecordDO) > 0;
    }

    public Boolean updateSelective(VisitRecordDO visitRecordDO) {
        return visitRecordMapper.updateByPrimaryKeySelective(visitRecordDO) > 0;
    }

    public VisitRecordDO getById(Long id) {
        return visitRecordMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询拜访加盟商
     * @param regionIds 加盟商所属大区id
     * @param keyword 加盟商姓名或手机号
     * @return 拜访加盟商列表VO
     */
    public List<VisitPartnerListVO> getPartnerList(List<Long> regionIds, String keyword, String userId) {
        return visitRecordMapper.getPartnerList(regionIds, keyword, userId);
    }

    /**
     * 查询拜访加盟商线索id
     * @param regionIds 加盟商所属大区id
     * @param keyword 加盟商姓名或手机号
     * @param userId 拜访用户id
     * @return 线索id列表
     */
    public List<Long> getPartnerLineId(List<Long> regionIds, String keyword, String userId) {
        return visitRecordMapper.getPartnerLineId(regionIds, keyword, userId);
    }

    /**
     * 根据线索id查询拜访记录列表
     * @param lineIds 线索id
     * @return 拜访记录列表VO列表
     */
    public List<VisitPartnerListVO> getVisitRecordListByLineIds(List<Long> lineIds, String userId) {
        if (CollectionUtils.isEmpty(lineIds)) {
            return Collections.emptyList();
        }
        return visitRecordMapper.getVisitRecordListByLineIds(lineIds, userId);
    }

    /**
     * 根据线索查询
     * @param lineId 线索id
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 拜访记录列表
     */
    public List<VisitRecordDO> getByLineId(Long lineId, Date startDate, Date endDate, String userId) {
        Example example = new Example(VisitRecordDO.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("lineId", lineId)
                .andGreaterThanOrEqualTo("visitDate", startDate)
                .andLessThanOrEqualTo("visitDate", endDate);
        if (StringUtils.isNotBlank(userId)) {
            criteria.andEqualTo("userId", userId);
        }
        example.setOrderByClause("create_time DESC");
        return visitRecordMapper.selectByExample(example);
    }

    public Boolean deleteById(Long id) {
        return visitRecordMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 拜访记录列表
     * @param request 拜访记录查询Request
     * @return 拜访记录列表VO列表
     */
    public List<VisitRecordListVO> getVisitRecordList(VisitRecordQueryRequest request) {
        return visitRecordMapper.getVisitRecordList(request);
    }

    /**
     * 拜访记录数量
     * @param request 拜访记录查询Request
     * @return 数量
     */
    public Long getVisitRecordCount(VisitRecordQueryRequest request) {
        return visitRecordMapper.getVisitRecordCount(request);
    }

    /**
     * 根据线索id查询已完成拜访数量
     * @param lineId 线索id
     * @return 数量
     */
    public Integer getCompleteVisitNum(Long lineId) {
        return visitRecordMapper.selectCount(VisitRecordDO.builder().lineId(lineId).status(VisitStatusEnum.COMPLETE.getStatus()).build());
    }

    /**
     * 统计
     */
    public VisitRecordStatusStatisticsVO countStatusStatistics(VisitRecordQueryRequest request) {
        return visitRecordMapper.countStatusStatistics(request);
    }
}
