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

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.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) {
        return baseMapper.queryList(param, pkCountry);
    }

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

        // 删除空单主表
        UpdateWrapper<CuMemberEmpty> emptyUpdateWrapper = new UpdateWrapper<>();
        emptyUpdateWrapper.in(SystemFieldConstants.PK_ID, pkIdList);
        emptyUpdateWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        emptyUpdateWrapper.set(SystemFieldConstants.DEL_FLAG, EDelFlag.DELETE.getValue());
        emptyUpdateWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        emptyUpdateWrapper.set(SystemFieldConstants.MODIFIED_TIME, nowDate);
        this.update(emptyUpdateWrapper);

        // 删除空单明细表
        UpdateWrapper<CuMemberEmptyDetail> emptyDetailUpdateWrapper = new UpdateWrapper<>();
        emptyDetailUpdateWrapper.in("PK_MEMBER_EMPTY", pkIdList);
        emptyDetailUpdateWrapper.set(SystemFieldConstants.DEL_FLAG, EDelFlag.DELETE.getValue());
        emptyDetailUpdateWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        emptyDetailUpdateWrapper.set(SystemFieldConstants.MODIFIED_TIME, nowDate);
        iCuMemberEmptyDetailService.update(emptyDetailUpdateWrapper);

        // 删除空单产品明细
        UpdateWrapper<CuMemberEmptyProduct> emptyProductUpdateWrapper = new UpdateWrapper<>();
        emptyProductUpdateWrapper.in("PK_MEMBER_EMPTY", pkIdList);
        emptyProductUpdateWrapper.set(SystemFieldConstants.DEL_FLAG, EDelFlag.DELETE.getValue());
        emptyProductUpdateWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        emptyProductUpdateWrapper.set(SystemFieldConstants.MODIFIED_TIME, 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;
    }
}
