package com.izouma.nineth.service;

import com.izouma.nineth.domain.*;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.enums.CollectionSource;
import com.izouma.nineth.enums.CollectionStatus;
import com.izouma.nineth.enums.CollectionType;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.*;
import com.izouma.nineth.utils.JpaUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class CompanyCollectionService {

    private CompanyCollectionRepo   companyCollectionRepo;
    private CollectionRepo          collectionRepo;
    private UserRepo                userRepo;
    private CollectionPrivilegeRepo collectionPrivilegeRepo;
    private SysConfigService        sysConfigService;

    public Page<CompanyCollection> all(PageQuery pageQuery) {
        return companyCollectionRepo.findAll(JpaUtils.toSpecification(pageQuery, CompanyCollection.class), JpaUtils.toPageRequest(pageQuery));
    }

    public void audit(Long id, CollectionStatus status, String reason) {
        CompanyCollection companyCollection = companyCollectionRepo.findById(id)
                .orElseThrow(new BusinessException("无申请"));


        if (!CollectionStatus.PENDING.equals(companyCollection.getStatus())) {
            throw new BusinessException("已审核");
        }

        companyCollection.setStatus(status);
        companyCollection.setReason(reason);

        if (CollectionStatus.FAIL.equals(status)) {
            //存状态
            companyCollectionRepo.save(companyCollection);
            return;
        }

        CollectionPrivilege privilege = collectionPrivilegeRepo.findByCompanyCollectionId(id);
        if (ObjectUtils.isNotEmpty(privilege)) {
            return;
        }

        Collection collection = new Collection();
        BeanUtils.copyProperties(companyCollection, collection);
        collection.setSource(CollectionSource.COMPANY);
        collection.setType(CollectionType.DEFAULT);
        collection.setStock(companyCollection.getTotal());

        //铸造者/持有者
        Long userId = companyCollection.getUserId();
        User user = userRepo.findById(userId).orElseThrow(new BusinessException("无企业"));
        collection.setMinter(user.getNickname());
        collection.setMinterId(userId);
        collection.setMinterAvatar(user.getAvatar());
        collection.setOwner(user.getNickname());
        collection.setOwnerId(userId);
        collection.setOwnerAvatar(user.getAvatar());

//        Showroom showroom = showroomRepo.findFirstByUserId(userId);
        int royalties = sysConfigService.getInt("royalties");
        int serviceCharge = sysConfigService.getInt("service_charge");
        collection.setRoyalties(royalties);
        collection.setServiceCharge(serviceCharge);
        collection.setId(null);
        collection = collectionRepo.save(collection);

        //企业属性
        collectionPrivilegeRepo.save(CollectionPrivilege.builder()
                .collectionId(collection.getId())
                .companyCollectionId(id)
                .build());

        //存状态
        companyCollectionRepo.save(companyCollection);
    }

}
