package cn.cslg.pas.service;

import cn.cslg.pas.common.model.dto.analysis.CustomSettingColorConfigDTO;
import cn.cslg.pas.common.utils.JsonUtils;
import cn.cslg.pas.common.model.dto.analysis.CustomSettingConfigDTO;
import cn.cslg.pas.common.model.dto.analysis.CustomSettingLineConfigDTO;
import cn.cslg.pas.common.model.dto.analysis.CustomSettingTableConfigDTO;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.domain.CustomAnalysisItemSetting;
import cn.cslg.pas.mapper.CustomAnalysisItemSettingMapper;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 自定义分析项目图表设置 服务实现类
 * </p>
 *
 * @author 王岩
 * @since 2021-12-23
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class CustomAnalysisItemSettingService extends ServiceImpl<CustomAnalysisItemSettingMapper, CustomAnalysisItemSetting> {
    private final LoginUtils loginUtils;
    public CustomAnalysisItemSetting getItemSettingByUid(String uid) {
        CustomAnalysisItemSetting temp = this.getOne(Wrappers.<CustomAnalysisItemSetting>lambdaQuery().eq(CustomAnalysisItemSetting::getUid, uid));
        if (temp == null) {
            temp = this.add(new CustomAnalysisItemSetting(), uid);
        }
        CustomSettingConfigDTO configDTO = new CustomSettingConfigDTO();
        configDTO.setColor(JsonUtils.jsonToList(temp.getColorConfig(), CustomSettingColorConfigDTO.class));
        configDTO.setLine(JsonUtils.jsonToPojo(temp.getLineConfig(), CustomSettingLineConfigDTO.class));
        configDTO.setTable(JsonUtils.jsonToList(temp.getTableConfig(), CustomSettingTableConfigDTO.class));
        temp.setConfig(configDTO);
        temp.setColorConfig(null);
        temp.setLineConfig(null);
        temp.setTableConfig(null);
        temp.setId(null);
        temp.setUid(null);
        temp.setCreateTime(null);
        temp.setUpdateTime(null);
        temp.setStatus(null);
        return temp;
    }

    public CustomAnalysisItemSetting add(CustomAnalysisItemSetting customAnalysisItemSetting, String uid) {
        customAnalysisItemSetting.setUid(uid);
        customAnalysisItemSetting.setCreateBy(loginUtils.getId());
        customAnalysisItemSetting.setColorConfig(JsonUtils.objectToJson(customAnalysisItemSetting.getConfig().getColor()));
        customAnalysisItemSetting.setTableConfig(JsonUtils.objectToJson(customAnalysisItemSetting.getConfig().getTable()));
        customAnalysisItemSetting.setLineConfig(JsonUtils.objectToJson(customAnalysisItemSetting.getConfig().getLine()));
        customAnalysisItemSetting.insert();
        return customAnalysisItemSetting;
    }

    public void edit(CustomAnalysisItemSetting customAnalysisItemSetting, String uid) {
        CustomAnalysisItemSetting temp = this.getOne(Wrappers.<CustomAnalysisItemSetting>lambdaQuery().eq(CustomAnalysisItemSetting::getUid, uid));
        if (temp == null) {
            temp = this.add(customAnalysisItemSetting, uid);
        }
        customAnalysisItemSetting.setId(temp.getId());
        customAnalysisItemSetting.setColorConfig(JsonUtils.objectToJson(customAnalysisItemSetting.getConfig().getColor()));
        customAnalysisItemSetting.setTableConfig(JsonUtils.objectToJson(customAnalysisItemSetting.getConfig().getTable()));
        customAnalysisItemSetting.setLineConfig(JsonUtils.objectToJson(customAnalysisItemSetting.getConfig().getLine()));
        customAnalysisItemSetting.updateById();
    }

    public void deleteByUid(String uid) {
        this.remove(Wrappers.<CustomAnalysisItemSetting>lambdaQuery().eq(CustomAnalysisItemSetting::getUid, uid));
    }
}
