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.EDelFlag;
import com.hzs.common.core.enums.EOrderPrefix;
import com.hzs.common.domain.member.empty.CuMemberEmpty;
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.mapper.CuMemberEmptyMapper;
import com.hzs.member.empty.param.MemberEmptyQueryParam;
import com.hzs.member.empty.param.MemberEmptyRegisterQueryParam;
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.Date;
import java.util.List;

/**
 * 空单批次 服务实现类
 *
 * @author hzs
 * @since 2023-06-16
 */
@Service
public class CuMemberEmptyServiceImpl extends ServiceImpl<CuMemberEmptyMapper, CuMemberEmpty> implements ICuMemberEmptyService {

    @Autowired
    private ICuMemberEmptyDetailService iCuMemberEmptyDetailService;
    @Autowired
    private ICuMemberEmptyProductService iCuMemberEmptyProductService;

    @Override
    public List<CuMemberEmpty> queryList(MemberEmptyQueryParam param, Integer pkCountry, Integer systemType) {
        return baseMapper.queryList(param, pkCountry, systemType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteData(List<Long> pkIdList, Long userId, Integer pkCountry) {
        // 当前时间
        Date nowDate = new Date();

        // 删除空单主表
        LambdaUpdateWrapper<CuMemberEmpty> emptyUpdateWrapper = new LambdaUpdateWrapper<>();
        emptyUpdateWrapper.in(CuMemberEmpty::getPkId, pkIdList);
        emptyUpdateWrapper.eq(CuMemberEmpty::getPkCountry, pkCountry);
        emptyUpdateWrapper.set(CuMemberEmpty::getDelFlag, EDelFlag.DELETE.getValue());
        emptyUpdateWrapper.set(CuMemberEmpty::getPkModified, userId);
        emptyUpdateWrapper.set(CuMemberEmpty::getModifiedTime, nowDate);
        this.update(emptyUpdateWrapper);

        // 删除空单明细表
        LambdaUpdateWrapper<CuMemberEmptyDetail> emptyDetailUpdateWrapper = new LambdaUpdateWrapper<>();
        emptyDetailUpdateWrapper.in(CuMemberEmptyDetail::getPkMemberEmpty, pkIdList);
        emptyDetailUpdateWrapper.set(CuMemberEmptyDetail::getDelFlag, EDelFlag.DELETE.getValue());
        emptyDetailUpdateWrapper.set(CuMemberEmptyDetail::getPkModified, userId);
        emptyDetailUpdateWrapper.set(CuMemberEmptyDetail::getModifiedTime, nowDate);
        iCuMemberEmptyDetailService.update(emptyDetailUpdateWrapper);

        // 删除空单产品明细
        LambdaUpdateWrapper<CuMemberEmptyProduct> emptyProductUpdateWrapper = new LambdaUpdateWrapper<>();
        emptyProductUpdateWrapper.in(CuMemberEmptyProduct::getPkMemberEmpty, pkIdList);
        emptyProductUpdateWrapper.set(CuMemberEmptyProduct::getDelFlag, EDelFlag.DELETE.getValue());
        emptyProductUpdateWrapper.set(CuMemberEmptyProduct::getPkModified, userId);
        emptyProductUpdateWrapper.set(CuMemberEmptyProduct::getModifiedTime, nowDate);
        iCuMemberEmptyProductService.update(emptyProductUpdateWrapper);
    }

    @Override
    public List<CuMemberEmptyDetailExt> emptyRegisterList(MemberEmptyRegisterQueryParam param, Integer pkCountry) {
        return baseMapper.emptyRegisterList(param, pkCountry);
    }

    @Override
    public String queryLastCode() {
        String code = baseMapper.queryLastCode();
        if (null == code) {
            code = EOrderPrefix.HANDLE_BUSINESS.getValue() + "10000";
        } else {
            code = EOrderPrefix.HANDLE_BUSINESS.getValue() + (Integer.parseInt(code.replaceAll(EOrderPrefix.HANDLE_BUSINESS.getValue(), "")) + 1);
        }
        return code;
    }
}
