package com.ruoyi.ss.mchApply.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.mchApply.domain.MchApply;
import com.ruoyi.ss.mchApply.domain.MchApplyQuery;
import com.ruoyi.ss.mchApply.domain.MchApplyVO;
import com.ruoyi.ss.mchApply.domain.enums.MchApplyStatus;
import com.ruoyi.ss.mchApply.mapper.MchApplyMapper;
import com.ruoyi.ss.mchApply.service.IMchApplyService;
import com.ruoyi.ss.user.service.ISmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

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

/**
 * 商家合作申请Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-28
 */
@Service
public class MchApplyServiceImpl implements IMchApplyService
{
    @Autowired
    private MchApplyMapper mchApplyMapper;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private ISmUserService userService;

    /**
     * 查询商家合作申请
     *
     * @param applyId 商家合作申请主键
     * @return 商家合作申请
     */
    @Override
    public MchApplyVO selectMchApplyByApplyId(Long applyId)
    {
        return mchApplyMapper.selectMchApplyByApplyId(applyId);
    }

    /**
     * 查询商家合作申请列表
     *
     * @param mchApply 商家合作申请
     * @return 商家合作申请
     */
    @Override
    public List<MchApplyVO> selectMchApplyList(MchApplyQuery mchApply)
    {
        return mchApplyMapper.selectMchApplyList(mchApply);
    }

    /**
     * 新增商家合作申请
     *
     * @param mchApply 商家合作申请
     * @return 结果
     */
    @Override
    public int insertMchApply(MchApply mchApply)
    {
        mchApply.setCreateTime(DateUtils.getNowDate());
        mchApply.setCreateBy(SecurityUtils.getUsername());
        return mchApplyMapper.insertMchApply(mchApply);
    }

    /**
     * 修改商家合作申请
     *
     * @param mchApply 商家合作申请
     * @return 结果
     */
    @Override
    public int updateMchApply(MchApply mchApply)
    {
        mchApply.setUpdateTime(DateUtils.getNowDate());
        return mchApplyMapper.updateMchApply(mchApply);
    }

    /**
     * 批量删除商家合作申请
     *
     * @param applyIds 需要删除的商家合作申请主键
     * @return 结果
     */
    @Override
    public int deleteMchApplyByApplyIds(Long[] applyIds)
    {
        return mchApplyMapper.deleteMchApplyByApplyIds(applyIds);
    }

    /**
     * 删除商家合作申请信息
     *
     * @param applyId 商家合作申请主键
     * @return 结果
     */
    @Override
    public int deleteMchApplyByApplyId(Long applyId)
    {
        return mchApplyMapper.deleteMchApplyByApplyId(applyId);
    }

    /**
     * 审核
     *
     * @param applyId 申请id
     * @param pass    是否通过
     * @param remark  审核意见
     */
    @Override
    public boolean verify(Long applyId, Boolean pass, String remark) {
        ServiceUtil.assertion(applyId == null, "id不允许为空");
        ServiceUtil.assertion(pass == null, "是否通过不允许为空");
        ServiceUtil.assertion(remark == null, "审核意见不允许为空");

        // 查询申请数据
        MchApplyVO dbApply = selectMchApplyByApplyId(applyId);
        ServiceUtil.assertion(dbApply == null, "申请不存在");

        // 修改内容
        MchApply apply = new MchApply();
        apply.setVerifyBy(SecurityUtils.getUserId());
        apply.setVerifyTime(LocalDateTime.now());
        apply.setStatus(MchApplyStatus.getStatusByPass(pass));
        apply.setVerifyRemark(remark);

        // 修改条件
        MchApplyQuery condition = new MchApplyQuery();
        condition.setApplyId(applyId);
        condition.setStatus(MchApplyStatus.APPROVING.getStatus());

        transactionTemplate.execute(status -> {
            // 修改申请状态
            int i = mchApplyMapper.updateWithCondition(apply, condition);
            ServiceUtil.assertion(i != 1, "当前申请状态已发生变更，请刷新后重试");

            return Boolean.TRUE;
        });

        return true;
    }

    /**
     * 查询数量
     *
     * @param query 查询条件
     * @return 数量
     */
    @Override
    public int selectCount(MchApplyQuery query) {
        return mchApplyMapper.selectCount(query);
    }
}
