package com.project.zcustom.service.service.employee.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.project.zcustom.controller.core.PageParamVo;
import com.project.zcustom.controller.core.StringUtils;
import com.project.zcustom.domain.employee.PlatEmployeeTypeFlow;
import com.project.zcustom.mapper.employee.PlatEmployeeTypeFlowMapper;
import com.project.zcustom.service.service.employee.IPlatEmployeeTypeFlowService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 近一周人流量统计Service业务层处理
 *
 * @author change
 * @date 2024-10-21
 */
@Service
public class PlatEmployeeTypeFlowServiceImpl extends ServiceImpl<PlatEmployeeTypeFlowMapper, PlatEmployeeTypeFlow> implements IPlatEmployeeTypeFlowService {

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<PlatEmployeeTypeFlow> getLambdaQueryWrapper(PlatEmployeeTypeFlow platEmployeeTypeFlow) {
        LambdaQueryWrapper<PlatEmployeeTypeFlow> lqw = new LambdaQueryWrapper<PlatEmployeeTypeFlow>();
        if (StringUtils.isNotBlank(platEmployeeTypeFlow.getAppOrg())) {
            lqw.eq(PlatEmployeeTypeFlow::getAppOrg, platEmployeeTypeFlow.getAppOrg());
        }
        return lqw;
    }

