package com.tofly.watermeter.custom.service.meterBusiness.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.watermeter.custom.entity.meterBusiness.dto.TableQueryDto;
import com.tofly.watermeter.custom.mapper.meterBusiness.TableQueryMapper;
import com.tofly.watermeter.custom.service.meterBusiness.TableQueryService;
import com.tofly.watermeter.custom.service.meterBusiness.export.ExportCommonCodeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class TableQueryServiceImpl implements TableQueryService {

    private final TableQueryMapper tableQueryMapper;
    private final ExportCommonCodeService exportCommonCodeService;

    @Override
    public List<Map> countDay(HttpServletResponse response,TableQueryDto dto) {
        List<Map> maps = tableQueryMapper.countDay(dto);
        if (dto.getExport() == 1){
            exportCommonCodeService.exportCode(response,"tableQueryCountService", maps);
        }
        return maps;
    }

    @Override
    public Page<Map> queryInfo(HttpServletResponse response,Page page, Long vendor,Long type,Long val,Long export) {
        if(export != null && export == 1){
            exportCommonCodeService.exportCode(response,"tableQueryInfoService", tableQueryMapper.queryInfo(vendor,type,val));
        }
        return tableQueryMapper.queryInfo(page, vendor,type,val);
    }


    /*
     * 水表生命周期查询
     *
     * @param code  水表编号
     * @return map
     */
//    @Override
//    public Map<String, List<Map>> lifeCycle(String code) {
//
//        Map<String, List<Map>> map = new HashMap<>(7);
//
//        if (StringUtils.isEmpty(code)) {
//            throw new ToflyDeniedException("水表编号为空");
//        }
//
//        //入库
//        List<Map> inStorage = tableQueryMapper.putInStorage(code,"1");
//        if (inStorage.isEmpty()) {
//            map.put("0", null);
//        } else {
//            map.put("0", inStorage);
//        }
//        //首检
//        Bwrksbdatail tail = bwrksbdatailService.getOne(Wrappers.<Bwrksbdatail>lambdaQuery()
//                .eq(Bwrksbdatail::getBwSbbh, code).select(Bwrksbdatail::getBwBz));
//        if (tail != null) {
//            Bwrkdetail rktail = bwrkdetailService.getById(tail.getBwBz());
//            List<Map> result = tableQueryMapper.firstInspection(rktail.getBwLsbh());
//            if (result.isEmpty()) {
//                map.put("1", null);
//            } else {
//                map.put("1", result);
//            }
//        } else {
//            map.put("1", null);
//        }
//
//        //出库
//        Bwrksbdatail sbrktail = bwrksbdatailService.getOne(Wrappers.<Bwrksbdatail>lambdaQuery()
//                .eq(Bwrksbdatail::getBwSbbh, code));
//        List<Bwckdatail> list = bwckdatailService.list();
//        if (list.isEmpty()){
//            map.put("2", null);
//        }else {
//            List<Long> collect = list.stream()
//                    .filter(b -> Arrays.asList(b.getBwRkid().split(",")).contains(sbrktail.getId().toString())).map(Bwckdatail::getId)
//                    .collect(Collectors.toList());
//            if (collect.isEmpty()){
//                map.put("2", null);
//            }else {
//                map.put("2",tableQueryMapper.outbound(collect));
//            }
//        }
//
//        //退表
//
//        List<Map> withdrawing = tableQueryMapper.putInStorage(code,"8");
//        if (withdrawing.isEmpty()) {
//            map.put("3", null);
//        } else {
//            map.put("3", withdrawing);
//        }
//
//        //安装
//        List<Map> installTable = tableQueryMapper.putInStorage(code,"3");
//        if (installTable.isEmpty()) {
//            map.put("4", null);
//        } else {
//            map.put("4", installTable);
//        }
//
//        //换表
//        List<Map> changeTable = tableQueryMapper.changeTable(code);
//        if (changeTable.isEmpty()) {
//            map.put("5", null);
//        } else {
//            map.put("5", changeTable);
//        }
//
//        List<Map> turnOver = tableQueryMapper.turnOver(code);
//        if (turnOver.isEmpty()) {
//            map.put("6", null);
//        } else {
//            map.put("6", turnOver);
//        }
//
//        return map;
//    }
}
