package com.izouma.nineth.service;

import com.izouma.nineth.domain.ActivityPriorityDocument;
import com.izouma.nineth.domain.Asset;
import com.izouma.nineth.domain.UserWareHouse;
import com.izouma.nineth.enums.ItemType;
import com.izouma.nineth.repo.UserRepo;
import com.izouma.nineth.repo.UserWareHouseRepo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class ActivityPriorityDocumentService {
    private UserWareHouseRepo userWareHouseRepo;
    private UserRepo userRepo;

    /**
     * 重置，修改优先购
     *
     * @param assets
     * @param record true 重置，否则修改（次数不可修改）
     */
    public void resetAllUserWareHouseLsit(List<Asset> assets, ActivityPriorityDocument record, boolean reset) {
        if (assets.size() == 0) return;
        List<UserWareHouse> userWareHouseLsit = new ArrayList<>();
        for (Asset e : assets) {
            //查询更改
            List<UserWareHouse> userWareHouse = userWareHouseRepo.findByAssetIdandActivityPriorityDocumentId(e.getId(), record.getId());
            if (userWareHouse != null && userWareHouse.size() > 0) {
                for (UserWareHouse wareHouse : userWareHouse) {
                    if (reset) {
                        //重置废掉之前的 然后重新生成一份
                        wareHouse.setDel(true);
                        wareHouse.setDisabled(false);
                    } else {
                        wareHouse.setTargetCollectionIdList(record.getTargetCollectionIdList());
                        wareHouse.setTargetCollectionId(record.getTargetCollectionId());
                        wareHouse.setAdvancePurchaseTime(record.getAdvancePurchaseTime());
                        wareHouse.setItemId(record.ItemId);
                        wareHouse.setMinterId(record.getMinterId());
                        wareHouse.setItemName(ItemType.getValue(record.ItemId));
                        wareHouse.setText(record.getText());
                        wareHouse.setOverdueTime(record.getOverdueTime());
                    }
                    userWareHouseLsit.add(wareHouse);
                }
            }
        }
        userWareHouseRepo.saveAll(userWareHouseLsit);
        if (reset) {
            saveAllUserWareHouseLsit(assets, record);
        }
    }

    /**
     * 删除优先购
     *
     * @param assets
     */
    public void delAllUserWareHouseLsit(List<Asset> assets, Long id) {
        if (assets.size() == 0) return;
        List<UserWareHouse> userWareHouseLsit = new ArrayList<>();
        for (Asset e : assets) {
            //查询更改
            List<UserWareHouse> userWareHouse = userWareHouseRepo.findByAssetIdandActivityPriorityDocumentId(e.getId(), id);
            if (userWareHouse != null && userWareHouse.size() > 0) {
                for (UserWareHouse wareHouse : userWareHouse) {
                    //重置废掉
                    wareHouse.setDel(true);
                    wareHouse.setDisabled(false);
                    userWareHouseLsit.add(wareHouse);
                }
            }
        }
        userWareHouseRepo.saveAll(userWareHouseLsit);
    }

    /**
     * 添加优先购的时候给所有持有改资产的玩家发放一个优先购凭证
     *
     * @param assets
     * @param record
     */
    public void saveAllUserWareHouseLsit(List<Asset> assets, ActivityPriorityDocument record) {
        if (assets.size() == 0) return;
        List<UserWareHouse> userWareHouseLsit = new ArrayList<>();
        for (Asset e : assets) {
            userWareHouseLsit.add(UserWareHouse.builder()
                    .assetId(e.getId())
                    .userId(e.getUserId())
                    .projectId(e.getProjectId())
                    .disabled(true)
                    .itemId(record.ItemId)
                    .itemName(ItemType.getValue(record.ItemId))
                    .text(record.getText())
                    .phone(userRepo.findPhone(e.getUserId()))
                    .minterId(record.getMinterId())
                    .advancePurchaseCount(record.getAdvancePurchaseCount())
                    .advancePurchaseTime(record.getAdvancePurchaseTime())
                    .targetCollectionId(record.getTargetCollectionId())
                    .activityPriorityDocumentId(record.getId())
                    .overdueTime(record.getOverdueTime())
                    .targetCollectionIdList(record.getTargetCollectionIdList()).build());
        }
        userWareHouseRepo.saveAll(userWareHouseLsit);
    }
}
