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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.swing.CuMemberSwingNet;
import com.hzs.common.domain.member.swing.ext.CuMemberSwingNetExt;
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.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;

/**
 * <p>
 * 会员网体甩网配置 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-08-30
 */
@Service
public class CuMemberSwingNetServiceImpl extends ServiceImpl<CuMemberSwingNetMapper, CuMemberSwingNet> implements ICuMemberSwingNetService {

    @Autowired
    private ICuMemberSwingNetDetailService cuMemberSwingNetDetailService;

    @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.setPkCreator(pkCreator);
            cuMemberSwingNet.setPkCountry(pkCountry);
            cuMemberSwingNet.setCreationTime(dateTime);
            save(cuMemberSwingNet);
        }
        for (CuMemberSwingNetDetailVO cuMemberSwingNetDetailVO : cuMemberSwingNetParam.getCuMemberSwingNetDetailExtList()) {
            cuMemberSwingNetDetailVO.setPkSwingNet(cuMemberSwingNet.getPkId());
            cuMemberSwingNetDetailVO.setPkCreator(pkCreator);
            cuMemberSwingNetDetailVO.setPkCountry(pkCountry);
            cuMemberSwingNetDetailVO.setCreationTime(dateTime);
        }
        cuMemberSwingNetDetailService.mergeCuMemberSwingNetDetail(cuMemberSwingNetParam.getCuMemberSwingNetDetailExtList());
    }

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