package com.yc.ship.module.ota.service.distributoraudit;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.lang.Asserts;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.ota.controller.admin.distributoraudit.vo.DistributorAuditPageReqVO;
import com.yc.ship.module.ota.controller.admin.distributoraudit.vo.DistributorAuditSaveReqVO;
import com.yc.ship.module.ota.dal.dataobject.distributoraudit.DistributorAuditDO;
import com.yc.ship.module.ota.dal.mysql.distributoraudit.DistributorAuditMapper;
import com.yc.ship.module.ota.service.distributor.DistributorService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static com.yc.ship.module.ota.enums.ErrorCodeConstants.DISTRIBUTOR_AUDIT_NOT_EXISTS;


/**
 * 分销商授信提额/授信充值/余额充值 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DistributorAuditServiceImpl implements DistributorAuditService {


    @Resource
    private DistributorAuditMapper distributorAuditMapper;

    @Resource
    private DistributorService distributorService;

    @Override
    public Long createDistributorAudit(DistributorAuditSaveReqVO createReqVO) {
        // 插入
        DistributorAuditDO distributorAudit = BeanUtils.toBean(createReqVO, DistributorAuditDO.class);
        LocalDateTime now = LocalDateTime.now();
        distributorAudit.setId(IdWorker.getId(distributorAudit));
        distributorAudit.setStatus(0);
        distributorAudit.setOperateType(3);
        distributorAudit.setApplyTime(now);
        long loginUserId = getLoginUserId();
        distributorAudit.setApplyUser(loginUserId);
        distributorAuditMapper.insert(distributorAudit);
        // 返回
        return distributorAudit.getId();
    }

    @Override
    public Long updateDistributorAudit(DistributorAuditSaveReqVO updateReqVO) {
        // 校验存在
//        validateDistributorAuditExists(updateReqVO.getId());
        DistributorAuditDO distributorAuditDO = distributorAuditMapper.selectById(updateReqVO.getId());
        // 更新
        long loginUserId = getLoginUserId();
        Asserts.isTrue(distributorAuditDO.getStatus() == 0,"当前状态不能审核");
        DistributorAuditDO updateObj = BeanUtils.toBean(updateReqVO, DistributorAuditDO.class);
        LocalDateTime now = LocalDateTime.now();
        updateObj.setAuditUser(loginUserId);
        updateObj.setAuditTime(now);
        Long logId = null;
        if(updateReqVO.getStatus()==1){
            // 1.余额，2.授信充正，3.授信提额
            int operateType = updateReqVO.getOperateType();
            int type = operateType == 1 ? 1 : operateType == 2 ? 2 : 99;
            logId = distributorService.updateDistributorCard(updateReqVO.getDistributorId(), updateReqVO.getOperateMode(), distributorAuditDO.getAmount(), loginUserId, null, null);
            updateObj.setRechargeLogId(logId.toString());
        }
        // 更新
        distributorAuditMapper.updateById(updateObj);
        return logId;
    }

    @Override
    public void deleteDistributorAudit(Long id) {
        // 校验存在
        validateDistributorAuditExists(id);
        // 删除
        distributorAuditMapper.deleteById(id);
    }

    private void validateDistributorAuditExists(Long id) {
        if (distributorAuditMapper.selectById(id) == null) {
            throw exception(DISTRIBUTOR_AUDIT_NOT_EXISTS);
        }
    }

    @Override
    public DistributorAuditDO getDistributorAudit(Long id) {
        return distributorAuditMapper.selectById(id);
    }

    @Override
    public PageResult<DistributorAuditDO> getDistributorAuditPage(DistributorAuditPageReqVO pageReqVO) {
        return distributorAuditMapper.selectPage(pageReqVO);
    }


}
