package com.hzs.scm.pub.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SaOrderFieldConstants;
import com.hzs.common.core.constant.ScmFieldConstants;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.reflect.ReflectUtils;
import com.hzs.common.domain.scm.pub.ScmItemSpecs;
import com.hzs.scm.pub.param.ItemSpecsParam;
import com.hzs.scm.pub.service.IScmItemSpecsService;
import com.hzs.scm.pub.mapper.ScmItemSpecsMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单规格明细 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-11-21
 */
@Service
public class ScmItemSpecsServiceImpl extends ServiceImpl<ScmItemSpecsMapper, ScmItemSpecs> implements IScmItemSpecsService {

    @Override
    public void deleteItemSpecsById(Integer orderType, Long pkId, Long pkMember) {
        UpdateWrapper<ScmItemSpecs> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(SystemFieldConstants.DEL_FLAG, EYesNo.NO.getIntValue());
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, pkMember);
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, DateUtils.currentDateTime());
        updateWrapper.eq(SaOrderFieldConstants.ORDER_TYPE, orderType);
        updateWrapper.eq(SaOrderFieldConstants.PK_ORDER, pkId);
        update(updateWrapper);
    }

    @Override
    public void packageScmItemByParam(List<?> itemSpecsParamList, Object obj, Integer pkCountry, Long pkMember,
                                                    Long pkOrder, Long pkOrderItems, Integer orderType) {
        List<ScmItemSpecs> scmItemSpecsList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(itemSpecsParamList)){
            for (Object itemSpecsParam : itemSpecsParamList) {
                ScmItemSpecs scmItemSpecs = ScmItemSpecs.builder()
                        .orderType(orderType)
                        .pkSpecsType(ReflectUtils.invokeGetter(itemSpecsParam, "pkSpecsType"))
                        .pkSpecs(ReflectUtils.invokeGetter(itemSpecsParam, "pkSpecs"))
                        .specsName(ReflectUtils.invokeGetter(itemSpecsParam, "specsName"))
                        .build();
                scmItemSpecs.setPkCreator(pkMember);
                scmItemSpecs.setPkCountry(pkCountry);
                scmItemSpecs.setPkOrder(pkOrder);
                scmItemSpecs.setPkOrderItems(pkOrderItems);
                scmItemSpecsList.add(scmItemSpecs);
            }
        }
        if (scmItemSpecsList.size() > 0) {
            ReflectUtils.invokeSetter(obj, ScmFieldConstants.SCM_ITEM_SPECS_LIST, scmItemSpecsList);
        }
        ReflectUtils.invokeSetter(obj, SystemFieldConstants.PK_COUNTRY_VALUE, pkCountry);
        if (ReflectUtils.invokeGetter(obj, SystemFieldConstants.PK_ID_VALUE) == null) {
            ReflectUtils.invokeSetter(obj, ScmFieldConstants.PK_CREATOR_FIELD, pkMember);
        } else {
            ReflectUtils.invokeSetter(obj, ScmFieldConstants.PK_MODIFIED_FIELD, pkMember);
            ReflectUtils.invokeSetter(obj, ScmFieldConstants.MODIFIED_TIME_FIELD, DateUtils.currentDateTime());
        }
    }

    @Override
    public void packageScmItemSpecsList(List<ScmItemSpecs> scmItemSpecList, Long pkMember, Long pkOrder, Long pkOrderItems, Integer orderType) {
        scmItemSpecList.forEach(scmItemSpecs -> {
            scmItemSpecs.setOrderType(orderType);
            scmItemSpecs.setPkOrder(pkOrder);
            scmItemSpecs.setPkOrderItems(pkOrderItems);
            scmItemSpecs.setPkCreator(pkMember);
            scmItemSpecs.setCreationTime(null);
        });
    }

    @Override
    public List<ScmItemSpecs> packageOrderItemsSpecs(List<?> itemsList, Long pkMasterId) {
        List<ScmItemSpecs> saveScmItemSpecsList = new ArrayList<>();
        for (Object items : itemsList) {
            List<ScmItemSpecs> scmItemSpecsList = ReflectUtils.invokeGetter(items, ScmFieldConstants.SCM_ITEM_SPECS_LIST);
            if(CollectionUtil.isNotEmpty(scmItemSpecsList)){
                for (ScmItemSpecs scmItemSpecs : scmItemSpecsList) {
                    scmItemSpecs.setPkOrder(pkMasterId);
                    scmItemSpecs.setPkOrderItems(ReflectUtils.invokeGetter(items, SystemFieldConstants.PK_ID_VALUE));
                }
                saveScmItemSpecsList.addAll(scmItemSpecsList);
            }
        }
        return saveScmItemSpecsList;
    }

    @Override
    public Map<Long, List<ScmItemSpecs>> queryScmItemSpecsByCondition(Integer orderType, List<ScmItemSpecs> queryScmList) {
        Map<Long, List<ScmItemSpecs>> scmItemSpecsMap = new HashMap<>();
        List<ScmItemSpecs> scmItemSpecsList = baseMapper.queryScmItemSpecsByCondition(orderType, queryScmList);
        scmItemSpecsList.forEach(scmItemSpecs -> {
            if(scmItemSpecsMap.containsKey(scmItemSpecs.getPkOrderItems())){
                scmItemSpecsMap.get(scmItemSpecs.getPkOrderItems()).add(scmItemSpecs);
            }else{
                List<ScmItemSpecs> itemSpecsList = new ArrayList<>();
                itemSpecsList.add(scmItemSpecs);
                scmItemSpecsMap.put(scmItemSpecs.getPkOrderItems(), itemSpecsList);
            }
        });
        return scmItemSpecsMap;
    }
}
