package com.tuoheng.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuoheng.enums.MarkTypeEnum;
import com.tuoheng.mapper.DictDataMapper;
import com.tuoheng.mapper.PlatformMapper;
import com.tuoheng.model.dto.Platform;
import com.tuoheng.model.entity.Dict;
import com.tuoheng.model.entity.DictData;
import com.tuoheng.service.DictDataService;
import com.tuoheng.service.DictService;
import com.tuoheng.mapper.DictMapper;
import com.tuoheng.until.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 小影
 * @description 针对表【t_dict(字典类型表)】的数据库操作Service实现
 * @createDate 2023-06-02 16:27:18
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict>
        implements DictService {

    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private DictDataService dictDataService;
    @Autowired
    private PlatformMapper platformMapper;


    /**
     * 初始化平台对应字典数据
     *
     * @param code 平台code
     * @return
     */
    @Override
    @Transactional
    public JsonResult init(String code) {

        Platform platform = platformMapper.selectOne(Wrappers.<Platform>lambdaQuery()
                .eq(Platform::getMark, MarkTypeEnum.VALID.getCode())
                .eq(Platform::getPlatformCode, code));
        if (ObjectUtil.isNull(platform)) {
            return JsonResult.error("对应业务平台不存在,请检查后初始化");
        }

        List<DictData> list = dictDataMapper.selectList(Wrappers.<DictData>lambdaQuery()
                .eq(DictData::getMark, MarkTypeEnum.VALID.getCode())
                .eq(DictData::getDictType, "tuoheng-hhz"));
        //添加字典表
        Dict dict = new Dict();
        dict.setDictName(platform.getPlatformName());
        dict.setType(code);
        dictMapper.insert(dict);
        //添加对应数据
        for (DictData dictData : list) {
            dictData.setDictType(code);
            dictData.setId(null);
        }
        dictDataService.saveBatch(list);
        return JsonResult.success("初始化成功请修改对应数据值");
    }
}




