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

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EDealerYesNo;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.domain.member.empty.CuMemberEmpty;
import com.hzs.common.domain.member.empty.CuMemberEmptyCode;
import com.hzs.common.domain.member.empty.CuMemberEmptyDetail;
import com.hzs.common.domain.member.empty.CuMemberEmptyProduct;
import com.hzs.common.domain.member.empty.ext.CuMemberEmptyDetailExt;
import com.hzs.member.empty.dto.MemberEmptyDetailDTO;
import com.hzs.member.empty.mapper.CuMemberEmptyDetailMapper;
import com.hzs.member.empty.param.MemberEmptyAddParam;
import com.hzs.member.empty.service.ICuMemberEmptyCodeService;
import com.hzs.member.empty.service.ICuMemberEmptyDetailService;
import com.hzs.member.empty.service.ICuMemberEmptyProductService;
import com.hzs.member.empty.service.ICuMemberEmptyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 空单批次明细 服务实现类
 *
 * @author hzs
 * @since 2023-06-16
 */
@Service
public class CuMemberEmptyDetailServiceImpl extends ServiceImpl<CuMemberEmptyDetailMapper, CuMemberEmptyDetail> implements ICuMemberEmptyDetailService {

    @Autowired
    private ICuMemberEmptyService iCuMemberEmptyService;
    @Autowired
    private ICuMemberEmptyCodeService iCuMemberEmptyCodeService;
    @Autowired
    private ICuMemberEmptyProductService iCuMemberEmptyProductService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CuMemberEmptyDetailExt addData(MemberEmptyAddParam param, Long userId, Integer pkCountry, Integer systemType) {
        // 生成会员编号
        List<String> memberCodeList = iCuMemberEmptyCodeService.createCodeBatch(1, pkCountry, userId, systemType);

        // 空单批次ID
        Long pkMemberEmpty = param.getPkMemberEmpty();
        if (null == pkMemberEmpty) {
            // 空单批次为空，需要新建批次数据
            CuMemberEmpty cuMemberEmpty = new CuMemberEmpty();
            cuMemberEmpty.setBusinessCode(iCuMemberEmptyService.queryLastCode());
            cuMemberEmpty.setPkCreator(userId);
            cuMemberEmpty.setPkCountry(pkCountry);
            cuMemberEmpty.setSystemType(systemType);
            iCuMemberEmptyService.save(cuMemberEmpty);

            pkMemberEmpty = cuMemberEmpty.getPkId();
        }

        // 插入空单明细
        CuMemberEmptyDetail cuMemberEmptyDetail = new CuMemberEmptyDetail();
        cuMemberEmptyDetail.setPkMemberEmpty(pkMemberEmpty);
        cuMemberEmptyDetail.setMemberCode(memberCodeList.get(0));
        cuMemberEmptyDetail.setPkCreator(userId);
        cuMemberEmptyDetail.setPkCountry(pkCountry);
        cuMemberEmptyDetail.setPkSettleCountry(pkCountry);
        cuMemberEmptyDetail.setAutoFlag(EDealerYesNo.YES.getValue());
        if (this.save(cuMemberEmptyDetail)) {
            return baseMapper.queryEmptyDetail(Collections.singletonList(cuMemberEmptyDetail.getPkId())).get(0);
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delData(List<CuMemberEmptyDetail> emptyDetailList, Long userId, Integer pkCountry) {
        Date nowDate = new Date();

        // 空单明细ID列表
        List<Long> detailIdList = emptyDetailList.stream().map(CuMemberEmptyDetail::getPkId).collect(Collectors.toList());
        // 空单明细会员编号列表
        List<String> memberCodeList = emptyDetailList.stream().map(CuMemberEmptyDetail::getMemberCode).collect(Collectors.toList());

        // 删除空单会员编号
        LambdaUpdateWrapper<CuMemberEmptyCode> codeUpdateWrapper = new LambdaUpdateWrapper<>();
        codeUpdateWrapper.in(CuMemberEmptyCode::getMemberCode, memberCodeList);
        codeUpdateWrapper.eq(CuMemberEmptyCode::getDelFlag, EDelFlag.UN_DELETE.getValue());
        codeUpdateWrapper.set(CuMemberEmptyCode::getDelFlag, EDelFlag.DELETE.getValue());
        codeUpdateWrapper.set(CuMemberEmptyCode::getPkModified, userId);
        codeUpdateWrapper.set(CuMemberEmptyCode::getModifiedTime, nowDate);
        iCuMemberEmptyCodeService.update(codeUpdateWrapper);

        // 删除空单明细产品
        LambdaUpdateWrapper<CuMemberEmptyProduct> productUpdateWrapper = new LambdaUpdateWrapper<>();
        productUpdateWrapper.in(CuMemberEmptyProduct::getPkMemberEmptyDetail, detailIdList);
        productUpdateWrapper.eq(CuMemberEmptyProduct::getDelFlag, EDelFlag.UN_DELETE.getValue());
        productUpdateWrapper.set(CuMemberEmptyProduct::getDelFlag, EDelFlag.DELETE.getValue());
        productUpdateWrapper.set(CuMemberEmptyProduct::getPkModified, userId);
        productUpdateWrapper.set(CuMemberEmptyProduct::getModifiedTime, nowDate);
        iCuMemberEmptyProductService.update(productUpdateWrapper);

        // 删除空单明细
        LambdaUpdateWrapper<CuMemberEmptyDetail> detailUpdateWrapper = new LambdaUpdateWrapper<>();
        detailUpdateWrapper.in(CuMemberEmptyDetail::getPkId, detailIdList);
        detailUpdateWrapper.eq(CuMemberEmptyDetail::getDelFlag, EDelFlag.UN_DELETE.getValue());
        detailUpdateWrapper.set(CuMemberEmptyDetail::getDelFlag, EDelFlag.DELETE.getValue());
        detailUpdateWrapper.set(CuMemberEmptyDetail::getPkModified, userId);
        detailUpdateWrapper.set(CuMemberEmptyDetail::getModifiedTime, nowDate);
        this.update(detailUpdateWrapper);
    }

    @Override
    public List<Long> querySeqList(Integer param) {
        return baseMapper.querySeqList(param);
    }

    @Override
    public int insertBatch(List<CuMemberEmptyDetail> detailList, Long userId) {
        return baseMapper.insertBatch(detailList, userId);
    }

    @Override
    public int updateBatch(List<CuMemberEmptyDetail> detailList, Long userId) {
        return baseMapper.updateBatch(detailList, userId);
    }

    @Override
    public int updateAbnormalBatch(List<MemberEmptyDetailDTO> paramList, Long userId) {
        return baseMapper.updateAbnormalBatch(paramList, userId);
    }

    @Override
    public List<CuMemberEmptyDetailExt> queryPlaceParentTop(Long pkMemberEmpty) {
        return baseMapper.queryPlaceParentTop(pkMemberEmpty);
    }

    @Override
    public List<CuMemberEmptyDetailExt> queryParentTop(Long pkMemberEmpty) {
        return baseMapper.queryParentTop(pkMemberEmpty);
    }

    @Override
    public List<CuMemberEmptyDetailExt> queryPlaceParentTopAndProduct(Long pkMemberEmpty) {
        return baseMapper.queryPlaceParentTopAndProduct(pkMemberEmpty);
    }

    @Override
    public List<CuMemberEmptyDetailExt> queryPlaceParentTree(Long pkId, Long pkMemberEmpty) {
        return baseMapper.queryPlaceParentTree(pkId, pkMemberEmpty);
    }

    @Override
    public List<CuMemberEmptyDetailExt> queryParentTree(Long pkId, Long pkMemberEmpty) {
        return baseMapper.queryParentTree(pkId, pkMemberEmpty);
    }

    @Override
    public List<CuMemberEmptyDetailExt> queryPlaceParentTreeAndProduct(Long pkId, Long pkMemberEmpty) {
        return baseMapper.queryPlaceParentTreeAndProduct(pkId, pkMemberEmpty);
    }

    @Override
    public List<CuMemberEmptyDetailExt> queryEmptyDetailList(Long pkMemberEmpty) {
        return baseMapper.queryEmptyDetailList(pkMemberEmpty);
    }

    @Override
    public List<CuMemberEmptyDetailExt> queryDetailAndProductList(Long pkMemberEmpty) {
        return baseMapper.queryDetailAndProductList(pkMemberEmpty);
    }

    @Override
    public List<CuMemberEmptyDetailExt> queryEmptyDetail(List<Long> detailIdList) {
        return baseMapper.queryEmptyDetail(detailIdList);
    }

}
