package cn.cslg.pas.service;

import cn.cslg.pas.common.model.dto.analysis.SchemaDimensionDTO;
import cn.cslg.pas.common.utils.CacheUtils;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.dev33.satoken.stp.StpUtil;
import cn.cslg.pas.domain.CustomAnalysisItemSchema;
import cn.cslg.pas.common.model.dto.analysis.SchemaDataDTO;
import cn.cslg.pas.mapper.CustomAnalysisItemSchemaMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 自定义分析项目维度数据 服务实现类
 * </p>
 *
 * @author 王岩
 * @since 2021-12-23
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class CustomAnalysisItemSchemaService extends ServiceImpl<CustomAnalysisItemSchemaMapper, CustomAnalysisItemSchema> {

    private final CacheUtils cacheUtils;
    private final LoginUtils loginUtils;
    public List<CustomAnalysisItemSchema> getListByUid(String uid) {
        LambdaQueryWrapper<CustomAnalysisItemSchema> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomAnalysisItemSchema::getUid, uid);
        return this.list(queryWrapper);
    }

    public SchemaDimensionDTO getItemSchemaByUid(String uid) {
        List<CustomAnalysisItemSchema> schemaList = this.getListByUid(uid);
        CustomAnalysisItemSchema d1 = schemaList.stream().filter(item -> item.getDimension().equals("x")).findFirst().orElse(new CustomAnalysisItemSchema());
        CustomAnalysisItemSchema d2 = schemaList.stream().filter(item -> item.getDimension().equals("y")).findFirst().orElse(new CustomAnalysisItemSchema());
        SchemaDimensionDTO dto = new SchemaDimensionDTO();
        SchemaDataDTO x = new SchemaDataDTO();
        SchemaDataDTO y = new SchemaDataDTO();
        x.setField(d1.getField());
        x.setExpand(d1.getExpand());
        x.setPtype(d1.getPtype());
        x.setNum(d1.getNum());
        x.setType(d1.getType());
        y.setField(d2.getField());
        y.setExpand(d2.getExpand());
        y.setPtype(d2.getPtype());
        y.setNum(d2.getNum());
        y.setType(d2.getType());
        dto.setX(x);
        dto.setY(y);
        return dto;
    }

    public void add(SchemaDimensionDTO dto, String uid) {
        List<CustomAnalysisItemSchema> customAnalysisItemSchema = new ArrayList<>();
        CustomAnalysisItemSchema d1 = new CustomAnalysisItemSchema();
        CustomAnalysisItemSchema d2 = new CustomAnalysisItemSchema();
        d1.setUid(uid);
        d1.setDimension("x");
        d1.setField(dto.getX().getField());
        d1.setExpand(dto.getX().getExpand());
        d1.setPtype(dto.getX().getPtype());
        d1.setType(dto.getX().getType());
        d1.setNum(dto.getX().getNum());
        d1.setCreateBy(loginUtils.getId());
        d2.setUid(uid);
        d2.setDimension("y");
        d2.setField(dto.getY().getField());
        d2.setExpand(dto.getY().getExpand());
        d2.setPtype(dto.getY().getPtype());
        d2.setType(dto.getY().getType());
        d2.setNum(dto.getY().getNum());
        d2.setCreateBy(loginUtils.getId());
        customAnalysisItemSchema.add(d1);
        customAnalysisItemSchema.add(d2);
        this.saveBatch(customAnalysisItemSchema);
    }

    public void edit(SchemaDimensionDTO dto, String uid) {
        List<CustomAnalysisItemSchema> schemaList = this.getListByUid(uid);
        if (schemaList.size() == 0) {
            this.add(dto, uid);
        } else {
            schemaList.forEach(item -> {
                switch (item.getDimension()) {
                    case "x":
                        item.setField(dto.getX().getField());
                        item.setExpand(dto.getX().getExpand());
                        item.setPtype(dto.getX().getPtype());
                        item.setNum(dto.getX().getNum());
                        item.setType(dto.getX().getType());
                        break;
                    case "y":
                        item.setField(dto.getY().getField());
                        item.setExpand(dto.getY().getExpand());
                        item.setPtype(dto.getY().getPtype());
                        item.setNum(dto.getY().getNum());
                        item.setType(dto.getY().getType());
                        break;
                }
            });
            this.saveOrUpdateBatch(schemaList);
        }
    }

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