package com.qmth.paper.library.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.paper.library.common.bean.dto.syssetting.SettingObject;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.entity.SysSettingGlobal;
import com.qmth.paper.library.common.enums.ExceptionResultEnum;
import com.qmth.paper.library.common.mapper.SysSettingGlobalMapper;
import com.qmth.paper.library.common.service.CommonCacheService;
import com.qmth.paper.library.common.service.SysSettingGlobalService;
import com.qmth.paper.library.common.util.ServletUtil;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 系统设置 服务实现类
 * </p>
 */
@Service
public class SysSettingGlobalServiceImpl extends ServiceImpl<SysSettingGlobalMapper, SysSettingGlobal> implements SysSettingGlobalService {

    @Resource
    CommonCacheService commonCacheService;

    @Override
    public List<SettingObject> getData() {
        QueryWrapper<SysSettingGlobal> queryWrapper = new QueryWrapper<>();
        List<SysSettingGlobal> sysSettingList = this.list(queryWrapper);

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

        if (!sysSettingList.isEmpty()) {
            sysSettingGlobal = sysSettingList.get(0);
        }
        return JSON.parseArray(sysSettingGlobal.getParameterObject(), SettingObject.class);
    }

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

        QueryWrapper<SysSettingGlobal> queryWrapper = new QueryWrapper<>();
        SysSettingGlobal sysSettingGlobal = this.getOne(queryWrapper);

        // 新增
        if (sysSettingGlobal == null) {
            sysSettingGlobal = new SysSettingGlobal();
            sysSettingGlobal.setId(SystemConstant.getDbUuid());
            sysSettingGlobal.setParameterObject(JSON.toJSONString(settingObjects));
            sysSettingGlobal.setCreateId(userId);
            sysSettingGlobal.setCreateTime(System.currentTimeMillis());
        } else {
            sysSettingGlobal.setParameterObject(JSON.toJSONString(settingObjects));
            sysSettingGlobal.setUpdateId(userId);
            sysSettingGlobal.setUpdateTime(System.currentTimeMillis());

            commonCacheService.removeSysSettingGlobal();
        }
        return this.saveOrUpdate(sysSettingGlobal);
    }

    @Override
    public SysSettingGlobal getOnlyOne() {
        QueryWrapper<SysSettingGlobal> queryWrapper = new QueryWrapper<>();
        List<SysSettingGlobal> sysSettingGlobals = this.list(queryWrapper);
        if (!sysSettingGlobals.isEmpty() && sysSettingGlobals.size() != 1) {
            throw ExceptionResultEnum.ERROR.exception("全局参数未设置");
        }
        return sysSettingGlobals.get(0);
    }
}
