package com.mosty.wzzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mosty.base.model.dto.wzzx.WzcxQuery;
import com.mosty.base.model.dto.wzzx.WztjQuery;
import com.mosty.base.model.entity.qwzx.TbQwXfBxfsq;
import com.mosty.base.model.entity.wzzx.TbWzRywz;
import com.mosty.base.model.entity.wzzx.TbWzSblswz;
import com.mosty.base.model.entity.wzzx.TbWzXfwz;
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.wzzx.mapper.TbWzRywzMapper;
import com.mosty.wzzx.mapper.TbWzSblswzMapper;
import com.mosty.wzzx.mapper.TbWzXfwzMapper;
import com.mosty.wzzx.mapper.WztjMapper;
import com.mosty.wzzx.remote.TbBaseAdaptRemoteService;
import com.mosty.wzzx.service.WztjService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@AllArgsConstructor
public class WztjServiceImpl implements WztjService {

    private final WztjMapper wztjMapper;
    private final TbWzXfwzMapper tbWzXfwzMapper;
    private final TbWzRywzMapper tbWzRywzMapper;
    private final TbWzSblswzMapper tbWzSblswzMapper;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public List<Map<String, Object>> rywz(WztjQuery dto) {
        Map<String, Object> map = new HashMap<>();
        UserInfo user = UserInfoManager.get();
        map.put("useSql", PermissionsUtil.createSql("", user));
        map.put("dto", dto);
        return this.wztjMapper.jwzbmlbtj(map);
    }

    @Override
    public List<Map<String, Object>> xz(WztjQuery dto) {
        Map<String, Object> map = new HashMap<>();
        UserInfo user = UserInfoManager.get();
        map.put("useSql", PermissionsUtil.createSql("", user));
        map.put("dto", dto);
        return this.wztjMapper.xz(map);
    }

    @Override
    public List<Map<String, Object>> jrlcs(WztjQuery dto) {
        Map<String, Object> map = new HashMap<>();
        UserInfo user = UserInfoManager.get();
        map.put("useSql", PermissionsUtil.createSql("", user));
        map.put("dto", dto);
        return this.wztjMapper.jrlcs(map);
    }

    @Override
    public List<Map<String, Object>> rslcxz(WztjQuery dto) {
        Map<String, Object> map = new HashMap<>();
        UserInfo user = UserInfoManager.get();
        map.put("useSql", PermissionsUtil.createSql("", user));
        map.put("dto", dto);
        return this.wztjMapper.rslcxz(map);
    }

    @Override
    public List<Map<String, Object>> jrwzrs(WztjQuery dto) {
        Map<String, Object> map = new HashMap<>();
        UserInfo user = UserInfoManager.get();
        map.put("useSql", PermissionsUtil.createSql("", user));
        map.put("dto", dto);
        return this.wztjMapper.jrwzrs(map);
    }

    @Override
    public List<Map<String, Object>> jrxzwzs(WztjQuery dto) {
        Map<String, Object> map = new HashMap<>();
        UserInfo user = UserInfoManager.get();
        map.put("useSql", PermissionsUtil.createSql("", user));
        map.put("dto", dto);
        return this.wztjMapper.jrxzwzs(map);
    }

    @Override
    public IPage<Map<String, Object>> rywzlb(WztjQuery dto) {
        Map<String, Object> map = new HashMap<>();
        UserInfo user = UserInfoManager.get();
        map.put("pageIndex", (dto.getPageCurrent() - 1) * dto.getPageSize());
        map.put("pageSize", dto.getPageSize());
        map.put("useSql", PermissionsUtil.createSql("", user));
        map.put("dto", dto);
        int count = this.wztjMapper.rywzlbCount(map);
        List<Map<String, Object>> list = this.wztjMapper.rywzlb(map);
        IPage<Map<String, Object>> page = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<Map<String, Object>> bbgjcx(WzcxQuery dto) {
        Map<String, Object> map = new HashMap<>();
        UserInfo user = UserInfoManager.get();
        map.put("useSql", PermissionsUtil.createSql("", user));
        map.put("dto", dto);
        return this.wztjMapper.bbgjcx(map);
    }

    @Override
    public List<Map<String, Object>> bbwzcx(WzcxQuery dto) {
        Map<String, Object> map = new HashMap<>();
        UserInfo user = UserInfoManager.get();
        map.put("useSql", PermissionsUtil.createSql("", user));
        map.put("dto", dto);
        return this.wztjMapper.bbwzcx(map);
    }

    @Override
    public List<Map<String, Object>> mjgjcx(WzcxQuery dto) {
        Map<String, Object> map = new HashMap<>();
        UserInfo user = UserInfoManager.get();
        map.put("useSql", PermissionsUtil.createSql("", user));
        map.put("dto", dto);
        return this.wztjMapper.mjgjcx(map);
    }

    @Override
    public List<Map<String, Object>> wbbwz(WzcxQuery dto) {
        Map<String, Object> map = new HashMap<>();
        UserInfo user = UserInfoManager.get();
        map.put("useSql", PermissionsUtil.createSql("", user));
        map.put("dto", dto);
        return this.wztjMapper.wbbwz(map);
    }


    @Override
    public Map<String, Object> xfsltj(String type) {
        String kssj = null, jssj = null, startTime = null, endTime = null;
        if ("1".equals(type)) { // 类型如果为 1 说明是当天
            kssj = DateUtils.getQueryDateString(new Date(), "01");
            jssj = DateUtils.getQueryDateString(new Date(), "01");
            // 当天的环比
            startTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -1), "01");
            endTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -1), "01");
        } else if ("2".equals(type)) { // 7天
            kssj = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -7), "01");
            jssj = DateUtils.getQueryDateString(new Date(), "01");
            // 环比7天的 开始时间，结束时间
            startTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -14), "01");
            endTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -7), "01");
        } else if ("3".equals(type)) { // 30天
            kssj = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -30), "01");
            jssj = DateUtils.getQueryDateString(new Date(), "01");
            // 环比的30天，开始时间，结束时间
            startTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -60), "01");
            endTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -30), "01");
        }
