package com.fs.store.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.StringUtils;
import com.fs.store.domain.FsStoreOrderAudit;
import com.fs.store.domain.FsStoreOrderAuditLog;
import com.fs.store.enums.OrderAuditStateEnum;
import com.fs.store.mapper.FsStoreOrderAuditMapper;
import com.fs.store.param.FsStoreOrderAuditParam;
import com.fs.store.param.FsStoreOrderAuditReviewParam;
import com.fs.store.service.IFsStoreOrderAuditLogService;
import com.fs.store.service.IFsStoreOrderAuditService;
import com.fs.store.vo.FsStoreOrderAuditVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class FsStoreOrderAuditServiceImpl extends ServiceImpl<FsStoreOrderAuditMapper, FsStoreOrderAudit> implements IFsStoreOrderAuditService {

    private final IFsStoreOrderAuditLogService fsStoreOrderAuditLogService;

    /**
     * 查询订单审核列表
     * @param param 参数
     * @return  list
     */
    @Override
    public List<FsStoreOrderAuditVO> selectStoreOrderAuditVOList(FsStoreOrderAuditParam param) {
        return baseMapper.selectStoreOrderAuditVOList(param);
    }

    /**
     * 审核
     *
     * @param param   参数
     * @param userId  用户ID
     * @param isAdmin 是否管理端
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void audit(FsStoreOrderAuditReviewParam param, Long userId, boolean isAdmin) {
        FsStoreOrderAudit fsStoreOrderAudit = baseMapper.selectById(param.getAuditId());
        if (Objects.isNull(fsStoreOrderAudit)) {
            throw new CustomException("记录不存在");
        }
        OrderAuditStateEnum orderAuditStateEnum = OrderAuditStateEnum.getOrderAuditStateEnum(fsStoreOrderAudit.getAuditStatus());
        if (isAdmin && OrderAuditStateEnum.ADMIN_PENDING != orderAuditStateEnum) {
            throw new CustomException("请勿重复审核");
        }
        if (!isAdmin && OrderAuditStateEnum.COMPANY_PENDING != orderAuditStateEnum) {
            throw new CustomException("请勿重复审核");
        }

        LocalDateTime now = LocalDateTime.now();
        OrderAuditStateEnum newStatus;
        String message;
        if (param.getReviewType() == 1) { // 审核通过
            newStatus = isAdmin ? OrderAuditStateEnum.APPROVED : OrderAuditStateEnum.ADMIN_PENDING;
            message = isAdmin ? "总后台:审核通过" : "销售:审核通过";
        } else { // 审核拒绝
            newStatus = isAdmin ? OrderAuditStateEnum.ADMIN_REJECT : OrderAuditStateEnum.COMPANY_REJECT;
            message = isAdmin ? "总后台:审核拒绝" : "销售:审核拒绝";
            if (StringUtils.isBlank(param.getReviewContent())) {
                throw new CustomException("被拒原因不能为空");
            }
            fsStoreOrderAudit.setReason(param.getReviewContent());
        }

        if (isAdmin) {
            fsStoreOrderAudit.setAdminAuditTime(now);
            fsStoreOrderAudit.setAdminAuditUserId(userId);
        } else {
            fsStoreOrderAudit.setCompanyAuditTime(now);
            fsStoreOrderAudit.setCompanyAuditUserId(userId);
        }
        fsStoreOrderAudit.setAuditStatus(newStatus.getValue());

        baseMapper.updateById(fsStoreOrderAudit);

        FsStoreOrderAuditLog auditLog = new FsStoreOrderAuditLog();
        auditLog.setAuditId(fsStoreOrderAudit.getId());
        auditLog.setOrderId(fsStoreOrderAudit.getOrderId());
        auditLog.setContent(message);
        auditLog.setCreateTime(LocalDateTime.now());
        fsStoreOrderAuditLogService.save(auditLog);
    }
}
