package com.qmth.teachcloud.exchange.common.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.teachcloud.exchange.common.bean.dto.syssetting.SettingObject;
import com.qmth.teachcloud.exchange.common.contant.SystemConstant;
import com.qmth.teachcloud.exchange.common.entity.BasicSchool;
import com.qmth.teachcloud.exchange.common.entity.SysSetting;
import com.qmth.teachcloud.exchange.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.exchange.common.mapper.SysSettingMapper;
import com.qmth.teachcloud.exchange.common.service.CommonCacheService;
import com.qmth.teachcloud.exchange.common.service.SysSettingService;
import com.qmth.teachcloud.exchange.common.util.ServletUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 系统设置 服务实现类
 * </p>
 */
@Service
public class SysSettingServiceImpl extends ServiceImpl<SysSettingMapper, SysSetting> implements SysSettingService {

    @Resource
    CommonCacheService commonCacheService;

    @Override
    public SysSetting getData(Long schoolId) {
        QueryWrapper<SysSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysSetting::getSchoolId, schoolId);
        List<SysSetting> sysSettingList = this.list(queryWrapper);

        SysSetting sysSetting = new SysSetting();
        // 第一次设置，初始化数据
        if (sysSettingList.isEmpty()) {
            BasicSchool basicSchool = commonCacheService.schoolCache(schoolId);
            sysSetting = SysSetting.init(basicSchool);
            this.save(sysSetting);
        } else if (sysSettingList.size() > 1) {
            throw ExceptionResultEnum.ERROR.exception("系统设置有误，查出多条数据");
        }

        if (!sysSettingList.isEmpty()) {
            sysSetting = sysSettingList.get(0);
        }

        return sysSetting;
    }

    @Override
    public boolean saveData(List<SettingObject> settingObjects) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        Long userId = Long.valueOf(ServletUtil.getRequestUserId().toString());

        QueryWrapper<SysSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysSetting::getSchoolId, schoolId);
        SysSetting sysSetting = this.getOne(queryWrapper);

        String parameterObject = JSON.toJSONString(settingObjects);
        // 新增
        if (sysSetting == null) {
            sysSetting = new SysSetting();
            sysSetting.setId(SystemConstant.getDbUuid());
            sysSetting.setParameterObject(parameterObject);
            sysSetting.setSchoolId(schoolId);
            sysSetting.setCreateId(userId);
            sysSetting.setCreateTime(System.currentTimeMillis());
        } else {
            sysSetting.setParameterObject(parameterObject);
            sysSetting.setUpdateId(userId);
            sysSetting.setUpdateTime(System.currentTimeMillis());
            commonCacheService.removeSysSetting(schoolId);
        }
        return this.saveOrUpdate(sysSetting);
    }

    @Override
    public SysSetting getBySchoolId(Long schoolId) {
        return getData(schoolId);
    }

    @Override
    public void flushCache(String type) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        commonCacheService.removeSysSetting(schoolId);
        commonCacheService.getSysSetting(schoolId);
//        commonCacheService.removePrivilegeUrlCache(PrivilegePropertyEnum.AUTH);
//        commonCacheService.removePrivilegeUrlCache(PrivilegePropertyEnum.SYS);
    }
}
