package com.kxs.transfer.api.service.sys;

import com.aliyun.dts.subscribe.clients.record.OperationType;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.store.api.model.KxsActTotal;
import com.kxs.system.api.model.KxsCol;
import com.kxs.transfer.api.mapper.sys.KxsColMapper;
import com.kxs.transfer.api.model.table.DMLData;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 广告位分类表(KxsCol)表服务实现类
 *
 * @author 系统
 * @since 2024-06-25 15:45:08
 */
@Service("kxsColService")
public class KxsColService extends ServiceImpl<KxsColMapper, KxsCol> {

    public void changeData(DMLData dmlData) {
        //修改的字段值
        Map<String, Object> validFieldDataMap = dmlData.getValidFieldDataMap();
        //修改的字段
        List<String> changeFieldList = dmlData.getChangeFieldList();
        //操作的主键
        int id = Integer.parseInt(dmlData.getId());

        KxsCol kxsCol = new KxsCol();
        kxsCol.setId(id);

        OperationType operation = dmlData.getOperation();

        if(operation.equals(OperationType.DELETE)){
            baseMapper.deleteById(id);
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "Sort":
                    kxsCol.setSort(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "Url":
                    kxsCol.setUrl(validFieldDataMap.get(field).toString());
                    break;
                case "ColDetail":
                    kxsCol.setColDetail(validFieldDataMap.get(field).toString());
                    break;
                case "ColPicPath":
                    kxsCol.setColPic(validFieldDataMap.get(field).toString());
                    break;
                case "ColName":
                    kxsCol.setColName(validFieldDataMap.get(field).toString());
                    break;
                case "ColId":
                    kxsCol.setColId(validFieldDataMap.get(field).toString());
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsCol col = baseMapper.selectById(id);
            if(col == null){
                baseMapper.insert(kxsCol);
            }
        }
        if(operation.equals(OperationType.UPDATE)){
            baseMapper.updateById(kxsCol);
        }
    }
}

