package com.usky.fire.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.fire.domain.JcjAjxx;
import com.usky.fire.mapper.JcjAjxxMapper;
import com.usky.fire.service.JcjAjxxService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZYJ
 * @since 2023-01-17
 */
@DS("mhsystem")
@Service
public class JcjAjxxServiceImpl extends AbstractCrudService<JcjAjxxMapper, JcjAjxx> implements JcjAjxxService {

    @Override
    public CommonPage<JcjAjxx> jcjAjxxList(String streetTown, String startDate, String endDate, String caseType, Integer pageNum, Integer pageSize){
        List<String> streetTownList = new ArrayList<>();
        if(StringUtils.isNotBlank(streetTown)){
            String[] splitList = streetTown.split(",");
            for(int i=0;i<splitList.length;i++){
                streetTownList.add(splitList[i]);
            }
        }
        IPage<JcjAjxx> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<JcjAjxx> queryWrapper = Wrappers.lambdaQuery();
        if(streetTownList.size()>0){
            queryWrapper.in(JcjAjxx::getStreettown, streetTownList);
        }
        queryWrapper.between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate), JcjAjxx::getLasj, startDate, endDate)
                    .eq(StringUtils.isNotBlank(caseType), JcjAjxx::getAjlx, caseType)
                    .eq(JcjAjxx::getYxx,0)
                    .orderByDesc(JcjAjxx::getId);
        page = this.page(page, queryWrapper);
        return new CommonPage<>(page.getRecords(), page.getTotal(), pageNum, pageSize);

    }

    @Override
    public Map<String,Object> jcjAjxxStatistics(String streetTown, String startDate, String endDate){
        Map<String, Object> map = new HashMap<>();
        int fireCount = 0;
        int socialCount = 0;
        int emergencyCount = 0;
        fireCount = this.ajCount(streetTown, startDate, endDate, "火灾");
        map.put("fireCount",fireCount);
        socialCount = this.ajCount(streetTown, startDate, endDate, "社会救助");
        map.put("socialCount",socialCount);
        emergencyCount = this.ajCount(streetTown, startDate, endDate, "抢险救援");
        map.put("emergencyCount",emergencyCount);

        return map;
    }

    @Override
    public Integer ajCount(String streetTown, String startDate, String endDate, String caseType){
        List<String> streetTownList = new ArrayList<>();
        String[] str = streetTown.split(",");
        for(int i=0;i<str.length;i++){
            streetTownList.add(str[i]);
        }
        LambdaQueryWrapper<JcjAjxx> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(JcjAjxx::getStreettown, streetTownList)
                    .between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate), JcjAjxx::getLasj, startDate, endDate)
                    .eq(JcjAjxx::getAjlx, caseType)
                    .eq(JcjAjxx::getYxx, 0);
        int count = this.count(queryWrapper);

        return count;
    }
}
