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

import com.alibaba.fastjson2.JSONObject;
import com.project.common.core.domain.search.PageParamVo;
import com.project.common.utils.DateUtils;
import com.project.zcustom.domain.access.PlatAccess;
import com.project.zcustom.domain.employee.PlatEmployeeType;
import com.project.zcustom.domain.property.PlatPropertyTools;
import com.project.zcustom.mapper.access.PlatAccessMapper;
import com.project.zcustom.service.access.IPlatAccessService;
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.common.utils.StringUtils;

import java.util.*;

/**
 * 便捷出入Service业务层处理
 *
 * @author change
 * @date 2024-10-31
 */
@Service
public class PlatAccessServiceImpl extends ServiceImpl<PlatAccessMapper, PlatAccess> implements IPlatAccessService {

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

    /**
     * 分页查询便捷出入列表
     *
     * @param platAccess 便捷出入
     * @return 便捷出入集合
     */
    @Override
    public IPage<PlatAccess> pagingPlatAccessList(PlatAccess platAccess) {
        IPage<PlatAccess> page = new Page<>(platAccess.getPageNum(), platAccess.getPageSize());
        LambdaQueryWrapper<PlatAccess> lqw = getLambdaQueryWrapper(platAccess);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatAccess::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询便捷出入列表
     *
     * @param lqw
     * @return 便捷出入集合
     */
    @Override
    public IPage<PlatAccess> pagingPlatAccessList(PageParamVo pageParamVo, LambdaQueryWrapper<PlatAccess> lqw) {
        IPage<PlatAccess> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatAccess::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询便捷出入列表
     *
     * @param platAccess 便捷出入
     * @return 便捷出入
     */
    @Override
    public List<PlatAccess> selectPlatAccessList(PlatAccess platAccess) {
        LambdaQueryWrapper<PlatAccess> lqw = getLambdaQueryWrapper(platAccess);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatAccess::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询便捷出入列表
     *
     * @param platAccess 便捷出入
     * @return 便捷出入
     */
    @Override
    public List<PlatAccess> selectPlatAccessList(PlatAccess platAccess, Long limitNum) {
        LambdaQueryWrapper<PlatAccess> lqw = getLambdaQueryWrapper(platAccess);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatAccess::getCreateTime);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 便捷出入
     */
    @Override
    public PlatAccess getPlatAccessByField(String field, String filedValue) {
        QueryWrapper<PlatAccess> lqw = new QueryWrapper<PlatAccess>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 便捷出入
     */
    @Override
    public PlatAccess getPlatAccessByField(String field, String filedValue,String fieldString) {
        QueryWrapper<PlatAccess> lqw = new QueryWrapper<PlatAccess>();
        if(!(Objects.equals(field,"app_org") && Objects.equals(filedValue,"0000"))){
            lqw.eq(field, filedValue);
        }
        lqw.select(fieldString);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 便捷出入
     */
    @Override
    public List<PlatAccess> selectPlatAccessListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatAccess> lqw = new QueryWrapper<PlatAccess>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 便捷出入
     */
    @Override
    public List<PlatAccess> selectPlatAccessListByField(String field, String filedValue) {
        QueryWrapper<PlatAccess> lqw = new QueryWrapper<PlatAccess>();
        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<PlatAccess> lqw = new QueryWrapper<PlatAccess>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if (this.getOne(lqw, false) != null) {
            throw new RuntimeException(tips + "已存在");
        }
    }

    /**
     * 真删接口
     */
    @Override
    public void reallyDeletePlatAccess(LambdaQueryWrapper<PlatAccess> lqw) {
        baseMapper.reallyDeleteByWrapper(lqw);
    }

    /**
     *  添加便捷出入数据
     */
    @Override
    public void addPlatAccess(String appOrg, Map<String, Object> mapField){
        PlatAccess accessInfo = this.getPlatAccessByField("app_org", appOrg);
        try {
            if (StringUtils.isNotNull(accessInfo)) {
                this.setPlatAccess(mapField, accessInfo);
                this.updateById(accessInfo);
            } else {
                accessInfo = new PlatAccess();
                this.setPlatAccess(mapField, accessInfo);
                accessInfo.setAppOrg(appOrg);
                this.save(accessInfo);
            }
        } catch (Exception e) {
            e.getMessage();
        }
    }

    private void setPlatAccess(Map<String, Object> mapField, PlatAccess accessInfo) {
        List<String> keyList = Arrays.asList("carLicense","guardCard","blackLists","intraDayCar","beforeDayCar","twoDayCar","threeDayCar","fourDayCar","fiveDayCar","sixDayCar");

        StringUtils.setFieldValue(accessInfo,mapField,keyList,0, PlatAccess::setCarLicenseNum,Long.class);
        StringUtils.setFieldValue(accessInfo,mapField,keyList,1, PlatAccess::setGuardCardNum,Long.class);
        StringUtils.setFieldValue(accessInfo,mapField,keyList,2, PlatAccess::setBlackListsNum,Long.class);
        StringUtils.setFieldValue(accessInfo,StringUtils.getListToMap(mapField.get("intraDay").toString()),keyList,3, PlatAccess::setIntraCarNum,Long.class);
        StringUtils.setFieldValue(accessInfo,StringUtils.getListToMap(mapField.get("beforeDay").toString()),keyList,4, PlatAccess::setBeforeCarNum,Long.class);
        StringUtils.setFieldValue(accessInfo,StringUtils.getListToMap(mapField.get("twoDay").toString()),keyList,5, PlatAccess::setTwoDaysCarNum,Long.class);
        StringUtils.setFieldValue(accessInfo,StringUtils.getListToMap(mapField.get("threeDay").toString()),keyList,6, PlatAccess::setThreeDaysCarNum,Long.class);
        StringUtils.setFieldValue(accessInfo,StringUtils.getListToMap(mapField.get("fourDay").toString()),keyList,7, PlatAccess::setFourDaysCarNum,Long.class);
        StringUtils.setFieldValue(accessInfo,StringUtils.getListToMap(mapField.get("fiveDay").toString()),keyList,8, PlatAccess::setFiveDaysCarNum,Long.class);
        StringUtils.setFieldValue(accessInfo,StringUtils.getListToMap(mapField.get("sixDay").toString()),keyList,9, PlatAccess::setSixDaysCarNum,Long.class);
    }


    /**
     * 其他数据信息
     */
    public Map<String, Object> getOtherData(String appOrg){
        String fieldString = "car_license_num,guard_card_num,black_lists_num";
        if(Objects.equals(appOrg,"0000")){
            fieldString = "sum(car_license_num) as car_license_num,sum(guard_card_num) as guard_card_num,sum(black_lists_num) as black_lists_num";
        }
        PlatAccess platAccess = this.getPlatAccessByField("app_org", appOrg,fieldString);
        Map<String, Object> tempMap = new HashMap<>();
        if (StringUtils.isNull(platAccess)) {
            StringUtils.getMapData("车证数量", "carlicense", 0, tempMap);
            StringUtils.getMapData("门禁卡数量", "guardCard", 0, tempMap);
            StringUtils.getMapData("黑名单数量", "blackLists", 0, tempMap);
        } else {
            StringUtils.getMapData("车证数量", "carlicense", platAccess.getCarLicenseNum(), tempMap);
            StringUtils.getMapData("门禁卡数量", "guardCard", platAccess.getGuardCardNum(), tempMap);
            StringUtils.getMapData("黑名单数量", "blackLists", platAccess.getBlackListsNum(), tempMap);
        }
        return tempMap;
    }


    /**
     * 近一周车流量统计
     */
    @Override
    public Map<String, Object> getCarFlowData(String appOrg){
        // 获取7天前的日期
        List<String> dateList = DateUtils.generateDateList(7);
        String fieldString = "intra_car_num, before_car_num,two_days_car_num,three_days_car_num,four_days_car_num,five_days_car_num,six_days_car_num";
        if(Objects.equals(appOrg,"0000")){
            fieldString = "sum(intra_car_num) as intra_car_num," +
                    "sum(before_car_num) as before_car_num," +
                    "sum(two_days_car_num) as two_days_car_num," +
                    "sum(three_days_car_num) as three_days_car_num," +
                    "sum(four_days_car_num) as four_days_car_num," +
                    "sum(five_days_car_num) as five_days_car_num," +
                    "sum(six_days_car_num) as six_days_car_num";
        }
        PlatAccess platAccess = this.getPlatAccessByField("app_org", appOrg,fieldString);
        List<Long> valueList = StringUtils.getDefaultListLong(7);
        if(StringUtils.isNotNull(platAccess)){
            valueList = Arrays.asList(platAccess.getIntraCarNum(), platAccess.getBeforeCarNum(), platAccess.getTwoDaysCarNum(), platAccess.getThreeDaysCarNum(), platAccess.getFourDaysCarNum(), platAccess.getFiveDaysCarNum(), platAccess.getSixDaysCarNum());
            // 倒序
            Collections.reverse(valueList);
        }
        Map<String, Object> tempMap = new HashMap<>();
        StringUtils.createdTableData(dateList,valueList,"#ffd296",tempMap);
        return tempMap;
    }


}
