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.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.product.api.model.KxsMachine;
import com.kxs.product.api.model.KxsMachineRatio;
import com.kxs.product.api.model.KxsMerchant;
import com.kxs.transfer.api.mapper.product.KxsMerchantMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.service.product.KxsMachineRatioService;
import com.kxs.transfer.api.service.product.KxsMachineService;
import com.kxs.transfer.api.service.product.KxsMerchantService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 客小爽商户表(KxsMerchant)表服务实现类
 *
 * @author 系统
 * @since 2023-12-20 10:09:35
 */
@Service("kxsMerchantService")
@RequiredArgsConstructor
public class KxsMerchantServiceImpl extends ServiceImpl<KxsMerchantMapper, KxsMerchant> implements KxsMerchantService {

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

        KxsMerchant merchant = new KxsMerchant();
        merchant.setId(id);

        OperationType operation = dmlData.getOperation();

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

            switch (field) {
                case "UserId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    merchant.setMakerUserId(userId);
                    break;
                case "MerchantNo":
                    merchant.setMerchantNo(validFieldDataMap.get(field).toString());
                    break;
                case "MerchantName":
                    merchant.setMerchantName(validFieldDataMap.get(field).toString());
                    break;
                case "MerIdcardNo":
                    merchant.setMerchantCard(validFieldDataMap.get(field).toString());
                    break;
                case "MerchantMobile":
                    merchant.setMerchantPhone(validFieldDataMap.get(field).toString());
                    break;
                case "BrandId":
                    merchant.setBrandId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "KqSnNo":
                    String sn = validFieldDataMap.get(field).toString();
                    KxsMachine kxsMachine = kxsMachineService.getOne(Wrappers.<KxsMachine>lambdaQuery().eq(KxsMachine::getPosSn, sn));
                    if(kxsMachine != null){
                        merchant.setMachineId(kxsMachine.getId());
                        merchant.setUserId(kxsMachine.getUserId());
                    }
                    break;
                case "MerUserType":
                    merchant.setIsMaker(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "StandardStatus":
                    merchant.setStandardStatus(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "StandardMonths":
                    merchant.setStandardMonths(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "Remark":
                    merchant.setRemark(validFieldDataMap.get(field).toString());
                    break;
                case "CreateDate":
                    merchant.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    merchant.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsMerchant kxsMerchant = baseMapper.selectById(merchant.getId());
            if(kxsMerchant == null){
                baseMapper.insert(merchant);
            }
        }

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

            baseMapper.updateById(merchant);
        }

    }
}

