package com.gree.mall.manager.logic.material.stock;

import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.material.stock.WebsitStockDTO;
import com.gree.mall.manager.enums.material.DirectFlagEnum;
import com.gree.mall.manager.enums.material.PartsAttrEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class WebitPurchaseStockLogic {

    @Resource
    WebsitGoodsStockLogic websitGoodsStockLogic;
    @Resource
    StorageService storageService;

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void newInStock(WebsitPurchase purchase, List<WebsitPurchaseItem> websitPurchaseItems, String refType) throws Exception {
        List<WebsitStockDTO> shopStockDTOS = new ArrayList<>();
        for (WebsitPurchaseItem websitPurchaseItem : websitPurchaseItems) {
            // 入库注值
            WebsitStockDTO websitStockDTO = new WebsitStockDTO();
            websitStockDTO.setCompanyWechatId(websitPurchaseItem.getCompanyWechatId());
            websitStockDTO.setCompanyWechatName(purchase.getCompanyWechatName());
            websitStockDTO.setWebsitId(websitPurchaseItem.getWebsitId());
            websitStockDTO.setWebsitName(purchase.getWebsitName());
            websitStockDTO.setSdate(purchase.getCreateTime());
            websitStockDTO.setPartsAttr(PartsAttrEnum.NEW.toString());
            websitStockDTO.setGoodsId(websitPurchaseItem.getGoodsId());
            websitStockDTO.setGoodsName(websitPurchaseItem.getGoodsName());
            websitStockDTO.setGoodsType(websitPurchaseItem.getGoodsType());
            websitStockDTO.setPrice(websitPurchaseItem.getCost());
            websitStockDTO.setRef(websitPurchaseItem.getPurchaseId());
            websitStockDTO.setRefType(refType);
            websitStockDTO.setChangeQty(websitPurchaseItem.getRecQty());
            websitStockDTO.setDirectFlag(DirectFlagEnum.ADD.getKey());
            websitStockDTO.setRemark(purchase.getRemark());
            websitStockDTO.setOperateBy(purchase.getConfirmBy());
            websitStockDTO.setOperateTime(purchase.getConfirmTime());
            websitStockDTO.setObj("供应商");
            websitStockDTO.setStorageId(purchase.getStorageId());
            shopStockDTOS.add(websitStockDTO);
        }

        // 开始处理库存
        websitGoodsStockLogic.handleWebsitStock(shopStockDTOS);
    }

    public void newRetStock(WebsitPurchaseRet purchaseRet, List<WebsitPurchaseRetItem> purchaseRetItems, String refType) throws Exception {
        List<WebsitStockDTO> shopStockDTOS = new ArrayList<>();
        for (WebsitPurchaseRetItem websitPurchaseItem : purchaseRetItems) {
            // 出库注值
            WebsitStockDTO websitStockDTO = new WebsitStockDTO();
            websitStockDTO.setCompanyWechatId(websitPurchaseItem.getCompanyWechatId());
            websitStockDTO.setCompanyWechatName(purchaseRet.getCompanyWechatName());
            websitStockDTO.setWebsitId(websitPurchaseItem.getWebsitId());
            websitStockDTO.setWebsitName(websitPurchaseItem.getWebsitName());
            websitStockDTO.setSdate(purchaseRet.getCreateTime());
            websitStockDTO.setPartsAttr(PartsAttrEnum.NEW.toString());
            websitStockDTO.setGoodsId(websitPurchaseItem.getGoodsId());
            websitStockDTO.setGoodsName(websitPurchaseItem.getGoodsName());
            websitStockDTO.setGoodsType(websitPurchaseItem.getGoodsType());
            websitStockDTO.setPrice(websitPurchaseItem.getCost());
            websitStockDTO.setRef(websitPurchaseItem.getPurchaseRetId());
            websitStockDTO.setRefType(refType);
            websitStockDTO.setChangeQty(websitPurchaseItem.getRetQty());
            websitStockDTO.setDirectFlag(DirectFlagEnum.SUB.getKey());
            websitStockDTO.setRemark(purchaseRet.getRemark());
            websitStockDTO.setOperateBy(purchaseRet.getConfirmBy());
            websitStockDTO.setOperateTime(purchaseRet.getConfirmTime());
            websitStockDTO.setObj("供应商");
            websitStockDTO.setStorageId(purchaseRet.getStorageId());
            shopStockDTOS.add(websitStockDTO);
        }

        // 开始处理库存
        websitGoodsStockLogic.handleWebsitStock(shopStockDTOS);
    }

    public Storage handleStorage(AdminUserCom adminUser, String websitId, String goodsType, String storageId) {
//        String storageType = goodsType.equals(WebsitGoodsTypeEnum.M.getKey()) ? WebsitGoodsTypeEnum.M.getRemark() : WebsitGoodsTypeEnum.P.getRemark();
//        if (adminUser.getAdminCompanyWechat().getJoinCode().equals("NO")) {
//            final Storage storage = storageService.lambdaQuery()
//                    .eq(Storage::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
//                    .eq(Storage::getIsDefault, true)
//                    .like(Storage::getType, storageType)
//                    .last("limit 1")
//                    .one();
//            if (Objects.isNull(storage)) {
//                throw new RemoteServiceException("未发现默认仓");
//            }
//
//            storageId = storage.getStorageId();
//        } else {
            if (StringUtils.isBlank(storageId)) {
                throw new RemoteServiceException("请选择仓位");
            }
//        }

        final Storage storage = storageService.lambdaQuery()
                .eq(Storage::getStorageId, storageId)
                .eq(Storage::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
                .eq(Storage::getWebsitId, websitId)
                .last("limit 1")
                .one();
        if (Objects.isNull(storage)) {
            throw new RemoteServiceException("仓位信息不存在");
        }

        if (!storage.getStatus()) {
            throw new RemoteServiceException("仓位信息已失效");
        }

        return storage;
    }
}
