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.Storage;
import com.gree.mall.manager.plus.entity.WebsitSales;
import com.gree.mall.manager.plus.entity.WebsitSalesItem;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class WebitSalesStockLogic {

    @Resource
    MaterialGoodsStockLogic materialGoodsStockLogic;
    @Resource
    StorageService storageService;


    public void newSalesStock(WebsitSales sales, List<WebsitSalesItem> salesItems, String refType) throws Exception {
        List<WebsitStockDTO> shopStockDTOS = new ArrayList<>();
        for (WebsitSalesItem salesItem : salesItems) {
            // 出库注值
            WebsitStockDTO websitStockDTO = new WebsitStockDTO();
            websitStockDTO.setCompanyWechatId(salesItem.getCompanyWechatId());
            websitStockDTO.setCompanyWechatName(sales.getCompanyWechatName());
            websitStockDTO.setWebsitId(salesItem.getWebsitId());
            websitStockDTO.setWebsitName(salesItem.getWebsitName());
            websitStockDTO.setSdate(sales.getCreateTime());
            websitStockDTO.setPartsAttr(PartsAttrEnum.NEW.toString());
            websitStockDTO.setGoodsId(salesItem.getGoodsId());
            websitStockDTO.setGoodsName(salesItem.getGoodsName());
            websitStockDTO.setGoodsType(salesItem.getGoodsType());
            websitStockDTO.setPrice(salesItem.getPrice());
            websitStockDTO.setRef(salesItem.getSalesId());
            websitStockDTO.setRefType(refType);
            websitStockDTO.setChangeQty(salesItem.getSalesQty());
            websitStockDTO.setDirectFlag(DirectFlagEnum.SUB.getKey());
            websitStockDTO.setRemark(sales.getRemark());
            websitStockDTO.setOperateBy(sales.getConfirmBy());
            websitStockDTO.setOperateTime(sales.getConfirmTime());
            websitStockDTO.setObj(StringUtils.isBlank(sales.getWorkerId()) ? "顾客" : "师傅");
            websitStockDTO.setStorageId(sales.getStorageId());
            shopStockDTOS.add(websitStockDTO);
        }

        // 开始处理库存
        materialGoodsStockLogic.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") && 1 == 2) {
//            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;
    }
}
