package com.boman.web.core.service.accounting;
/**
 * @author tjf
 * @Date: 2022/05/13/11:14
 */

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.boman.common.core.exception.BaseException;
import com.boman.common.core.utils.sql.SqlUtil;
import com.boman.common.core.web.page.PageDomain;
import com.boman.common.core.web.page.TableSupport;
import com.boman.common.datasource.annotation.Master;
import com.boman.common.datasource.annotation.Slave;
import com.boman.domain.*;
import com.alibaba.fastjson.JSON;
import com.boman.common.core.utils.DateUtils;
import com.boman.common.core.utils.StringUtils;
import com.boman.domain.Czrk;
import com.boman.domain.SysUser;
import com.boman.domain.dto.AjaxResult;
import com.boman.system.api.RemoteDeptService;
import com.boman.web.core.domain.AccountingData;
import com.boman.web.core.domain.TSampling514;
import com.boman.web.core.domain.ToQianshanPersonner;
import com.boman.web.core.domain.vo.AccountingDataVo;
import com.boman.web.core.mapper.AccountingDataMapper;
import com.boman.web.core.mapper.CzrkMapper;
import com.boman.web.core.mapper.TSampling514Mapper;
import com.boman.web.core.service.TaskService;
import com.boman.web.core.utils.*;
import com.boman.web.core.utils.AuthUtils;
import com.boman.web.core.utils.CzrkUtils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

import static com.boman.common.core.utils.obj.ObjectUtils.map;
import static com.boman.web.core.utils.CzrkUtils.setQueryRole;

import static com.boman.web.core.utils.CzrkUtils.setAgeScope;
import static com.boman.web.core.utils.CzrkUtils.setQueryRole;

/**
 * @author tjf
 * @Date: 2022/05/13/11:14
 */
@Service
@Slf4j
public class AccountingDataServiceImpl implements IAccountingDataService {

    @Resource
    private AccountingDataMapper accountingDataMapper;
    @Resource
    private RemoteDeptService remoteDeptService;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    @Resource
    private CzrkMapper czrkMapper;

    @Resource
    private TSampling514Mapper TSampling514Mapper;

    @Resource
    TaskService taskService;

    /**
     * 潜山核酸数据列表页(户籍地址)
     *
     * @param accountingData
     * @return
     */
    @Override
    public List<AccountingDataVo> nucleicAcidHJList(AccountingDataVo accountingData) {
        SysUser sysUser = AuthUtils.getLoginUser().getSysUser();
        log.info("{}于{}查询核酸户籍人口列表，查询条件：{}"
                , sysUser.getUserName(), DateUtils.dateTimeNow(), JSON.toJSONString(accountingData));
        //获取时段
        String timeSlot = accountingData.getTimeSlot();
        if (StringUtils.isNotBlank(timeSlot)) {
            //开始时间
            String startTime = DateUtils.minusDayOfDate(Integer.parseInt(timeSlot));
            Date endTime = DateUtils.getNowDate();
            Map<String, Object> map = new HashMap<>();
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            accountingData.setParams(map);
        }

        //设置查询列表权限
        setQueryRole(accountingData, sysUser, Czrk.HJ);
        startPage();
        List<AccountingDataVo> list = accountingDataMapper.selectAccountingDataHjList(accountingData);
        CzrkUtils.packAddrNucleicAcid(list);
        return list;
    }

    /**
     * 潜山核酸数据列表页(居住地址)
     *
     * @param accountingData
     * @return
     */
    @Override
    public List<AccountingDataVo> nucleicAcidJzdzList(AccountingDataVo accountingData) {
        SysUser sysUser = AuthUtils.getLoginUser().getSysUser();
        log.info("{}于{}查询核酸居住人口列表，查询条件：{}"
                , sysUser.getUserName(), DateUtils.dateTimeNow(), JSON.toJSONString(accountingData));
        //获取时段
        String timeSlot = accountingData.getTimeSlot();
        if (StringUtils.isNotBlank(timeSlot)) {
            //开始时间
            String startTime = DateUtils.minusDayOfDate(Integer.parseInt(timeSlot));
            Date endTime = DateUtils.getNowDate();
            Map<String, Object> map = new HashMap<>();
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            accountingData.setParams(map);
        }
        //设置查询列表权限
        setQueryRole(accountingData, sysUser, Czrk.CZ);
        startPage();
        List<AccountingDataVo> list = accountingDataMapper.selectAccountingDataJzdzList(accountingData);
        CzrkUtils.packAddrNucleicAcid(list);
        return list;
    }

    private void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }

    /**
     * 核酸数据导出(户籍)
     *
     * @param accountingData
     * @throws IOException
     */
    @Override
    public List<AccountingDataVo> getExportHj(AccountingDataVo accountingData) {
        SysUser sysUser = AuthUtils.getLoginUser().getSysUser();
        //设置查询列表权限
        setQueryRole(accountingData, sysUser, Czrk.HJ);
        List<AccountingDataVo> accountingDataList = accountingDataMapper.getExportHj(accountingData);

        CzrkUtils.packAddrNucleicAcid(accountingDataList);
        if (accountingDataList == null || accountingDataList.size() <= 0) {
            throw new BaseException("请下载属于本乡镇数据");
        }
        return accountingDataList;
    }

    /***
     * 核酸数据导出(常住)
     * @param accountingData
     * @return
     */
    @Override
    public List<AccountingDataVo> exportCz(AccountingDataVo accountingData) {
        SysUser sysUser = AuthUtils.getLoginUser().getSysUser();
        //设置查询列表权限
        setQueryRole(accountingData, sysUser, Czrk.CZ);
        List<AccountingDataVo> accountingDataList = accountingDataMapper.exportCz(accountingData);

        CzrkUtils.packAddrNucleicAcid(accountingDataList);
        if (accountingDataList == null || accountingDataList.size() <= 0) {
            throw new BaseException("请下载属于本乡镇数据");
        }
        return accountingDataList;
    }


    @Override
    public AccountingDataVo getAccountingDataOne() {
        return accountingDataMapper.getAccountingDataOne();
    }

    @Override
    public List<AccountingDataVo> getAllData() {
        return accountingDataMapper.getAllData();
    }

    @Override
    public List<TSampling514> getTSampling514AllData(String startTime, String endTime) {
        return TSampling514Mapper.getTSampling514AllData(startTime, endTime);
    }

    @Override
    public List<TSampling514> getTSampling514AllDataNoTime() {
        return TSampling514Mapper.getTSampling514AllDataNoTime();
    }

    /**
     * 从第三方库获取核酸数据
     */
    @Slave
    @Override
    public void thirdNucleicAcid(String startTime,String endTime) {
        System.out.println("batchInsert 查询数据开始========");
        long start1 = System.currentTimeMillis();
        List<AccountingDataVo> accountingDataVos = accountingDataMapper.thirdNucleicAcid(startTime,endTime);
        if(accountingDataVos!=null){
            taskService.insertAccountingData(accountingDataVos);
        }
        long end1 = System.currentTimeMillis();
        System.out.println("---------------查询数据耗时" + (end1 - start1) + "---------------");
    }
}
