
package com.tmerclub.cloud.biz.service.impl;

import com.tmerclub.cloud.api.auth.feign.AccountFeignClient;
import com.tmerclub.cloud.biz.mapper.AttachFileGroupMapper;
import com.tmerclub.cloud.biz.mapper.AttachFileMapper;
import com.tmerclub.cloud.biz.model.AttachFileGroup;
import com.tmerclub.cloud.biz.service.AttachFileGroupService;
import com.tmerclub.cloud.biz.vo.AttachFileGroupVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.security.AuthUserContext;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author YXF
 * @date 2020-12-04 16:15:02
 */
@Service
public class AttachFileGroupServiceImpl implements AttachFileGroupService {

    @Autowired
    private AttachFileMapper attachFileMapper;
    @Autowired
    private AttachFileGroupMapper attachFileGroupMapper;

    @DubboReference
    private AccountFeignClient accountFeignClient;

    @Override
    public List<AttachFileGroupVO> list(Integer type) {
        if (Objects.equals(AuthUserContext.get().getTenantId(), Constant.DEFAULT_SHOP_ID)) {
            return attachFileGroupMapper.list(null, AuthUserContext.get().getSysType(), AuthUserContext.getAccountUid(), type);
        }
        return attachFileGroupMapper.list(AuthUserContext.get().getTenantId(), AuthUserContext.get().getSysType(), null, type);
    }

    @Override
    public AttachFileGroupVO getByAttachFileGroupId(Long attachFileGroupId) {
        return attachFileGroupMapper.getByAttachFileGroupId(attachFileGroupId);
    }

    @Override
    public void save(AttachFileGroup attachFileGroup) {
        attachFileGroup.setShopId(AuthUserContext.get().getTenantId());
        this.checkGroupNameIsRepeat(attachFileGroup.getName(), attachFileGroup.getShopId(), attachFileGroup.getSysType(), attachFileGroup.getType(), null);
        attachFileGroup.setUid(AuthUserContext.getAccountUid());
        attachFileGroupMapper.save(attachFileGroup);
    }

    @Override
    public void update(AttachFileGroup attachFileGroup) {
        this.checkGroupNameIsRepeat(attachFileGroup.getName(), attachFileGroup.getShopId(), attachFileGroup.getSysType(), attachFileGroup.getType(), attachFileGroup.getAttachFileGroupId());
        attachFileGroup.setShopId(AuthUserContext.get().getTenantId());
        attachFileGroupMapper.update(attachFileGroup);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long attachFileGroupId) {
        attachFileGroupMapper.deleteById(attachFileGroupId);
        attachFileMapper.updateBatchByAttachFileGroupId(attachFileGroupId);
    }

    @Override
    public void updateShopIdByUid(Long shopId, Long uid) {
        attachFileGroupMapper.updateShopIdByUid(shopId, uid);
    }

    /**
     * 检查分组名是否重复
     *
     * @param name              分组名称
     * @param shopId            店铺id
     * @param sysType           系统类型
     * @param type              类型
     * @param attachFileGroupId 分组id
     */
    private void checkGroupNameIsRepeat(String name, Long shopId, Integer sysType, Integer type, Long attachFileGroupId) {
        int count = attachFileGroupMapper.countByNameAndShopId(name, shopId, sysType, type, attachFileGroupId);
        if (count > 0) {
            throw new LuckException("分组名称重复");
        }
    }
}
