package com.kxs.transfer.api.service.user.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.transfer.api.mapper.user.KxsUserAddressMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.model.table.FieldData;
import com.kxs.transfer.api.service.user.KxsUserAddressService;
import com.kxs.user.api.model.KxsUser;
import com.kxs.user.api.model.KxsUserAddress;
import com.kxs.user.api.model.KxsUserAmount;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 用户收货地址表(KxsUserAddress)表服务实现类
 *
 * @author 系统
 * @since 2024-01-03 09:39:56
 */
@Service("kxsUserAddressService")
public class KxsUserAddressServiceImpl extends ServiceImpl<KxsUserAddressMapper, KxsUserAddress> implements KxsUserAddressService {

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

        KxsUserAddress address = new KxsUserAddress();
        address.setId(id);
        //操作类型
        OperationType operation = dmlData.getOperation();
        if(operation.equals(OperationType.DELETE)){
            // 更新数据
            baseMapper.deleteById(id);
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "RealName":
                    address.setConsignee(validFieldDataMap.get(field).toString());
                    break;
                case "Mobile":
                    address.setPhone(validFieldDataMap.get(field).toString());
                    break;
                case "Address":
                    address.setAddress(validFieldDataMap.get(field).toString());
                    break;
                case "UserId":
                    address.setUserId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "IsDefault":
                    address.setIsDefault(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "Areas":
                    String[] split = validFieldDataMap.get(field).toString().split(",");
                    if(split.length == 3){
                        address.setProvince(split[0]);
                        address.setCity(split[1]);
                        address.setArea(split[2]);
                    }
                    break;
                case "CreateDate":
                    address.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    address.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.UPDATE)){
            // 更新数据
            baseMapper.updateById(address);
        }
        if(operation.equals(OperationType.INSERT)){

            KxsUserAddress kxsUserAddress = baseMapper.selectById(address.getId());
            if (kxsUserAddress != null){
                baseMapper.updateById(address);
            }else{
                // 插入数据
                baseMapper.insert(address);
            }
        }
    }
}

