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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.gree.mall.manager.bean.material.stock.WorkerStockDTO;
import com.gree.mall.manager.enums.material.DirectFlagEnum;
import com.gree.mall.manager.enums.material.PartsAttrEnum;
import com.gree.mall.manager.enums.material.WebsitGoodsTypeEnum;
import com.gree.mall.manager.enums.material.WorkerStockFlagEnum;
import com.gree.mall.manager.plus.entity.WebsitSales;
import com.gree.mall.manager.plus.entity.WebsitSalesItem;
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;

@Slf4j
@Service
public class WorkSalesStockLogic {

    @Resource
    MaterialGoodsStockLogic materialGoodsStockLogic;

    public void salesInStock(WebsitSales sales, List<WebsitSalesItem> salesItems, String refType) throws Exception {
        List<WorkerStockDTO> workerStockDTOS = new ArrayList<>();
        for (WebsitSalesItem item : salesItems) {
            // 类型为辅材并且师傅辅材编号为空时跳过本次
            if (sales.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey())
                    && StringUtils.isBlank(item.getWorkerGoodsId())) {
                continue;
            }
            WorkerStockDTO workerStockDTO = new WorkerStockDTO();
            workerStockDTO.setCompanyWechatId(sales.getCompanyWechatId());
            workerStockDTO.setCompanyWechatName(sales.getCompanyWechatName());
            workerStockDTO.setIdentity(sales.getIdentity());
            workerStockDTO.setWorkerId(sales.getWorkerId());
            workerStockDTO.setWorkerName(sales.getWorkerName());
            workerStockDTO.setWebsitId(sales.getWebsitId());
            workerStockDTO.setWebsitName(sales.getWebsitName());
            workerStockDTO.setSdate(sales.getCreateTime());
            workerStockDTO.setWorkerWebsitId(sales.getWorkerWebsitId());
            workerStockDTO.setWorkerWebsitName(sales.getWorkerWebsitName());
            if (sales.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey())) {
                workerStockDTO.setWebsitGoodsId(item.getGoodsId());
                workerStockDTO.setWebsitGoodsName(item.getGoodsName());
                workerStockDTO.setGoodsSalesConvertQty(item.getGoodsSalesConvertQty());
                workerStockDTO.setGoodsSpecification(item.getGoodsSpecification());
                workerStockDTO.setWebsitGoodsQty(item.getSalesQty());
                workerStockDTO.setWebsitGoodsPrice(item.getPrice());
                workerStockDTO.setGoodsId(item.getWorkerGoodsId());
                workerStockDTO.setGoodsName(item.getWorkerGoodsName());
            } else {
                workerStockDTO.setGoodsId(item.getGoodsId());
                workerStockDTO.setGoodsName(item.getGoodsName());
            }
            workerStockDTO.setGoodsType(item.getGoodsType());
            workerStockDTO.setChangeQty(item.getConvertQty());
            workerStockDTO.setPrice(item.getConvertPrice());
            workerStockDTO.setGoodsStockUnit(item.getGoodsSalesUnit());
            workerStockDTO.setRef(sales.getSalesId());
            workerStockDTO.setRefType(refType);
            workerStockDTO.setPartsAttr(PartsAttrEnum.NEW.getKey());
            workerStockDTO.setDirectFlag(DirectFlagEnum.ADD.getKey());
            workerStockDTO.setFlag(WorkerStockFlagEnum.TAKE.getKey());
            workerStockDTO.setObj("师傅");
            workerStockDTO.setWorkerOrderNo("");
            workerStockDTO.setRemark(sales.getRemark());
            workerStockDTO.setOperateBy(sales.getWorkerName());
            workerStockDTO.setOperateTime(DateUtil.date());
            workerStockDTOS.add(workerStockDTO);
        }
        if (CollectionUtil.isNotEmpty(workerStockDTOS)) {
            // 开始处理库存
            materialGoodsStockLogic.handleWorkerStock(workerStockDTOS);
        }
    }

}
