package com.kxs.transfer.api.service.product.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.aliyun.dts.subscribe.clients.record.OperationType;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.product.api.model.KxsMachineRatio;
import com.kxs.product.api.model.KxsTicket;
import com.kxs.transfer.api.mapper.product.KxsTicketMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.service.product.KxsTicketService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 客小爽券码表(KxsTicket)表服务实现类
 *
 * @author 系统
 * @since 2023-12-20 10:09:35
 */
@Service("kxsTicketService")
@RequiredArgsConstructor
public class KxsTicketServiceImpl extends ServiceImpl<KxsTicketMapper, KxsTicket> implements KxsTicketService {
    @Override
    public void changeData(DMLData dmlData) {
        //修改的字段值
        Map<String, Object> validFieldDataMap = dmlData.getValidFieldDataMap();
        //修改的字段
        List<String> changeFieldList = dmlData.getChangeFieldList();
        //操作的主键
        int id = Integer.parseInt(dmlData.getId());

        KxsTicket ticket = new KxsTicket();
        ticket.setId(id);

        OperationType operation = dmlData.getOperation();

        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    ticket.setUserId(userId);
                    break;
                case "IsUse":
                    int isUse = Integer.parseInt(validFieldDataMap.get(field).toString());
                    if(isUse == 1){
                        ticket.setStatus(isUse);
                    }
                    break;
                case "IsLock":
                    int isLock = Integer.parseInt(validFieldDataMap.get(field).toString());
                    if(isLock == 1){
                        ticket.setStatus(2);
                    }
                    break;
                case "ExchangeCode":
                    ticket.setTicketCode(validFieldDataMap.get(field).toString());
                    break;
                case "QueryCount":
                    ticket.setTicketType(Integer.parseInt(validFieldDataMap.get(field).toString()) - 1);
                    break;
                case "LeaderUserId":
                    ticket.setLeaderId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "OpId":
                    ticket.setPartnerId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "CreateDate":
                    ticket.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    ticket.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }
        KxsTicket kxsTicket = baseMapper.selectById(ticket.getId());

        if(operation.equals(OperationType.INSERT)){
            if(kxsTicket == null){
                baseMapper.insert(ticket);
            }
        }

        if(operation.equals(OperationType.UPDATE)){

            baseMapper.updateById(ticket);
        }
    }
}

