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.KxsTicket;
import com.kxs.product.api.model.KxsTicketTransfer;
import com.kxs.product.api.model.KxsTicketTransferInfo;
import com.kxs.transfer.api.mapper.product.KxsTicketMapper;
import com.kxs.transfer.api.mapper.product.KxsTicketTransferInfoMapper;
import com.kxs.transfer.api.mapper.product.KxsTicketTransferMapper;
import com.kxs.transfer.api.mapper.user.KxsUserMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.service.product.KxsTicketTransferService;
import com.kxs.user.api.model.KxsUser;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 客小爽券码划拨记录表(KxsTicketTransfer)表服务实现类
 *
 * @author 系统
 * @since 2023-12-20 10:09:35
 */
@Service("kxsTicketTransferService")
@RequiredArgsConstructor
public class KxsTicketTransferServiceImpl extends ServiceImpl<KxsTicketTransferMapper, KxsTicketTransfer> implements KxsTicketTransferService {

    private final KxsUserMapper kxsUserMapper;


    private final KxsTicketTransferInfoMapper kxsTicketTransferInfoMapper;

    private final KxsTicketMapper kxsTicketMapper;

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

        KxsTicketTransfer ticketTransferInfo = new KxsTicketTransfer();
        ticketTransferInfo.setId(id);

        OperationType operation = dmlData.getOperation();

        for (String field : changeFieldList) {

            switch (field) {
                case "FromUserId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    ticketTransferInfo.setSendUserId(userId);
                    KxsUser user = kxsUserMapper.selectById(userId);
                    if(user != null){
                        ticketTransferInfo.setSendUserCode(user.getUserCode());
                        ticketTransferInfo.setSendUserName(user.getUsername());
                    }

                    break;
                case "ToUserId":
                    int toUserId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    ticketTransferInfo.setToUserId(toUserId);
                    KxsUser toUser = kxsUserMapper.selectById(toUserId);
                    if(toUser != null){
                        ticketTransferInfo.setToUserCode(toUser.getUserCode());
                        ticketTransferInfo.setToUserName(toUser.getUsername());
                    }
                    break;
                case "ChangeCount":
                    ticketTransferInfo.setTransferNum(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "OrderNo":
                    ticketTransferInfo.setOrderSn(validFieldDataMap.get(field).toString());
                    break;
                case "QueryCount":
                    ticketTransferInfo.setTicketType(Integer.parseInt(validFieldDataMap.get(field).toString()) - 1);
                    break;
                case "CreateDate":
                    ticketTransferInfo.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    ticketTransferInfo.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }
        KxsTicketTransfer kxsTicketTransfer = baseMapper.selectById(ticketTransferInfo.getId());

        if(operation.equals(OperationType.INSERT)){
            if(kxsTicketTransfer == null){
                baseMapper.insert(ticketTransferInfo);
            }
        }

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

            baseMapper.updateById(ticketTransferInfo);
        }
    }

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

        KxsTicketTransferInfo ticketTransferInfo = new KxsTicketTransferInfo();
        ticketTransferInfo.setId(id);

        OperationType operation = dmlData.getOperation();

        for (String field : changeFieldList) {

            switch (field) {
                case "PosCouponId":
                    int tid = Integer.parseInt(validFieldDataMap.get(field).toString());
                    KxsTicket ticket = kxsTicketMapper.selectById(tid);
                    if(ticket != null){
                        ticketTransferInfo.setTicketCode(ticket.getTicketCode());
                    }
                    ticketTransferInfo.setTicketId(tid);
                    break;
                case "ExchangeCode":
                    ticketTransferInfo.setTicketCode(validFieldDataMap.get(field).toString());
                    break;
                case "OrderNo":
                    ticketTransferInfo.setOrderSn(validFieldDataMap.get(field).toString());
                    break;
                case "CreateDate":
                    ticketTransferInfo.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    ticketTransferInfo.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }
        KxsTicketTransferInfo kxsTicketTransferInfo = kxsTicketTransferInfoMapper.selectById(ticketTransferInfo.getId());

        if(operation.equals(OperationType.INSERT)){
            if(kxsTicketTransferInfo == null){
                kxsTicketTransferInfoMapper.insert(ticketTransferInfo);
            }
        }

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

            kxsTicketTransferInfoMapper.updateById(ticketTransferInfo);
        }
    }
}

