package com.nmgs.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nmgs.entity.*;
import com.nmgs.mapper.*;
import com.nmgs.service.IPayIntegrationNumberService;
import com.nmgs.util.PropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;


@Service
@Transactional
@Slf4j
public class PayIntegrationNumberServiceImpl implements IPayIntegrationNumberService {


    @Autowired
    private PayIntegrationNumberMapper payIntegrationNumberMapper;

    @Autowired
    private IntegrationListMapper integrationListMapper;



    @Override
    public List<PayIntegrationNumber> getPayIntegrationNumberList() {

        List<PayIntegrationNumber> payList = payIntegrationNumberMapper.getPayIntegrationNumberList();
        return payList;
    }


    @Override
    public int savePayIntegrationNumber(List<PayIntegrationNumber> payIntegrationNumbers) {

        int res = 0;
        //如果数据为空，直接返回
        if(null == payIntegrationNumbers || payIntegrationNumbers.isEmpty()){
            return 0;
        }
        for (PayIntegrationNumber payIntegrationNumber : payIntegrationNumbers) {
            payIntegrationNumber.setValue(payIntegrationNumber.getValue().multiply(new BigDecimal(100)));
            //判断传回的值中，id是否有值，没有就是新增
            if(null == payIntegrationNumber.getId()){
                //查询当前数据中id的最大值,给id赋值
                Integer maxId = payIntegrationNumberMapper.getMaxId();
                //如果为空则赋值成1
                if(null == maxId){
                    maxId = 1;
                }else{
                    maxId =  maxId+1;
                }
                //给密码加密
                payIntegrationNumber.setId(maxId);
                res = payIntegrationNumberMapper.insert(payIntegrationNumber);
            }else{
                //设置修改条件，根据id修改
                LambdaQueryWrapper<PayIntegrationNumber> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(null != payIntegrationNumber.getId(),
                        PayIntegrationNumber::getId,
                        payIntegrationNumber.getId());

                 res = payIntegrationNumberMapper.update(payIntegrationNumber,lambdaQueryWrapper);
            }
        }
        return res;
    }



    @Override
    public JSONArray getIntegrationGrowTotal(Map<String, Object> params) {
        JSONArray jsonArray = new JSONArray();

        //从配置文件中读取数据库连接类型
        params.put("DBTYPE", PropertiesUtil.getValue("DBType"));

        //判断是日统计还是月统计
        Boolean greenTotalCollectionType = (Boolean) params.get("greenTotalCollectionType");

        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String, String>> collectionDate = new ArrayList<>();
        Date currentDate = null;
        Date endDate= null;
        try {
            currentDate= format.parse((String) params.get("classDateBeginQuery"));;//DateUtility.parse(startTime)
            endDate= format.parse((String) params.get("classDateEndQuery")); //endTime.parse(endTime)
/*            List<DateSplitUtils.DateSplit> dateSplits = new ArrayList<>();

            dateSplits = DateSplitUtils.splitDate(currentDate, endDate, DateSplitUtils.IntervalType.DAY,1);

            for (DateSplitUtils.DateSplit dateSplit : dateSplits) {
                Map<String, String> date = new HashMap<>();
                date.put("startTime", dateSplit.getStartDateTimeStr());
                date.put("endTime", dateSplit.getEndDateTimeStr());
                collectionDate.add(date);
            }*/
            //设置数据总条数
            for (int i = 0; i < collectionDate.size(); i++) {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDate date = LocalDate.parse(collectionDate.get(i).get("startTime").substring(0,10), formatter);
                int dayOfMonth = date.getDayOfMonth();
                JSONObject dateTime = new JSONObject();
                dateTime.put("dateName", dayOfMonth + "号");
                dateTime.put("dateCount", 0);
                jsonArray.add(dateTime);
            }
            for (int i = 0; i < collectionDate.size(); i++) {
                Map<String, Object> param = new HashMap<>();
                //从配置文件中读取数据库连接类型
                param.put("DBTYPE", PropertiesUtil.getValue("DBType"));
                //构建参数
                param.put("startTime", collectionDate.get(i).get("startTime"));
                param.put("endTime", collectionDate.get(i).get("endTime"));
                //调用接口，取值
                List<Map<String, Object>> userTotal = integrationListMapper.getIntegrationGrowTotal(param);
                if (null != userTotal && !userTotal.isEmpty() && null != userTotal.get(0)) {
                    //给对应位置的时间段赋值
                    JSONObject jSONObject = (JSONObject) jsonArray.get(i);
                    jSONObject.put("dateCount", userTotal.get(0).get("TOTALCOUNT"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询数据失败" + e);
        }
        return jsonArray;
    }



    @Override
    public List<Map<String, Object>> getCarIntegrationGrowTotal(Map<String, Object> params) {
        List<Map<String,Object>> res = new ArrayList<>();

        //从配置文件中读取数据库连接类型
        params.put("DBTYPE", PropertiesUtil.getValue("DBType"));
        //判断是日统计还是月统计
        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            format.parse((String) params.get("classDateBeginQuery"));;//DateUtility.parse(startTime)
            format.parse((String) params.get("classDateEndQuery")); //endTime.parse(endTime)
            res = integrationListMapper.getCarIntegrationGrowTotal(params);
            return res;
        } catch (Exception e) {
            log.info("查询数据失败" + e);
            return res;
        }
    }

    @Override
    public Map<String, Object> getMonthFeeTotal(Map<String, Object> params) {
        Map<String,Object> res = new HashMap<>();

        //从配置文件中读取数据库连接类型
        params.put("DBTYPE", PropertiesUtil.getValue("DBType"));
        //判断是日统计还是月统计
        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            format.parse((String) params.get("classDateBeginQuery"));;//DateUtility.parse(startTime)
            format.parse((String) params.get("classDateEndQuery")); //endTime.parse(endTime)
            res = integrationListMapper.getMonthFeeTotal(params);
            return res;
        } catch (Exception e) {
            log.info("查询数据失败" + e);
            return res;
        }
    }

    @Override
    public List<Map<String, Object>> getFeeTypeTotal(Map<String, Object> params) {
        List<Map<String,Object>> res = new ArrayList<>();

        //从配置文件中读取数据库连接类型
        params.put("DBTYPE", PropertiesUtil.getValue("DBType"));
        //判断是日统计还是月统计
        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            format.parse((String) params.get("classDateBeginQuery"));;//DateUtility.parse(startTime)
            format.parse((String) params.get("classDateEndQuery")); //endTime.parse(endTime)
            res = integrationListMapper.getFeeTypeTotal(params);
            return res;
        } catch (Exception e) {
            log.info("查询数据失败" + e);
            return res;
        }
    }


    @Override
    public List<Map<String, Object>> getMileScoreType(Map<String, Object> params) {
        List<Map<String,Object>> res = new ArrayList<>();

        //从配置文件中读取数据库连接类型
        params.put("DBTYPE", PropertiesUtil.getValue("DBType"));
        //判断是日统计还是月统计
        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            format.parse((String) params.get("classDateBeginQuery"));;//DateUtility.parse(startTime)
            format.parse((String) params.get("classDateEndQuery")); //endTime.parse(endTime)
            //res = vehicleScoreMapper.getMileScoreType(params);
            return res;
        } catch (Exception e) {
            log.info("查询数据失败" + e);
            return res;
        }
    }
}
