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.system.api.model.KxsBanner;
import com.kxs.system.api.model.KxsCol;
import com.kxs.transfer.api.mapper.sys.KxsBannerMapper;
import com.kxs.transfer.api.model.table.DMLData;
import org.springframework.stereotype.Service;

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

/**
 * 首页广告管理表(KxsBanner)表服务实现类
 *
 * @author 系统
 * @since 2024-06-25 15:45:05
 */
@Service("kxsBannerService")
public class KxsBannerService extends ServiceImpl<KxsBannerMapper, KxsBanner> {

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

        KxsBanner kxsBanner = new KxsBanner();
        kxsBanner.setId(id);

        OperationType operation = dmlData.getOperation();

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

            switch (field) {
                case "Sort":
                    kxsBanner.setSort(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "Status":
                    kxsBanner.setStatus(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "Url":
                    kxsBanner.setUrl(validFieldDataMap.get(field).toString());
                    break;
                case "SeoTitle":
                    kxsBanner.setRemark(validFieldDataMap.get(field).toString());
                    break;
                case "PicPath":
                    kxsBanner.setPicUrl(validFieldDataMap.get(field).toString());
                    break;
                case "Title":
                    kxsBanner.setTitle(validFieldDataMap.get(field).toString());
                    break;
                case "ColId":
                    kxsBanner.setColId(validFieldDataMap.get(field).toString());
                    break;
                case "Contents":
                    kxsBanner.setContents(validFieldDataMap.get(field).toString());
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsBanner banner = baseMapper.selectById(id);
            if(banner == null){
                baseMapper.insert(kxsBanner);
            }
        }
        if(operation.equals(OperationType.UPDATE)){
            baseMapper.updateById(kxsBanner);
        }
    }
}

