package com.hzs.member.swing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.swing.CuMemberSwingNet;
import com.hzs.common.domain.member.swing.CuMemberSwingNetDetail;
import com.hzs.common.domain.member.swing.ext.CuMemberSwingNetExt;
import com.hzs.common.domain.member.swing.ext.CuMemberSwingNetLogExt;
import com.hzs.member.swing.mapper.CuMemberSwingNetMapper;
import com.hzs.member.swing.param.CuMemberSwingNetDetailVO;
import com.hzs.member.swing.param.CuMemberSwingNetParam;
import com.hzs.member.swing.param.SwingNetLogParam;
import com.hzs.member.swing.service.ICuMemberSwingNetDetailService;
import com.hzs.member.swing.service.ICuMemberSwingNetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 会员网体甩网配置 服务实现类
 */
@Service
public class CuMemberSwingNetServiceImpl extends ServiceImpl<CuMemberSwingNetMapper, CuMemberSwingNet> implements ICuMemberSwingNetService {

    @Autowired
    private ICuMemberSwingNetDetailService iCuMemberSwingNetDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCuMemberSingNet(CuMemberSwingNetParam cuMemberSwingNetParam, Integer pkCountry, Long pkCreator) {
        // 修改
        CuMemberSwingNet cuMemberSwingNet = BeanUtil.copyProperties(cuMemberSwingNetParam, CuMemberSwingNet.class);
        Date dateTime = DateUtils.currentDateTime();
        if (cuMemberSwingNetParam.getPkId() != null) {
            cuMemberSwingNet.setPkModified(pkCreator);
            cuMemberSwingNet.setModifiedTime(dateTime);
            baseMapper.updateById(cuMemberSwingNet);
        } else {
            cuMemberSwingNet.setPkCountry(pkCountry);
            cuMemberSwingNet.setPkCreator(pkCreator);
            cuMemberSwingNet.setCreationTime(dateTime);
            save(cuMemberSwingNet);
        }

        List<CuMemberSwingNetDetailVO> detailList = cuMemberSwingNetParam.getCuMemberSwingNetDetailExtList();
        for (CuMemberSwingNetDetailVO cuMemberSwingNetDetailVO : detailList) {
            cuMemberSwingNetDetailVO.setPkSwingNet(cuMemberSwingNet.getPkId());
            cuMemberSwingNetDetailVO.setPkCountry(pkCountry);
            cuMemberSwingNetDetailVO.setPkCreator(pkCreator);
            cuMemberSwingNetDetailVO.setCreationTime(dateTime);
        }

        if (null != cuMemberSwingNet.getPkId()) {
            List<Long> detailIdList = detailList.stream().map(CuMemberSwingNetDetailVO::getPkId).filter(Objects::nonNull).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(detailIdList)) {
                // 处理明细删除数据
                LambdaUpdateWrapper<CuMemberSwingNetDetail> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(CuMemberSwingNetDetail::getPkSwingNet, cuMemberSwingNet.getPkId());
                updateWrapper.notIn(CuMemberSwingNetDetail::getPkId, detailIdList);
                updateWrapper.eq(CuMemberSwingNetDetail::getDelFlag, EDelFlag.UN_DELETE.getValue());
                updateWrapper.set(CuMemberSwingNetDetail::getDelFlag, EDelFlag.DELETE.getValue());
                updateWrapper.set(CuMemberSwingNetDetail::getPkModified, pkCreator);
                updateWrapper.set(CuMemberSwingNetDetail::getModifiedTime, dateTime);
                iCuMemberSwingNetDetailService.update(updateWrapper);
            }
        }

        iCuMemberSwingNetDetailService.mergeCuMemberSwingNetDetail(detailList);
    }

    @Override
    public List<CuMemberSwingNetExt> queryCuMemberSwingNetExt(CuMemberSwingNetParam cuMemberSwingNetParam) {
        return baseMapper.queryCuMemberSwingNetExt(cuMemberSwingNetParam);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteNet(Long pkId, Long userId) {
        Date nowDate = new Date();

        LambdaUpdateWrapper<CuMemberSwingNet> swingNetWrapper = new LambdaUpdateWrapper<>();
        swingNetWrapper.eq(CuMemberSwingNet::getPkId, pkId);
        swingNetWrapper.set(CuMemberSwingNet::getPkModified, userId);
        swingNetWrapper.set(CuMemberSwingNet::getModifiedTime, nowDate);
        swingNetWrapper.set(CuMemberSwingNet::getDelFlag, EDelFlag.DELETE.getValue());
        this.update(swingNetWrapper);

        LambdaUpdateWrapper<CuMemberSwingNetDetail> swingNetDetailWrapper = new LambdaUpdateWrapper<>();
        swingNetDetailWrapper.eq(CuMemberSwingNetDetail::getPkSwingNet, pkId);
        swingNetDetailWrapper.set(CuMemberSwingNetDetail::getPkModified, userId);
        swingNetDetailWrapper.set(CuMemberSwingNetDetail::getModifiedTime, nowDate);
        swingNetDetailWrapper.set(CuMemberSwingNetDetail::getDelFlag, EDelFlag.DELETE.getValue());
        iCuMemberSwingNetDetailService.update(swingNetDetailWrapper);
    }

    @Override
    public List<CuMemberSwingNetLogExt> swingNetLogList(SwingNetLogParam param) {
        return baseMapper.swingNetLogList(param);
    }
}