    /**
     * 分页查询近一周人流量统计列表
     *
     * @param platEmployeeTypeFlow 近一周人流量统计
     * @return 近一周人流量统计集合
     */
    @Override
    public IPage<PlatEmployeeTypeFlow> pagingPlatEmployeeTypeFlowList(PlatEmployeeTypeFlow platEmployeeTypeFlow) {
        IPage<PlatEmployeeTypeFlow> page = new Page<>(platEmployeeTypeFlow.getPageNum(), platEmployeeTypeFlow.getPageSize());
        LambdaQueryWrapper<PlatEmployeeTypeFlow> lqw = getLambdaQueryWrapper(platEmployeeTypeFlow);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatEmployeeTypeFlow::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询近一周人流量统计列表
     *
     * @param lqw
     * @return 近一周人流量统计集合
     */
    @Override
    public IPage<PlatEmployeeTypeFlow> pagingPlatEmployeeTypeFlowList(PageParamVo pageParamVo, LambdaQueryWrapper<PlatEmployeeTypeFlow> lqw) {
        IPage<PlatEmployeeTypeFlow> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatEmployeeTypeFlow::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询近一周人流量统计列表
     *
     * @param platEmployeeTypeFlow 近一周人流量统计
     * @return 近一周人流量统计
     */
    @Override
    public List<PlatEmployeeTypeFlow> selectPlatEmployeeTypeFlowList(PlatEmployeeTypeFlow platEmployeeTypeFlow) {
        LambdaQueryWrapper<PlatEmployeeTypeFlow> lqw = getLambdaQueryWrapper(platEmployeeTypeFlow);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatEmployeeTypeFlow::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询近一周人流量统计列表
     *
     * @param platEmployeeTypeFlow 近一周人流量统计
     * @return 近一周人流量统计
     */
    @Override
    public List<PlatEmployeeTypeFlow> selectPlatEmployeeTypeFlowList(PlatEmployeeTypeFlow platEmployeeTypeFlow, Long limitNum) {
        LambdaQueryWrapper<PlatEmployeeTypeFlow> lqw = getLambdaQueryWrapper(platEmployeeTypeFlow);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatEmployeeTypeFlow::getCreateTime);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 近一周人流量统计
     */
    @Override
    public PlatEmployeeTypeFlow getPlatEmployeeTypeFlowByField(String field, String filedValue) {
        QueryWrapper<PlatEmployeeTypeFlow> lqw = new QueryWrapper<PlatEmployeeTypeFlow>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 近一周人流量统计
     */
    @Override
    public List<PlatEmployeeTypeFlow> selectPlatEmployeeTypeFlowListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatEmployeeTypeFlow> lqw = new QueryWrapper<PlatEmployeeTypeFlow>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        lqw.orderByDesc("time_sign");
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 近一周人流量统计
     */
    @Override
    public List<PlatEmployeeTypeFlow> selectPlatEmployeeTypeFlowListByField(String field, String filedValue) {
        QueryWrapper<PlatEmployeeTypeFlow> lqw = new QueryWrapper<PlatEmployeeTypeFlow>();
        lqw.eq(field, filedValue);
        return this.list(lqw);
    }

    /**
     * 判断指定字段值是否存在
     *
     * @param field      字段名称
     * @param filedValue 字段值
     */
    @Override
    public void checkFieldExist(String field, String filedValue, String tips, Long id) {
        QueryWrapper<PlatEmployeeTypeFlow> lqw = new QueryWrapper<PlatEmployeeTypeFlow>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if (this.getOne(lqw, false) != null) {
            throw new RuntimeException(tips + "已存在");
        }
    }

    /**
     * 处理人流量数据
     * 员工概况 近一周人流量统计
     * @param appOrg
     * @param mapField
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleEmployeeFlow(String appOrg,Map<String, Object> mapField){
        // 先删除 employeeTypeFlow 数据
        LambdaQueryWrapper<PlatEmployeeTypeFlow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlatEmployeeTypeFlow::getAppOrg, appOrg);
        baseMapper.reallyDeleteByWrapper(queryWrapper);
        // 当天(today)
        List<String> todayList = Arrays.asList("todayIns", "todayLoan", "todayAsso", "todayPro","todayVis");
        // 前一天(dayBefore)
        List<String> beforeList = Arrays.asList("dayBeforeIns", "dayBeforeLoan", "dayBeforeAsso", "dayBeforePro","dayBeforeVis");
        // 前两天(firstTwoDays)
        List<String> twodList = Arrays.asList("firstTwoDaysIns", "firstTwoDaysLoan", "firstTwoDaysAsso", "firstTwoDaysPro","firstTwoDaysVis");
        // 前三天(firstThreeDays)
        List<String> thirdList = Arrays.asList("firstThreeDaysIns", "firstThreeDaysLoan", "firstThreeDaysAsso", "firstThreeDaysPro","firstThreeDaysVis");
        // 前四天(firstFourDays)
        List<String> fourList = Arrays.asList("firstFourDaysIns", "firstFourDaysLoan", "firstFourDaysAsso", "firstFourDaysPro","firstFourDaysVis");
        // 前五天(firstFiveDays)
        List<String> fiveList = Arrays.asList("firstFiveDaysIns", "firstFiveDaysLoan", "firstFiveDaysAsso", "firstFiveDaysPro","firstFiveDaysVis");
        // 前六天(firstSixDays)
        List<String> sixList = Arrays.asList("firstSixDaysIns", "firstSixDaysLoan", "firstSixDaysAsso", "firstSixDaysPro","firstSixDaysVis");

        List<String> weekList = Arrays.asList("today","dayBefore","firstTwoDays","firstThreeDays","firstFourDays","firstFiveDays","firstSixDays");
        List<List<String>> week2List = Arrays.asList(todayList, beforeList, twodList, thirdList, fourList, fiveList, sixList);
        List<PlatEmployeeTypeFlow> addList = new ArrayList<>();
        for (int i = 0; i < weekList.size(); i++){
            List<String> list = week2List.get(i);
            PlatEmployeeTypeFlow platEmployeeTypeFlow = new PlatEmployeeTypeFlow();
            platEmployeeTypeFlow.setAppOrg(appOrg);
            platEmployeeTypeFlow.setTimeSign(i+1);
            String key = weekList.get(i);
            Map<String, Object> mapJson = StringUtils.getListToMap(mapField.get(key).toString());
            StringUtils.setFieldValue(platEmployeeTypeFlow, mapJson, list, 0, PlatEmployeeTypeFlow::setInsideNum, Long.class);
            StringUtils.setFieldValue(platEmployeeTypeFlow, mapJson, list, 1, PlatEmployeeTypeFlow::setLoanNum, Long.class);
            StringUtils.setFieldValue(platEmployeeTypeFlow, mapJson, list, 2, PlatEmployeeTypeFlow::setAssociationNum, Long.class);
            StringUtils.setFieldValue(platEmployeeTypeFlow, mapJson, list, 3, PlatEmployeeTypeFlow::setPropertyNum, Long.class);
            StringUtils.setFieldValue(platEmployeeTypeFlow, mapJson, list, 4, PlatEmployeeTypeFlow::setVisitNum, Long.class);
            addList.add(platEmployeeTypeFlow);
        }
        this.saveBatch(addList);
    }

}
