package cn.newfeifan.mall.sale.service.merchantapplylog;

import cn.newfeifan.mall.module.system.dal.dataobject.user.AdminUserDO;
import cn.newfeifan.mall.module.system.service.user.AdminUserService;
import cn.newfeifan.mall.sale.controller.admin.merchantapplylog.vo.MerchantApplyLogPageReqVO;
import cn.newfeifan.mall.sale.controller.admin.merchantapplylog.vo.MerchantApplyLogRespVO;
import cn.newfeifan.mall.sale.controller.admin.merchantapplylog.vo.MerchantApplyLogSaveReqVO;
import cn.newfeifan.mall.sale.dal.dataobject.merchant.MerchantDO;
import cn.newfeifan.mall.sale.dal.dataobject.merchantapply.MerchantApplyDO;
import cn.newfeifan.mall.sale.dal.dataobject.merchantapplylog.MerchantApplyLogDO;
import cn.newfeifan.mall.sale.dal.mysql.merchantapplylog.MerchantApplyLogMapper;
import cn.newfeifan.mall.sale.service.merchant.MerchantService;
import cn.newfeifan.mall.sale.service.merchantapply.MerchantApplyService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.newfeifan.mall.module.enums.ErrorCodeConstants.MERCHANT_APPLY_LOG_NOT_EXISTS;

/**
 * 商户审核记录 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class MerchantApplyLogServiceImpl implements MerchantApplyLogService {

    @Resource
    private MerchantApplyLogMapper merchantApplyLogMapper;

    @Resource
    private MerchantService merchantService;

    @Resource
    private MerchantApplyService merchantApplyService;

    @Resource
    AdminUserService adminUserService;

    @Override
    public Long createMerchantApplyLog(MerchantApplyLogSaveReqVO createReqVO) {
        // 插入
        MerchantApplyLogDO merchantApplyLog = BeanUtils.toBean(createReqVO, MerchantApplyLogDO.class);

        // 设置审核人
        merchantApplyLog.setCheckSystemUserId(getLoginUserId());

        merchantApplyLogMapper.insert(merchantApplyLog);
        // 返回
        return merchantApplyLog.getId();
    }

    @Override
    public void updateMerchantApplyLog(MerchantApplyLogSaveReqVO updateReqVO) {
        // 校验存在
        validateMerchantApplyLogExists(updateReqVO.getId());
        // 更新
        MerchantApplyLogDO updateObj = BeanUtils.toBean(updateReqVO, MerchantApplyLogDO.class);
        merchantApplyLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteMerchantApplyLog(Long id) {
        // 校验存在
        validateMerchantApplyLogExists(id);
        // 删除
        merchantApplyLogMapper.deleteById(id);
    }

    private void validateMerchantApplyLogExists(Long id) {
        if (merchantApplyLogMapper.selectById(id) == null) {
            throw exception(MERCHANT_APPLY_LOG_NOT_EXISTS);
        }
    }

    @Override
    public MerchantApplyLogDO getMerchantApplyLog(Long id) {
        return merchantApplyLogMapper.selectById(id);
    }

    @Override
    public PageResult<MerchantApplyLogRespVO> getMerchantApplyLogPage(MerchantApplyLogPageReqVO pageReqVO) {

        if(pageReqVO.getMerchantApplyId() == null){
            MerchantDO merchant = merchantService.getMerchant(pageReqVO.getMerchantId());

            MerchantApplyDO merchantApply = merchantApplyService.getMerchantApply(merchant.getMerchantApplyId());

            if (merchantApply == null) {
                return PageResult.empty();
            }

            pageReqVO.setMerchantApplyId(merchantApply.getId());
        }

        PageResult<MerchantApplyLogDO> result = merchantApplyLogMapper.selectPage(pageReqVO);
        PageResult<MerchantApplyLogRespVO> bean = BeanUtils.toBean(result, MerchantApplyLogRespVO.class);

        for (MerchantApplyLogRespVO respVO : bean.getList()) {
            AdminUserDO user = adminUserService.getUser(respVO.getCheckSystemUserId());
            respVO.setCheckName(user.getNickname());
        }

        return bean;
    }

}