package com.hzs.bonus.bonus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.bonus.mapper.BdWaresBonusMapper;
import com.hzs.bonus.bonus.param.BdWaresBonusParam;
import com.hzs.bonus.bonus.service.IBdWaresBonusItemsService;
import com.hzs.bonus.bonus.service.IBdWaresBonusService;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.bonus.BdWaresBonus;
import com.hzs.common.domain.member.bonus.BdWaresBonusItems;
import com.hzs.common.domain.member.ext.BdWaresBonusExt;
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.Date;
import java.util.List;

/**
 * 特殊商品分配奖金 服务实现类
 */
@Service
public class BdWaresBonusServiceImpl extends ServiceImpl<BdWaresBonusMapper, BdWaresBonus> implements IBdWaresBonusService {

    @Autowired
    private IBdWaresBonusItemsService iBdWaresBonusItemsService;

    @Override
    public List<BdWaresBonusExt> querySpecialWaresBonus(BdWaresBonusParam waresBonusParam) {
        return baseMapper.querySpecialWaresBonus(waresBonusParam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSpecialWaresBonus(Long pkId, Long userId) {
        // 删除明细表
        iBdWaresBonusItemsService.deleteWaresBonusItems(null, pkId, userId);
        LambdaUpdateWrapper<BdWaresBonus> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(BdWaresBonus::getPkModified, userId);
        updateWrapper.set(BdWaresBonus::getDelFlag, EYesNo.NO.getIntValue());
        updateWrapper.set(BdWaresBonus::getModifiedTime, DateUtils.currentDateTime());
        updateWrapper.eq(BdWaresBonus::getPkId, pkId);
        update(updateWrapper);
    }

    @Override
    public BdWaresBonus queryWaresBonusById(BdWaresBonusParam waresBonusParam) {
        LambdaQueryWrapper<BdWaresBonus> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdWaresBonus::getPkWares, waresBonusParam.getPkWares());
        if (waresBonusParam.getPkId() != null) {
            queryWrapper.ne(BdWaresBonus::getPkId, waresBonusParam.getPkId());
        }
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertOrUpdateBdWaresBonusByParam(BdWaresBonusParam bdWaresBonusParam, Long userId) {
        // 主表
        BdWaresBonus bdWaresBonus = BeanUtil.copyProperties(bdWaresBonusParam, BdWaresBonus.class);
        if (bdWaresBonus.getPkId() == null) {
            bdWaresBonus.setPkCreator(userId);
            baseMapper.insert(bdWaresBonus);
        } else {
            bdWaresBonus.setPkModified(userId);
            baseMapper.updateById(bdWaresBonus);
        }
        // 明细
        List<BdWaresBonusItems> bdWaresBonusItems = BeanUtil.copyToList(bdWaresBonusParam.getWaresBonusItemsParamList(), BdWaresBonusItems.class);
        // 分类分为有id和没有id的
        List<BdWaresBonusItems> insertWaresBonusItems = new ArrayList<>();
        List<BdWaresBonusItems> updateWaresBonusItems = new ArrayList<>();
        Date currentDate = DateUtils.currentDateTime();
        for (BdWaresBonusItems bdWaresBonusItem : bdWaresBonusItems) {
            bdWaresBonusItem.setPkWaresBonus(bdWaresBonus.getPkId());
            bdWaresBonusItem.setPkCountry(bdWaresBonus.getPkCountry());
            if (bdWaresBonusItem.getPkId() == null) {
                bdWaresBonusItem.setPkCreator(userId);
                insertWaresBonusItems.add(bdWaresBonusItem);
            } else {
                bdWaresBonusItem.setPkModified(userId);
                bdWaresBonusItem.setModifiedTime(currentDate);
                updateWaresBonusItems.add(bdWaresBonusItem);
            }
        }
        if (updateWaresBonusItems.size() > 0) {
            // 删除
            iBdWaresBonusItemsService.deleteWaresBonusItems(updateWaresBonusItems, updateWaresBonusItems.get(0).getPkWaresBonus(), userId);
            iBdWaresBonusItemsService.updateWaresBonusItems(updateWaresBonusItems);
        }
        // 新增
        if (insertWaresBonusItems.size() > 0) {
            iBdWaresBonusItemsService.insetWaresBonusItems(insertWaresBonusItems);
        }
    }

    @Override
    public List<BdWaresBonusExt> querySpecialWaresBonus() {
        return baseMapper.queryAllSpecialWaresBonus();
    }
}
