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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.domain.scm.warn.IcWarnConfig;
import com.hzs.common.domain.scm.warn.IcWarnConfigSpecs;
import com.hzs.common.domain.scm.warn.ext.IcWarnConfigExt;
import com.hzs.scm.pub.param.ItemSpecsParam;
import com.hzs.scm.warn.mapper.IcWarnConfigMapper;
import com.hzs.scm.warn.param.WarnConfigParam;
import com.hzs.scm.warn.service.IIcWarnConfigService;
import com.hzs.scm.warn.service.IIcWarnConfigSpecsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品预警配置 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2024-03-26
 */
@Service
public class IcWarnConfigServiceImpl extends ServiceImpl<IcWarnConfigMapper, IcWarnConfig> implements IIcWarnConfigService {

    @Autowired
    private IIcWarnConfigSpecsService warnConfigSpecsService;

    @Override
    public List<IcWarnConfigExt> queryWarnConfigByCondition(WarnConfigParam warnConfigParam) {
        return baseMapper.queryWarnConfigByCondition(warnConfigParam);
    }

    @Override
    public List<IcWarnConfigExt> queryWarnConfigInventory() {
        return baseMapper.queryWarnConfigInventory();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWarnConfigByParam(WarnConfigParam warnConfigParam) {
        if (warnConfigParam.getPkId() == null) {
            IcWarnConfig icWarnConfig = BeanUtil.copyProperties(warnConfigParam, IcWarnConfig.class);
            icWarnConfig.setPkCreator(warnConfigParam.getPkCreator());
            save(icWarnConfig);
            warnConfigParam.setPkId(icWarnConfig.getPkId());
        } else {
            // 删除规格
            warnConfigSpecsService.deleteWarnConfigSpecsById(warnConfigParam.getPkId(), warnConfigParam.getPkCreator());
            UpdateWrapper<IcWarnConfig> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("PK_PRODUCT_BAS", warnConfigParam.getPkProductBas());
            updateWrapper.set("WARN_QUANTITY", warnConfigParam.getWarnQuantity());
            updateWrapper.set("WARN_PERIOD", warnConfigParam.getWarnPeriod());
            updateWrapper.set("MODIFIED_TIME", DateUtils.currentDateTime());
            updateWrapper.set("PK_MODIFIED", warnConfigParam.getPkCreator());
            updateWrapper.eq(SystemFieldConstants.PK_ID, warnConfigParam.getPkId());
            update(updateWrapper);
        }
        List<IcWarnConfigSpecs> warnConfigSpecsList = new ArrayList<>();
        for (ItemSpecsParam itemSpecsParam : warnConfigParam.getScmItemSpecsList()) {
            IcWarnConfigSpecs scmItemSpecs = IcWarnConfigSpecs.builder()
                    .pkWarnConfig(warnConfigParam.getPkId())
                    .pkSpecsType(itemSpecsParam.getPkSpecsType())
                    .pkSpecs(itemSpecsParam.getPkSpecs())
                    .specsName(itemSpecsParam.getSpecsName())
                    .build();
            scmItemSpecs.setPkCreator(warnConfigParam.getPkCreator());
            scmItemSpecs.setPkCountry(warnConfigParam.getPkCountry());
            warnConfigSpecsList.add(scmItemSpecs);
        }
        warnConfigSpecsService.saveBatch(warnConfigSpecsList);
    }

    @Override
    public void deleteWarnConfigById(Integer pkId, Long pkMember) {
        UpdateWrapper<IcWarnConfig> 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(SystemFieldConstants.PK_ID, pkId);
        update(updateWrapper);
    }
}
