package com.hzs.sale.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SaOrderFieldConstants;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EApproveRechargeStatus;
import com.hzs.common.core.utils.CommonUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.sale.ext.SaOrderSelfLogExt;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.common.domain.sale.order.SaOrderSelfLog;
import com.hzs.sale.order.mapper.SaOrderSelfLogMapper;
import com.hzs.sale.order.param.SelfPickUpParam;
import com.hzs.sale.order.service.ISaOrderSelfLogService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 订单自提申请记录 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-04-19
 */
@Service
public class SaOrderSelfLogServiceImpl extends ServiceImpl<SaOrderSelfLogMapper, SaOrderSelfLog> implements ISaOrderSelfLogService {

    @Override
    public void applySelfPickUpSaOrder(SaOrder saOrder, SelfPickUpParam selfPickUpVO) {
        // 本批订单修改记录编号
        selfPickUpVO.setSelfCode(CommonUtil.createSelfCode());
        SaOrderSelfLog saOrderSelfLog = SaOrderSelfLog.builder()
                .pkOrder(saOrder.getPkId())
                .selfCode(selfPickUpVO.getSelfCode())
                .phone(selfPickUpVO.getPhone())
                .approveStatus(EApproveRechargeStatus.SUBMIT.getValue())
                .remark(selfPickUpVO.getRemark())
                .build();
        saOrderSelfLog.setCreationTime(DateUtils.currentDateTime());
        saOrderSelfLog.setPkCreator(selfPickUpVO.getPkCreator());
        saOrderSelfLog.setPkCountry(selfPickUpVO.getPkSettleCountry());
        save(saOrderSelfLog);
    }

    @Override
    public List<SaOrderSelfLogExt> querySaOrderSelfLog(SelfPickUpParam selfPickUpVO) {
        // 默认查询已申请、审核中
        return baseMapper.querySaOrderSelfLog(selfPickUpVO);
    }

    @Override
    public SaOrderSelfLog querySaOrderSelfLogById(Long pkId) {
        return baseMapper.selectById(pkId);
    }

    @Override
    public List<SaOrderSelfLogExt> queryMemberSaOrderSelfLog(SelfPickUpParam selfPickUpVO) {
        return baseMapper.queryMemberSaOrderSelfLog(selfPickUpVO);
    }

    @Override
    public void updateSaOrderSelfLog(SelfPickUpParam selfPickUpVO, Long pkId) {
        UpdateWrapper<SaOrderSelfLog> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, DateUtils.currentDate());
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, selfPickUpVO.getPkCreator());
        updateWrapper.set(SystemFieldConstants.APPROVE_STATUS, selfPickUpVO.getApproveStatus());
        if(StringUtils.isNotEmpty(selfPickUpVO.getRejectMsg())){
            updateWrapper.set("REJECT_MSG", selfPickUpVO.getRejectMsg());
        }
        updateWrapper.eq(SystemFieldConstants.PK_ID, pkId);
        update(updateWrapper);
    }
}
