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

import com.project.zcustom.domain.employee.PlatEmployeeTypeFlow;
import com.project.zcustom.mapper.employee.PlatEmployeeTypeFlowMapper;
import com.project.zcustom.service.employee.IPlatEmployeeTypeFlowService;
import com.project.zcustom.controller.core.PageParamVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.zcustom.controller.core.StringUtils;
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
    public void handleEmployeeFlow(String appOrg,Map<String, Object> mapField){
        // 先删除 employeeTypeFlow 数据
        LambdaQueryWrapper<PlatEmployeeTypeFlow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlatEmployeeTypeFlow::getAppOrg, appOrg);
        baseMapper.reallyDeleteByWrapper(queryWrapper);
        List<String> firstList = Arrays.asList("dayBeforeIns", "dayBeforeLoan", "dayBeforeAsso", "dayBeforePro","dayBeforeVis");// 前一天的数据
        List<String> secondList = Arrays.asList("twoDaysIns", "twoDaysLoan", "twoDaysAsso", "twoDaysPro","twoDaysVis"); // 前两天的数据
        List<String> thirdList = Arrays.asList("firstThreeDaysIns", "firstThreeDaysLoan", "firstThreeDaysAsso", "firstThreeDaysPro","firstThreeDaysVis");// 前三天的数据
        List<String> fourList = Arrays.asList("firstFourDaysIns", "firstFourDaysLoan", "firstFourDaysAsso", "firstFourDaysPro","firstFourDaysVis");// 前四天的数据
        List<String> fiveList = Arrays.asList("firstFiveDaysIns", "firstFiveDaysLoan", "firstFiveDaysAsso", "firstFiveDaysPro","firstFiveDaysVis");// 前五天的数据
        List<String> sixList = Arrays.asList("firstSixDaysIns", "firstSixDaysLoan", "firstSixDaysAsso", "firstSixDaysPro","firstSixDaysVis");// 前六天的数据
        List<String> sevenList = Arrays.asList("firstSevenDaysIns", "firstSevenDaysLoan", "firstSevenDaysAsso", "firstSevenDaysPro","firstSevenDaysVis");// 前七天的数据
        List<List<String>> weekList = Arrays.asList(firstList, secondList, thirdList, fourList, fiveList, sixList, sevenList);
        List<PlatEmployeeTypeFlow> addList = new ArrayList<>();
        for (int i = 0; i < weekList.size(); i++){
            List<String> list = weekList.get(i);
            PlatEmployeeTypeFlow platEmployeeTypeFlow = new PlatEmployeeTypeFlow();
            platEmployeeTypeFlow.setAppOrg(appOrg);
            platEmployeeTypeFlow.setTimeSign(i+1);
            StringUtils.setFieldValue(platEmployeeTypeFlow, mapField, list, 0, PlatEmployeeTypeFlow::setInsideNum, Long.class);
            StringUtils.setFieldValue(platEmployeeTypeFlow, mapField, list, 1, PlatEmployeeTypeFlow::setLoanNum, Long.class);
            StringUtils.setFieldValue(platEmployeeTypeFlow, mapField, list, 2, PlatEmployeeTypeFlow::setAssociationNum, Long.class);
            StringUtils.setFieldValue(platEmployeeTypeFlow, mapField, list, 3, PlatEmployeeTypeFlow::setPropertyNum, Long.class);
            StringUtils.setFieldValue(platEmployeeTypeFlow, mapField, list, 4, PlatEmployeeTypeFlow::setVisitNum, Long.class);
//            addList.add(platEmployeeTypeFlow);
            baseMapper.insertPlatEmployeeTypeFlow(platEmployeeTypeFlow);
        }
    }

}
