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.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SystemFieldConstants;
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)) {
                // 处理明细删除数据
                UpdateWrapper<CuMemberSwingNetDetail> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("PK_SWING_NET", cuMemberSwingNet.getPkId());
                updateWrapper.notIn(SystemFieldConstants.PK_ID, detailIdList);
                updateWrapper.eq(SystemFieldConstants.DEL_FLAG, EDelFlag.UN_DELETE.getValue());
                updateWrapper.set(SystemFieldConstants.DEL_FLAG, EDelFlag.DELETE.getValue());
                updateWrapper.set(SystemFieldConstants.PK_MODIFIED, pkCreator);
                updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, 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();

        UpdateWrapper<CuMemberSwingNet> swingNetWrapper = new UpdateWrapper<>();
        swingNetWrapper.eq(SystemFieldConstants.PK_ID, pkId);
        swingNetWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        swingNetWrapper.set(SystemFieldConstants.MODIFIED_TIME, nowDate);
        swingNetWrapper.set(SystemFieldConstants.DEL_FLAG, EDelFlag.DELETE.getValue());
        this.update(swingNetWrapper);

        UpdateWrapper<CuMemberSwingNetDetail> swingNetDetailWrapper = new UpdateWrapper<>();
        swingNetDetailWrapper.eq("PK_SWING_NET", pkId);
        swingNetDetailWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        swingNetDetailWrapper.set(SystemFieldConstants.MODIFIED_TIME, nowDate);
        swingNetDetailWrapper.set(SystemFieldConstants.DEL_FLAG, EDelFlag.DELETE.getValue());
        iCuMemberSwingNetDetailService.update(swingNetDetailWrapper);
    }

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