package com.fzm.mall.server.admin.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.SystemClock;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.goods_center.mapper.SkuMapper;
import com.fzm.mall.server.admin.goods_center.model.Sku;
import com.fzm.mall.server.admin.system.entity.dto.*;
import com.fzm.mall.server.admin.system.mapper.SysBlockChainPassMapper;
import com.fzm.mall.server.admin.system.model.SysBlockChainManagement;
import com.fzm.mall.server.admin.system.model.SysBlockChainPass;
import com.fzm.mall.server.admin.system.model.SysBlockChainPass;
import com.fzm.mall.server.admin.system.service.ISysBlockChainPassService;
import com.fzm.mall.server.admin.util.CopyUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 系统中心-区块链管理-通证 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-08-12
 */
@Service
@Transactional
public class SysBlockChainPassServiceImpl extends ServiceImpl<SysBlockChainPassMapper, SysBlockChainPass> implements ISysBlockChainPassService {
    
    @Autowired
    private SysBlockChainPassMapper passMapper;

    @Autowired
    private SkuMapper skuMapper;

    @Override
    public void save(SysBlockChainPassSaveDTO saveDTO) {
        SysBlockChainPass pass = CopyUtil.to(saveDTO, SysBlockChainPass.class);
        checkUnique(pass.getBlockChainId(), pass.getPassIdentification());

        pass.init();
        passMapper.insert(pass);
    }

    @Override
    public void update(SysBlockChainPassUpdateDTO updateDTO) {
        SysBlockChainPass pass = CopyUtil.to(updateDTO, SysBlockChainPass.class);
        checkUnique(pass.getBlockChainId(), pass.getPassIdentification());

        UpdateWrapper<SysBlockChainPass> wrapper = new UpdateWrapper<>();
        wrapper.eq("pass_id", pass.getPassId())
                .eq("update_time", pass.getUpdateTime());
        int update = passMapper.update(pass, wrapper);
        if (update == 0) {
            throw new MyException(MallResponseError.UPDATE_FAIL);
        }
    }

    private void checkUnique(String blockChainId, String passIdentification) {
        QueryWrapper<SysBlockChainPass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("block_chain_id", blockChainId)
                .eq("pass_identification", passIdentification)
                .eq("status", SysBlockChainPass.PASS_STATUS_EFFECTIVE);
        SysBlockChainPass pass = passMapper.selectOne(queryWrapper);
        if (pass != null) {
            throw new MyException(MallResponseError.SAVE_DUPLICATE);
        }
    }

    @Override
    public void validStateUpdate(String passId, Integer validState, Long updateTime) {
        UpdateWrapper<SysBlockChainPass> wrapper = new UpdateWrapper<>();
        wrapper.eq("pass_id", passId)
                .eq("update_time", updateTime)
                .set("update_time", SystemClock.now())
                .set("status", validState);
        int update = passMapper.update(null, wrapper);
        if (update == 0) {
            throw new MyException(MallResponseError.UPDATE_FAIL);
        }
    }

    @Override
    public void delete(String passId) {
        QueryWrapper<SysBlockChainPass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pass_id", passId);
        SysBlockChainPass pass = passMapper.selectOne(queryWrapper);

        checkPassBeforeDelete(pass);

        UpdateWrapper<SysBlockChainPass> wrapper = new UpdateWrapper<>();
        wrapper.eq("pass_id", passId)
                .set("status", SysBlockChainPass.PASS_STATUS_DELETED);
        passMapper.update(null, wrapper);
    }

    @Override
    public SysBlockChainPassDTO read(String blockChainId) {
        QueryWrapper<SysBlockChainPass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pass_id", blockChainId);
        SysBlockChainPass pass = passMapper.selectOne(queryWrapper);
        return CopyUtil.to(pass, SysBlockChainPassDTO.class);
    }

    @Override
    public IPage<SysBlockChainPassDTO> page(Page<SysBlockChainPassDTO> page, String blockChainId, String keyword) {
        return passMapper.page(page, blockChainId, keyword);
    }

    private void checkPassBeforeDelete(SysBlockChainPass pass) {
        if (pass == null) {
            return;
        }
        /**
         * 只有停用才可被删除
         */
        if (pass.getStatus() != SysBlockChainPass.PASS_STATUS_DISABLED) {
            throw new MyException(MallResponseError.DELETE_FAIL);
        }
    }
}
