package com.mosty.yjzl.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mosty.base.model.entity.qwzx.TbQwxfbbDk;
import com.mosty.base.model.entity.yjzl.TbZlxx;
import com.mosty.base.model.vo.yjzl.*;
import com.mosty.base.utils.DateUtils;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.yjzl.mapper.TbYjzltjMapper;
import com.mosty.yjzl.mapper.TbZlxxMapper;
import com.mosty.yjzl.remote.TbBaseAdaptRemoteService;
import com.mosty.yjzl.service.TbYjxxSjService;
import com.mosty.yjzl.service.TbYjzltjService;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class TbYjzltjServiceImpl implements TbYjzltjService {

    private TbYjzltjMapper tbYjzltjMapper;

    private TbZlxxMapper tbZlxxMapper;

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public List<TbYjzltjxxVo> rwzttjxx() {
        UserInfo user = UserInfoManager.get();
        Map<String, Object> map = new HashMap<>();
        map.put("sfz", user.getIdEntityCard());
        map.put("ssbmid", String.valueOf(user.getDeptId()));
        return tbYjzltjMapper.rwzttjxx(map);
    }

    @Override
    public List<TbYjzltjphVo> rwphtjxx(String type) {
        String kssj = "", jssj = "";
        if ("1".equals(type)) {
            kssj = com.mosty.base.utils.DateUtils.getQueryDateString(new Date(), "01");
            jssj = com.mosty.base.utils.DateUtils.getQueryDateString(new Date(), "01");
        } else if ("2".equals(type)) {
            kssj = com.mosty.base.utils.DateUtils.getQueryDateString(com.mosty.base.utils.DateUtils.getNextDate(new Date(), "D", -7), "01");
            jssj = com.mosty.base.utils.DateUtils.getQueryDateString(new Date(), "01");
        } else if ("3".equals(type)) {
            kssj = com.mosty.base.utils.DateUtils.getQueryDateString(com.mosty.base.utils.DateUtils.getNextDate(new Date(), "D", -30), "01");
            jssj = com.mosty.base.utils.DateUtils.getQueryDateString(new Date(), "01");
        } else if ("4".equals(type)) {
            kssj = com.mosty.base.utils.DateUtils.getQueryDateString(com.mosty.base.utils.DateUtils.getNextDate(new Date(), "D", -90), "01");
            jssj = com.mosty.base.utils.DateUtils.getQueryDateString(new Date(), "01");
        }
        return tbYjzltjMapper.rwphtjxx(kssj, jssj);
    }

    @Override
    public Map<String, Object> zlZqtjxx(String type) {
        Map<String, Object> map = new HashMap<>();
        List<String> timeList = new ArrayList<>();
        List<Map<String, Object>> resList;
        if ("1".equals(type)) {
            String t = DateUtils.getQueryDateString(new Date(), "01") + " ";
            String m = ":00:00";
            String s = ":59:59";
            timeList.add(t + "23" + s);
            timeList.add(t + "20" + m);
            timeList.add(t + "16" + m);
            timeList.add(t + "12" + m);
            timeList.add(t + "08" + m);
            timeList.add(t + "04" + m);
            timeList.add(t + "00" + m);
        } else if ("2".equals(type)) {
            for (int i = 0; i < 7; i++) {
                timeList.add(DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -i), "01"));
            }
        } else if ("3".equals(type)) {
            for (int i = 0; i < 7; i++) {
                timeList.add(DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -i * 6), "01"));
            }
        } else if ("4".equals(type)) {
            for (int i = 0; i < 7; i++) {
                timeList.add(DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -i * 9), "01"));
            }
        }
        Integer[] data1 = new Integer[]{0, 0, 0, 0, 0, 0, 0};
        Integer[] data2 = new Integer[]{0, 0, 0, 0, 0, 0, 0};
        Integer[] data3 = new Integer[]{0, 0, 0, 0, 0, 0, 0};
        Integer[] data4 = new Integer[]{0, 0, 0, 0, 0, 0, 0};
        Integer[] data5 = new Integer[]{0, 0, 0, 0, 0, 0, 0};
        Integer[] data6 = new Integer[]{0, 0, 0, 0, 0, 0, 0};
        Integer[] data7 = new Integer[]{0, 0, 0, 0, 0, 0, 0};
        Integer[] data8 = new Integer[]{0, 0, 0, 0, 0, 0, 0};
        Collections.reverse(timeList);
        for (int i = 0; i < timeList.size() - 1; i++) {
            Map<String, Object> mp = new HashMap();
            if ("1".equals(type)) {
                mp.put("kssj", timeList.get(i));
                mp.put("jssj", timeList.get(i + 1));
            } else {
                mp.put("start", timeList.get(i));
                mp.put("end", timeList.get(i + 1));
            }
            resList = this.tbYjzltjMapper.zlZqtjxx(mp);
            for (int j = 0; j < resList.size(); j++) {
                Map<String, Object> item = resList.get(j);
                String zllx = item.get("zllx").toString();
                if ("01".equals(zllx)) {
                    data1[j] = Integer.parseInt(item.get("count") + "");
                } else if ("02".equals(zllx)) {
                    data2[j] = Integer.parseInt(item.get("count") + "");
                } else if ("03".equals(zllx)) {
                    data3[j] = Integer.parseInt(item.get("count") + "");
                } else if ("04".equals(zllx)) {
                    data4[j] = Integer.parseInt(item.get("count") + "");
                } else if ("05".equals(zllx)) {
                    data5[j] = Integer.parseInt(item.get("count") + "");
                } else if ("06".equals(zllx)) {
                    data6[j] = Integer.parseInt(item.get("count") + "");
                } else if ("07".equals(zllx)) {
                    data7[j] = Integer.parseInt(item.get("count") + "");
                } else if ("08".equals(zllx)) {
                    data8[j] = Integer.parseInt(item.get("count") + "");
                }
            }
        }
        timeList = timeList.stream().map(item -> {
            if ("1".equals(type)) {
                return DateUtils.getQueryDateString(DateUtils.strToDate(item, "02"), "16");
            } else {
                return DateUtils.getQueryDateString(DateUtils.strToDate(item, "01"), "18");
            }
        }).collect(Collectors.toList());
        map.put("time", timeList);
        map.put("data1", data1);
        map.put("data2", data2);
        map.put("data3", data3);
        map.put("data4", data4);
        map.put("data5", data5);
        map.put("data6", data6);
        map.put("data7", data7);
        map.put("data8", data8);
        return map;
    }

    @Override
    public List<TbYzzlDjtjVo> zlDjtjxx(String type) {
        String kssj = "", jssj = "";
        if ("1".equals(type)) {
            kssj = com.mosty.base.utils.DateUtils.getQueryDateString(new Date(), "01");
            jssj = com.mosty.base.utils.DateUtils.getQueryDateString(new Date(), "01");
        } else if ("2".equals(type)) {
            kssj = com.mosty.base.utils.DateUtils.getQueryDateString(com.mosty.base.utils.DateUtils.getNextDate(new Date(), "D", -7), "01");
            jssj = com.mosty.base.utils.DateUtils.getQueryDateString(new Date(), "01");
        } else if ("3".equals(type)) {
            kssj = com.mosty.base.utils.DateUtils.getQueryDateString(com.mosty.base.utils.DateUtils.getNextDate(new Date(), "D", -30), "01");
            jssj = com.mosty.base.utils.DateUtils.getQueryDateString(new Date(), "01");
        } else if ("4".equals(type)) {
            kssj = com.mosty.base.utils.DateUtils.getQueryDateString(com.mosty.base.utils.DateUtils.getNextDate(new Date(), "D", -90), "01");
            jssj = com.mosty.base.utils.DateUtils.getQueryDateString(new Date(), "01");
        }
        Map<String, Object> map = new HashMap();
        map.put("kssj", kssj);
        map.put("jssj", jssj);
        return tbYjzltjMapper.zlDjtjxx(map);
    }

    @Override
    public TbZlxxRgzlCountVO selectRgzlCount(String ssbmdm, String isChild) {
        String ssbm = this.tbBaseAdaptRemoteService.getSsbm(ssbmdm, isChild);
        QueryWrapper<TbZlxx> rgzlQw = new QueryWrapper<>();
        rgzlQw.likeRight("ssbmdm", ssbm);
        rgzlQw.eq("zlly", "06");
        rgzlQw.ge("xt_cjsj", DateUtils.strToDate(DateUtils.getNowDate() + " 00:00:00", "02"));
        rgzlQw.le("xt_cjsj", DateUtils.strToDate(DateUtils.getNowDate() + " 23:59:59", "02"));
        Integer rgzl = this.tbZlxxMapper.selectCount(rgzlQw);

        QueryWrapper<TbZlxx> sjqzQw = new QueryWrapper<>();
        sjqzQw.likeRight("ssbmdm", ssbm);
        sjqzQw.eq("zlly", "10");
        sjqzQw.ge("xt_cjsj", DateUtils.strToDate(DateUtils.getNowDate() + " 00:00:00", "02"));
        sjqzQw.le("xt_cjsj", DateUtils.strToDate(DateUtils.getNowDate() + " 23:59:59", "02"));
        Integer sjqz = this.tbZlxxMapper.selectCount(sjqzQw);

        TbZlxxRgzlCountVO resultVO = new TbZlxxRgzlCountVO();
        resultVO.setRgzlCount(rgzl == null ? 0 : rgzl);
        resultVO.setSjqzCount(sjqz == null ? 0 : sjqz);
        return resultVO;
    }
}