//        UserInfo user = UserInfoManager.get(); // 获取登陆信息
        QueryWrapper<TbWzXfwz> qw = new QueryWrapper<>(); // mybatisplus
//        PermissionsUtil.queryWrapperUtil(qw, user); // mybatisplus
        String ssbm = this.tbBaseAdaptRemoteService.getSsbm(null, null);

        qw.likeRight(StringUtils.isNotBlank(ssbm), "ssbmdm", ssbm);

        Map<String, Object> map = new HashMap<>();
        int nowCount = this.tbWzXfwzMapper.selectCount( // 近期的count：计数 lambda遍历 根据日期开始时间结束时间计数 比较ge大于开始时间，le小于结束时间
                qw.lambda().ge(TbWzXfwz::getDwrq, kssj)
                        .le(TbWzXfwz::getDwrq, jssj)
        );
        // 近期的count
        map.put("nowCount", nowCount);
        // 上一期的数量
        int prevCount = this.tbWzXfwzMapper.selectCount(
                qw.lambda().ge(TbWzXfwz::getDwrq, startTime)
                        .le(TbWzXfwz::getDwrq, endTime)
        );
        map.put("prevCount", prevCount);
        map.put("hb", prevCount != 0 ? ( // 环比, 三目， 保存两位小数， BigDecimal，
                new BigDecimal((nowCount - prevCount)).divide(new BigDecimal(prevCount), BigDecimal.ROUND_HALF_UP, 4)
        ) : 0);
        map.put("hbCount", nowCount - prevCount); // 环比计数 近期的count - 上期的count
        return map;
    }

    @Override
    public Map<String, Object> rysltj(String type) {
        String kssj = null, jssj = null, startTime = null, endTime = null;
        if ("1".equals(type)) {
            kssj = DateUtils.getQueryDateString(new Date(), "01");
            jssj = DateUtils.getQueryDateString(new Date(), "01");
            startTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -1), "01");
            endTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -1), "01");
        } else if ("2".equals(type)) {
            kssj = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -7), "01");
            jssj = DateUtils.getQueryDateString(new Date(), "01");
            startTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -14), "01");
            endTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -7), "01");
        } else if ("3".equals(type)) {
            kssj = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -30), "01");
            jssj = DateUtils.getQueryDateString(new Date(), "01");
            startTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -60), "01");
            endTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -30), "01");
        }
        UserInfo user = UserInfoManager.get();
        String useSql = PermissionsUtil.createSql("",user);
        Map<String, Object> map = new HashMap<>();
        int nowCount = tbWzRywzMapper.getRysltjCount(kssj,jssj,useSql); // 需要去重
        // 近期
        map.put("nowCount", nowCount);
        // 上一期的数量
        int prevCount = tbWzRywzMapper.getRysltjCount(startTime,endTime,useSql);
        map.put("prevCount", prevCount);
        map.put("hb", prevCount != 0 ? (
                new BigDecimal((nowCount - prevCount)).divide(new BigDecimal(prevCount), BigDecimal.ROUND_HALF_UP, 4)
        ) : 0);
        map.put("hbCount", nowCount - prevCount); // 环比计数 近期的count - 上期的count
        return map;
    }

    @Override
    public Map<String, Object> sblswz(String type) {
        String kssj = null, jssj = null, startTime = null, endTime = null;
        if ("1".equals(type)) {
            kssj = DateUtils.getQueryDateString(new Date(), "01");
            jssj = DateUtils.getQueryDateString(new Date(), "01");
            startTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -1), "01");
            endTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -1), "01");
        } else if ("2".equals(type)) {
            kssj = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -7), "01");
            jssj = DateUtils.getQueryDateString(new Date(), "01");
            startTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -14), "01");
            endTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -7), "01");
        } else if ("3".equals(type)) {
            kssj = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -30), "01");
            jssj = DateUtils.getQueryDateString(new Date(), "01");
            startTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -60), "01");
            endTime = DateUtils.getQueryDateString(DateUtils.getNextDate(new Date(), "D", -30), "01");
        }
        UserInfo user = UserInfoManager.get();
        String useSql = PermissionsUtil.createSql("",user);
        Map<String, Object> map = new HashMap<>();
        int nowCount = tbWzSblswzMapper.getSblswzCount(kssj,jssj,useSql);
        // 近期
        map.put("nowCount", nowCount);
        // 上一期的数量
        int prevCount = tbWzSblswzMapper.getSblswzCount(startTime,endTime,useSql);
        map.put("prevCount", prevCount);
        map.put("hb", prevCount != 0 ? (
                new BigDecimal((nowCount - prevCount)).divide(new BigDecimal(prevCount), BigDecimal.ROUND_HALF_UP, 4)
        ) : 0);
        map.put("hbCount", nowCount - prevCount);
        return map;
    }
}
