package com.chushang.inspection.terminal.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.inspection.project.vo.CountVO;
import com.chushang.inspection.project.vo.TerminalApp;
import com.chushang.inspection.terminal.po.Store;
import com.chushang.inspection.terminal.po.Terminal;
import com.chushang.inspection.terminal.query.TerminalAppQuery;
import com.chushang.inspection.terminal.query.TerminalQuery;
import com.chushang.inspection.work.dto.DispatchDTO;
import com.chushang.inspection.work.dto.WrkIcbcJsReceive;
import com.chushang.inspection.work.po.WrkIcbcJs;
import com.chushang.inspection.work.query.DispatchQuery;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
  * @auther: zhao
  * @date: 2024/6/21 16:39
  */
public interface TerminalService extends IService<Terminal>{

    /**
     * 终端列表
     */
    PageResult pageList(TerminalQuery query);

    /**
     * 派单记录
     * @param query
     * @return
     */
    PageResult dispatchRecord(TerminalQuery query);

    /**
     * 查询终端app
     */
    PageResult queryPageApp(TerminalAppQuery query);

    /**
     * 终端详情app id
     * @param terminalId
     * @param taskId
     * @return
     */
    TerminalApp getAppById(Long terminalId, Long taskId);

    /**
     * 修改终端占用状态
     * @param terminalIds
     * @param i
     * @return
     */
    void updateOccupy(Collection<Long> terminalIds, int i);

    /**
     * 查询终端商户巡检信息
     * @param query
     * @return
     */
    List<DispatchDTO> getStoreByTasKIdOrIds(DispatchQuery query);

    JSONObject save(Store store, WrkIcbcJsReceive dto);


    long updateTerminalAndStore(TerminalQuery query);



    JSONObject getTerMap(Long terminalId);

    List<CountVO> countByProjectIds(Set<Long> projectIds);

    default Map<Long, Integer> countByProjectIdsMap(Set<Long> projectIds){
        List<CountVO> countVOS = countByProjectIds(projectIds);
        if (CollectionUtil.isNotEmpty(countVOS)){
            return countVOS.stream().collect(Collectors.toMap(
                    CountVO::getProjectId,
                    CountVO::getTotal,
                    Integer::sum
            ));
        }
        return Map.of();
    }

    JSONObject save(Store store, WrkIcbcJs w);
}
